How to use Detach method of Gherkin.GherkinLine class

Best Gherkin-dotnet code snippet using Gherkin.GherkinLine.Detach

Run Gherkin-dotnet automation tests on LambdaTest cloud grid

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

GherkinLine.cs

Source: GherkinLine.cs Github

copy
1using System;
2using System.Text.RegularExpressions;
3using System.Collections.Generic;
4using System.Linq;
5using Gherkin.Ast;
6
7namespace Gherkin
8{
9    public class GherkinLine : IGherkinLine
10    {
11        private static char[] inlineWhitespaceChars = new char[] { ' ', '\t', '\u00A0'};
12
13        private readonly string lineText;
14        private readonly string trimmedLineText;
15        public int LineNumber { get; private set; }
16
17        public GherkinLine(string line, int lineNumber)
18        {
19            this.LineNumber = lineNumber;
20
21            this.lineText = line;
22            this.trimmedLineText = this.lineText.TrimStart();
23        }
24
25        public void Detach()
26        {
27            //nop
28        }
29
30        public int Indent
31        {
32            get { return lineText.Length - trimmedLineText.Length; }
33        }
34
35        public bool IsEmpty()
36        {
37            return trimmedLineText.Length == 0;
38        }
39
40        public bool StartsWith(string text)
41        {
42            return trimmedLineText.StartsWith(text);
43        }
44
45        public bool StartsWithTitleKeyword(string text)
46        {
47            return StringUtils.StartsWith(trimmedLineText, text) &&
48                StartsWithFrom(trimmedLineText, text.Length, GherkinLanguageConstants.TITLE_KEYWORD_SEPARATOR);
49        }
50
51        private static bool StartsWithFrom(string text, int textIndex, string value)
52        {
53            return string.CompareOrdinal(text, textIndex, value, 0, value.Length) == 0;
54        }
55
56        public string GetLineText(int indentToRemove)
57        {
58            if (indentToRemove < 0 || indentToRemove > Indent)
59                return trimmedLineText;
60
61            return lineText.Substring(indentToRemove);
62        }
63
64        public string GetRestTrimmed(int length)
65        {
66            return trimmedLineText.Substring(length).Trim();
67        }
68
69        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;
80
81                    if (tagName.IndexOfAny(inlineWhitespaceChars) >= 0)
82                        throw new InvalidTagException("A tag may not contain whitespace", new Location(LineNumber, position));
83
84                    yield return new GherkinLineSpan(position, tagName);
85                    position += item.Length;
86                }
87                position++; // separator
88            }
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 last
96            {
97                if (!isBeforeFirst)
98                {
99                    int trimmedStart;
100                    var cellText = Trim(item.Item1, out trimmedStart);
101                    var cellPosition = item.Item2 + trimmedStart;
102
103                    if (cellText.Length == 0)
104                        cellPosition = item.Item2;
105
106                    yield return new GherkinLineSpan(Indent + cellPosition + 1, cellText);
107                }
108
109                isBeforeFirst = false;
110            }
111        }
112
113        private IEnumerable<Tuple<string, int>> SplitCells(string row)
114        {
115            var rowEnum = row.GetEnumerator();            
116
117            string cell = "";
118            int pos = 0;
119            int startPos = 0;
120            while (rowEnum.MoveNext()) {
121                pos++;
122                char c = rowEnum.Current;
123                if (c.ToString() == GherkinLanguageConstants.TABLE_CELL_SEPARATOR) {
124                    yield return Tuple.Create(cell, startPos);
125                    cell = "";
126                    startPos = pos;
127                } else if (c == GherkinLanguageConstants.TABLE_CELL_ESCAPE_CHAR) {
128                    rowEnum.MoveNext();
129                    pos++;
130                    c = rowEnum.Current;
131                    if (c == GherkinLanguageConstants.TABLE_CELL_NEWLINE_ESCAPE) {
132                        cell += "\n";
133                    } else {
134                        if (c.ToString() != GherkinLanguageConstants.TABLE_CELL_SEPARATOR && c != GherkinLanguageConstants.TABLE_CELL_ESCAPE_CHAR) {
135                            cell += GherkinLanguageConstants.TABLE_CELL_ESCAPE_CHAR;
136                        }
137                        cell += c;
138                    }
139                } else {
140                    cell += c;
141                }
142            }
143            yield return Tuple.Create(cell, startPos);
144        }
145
146        private string Trim(string s, out int trimmedStart)
147        {
148            trimmedStart = 0;
149            while (trimmedStart < s.Length && inlineWhitespaceChars.Contains(s[trimmedStart]))
150                trimmedStart++;
151
152            return s.Trim(inlineWhitespaceChars);
153        }
154    }
155}
156
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

Trigger Detach code on LambdaTest Cloud Grid

Execute automation tests with Detach 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)