How to use Source class of Gherkin.CucumberMessages.Types package

Best Gherkin-dotnet code snippet using Gherkin.CucumberMessages.Types.Source

AstMessagesConverter.cs

Source:AstMessagesConverter.cs Github

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using Gherkin.Ast;5using Gherkin.CucumberMessages.Types;6using Background = Gherkin.CucumberMessages.Types.Background;7using Comment = Gherkin.CucumberMessages.Types.Comment;8using Examples = Gherkin.CucumberMessages.Types.Examples;9using Feature = Gherkin.CucumberMessages.Types.Feature;10using Location = Gherkin.CucumberMessages.Types.Location;11using Rule = Gherkin.CucumberMessages.Types.Rule;12using Step = Gherkin.CucumberMessages.Types.Step;13using DataTable = Gherkin.CucumberMessages.Types.DataTable;14using DocString = Gherkin.CucumberMessages.Types.DocString;15using GherkinDocument = Gherkin.CucumberMessages.Types.GherkinDocument;16using Scenario = Gherkin.CucumberMessages.Types.Scenario;17using TableCell = Gherkin.CucumberMessages.Types.TableCell;18using TableRow = Gherkin.CucumberMessages.Types.TableRow;19using Tag = Gherkin.CucumberMessages.Types.Tag;20namespace Gherkin.CucumberMessages21{22    public class AstMessagesConverter23    {24        private readonly IIdGenerator _idGenerator;25        public AstMessagesConverter(IIdGenerator idGenerator)26        {27            _idGenerator = idGenerator;28        }29        public GherkinDocument ConvertGherkinDocumentToEventArgs(Ast.GherkinDocument gherkinDocument, string sourceEventUri)30        {31            return new GherkinDocument()32            {33                Uri = sourceEventUri,34                Feature = ConvertFeature(gherkinDocument),35                Comments = ConvertComments(gherkinDocument)36            };37        }38        private IReadOnlyCollection<Comment> ConvertComments(Ast.GherkinDocument gherkinDocument)39        {40            return gherkinDocument.Comments.Select(c =>41                new Comment()42                {43                    Text = c.Text,44                    Location = ConvertLocation(c.Location)45                }).ToReadOnlyCollection();46        }47        private Feature ConvertFeature(Ast.GherkinDocument gherkinDocument)48        {49            var feature = gherkinDocument.Feature;50            if (feature == null)51            {52                return null;53            }54            var children = feature.Children.Select(ConvertToFeatureChild).ToReadOnlyCollection();55            var tags = feature.Tags.Select(ConvertTag).ToReadOnlyCollection();56            return new Feature()57            {58                Name = CucumberMessagesDefaults.UseDefault(feature.Name, CucumberMessagesDefaults.DefaultName),59                Description = CucumberMessagesDefaults.UseDefault(feature.Description, CucumberMessagesDefaults.DefaultDescription),60                Keyword = feature.Keyword,61                Language = feature.Language,62                Location = ConvertLocation(feature.Location),63                Children = children,64                Tags = tags65            };66        }67        private static Location ConvertLocation(Ast.Location location)68        {69            return new Location(location.Column, location.Line);70        }71        private FeatureChild ConvertToFeatureChild(IHasLocation hasLocation)72        {73            var tuple = ConvertToChild(hasLocation);74            return new FeatureChild(tuple.Item3, tuple.Item1, tuple.Item2);75        }76        77        private RuleChild ConvertToRuleChild(IHasLocation hasLocation)78        {79            var tuple = ConvertToChild(hasLocation);80            return new RuleChild(tuple.Item1, tuple.Item3);81        }82        83        private Tuple<Background, Rule, Scenario> ConvertToChild(IHasLocation hasLocation)84        {85            switch (hasLocation)86            {87                case Gherkin.Ast.Background background:88                    var backgroundSteps = background.Steps.Select(ConvertStep).ToList();89                    return new Tuple<Background, Rule, Scenario>(new Background90                        {91                            Id = _idGenerator.GetNewId(),92                            Location = ConvertLocation(background.Location),93                            Name = CucumberMessagesDefaults.UseDefault(background.Name, CucumberMessagesDefaults.DefaultName),94                            Description = CucumberMessagesDefaults.UseDefault(background.Description, CucumberMessagesDefaults.DefaultDescription),95                            Keyword = background.Keyword,96                            Steps = backgroundSteps97                        }, null, null);98                case Ast.Scenario scenario:99                    var steps = scenario.Steps.Select(ConvertStep).ToList();100                    var examples = scenario.Examples.Select(ConvertExamples).ToReadOnlyCollection();101                    var tags = scenario.Tags.Select(ConvertTag).ToReadOnlyCollection();102                    return new Tuple<Background, Rule, Scenario>(null, null, new Scenario()103                        {104                            Id = _idGenerator.GetNewId(),105                            Keyword = scenario.Keyword,106                            Location = ConvertLocation(scenario.Location),107                            Name = CucumberMessagesDefaults.UseDefault(scenario.Name, CucumberMessagesDefaults.DefaultName),108                            Description = CucumberMessagesDefaults.UseDefault(scenario.Description, CucumberMessagesDefaults.DefaultDescription),109                            Steps = steps,110                            Examples = examples,111                            Tags = tags112                        });113                case Ast.Rule rule:114                    {115                        var ruleChildren = rule.Children.Select(ConvertToRuleChild).ToReadOnlyCollection();116                        var ruleTags = rule.Tags.Select(ConvertTag).ToReadOnlyCollection();117                        return new Tuple<Background, Rule, Scenario>(null, new Rule118                            {119                                Id = _idGenerator.GetNewId(),120                                Name = CucumberMessagesDefaults.UseDefault(rule.Name, CucumberMessagesDefaults.DefaultName),121                                Description = CucumberMessagesDefaults.UseDefault(rule.Description, CucumberMessagesDefaults.DefaultDescription),122                                Keyword = rule.Keyword,123                                Children = ruleChildren,124                                Location = ConvertLocation(rule.Location),125                                Tags = ruleTags126                            }, null);127                    }128                default:129                    throw new NotImplementedException();130            }131        }132        private Examples ConvertExamples(Ast.Examples examples)133        {134            var header = ConvertTableHeader(examples);135            var body = ConvertToTableBody(examples);136            var tags = examples.Tags.Select(ConvertTag).ToReadOnlyCollection();137            return new Examples()138            {139                Id = _idGenerator.GetNewId(),140                Name = CucumberMessagesDefaults.UseDefault(examples.Name, CucumberMessagesDefaults.DefaultName),141                Keyword = examples.Keyword,142                Description = CucumberMessagesDefaults.UseDefault(examples.Description, CucumberMessagesDefaults.DefaultDescription),143                Location = ConvertLocation(examples.Location),144                TableHeader = header,145                TableBody = body,146                Tags = tags147            };148        }149        private IReadOnlyCollection<TableRow> ConvertToTableBody(Ast.Examples examples)150        {151            if (examples.TableBody == null)152                return new List<TableRow>();153            return ConvertToTableRow(examples.TableBody);154        }155        private IReadOnlyCollection<TableRow> ConvertToTableRow(IEnumerable<Gherkin.Ast.TableRow> rows)156        {157            return rows.Select(b =>158                new TableRow159                {160                    Id = _idGenerator.GetNewId(),161                    Location = ConvertLocation(b.Location),162                    Cells = b.Cells.Select(ConvertCell).ToReadOnlyCollection()163                }).ToReadOnlyCollection();164        }165        private TableRow ConvertTableHeader(Ast.Examples examples)166        {167            if (examples.TableHeader == null)168                return null;169            return new TableRow170            {171                Id = _idGenerator.GetNewId(),172                Location = ConvertLocation(examples.TableHeader.Location),173                Cells = examples.TableHeader.Cells.Select(ConvertCell).ToReadOnlyCollection()174            };175        }176        private Tag ConvertTag(Ast.Tag tag)177        {178            return new Tag179            {180                Id = _idGenerator.GetNewId(),181                Location = ConvertLocation(tag.Location),182                Name = tag.Name183            };184        }185        private TableCell ConvertCell(Ast.TableCell c)186        {187            return new TableCell()188            {189                Value = CucumberMessagesDefaults.UseDefault(c.Value, CucumberMessagesDefaults.DefaultCellValue),190                Location = ConvertLocation(c.Location)191            };192        }193        private Step ConvertStep(Ast.Step step)194        {195            DataTable dataTable = null;196            if (step.Argument is Gherkin.Ast.DataTable astDataTable) 197            {198                var rows = ConvertToTableRow(astDataTable.Rows);199                dataTable = new DataTable200                {201                    Rows = rows,202                    Location = ConvertLocation(astDataTable.Location)203                };204            }205            DocString docString = null;206           if (step.Argument is Gherkin.Ast.DocString astDocString) 207            {208                docString = new DocString209                {210                    Content = astDocString.Content,211                    MediaType = astDocString.ContentType,212                    Delimiter = astDocString.Delimiter ?? "\"\"\"", //TODO: store DocString delimiter in Gherkin AST213                    Location = ConvertLocation(astDocString.Location)214                };215            }216            return new Step()217            {218                Id = _idGenerator.GetNewId(),219                Keyword = step.Keyword,220                Text = step.Text,221                DataTable = dataTable,222                DocString = docString,223                Location = ConvertLocation(step.Location)224            };225        }226    }227}...

Full Screen

Full Screen

EventTestBase.cs

Source:EventTestBase.cs Github

copy

Full Screen

...50                FullPath = fullPathToTestFeatureFile,51                ExpectedFileFullPath = expectedAstFile52            };53        }54        protected List<Envelope> ProcessGherkinEvents(string fullPathToTestFeatureFile, bool printSource, bool printAst, bool printPickles)55        {56            var raisedEvents = new List<Envelope>();57            var sourceProvider = new SourceProvider();58            var sources = sourceProvider.GetSources(new List<string> {fullPathToTestFeatureFile});59            var gherkinEventsProvider = new GherkinEventsProvider(printSource, printAst, printPickles, idGenerator);60            foreach (var source in sources)61            {62                foreach (var evt in gherkinEventsProvider.GetEvents(source))63                {64                    raisedEvents.Add(evt);65                }66            }67            return raisedEvents;68        }69        protected string GetExpectedContent(string expectedAstFile)70        {71            return File.ReadAllText(expectedAstFile, Encoding.UTF8);72        }73    }...

Full Screen

Full Screen

GherkinEventsProvider.cs

Source:GherkinEventsProvider.cs Github

copy

Full Screen

...12        private readonly PickleCompiler _pickleCompiler;13        private readonly AstMessagesConverter _astMessagesConverter;14        readonly bool _printAst;15        readonly bool _printPickles;16        readonly bool _printSource;17        public GherkinEventsProvider(bool printSource, bool printAst, bool printPickles, IIdGenerator idGenerator)18        {19            _printSource = printSource;20            _astMessagesConverter = new AstMessagesConverter(idGenerator);21            _pickleCompiler = new PickleCompiler(idGenerator);22            _printAst = printAst;23            _printPickles = printPickles;24        }25        public IEnumerable<Envelope> GetEvents(Source source)26        {27            var events = new List<Envelope>();28            try29            {30                var gherkinDocument = _parser.Parse(new StringReader(source.Data));31                if (_printSource)32                {33                    events.Add(new Envelope34                    {35                        Source = source36                    });37                }38                if (_printAst)39                {40                    events.Add(new Envelope41                    {42                        GherkinDocument =43                            _astMessagesConverter.ConvertGherkinDocumentToEventArgs(gherkinDocument, source.Uri)44                    });45                }46                if (_printPickles)47                {48                    var pickles = _pickleCompiler.Compile(_astMessagesConverter.ConvertGherkinDocumentToEventArgs(gherkinDocument, source.Uri));49                    foreach (Pickle pickle in pickles)50                    {51                        events.Add(new Envelope52                        {53                            Pickle = pickle54                        });55                    }56                }57            }58            catch (CompositeParserException e)59            {60                foreach (ParserException error in e.Errors)61                {62                    AddParseError(events, error, source.Uri);63                }64            }65            catch (ParserException e)66            {67                AddParseError(events, e, source.Uri);68            }69            return events;70        }71        private void AddParseError(List<Envelope> events, ParserException e, String uri)72        {73            events.Add(new Envelope74            {75                ParseError = new ParseError()76                {77                    Message = e.Message,78                    Source = new SourceReference()79                    {80                        Location = new Location(e.Location.Column, e.Location.Line),81                        Uri = uri82                    }83                }84            });85        }86    }87}...

Full Screen

Full Screen

SourceTests.cs

Source:SourceTests.cs Github

copy

Full Screen

...4using Gherkin.Specs.Helper;5using Xunit;6namespace Gherkin.Specs7{8    public class SourceTests : EventTestBase9    {10        [Theory, MemberData(nameof(TestFileProvider.GetValidTestFiles), MemberType = typeof(TestFileProvider))]11        public void TestSourceMessage(string testFeatureFile)12        {13            var testFile = GetFullPathToTestFeatureFile(testFeatureFile, "good", ".source.ndjson");14            var expectedAstContent = GetExpectedContent(testFile.ExpectedFileFullPath);15            var expectedGherkinDocumentEvent = NDJsonParser.Deserialize<Envelope>(expectedAstContent);16            var raisedEvents = ProcessGherkinEvents(testFile.FullPath, true, false, false);17            raisedEvents.Should().Match(list => list.All(e => e.Source != null));18            AssertEvents(testFeatureFile, raisedEvents, expectedGherkinDocumentEvent, testFile);19        }20    }21}...

Full Screen

Full Screen

SourceProvider.cs

Source:SourceProvider.cs Github

copy

Full Screen

...4using Gherkin.CucumberMessages.Types;56namespace Gherkin.Specs.EventStubs7{8    public class SourceProvider9    {10        private const string GherkinMediaType = "text/x.cucumber.gherkin+plain";1112        public IEnumerable<Source> GetSources(IEnumerable<string> paths)13        {14            foreach (var path in paths)15            {16                string data = File.ReadAllText(path);17                yield return new Source18                {19                    Data = data,20                    Uri = path,21                    MediaType = GherkinMediaType22                };23            }24        }2526    }27}
...

Full Screen

Full Screen

Envelope.cs

Source:Envelope.cs Github

copy

Full Screen

...8        [DataMember(Name = "gherkinDocument")]9        public GherkinDocument GherkinDocument { get; set; }1011        [DataMember(Name = "source")]12        public Source Source { get; set; }1314        [DataMember(Name = "pickle")]15        public Pickle Pickle { get; set; }1617        [DataMember(Name = "parseError")]18        public ParseError ParseError { get; set; }19    }20}
...

Full Screen

Full Screen

SourceReference.cs

Source:SourceReference.cs Github

copy

Full Screen

1using System.Runtime.Serialization;2namespace Gherkin.CucumberMessages.Types3{4    public class SourceReference5    {6        [DataMember(Name = "location")]7        public Location Location { get; set; }8        [DataMember(Name = "uri")]9        public string Uri { get; set; }10        //TODO: javaMethod, javaStackTraceElement11    }12}...

Full Screen

Full Screen

ParseError.cs

Source:ParseError.cs Github

copy

Full Screen

...3{4    public class ParseError5    {6        [DataMember(Name = "source")]7        public SourceReference Source { get; set; }8        [DataMember(Name = "message")]9        public string Message { get; set; }10    }11}...

Full Screen

Full Screen

Source

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.IO;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7using Gherkin.Ast;

Full Screen

Full Screen

Source

Using AI Code Generation

copy

Full Screen

1using Gherkin.CucumberMessages.Types;2{3    static void Main(string[] args)4    {5        {6        };7    }8}9using Gherkin.SpecFlow.Parser;10{11    static void Main(string[] args)12    {13        {14        };15    }16}

Full Screen

Full Screen

Source

Using AI Code Generation

copy

Full Screen

1var source = new Source();2source.Content = "Feature: Hello World";3source.Uri = "1.cs";4var source = new Source();5source.Content = "Feature: Hello World";6source.Uri = "2.cs";7var source = new Source();8source.Content = "Feature: Hello World";9source.Uri = "3.cs";10var source = new Source();11source.Content = "Feature: Hello World";12source.Uri = "4.cs";13var source = new Source();14source.Content = "Feature: Hello World";15source.Uri = "5.cs";16var source = new Source();17source.Content = "Feature: Hello World";18source.Uri = "6.cs";19var source = new Source();20source.Content = "Feature: Hello World";21source.Uri = "7.cs";22var source = new Source();23source.Content = "Feature: Hello World";24source.Uri = "8.cs";25var source = new Source();26source.Content = "Feature: Hello World";27source.Uri = "9.cs";28var source = new Source();29source.Content = "Feature: Hello World";30source.Uri = "10.cs";31var source = new Source();32source.Content = "Feature: Hello World";33source.Uri = "11.cs";

Full Screen

Full Screen

Source

Using AI Code Generation

copy

Full Screen

1using Gherkin.CucumberMessages.Types;2Source source = new Source();3source.Uri = "test.feature";4source.Data = "Feature: Test";5using Gherkin.Events;6Source source = new Source();7source.Uri = "test.feature";8source.Data = "Feature: Test";

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gherkin-dotnet automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful