How to use BuildRule method of Gherkin.CucumberMessages.Pickles.PickleCompiler class

Best Gherkin-dotnet code snippet using Gherkin.CucumberMessages.Pickles.PickleCompiler.BuildRule

Run Gherkin-dotnet automation tests on LambdaTest cloud grid

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

PickleCompiler.cs

Source: PickleCompiler.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Gherkin.CucumberMessages.Types;
5
6// ReSharper disable PossibleMultipleEnumeration
7
8namespace Gherkin.CucumberMessages.Pickles
9{
10    public class PickleCompiler
11    {
12        private readonly IIdGenerator _idGenerator;
13
14        public PickleCompiler(IIdGenerator idGenerator)
15        {
16            _idGenerator = idGenerator;
17        }
18
19        public List<Pickle> Compile(GherkinDocument gherkinDocument)
20        {
21            var pickles = new List<Pickle>();
22            var feature = gherkinDocument.Feature;
23            if (feature == null)
24            {
25                return pickles;
26            }
27
28            var language = feature.Language;
29            var tags = feature.Tags;
30
31            BuildFeature(pickles, language, tags, Enumerable.Empty<PickleStep>, feature.Children, gherkinDocument.Uri);
32
33            return pickles;
34        }
35
36        protected virtual void BuildFeature(List<Pickle> pickles, string language, IEnumerable<Tag> tags,
37            Func<IEnumerable<PickleStep>> backgroundStepsFactory, IEnumerable<FeatureChild> children,
38            string gherkinDocumentUri) 
39        {
40            if (children == null)
41                return;
42
43            foreach (var child in children)
44            {
45                if (child.Background != null)
46                {
47                    backgroundStepsFactory = BuildBackground(child.Background, backgroundStepsFactory);
48                }
49                else if (child.Rule != null)
50                {
51                    var mergedRuleTags = tags.Concat(child.Rule.Tags);
52                    BuildRule(pickles, language, mergedRuleTags, backgroundStepsFactory, child.Rule.Children, gherkinDocumentUri);
53                }
54                else if (child.Scenario != null)
55                {
56                    BuildScenario(pickles, language, tags, backgroundStepsFactory, gherkinDocumentUri, child.Scenario);
57                }
58            }
59        }
60
61        protected virtual void BuildRule(List<Pickle> pickles, string language, IEnumerable<Tag> tags,
62            Func<IEnumerable<PickleStep>> backgroundStepsFactory, IEnumerable<RuleChild> children,
63            string gherkinDocumentUri) 
64        {
65            if (children == null)
66                return;
67
68            foreach (var child in children)
69            {
70                if (child.Background != null)
71                {
72                    backgroundStepsFactory = BuildBackground(child.Background, backgroundStepsFactory);
73                }
74                else if (child.Scenario != null)
75                {
76                    BuildScenario(pickles, language, tags, backgroundStepsFactory, gherkinDocumentUri, child.Scenario);
77                }
78            }
79        }
80
81        private Func<IEnumerable<PickleStep>> BuildBackground(Background background, Func<IEnumerable<PickleStep>> backgroundStepsFactory)
82        {
83            var previousFactory = backgroundStepsFactory;
84            backgroundStepsFactory = () => previousFactory().Concat(PickleSteps(background.Steps));
85            return backgroundStepsFactory;
86        }
87
88        private void BuildScenario(List<Pickle> pickles, string language, IEnumerable<Tag> tags, Func<IEnumerable<PickleStep>> backgroundStepsFactory, string gherkinDocumentUri, Scenario scenario)
89        {
90            if (!scenario.Examples.Any())
91            {
92                CompileScenario(pickles, backgroundStepsFactory, scenario, tags, language, gherkinDocumentUri);
93            }
94            else
95            {
96                CompileScenarioOutline(pickles, backgroundStepsFactory, scenario, tags, language, gherkinDocumentUri);
97            }
98        }
99
100        protected virtual void CompileScenario(List<Pickle> pickles,
101            Func<IEnumerable<PickleStep>> backgroundStepsFactory, Scenario scenario, IEnumerable<Tag> featureTags,
102            string language, string gherkinDocumentUri)
103        {
104            var steps = new List<PickleStep>();
105            if (scenario.Steps.Any())
106                steps.AddRange(backgroundStepsFactory());
107
108            var scenarioTags = new List<Tag>();
109            scenarioTags.AddRange(featureTags);
110            scenarioTags.AddRange(scenario.Tags);
111
112            steps.AddRange(PickleSteps(scenario.Steps));
113
114            Pickle pickle = new Pickle(
115                    _idGenerator.GetNewId(),
116                    gherkinDocumentUri,
117                    scenario.Name,
118                    language,
119                    steps,
120                    PickleTags(scenarioTags),
121                    new []{ scenario.Id }
122            );
123            pickles.Add(pickle);
124        }
125
126        protected virtual void CompileScenarioOutline(List<Pickle> pickles,
127            Func<IEnumerable<PickleStep>> backgroundStepsFactory, Scenario scenarioOutline,
128            IEnumerable<Tag> featureTags, string language, string gherkinDocumentUri)
129        {
130            foreach (var examples in scenarioOutline.Examples)
131            {
132                if (examples.TableHeader == null) continue;
133                var variableCells = examples.TableHeader.Cells;
134                foreach (var values in examples.TableBody)
135                {
136                    var valueCells = values.Cells;
137
138                    var steps = new List<PickleStep>();
139                    if (scenarioOutline.Steps.Any())
140                        steps.AddRange(backgroundStepsFactory());
141
142                    var tags = new List<Tag>();
143                    tags.AddRange(featureTags);
144                    tags.AddRange(scenarioOutline.Tags);
145                    tags.AddRange(examples.Tags);
146
147                    foreach(var scenarioOutlineStep in scenarioOutline.Steps)
148                    {
149                        string stepText = Interpolate(scenarioOutlineStep.Text, variableCells, valueCells);
150
151                        PickleStep pickleStep = CreatePickleStep(
152                                scenarioOutlineStep,
153                                stepText,
154                                CreatePickleArgument(scenarioOutlineStep, variableCells, valueCells),
155                                new[] { scenarioOutlineStep.Id, values.Id }
156                        );
157                        steps.Add(pickleStep);
158                    }
159
160                    Pickle pickle = new Pickle(
161                            _idGenerator.GetNewId(),
162                            gherkinDocumentUri,
163                            Interpolate(scenarioOutline.Name, variableCells, valueCells),
164                            language, 
165                            steps,
166                            PickleTags(tags),
167                            new[] { scenarioOutline.Id, values.Id }
168                    );
169
170                    pickles.Add(pickle);
171                }
172            }
173        }
174
175        protected virtual PickleStep CreatePickleStep(Step step, string text, PickleStepArgument argument, IEnumerable<string> astNodeIds)
176        {
177            return new PickleStep(argument, astNodeIds, _idGenerator.GetNewId(), text);
178        }
179
180        protected virtual PickleStepArgument CreatePickleArgument(Step argument)
181        {
182            var noCells = Enumerable.Empty<TableCell>();
183            return CreatePickleArgument(argument, noCells, noCells);
184        }
185
186        protected virtual PickleStepArgument CreatePickleArgument(Step step, IEnumerable<TableCell> variableCells, IEnumerable<TableCell> valueCells)
187        {
188            if (step.DataTable != null) {
189                var t = step.DataTable;
190                var rows = t.Rows;
191                var newRows = new List<PickleTableRow>(rows.Count());
192                foreach(var row in rows)
193                {
194                    var cells = row.Cells;
195                    var newCells = new List<PickleTableCell>();
196                    foreach(var cell in cells)
197                    {
198                        newCells.Add(
199                                new PickleTableCell(
200                                        Interpolate(cell.Value, variableCells, valueCells)
201                                )
202                        );
203                    }
204                    newRows.Add(new PickleTableRow(newCells));
205                }
206                return new PickleStepArgument
207                    {
208                        DataTable = new PickleTable(newRows)
209                    };
210            }
211
212            if (step.DocString != null) {
213                var ds = step.DocString;
214                return
215                    new PickleStepArgument
216                    {
217                        DocString = new PickleDocString(
218                            Interpolate(ds.Content, variableCells, valueCells),
219                            ds.MediaType == null ? null : Interpolate(ds.MediaType, variableCells, valueCells))
220                    };
221            } 
222            
223            return null;
224        }
225
226        protected virtual PickleStep[] PickleSteps(IEnumerable<Step> steps)
227        {
228            var result = new List<PickleStep>();
229            foreach(var step in steps)
230            {
231                result.Add(PickleStep(step));
232            }
233            return result.ToArray();
234        }
235
236        protected virtual PickleStep PickleStep(Step step)
237        {
238            return CreatePickleStep(
239                    step,
240                    step.Text,
241                    CreatePickleArgument(step),
242                    new []{ step.Id }
243            );
244        }
245
246        protected virtual string Interpolate(string name, IEnumerable<TableCell> variableCells, IEnumerable<TableCell> valueCells)
247        {
248            int col = 0;
249            foreach (var variableCell in variableCells)
250            {
251                var valueCell = valueCells.ElementAt(col++);
252                string header = variableCell.Value;
253                string value = valueCell.Value;
254                name = name.Replace("<" + header + ">", value);
255            }
256            return name;
257        }
258
259        protected virtual List<PickleTag> PickleTags(List<Tag> tags)
260        {
261            var result = new List<PickleTag>();
262            foreach(var tag in tags)
263            {
264                result.Add(PickleTag(tag));
265            }
266            return result;
267        }
268
269        protected virtual PickleTag PickleTag(Tag tag)
270        {
271            return new PickleTag(tag.Name, tag.Id);
272        }
273    }
274}
275
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 method in PickleCompiler

Trigger BuildRule code on LambdaTest Cloud Grid

Execute automation tests with BuildRule on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

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)