PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Faker.Tests/SelectorTests/StringSelectorTests.cs

http://github.com/Aaronontheweb/faker-csharp
C# | 256 lines | 202 code | 35 blank | 19 comment | 2 complexity | 56c43bc6b207770b1757e310d11ed4d5 MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Faker.Selectors;
  6. using FluentAssertions;
  7. using Xunit;
  8. namespace Faker.Tests.SelectorTests
  9. {
  10. public class StringSelectorTests
  11. {
  12. #region Test classes for use against our string selectors...
  13. private class FullNameTestClass
  14. {
  15. public string fullname { get; set; }
  16. public string fullName { get; set; }
  17. public string Fullname { get; set; }
  18. public string FullName { get; set; }
  19. public string Full_name { get; set; }
  20. public string Full_Name { get; set; }
  21. public string full_name { get; set; }
  22. public string full_Name { get; set; }
  23. public string name { get; set; }
  24. public string Name { get; set; }
  25. public string NAME { get; set; }
  26. }
  27. private class FirstNameTestClass
  28. {
  29. public string firstname { get; set; }
  30. public string firstName { get; set; }
  31. public string Firstname { get; set; }
  32. public string FirstName { get; set; }
  33. public string First_name { get; set; }
  34. public string First_Name { get; set; }
  35. public string first_name { get; set; }
  36. public string first_Name { get; set; }
  37. }
  38. private class LastNameTestClass
  39. {
  40. public string lastname { get; set; }
  41. public string lastName { get; set; }
  42. public string Lastname { get; set; }
  43. public string LastName { get; set; }
  44. public string Last_name { get; set; }
  45. public string Last_Name { get; set; }
  46. public string last_name { get; set; }
  47. public string last_Name { get; set; }
  48. }
  49. private class EmailTestClass
  50. {
  51. public string emailaddress { get; set; }
  52. public string email_address { get; set; }
  53. public string Email_address { get; set; }
  54. public string email_Address { get; set; }
  55. public string emailAddress { get; set; }
  56. public string EmailAddress { get; set; }
  57. public string Emailaddress { get; set; }
  58. public string email { get; set; }
  59. public string Email { get; set; }
  60. public string EMAIL { get; set; }
  61. }
  62. private class RandomStringsClass
  63. {
  64. public string string1 { get; set; }
  65. public string asdf4w5sfsdfsdf { get; set; }
  66. public string __sdfsf__ { get; set; }
  67. }
  68. #endregion
  69. [Fact(DisplayName = "Tests to see if our regex can match all of the variations of the FullName field")]
  70. public void Full_Name_Variations_All_Match()
  71. {
  72. var nameSelector = new FullNameSelector();
  73. var fullNameClass = new FullNameTestClass();
  74. //Iterate over all of the properties in the fullNameClass object...
  75. foreach(var property in fullNameClass.GetType().GetProperties())
  76. {
  77. var nameSelectorResult = nameSelector.CanBind(property);
  78. Assert.True(nameSelectorResult, string.Format("{0} should have been a valid match", property.Name));
  79. }
  80. }
  81. [Fact(DisplayName = "Tests to see if all of our field values are properly injected...")]
  82. public void Full_Name_Variations_All_Injected()
  83. {
  84. var nameSelector = new FullNameSelector();
  85. var fullNameClass = new FullNameTestClass();
  86. //Iterate over all of the properties in the fullNameClass object...
  87. foreach (var property in fullNameClass.GetType().GetProperties())
  88. {
  89. //Inject the value into the property
  90. nameSelector.Generate(fullNameClass, property);
  91. }
  92. //Iterate over all of the properties again
  93. foreach(var property in fullNameClass.GetType().GetProperties())
  94. {
  95. var fieldValue = property.GetValue(fullNameClass, null) as string;
  96. Assert.NotNull(fieldValue);
  97. fieldValue.Length.Should().BeGreaterThan(0);
  98. }
  99. }
  100. [Fact(DisplayName = "Tests to see if our regex can match all of the variations of the FullName field")]
  101. public void First_Name_Variations_All_Match()
  102. {
  103. var nameSelector = new FirstNameSelector();
  104. var firstNameTestClass = new FirstNameTestClass();
  105. //Iterate over all of the properties in the fullNameClass object...
  106. foreach (var property in firstNameTestClass.GetType().GetProperties())
  107. {
  108. var nameSelectorResult = nameSelector.CanBind(property);
  109. Assert.True(nameSelectorResult, string.Format("{0} should have been a valid match", property.Name));
  110. }
  111. }
  112. [Fact(DisplayName = "Tests to see if all of our field values are properly injected...")]
  113. public void First_Name_Variations_All_Injected()
  114. {
  115. var nameSelector = new FirstNameSelector();
  116. var firstNameTestClass = new FirstNameTestClass();
  117. //Iterate over all of the properties in the fullNameClass object...
  118. foreach (var property in firstNameTestClass.GetType().GetProperties())
  119. {
  120. //Inject the value into the property
  121. nameSelector.Generate(firstNameTestClass, property);
  122. }
  123. //Iterate over all of the properties again
  124. foreach (var property in firstNameTestClass.GetType().GetProperties())
  125. {
  126. var fieldValue = property.GetValue(firstNameTestClass, null) as string;
  127. Assert.NotNull(fieldValue);
  128. fieldValue.Length.Should().BeGreaterThan(0);
  129. }
  130. }
  131. [Fact(DisplayName = "Tests to see if our regex can match all of the variations of the FullName field")]
  132. public void Last_Name_Variations_All_Match()
  133. {
  134. var nameSelector = new LastNameSelector();
  135. var lastNameTestClass = new LastNameTestClass();
  136. //Iterate over all of the properties in the fullNameClass object...
  137. foreach (var property in lastNameTestClass.GetType().GetProperties())
  138. {
  139. var nameSelectorResult = nameSelector.CanBind(property);
  140. Assert.True(nameSelectorResult, string.Format("{0} should have been a valid match", property.Name));
  141. }
  142. }
  143. [Fact(DisplayName = "Tests to see if all of our field values are properly injected...")]
  144. public void Last_Name_Variations_All_Injected()
  145. {
  146. var nameSelector = new LastNameSelector();
  147. var lastNameTestClass = new LastNameTestClass();
  148. //Iterate over all of the properties in the fullNameClass object...
  149. foreach (var property in lastNameTestClass.GetType().GetProperties())
  150. {
  151. //Inject the value into the property
  152. nameSelector.Generate(lastNameTestClass, property);
  153. }
  154. //Iterate over all of the properties again
  155. foreach (var property in lastNameTestClass.GetType().GetProperties())
  156. {
  157. var fieldValue = property.GetValue(lastNameTestClass, null) as string;
  158. Assert.NotNull(fieldValue);
  159. fieldValue.Length.Should().BeGreaterThan(0);
  160. }
  161. }
  162. [Fact(DisplayName = "Tests to see if our regex can match all of the variations of the EmailAddress field")]
  163. public void Email_Address_Variations_All_Match()
  164. {
  165. var emailSelector = new EmailSelector();
  166. var emailTestClass = new EmailTestClass();
  167. //Iterate over all of the properties in the EmailTestClass object...
  168. foreach (var property in emailTestClass.GetType().GetProperties())
  169. {
  170. var emailSelectorResult = emailSelector.CanBind(property);
  171. Assert.True(emailSelectorResult, string.Format("{0} should have been a valid match", property.Name));
  172. }
  173. }
  174. [Fact(DisplayName = "Tests to see if all of our field values are properly injected...")]
  175. public void Email_Address_Variations_All_Injected()
  176. {
  177. var emailSelector = new EmailSelector();
  178. var emailTestClass = new EmailTestClass();
  179. //Iterate over all of the properties in the fullNameClass object...
  180. foreach (var property in emailTestClass.GetType().GetProperties())
  181. {
  182. //Inject the value into the property
  183. emailSelector.Generate(emailTestClass, property);
  184. }
  185. //Iterate over all of the properties again
  186. foreach (var property in emailTestClass.GetType().GetProperties())
  187. {
  188. var fieldValue = property.GetValue(emailTestClass, null) as string;
  189. Assert.NotNull(fieldValue);
  190. fieldValue.Length.Should().BeGreaterThan(0);
  191. }
  192. }
  193. [Fact(DisplayName = "Tests to see if all of our field values are properly injected...")]
  194. public void String_Selector_Injects_All_Strings()
  195. {
  196. var stringSelector = new StringSelector();
  197. var randomStringsClass = new RandomStringsClass();
  198. //Iterate over all of the properties in the fullNameClass object...
  199. foreach (var property in randomStringsClass.GetType().GetProperties())
  200. {
  201. //Inject the value into the property
  202. stringSelector.Generate(randomStringsClass, property);
  203. }
  204. //Iterate over all of the properties again
  205. foreach (var property in randomStringsClass.GetType().GetProperties())
  206. {
  207. var fieldValue = property.GetValue(randomStringsClass, null) as string;
  208. Assert.NotNull(fieldValue);
  209. fieldValue.Length.Should().BeGreaterThan(0);
  210. }
  211. }
  212. [Fact(DisplayName = "Test to ensure that a string selector can become nullable but still match to strings")]
  213. public void String_Selectors_must_be_able_to_become_nullable()
  214. {
  215. var stringSelector = new StringSelector();
  216. var nullable = stringSelector.Nullable();
  217. Assert.True(nullable.CanBind(typeof(string)));
  218. Assert.True(nullable.TargetType == typeof(string));
  219. }
  220. }
  221. }