PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Machine.Specifications.Specs/Controller/ControllerSpecs.cs

http://github.com/machine/machine.specifications
C# | 201 lines | 164 code | 34 blank | 3 comment | 0 complexity | ec45a3ef9878e2006c3509e40f600239 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, LGPL-2.1, BSD-3-Clause, MIT, CC-BY-SA-3.0
  1. using FluentAssertions;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Example.Random.SingleContextInThisNamespace;
  6. using Example.Random;
  7. using System;
  8. namespace Machine.Specifications.Specs.Controller
  9. {
  10. public class With_Controller
  11. {
  12. protected static Machine.Specifications.Controller.Controller Controller;
  13. protected static List<string> ListenEvents;
  14. Establish context = () => {
  15. ListenEvents = new List<string>();
  16. TestAssemblyContext.OnAssemblyStartRun = false;
  17. TestAssemblyContext.OnAssemblyCompleteRun = false;
  18. Controller = new Machine.Specifications.Controller.Controller((string eventText) => {
  19. ListenEvents.Add(eventText);
  20. });
  21. };
  22. }
  23. [Behaviors]
  24. public class RunListenerNotificationBehaviors
  25. {
  26. protected static List<string> ListenEvents;
  27. It notifies_listener_of_protocol_version = () => {
  28. ListenEvents.All(e => e.Contains("<listener version=\"1.0\">")).Should().BeTrue();
  29. };
  30. It notifies_listener_that_the_run_has_started = () => {
  31. ListenEvents.Count(e => e.Contains("<onrunstart />")).Should().Be(1);
  32. };
  33. It notifies_listener_that_the_run_has_ended = () => {
  34. ListenEvents.Count(e => e.Contains("<onrunend />")).Should().Be(1);
  35. };
  36. It notifies_listener_that_the_assembly_run_has_ended = () => {
  37. ListenEvents.Count(e => e.Contains("<onassemblyend>")).Should().Be(1);
  38. };
  39. It notifies_listener_that_the_assembly_run_has_started = () => {
  40. ListenEvents.Count(e => e.Contains("<onassemblystart>")).Should().Be(1);
  41. };
  42. It notifies_listener_that_the_context_run_has_ended = () => {
  43. ListenEvents.Count(e => e.Contains("<oncontextend>")).Should().BeGreaterOrEqualTo(1);
  44. };
  45. It notifies_listener_that_the_context_run_has_started = () => {
  46. ListenEvents.Count(e => e.Contains("<oncontextstart>")).Should().BeGreaterOrEqualTo(1);
  47. };
  48. It notifies_listener_that_the_spec_run_has_ended = () => {
  49. ListenEvents.Count(e => e.Contains("<onspecificationstart>")).Should().BeGreaterOrEqualTo(1);
  50. };
  51. It notifies_listener_that_the_spec_run_has_started = () => {
  52. ListenEvents.Count(e => e.Contains("<onspecificationend>")).Should().BeGreaterOrEqualTo(1);
  53. };
  54. It runs_assembly_context_start = () => {
  55. TestAssemblyContext.OnAssemblyStartRun.Should().BeTrue();
  56. };
  57. It runs_assembly_complete = () => {
  58. TestAssemblyContext.OnAssemblyCompleteRun.Should().BeTrue();
  59. };
  60. }
  61. [Subject(typeof(Machine.Specifications.Controller.Controller))]
  62. public class When_running_assemblies : With_Controller
  63. {
  64. Because of = () => {
  65. Controller.StartRun();
  66. Controller.RunAssemblies(new[] {
  67. typeof(context_without_any_other_in_the_same_namespace).GetTypeInfo().Assembly,
  68. });
  69. Controller.EndRun();
  70. };
  71. Behaves_like<RunListenerNotificationBehaviors> run_listener_notifier;
  72. }
  73. [Subject(typeof(Machine.Specifications.Controller.Controller))]
  74. public class When_running_namespace : With_Controller
  75. {
  76. Because of = () => {
  77. Controller.StartRun();
  78. Controller.RunNamespaces(typeof(context_without_any_other_in_the_same_namespace).GetTypeInfo().Assembly,
  79. new[] { typeof(context_without_any_other_in_the_same_namespace).Namespace});
  80. Controller.EndRun();
  81. };
  82. Behaves_like<RunListenerNotificationBehaviors> run_listener_notifier;
  83. }
  84. [Subject(typeof(Machine.Specifications.Controller.Controller))]
  85. public class When_running_types : With_Controller
  86. {
  87. Because of = () => {
  88. Controller.StartRun();
  89. Controller.RunTypes(typeof(context_without_any_other_in_the_same_namespace).GetTypeInfo().Assembly,
  90. new[] {
  91. typeof(context_without_any_other_in_the_same_namespace),
  92. });
  93. Controller.EndRun();
  94. };
  95. Behaves_like<RunListenerNotificationBehaviors> run_listener_notifier;
  96. }
  97. [Subject(typeof(Machine.Specifications.Controller.Controller))]
  98. public class When_running_members : With_Controller
  99. {
  100. Because of = () => {
  101. Controller.StartRun();
  102. Controller.RunMembers(typeof(context_without_any_other_in_the_same_namespace).GetTypeInfo().Assembly,
  103. new[] {
  104. typeof(context_without_any_other_in_the_same_namespace).GetField("spec1", BindingFlags.NonPublic | BindingFlags.Instance),
  105. typeof(context_without_any_other_in_the_same_namespace).GetField("spec2", BindingFlags.NonPublic | BindingFlags.Instance),
  106. });
  107. Controller.EndRun();
  108. };
  109. Behaves_like<RunListenerNotificationBehaviors> run_listener_notifier;
  110. }
  111. [Subject(typeof(Machine.Specifications.Controller.Controller))]
  112. public class When_runspecs_is_given_two_standard_specs : With_Controller
  113. {
  114. static Type Type = typeof(context_with_specs_and_behaviors);
  115. Because of = () => {
  116. Controller.StartRun();
  117. Controller.RunSpecs(Type.GetTypeInfo().Assembly,
  118. new[] {
  119. Type.FullName + ".spec1",
  120. Type.FullName + ".spec2",
  121. });
  122. Controller.EndRun();
  123. };
  124. Behaves_like<RunListenerNotificationBehaviors> run_listener_notifier;
  125. It runs_both_specs = () => {
  126. // two mentions: one for start and one for end spec run
  127. ListenEvents.Count(e => e.Contains("spec1")).Should().Be(2);
  128. ListenEvents.Count(e => e.Contains("spec2")).Should().Be(2);
  129. };
  130. }
  131. [Subject(typeof(Machine.Specifications.Controller.Controller))]
  132. public class When_runspecs_is_given_a_behavior_spec : With_Controller
  133. {
  134. static Type Type = typeof(context_with_specs_and_behaviors);
  135. Because of = () => {
  136. Controller.StartRun();
  137. Controller.RunSpecs(Type.GetTypeInfo().Assembly,
  138. new[] {
  139. Type.FullName + ".behavior1",
  140. });
  141. Controller.EndRun();
  142. };
  143. Behaves_like<RunListenerNotificationBehaviors> run_listener_notifier;
  144. It runs_only_the_behavior_spec = () => {
  145. // two mentions: one for start and one for end spec run
  146. ListenEvents.Count(e => e.Contains("<onspecificationstart>")).Should().Be(1);
  147. ListenEvents.Count(e => e.Contains("behavior1")).Should().Be(2);
  148. };
  149. }
  150. [Subject(typeof(Machine.Specifications.Controller.Controller))]
  151. public class When_runspecs_is_given_a_behaves_like_field : With_Controller
  152. {
  153. static Type Type = typeof(context_with_specs_and_behaviors);
  154. Because of = () => {
  155. Controller.StartRun();
  156. Controller.RunSpecs(Type.GetTypeInfo().Assembly,
  157. new[] {
  158. Type.FullName + ".behaviors",
  159. });
  160. Controller.EndRun();
  161. };
  162. It does_not_run_anything = () => {
  163. // two mentions: one for start and one for end spec run
  164. ListenEvents.Count(e => e.Contains("<onspecificationstart>")).Should().Be(0);
  165. };
  166. }
  167. }