PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Tests/GeneratorTests/DecoratorRegistryTests.cs

http://github.com/techtalk/SpecFlow
C# | 263 lines | 213 code | 50 blank | 0 comment | 4 complexity | 3b912cc2c3b3cd89f3f4723a0ea174a4 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.CodeDom;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using BoDi;
  6. using Moq;
  7. using NUnit.Framework;
  8. using TechTalk.SpecFlow.Generator;
  9. using TechTalk.SpecFlow.Generator.UnitTestConverter;
  10. using TechTalk.SpecFlow.Parser.SyntaxElements;
  11. using FluentAssertions;
  12. namespace TechTalk.SpecFlow.GeneratorTests
  13. {
  14. [TestFixture]
  15. public class DecoratorRegistryTests
  16. {
  17. private IObjectContainer container;
  18. [SetUp]
  19. public void Setup()
  20. {
  21. container = new ObjectContainer();
  22. }
  23. internal static Mock<ITestClassTagDecorator> CreateTestClassTagDecoratorMock(string expectedTag = null)
  24. {
  25. var testClassDecoratorMock = new Mock<ITestClassTagDecorator>();
  26. testClassDecoratorMock.Setup(d => d.ApplyOtherDecoratorsForProcessedTags).Returns(false);
  27. testClassDecoratorMock.Setup(d => d.RemoveProcessedTags).Returns(true);
  28. testClassDecoratorMock.Setup(d => d.Priority).Returns(PriorityValues.Normal);
  29. if (expectedTag == null)
  30. testClassDecoratorMock.Setup(d => d.CanDecorateFrom(It.IsAny<string>(), It.IsAny<TestClassGenerationContext>())).Returns(true);
  31. else
  32. testClassDecoratorMock.Setup(d => d.CanDecorateFrom(expectedTag, It.IsAny<TestClassGenerationContext>())).Returns(true);
  33. return testClassDecoratorMock;
  34. }
  35. internal static Mock<ITestClassDecorator> CreateTestClassDecoratorMock()
  36. {
  37. var testClassDecoratorMock = new Mock<ITestClassDecorator>();
  38. testClassDecoratorMock.Setup(d => d.Priority).Returns(PriorityValues.Normal);
  39. testClassDecoratorMock.Setup(d => d.CanDecorateFrom(It.IsAny<TestClassGenerationContext>())).Returns(true);
  40. return testClassDecoratorMock;
  41. }
  42. internal static Mock<ITestMethodDecorator> CreateTestMethodDecoratorMock()
  43. {
  44. var testClassDecoratorMock = new Mock<ITestMethodDecorator>();
  45. testClassDecoratorMock.Setup(d => d.Priority).Returns(PriorityValues.Normal);
  46. testClassDecoratorMock.Setup(d => d.CanDecorateFrom(It.IsAny<TestClassGenerationContext>(), It.IsAny<CodeMemberMethod>())).Returns(true);
  47. return testClassDecoratorMock;
  48. }
  49. internal static Mock<ITestMethodTagDecorator> CreateTestMethodTagDecoratorMock(string expectedTag = null)
  50. {
  51. var testClassDecoratorMock = new Mock<ITestMethodTagDecorator>();
  52. testClassDecoratorMock.Setup(d => d.ApplyOtherDecoratorsForProcessedTags).Returns(false);
  53. testClassDecoratorMock.Setup(d => d.RemoveProcessedTags).Returns(true);
  54. testClassDecoratorMock.Setup(d => d.Priority).Returns(PriorityValues.Normal);
  55. if (expectedTag == null)
  56. testClassDecoratorMock.Setup(d => d.CanDecorateFrom(It.IsAny<string>(), It.IsAny<TestClassGenerationContext>(), It.IsAny<CodeMemberMethod>())).Returns(true);
  57. else
  58. testClassDecoratorMock.Setup(d => d.CanDecorateFrom(expectedTag, It.IsAny<TestClassGenerationContext>(), It.IsAny<CodeMemberMethod>())).Returns(true);
  59. return testClassDecoratorMock;
  60. }
  61. private DecoratorRegistry CreateDecoratorRegistry()
  62. {
  63. return new DecoratorRegistry(container);
  64. }
  65. private static TestClassGenerationContext CreateGenerationContext(string tag)
  66. {
  67. return new TestClassGenerationContext(null, new Feature { Tags = new Tags(new Tag(tag)) }, null, null, null, null, null, null, null, null, null, true, false);
  68. }
  69. [Test]
  70. public void Should_decorate_test_class()
  71. {
  72. var testClassDecoratorMock = CreateTestClassDecoratorMock();
  73. container.RegisterInstanceAs(testClassDecoratorMock.Object, "foo");
  74. var registry = CreateDecoratorRegistry();
  75. List<string> unprocessedTags;
  76. registry.DecorateTestClass(CreateGenerationContext("dummy"), out unprocessedTags);
  77. testClassDecoratorMock.Verify(d => d.DecorateFrom(It.IsAny<TestClassGenerationContext>()));
  78. }
  79. [Test]
  80. public void Should_decorate_test_class_when_not_applicable()
  81. {
  82. var testClassDecoratorMock = CreateTestClassDecoratorMock();
  83. testClassDecoratorMock.Setup(d => d.CanDecorateFrom(It.IsAny<TestClassGenerationContext>())).Returns(false);
  84. container.RegisterInstanceAs(testClassDecoratorMock.Object, "foo");
  85. var registry = CreateDecoratorRegistry();
  86. List<string> unprocessedTags;
  87. registry.DecorateTestClass(CreateGenerationContext("dummy"), out unprocessedTags);
  88. testClassDecoratorMock.Verify(d => d.DecorateFrom(It.IsAny<TestClassGenerationContext>()), Times.Never());
  89. }
  90. [Test]
  91. public void Should_decorate_test_class_based_on_tag()
  92. {
  93. var testClassDecoratorMock = CreateTestClassTagDecoratorMock();
  94. container.RegisterInstanceAs(testClassDecoratorMock.Object, "foo");
  95. var registry = CreateDecoratorRegistry();
  96. List<string> unprocessedTags;
  97. registry.DecorateTestClass(CreateGenerationContext("foo"), out unprocessedTags);
  98. testClassDecoratorMock.Verify(d => d.DecorateFrom(It.IsAny<string>(), It.IsAny<TestClassGenerationContext>()));
  99. }
  100. [Test]
  101. public void Should_remove_processed_tag_from_test_class_category_list()
  102. {
  103. var testClassDecoratorMock = CreateTestClassTagDecoratorMock();
  104. testClassDecoratorMock.Setup(d => d.RemoveProcessedTags).Returns(true);
  105. container.RegisterInstanceAs(testClassDecoratorMock.Object, "foo");
  106. var registry = CreateDecoratorRegistry();
  107. List<string> classCats = null;
  108. registry.DecorateTestClass(CreateGenerationContext("foo"), out classCats);
  109. classCats.Should().NotBeNull();
  110. classCats.Should().NotContain("foo");
  111. }
  112. [Test]
  113. public void Should_keep_processed_tag_from_test_class_category_list()
  114. {
  115. var testClassDecoratorMock = CreateTestClassTagDecoratorMock();
  116. testClassDecoratorMock.Setup(d => d.RemoveProcessedTags).Returns(false);
  117. container.RegisterInstanceAs(testClassDecoratorMock.Object, "foo");
  118. var registry = CreateDecoratorRegistry();
  119. List<string> classCats = null;
  120. registry.DecorateTestClass(CreateGenerationContext("foo"), out classCats);
  121. classCats.Should().NotBeNull();
  122. classCats.Should().Contain("foo");
  123. }
  124. [Test]
  125. public void Should_allow_multiple_decorators()
  126. {
  127. var testClassDecoratorMock1 = CreateTestClassTagDecoratorMock();
  128. testClassDecoratorMock1.Setup(d => d.ApplyOtherDecoratorsForProcessedTags).Returns(true);
  129. container.RegisterInstanceAs(testClassDecoratorMock1.Object, "foo1");
  130. var testClassDecoratorMock2 = CreateTestClassTagDecoratorMock();
  131. testClassDecoratorMock2.Setup(d => d.ApplyOtherDecoratorsForProcessedTags).Returns(true);
  132. container.RegisterInstanceAs(testClassDecoratorMock2.Object, "foo2");
  133. var registry = CreateDecoratorRegistry();
  134. List<string> unprocessedTags;
  135. registry.DecorateTestClass(CreateGenerationContext("foo"), out unprocessedTags);
  136. testClassDecoratorMock1.Verify(d => d.DecorateFrom(It.IsAny<string>(), It.IsAny<TestClassGenerationContext>()));
  137. testClassDecoratorMock2.Verify(d => d.DecorateFrom(It.IsAny<string>(), It.IsAny<TestClassGenerationContext>()));
  138. }
  139. [Test]
  140. public void Should_higher_priority_decorator_applied_first()
  141. {
  142. List<string> executionOrder = new List<string>();
  143. var testClassDecoratorMock1 = CreateTestClassTagDecoratorMock();
  144. testClassDecoratorMock1.Setup(d => d.ApplyOtherDecoratorsForProcessedTags).Returns(true);
  145. testClassDecoratorMock1.Setup(d => d.DecorateFrom(It.IsAny<string>(), It.IsAny<TestClassGenerationContext>()))
  146. .Callback((string t, TestClassGenerationContext c) => executionOrder.Add("foo1"));
  147. container.RegisterInstanceAs(testClassDecoratorMock1.Object, "foo1");
  148. var testClassDecoratorMock2 = CreateTestClassTagDecoratorMock();
  149. testClassDecoratorMock2.Setup(d => d.ApplyOtherDecoratorsForProcessedTags).Returns(true);
  150. testClassDecoratorMock2.Setup(d => d.Priority).Returns(PriorityValues.High);
  151. testClassDecoratorMock2.Setup(d => d.DecorateFrom(It.IsAny<string>(), It.IsAny<TestClassGenerationContext>()))
  152. .Callback((string t, TestClassGenerationContext c) => executionOrder.Add("foo2"));
  153. container.RegisterInstanceAs(testClassDecoratorMock2.Object, "foo2");
  154. var registry = CreateDecoratorRegistry();
  155. List<string> unprocessedTags;
  156. registry.DecorateTestClass(CreateGenerationContext("foo"), out unprocessedTags);
  157. executionOrder.Should().Equal(new object[] { "foo2", "foo1" });
  158. }
  159. [Test]
  160. public void Should_not_decorate_test_method_for_feature_tag()
  161. {
  162. var testMethodDecoratorMock = CreateTestMethodTagDecoratorMock();
  163. container.RegisterInstanceAs(testMethodDecoratorMock.Object, "foo");
  164. var registry = CreateDecoratorRegistry();
  165. List<string> unprocessedTags;
  166. registry.DecorateTestMethod(CreateGenerationContext("foo"), null, new Tag[] { }, out unprocessedTags);
  167. testMethodDecoratorMock.Verify(d => d.DecorateFrom("foo", It.IsAny<TestClassGenerationContext>(), It.IsAny<CodeMemberMethod>()), Times.Never());
  168. }
  169. [Test]
  170. public void Should_decorate_test_method_for_scenario_tag()
  171. {
  172. var testMethodDecoratorMock = CreateTestMethodTagDecoratorMock();
  173. container.RegisterInstanceAs(testMethodDecoratorMock.Object, "foo");
  174. var registry = CreateDecoratorRegistry();
  175. List<string> unprocessedTags;
  176. registry.DecorateTestMethod(CreateGenerationContext("dummy"), null, new Tag[] { new Tag("foo") }, out unprocessedTags);
  177. testMethodDecoratorMock.Verify(d => d.DecorateFrom(It.IsAny<string>(), It.IsAny<TestClassGenerationContext>(), It.IsAny<CodeMemberMethod>()));
  178. }
  179. [Test]
  180. public void Should_decorate_test_method()
  181. {
  182. var testMethodDecoratorMock = CreateTestMethodDecoratorMock();
  183. container.RegisterInstanceAs(testMethodDecoratorMock.Object, "foo");
  184. var registry = CreateDecoratorRegistry();
  185. List<string> unprocessedTags;
  186. registry.DecorateTestMethod(CreateGenerationContext("dummy"), null, new Tag[] { new Tag("dummy") }, out unprocessedTags);
  187. testMethodDecoratorMock.Verify(d => d.DecorateFrom(It.IsAny<TestClassGenerationContext>(), It.IsAny<CodeMemberMethod>()));
  188. }
  189. [Test]
  190. public void Should_not_decorate_test_method_when_not_applicable()
  191. {
  192. var testMethodDecoratorMock = CreateTestMethodDecoratorMock();
  193. testMethodDecoratorMock.Setup(d => d.CanDecorateFrom(It.IsAny<TestClassGenerationContext>(), It.IsAny<CodeMemberMethod>()))
  194. .Returns(false);
  195. container.RegisterInstanceAs(testMethodDecoratorMock.Object, "foo");
  196. var registry = CreateDecoratorRegistry();
  197. List<string> unprocessedTags;
  198. registry.DecorateTestMethod(CreateGenerationContext("dummy"), null, new Tag[] { new Tag("dummy") }, out unprocessedTags);
  199. testMethodDecoratorMock.Verify(d => d.DecorateFrom(It.IsAny<TestClassGenerationContext>(), It.IsAny<CodeMemberMethod>()), Times.Never());
  200. }
  201. }
  202. internal class DecoratorRegistryStub : IDecoratorRegistry
  203. {
  204. public void DecorateTestClass(TestClassGenerationContext generationContext, out List<string> unprocessedTags)
  205. {
  206. unprocessedTags = new List<string>();
  207. }
  208. public void DecorateTestMethod(TestClassGenerationContext generationContext, CodeMemberMethod testMethod, IEnumerable<Tag> tags, out List<string> unprocessedTags)
  209. {
  210. unprocessedTags = new List<string>();
  211. }
  212. }
  213. }