Esempio n. 1
1
        public Feature Parse(TextReader featureFileReader)
        {
            var language = this.DetermineLanguage();
            var gherkinParser = new Gherkin.Parser();

            Gherkin.Ast.Feature feature = gherkinParser.Parse(
                new Gherkin.TokenScanner(featureFileReader),
                new Gherkin.TokenMatcher(language));

            Feature result = new Mapper(feature.Language).MapToFeature(feature);

            return result;
        }
Esempio n. 2
0
        public Feature Parse(TextReader featureFileReader)
        {
            var language      = this.DetermineLanguage();
            var gherkinParser = new Gherkin.Parser();

            try
            {
                Gherkin.Ast.GherkinDocument gherkinDocument = gherkinParser.Parse(
                    new Gherkin.TokenScanner(featureFileReader),
                    new Gherkin.TokenMatcher(new CultureAwareDialectProvider(language)));

                Feature result = new Mapper(this.configuration, gherkinDocument.Feature.Language).MapToFeature(gherkinDocument);
                result = this.RemoveFeatureWithExcludeTags(result);

                if (result != null)
                {
                    this.descriptionProcessor.Process(result);
                }

                return(result);
            }
            catch (Gherkin.CompositeParserException exception)
            {
                throw new FeatureParseException("Unable to parse feature", exception);
            }
        }
Esempio n. 3
0
        public Feature Parse(TextReader featureFileReader)
        {
            var gherkinParser = new Gherkin.Parser();
            Gherkin.Ast.Feature feature = gherkinParser.Parse(featureFileReader);
            Feature result = new Mapper(feature.Language).MapToFeature(feature);

            return result;
        }
Esempio n. 4
0
        public Feature Parse(TextReader featureFileReader)
        {
            var gherkinParser = new Gherkin.Parser();

            Gherkin.Ast.Feature feature = gherkinParser.Parse(featureFileReader);
            Feature             result  = new Mapper(feature.Language).MapToFeature(feature);

            return(result);
        }
Esempio n. 5
0
        public Feature Parse(TextReader featureFileReader)
        {
            var language = this.DetermineLanguage();
            var gherkinParser = new Gherkin.Parser();

            Gherkin.Ast.GherkinDocument gherkinDocument = gherkinParser.Parse(
                new Gherkin.TokenScanner(featureFileReader),
                new Gherkin.TokenMatcher(language));

            Feature result = new Mapper(this.configuration, gherkinDocument.Feature.Language).MapToFeature(gherkinDocument);

            return result;
        }
Esempio n. 6
0
        public Feature Parse(TextReader featureFileReader)
        {
            var language      = this.DetermineLanguage();
            var gherkinParser = new Gherkin.Parser();

            Gherkin.Ast.GherkinDocument gherkinDocument = gherkinParser.Parse(
                new Gherkin.TokenScanner(featureFileReader),
                new Gherkin.TokenMatcher(language));

            Feature result = new Mapper(this.configuration, gherkinDocument.Feature.Language).MapToFeature(gherkinDocument);

            return(result);
        }
        private static Gherkin.Ast.GherkinDocument CreateGherkinDocument(string scenario, params string[] steps)
        {
            var gherkinText =
                @"Feature: Some Sample Feature
    In order to learn Math
    As a regular human
    I want to add two numbers using Calculator

Scenario: " + scenario + @"
" + string.Join(Environment.NewLine, steps)
            ;

            using (var gherkinStream = new MemoryStream(Encoding.UTF8.GetBytes(gherkinText)))
                using (var gherkinReader = new StreamReader(gherkinStream))
                {
                    var parser = new Gherkin.Parser();
                    return(parser.Parse(gherkinReader));
                }
        }
Esempio n. 8
0
        public static File ParseCore(string filePath, CharacterPositionFinder finder, Encoding encoding)
        {
            var text          = SystemFile.ReadAllText(filePath, encoding);
            var lastCharacter = text.Length - 1;

            var file = new File
            {
                Name         = filePath,
                FooterSpan   = CharacterSpan.None,              // there is no footer
                LocationSpan = lastCharacter >= 0
                                               ? new LocationSpan(finder.GetLineInfo(0), finder.GetLineInfo(lastCharacter))
                                               : new LocationSpan(LineInfo.None, LineInfo.None),
            };

            try
            {
                var parser   = new GherkinParser();
                var document = parser.Parse(filePath);

                var root = new Container
                {
                    Type         = nameof(GherkinDocument),
                    Name         = string.Empty,
                    LocationSpan = file.LocationSpan,
                    HeaderSpan   = new CharacterSpan(0, 0),                                      // there is no header
                    FooterSpan   = new CharacterSpan(Math.Max(0, lastCharacter), lastCharacter), // there is no footer
                };

                file.Children.Add(root);

                var feature = document.Feature;
                if (feature != null)
                {
                    // get locations ordered so that we know the position of the feature
                    var locations = document.Comments.Select(_ => _.Location).ToList();
                    locations.Add(feature.Location);

                    var sortedLocations = locations.OrderBy(_ => _.Line).ThenBy(_ => _.Column).ToList();

                    var positionAfterFeature = sortedLocations.IndexOf(feature.Location) + 1;

                    var location = positionAfterFeature < sortedLocations.Count - 1
                                    ? GetLineInfo(locations[positionAfterFeature + 1])
                                    : file.LocationSpan.End;

                    var parsedChild = ParseFeature(feature, finder, location);
                    root.Children.Add(parsedChild);
                }
            }
            catch (Exception ex)
            {
                // try to adjust location span to include full file content
                // but ignore empty files as parsing errors
                var lines = SystemFile.ReadLines(filePath).Count();
                if (lines == 0)
                {
                    file.LocationSpan = new LocationSpan(LineInfo.None, LineInfo.None);
                }
                else
                {
                    file.ParsingErrors.Add(new ParsingError
                    {
                        ErrorMessage = ex.Message,
                        Location     = LineInfo.None,
                    });

                    file.LocationSpan = new LocationSpan(new LineInfo(1, 0), new LineInfo(lines + 1, 0));
                }
            }

            return(file);
        }
        protected static TestFeature DiscoverPesterFeatures(string source, IMessageLogger logger)
        {
            if (!File.Exists(source))
            {
                return(null);
            }

            Gherkin.Parser parser = new Gherkin.Parser();

            GherkinDocument document = parser.Parse(source);

            TestFeature feature = new TestFeature
            {
                Name     = Path.GetFileNameWithoutExtension(source),
                Path     = source,
                Document = document
            };

            foreach (ScenarioDefinition scenario in document.Feature.Children)
            {
                if (scenario is ScenarioOutline outline)
                {
                    foreach (Examples example in outline.Examples)
                    {
                        TestScenario testScenario = new TestScenario()
                        {
                            Name     = $"{scenario.Name}\n  Examples:{example.Name}",
                            Path     = source,
                            Scenario = scenario
                        };
                        feature.Scenarios.Add(testScenario);
                        foreach (TableRow row in example.TableBody)
                        {
                            foreach (Step step in scenario.Steps)
                            {
                                TestStep testStep = new TestStep()
                                {
                                    Name = step.Text,
                                    Path = source,
                                    Step = step
                                };
                                testScenario.Steps.Add(testStep);
                            }
                        }
                    }
                }
                else
                {
                    TestScenario testScenario = new TestScenario()
                    {
                        Name     = scenario.Name,
                        Path     = source,
                        Scenario = scenario
                    };

                    feature.Scenarios.Add(testScenario);

                    foreach (Step step in scenario.Steps)
                    {
                        TestStep testStep = new TestStep()
                        {
                            Name = step.Text,
                            Path = source,
                            Step = step
                        };
                        testScenario.Steps.Add(testStep);
                    }
                }
            }
            if (feature.Scenarios.Count == 0)
            {
                return(null);
            }
            return(feature);
        }