PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/ValueRetrieverTests/EnumValueRetrieverTests.cs

http://github.com/techtalk/SpecFlow
C# | 158 lines | 139 code | 19 blank | 0 comment | 10 complexity | a13e3321d2c4a15f4f4d02d31a4cebfc MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using Xunit;
  3. using FluentAssertions;
  4. using TechTalk.SpecFlow.Assist.ValueRetrievers;
  5. using TechTalk.SpecFlow.RuntimeTests.AssistTests.ExampleEntities;
  6. namespace TechTalk.SpecFlow.RuntimeTests.AssistTests.ValueRetrieverTests
  7. {
  8. public class EnumValueRetrieverTests
  9. {
  10. [Fact]
  11. public void Throws_an_exception_when_the_value_is_not_an_enum()
  12. {
  13. var retriever = new EnumValueRetriever();
  14. var exceptionThrown = false;
  15. try
  16. {
  17. retriever.GetValue("NotDefinied", typeof (Sex));
  18. }
  19. catch (InvalidOperationException exception)
  20. {
  21. if (exception.Message == "No enum with value NotDefinied found")
  22. exceptionThrown = true;
  23. }
  24. exceptionThrown.Should().BeTrue();
  25. }
  26. [Fact]
  27. public void Should_return_the_value_when_it_matches_the_enum()
  28. {
  29. var retriever = new EnumValueRetriever();
  30. retriever.GetValue("Male", typeof (Sex)).Should().Be(Sex.Male);
  31. }
  32. [Fact]
  33. public void Should_return_the_value_when_it_includes_extra_spaces()
  34. {
  35. var retriever = new EnumValueRetriever();
  36. retriever.GetValue("Unknown Sex", typeof (Sex)).Should().Be(Sex.UnknownSex);
  37. }
  38. [Fact]
  39. public void Returns_the_value_regardless_of_proper_casing()
  40. {
  41. var retriever = new EnumValueRetriever();
  42. retriever.GetValue("feMale", typeof (Sex)).Should().Be(Sex.Female);
  43. }
  44. [Fact]
  45. public void Returns_the_proper_value_when_spaces_and_casing_is_wrong()
  46. {
  47. var retriever = new EnumValueRetriever();
  48. retriever.GetValue("unknown sex", typeof (Sex)).Should().Be(Sex.UnknownSex);
  49. }
  50. [Fact]
  51. public void Throws_an_exception_when_the_value_is_null()
  52. {
  53. var retriever = new EnumValueRetriever();
  54. var exceptionThrown = false;
  55. try
  56. {
  57. retriever.GetValue(null, typeof (Sex));
  58. }
  59. catch (InvalidOperationException exception)
  60. {
  61. if (exception.Message == "No enum with value {null} found")
  62. exceptionThrown = true;
  63. }
  64. exceptionThrown.Should().BeTrue();
  65. }
  66. [Fact]
  67. public void Throws_an_exception_when_the_value_is_empty()
  68. {
  69. var retriever = new EnumValueRetriever();
  70. var exceptionThrown = false;
  71. try
  72. {
  73. retriever.GetValue(string.Empty, typeof (Sex));
  74. }
  75. catch (InvalidOperationException exception)
  76. {
  77. if (exception.Message == "No enum with value {empty} found")
  78. exceptionThrown = true;
  79. }
  80. exceptionThrown.Should().BeTrue();
  81. }
  82. [Fact]
  83. public void Does_not_throw_an_exception_when_the_value_is_null_and_enum_type_is_not_nullable()
  84. {
  85. var retriever = new EnumValueRetriever();
  86. var exceptionThrown = false;
  87. try
  88. {
  89. retriever.GetValue(null, typeof (Sex?));
  90. }
  91. catch (InvalidOperationException exception)
  92. {
  93. if (exception.Message == "No enum with value {null} found")
  94. exceptionThrown = true;
  95. }
  96. exceptionThrown.Should().BeFalse();
  97. }
  98. [Fact]
  99. public void Does_not_throw_an_exception_when_the_value_is_empty_and_enum_type_is_not_nullable()
  100. {
  101. var retriever = new EnumValueRetriever();
  102. var exceptionThrown = false;
  103. try
  104. {
  105. retriever.GetValue(string.Empty, typeof (Sex));
  106. }
  107. catch (InvalidOperationException exception)
  108. {
  109. if (exception.Message == "No enum with value {empty} found")
  110. exceptionThrown = true;
  111. }
  112. exceptionThrown.Should().BeTrue();
  113. }
  114. [Fact]
  115. public void Should_return_the_value_when_it_matches_the_nullable_enum()
  116. {
  117. var retriever = new EnumValueRetriever();
  118. retriever.GetValue("Male", typeof (Sex?)).Should().Be(Sex.Male);
  119. }
  120. [Fact]
  121. public void Should_return_the_value_when_it_includes_extra_spaces_on_the_nullable_enum()
  122. {
  123. var retriever = new EnumValueRetriever();
  124. retriever.GetValue("Unknown Sex", typeof (Sex?)).Should().Be(Sex.UnknownSex);
  125. }
  126. [Fact]
  127. public void Returns_the_value_regardless_of_proper_casing_on_a_nullable_enum()
  128. {
  129. var retriever = new EnumValueRetriever();
  130. retriever.GetValue("feMale", typeof (Sex?)).Should().Be(Sex.Female);
  131. }
  132. [Fact]
  133. public void Returns_the_proper_value_when_spaces_and_casing_is_wrong_on_a_nullable_enum()
  134. {
  135. var retriever = new EnumValueRetriever();
  136. retriever.GetValue("unknown sex", typeof (Sex?)).Should().Be(Sex.UnknownSex);
  137. }
  138. }
  139. }