PageRenderTime 339ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/IdeIntegrationTests/RemoteAppDomainTestGeneratorFactoryTests.cs

http://github.com/techtalk/SpecFlow
C# | 293 lines | 251 code | 41 blank | 1 comment | 0 complexity | 9d26b9e591e5c2ba8a4f86e28a6e3411 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using Moq;
  9. using NUnit.Framework;
  10. using TechTalk.SpecFlow;
  11. using TechTalk.SpecFlow.Generator;
  12. using TechTalk.SpecFlow.Generator.Interfaces;
  13. using TechTalk.SpecFlow.IdeIntegration;
  14. using Should;
  15. using TechTalk.SpecFlow.IdeIntegration.Generator;
  16. using TechTalk.SpecFlow.IdeIntegration.Tracing;
  17. using TechTalk.SpecFlow.Utils;
  18. namespace IdeIntegrationTests
  19. {
  20. [TestFixture]
  21. public class RemoteAppDomainTestGeneratorFactoryTests
  22. {
  23. private string currentGeneratorFolder;
  24. private Mock<IIdeTracer> tracerStub;
  25. [SetUp]
  26. public void Setup()
  27. {
  28. currentGeneratorFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
  29. tracerStub = new Mock<IIdeTracer>();
  30. }
  31. private RemoteAppDomainTestGeneratorFactory CreateRemoteAppDomainTestGeneratorFactory()
  32. {
  33. return CreateRemoteAppDomainTestGeneratorFactory(currentGeneratorFolder);
  34. }
  35. private RemoteAppDomainTestGeneratorFactory CreateRemoteAppDomainTestGeneratorFactory(string generatorFolder)
  36. {
  37. var factory = new RemoteAppDomainTestGeneratorFactory(tracerStub.Object);
  38. factory.Setup(generatorFolder);
  39. return factory;
  40. }
  41. [Test]
  42. public void Should_be_able_to_initialize()
  43. {
  44. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  45. {
  46. remoteFactory.EnsureInitialized();
  47. remoteFactory.IsRunning.ShouldBeTrue();
  48. }
  49. }
  50. [Test]
  51. public void Should_be_able_to_return_generator_version()
  52. {
  53. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  54. {
  55. var version = remoteFactory.GetGeneratorVersion();
  56. version.ShouldNotBeNull();
  57. version.ShouldEqual(TestGeneratorFactory.GeneratorVersion);
  58. }
  59. }
  60. [Test]
  61. public void Should_be_able_to_create_generator_with_default_config()
  62. {
  63. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  64. {
  65. var generator = remoteFactory.CreateGenerator(new ProjectSettings());
  66. generator.ShouldNotBeNull();
  67. }
  68. }
  69. [Serializable]
  70. private class DummyGenerator : MarshalByRefObject, ITestGenerator
  71. {
  72. public TestGeneratorResult GenerateTestFile(FeatureFileInput featureFileInput, GenerationSettings settings)
  73. {
  74. throw new NotImplementedException();
  75. }
  76. public Version DetectGeneratedTestVersion(FeatureFileInput featureFileInput)
  77. {
  78. throw new NotImplementedException();
  79. }
  80. public string GetTestFullPath(FeatureFileInput featureFileInput)
  81. {
  82. throw new NotImplementedException();
  83. }
  84. public void Dispose()
  85. {
  86. //nop;
  87. }
  88. public override string ToString()
  89. {
  90. return "DummyGenerator";
  91. }
  92. }
  93. [Test]
  94. public void Should_create_custom_generator_when_configured_so()
  95. {
  96. var configurationHolder = new SpecFlowConfigurationHolder(string.Format(@"
  97. <specFlow>
  98. <generator>
  99. <dependencies>
  100. <register type=""{0}"" as=""{1}""/>
  101. </dependencies>
  102. </generator>
  103. </specFlow>",
  104. typeof(DummyGenerator).AssemblyQualifiedName,
  105. typeof(ITestGenerator).AssemblyQualifiedName));
  106. var projectSettings = new ProjectSettings();
  107. projectSettings.ConfigurationHolder = configurationHolder;
  108. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  109. {
  110. var generator = remoteFactory.CreateGenerator(projectSettings);
  111. generator.ToString().ShouldEqual("DummyGenerator"); // since the type is wrapped, we can only check it this way
  112. }
  113. }
  114. [Test]
  115. public void Should_be_able_to_load_generator_from_another_folder()
  116. {
  117. using(var tempFolder = new TempFolder())
  118. {
  119. var runtimeAssemblyFile = typeof(BindingAttribute).Assembly.Location;
  120. var generatorAssemblyFile = typeof(TestGeneratorFactory).Assembly.Location;
  121. var utilsAssemblyFile = typeof(FileSystemHelper).Assembly.Location;
  122. FileSystemHelper.CopyFileToFolder(runtimeAssemblyFile, tempFolder.FolderName);
  123. FileSystemHelper.CopyFileToFolder(generatorAssemblyFile, tempFolder.FolderName);
  124. FileSystemHelper.CopyFileToFolder(utilsAssemblyFile, tempFolder.FolderName);
  125. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory(tempFolder.FolderName))
  126. {
  127. var generator = remoteFactory.CreateGenerator(new ProjectSettings());
  128. generator.ShouldNotBeNull();
  129. }
  130. }
  131. }
  132. [Test]
  133. public void Should_cleanup_ater_dispose()
  134. {
  135. RemoteAppDomainTestGeneratorFactory remoteFactory;
  136. using (remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  137. {
  138. remoteFactory.EnsureInitialized();
  139. }
  140. remoteFactory.IsRunning.ShouldBeFalse();
  141. }
  142. [Test]
  143. public void Should_start_running_delayed()
  144. {
  145. RemoteAppDomainTestGeneratorFactory remoteFactory;
  146. using (remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  147. {
  148. remoteFactory.IsRunning.ShouldBeFalse();
  149. }
  150. }
  151. [Test]
  152. public void Should_cleanup_after_generator_disposed_when_timeout_ellapses()
  153. {
  154. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  155. {
  156. remoteFactory.AppDomainCleanupTime = TimeSpan.FromSeconds(1);
  157. var generator = remoteFactory.CreateGenerator(new ProjectSettings());
  158. generator.Dispose();
  159. Thread.Sleep(TimeSpan.FromSeconds(1.1));
  160. remoteFactory.IsRunning.ShouldBeFalse();
  161. }
  162. }
  163. [Test]
  164. public void Should_not_cleanup_after_generator_disposed_immediately()
  165. {
  166. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  167. {
  168. var generator = remoteFactory.CreateGenerator(new ProjectSettings());
  169. generator.Dispose();
  170. remoteFactory.IsRunning.ShouldBeTrue();
  171. }
  172. }
  173. [Test]
  174. public void Should_not_cleanup_when_one_generator_is_still_used()
  175. {
  176. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  177. {
  178. var generator1 = remoteFactory.CreateGenerator(new ProjectSettings());
  179. var generator2 = remoteFactory.CreateGenerator(new ProjectSettings());
  180. generator1.Dispose();
  181. remoteFactory.IsRunning.ShouldBeTrue();
  182. }
  183. }
  184. [Test]
  185. public void Should_cleanup_when_all_generators_are_disposed()
  186. {
  187. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  188. {
  189. remoteFactory.AppDomainCleanupTime = TimeSpan.FromSeconds(1);
  190. var generator1 = remoteFactory.CreateGenerator(new ProjectSettings());
  191. var generator2 = remoteFactory.CreateGenerator(new ProjectSettings());
  192. generator1.Dispose();
  193. generator2.Dispose();
  194. Thread.Sleep(TimeSpan.FromSeconds(1.1));
  195. remoteFactory.IsRunning.ShouldBeFalse();
  196. }
  197. }
  198. [Test]
  199. public void Should_be_able_generate_from_a_simple_valid_feature()
  200. {
  201. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  202. {
  203. var generator = remoteFactory.CreateGenerator(new ProjectSettings()
  204. {
  205. ProjectFolder = Path.GetTempPath()
  206. });
  207. FeatureFileInput featureFileInput = new FeatureFileInput("Test.feature")
  208. {
  209. FeatureFileContent = @"
  210. Feature: Addition
  211. @mytag
  212. Scenario: Add two numbers
  213. Given I have entered 50 into the calculator
  214. And I have entered 70 into the calculator
  215. When I press add
  216. Then the result should be 120 on the screen
  217. "
  218. };
  219. var result = generator.GenerateTestFile(featureFileInput, new GenerationSettings());
  220. result.ShouldNotBeNull();
  221. result.Success.ShouldBeTrue();
  222. result.GeneratedTestCode.ShouldNotBeNull();
  223. }
  224. }
  225. [Test]
  226. public void Should_be_able_generate_from_a_simple_invalid_feature()
  227. {
  228. using (var remoteFactory = CreateRemoteAppDomainTestGeneratorFactory())
  229. {
  230. var generator = remoteFactory.CreateGenerator(new ProjectSettings()
  231. {
  232. ProjectFolder = Path.GetTempPath()
  233. });
  234. FeatureFileInput featureFileInput = new FeatureFileInput("Test.feature")
  235. {
  236. FeatureFileContent = @"
  237. Feature: Addition
  238. Scenario: Add two numbers
  239. Given I have entered 50 into the calculator
  240. AndXXX the keyword is misspelled
  241. "
  242. };
  243. var result = generator.GenerateTestFile(featureFileInput, new GenerationSettings());
  244. result.ShouldNotBeNull();
  245. result.Success.ShouldBeFalse();
  246. result.Errors.ShouldNotBeNull();
  247. result.Errors.ShouldNotBeEmpty();
  248. }
  249. }
  250. }
  251. }