Best Gherkin-dotnet code snippet using Gherkin.CucumberMessages.Types.Background
AstMessagesConverter.cs
Source:AstMessagesConverter.cs
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 }...
PickleCompiler.cs
Source:PickleCompiler.cs
...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);...
Background.cs
Source:Background.cs
2using System.Runtime.Serialization;34namespace Gherkin.CucumberMessages.Types5{6 public class Background7 {8 [DataMember(Name = "location")]9 public Location Location { get; set; }1011 [DataMember(Name = "keyword")]12 public string Keyword { get; set; }1314 [DataMember(Name = "name")]15 public string Name { get; set; }1617 [DataMember(Name = "description")]18 public string Description { get; set; }1920 [DataMember(Name = "steps")]
...
FeatureChild.cs
Source:FeatureChild.cs
...5 {6 [DataMember(Name = "scenario")]7 public Scenario Scenario { get; set; }8 [DataMember(Name = "background")]9 public Background Background { get; set; }10 [DataMember(Name = "rule")]11 public Rule Rule { get; set; }12 public FeatureChild()13 {14 }15 public FeatureChild(Scenario scenario, Background background, Rule rule)16 {17 Scenario = scenario;18 Background = background;19 Rule = rule;20 }21 }22}...
RuleChild.cs
Source:RuleChild.cs
...3{4 public class RuleChild5 {6 [DataMember(Name = "background")]7 public Background Background { get; set; }8 [DataMember(Name = "scenario")]9 public Scenario Scenario { get; set; }10 public RuleChild()11 {12 }13 public RuleChild(Background background, Scenario scenario)14 {15 Background = background;16 Scenario = scenario;17 }18 }19}...
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!!