PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Runners/Machine.Specifications.ConsoleRunner.Specs/ProgramSpecs.cs

https://github.com/machine/machine.specifications
C# | 300 lines | 224 code | 73 blank | 3 comment | 2 complexity | 8aa723fbeea4ed0c10b2fcb83a57ce68 MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text.RegularExpressions;
  6. using FluentAssertions;
  7. using Machine.Specifications.ConsoleRunner.Properties;
  8. namespace Machine.Specifications.ConsoleRunner.Specs
  9. {
  10. // TODO: Add Subject count
  11. // TODO: Add Tag and filter by tag
  12. // TODO: Add awesome client side reporting stuff
  13. [Subject("Console runner")]
  14. public class when_arguments_are_not_provided
  15. : ConsoleRunnerSpecs
  16. {
  17. Because of = ()=>
  18. program.Run(new string[] {});
  19. It should_print_usage_statement = ()=>
  20. console.Lines.Should().Contain(Options.Usage());
  21. }
  22. [Subject("Console runner")]
  23. public class when_running_a_specification_assembly
  24. : ConsoleRunnerSpecs
  25. {
  26. Because of =
  27. () => program.Run(new [] { GetPath("Example.dll") });
  28. It should_write_the_assembly_name =
  29. () => console.Lines.Should().Contain(l => l.Contains("Example"));
  30. It should_write_the_specifications =
  31. () => console.Lines.Should().Contain(
  32. "ť should debit the from account by the amount transferred",
  33. "ť should credit the to account by the amount transferred",
  34. "ť should not allow the transfer");
  35. It should_write_the_contexts =
  36. () => console.Lines.Should().Contain(
  37. "Account Funds transfer, when transferring between two accounts",
  38. "Account Funds transfer, when transferring an amount larger than the balance of the from account"
  39. );
  40. It should_write_the_count_of_contexts =
  41. () => console.Lines.Should().Contain(l => l.Contains("Contexts: 3"));
  42. It should_write_the_count_of_specifications =
  43. () => console.Lines.Should().Contain(l => l.Contains("Specifications: 6"));
  44. It should_write_the_run_time =
  45. () => console.Lines.Should().Contain(l => l.Contains("Time: "));
  46. }
  47. [Subject("Console runner")]
  48. public class when_running_a_specification_assembly_and_silent_is_set
  49. : ConsoleRunnerSpecs
  50. {
  51. Because of =
  52. () => program.Run(new [] { GetPath("Example.dll"), "--silent" });
  53. It should_not_write_the_assembly_name =
  54. () => console.Lines.Should().NotContain(l => l.Contains("Example"));
  55. It should_not_write_the_contexts =
  56. () => console.Lines.Should().Match(l => !l.All(_ => _.StartsWith("Account Funds transfer")));
  57. It should_not_write_the_specifications =
  58. () => console.Lines.Should().Match(l => !l.All(_ => _.StartsWith("ť should")));
  59. It should_write_the_count_of_contexts =
  60. () => console.Lines.Should().Contain(l => l.Contains("Contexts: 3"));
  61. It should_write_the_count_of_specifications =
  62. () => console.Lines.Should().Contain(l => l.Contains("Specifications: 6"));
  63. It should_write_the_run_time =
  64. () => console.Lines.Should().Contain(l => l.Contains("Time: "));
  65. }
  66. [Subject("Console runner")]
  67. public class when_running_a_specification_assembly_and_progress_is_set
  68. : ConsoleRunnerSpecs
  69. {
  70. Because of =
  71. () => program.Run(new [] { GetPath("Example.dll"), "--progress" });
  72. It should_write_the_assembly_name =
  73. () => console.Lines.Should().Contain(l => l.Contains("Example"));
  74. It should_not_write_the_contexts =
  75. () => console.Lines.Should().Match(l => !l.All(_ => _.StartsWith("Account Funds transfer")));
  76. It should_not_write_the_specifications =
  77. () => console.Lines.Should().Match(l => !l.All(_ => _.StartsWith("ť should")));
  78. It should_write_the_specification_results =
  79. () => console.Lines.Should().Contain("...***");
  80. It should_write_the_count_of_contexts =
  81. () => console.Lines.Should().Contain(l => l.Contains("Contexts: 3"));
  82. It should_write_the_count_of_specifications =
  83. () => console.Lines.Should().Contain(l => l.Contains("Specifications: 6"));
  84. It should_write_the_run_time =
  85. () => console.Lines.Should().Contain(l => l.Contains("Time: "));
  86. }
  87. [Subject("Console runner")]
  88. public class when_specifying_a_missing_assembly_on_the_command_line
  89. : ConsoleRunnerSpecs
  90. {
  91. const string missingAssemblyName = "Some.Missing.Assembly.dll";
  92. public static ExitCode exitCode;
  93. Because of = ()=>
  94. exitCode = program.Run(new[] { missingAssemblyName });
  95. It should_output_an_error_message_with_the_name_of_the_missing_assembly = ()=>
  96. console.Lines.Should().Contain(string.Format(Resources.MissingAssemblyError, missingAssemblyName));
  97. It should_return_the_Error_exit_code = ()=>
  98. exitCode.Should().Be(ExitCode.Error);
  99. }
  100. [Subject("Console runner")]
  101. public class when_a_specification_fails : ConsoleRunnerSpecs
  102. {
  103. public static ExitCode exitCode;
  104. Because of =
  105. () => exitCode = program.Run(new[] { GetPath("Example.Failing.dll") });
  106. It should_write_the_failure =
  107. () => console.Lines.Should().Contain(l => l.Contains("Exception"));
  108. It should_write_the_count_of_failed_specifications =
  109. () => console.Lines.Should().Contain(l => l.Contains("1 failed"));
  110. It should_return_the_Failure_exit_code =
  111. () => exitCode.Should().Be(ExitCode.Failure);
  112. It should_separate_failures_from_the_rest_of_the_test_run =
  113. () => console.Output.Should().MatchRegex(String.Format("\\S{0}{0}{0}Failures:{0}{0}\\S", Regex.Escape(Environment.NewLine)));
  114. }
  115. [Subject("Console runner")]
  116. public class when_a_specification_fails_and_silent_is_set : ConsoleRunnerSpecs
  117. {
  118. public static ExitCode exitCode;
  119. Because of =
  120. () => exitCode = program.Run(new[] { GetPath("Example.Failing.dll"), "--silent", "--exclude", "example" });
  121. It should_write_the_count_of_failed_specifications =
  122. () => console.Lines.Should().Contain(l => l.Contains("1 failed"));
  123. It should_return_the_failure_exit_code =
  124. () => exitCode.Should().Be(ExitCode.Failure);
  125. It should_write_a_summary_of_failing_specifications =
  126. () => console.Lines.Should().Contain("Failures:", "Scott Bellware, at any given moment", "ť will fail (FAIL)");
  127. It should_write_failure_stack_traces =
  128. () => console.Lines.Should().Contain(l => l.Contains("hi scott, love you, miss you."));
  129. It should_separate_failures_from_the_rest_of_the_test_run =
  130. () => console.Output.Should().MatchRegex(String.Format("\\S{0}{0}Failures:{0}{0}\\S", Regex.Escape(Environment.NewLine)));
  131. }
  132. [Subject("Console runner")]
  133. public class when_a_specification_fails_and_progress_is_set : ConsoleRunnerSpecs
  134. {
  135. public static ExitCode exitCode;
  136. Because of =
  137. () => exitCode = program.Run(new[] { GetPath("Example.Failing.dll"), "--progress", "--exclude", "example" });
  138. It should_write_failed_specification_results =
  139. () => console.Lines.Should().Contain("F");
  140. It should_write_the_count_of_failed_specifications =
  141. () => console.Lines.Should().Contain(l => l.Contains("1 failed"));
  142. It should_return_the_failure_exit_code =
  143. () => exitCode.Should().Be(ExitCode.Failure);
  144. It should_write_a_summary_of_failing_specifications =
  145. () => console.Lines.Should().Contain("Failures:", "Scott Bellware, at any given moment", "ť will fail (FAIL)");
  146. It should_write_failure_stack_traces =
  147. () => console.Lines.Should().Contain(l => l.Contains("hi scott, love you, miss you."));
  148. It should_separate_failures_from_the_rest_of_the_test_run =
  149. () => console.Output.Should().MatchRegex(String.Format("\\S{0}{0}{0}Failures:{0}{0}\\S", Regex.Escape(Environment.NewLine)));
  150. }
  151. [Subject("Console runner")]
  152. public class when_specifying_an_include_filter : ConsoleRunnerSpecs
  153. {
  154. Because of = ()=>
  155. program.Run(new [] { GetPath("Example.dll"), "--include", "failure"});
  156. It should_execute_specs_with_the_included_tag = () =>
  157. console.Lines.Should().Contain(l => l.Contains("Account Funds transfer, when transferring an amount larger than the balance of the from account"));
  158. It should_not_execute_specs_that_are_not_included = () =>
  159. console.Lines.Should().NotContain("Account Funds transfer, when transferring between two accounts");
  160. }
  161. [Subject("Console runner")]
  162. public class when_running_from_directory_different_from_assembly_location : ConsoleRunnerSpecs
  163. {
  164. Because of = () =>
  165. program.Run(new[] { GetPath(@"ExternalFile\Example.UsingExternalFile.dll") });
  166. It should_pass_the_specification_which_depends_on_external_file = () =>
  167. console.Lines.Should().Contain(
  168. "External resources usage, when using file copied to assembly output directory",
  169. "ť should be able to locate it by relative path");
  170. It should_pass_all_specifications = () =>
  171. console.Lines.Should().NotContain("failed");
  172. }
  173. [Subject("Console runner")]
  174. [Tags("Issue-157")]
  175. public class when_running_two_spec_assemblies_and_the_first_has_failing_specifications : ConsoleRunnerSpecs
  176. {
  177. static ExitCode ExitCode;
  178. Because of = () =>
  179. ExitCode = program.Run(new[] { GetPath(@"Issue157\Example.Issue157-Fail.dll"), GetPath(@"Issue157\Example.Issue157-Success.dll") });
  180. It should_fail_the_run = () =>
  181. ExitCode.Should().NotBe(ExitCode.Success);
  182. }
  183. [Subject("Console runner")]
  184. [Tags("Issue-157")]
  185. public class when_running_two_spec_assemblies_and_the_second_has_failing_specifications : ConsoleRunnerSpecs
  186. {
  187. static ExitCode ExitCode;
  188. Because of = () =>
  189. ExitCode = program.Run(new[] { GetPath(@"Issue157\Example.Issue157-Success.dll"), GetPath(@"Issue157\Example.Issue157-Fail.dll") });
  190. It should_fail_the_run = () =>
  191. ExitCode.Should().NotBe(ExitCode.Success);
  192. }
  193. [Subject("Console runner")]
  194. public class when_running_two_spec_assemblies : ConsoleRunnerSpecs
  195. {
  196. static ExitCode ExitCode;
  197. Because of = () =>
  198. ExitCode = program.Run(new[] { GetPath(@"Example.dll"), GetPath(@"Example.Failing.dll") });
  199. It should_write_the_summary_once = () =>
  200. console.Lines.Count(x => x.StartsWith("Contexts: ")).Should().Be(1);
  201. }
  202. public class ConsoleRunnerSpecs
  203. {
  204. const string TeamCityIndicator = "TEAMCITY_PROJECT_NAME";
  205. static string TeamCityEnvironment;
  206. public static Program program;
  207. public static FakeConsole console;
  208. Establish context = () =>
  209. {
  210. console = new FakeConsole();
  211. program = new Program(console);
  212. TeamCityEnvironment = Environment.GetEnvironmentVariable(TeamCityIndicator);
  213. Environment.SetEnvironmentVariable(TeamCityIndicator, String.Empty);
  214. };
  215. Cleanup after = () =>
  216. {
  217. if (TeamCityEnvironment != null)
  218. {
  219. Environment.SetEnvironmentVariable(TeamCityIndicator, TeamCityEnvironment);
  220. }
  221. };
  222. protected static string GetPath(string path)
  223. {
  224. return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), path);
  225. }
  226. }
  227. }