Best Gherkin-dotnet code snippet using Gherkin.CucumberMessages.Types.Pickle
PickleCompiler.cs
Source:PickleCompiler.cs
2using System.Collections.Generic;3using System.Linq;4using Gherkin.CucumberMessages.Types;5// ReSharper disable PossibleMultipleEnumeration6namespace Gherkin.CucumberMessages.Pickles7{8 public class PickleCompiler9 {10 private readonly IIdGenerator _idGenerator;11 public PickleCompiler(IIdGenerator idGenerator)12 {13 _idGenerator = idGenerator;14 }15 public List<Pickle> Compile(GherkinDocument gherkinDocument)16 {17 var pickles = new List<Pickle>();18 var feature = gherkinDocument.Feature;19 if (feature == null)20 {21 return pickles;22 }23 var language = feature.Language;24 var tags = feature.Tags;25 BuildFeature(pickles, language, tags, Enumerable.Empty<PickleStep>, feature.Children, gherkinDocument.Uri);26 return pickles;27 }28 protected virtual void BuildFeature(List<Pickle> pickles, string language, IEnumerable<Tag> tags,29 Func<IEnumerable<PickleStep>> backgroundStepsFactory, IEnumerable<FeatureChild> children,30 string gherkinDocumentUri) 31 {32 if (children == null)33 return;34 foreach (var child in children)35 {36 if (child.Background != null)37 {38 backgroundStepsFactory = BuildBackground(child.Background, backgroundStepsFactory);39 }40 else if (child.Rule != null)41 {42 var mergedRuleTags = tags.Concat(child.Rule.Tags);43 BuildRule(pickles, language, mergedRuleTags, backgroundStepsFactory, child.Rule.Children, gherkinDocumentUri);44 }45 else if (child.Scenario != null)46 {47 BuildScenario(pickles, language, tags, backgroundStepsFactory, gherkinDocumentUri, child.Scenario);48 }49 }50 }51 protected virtual void BuildRule(List<Pickle> pickles, string language, IEnumerable<Tag> tags,52 Func<IEnumerable<PickleStep>> backgroundStepsFactory, IEnumerable<RuleChild> children,53 string gherkinDocumentUri) 54 {55 if (children == null)56 return;57 foreach (var child in children)58 {59 if (child.Background != null)60 {61 backgroundStepsFactory = BuildBackground(child.Background, backgroundStepsFactory);62 }63 else if (child.Scenario != null)64 {65 BuildScenario(pickles, language, tags, backgroundStepsFactory, gherkinDocumentUri, child.Scenario);66 }67 }68 }69 private Func<IEnumerable<PickleStep>> BuildBackground(Background background, Func<IEnumerable<PickleStep>> backgroundStepsFactory)70 {71 var previousFactory = backgroundStepsFactory;72 backgroundStepsFactory = () => previousFactory().Concat(PickleSteps(background.Steps));73 return backgroundStepsFactory;74 }75 private void BuildScenario(List<Pickle> pickles, string language, IEnumerable<Tag> tags, Func<IEnumerable<PickleStep>> backgroundStepsFactory, string gherkinDocumentUri, Scenario scenario)76 {77 if (!scenario.Examples.Any())78 {79 CompileScenario(pickles, backgroundStepsFactory, scenario, tags, language, gherkinDocumentUri);80 }81 else82 {83 CompileScenarioOutline(pickles, backgroundStepsFactory, scenario, tags, language, gherkinDocumentUri);84 }85 }86 protected virtual void CompileScenario(List<Pickle> pickles,87 Func<IEnumerable<PickleStep>> backgroundStepsFactory, Scenario scenario, IEnumerable<Tag> featureTags,88 string language, string gherkinDocumentUri)89 {90 var steps = new List<PickleStep>();91 if (scenario.Steps.Any())92 steps.AddRange(backgroundStepsFactory());93 var scenarioTags = new List<Tag>();94 scenarioTags.AddRange(featureTags);95 scenarioTags.AddRange(scenario.Tags);96 steps.AddRange(PickleSteps(scenario.Steps));97 Pickle pickle = new Pickle(98 _idGenerator.GetNewId(),99 gherkinDocumentUri,100 scenario.Name,101 language,102 steps,103 PickleTags(scenarioTags),104 new []{ scenario.Id }105 );106 pickles.Add(pickle);107 }108 protected virtual void CompileScenarioOutline(List<Pickle> pickles,109 Func<IEnumerable<PickleStep>> backgroundStepsFactory, Scenario scenarioOutline,110 IEnumerable<Tag> featureTags, string language, string gherkinDocumentUri)111 {112 foreach (var examples in scenarioOutline.Examples)113 {114 if (examples.TableHeader == null) continue;115 var variableCells = examples.TableHeader.Cells;116 foreach (var values in examples.TableBody)117 {118 var valueCells = values.Cells;119 var steps = new List<PickleStep>();120 if (scenarioOutline.Steps.Any())121 steps.AddRange(backgroundStepsFactory());122 var tags = new List<Tag>();123 tags.AddRange(featureTags);124 tags.AddRange(scenarioOutline.Tags);125 tags.AddRange(examples.Tags);126 foreach(var scenarioOutlineStep in scenarioOutline.Steps)127 {128 string stepText = Interpolate(scenarioOutlineStep.Text, variableCells, valueCells);129 PickleStep pickleStep = CreatePickleStep(130 scenarioOutlineStep,131 stepText,132 CreatePickleArgument(scenarioOutlineStep, variableCells, valueCells),133 new[] { scenarioOutlineStep.Id, values.Id }134 );135 steps.Add(pickleStep);136 }137 Pickle pickle = new Pickle(138 _idGenerator.GetNewId(),139 gherkinDocumentUri,140 Interpolate(scenarioOutline.Name, variableCells, valueCells),141 language, 142 steps,143 PickleTags(tags),144 new[] { scenarioOutline.Id, values.Id }145 );146 pickles.Add(pickle);147 }148 }149 }150 protected virtual PickleStep CreatePickleStep(Step step, string text, PickleStepArgument argument, IEnumerable<string> astNodeIds)151 {152 return new PickleStep(argument, astNodeIds, _idGenerator.GetNewId(), text);153 }154 protected virtual PickleStepArgument CreatePickleArgument(Step argument)155 {156 var noCells = Enumerable.Empty<TableCell>();157 return CreatePickleArgument(argument, noCells, noCells);158 }159 protected virtual PickleStepArgument CreatePickleArgument(Step step, IEnumerable<TableCell> variableCells, IEnumerable<TableCell> valueCells)160 {161 if (step.DataTable != null) {162 var t = step.DataTable;163 var rows = t.Rows;164 var newRows = new List<PickleTableRow>(rows.Count());165 foreach(var row in rows)166 {167 var cells = row.Cells;168 var newCells = new List<PickleTableCell>();169 foreach(var cell in cells)170 {171 newCells.Add(172 new PickleTableCell(173 Interpolate(cell.Value, variableCells, valueCells)174 )175 );176 }177 newRows.Add(new PickleTableRow(newCells));178 }179 return new PickleStepArgument180 {181 DataTable = new PickleTable(newRows)182 };183 }184 if (step.DocString != null) {185 var ds = step.DocString;186 return187 new PickleStepArgument188 {189 DocString = new PickleDocString(190 Interpolate(ds.Content, variableCells, valueCells),191 ds.MediaType == null ? null : Interpolate(ds.MediaType, variableCells, valueCells))192 };193 } 194 195 return null;196 }197 protected virtual PickleStep[] PickleSteps(IEnumerable<Step> steps)198 {199 var result = new List<PickleStep>();200 foreach(var step in steps)201 {202 result.Add(PickleStep(step));203 }204 return result.ToArray();205 }206 protected virtual PickleStep PickleStep(Step step)207 {208 return CreatePickleStep(209 step,210 step.Text,211 CreatePickleArgument(step),212 new []{ step.Id }213 );214 }215 protected virtual string Interpolate(string name, IEnumerable<TableCell> variableCells, IEnumerable<TableCell> valueCells)216 {217 int col = 0;218 foreach (var variableCell in variableCells)219 {220 var valueCell = valueCells.ElementAt(col++);221 string header = variableCell.Value;222 string value = valueCell.Value;223 name = name.Replace("<" + header + ">", value);224 }225 return name;226 }227 protected virtual List<PickleTag> PickleTags(List<Tag> tags)228 {229 var result = new List<PickleTag>();230 foreach(var tag in tags)231 {232 result.Add(PickleTag(tag));233 }234 return result;235 }236 protected virtual PickleTag PickleTag(Tag tag)237 {238 return new PickleTag(tag.Name, tag.Id);239 }240 }241}...
GherkinEventsProvider.cs
Source:GherkinEventsProvider.cs
1using System;2using System.Collections.Generic;3using System.IO;4using Gherkin.CucumberMessages;5using Gherkin.CucumberMessages.Pickles;6using Gherkin.CucumberMessages.Types;7namespace Gherkin.Specs.EventStubs8{9 public class GherkinEventsProvider10 {11 private readonly Parser _parser = new Parser();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);...
PicklesTests.cs
Source:PicklesTests.cs
...4using Gherkin.Specs.Helper;5using Xunit;6namespace Gherkin.Specs7{8 public class PicklesTests : EventTestBase9 {10 [Theory, MemberData(nameof(TestFileProvider.GetValidTestFiles), MemberType = typeof(TestFileProvider))]11 public void TestPickleCompilation(string testFeatureFile)12 {13 var testFile = GetFullPathToTestFeatureFile(testFeatureFile, "good", ".pickles.ndjson");14 var expectedContent = GetExpectedContent(testFile.ExpectedFileFullPath);15 var expectedEvents = NDJsonParser.Deserialize<Envelope>(expectedContent);16 var raisedEvents = ProcessGherkinEvents(testFile.FullPath, false, false, true);17 raisedEvents.Should().Match(list => list.All(e => e.Pickle != null));18 AssertEvents(testFeatureFile, raisedEvents, expectedEvents, testFile);19 }20 }21}...
Envelope.cs
Source:Envelope.cs
...11 [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}
...
PickleTableRow.cs
Source:PickleTableRow.cs
1using System.Collections.Generic;2using System.Runtime.Serialization;3namespace Gherkin.CucumberMessages.Types4{5 public class PickleTableRow6 {7 [DataMember(Name = "cells")]8 public IEnumerable<PickleTableCell> Cells { get; set; }9 public PickleTableRow()10 {11 }12 13 public PickleTableRow(IEnumerable<PickleTableCell> cells)14 {15 Cells = cells;16 }17 }18}...
PickleTable.cs
Source:PickleTable.cs
1using System.Collections.Generic;2using System.Runtime.Serialization;3namespace Gherkin.CucumberMessages.Types4{5 public class PickleTable6 {7 [DataMember(Name = "rows")]8 public IEnumerable<PickleTableRow> Rows { get; set; }9 public PickleTable()10 {11 }12 13 public PickleTable(IEnumerable<PickleTableRow> rows)14 {15 Rows = rows;16 }17 }18}...
PickleTableCell.cs
Source:PickleTableCell.cs
1using System.Runtime.Serialization;2namespace Gherkin.CucumberMessages.Types3{4 public class PickleTableCell5 {6 [DataMember(Name = "value")]7 public string Value { get; set; }8 public PickleTableCell()9 {10 }11 12 public PickleTableCell(string value)13 {14 Value = value;15 }16 }17}...
PickleStepArgument.cs
Source:PickleStepArgument.cs
1using System.Runtime.Serialization;2namespace Gherkin.CucumberMessages.Types3{4 public class PickleStepArgument5 {6 [DataMember(Name = "docString")]7 public PickleDocString DocString { get; set; }8 [DataMember(Name = "dataTable")]9 public PickleTable DataTable { get; set; }10 }11}...
Pickle
Using AI Code Generation
1using System;2using System.IO;3using System.Linq;4using Gherkin.CucumberMessages.Types;5using Newtonsoft.Json;6using Newtonsoft.Json.Linq;7using Newtonsoft.Json.Serialization;8{9 {10 static void Main(string[] args)11 {12 var source = File.ReadAllText("1.feature");13 var messages = new Messages(source);14 Pickle pickle = messages.Pickles.First();15 var json = JsonConvert.SerializeObject(pickle, new JsonSerializerSettings16 {17 ContractResolver = new CamelCasePropertyNamesContractResolver()18 });19 Console.WriteLine(json);20 }21 }22}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!