private void GenerateCodeFromFeature(Feature feature, TextWriter writer)
		{
		    CodeDomHelper codeDomHelper = new CodeDomHelper(CodeDomProviderLanguage.CSharp);
		    XUnitTestGeneratorProvider xUnitTestGeneratorProvider = new XUnitTestGeneratorProvider();
		    SpecFlowUnitTestConverter converter = FactoryMethods.CreateUnitTestConverter(xUnitTestGeneratorProvider);
			var codeNamespace = converter.GenerateUnitTestFixture(feature, "TestClassName", "Target.Namespace");

			CSharpCodeProvider codeProvider = new CSharpCodeProvider();
			CodeGeneratorOptions options = new CodeGeneratorOptions();
			codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, options);
		}
        private void CompareWithExpectedResult(Feature feature, string expectedResultFileName)
        {
            string expected = TestFileHelper.ReadFile(expectedResultFileName);
            string got = GenerateCodeFromFeature(feature);

            Assert.AreEqual(expected, got);
        }
 private void GenerateCodeFromFeature(Feature feature, string fileName)
 {
     using (var writer = new StreamWriter(fileName, false, Encoding.UTF8))
     {
         GenerateCodeFromFeature(feature, writer);
     }
 }
Ejemplo n.º 4
0
        private object CompileAndCreateTest(string fileName, Feature feature)
        {
            string className = Path.GetFileNameWithoutExtension(fileName);
            const string targetNamespace = "Target.Namespace";
            // the row test generation has to be set to false, because our verifications support the old style test generation only
            SpecFlowUnitTestConverter converter = new SpecFlowUnitTestConverter(CreateUnitTestGeneratorProvider(), new CodeDomHelper(CodeDomProviderLanguage.CSharp), new GeneratorConfiguration { AllowRowTests = false, AllowDebugGeneratedFiles = true }); 
            var codeNamespace = converter.GenerateUnitTestFixture(feature, className, targetNamespace);
            var compileUnit = new CodeCompileUnit();
            compileUnit.Namespaces.Add(codeNamespace);

            Dictionary<string, string> providerOptions = new Dictionary<string, string>();
            providerOptions["CompilerVersion"] = "v3.5";

            CSharpCodeProvider codeProvider = new CSharpCodeProvider(providerOptions);

            CompilerParameters compilerParameters = new CompilerParameters();
            compilerParameters.GenerateInMemory = true;
            compilerParameters.TempFiles.KeepFiles = true;

            compilerParameters.ReferencedAssemblies.Add(
                TestFileHelper.GetAssemblyPath(typeof (GeneratedCodeAttribute))); //System
            compilerParameters.ReferencedAssemblies.Add(
                TestFileHelper.GetAssemblyPath(typeof (TestAttribute))); //NUnit
            compilerParameters.ReferencedAssemblies.Add(
                TestFileHelper.GetAssemblyPath(typeof (ITestRunner))); //TechTalk.SpecFlow

            var results = codeProvider.CompileAssemblyFromDom(compilerParameters, compileUnit);

            if (results.NativeCompilerReturnValue != 0)
                throw new InvalidOperationException("Test cannot be compiled: " + 
                                                    string.Join(Environment.NewLine, results.Errors.Cast<CompilerError>().Select(ce => ce.ToString()).ToArray()));

            Type testType = results.CompiledAssembly.GetType(targetNamespace + "." + className, true);
            return Activator.CreateInstance(testType);
        }
Ejemplo n.º 5
0
 private void SerializeFeature(Feature feature, string fileName)
 {
     using (var writer = new StreamWriter(fileName, false, Encoding.UTF8))
     {
         SerializeFeature(feature, writer);
     }
 }
Ejemplo n.º 6
0
        public static bool MatchPrefix(this ITagFilterMatcher tagFilterMatcher, string tagFilter, Feature feature)
        {
            if (feature.Tags == null)
                return false;

            return tagFilterMatcher.MatchPrefix(tagFilter, feature.Tags.Select(t => t.Name));
        }
Ejemplo n.º 7
0
        private void CompareWithExpectedResult(Feature feature, string expectedResultFileName)
        {
            string expected = TestFileHelper.ReadFile(expectedResultFileName).Replace("\r", "");
            string got = SerializeFeature(feature).Replace("\r", "");

            Assert.AreEqual(expected, got);
        }
 private string GenerateCodeFromFeature(Feature feature)
 {
     using (var writer = new Utf8StringWriter())
     {
         GenerateCodeFromFeature(feature, writer);
         return writer.ToString();
     }
 }
        public void Should_UnitTestFeatureGeneratorProvider_create_UnitTestFeatureGenerator_instance()
        {
            var generatorProvider = CreateUnitTestFeatureGeneratorProvider();
            Feature anyFeature = new Feature();
            var generator = generatorProvider.CreateGenerator(anyFeature);

            generator.Should().BeOfType<UnitTestFeatureGenerator>();
        }
        public void Should_UnitTestFeatureGeneratorProvider_create_valid_instance()
        {
            var generatorProvider = CreateUnitTestFeatureGeneratorProvider();
            Feature anyFeature = new Feature();
            var generator = generatorProvider.CreateGenerator(anyFeature);

            generator.Should().NotBeNull();
        }
Ejemplo n.º 11
0
 private string SerializeFeature(Feature feature)
 {
     using (var writer = new Utf8StringWriter())
     {
         SerializeFeature(feature, writer);
         return writer.ToString();
     }
 }
Ejemplo n.º 12
0
        public ScenarioOutlineViewModel(Feature feature, ScenarioOutline scenarioOutline)
            : base(scenarioOutline, feature)
        {
            var bgSteps = feature.Background != null ? feature.Background.Steps.AsEnumerable() : Enumerable.Empty<ScenarioStep>();

            StepTemplates = bgSteps.Concat(scenarioOutline.Steps).Select(x => new StepTemplateViewModel(x)).ToList();

            Examples = scenarioOutline.Examples.ExampleSets.Select(x => new ScenarioOutlineExampleSetViewModel(this, x)).ToList();
        }
Ejemplo n.º 13
0
        public void Should_create_UnitTestFeatureGenerator_with_default_setup()
        {
            var featureGeneratorRegistry = CreateFeatureGeneratorRegistry();

            Feature anyFeature = new Feature();
            var generator = featureGeneratorRegistry.CreateGenerator(anyFeature);

            generator.Should().BeOfType<UnitTestFeatureGenerator>();
        }
Ejemplo n.º 14
0
 protected override void ExecuteTests(object test, Feature feature)
 {
     NUnitTestExecutor.ExecuteNUnitTests(test,
         delegate(Exception exception)
         {
             Assert.IsInstanceOf(typeof(InconclusiveException), exception);
             return true;
         });
 }
Ejemplo n.º 15
0
        private void GenerateCodeFromFeature(Feature feature, TextWriter writer)
        {
            SpecFlowUnitTestConverter converter = FactoryMethods.CreateUnitTestConverter(new NUnitTestConverter());
            var codeNamespace = converter.GenerateUnitTestFixture(feature, "TestClassName", "Target.Namespace");

            CSharpCodeProvider codeProvider = new CSharpCodeProvider();
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, options);
        }
Ejemplo n.º 16
0
        public static bool GetTagValue(this ITagFilterMatcher tagFilterMatcher, string tagFilter, Feature feature, out string value)
        {
            if (feature.Tags == null)
            {
                value = null;
                return false;
            }

            return tagFilterMatcher.GetTagValue(tagFilter, feature.Tags.Select(t => t.Name), out value);
        }
Ejemplo n.º 17
0
        public FeatureViewModel(Feature feature)
        {
            FeatureNode = feature;
            if (feature.Background != null)
            {
                BackgroundSteps = feature.Background.Steps.Select(x => new StepViewModel(x)).ToList();
            }

            Scenarios = feature.Scenarios.Select(x => x is ScenarioOutline ? (ScenarioViewModelBase)new ScenarioOutlineViewModel(feature, (ScenarioOutline) x) : new ScenarioViewModel(feature, x)).ToList();
        }
        private static string GetPlainText(string fileName, Scenario scenario, Feature feature)
        {
            var lines = File.ReadAllLines(fileName);

            var nextScenario = feature.Scenarios.SkipWhile(s => s != scenario).Skip(1).FirstOrDefault();

            int startLine = scenario.FilePosition.Line;
            int endLine = nextScenario == null ? lines.Count() : nextScenario.FilePosition.Line - 1;

            return string.Join(Environment.NewLine, lines.Skip(startLine - 1).Take(endLine - startLine + 1)).Replace("\t", "  ");
        }
        private void GenerateCodeFromFeature(Feature feature, TextWriter writer)
        {
            var codeDomHelper = new CodeDomHelper(CodeDomProviderLanguage.CSharp);
            var mbUnitTestGeneratorProvider = new MbUnitTestGeneratorProvider();
            var converter = new SpecFlowUnitTestConverter(mbUnitTestGeneratorProvider, codeDomHelper, true);
            CodeNamespace codeNamespace = converter.GenerateUnitTestFixture(feature, "TestClassName", "Target.Namespace");

            var codeProvider = new CSharpCodeProvider();
            var options = new CodeGeneratorOptions();
            codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, options);
        }
Ejemplo n.º 20
0
 public Feature GetResult()
 {
     var feature = new Feature(
         title,
         tags,
         description,
         background == null ? null : background.GetResult(),
         scenarios.Select(sb => sb.GetResult()).ToArray());
     feature.SourceFile = sourceFilePath;
     return feature;
 }
    /// <summary>
    /// Generates the unit test fixture.
    /// </summary>
    /// <param name="feature">The feature.</param>
    /// <param name="testClassName">Name of the test class.</param>
    /// <param name="targetNamespace">The target namespace.</param>
    /// <returns>Instance of CodeNamespace.</returns>
    public CodeNamespace GenerateUnitTestFixture(Feature feature, string testClassName, string targetNamespace)
    {
      CodeNamespace result = this.baseFeatureGenerator.GenerateUnitTestFixture(feature, testClassName, targetNamespace);

      if (this.IsLiveTest(feature))
      {
        this.DecorateStaticMethods(result);
        this.DecorateFixtureClasses(result);
      }

      return result;
    }
Ejemplo n.º 22
0
        public string GetFeatureText(Feature feature, CultureInfo defaultLanguage = null)
        {
            defaultLanguage = defaultLanguage ?? new CultureInfo("en-US");
            var dialectServices = new GherkinDialectServices(defaultLanguage);
            var dialect = dialectServices.GetGherkinDialect(feature);

            var result = new StringBuilder();

            AppendHeader(defaultLanguage, dialect, result);

            AppendTags(result, feature.Tags);
            AppendFeatureLine(feature, result);
            AppendLine(result);

            if (feature.Background != null)
            {
                AppendBackgroundLine(feature.Background, result);
                AppendSteps(feature.Background.Steps, result);
                AppendLine(result);
            }

            foreach (var scenario in feature.Scenarios)
            {
                var outline = scenario as ScenarioOutline;
                bool isOutline = outline != null;

                AppendTags(result, scenario.Tags);
                AppendScenarioLine(result, scenario, dialect, isOutline);

                AppendSteps(scenario.Steps, result);

                AppendLine(result);

                if (isOutline)
                {
                    foreach (var exampleSet in outline.Examples.ExampleSets)
                    {
                        AppendTags(result, exampleSet.Tags);
                        AppendExampleSetLine(result, dialect, exampleSet);
                        AppendTable(result, exampleSet.Table, indent);
                        AppendLine(result);
                    }
                }

                AppendLine(result);
            }

            return result.ToString();
        }
Ejemplo n.º 23
0
        public TestClassGenerationContext(Feature feature, CodeNamespace ns, CodeTypeDeclaration testClass, CodeMemberMethod testClassInitializeMethod, CodeMemberMethod testClassCleanupMethod, CodeMemberMethod testInitializeMethod, CodeMemberMethod testCleanupMethod, CodeMemberMethod scenarioInitializeMethod, CodeMemberMethod scenarioCleanupMethod, bool generateRowTests, bool generateAsynchTests)
        {
            Feature = feature;
            Namespace = ns;
            TestClass = testClass;
            TestClassInitializeMethod = testClassInitializeMethod;
            TestClassCleanupMethod = testClassCleanupMethod;
            TestInitializeMethod = testInitializeMethod;
            TestCleanupMethod = testCleanupMethod;
            ScenarioInitializeMethod = scenarioInitializeMethod;
            ScenarioCleanupMethod = scenarioCleanupMethod;
            GenerateRowTests = generateRowTests;
            GenerateAsynchTests = generateAsynchTests;

            CustomData = new Dictionary<string, object>();
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Converts the provided <see cref="SpecFlow.Feature"/> instance into a <see cref="Augurk.Entities.Feature"/> instance.
        /// </summary>
        /// <param name="feature">The <see cref="SpecFlow.Feature"/> instance that should be converted.</param>
        /// <returns>The converted <see cref="Augurk.Entities.Feature"/> instance.</returns>
        public static Feature ConvertToFeature(this SpecFlow.Feature feature)
        {
            if (feature == null)
            {
                throw new ArgumentNullException("feature");
            }

            return(new Feature()
            {
                Title = feature.Title,
                Description = feature.Description,
                Tags = feature.Tags.ConvertToStrings(),
                Scenarios = feature.Scenarios.Select(scenario => scenario.ConvertToScenario()).ToArray(),
                Background = feature.Background.ConvertToBackground()
            });
        }
Ejemplo n.º 25
0
 public static FeatureVm CreateFromSpecFlowFeature(Feature specFlowFeature)
 {
     return new FeatureVm
                {
                    Background = specFlowFeature.Background,
                    Comments = specFlowFeature.Comments,
                    Description = specFlowFeature.Description,
                    Directory = Path.GetDirectoryName(specFlowFeature.SourceFile),
                    FileName = Path.GetFileName(specFlowFeature.SourceFile),
                    Keyword = specFlowFeature.Keyword,
                    Language = specFlowFeature.Language,
                    Scenarios = ScenarioListVm.CreateFromSpecFlowScenarios(specFlowFeature.Scenarios),
                    SourceFile = specFlowFeature.SourceFile,
                    Tags = specFlowFeature.Tags,
                    Title = specFlowFeature.Title
                };
 }
Ejemplo n.º 26
0
        public void Should_call_provider_wiht_the_given_feature()
        {
            var dummyGenerator = new Mock<IFeatureGenerator>().Object;

            var genericHighPrioProvider = new Mock<IFeatureGeneratorProvider>();
            genericHighPrioProvider.Setup(p => p.CreateGenerator(It.IsAny<Feature>())).Returns(dummyGenerator);
            genericHighPrioProvider.Setup(p => p.CanGenerate(It.IsAny<Feature>())).Returns(true); // generic
            genericHighPrioProvider.Setup(p => p.Priority).Returns(1); // high-prio

            container.RegisterInstanceAs(genericHighPrioProvider.Object, "custom");

            var featureGeneratorRegistry = CreateFeatureGeneratorRegistry();

            Feature theFeature = new Feature();
            featureGeneratorRegistry.CreateGenerator(theFeature);

            genericHighPrioProvider.Verify(p => p.CreateGenerator(theFeature), Times.Once());
        }
Ejemplo n.º 27
0
        public void WhenIParseTheFile()
        {
            var contentReader = new StringReader(gherkinContent);

            try
            {
                feature = parser.Parse(contentReader, "sample.feature");
            }
            catch(SpecFlowParserException ex)
            {
                parsingErrors = ex;
                Console.WriteLine("-> parsing errors");
                foreach (ErrorDetail errorDetail in parsingErrors.ErrorDetails)
                {
                    Console.WriteLine("-> {0}:{1} {2}", errorDetail.Line, errorDetail.Column, errorDetail.Message);
                }
            }
        }
Ejemplo n.º 28
0
        public void Should_use_generic_provider_with_higher_priority()
        {
            var dummyGenerator = new Mock<IFeatureGenerator>().Object;

            var genericHighPrioProvider = new Mock<IFeatureGeneratorProvider>();
            genericHighPrioProvider.Setup(p => p.CreateGenerator(It.IsAny<Feature>())).Returns(dummyGenerator);
            genericHighPrioProvider.Setup(p => p.CanGenerate(It.IsAny<Feature>())).Returns(true); // generic
            genericHighPrioProvider.Setup(p => p.Priority).Returns(1); // high-prio

            container.RegisterInstanceAs(genericHighPrioProvider.Object, "custom");

            var featureGeneratorRegistry = CreateFeatureGeneratorRegistry();

            Feature anyFeature = new Feature();
            var generator = featureGeneratorRegistry.CreateGenerator(anyFeature);

            generator.Should().Be(dummyGenerator);
        }
Ejemplo n.º 29
0
        public void Should_skip_high_priority_provider_when_not_applicable()
        {
            var dummyGenerator = new Mock<IFeatureGenerator>().Object;

            Feature theFeature = new Feature();

            var genericHighPrioProvider = new Mock<IFeatureGeneratorProvider>();
            genericHighPrioProvider.Setup(p => p.CreateGenerator(It.IsAny<Feature>())).Returns(dummyGenerator);
            genericHighPrioProvider.Setup(p => p.CanGenerate(theFeature)).Returns(false); // not applicable for aFeature
            genericHighPrioProvider.Setup(p => p.Priority).Returns(1); // high-prio

            container.RegisterInstanceAs(genericHighPrioProvider.Object, "custom");

            var featureGeneratorRegistry = CreateFeatureGeneratorRegistry();

            var generator = featureGeneratorRegistry.CreateGenerator(theFeature);

            generator.Should().BeOfType<UnitTestFeatureGenerator>();
        }
Ejemplo n.º 30
0
        public Feature GetResult()
        {
            var scenarioResults = scenarios.Select(sb => sb.GetResult()).ToArray();

            comments.Sort((c1, c2) => c1.FilePosition.Line.CompareTo(c2.FilePosition.Line));

            var feature = new Feature(
                keyword,
                title,
                tags,
                description,
                background == null ? null : background.GetResult(),
                scenarioResults,
                comments.ToArray());

            feature.SourceFile = sourceFilePath;
            feature.FilePosition = position;
            return feature;
        }
Ejemplo n.º 31
0
 protected virtual void AppendFeatureLine(Feature feature, StringBuilder result)
 {
     AppendNodeLine(result, feature.FilePosition, "{0}: {1}", feature.Keyword, feature.Title);
     if (!string.IsNullOrEmpty(feature.Description))
         AppendMulitLine(result, feature.Description);
 }