PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests.cs

http://github.com/techtalk/SpecFlow
C# | 352 lines | 305 code | 47 blank | 0 comment | 4 complexity | b952f3a45f8bb98a0b1aff42b0d8d8a4 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Threading;
  5. using FluentAssertions;
  6. using Xunit;
  7. using TechTalk.SpecFlow.Assist;
  8. using TechTalk.SpecFlow.RuntimeTests.AssistTests.ExampleEntities;
  9. namespace TechTalk.SpecFlow.RuntimeTests.AssistTests
  10. {
  11. public class RowExtensionMethodTests
  12. {
  13. public RowExtensionMethodTests()
  14. {
  15. Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
  16. }
  17. [Fact]
  18. public void GetString_should_return_the_string_value_from_the_row()
  19. {
  20. var table = new Table("Name");
  21. table.AddRow("John Galt");
  22. table.Rows.First()
  23. .GetString("Name").Should().Be("John Galt");
  24. }
  25. [Fact]
  26. public void GetString_should_return_null_if_the_value_is_not_defined()
  27. {
  28. var table = new Table("Name");
  29. table.AddRow("John Galt");
  30. table.Rows.First()
  31. .GetString("SomethingThatDoesNotExist").Should().Be(null);
  32. }
  33. [Fact]
  34. public void GetInt_should_return_the_int_from_the_row()
  35. {
  36. var table = new Table("Count");
  37. table.AddRow("3");
  38. table.Rows.First()
  39. .GetInt32("Count").Should().Be(3);
  40. }
  41. [Fact]
  42. public void GetInt_should_return_MinValue_when_the_value_is_not_defined()
  43. {
  44. var table = new Table("Count");
  45. table.AddRow("4");
  46. table.Rows.First()
  47. .GetInt32("SomethingThatDoesNotExist").Should().Be(int.MinValue);
  48. }
  49. [Fact]
  50. public void GetInt_should_return_MinValue_when_the_value_is_empty()
  51. {
  52. var table = new Table("Count");
  53. table.AddRow("");
  54. table.Rows.First()
  55. .GetInt32("Count").Should().Be(int.MinValue);
  56. }
  57. [Fact]
  58. public void GetDecimal_should_return_the_decimal_from_the_row()
  59. {
  60. var table = new Table("Amount");
  61. table.AddRow(4.01M.ToString());
  62. table.Rows.First()
  63. .GetDecimal("Amount").Should().Be(4.01M);
  64. }
  65. [Fact]
  66. public void GetDecimal_should_return_MinValue_when_the_value_is_not_defined()
  67. {
  68. var table = new Table("Amount");
  69. table.AddRow("4.01");
  70. table.Rows.First()
  71. .GetDecimal("SomethingThatDoesNotExist").Should().Be(decimal.MinValue);
  72. }
  73. [Fact]
  74. public void GetDecimal_should_return_MinValue_when_the_value_is_empty()
  75. {
  76. var table = new Table("Amount");
  77. table.AddRow("");
  78. table.Rows.First()
  79. .GetDecimal("Amount").Should().Be(decimal.MinValue);
  80. }
  81. [Fact]
  82. public void GetDateTime_should_return_the_datetime_from_the_row()
  83. {
  84. var table = new Table("Birthdate");
  85. table.AddRow("4/28/2009 21:02:03");
  86. table.Rows.First()
  87. .GetDateTime("Birthdate").Should().Be(new DateTime(2009, 4, 28, 21, 2, 3));
  88. }
  89. [Fact]
  90. public void GetDateTime_should_return_MinValue_when_the_value_is_not_defined()
  91. {
  92. var table = new Table("Birthdate");
  93. table.AddRow("4/28/2009 21:02:03");
  94. table.Rows.First()
  95. .GetDateTime("SomethingThatDoesNotExist").Should().Be(DateTime.MinValue);
  96. }
  97. [Fact]
  98. public void GetDateTime_should_return_MinValue_when_the_value_is_empty()
  99. {
  100. var table = new Table("Birthdate");
  101. table.AddRow("");
  102. table.Rows.First()
  103. .GetDateTime("Birthdate").Should().Be(DateTime.MinValue);
  104. }
  105. [Fact]
  106. public void GetBool_returns_true_when_the_value_is_true()
  107. {
  108. var table = new Table("IsNeat");
  109. table.AddRow("true");
  110. table.Rows.First()
  111. .GetBoolean("IsNeat").Should().BeTrue();
  112. }
  113. [Fact]
  114. public void GetBool_returns_false_when_the_value_is_false()
  115. {
  116. var table = new Table("IsNeat");
  117. table.AddRow("false");
  118. table.Rows.First()
  119. .GetBoolean("IsNeat").Should().BeFalse();
  120. }
  121. [Fact]
  122. public void GetBool_throws_an_exception_when_the_value_is_not_true_or_false()
  123. {
  124. var table = new Table("IsNeat");
  125. table.AddRow("is not true nor false");
  126. var exceptionThrown = false;
  127. try
  128. {
  129. table.Rows.First()
  130. .GetBoolean("IsNeat");
  131. }
  132. catch (InvalidCastException exception)
  133. {
  134. if (exception.Message == "You must use 'true' or 'false' when setting bools for IsNeat")
  135. exceptionThrown = true;
  136. }
  137. exceptionThrown.Should().BeTrue();
  138. }
  139. [Fact]
  140. public void GetBool_returns_false_when_the_value_is_empty()
  141. {
  142. var table = new Table("IsNeat");
  143. table.AddRow("");
  144. table.Rows.First()
  145. .GetBoolean("IsNeat").Should().BeFalse();
  146. }
  147. [Fact]
  148. public void GetBool_throws_an_exception_when_the_id_is_not_defined()
  149. {
  150. var table = new Table("IsNeat");
  151. table.AddRow("is not true nor false");
  152. var exceptionThrown = false;
  153. try
  154. {
  155. table.Rows.First()
  156. .GetBoolean("SomethingThatDoesNotExist");
  157. }
  158. catch (InvalidOperationException exception)
  159. {
  160. if (exception.Message == "SomethingThatDoesNotExist could not be found in the row.")
  161. exceptionThrown = true;
  162. }
  163. exceptionThrown.Should().BeTrue();
  164. }
  165. [Fact]
  166. public void GetDouble_should_return_the_double_from_the_row()
  167. {
  168. var table = new Table("Amount");
  169. table.AddRow(4.01M.ToString());
  170. table.Rows.First()
  171. .GetDouble("Amount").Should().Be(4.01);
  172. }
  173. [Fact]
  174. public void GetChar_should_return_the_character_from_the_row()
  175. {
  176. var table = new Table("Character");
  177. table.AddRow("M");
  178. table.Rows.First()
  179. .GetChar("Character").Should().Be('M');
  180. }
  181. [Fact]
  182. public void GetDouble_should_return_MinValue_when_the_value_is_not_defined()
  183. {
  184. var table = new Table("Amount");
  185. table.AddRow("4.01");
  186. table.Rows.First()
  187. .GetDouble("SomethingThatDoesNotExist").Should().Be(double.MinValue);
  188. }
  189. [Fact]
  190. public void GetDouble_should_return_MinValue_when_the_value_is_empty()
  191. {
  192. var table = new Table("Amount");
  193. table.AddRow("");
  194. table.Rows.First()
  195. .GetDouble("Amount").Should().Be(double.MinValue);
  196. }
  197. [Fact]
  198. public void GetGuid_should_return_guid_version_of_string()
  199. {
  200. var table = new Table("Guid");
  201. table.AddRow("285B31CC-C5C2-4630-A1C5-EE7431717C3F");
  202. table.Rows.First()
  203. .GetGuid("Guid").Should().Be(new Guid("285B31CC-C5C2-4630-A1C5-EE7431717C3F"));
  204. }
  205. [Fact]
  206. public void GetGuid_should_return_MinValue_when_the_value_is_not_defined()
  207. {
  208. var table = new Table("Guid");
  209. table.AddRow("285B31CC-C5C2-4630-A1C5-EE7431717C3F");
  210. table.Rows.First()
  211. .GetGuid("SomethingThatDoesNotExist").Should().Be(new Guid());
  212. }
  213. [Fact]
  214. public void GetGuid_should_return_MinValue_when_the_value_is_empty()
  215. {
  216. var table = new Table("GetGuid");
  217. table.AddRow("");
  218. table.Rows.First()
  219. .GetGuid("GetGuid").Should().Be(new Guid());
  220. }
  221. [Fact]
  222. public void GetSingle_should_return_the_single_from_the_row()
  223. {
  224. var table = new Table("Amount");
  225. table.AddRow(99.90F.ToString());
  226. table.Rows.First()
  227. .GetSingle("Amount").Should().Be(99.90F);
  228. }
  229. [Fact]
  230. public void GetSingle_should_return_MinValue_when_the_value_is_empty()
  231. {
  232. var table = new Table("Amount");
  233. table.AddRow("");
  234. table.Rows.First()
  235. .GetSingle("Amount").Should().Be(Single.MinValue);
  236. }
  237. [Fact]
  238. public void GetSingle_should_return_MinValue_When_the_value_is_not_defined()
  239. {
  240. var table = new Table("Amount");
  241. table.AddRow(11.11F.ToString());
  242. table.Rows.First()
  243. .GetSingle("SomethingThatDoesNotExist").Should().Be(Single.MinValue);
  244. }
  245. [Fact]
  246. public void GetEnumValue_should_return_the_enum_field_form_the_row()
  247. {
  248. var table = new Table("Enum");
  249. table.AddRow("Male");
  250. var firstRow = table.Rows.First();
  251. firstRow.GetEnumValue<Sex>("Enum").Should().Be(Sex.Male);
  252. }
  253. [Fact]
  254. public void GetEnumValue_should_throw_when_the_given_value_does_not_exist()
  255. {
  256. var table = new Table("Enum");
  257. table.AddRow("MemberDoesNotExist");
  258. var firstRow = table.Rows.First();
  259. Action act = () => firstRow.GetEnumValue<Sex>("Enum");
  260. act.Should().Throw<ArgumentException>();
  261. }
  262. [Fact]
  263. public void GetEnumValue_should_throw_when_the_given_value_does_not_match_case()
  264. {
  265. var table = new Table("Enum");
  266. table.AddRow("female");
  267. var firstRow = table.Rows.First();
  268. Action act = () => firstRow.GetEnumValue<Sex>("Enum");
  269. act.Should().Throw<ArgumentException>();
  270. }
  271. [Fact]
  272. public void GetSingle_should_throw_when_the_given_value_is_not_defined()
  273. {
  274. var table = new Table("Enum");
  275. table.AddRow("Female");
  276. var firstRow = table.Rows.First();
  277. Action act = () => firstRow.GetEnumValue<Sex>("SomethingThatDoesNotExist");
  278. act.Should().Throw<IndexOutOfRangeException>();
  279. }
  280. [Fact]
  281. public void Create_instance_fills_a_new_instance()
  282. {
  283. var expectedPerson = new Person() { FirstName = "Max", LastName = "Mustermann", BirthDate = new DateTime(1980, 3, 31) };
  284. var table = new Table("FirstName", "LastName", "BirthDate");
  285. table.AddRow(expectedPerson.FirstName, expectedPerson.LastName, expectedPerson.BirthDate.ToString("yyyy-MM-dd"));
  286. var row = table.Rows[0];
  287. var person = row.CreateInstance(() => new Person());
  288. person.FirstName.Should().Be(expectedPerson.FirstName);
  289. person.LastName.Should().Be(expectedPerson.LastName);
  290. person.BirthDate.Should().Be(expectedPerson.BirthDate);
  291. }
  292. [Fact]
  293. public void Create_instance_creates_a_new_instance()
  294. {
  295. var expectedPerson = new Person() { FirstName = "Max", LastName = "Mustermann", BirthDate = new DateTime(1978, 9, 27) };
  296. var table = new Table("FirstName", "LastName", "BirthDate");
  297. table.AddRow(expectedPerson.FirstName, expectedPerson.LastName, expectedPerson.BirthDate.ToString("yyyy-MM-dd"));
  298. var row = table.Rows[0];
  299. var person = row.CreateInstance<Person>();
  300. person.FirstName.Should().Be(expectedPerson.FirstName);
  301. person.LastName.Should().Be(expectedPerson.LastName);
  302. person.BirthDate.Should().Be(expectedPerson.BirthDate);
  303. }
  304. }
  305. }