PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Examples/Example.Random/ExampleSpecs.cs

https://github.com/machine/machine.specifications
C# | 340 lines | 273 code | 67 blank | 0 comment | 0 complexity | cafb05b6bd58ae1d3e839c13fcbbceca MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using FluentAssertions;
  5. using Machine.Specifications;
  6. namespace Example.Random
  7. {
  8. public static class tag
  9. {
  10. public const string example = "example";
  11. public const string some_other_tag = "some other tag";
  12. public const string one_more_tag = "one more tag";
  13. }
  14. [SetupForEachSpecification, Tags(tag.example)]
  15. public class context_with_multiple_specifications_and_setup_for_each
  16. {
  17. public static int EstablishRunCount;
  18. public static int BecauseClauseRunCount;
  19. Establish context = () => EstablishRunCount++;
  20. Because of = () => BecauseClauseRunCount++;
  21. It spec1 = () => { };
  22. It spec2 = () => { };
  23. }
  24. [Tags(tag.example, "foobar")]
  25. public class context_with_multiple_specifications
  26. {
  27. public static int EstablishRunCount;
  28. public static int BecauseClauseRunCount;
  29. Establish context = () => EstablishRunCount++;
  30. Because of = () => BecauseClauseRunCount++;
  31. It spec1 = () => { };
  32. It spec2 = () => { };
  33. }
  34. [Tags(tag.example, tag.example)]
  35. [Tags(tag.example)]
  36. public class context_with_duplicate_tags
  37. {
  38. It bla_bla = () => { };
  39. }
  40. [Tags(tag.example, tag.some_other_tag, tag.one_more_tag)]
  41. public class context_with_tags
  42. {
  43. It bla_bla = () => { };
  44. }
  45. public class context_with_unimplemented_specs
  46. {
  47. It should_be_unimplemented;
  48. }
  49. [Ignore("example reason")]
  50. public class context_with_ignore : context_with_no_specs
  51. {
  52. public static bool IgnoredSpecRan;
  53. It should_be_ignored = () =>
  54. IgnoredSpecRan = true;
  55. }
  56. public class context_with_ignore_on_one_spec : context_with_no_specs
  57. {
  58. public static bool IgnoredSpecRan;
  59. [Ignore("example reason")]
  60. It should_be_ignored = () =>
  61. IgnoredSpecRan = true;
  62. }
  63. [Ignore("example reason")]
  64. public class context_with_ignore_and_reason : context_with_no_specs
  65. {
  66. public static bool IgnoredSpecRan;
  67. It should_be_ignored = () =>
  68. IgnoredSpecRan = true;
  69. }
  70. public class context_with_ignore_and_reason_on_one_spec : context_with_no_specs
  71. {
  72. public static bool IgnoredSpecRan;
  73. [Ignore("example reason")]
  74. It should_be_ignored = () =>
  75. IgnoredSpecRan = true;
  76. }
  77. [Tags(tag.example)]
  78. public class context_with_no_specs
  79. {
  80. public static bool ContextEstablished;
  81. public static bool CleanupOccurred;
  82. Establish context = () =>
  83. {
  84. ContextEstablished = true;
  85. };
  86. Cleanup after_each = () =>
  87. {
  88. CleanupOccurred = true;
  89. };
  90. }
  91. [Subject(typeof(int), "Some description")]
  92. [Tags(tag.example)]
  93. public class context_with_subject
  94. {
  95. }
  96. public class context_with_parent_with_subject : context_with_subject
  97. {
  98. }
  99. [Subject(typeof(int), "Parent description")]
  100. public class parent_context
  101. {
  102. It should_be_able_to_assert_something = () =>
  103. true.Should().BeTrue();
  104. public class nested_context
  105. {
  106. It should_be_able_to_assert_something_else = () =>
  107. false.Should().BeFalse();
  108. }
  109. public class nested_context_inheriting_another_concern : context_with_subject
  110. {
  111. It should_be_able_to_assert_something_else = () =>
  112. false.Should().BeFalse();
  113. }
  114. [Subject(typeof(int), "Nested description")]
  115. public class nested_context_inheriting_and_owning_a_concern : context_with_subject
  116. {
  117. It should_be_able_to_assert_something_else = () =>
  118. false.Should().BeFalse();
  119. }
  120. }
  121. public class parent_context_without_concern
  122. {
  123. It should_be_able_to_assert_something = () =>
  124. true.Should().BeTrue();
  125. public class nested_context
  126. {
  127. It should_be_able_to_assert_something_else = () =>
  128. false.Should().BeFalse();
  129. }
  130. }
  131. public class parent_context_that_has_its_own_because_block
  132. {
  133. Because of = () =>
  134. {
  135. };
  136. public class nested_context_that_has_a_because_block_which
  137. {
  138. Because of = () =>
  139. {
  140. };
  141. }
  142. }
  143. [Tags(tag.example)]
  144. public class context_with_failing_specs
  145. {
  146. It should = () => { throw new InvalidOperationException("something went wrong"); };
  147. }
  148. [Tags(tag.example)]
  149. public class context_with_failing_establish
  150. {
  151. Establish context = () => { throw new InvalidOperationException("something went wrong"); };
  152. It should = () => { };
  153. }
  154. [Tags(tag.example)]
  155. public class context_with_failing_because
  156. {
  157. Because of = () => { throw new InvalidOperationException("something went wrong"); };
  158. It should = () => { };
  159. }
  160. [Tags(tag.example)]
  161. public class context_with_console_output
  162. {
  163. Establish context =
  164. () => Console.Out.WriteLine("Console.Out message in establish");
  165. Because of =
  166. () => Console.Out.WriteLine("Console.Out message in because");
  167. Cleanup after =
  168. () => Console.Out.WriteLine("Console.Out message in cleanup");
  169. It should_log_messages =
  170. () => Console.Out.WriteLine("Console.Out message in spec");
  171. It should_log_messages_also_for_the_nth_spec =
  172. () => Console.Out.WriteLine("Console.Out message in nth spec");
  173. }
  174. [Tags(tag.example)]
  175. public class context_with_console_error_output
  176. {
  177. Establish context =
  178. () => Console.Error.WriteLine("Console.Error message in establish");
  179. Because of =
  180. () => Console.Error.WriteLine("Console.Error message in because");
  181. Cleanup after =
  182. () => Console.Error.WriteLine("Console.Error message in cleanup");
  183. It should_log_messages =
  184. () => Console.Error.WriteLine("Console.Error message in spec");
  185. It should_log_messages_also_for_the_nth_spec =
  186. () => Console.Error.WriteLine("Console.Error message in nth spec");
  187. }
  188. [Tags(tag.example)]
  189. public class context_with_debug_output
  190. {
  191. Establish context =
  192. () => Debug.WriteLine("Debug.WriteLine message in establish");
  193. Because of =
  194. () => Debug.WriteLine("Debug.WriteLine message in because");
  195. Cleanup after =
  196. () => Debug.WriteLine("Debug.WriteLine message in cleanup");
  197. It should_log_messages =
  198. () => Debug.WriteLine("Debug.WriteLine message in spec");
  199. It should_log_messages_also_for_the_nth_spec =
  200. () => Debug.WriteLine("Debug.WriteLine message in nth spec");
  201. }
  202. [SetupForEachSpecification, Tags(tag.example)]
  203. public class context_with_console_output_and_setup_for_each
  204. {
  205. Establish context =
  206. () => Console.Out.WriteLine("Console.Out message in establish");
  207. Because of =
  208. () => Console.Out.WriteLine("Console.Out message in because");
  209. Cleanup after =
  210. () => Console.Out.WriteLine("Console.Out message in cleanup");
  211. It should_log_messages =
  212. () => Console.Out.WriteLine("Console.Out message in spec");
  213. It should_log_messages_also_for_the_nth_spec =
  214. () => Console.Out.WriteLine("Console.Out message in nth spec");
  215. }
  216. [Tags(tag.example)]
  217. public class context_with_inner_exception
  218. {
  219. It should_throw = () =>
  220. {
  221. try
  222. {
  223. throw new Exception("INNER123");
  224. }
  225. catch (Exception err)
  226. {
  227. throw new TargetInvocationException(err);
  228. }
  229. };
  230. }
  231. public class Container
  232. {
  233. [Tags(tag.example)]
  234. public class nested_context
  235. {
  236. It should_be_run = () => { };
  237. }
  238. }
  239. [Tags(tag.example)]
  240. public class context_with_public_It_field
  241. {
  242. public It should;
  243. }
  244. [Tags(tag.example)]
  245. public class context_with_protected_It_field
  246. {
  247. protected It should;
  248. }
  249. [Tags(tag.example)]
  250. public class context_with_internal_It_field
  251. {
  252. #pragma warning disable 0649
  253. internal It should;
  254. #pragma warning restore 0649
  255. }
  256. [Tags(tag.example)]
  257. public class context_with_public_Behaves_like_field
  258. {
  259. public Behaves_like<Behaviors> behavior;
  260. }
  261. [Tags(tag.example)]
  262. public class context_with_nonprivate_framework_fields
  263. {
  264. public Establish establish;
  265. public Because because;
  266. public Cleanup cleanup;
  267. #pragma warning disable 0649
  268. internal Behaves_like<Behaviors> behavior;
  269. #pragma warning restore 0649
  270. protected It specification;
  271. It private_specification;
  272. }
  273. }