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

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

Run Gherkin-dotnet automation tests on LambdaTest cloud grid

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

AstMessagesConverter.cs

Source: AstMessagesConverter.cs Github

copy
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;
20
21namespace Gherkin.CucumberMessages
22{
23    public class AstMessagesConverter
24    {
25        private readonly IIdGenerator _idGenerator;
26
27        public AstMessagesConverter(IIdGenerator idGenerator)
28        {
29            _idGenerator = idGenerator;
30        }
31
32        public GherkinDocument ConvertGherkinDocumentToEventArgs(Ast.GherkinDocument gherkinDocument, string sourceEventUri)
33        {
34            return new GherkinDocument()
35            {
36                Uri = sourceEventUri,
37                Feature = ConvertFeature(gherkinDocument),
38                Comments = ConvertComments(gherkinDocument)
39            };
40        }
41
42        private IReadOnlyCollection<Comment> ConvertComments(Ast.GherkinDocument gherkinDocument)
43        {
44            return gherkinDocument.Comments.Select(c =>
45                new Comment()
46                {
47                    Text = c.Text,
48                    Location = ConvertLocation(c.Location)
49                }).ToReadOnlyCollection();
50        }
51
52        private Feature ConvertFeature(Ast.GherkinDocument gherkinDocument)
53        {
54            var feature = gherkinDocument.Feature;
55            if (feature == null)
56            {
57                return null;
58            }
59
60            var children = feature.Children.Select(ConvertToFeatureChild).ToReadOnlyCollection();
61            var tags = feature.Tags.Select(ConvertTag).ToReadOnlyCollection();
62
63            return new Feature()
64            {
65                Name = CucumberMessagesDefaults.UseDefault(feature.Name, CucumberMessagesDefaults.DefaultName),
66                Description = CucumberMessagesDefaults.UseDefault(feature.Description, CucumberMessagesDefaults.DefaultDescription),
67                Keyword = feature.Keyword,
68                Language = feature.Language,
69                Location = ConvertLocation(feature.Location),
70                Children = children,
71                Tags = tags
72            };
73        }
74
75        private static Location ConvertLocation(Ast.Location location)
76        {
77            return new Location(location.Column, location.Line);
78        }
79
80        private FeatureChild ConvertToFeatureChild(IHasLocation hasLocation)
81        {
82            var tuple = ConvertToChild(hasLocation);
83            return new FeatureChild(tuple.Item3, tuple.Item1, tuple.Item2);
84        }
85        
86        private RuleChild ConvertToRuleChild(IHasLocation hasLocation)
87        {
88            var tuple = ConvertToChild(hasLocation);
89            return new RuleChild(tuple.Item1, tuple.Item3);
90        }
91        
92        private Tuple<Background, Rule, Scenario> ConvertToChild(IHasLocation hasLocation)
93        {
94            switch (hasLocation)
95            {
96                case Gherkin.Ast.Background background:
97                    var backgroundSteps = background.Steps.Select(ConvertStep).ToList();
98                    return new Tuple<Background, Rule, Scenario>(new Background
99                        {
100                            Id = _idGenerator.GetNewId(),
101                            Location = ConvertLocation(background.Location),
102                            Name = CucumberMessagesDefaults.UseDefault(background.Name, CucumberMessagesDefaults.DefaultName),
103                            Description = CucumberMessagesDefaults.UseDefault(background.Description, CucumberMessagesDefaults.DefaultDescription),
104                            Keyword = background.Keyword,
105                            Steps = backgroundSteps
106                        }, null, null);
107                case Ast.Scenario scenario:
108                    var steps = scenario.Steps.Select(ConvertStep).ToList();
109                    var examples = scenario.Examples.Select(ConvertExamples).ToReadOnlyCollection();
110                    var tags = scenario.Tags.Select(ConvertTag).ToReadOnlyCollection();
111                    return new Tuple<Background, Rule, Scenario>(null, null, new Scenario()
112                        {
113                            Id = _idGenerator.GetNewId(),
114                            Keyword = scenario.Keyword,
115                            Location = ConvertLocation(scenario.Location),
116                            Name = CucumberMessagesDefaults.UseDefault(scenario.Name, CucumberMessagesDefaults.DefaultName),
117                            Description = CucumberMessagesDefaults.UseDefault(scenario.Description, CucumberMessagesDefaults.DefaultDescription),
118                            Steps = steps,
119                            Examples = examples,
120                            Tags = tags
121                        });
122                case Ast.Rule rule:
123                    {
124                        var ruleChildren = rule.Children.Select(ConvertToRuleChild).ToReadOnlyCollection();
125                        var ruleTags = rule.Tags.Select(ConvertTag).ToReadOnlyCollection();
126                        return new Tuple<Background, Rule, Scenario>(null, new Rule
127                            {
128                                Id = _idGenerator.GetNewId(),
129                                Name = CucumberMessagesDefaults.UseDefault(rule.Name, CucumberMessagesDefaults.DefaultName),
130                                Description = CucumberMessagesDefaults.UseDefault(rule.Description, CucumberMessagesDefaults.DefaultDescription),
131                                Keyword = rule.Keyword,
132                                Children = ruleChildren,
133                                Location = ConvertLocation(rule.Location),
134                                Tags = ruleTags
135                            }, null);
136                    }
137
138
139
140                default:
141                    throw new NotImplementedException();
142            }
143
144        }
145
146        private Examples ConvertExamples(Ast.Examples examples)
147        {
148            var header = ConvertTableHeader(examples);
149            var body = ConvertToTableBody(examples);
150            var tags = examples.Tags.Select(ConvertTag).ToReadOnlyCollection();
151            return new Examples()
152            {
153                Id = _idGenerator.GetNewId(),
154                Name = CucumberMessagesDefaults.UseDefault(examples.Name, CucumberMessagesDefaults.DefaultName),
155                Keyword = examples.Keyword,
156                Description = CucumberMessagesDefaults.UseDefault(examples.Description, CucumberMessagesDefaults.DefaultDescription),
157                Location = ConvertLocation(examples.Location),
158                TableHeader = header,
159                TableBody = body,
160                Tags = tags
161            };
162        }
163
164        private IReadOnlyCollection<TableRow> ConvertToTableBody(Ast.Examples examples)
165        {
166            if (examples.TableBody == null)
167                return new List<TableRow>();
168
169            return ConvertToTableRow(examples.TableBody);
170        }
171
172        private IReadOnlyCollection<TableRow> ConvertToTableRow(IEnumerable<Gherkin.Ast.TableRow> rows)
173        {
174            return rows.Select(b =>
175                new TableRow
176                {
177                    Id = _idGenerator.GetNewId(),
178                    Location = ConvertLocation(b.Location),
179                    Cells = b.Cells.Select(ConvertCell).ToReadOnlyCollection()
180                }).ToReadOnlyCollection();
181        }
182
183        private TableRow ConvertTableHeader(Ast.Examples examples)
184        {
185            if (examples.TableHeader == null)
186                return null;
187
188            return new TableRow
189            {
190                Id = _idGenerator.GetNewId(),
191                Location = ConvertLocation(examples.TableHeader.Location),
192                Cells = examples.TableHeader.Cells.Select(ConvertCell).ToReadOnlyCollection()
193            };
194        }
195
196        private Tag ConvertTag(Ast.Tag tag)
197        {
198            return new Tag
199            {
200                Id = _idGenerator.GetNewId(),
201                Location = ConvertLocation(tag.Location),
202                Name = tag.Name
203            };
204        }
205
206        private TableCell ConvertCell(Ast.TableCell c)
207        {
208            return new TableCell()
209            {
210                Value = CucumberMessagesDefaults.UseDefault(c.Value, CucumberMessagesDefaults.DefaultCellValue),
211                Location = ConvertLocation(c.Location)
212            };
213        }
214
215        private Step ConvertStep(Ast.Step step)
216        {
217            DataTable dataTable = null;
218            if (step.Argument is Gherkin.Ast.DataTable astDataTable) 
219            {
220                var rows = ConvertToTableRow(astDataTable.Rows);
221                dataTable = new DataTable
222                {
223                    Rows = rows,
224                    Location = ConvertLocation(astDataTable.Location)
225                };
226            }
227
228            DocString docString = null;
229           if (step.Argument is Gherkin.Ast.DocString astDocString) 
230            {
231                docString = new DocString
232                {
233                    Content = astDocString.Content,
234                    MediaType = astDocString.ContentType,
235                    Delimiter = astDocString.Delimiter ?? "\"\"\"", //TODO: store DocString delimiter in Gherkin AST
236                    Location = ConvertLocation(astDocString.Location)
237                };
238            }
239
240            return new Step()
241            {
242                Id = _idGenerator.GetNewId(),
243                Keyword = step.Keyword,
244                Text = step.Text,
245                DataTable = dataTable,
246                DocString = docString,
247                Location = ConvertLocation(step.Location)
248            };
249        }
250    }
251}
Full Screen

FeatureChild.cs

Source: FeatureChild.cs Github

copy
1using System.Runtime.Serialization;
2
3namespace Gherkin.CucumberMessages.Types
4{
5    public class FeatureChild
6    {
7        [DataMember(Name = "scenario")]
8        public Scenario Scenario { get; set; }
9
10        [DataMember(Name = "background")]
11        public Background Background { get; set; }
12
13        [DataMember(Name = "rule")]
14        public Rule Rule { get; set; }
15
16        public FeatureChild()
17        {
18        }
19
20        public FeatureChild(Scenario scenario, Background background, Rule rule)
21        {
22            Scenario = scenario;
23            Background = background;
24            Rule = rule;
25        }
26    }
27}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in FeatureChild

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)