/Source/Reporting/Machine.Specifications.Reporting.Specs/Visitors/FileBasedResultSupplementPreparationSpecs.cs

https://github.com/machine/machine.specifications · C# · 225 lines · 187 code · 38 blank · 0 comment · 0 complexity · 8cc554e1abc58ec2d62fb2f7534238d1 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FluentAssertions;
  5. using Machine.Specifications.Reporting.Model;
  6. using Machine.Specifications.Reporting.Visitors;
  7. using Machine.Specifications.Utility;
  8. using Rhino.Mocks;
  9. namespace Machine.Specifications.Reporting.Specs.Visitors
  10. {
  11. [Subject(typeof(FileBasedResultSupplementPreparation))]
  12. public class when_no_supplements_need_to_be_prepared_for_the_html_report : ReportSpecs
  13. {
  14. static FileBasedResultSupplementPreparation Preparation;
  15. static Run Report;
  16. static Func<string> ResourcePathCreator;
  17. Establish context = () =>
  18. {
  19. ResourcePathCreator = MockRepository.GenerateStub<Func<string>>();
  20. Preparation = new FileBasedResultSupplementPreparation(MockRepository.GenerateStub<IFileSystem>());
  21. Preparation.Initialize(new VisitorContext { ResourcePathCreator = ResourcePathCreator });
  22. Report = Run(Assembly("assembly 1",
  23. Concern("a 1 concern 1",
  24. Context("a 1 c 1 context 1",
  25. Spec("it", "a 1 c 1 c 1 specification 2", Result.Pass())
  26. )
  27. )
  28. ));
  29. };
  30. Because of = () => Preparation.Visit(Report);
  31. It should_not_create_the_folder_for_supplements =
  32. () => ResourcePathCreator.AssertWasNotCalled(x => x());
  33. }
  34. [Subject(typeof(FileBasedResultSupplementPreparation))]
  35. public class when_file_based_result_supplements_are_prepared_for_the_html_report : ReportSpecs
  36. {
  37. static FileBasedResultSupplementPreparation Preparation;
  38. static Specification Images;
  39. static Specification HtmlFiles;
  40. static Run Report;
  41. static Specification Texts;
  42. Establish context = () =>
  43. {
  44. Preparation = new FileBasedResultSupplementPreparation(MockRepository.GenerateStub<IFileSystem>());
  45. Preparation.Initialize(new VisitorContext { ResourcePathCreator = () => @"C:\report\resources" });
  46. Images = Spec("it", "a 1 c 1 c 1 specification 1",
  47. Result.Supplement(Result.Pass(),
  48. "Images",
  49. new Dictionary<string, string>
  50. {
  51. { "img-image", @"C:\some\image.png" },
  52. { "img-another-image", @"C:\some\other\image.png" }
  53. }));
  54. HtmlFiles = Spec("it", "a 2 c 1 c 1 specification 1",
  55. Result.Supplement(Result.Pass(),
  56. "HTML",
  57. new Dictionary<string, string>
  58. {
  59. { "html-file", @"C:\some\file.html" },
  60. { "html-another-file", @"C:\some\other\file.html" }
  61. }));
  62. Texts = Spec("it", "a 2 c 1 c 2 specification 1",
  63. Result.Supplement(Result.Pass(),
  64. "Text",
  65. new Dictionary<string, string>
  66. {
  67. { "text-text", "text" },
  68. { "text-another-text", "text" }
  69. }));
  70. Report = Run(Assembly("assembly 1",
  71. Concern("a 1 concern 1",
  72. Context("a 1 c 1 context 1",
  73. Images,
  74. Spec("it", "a 1 c 1 c 1 specification 2", Result.Pass())
  75. )
  76. )
  77. ),
  78. Assembly("assembly 2",
  79. Concern("a 2 concern 1",
  80. Context("a 2 c 1 context 1",
  81. Spec("it", "a 2 c 1 c 1 specification 2", Result.Pass()),
  82. HtmlFiles),
  83. Context("a 2 c 1 context 2",
  84. Texts,
  85. Spec("it", "a 2 c 1 c 2 specification 2", Result.Pass())))));
  86. };
  87. Because of = () => Preparation.Visit(Report);
  88. It should_copy_all_image_files_to_the_report_resource_directory =
  89. () => Images.Supplements.Values.Each(v => v.Values.Each(f => f.Should().StartWith(@"C:\report\resources\")));
  90. It should_copy_all_HTML_files_to_the_report_resource_directory =
  91. () => HtmlFiles.Supplements.Values.Each(v => v.Values.Each(f => f.Should().StartWith(@"C:\report\resources\")));
  92. It should_not_alter_text_supplements =
  93. () => Texts.Supplements.Values.Each(v => v.Values.Each(f => f.Should().Be("text")));
  94. }
  95. [Subject(typeof(FileBasedResultSupplementPreparation))]
  96. public class when_copying_file_based_result_supplements_fails : ReportSpecs
  97. {
  98. static FileBasedResultSupplementPreparation Preparation;
  99. static Specification Failing;
  100. static Run Report;
  101. static IDictionary<string, string> FirstSupplement;
  102. Establish context = () =>
  103. {
  104. var fileSystem = MockRepository.GenerateStub<IFileSystem>();
  105. fileSystem
  106. .Stub(x => x.Move(Arg<string>.Is.Anything, Arg<string>.Is.Anything))
  107. .Throw(PrepareException());
  108. Preparation = new FileBasedResultSupplementPreparation(fileSystem);
  109. Preparation.Initialize(new VisitorContext { ResourcePathCreator = () => @"C:\report\resources" });
  110. Failing = Spec("it", "a 1 c 1 c 1 specification 1",
  111. Result.Supplement(Result.Pass(),
  112. "Failing Images and Text",
  113. new Dictionary<string, string>
  114. {
  115. { "img-image", @"C:\some\image.png" },
  116. { "img-another-image", @"C:\some\other\image.png" },
  117. { "text-some-text", "text" }
  118. }));
  119. Report = Run(Assembly("assembly 1",
  120. Concern("a 1 concern 1",
  121. Context("a 1 c 1 context 1",
  122. Failing))));
  123. };
  124. Because of = () =>
  125. {
  126. Preparation.Visit(Report);
  127. FirstSupplement = Report.Assemblies.First()
  128. .Concerns.First()
  129. .Contexts.First()
  130. .Specifications.First()
  131. .Supplements.First()
  132. .Value;
  133. };
  134. It should_replace_file_results_with_text_results =
  135. () => FirstSupplement.Keys.Each(x => x.Should().StartWith("text-"));
  136. It should_replace_file_results_with_text_results_where_the_original_key_is_prefixed_with__text__ =
  137. () => FirstSupplement.Keys.Where(x => x.StartsWith("text-img-")).Count().Should().Be(2);
  138. It should_replace_file_results_with_text_results_containing_the_error_message =
  139. () => FirstSupplement
  140. .Where(x => x.Key.StartsWith("text-img-"))
  141. .Each(x => x.Value.Should().StartWith(@"Failed to copy supplement C:\some\"));
  142. }
  143. [Subject(typeof(FileBasedResultSupplementPreparation))]
  144. public class when_copying_file_based_result_supplements_fails_and_the_error_message_generates_a_conflict : ReportSpecs
  145. {
  146. static FileBasedResultSupplementPreparation Preparation;
  147. static Specification Failing;
  148. static Run Report;
  149. static IDictionary<string, string> FirstSupplement;
  150. Establish context = () =>
  151. {
  152. var fileSystem = MockRepository.GenerateStub<IFileSystem>();
  153. fileSystem
  154. .Stub(x => x.Move(Arg<string>.Is.Anything, Arg<string>.Is.Anything))
  155. .Throw(PrepareException());
  156. Preparation = new FileBasedResultSupplementPreparation(fileSystem);
  157. Preparation.Initialize(new VisitorContext { ResourcePathCreator = () => @"C:\report\resources" });
  158. Failing = Spec("it", "a 1 c 1 c 1 specification 1",
  159. Result.Supplement(Result.Pass(),
  160. "Failing Images and Text",
  161. new Dictionary<string, string>
  162. {
  163. { "img-image", @"C:\some\image.png" },
  164. { "text-img-image-error", "will conflict with error for img-image" },
  165. { "text-img-image-error-error", "will conflict with error for img-image" }
  166. }));
  167. Report = Run(Assembly("assembly 1",
  168. Concern("a 1 concern 1",
  169. Context("a 1 c 1 context 1",
  170. Failing))));
  171. };
  172. Because of = () =>
  173. {
  174. Preparation.Visit(Report);
  175. FirstSupplement = Report.Assemblies.First()
  176. .Concerns.First()
  177. .Contexts.First()
  178. .Specifications.First()
  179. .Supplements.First()
  180. .Value;
  181. };
  182. It should_succeed =
  183. () => true.Should().BeTrue();
  184. It should_create_a_unique_error_key =
  185. () => FirstSupplement.Keys.Should().Contain("text-img-image-error-error-error");
  186. }
  187. }