How to use GherkinLine class of Gherkin package

Best Gherkin-dotnet code snippet using Gherkin.GherkinLine

GherkinParser.cs

Source:GherkinParser.cs Github

copy

Full Screen

...108            mapping = dialect.WhyKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeWhy) });109            foreach (var map in mapping)110            {111                if (current >= lines.Length) yield break;112                var gherkinLine = new GherkinLine(lines[current], current);113                if (gherkinLine.StartsWith(map.Key))114                {115                    yield return CreateNarrative(map, gherkinLine.GetRestTrimmed(map.Key.Length).Trim(), lineNumber + current);116                    current++;117                    hasTopLevel = true;118                    break;119                }120            }121            if (hasTopLevel)122            {123                hasTopLevel = false;124                // process And , But125                mapping = dialect.AndStepKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeAndWhy) })126                    .Union(dialect.ButStepKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeButWhy) }));127                var gotOne = false;128                while (true)129                {130                    foreach (var map in mapping)131                    {132                        if (current >= lines.Length) yield break;133                        var gherkinLine = new GherkinLine(lines[current], current);134                        if (gherkinLine.StartsWith(map.Key))135                        {136                            yield return CreateNarrative(map, gherkinLine.GetRestTrimmed(map.Key.Length), lineNumber + current);137                            current++;138                            gotOne = true;139                            break;140                        }141                    }142                    if (!gotOne) break;143                    gotOne = false;144                }145            }146            mapping = dialect.WhoKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeWho) });147            foreach (var map in mapping)148            {149                if (current >= lines.Length) yield break;150                var gherkinLine = new GherkinLine(lines[current], current);151                if (gherkinLine.StartsWith(map.Key))152                {153                    yield return CreateNarrative(map, gherkinLine.GetRestTrimmed(map.Key.Length), lineNumber + current);154                    current++;155                    hasTopLevel = true;156                    break;157                }158            }159            if (hasTopLevel)160            {161                hasTopLevel = false;162                // process And , But163                mapping = dialect.AndStepKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeAndWho) })164                    .Union(dialect.ButStepKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeButWho) }));165                var gotOne = false;166                while (true)167                {168                    foreach (var map in mapping)169                    {170                        if (current >= lines.Length) yield break;171                        var gherkinLine = new GherkinLine(lines[current], current);172                        if (gherkinLine.StartsWith(map.Key))173                        {174                            yield return CreateNarrative(map, gherkinLine.GetRestTrimmed(map.Key.Length), lineNumber + current);175                            current++;176                            gotOne = true;177                            break;178                        }179                    }180                    if (!gotOne) break;181                    gotOne = false;182                }183            }184            mapping = dialect.WhereKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeWhere) });185            foreach (var map in mapping)186            {187                if (current >= lines.Length) yield break;188                var gherkinLine = new GherkinLine(lines[current], current);189                if (gherkinLine.StartsWith(map.Key))190                {191                    yield return CreateNarrative(map, gherkinLine.GetRestTrimmed(map.Key.Length), lineNumber + current);192                    current++;193                    hasTopLevel = true;194                    break;195                }196            }197            if (hasTopLevel)198            {199                hasTopLevel = false;200                // process And , But201                mapping = dialect.AndStepKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeAndWhere) })202                    .Union(dialect.ButStepKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeButWhere) }));203                var gotOne = false;204                while (true)205                {206                    foreach (var map in mapping)207                    {208                        if (current >= lines.Length) yield break;209                        var gherkinLine = new GherkinLine(lines[current], current);210                        if (gherkinLine.StartsWith(map.Key))211                        {212                            yield return CreateNarrative(map, gherkinLine.GetRestTrimmed(map.Key.Length), lineNumber + current);213                            current++;214                            gotOne = true;215                            break;216                        }217                    }218                    if (!gotOne) break;219                    gotOne = false;220                }221            }222            mapping = dialect.WhatKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeWhat) });223            foreach (var map in mapping)224            {225                if (current >= lines.Length) yield break;226                var gherkinLine = new GherkinLine(lines[current], current);227                if (gherkinLine.StartsWith(map.Key))228                {229                    yield return CreateNarrative(map, gherkinLine.GetRestTrimmed(map.Key.Length), lineNumber + current);230                    current++;231                    hasTopLevel = true;232                    break;233                }234            }235            if (hasTopLevel)236            {237                hasTopLevel = false;238                // process And , But239                mapping = dialect.AndStepKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeAndWhat) })240                    .Union(dialect.ButStepKeywords.Select(w => new NarrativeMap { Key = w, Type = typeof(NarrativeButWhat) }));241                foreach (var map in mapping)242                {243                    if (current >= lines.Length) yield break;244                    var gherkinLine = new GherkinLine(lines[current], current);245                    if (gherkinLine.StartsWith(map.Key))246                    {247                        yield return CreateNarrative(map, gherkinLine.GetRestTrimmed(map.Key.Length), lineNumber + current);248                        current++;249                        break;250                    }251                }252            }253        }254        private struct NarrativeMap255        {256            public string Key;257            public Type Type;258        }...

Full Screen

Full Screen

GherkinSimpleParser.cs

Source:GherkinSimpleParser.cs Github

copy

Full Screen

...71        }72        private Token ToToken(string line)73        {74            var location = new Ast.Location(1);75            return new Token(new GherkinLine(line, 1), location);76        }77        public Token ParseToken(string line)78        {79            Token token = ToToken(line);80            if (TokenMatcher.Match_FeatureLine(token) ||81                TokenMatcher.Match_ScenarioLine(token) ||82                TokenMatcher.Match_ScenarioOutlineLine(token) ||83                TokenMatcher.Match_BackgroundLine(token) ||84                TokenMatcher.Match_ExamplesLine(token) ||85                TokenMatcher.Match_StepLine(token) ||86                TokenMatcher.Match_TagLine(token) ||87                TokenMatcher.Match_TableRow(token) ||88                SimpleMatchLanguage(token) ||89                TokenMatcher.Match_DocStringSeparator(token))...

Full Screen

Full Screen

GherkinLine.cs

Source:GherkinLine.cs Github

copy

Full Screen

...5using Gherkin.Ast;67namespace Gherkin8{9    public class GherkinLine : IGherkinLine10    {11        private static char[] inlineWhitespaceChars = new char[] { ' ', '\t', '\u00A0'};1213        private readonly string lineText;14        private readonly string trimmedLineText;15        public int LineNumber { get; private set; }1617        public GherkinLine(string line, int lineNumber)18        {19            this.LineNumber = lineNumber;2021            this.lineText = line;22            this.trimmedLineText = this.lineText.TrimStart();23        }2425        public void Detach()26        {27            //nop28        }2930        public int Indent31        {32            get { return lineText.Length - trimmedLineText.Length; }33        }3435        public bool IsEmpty()36        {37            return trimmedLineText.Length == 0;38        }3940        public bool StartsWith(string text)41        {42            return trimmedLineText.StartsWith(text);43        }4445        public bool StartsWithTitleKeyword(string text)46        {47            return StringUtils.StartsWith(trimmedLineText, text) &&48                StartsWithFrom(trimmedLineText, text.Length, GherkinLanguageConstants.TITLE_KEYWORD_SEPARATOR);49        }5051        private static bool StartsWithFrom(string text, int textIndex, string value)52        {53            return string.CompareOrdinal(text, textIndex, value, 0, value.Length) == 0;54        }5556        public string GetLineText(int indentToRemove)57        {58            if (indentToRemove < 0 || indentToRemove > Indent)59                return trimmedLineText;6061            return lineText.Substring(indentToRemove);62        }6364        public string GetRestTrimmed(int length)65        {66            return trimmedLineText.Substring(length).Trim();67        }6869        public IEnumerable<GherkinLineSpan> GetTags()70        {71            var uncommentedLine = Regex.Split(trimmedLineText, @"\s" + GherkinLanguageConstants.COMMENT_PREFIX)[0];72            int position = Indent;73            foreach (string item in uncommentedLine.Split(GherkinLanguageConstants.TAG_PREFIX[0]))74            {75                if (item.Length > 0)76                {77                    var tagName = GherkinLanguageConstants.TAG_PREFIX + item.TrimEnd(inlineWhitespaceChars);78                    if (tagName.Length == 1)79                        continue;8081                    if (tagName.IndexOfAny(inlineWhitespaceChars) >= 0)82                        throw new InvalidTagException("A tag may not contain whitespace", new Location(LineNumber, position));8384                    yield return new GherkinLineSpan(position, tagName);85                    position += item.Length;86                }87                position++; // separator88            }89        }90        91        public IEnumerable<GherkinLineSpan> GetTableCells()92        {93            var items = SplitCells(trimmedLineText).ToList();94            bool isBeforeFirst = true;95            foreach (var item in items.Take(items.Count - 1)) // skipping the one after last96            {97                if (!isBeforeFirst)98                {99                    int trimmedStart;100                    var cellText = Trim(item.Item1, out trimmedStart);101                    var cellPosition = item.Item2 + trimmedStart;102103                    if (cellText.Length == 0)104                        cellPosition = item.Item2;105106                    yield return new GherkinLineSpan(Indent + cellPosition + 1, cellText);107                }108109                isBeforeFirst = false;110            }111        }112113        private IEnumerable<Tuple<string, int>> SplitCells(string row)114        {115            var rowEnum = row.GetEnumerator();            116117            string cell = "";118            int pos = 0;119            int startPos = 0;120            while (rowEnum.MoveNext()) {
...

Full Screen

Full Screen

GherkinUtil.cs

Source:GherkinUtil.cs Github

copy

Full Screen

...34            ICSharpCode.AvalonEdit.Document.DocumentLine line = document.GetLineByNumber(1);35            while (line != null)36            {37                string line_text = document.GetText(line.Offset, line.TotalLength);38                Token token = new Token(new GherkinLine(line_text, line.LineNumber), location);39                try40                {41                    if (s_TokenMatcher.Match_Language(token) && IsSupported(token.MatchedText))42                    {43                        return token.MatchedText;44                    }45                    if (line.LineNumber > 5) return DEFAULT_LANGUAGE;46                    line = line.NextLine;47                }48                catch49                {50                    return DEFAULT_LANGUAGE;51                }52            }...

Full Screen

Full Screen

Lexer.cs

Source:Lexer.cs Github

copy

Full Screen

...17            _tokenReceiver = tokenReceiver;18        }19        public void Lex(string gherkin)20        {21            foreach (var gherkinLine in GetGherkinLines(gherkin))22            {23                MatchAndCallback("^Given (.*)", _tokenReceiver.Given, gherkinLine);24                MatchAndCallback("^When (.*)", _tokenReceiver.When, gherkinLine);25                MatchAndCallback("^Then (.*)", _tokenReceiver.Then, gherkinLine);26            }27            _tokenReceiver.EndOfFile();28        }29        private static void MatchAndCallback(string pattern, Action<string> callback, string gherkinLine)30        {31            var m = Regex.Match(gherkinLine, pattern);32            33            while (m.Success)34            {35                var theText = m.Groups[1].Value;36                callback(theText);37                38                m = m.NextMatch();39            }40        }41        private static string[] GetGherkinLines(string gherkin)42        {43            return gherkin.Split(new[] { "\n", "\r\n" }, StringSplitOptions.None);44        }45    }46}...

Full Screen

Full Screen

TokenScanner.cs

Source:TokenScanner.cs Github

copy

Full Screen

...26        public virtual Token Read()27        {28            var line = reader.ReadLine();29            var location = new Ast.Location(++lineNumber);30            return line == null ? new Token(null, location) : new Token(new GherkinLine(line, lineNumber), location);31        }32    }
...

Full Screen

Full Screen

GherkinLine

Using AI Code Generation

copy

Full Screen

1using Gherkin;2using Gherkin;3using Gherkin;4using Gherkin;5using Gherkin;6using Gherkin;7using Gherkin;8using Gherkin;9using Gherkin;10using Gherkin;11using Gherkin;12using Gherkin;13using Gherkin;14using Gherkin;15using Gherkin;16using Gherkin;17using Gherkin;18using Gherkin;19using Gherkin;20using Gherkin;21using Gherkin;22using Gherkin;23using Gherkin;24using Gherkin;25using Gherkin;

Full Screen

Full Screen

GherkinLine

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Gherkin;7using System.IO;8{9    {10        static void Main(string[] args)11        {12            string path = @"C:\Users\test\Documents\Visual Studio 2015\Projects\GherkinLine\GherkinLine\Feature1.feature";13            string line;14            StreamReader file = new StreamReader(path);15            while ((line = file.ReadLine()) != null)16            {17                GherkinLine gherkinLine = new GherkinLine(line, 0);18                Console.WriteLine(gherkinLine.getRestTrimmed(0));19            }20            Console.ReadLine();21        }22    }23}

Full Screen

Full Screen

GherkinLine

Using AI Code Generation

copy

Full Screen

1{2    {3        static void Main(string[] args)4        {5            GherkinLine gl = new GherkinLine("Scenario: Sample Scenario", 1);6            Console.WriteLine(gl.LineType);7            Console.WriteLine(gl.LineText);8            Console.WriteLine(gl.LineNumber);9            Console.WriteLine(gl.TrimmedLineText);10            Console.WriteLine(gl.Keyword);11            Console.WriteLine(gl.GetRestTrimmed(1));12            Console.WriteLine(gl.Indentation);13            Console.WriteLine(gl.Language);14            Console.WriteLine(gl.IsEmpty);15            Console.WriteLine(gl.IsBlank);16            Console.WriteLine(gl.IsComment);17            Console.WriteLine(gl.IsTag);18            Console.WriteLine(gl.IsLanguage);19            Console.WriteLine(gl.IsFeatureLine);20            Console.WriteLine(gl.IsBackgroundLine);21            Console.WriteLine(gl.IsScenarioLine);22            Console.WriteLine(gl.IsScenarioOutlineLine);23            Console.WriteLine(gl.IsExamplesLine);24            Console.WriteLine(gl.IsStepLine);25            Console.WriteLine(gl.IsDocStringSeparator);26            Console.WriteLine(gl.IsTableRow);27            Console.WriteLine(gl.IsEof);28            Console.WriteLine(gl.IsOther);29            Console.WriteLine(gl.IsNotEmpty);30            Console.WriteLine(gl.IsNotBlank);31            Console.WriteLine(gl.IsNotComment);32            Console.WriteLine(gl.IsNotTag);33            Console.WriteLine(gl.IsNotLanguage);

Full Screen

Full Screen

GherkinLine

Using AI Code Generation

copy

Full Screen

1using  Gherkin;2{3    {4         static   void  Main( string [] args)5        {6             string  line =  "Feature: 1" ;7            GherkinLine gherkinLine =  new  GherkinLine(line,  1 );8             string  lineText = gherkinLine.LineText;9             int  indent = gherkinLine.Indent;10             int  lineNumber = gherkinLine.LineNumber;11             bool  isEmpty = gherkinLine.IsEmpty;12             bool  isComment = gherkinLine.IsComment;13             bool  isStepLine = gherkinLine.IsStepLine;14             bool  isDocStringSeparator = gherkinLine.IsDocStringSeparator;15             bool  isTableRow = gherkinLine.IsTableRow;16             bool  isLanguage = gherkinLine.IsLanguage;17             bool  isTagLine = gherkinLine.IsTagLine;18             bool  isEOF = gherkinLine.IsEOF;19             bool  isTitle = gherkinLine.IsTitle;20             bool  isOther = gherkinLine.IsOther;21             string  trimmedLineText = gherkinLine.TrimmedLineText;22             string  keyword = gherkinLine.Keyword;23             string  title = gherkinLine.Title;24             string  restTrimmed = gherkinLine.RestTrimmed;25             string  indentString = gherkinLine.IndentString;26             string  name = gherkinLine.Name;27             string  description = gherkinLine.Description;28             string  language = gherkinLine.Language;29             string  tags = gherkinLine.Tags;30             string  contents = gherkinLine.Contents;31             string  comments = gherkinLine.Comments;32             string  tableCells = gherkinLine.TableCells;33             string  text = gherkinLine.Text;34             string  docStringSeparator = gherkinLine.DocStringSeparator;35             string  docStringContents = gherkinLine.DocStringContents;36             string  stepLine = gherkinLine.StepLine;37             string  stepKeyword = gherkinLine.StepKeyword;38             string  stepText = gherkinLine.StepText;39             string  stepArg = gherkinLine.StepArg;40             string  stepArgument = gherkinLine.StepArgument;

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