EnumMapperTest.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using PetaPoco.Internal;
  2. using Shouldly;
  3. using System;
  4. using System.Collections.Generic;
  5. using Xunit;
  6. namespace PetaPoco.Tests.Unit.Core
  7. {
  8. public class EnumMapperTest
  9. {
  10. [Fact]
  11. public void Mapping_ExistingEnum_Ok()
  12. {
  13. var expected = FakeEnum.ExistingValue;
  14. var enumType = expected.GetType();
  15. var name = Enum.GetName(enumType, expected);
  16. var actual = EnumMapper.EnumFromString(enumType, name);
  17. actual.ShouldBe(expected);
  18. }
  19. [Fact]
  20. public void Mapping_NonExistingEnum_ThrowsMeaningfulException()
  21. {
  22. var enumType = typeof(FakeEnum);
  23. var nonExistentName = "ThisValueDoesNotExistOnAnyEnum";
  24. try
  25. {
  26. EnumMapper.EnumFromString(enumType, nonExistentName);
  27. }
  28. catch (KeyNotFoundException ex)
  29. {
  30. var message = ex.Message;
  31. message.ShouldContain(nonExistentName, "missing value to convert");
  32. message.ShouldContain(enumType.Name, "missing Enum to convert into");
  33. return;
  34. }
  35. Assert.False(true, "Expedted InvalidOperationException");
  36. }
  37. private enum FakeEnum
  38. {
  39. ExistingValue
  40. }
  41. }
  42. }