Exemple #1
0
        public void GenerateScenarioExampleTests()
        {
            var parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFile))
            {
                var feature = parser.Parse(reader, null);
                feature.Should().NotBeNull();

                var           sampleTestGeneratorProvider = new SimpleTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));
                var           converter = CreateUnitTestConverter(sampleTestGeneratorProvider);
                CodeNamespace code      = converter.GenerateUnitTestFixture(feature, "TestClassName", "Target.Namespace");

                code.Should().NotBeNull();


                // make sure name space is changed
                code.Name.Should().Be(SimpleTestGeneratorProvider.DefaultNameSpace);

                // make sure all method titles are changed correctly
                List <string> methodTitles = new List <string>();
                for (int i = 0; i < code.Types[0].Members.Count; i++)
                {
                    methodTitles.Add(code.Types[0].Members[i].Name);
                }

                foreach (var title in sampleTestGeneratorProvider.newTitles)
                {
                    methodTitles.Contains(title).Should().BeTrue();
                }
            }
        }
        public void Should_register_testOutputHelper_on_scenario_setup()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFile))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var           converter = provider.CreateUnitTestConverter();
                CodeNamespace code      = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var scenarioStartMethod = code.Class().Members().Single(m => m.Name == @"ScenarioSetup");

                scenarioStartMethod.Statements.Count.Should().Be(2);
                var expression = (scenarioStartMethod.Statements[1] as CodeExpressionStatement).Expression;
                var method     = (expression as CodeMethodInvokeExpression).Method;
                (method.TargetObject as CodePropertyReferenceExpression).PropertyName.Should().Be("ScenarioContainer");
                method.MethodName.Should().Be("RegisterInstanceAs");
                method.TypeArguments.Should().NotBeNullOrEmpty();
                method.TypeArguments[0].BaseType.Should().Be("Xunit.Abstractions.ITestOutputHelper");

                ((expression as CodeMethodInvokeExpression).Parameters[0] as CodeVariableReferenceExpression).VariableName.Should().Be("_testOutputHelper");
            }
        }
        public void GenerateScenarioExampleTests()
        {
            var parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFile))
            {
                var feature = parser.Parse(reader, null);
                feature.Should().NotBeNull();

                var           sampleTestGeneratorProvider = new SimpleTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));
                var           converter = CreateUnitTestConverter(sampleTestGeneratorProvider);
                CodeNamespace code      = converter.GenerateUnitTestFixture(feature, null, null);

                code.Should().NotBeNull();

                // make sure name space is changed
                Assert.Equal(code.Name, SimpleTestGeneratorProvider.DefaultNameSpace);

                Assert.Equal("SampleFeatureFileThatsGotWeirdNamesFeature", code.Types[0].Name);

                foreach (var method in code.Types[0].Members.OfType <CodeMemberMethod>())
                {
                    var parameterNames = method.Parameters.Cast <CodeParameterDeclarationExpression>().Select(v => v.Name).ToArray();
                    parameterNames.GroupBy(v => v).Where(kv => kv.Count() > 1).Should().BeEmpty("All parameters should be unique");
                    foreach (var parameter in parameterNames)
                    {
                        parameter.Should().NotBeNullOrWhiteSpace();
                    }
                }
            }
        }
        public void MsTestGeneratorShouldInvokeFeatureSetupMethodWithGlobalNamespaceAlias()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFileWithMultipleExampleSets))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var sampleTestGeneratorProvider = new MsTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var           converter = sampleTestGeneratorProvider.CreateUnitTestConverter();
                CodeNamespace code      = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var featureSetupCall = code
                                       .Class()
                                       .Members()
                                       .Single(m => m.Name == "TestInitialize")
                                       .Statements
                                       .OfType <CodeConditionStatement>()
                                       .First()
                                       .TrueStatements
                                       .OfType <CodeExpressionStatement>()
                                       .First()
                                       .Expression
                                       .As <CodeMethodInvokeExpression>();

                featureSetupCall.Should().NotBeNull();
                featureSetupCall.Method.MethodName.Should().Be("FeatureSetup");
                featureSetupCall.Method.TargetObject.As <CodeTypeReferenceExpression>().Type.Options.Should().Be(CodeTypeReferenceOptions.GlobalReference);
            }
        }
        public void MsTestGeneratorShouldSetDescriptionCorrectlyWhenExampleSetIdentifierIsUsed()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFileWithMultipleExampleSets))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var sampleTestGeneratorProvider = new MsTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var           converter = sampleTestGeneratorProvider.CreateUnitTestConverter();
                CodeNamespace code      = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var descriptionAttributeForFirstScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_ExampleSet0_Something").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForFirstScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: something");
                var descriptionAttributeForSecondScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_ExampleSet0_SomethingElse").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForSecondScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: something else");
                var descriptionAttributeForThirdScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_ExampleSet1_Another").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForThirdScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: another");
                var descriptionAttributeForFourthScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_ExampleSet1_AndAnother").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForFourthScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: and another");
            }
        }
        public void Should_initialize_testOutputHelper_field_in_constructor()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFile))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var           converter = provider.CreateUnitTestConverter();
                CodeNamespace code      = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var classContructor = code.Class().Members().Single(m => m.Name == ".ctor");
                classContructor.Should().NotBeNull();
                classContructor.Parameters.Count.Should().Be(2);
                classContructor.Parameters[1].Type.BaseType.Should().Be("Xunit.Abstractions.ITestOutputHelper");
                classContructor.Parameters[1].Name.Should().Be("testOutputHelper");

                var initOutputHelper = classContructor.Statements.OfType <CodeAssignStatement>().First();
                initOutputHelper.Should().NotBeNull();
                ((CodeFieldReferenceExpression)(initOutputHelper.Left)).FieldName.Should().Be("_testOutputHelper");
                ((CodeVariableReferenceExpression)(initOutputHelper.Right)).VariableName.Should().Be("testOutputHelper");
            }
        }
        public void GenerateScenarioExampleTests()
        {
            var parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));
            using (var reader = new StringReader(SampleFeatureFile))
            {
                var feature = parser.Parse(reader, null);                    
                Assert.IsNotNull(feature);

                var sampleTestGeneratorProvider = new SimpleTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));
                var converter = CreateUnitTestConverter(sampleTestGeneratorProvider);
                CodeNamespace code = converter.GenerateUnitTestFixture(feature, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                  
                // make sure name space is changed
                Assert.AreEqual(code.Name, SimpleTestGeneratorProvider.DefaultNameSpace);

                // make sure all method titles are changed correctly
                List<string> methodTitles = new List<string>();
                for (int i = 0; i < code.Types[0].Members.Count; i++)
                {
                    methodTitles.Add(code.Types[0].Members[i].Name);
                }

                foreach (var title in sampleTestGeneratorProvider.newTitles)
                {
                    Assert.IsTrue(methodTitles.Contains(title));
                }
            }
        }
Exemple #8
0
        protected SpecFlowDocument ParseDocument(string content, string path, SpecFlowGherkinParser parser)
        {
            var sourceMap = this.SpecFlowSourceMapper.ReadSourceMap(content);

            this.FeatureFilePath = sourceMap?.SourcePath ?? path;

            return(parser.Parse(new StringReader(content), this.FeatureFilePath));
        }
Exemple #9
0
        public void Parser_handles_empty_feature_file_without_error()
        {
            var parser = new SpecFlowGherkinParser(CultureInfo.GetCultureInfo("en"));

            Action act = () => parser.Parse(new StringReader(""), null);

            act.ShouldNotThrow();
        }
Exemple #10
0
        public void Parser_handles_empty_feature_file_without_error()
        {
            var parser = new SpecFlowGherkinParser(CultureInfo.GetCultureInfo("en"));

            Action act = () => parser.Parse(new StringReader(""), null);

            act.Should().NotThrow();
        }
        protected SpecFlowDocument CreateSpecFlowDocument(string document)
        {
            var parser = new SpecFlowGherkinParser(new CultureInfo("en-GB"));

            using (var reader = new StringReader(document))
            {
                return(parser.Parse(reader, new SpecFlowDocumentLocation("Test")));
            }
        }
Exemple #12
0
        public SpecFlowDocument ParseDocumentFromString(string documentSource, CultureInfo parserCultureInfo = null)
        {
            var parser = new SpecFlowGherkinParser(parserCultureInfo ?? new CultureInfo("en-US"));

            using (var reader = new StringReader(documentSource))
            {
                var document = parser.Parse(reader, null);
                document.Should().NotBeNull();
                return(document);
            }
        }
Exemple #13
0
        public void Parser_throws_meaningful_exception_when_Examples_are_missing_in_Scenario_Outline()
        {
            var feature = @"Feature: Missing
                            Scenario Outline: No Examples";

            var parser = new SpecFlowGherkinParser(CultureInfo.GetCultureInfo("en"));

            Action act = () => parser.Parse(new StringReader(feature), null);

            act.Should().Throw <SemanticParserException>().WithMessage("(2:29): Scenario Outline 'No Examples' has no examples defined")
            .And.Location.Line.Should().Be(2);
        }
Exemple #14
0
        public void Parser_doesnt_throw_exception_when_Examples_are_missing_in_Scenario_Outline()
        {
            // this is accepted by Gherkin v6 and treated as Scenario
            var feature = @"Feature: Missing
                            Scenario Outline: No Examples";

            var parser = new SpecFlowGherkinParser(CultureInfo.GetCultureInfo("en"));

            Action act = () => parser.Parse(new StringReader(feature), null);

            act.Should().NotThrow();
        }
Exemple #15
0
 public static List<SpecFlowDocument> GetParsedFeatures(IEnumerable<string> featureFiles, CultureInfo featureLanguage)
 {
     List<SpecFlowDocument> parsedSpecFlowDocument = new List<SpecFlowDocument>();
     foreach (var featureFile in featureFiles)
     {
         SpecFlowGherkinParser parser = new SpecFlowGherkinParser(featureLanguage);
         using (var reader = new StreamReader(featureFile))
         {
             var specFlowFeature = parser.Parse(reader, featureFile);
             parsedSpecFlowDocument.Add(specFlowFeature);
         }
     }
     return parsedSpecFlowDocument;
 }
Exemple #16
0
        public void Parser_doesnt_throw_exception_when_Examples_are_provided_for_Scenario_Outline()
        {
            var feature = @"Feature: Missing
                    Scenario Outline: No Examples
                    Given I do <thing>
                    Examples:
                    | thing |
                    | test  |";

            var parser = new SpecFlowGherkinParser(CultureInfo.GetCultureInfo("en"));

            Action act = () => parser.Parse(new StringReader(feature), null);

            act.Should().NotThrow();
        }
Exemple #17
0
        public static List <SpecFlowDocument> GetParsedFeatures(IEnumerable <string> featureFiles, CultureInfo featureLanguage)
        {
            List <SpecFlowDocument> parsedSpecFlowDocument = new List <SpecFlowDocument>();

            foreach (var featureFile in featureFiles)
            {
                SpecFlowGherkinParser parser = new SpecFlowGherkinParser(featureLanguage);
                using (var reader = new StreamReader(featureFile))
                {
                    var specFlowFeature = parser.Parse(reader, featureFile);
                    parsedSpecFlowDocument.Add(specFlowFeature);
                }
            }
            return(parsedSpecFlowDocument);
        }
Exemple #18
0
 public static List<Feature> GetParsedFeatures(IEnumerable<string> featureFiles, CultureInfo featureLanguage)
 {
     List<Feature> parsedFeatures = new List<Feature>();
     foreach (var featureFile in featureFiles)
     {
         SpecFlowGherkinParser parser = new SpecFlowGherkinParser(featureLanguage);
         using (var reader = new StreamReader(featureFile))
         {
             var specFlowFeature = parser.Parse(reader, featureFile);
             var compatibleFeature = CompatibleAstConverter.ConvertToCompatibleFeature(specFlowFeature);
             parsedFeatures.Add(compatibleFeature);
         }
     }
     return parsedFeatures;
 }
Exemple #19
0
        public static List <Feature> GetParsedFeatures(IEnumerable <string> featureFiles, CultureInfo featureLanguage)
        {
            List <Feature> parsedFeatures = new List <Feature>();

            foreach (var featureFile in featureFiles)
            {
                SpecFlowGherkinParser parser = new SpecFlowGherkinParser(featureLanguage);
                using (var reader = new StreamReader(featureFile))
                {
                    var specFlowFeature   = parser.Parse(reader, featureFile);
                    var compatibleFeature = CompatibleAstConverter.ConvertToCompatibleFeature(specFlowFeature);
                    parsedFeatures.Add(compatibleFeature);
                }
            }
            return(parsedFeatures);
        }
Exemple #20
0
        private static CodeNamespace GenerateCodeNamespaceFromFeature(string feature)
        {
            CodeNamespace code;

            using (var reader = new StringReader(feature))
            {
                SpecFlowGherkinParser parser   = new SpecFlowGherkinParser(new CultureInfo("en-US"));
                SpecFlowDocument      document = parser.Parse(reader, "test.feature");

                var featureGenerator = CreateFeatureGenerator();

                code = featureGenerator.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");
            }

            return(code);
        }
Exemple #21
0
        public CodeNamespace GenerateCodeNamespaceFromFeature(string feature, bool parallelCode = false, string[] ignoreParallelTags = null)
        {
            CodeNamespace code;

            using (var reader = new StringReader(feature))
            {
                var parser   = new SpecFlowGherkinParser(new CultureInfo("en-US"));
                var document = parser.Parse(reader, "test.feature");

                var featureGenerator = CreateFeatureGenerator(parallelCode, ignoreParallelTags);

                code = featureGenerator.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");
            }

            return(code);
        }
Exemple #22
0
        public void Parser_throws_meaningful_exception_when_Examples_are_missing_in_multiple_Scenario_Outlines()
        {
            var feature = @"Feature: Missing
                            Scenario Outline: No Examples
                            Scenario Outline: Still no Examples";

            var parser = new SpecFlowGherkinParser(CultureInfo.GetCultureInfo("en"));

            Action act = () => parser.Parse(new StringReader(feature), null);

            var expectedErrors = new List <SemanticParserException> {
                new SemanticParserException("Scenario Outline 'No Examples' has no examples defined", new Location(2, 29)),
                new SemanticParserException("Scenario Outline 'Still no Examples' has no examples defined", new Location(3, 29))
            };

            act.Should().Throw <CompositeParserException>().And.Errors.Should().BeEquivalentTo(expectedErrors);
        }
Exemple #23
0
        public void Parser_throws_meaningful_exception_when_Examples_have_duplicate_header_in_Scenario_Outline()
        {
            var feature = @"Feature: Duplicate
                            Scenario Outline: Duplicate Examples table headers
                            Given I am <acting>
                            
                            Examples:
                            | acting  | acting   |
                            | driving | drinking |
                            ";

            var parser = new SpecFlowGherkinParser(CultureInfo.GetCultureInfo("en"));

            Action act = () => parser.Parse(new StringReader(feature), null);

            act.Should().Throw <SemanticParserException>().WithMessage("(2:29): Scenario Outline 'Duplicate Examples table headers' already contains an example column with header 'acting'")
            .And.Location.Line.Should().Be(2);
        }
Exemple #24
0
        private CodeNamespace GenerateTestFileCode(FeatureFileInput featureFileInput)
        {
            string targetNamespace = GetTargetNamespace(featureFileInput) ?? "SpecFlow.GeneratedTests";

            var             parser = new SpecFlowGherkinParser(generatorConfiguration.FeatureLanguage);
            SpecFlowFeature feature;

            using (var contentReader = featureFileInput.GetFeatureFileContentReader(projectSettings))
            {
                feature = parser.Parse(contentReader, featureFileInput.GetFullPath(projectSettings));
            }

            var featureGenerator = featureGeneratorRegistry.CreateGenerator(feature);

            var codeNamespace = featureGenerator.GenerateUnitTestFixture(feature, null, targetNamespace);

            return(codeNamespace);
        }
        public void Should_add_testOutputHelper_field_in_class()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFile))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var           converter = provider.CreateUnitTestConverter();
                CodeNamespace code      = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var loggerInstance = code.Class().Members.OfType <CodeMemberField>().First(m => m.Name == @"_testOutputHelper");
                loggerInstance.Type.BaseType.Should().Be("Xunit.Abstractions.ITestOutputHelper");
                loggerInstance.Attributes.Should().Be(MemberAttributes.Private | MemberAttributes.Final);
            }
        }
        public void GenerateScenarioExampleTests()
        {
            var parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));
            using (var reader = new StringReader(SampleFeatureFile))
            {
                var feature = parser.Parse(reader, null);
                Assert.IsNotNull(feature);

                var sampleTestGeneratorProvider = new SimpleTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));
                var converter = CreateUnitTestConverter(sampleTestGeneratorProvider);
                CodeNamespace code = converter.GenerateUnitTestFixture(feature, null, null);

                Assert.IsNotNull(code);

                // make sure name space is changed
                Assert.AreEqual(code.Name, SimpleTestGeneratorProvider.DefaultNameSpace);

                Assert.AreEqual(code.Types[0].Name, "SampleFeatureFileThatsGotWeirdNamesFeature");
            }
        }
        public void MsTestGeneratorShouldSetDescriptionCorrectlyWhenVariantNameFirstColumnIsTheSame()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFileSameFirstColumn))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var sampleTestGeneratorProvider = new MsTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var           converter = sampleTestGeneratorProvider.CreateUnitTestConverter();
                CodeNamespace code      = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var descriptionAttributeForFirstScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_Variant0").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForFirstScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: Variant 0");
                var descriptionAttributeForSecondScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_Variant1").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForSecondScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: Variant 1");
            }
        }
        public void GenerateScenarioExampleTests()
        {
            var parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFile))
            {
                var feature = parser.Parse(reader, null);
                feature.Should().NotBeNull();

                var           sampleTestGeneratorProvider = new SimpleTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));
                var           converter = CreateUnitTestConverter(sampleTestGeneratorProvider);
                CodeNamespace code      = converter.GenerateUnitTestFixture(feature, null, null);

                code.Should().NotBeNull();

                // make sure name space is changed
                Assert.Equal(code.Name, SimpleTestGeneratorProvider.DefaultNameSpace);

                Assert.Equal(code.Types[0].Name, "SampleFeatureFileThatsGotWeirdNamesFeature");
            }
        }
Exemple #29
0
        public void ParseFile()
        {
            var contentReader = new StringReader(FileContent);

            ParsedDocument = null;
            ParsingErrors  = new ParserException[0];

            try
            {
                ParsedDocument = _parser.Parse(contentReader, "sample.feature");
                ParsedDocument.Should().NotBeNull();
            }
            catch (ParserException ex)
            {
                ParsingErrors = ex.GetParserExceptions();
                Console.WriteLine("-> parsing errors");
                foreach (var error in ParsingErrors)
                {
                    Console.WriteLine("-> {0}:{1} {2}", error.Location?.Line ?? 0, error.Location?.Column ?? 0, error.Message);
                }
            }
        }
Exemple #30
0
        public void ParseFile()
        {
            var contentReader = new StringReader(FileContent);

            ParsedDocument = null;
            ParsingErrors  = new ParserException[0];

            try
            {
                ParsedDocument = parser.Parse(contentReader, "sample.feature");
                Assert.IsNotNull(ParsedDocument);
            }
            catch (ParserException ex)
            {
                ParsingErrors = ex.GetParserExceptions();
                Console.WriteLine("-> parsing errors");
                foreach (var error in ParsingErrors)
                {
                    Console.WriteLine("-> {0}:{1} {2}", error.Location == null ? 0 : error.Location.Line, error.Location == null ? 0 : error.Location.Column, error.Message);
                }
            }
        }
Exemple #31
0
        private CodeNamespace GenerateTestFileCode(FeatureFileInput featureFileInput)
        {
            string targetNamespace = GetTargetNamespace(featureFileInput) ?? "SpecFlow.GeneratedTests";

            var parser = new SpecFlowGherkinParser(generatorConfiguration.FeatureLanguage);
            SpecFlowFeature feature;
            using (var contentReader = featureFileInput.GetFeatureFileContentReader(projectSettings))
            {
                feature = parser.Parse(contentReader, featureFileInput.GetFullPath(projectSettings));
            }

            var featureGenerator = featureGeneratorRegistry.CreateGenerator(feature);

            var codeNamespace = featureGenerator.GenerateUnitTestFixture(feature, null, targetNamespace);
            return codeNamespace;
        }
        private static CodeNamespace GenerateCodeNamespaceFromFeature(string feature)
        {
            CodeNamespace code;
            using (var reader = new StringReader(feature))
            {
                SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));
                SpecFlowDocument document = parser.Parse(reader, "test.feature");

                var featureGenerator = CreateFeatureGenerator();

                code = featureGenerator.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");
            }

            return code;
        }
        public void MsTestGeneratorShouldInvokeFeatureSetupMethodWithGlobalNamespaceAlias()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));
            using (var reader = new StringReader(SampleFeatureFileWithMultipleExampleSets))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var sampleTestGeneratorProvider = new MsTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var converter = sampleTestGeneratorProvider.CreateUnitTestConverter();
                CodeNamespace code = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var featureSetupCall = code
                    .Class()
                    .Members()
                    .Single(m => m.Name == "TestInitialize")
                    .Statements
                    .OfType<CodeConditionStatement>()
                    .First()
                    .TrueStatements
                    .OfType<CodeExpressionStatement>()
                    .First()
                    .Expression
                    .As<CodeMethodInvokeExpression>();

                featureSetupCall.Should().NotBeNull();
                featureSetupCall.Method.MethodName.Should().Be("FeatureSetup");
                featureSetupCall.Method.TargetObject.As<CodeTypeReferenceExpression>().Type.Options.Should().Be(CodeTypeReferenceOptions.GlobalReference);
            }
        }
        public void MsTestGeneratorShouldSetDescriptionCorrectlyWhenExampleSetIdentifierIsUsed()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));
            using (var reader = new StringReader(SampleFeatureFileWithMultipleExampleSets))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var sampleTestGeneratorProvider = new MsTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var converter = sampleTestGeneratorProvider.CreateUnitTestConverter();
                CodeNamespace code = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var descriptionAttributeForFirstScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_ExampleSet0_Something").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForFirstScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: something");
                var descriptionAttributeForSecondScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_ExampleSet0_SomethingElse").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForSecondScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: something else");
                var descriptionAttributeForThirdScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_ExampleSet1_Another").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForThirdScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: another");
                var descriptionAttributeForFourthScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_ExampleSet1_AndAnother").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForFourthScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: and another");
            }
        }
        public void MsTestGeneratorShouldSetDescriptionCorrectlyWhenVariantNameFirstColumnIsTheSame()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));
            using (var reader = new StringReader(SampleFeatureFileSameFirstColumn))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var sampleTestGeneratorProvider = new MsTestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var converter = sampleTestGeneratorProvider.CreateUnitTestConverter();
                CodeNamespace code = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var descriptionAttributeForFirstScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_Variant0").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForFirstScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: Variant 0");
                var descriptionAttributeForSecondScenarioOutline = code.Class().Members().Single(m => m.Name == "SimpleScenarioOutline_Variant1").CustomAttributes().Single(a => a.Name == TestDescriptionAttributeName);
                descriptionAttributeForSecondScenarioOutline.ArgumentValues().First().Should().Be("Simple Scenario Outline: Variant 1");
            }
        }
Exemple #36
0
 protected virtual SpecFlowDocument ParseContent(SpecFlowGherkinParser parser, TextReader contentReader, string sourceFilePath)
 {
     return(parser.Parse(contentReader, sourceFilePath));
 }
Exemple #37
0
 protected virtual SpecFlowFeature ParseContent(SpecFlowGherkinParser parser, TextReader contentReader, string sourceFilePath)
 {
     return parser.Parse(contentReader, sourceFilePath);
 }