Best Gherkin-ruby code snippet using Gherkin.get_tokens
ast_builder.rb
Source:ast_builder.rb
...43 def get_tags(node)44 tags = []45 tags_node = node.get_single(:Tags)46 return tags unless tags_node47 tags_node.get_tokens(:TagLine).each do |token|48 token.matched_items.each do |tag_item|49 tags.push(Cucumber::Messages::GherkinDocument::Feature::Tag.new(50 location: get_location(token, tag_item.column),51 name: tag_item.text,52 id: @id_generator.new_id53 ))54 end55 end56 tags57 end58 def get_table_rows(node)59 rows = node.get_tokens(:TableRow).map do |token|60 Cucumber::Messages::GherkinDocument::Feature::TableRow.new(61 id: @id_generator.new_id,62 location: get_location(token, 0),63 cells: get_cells(token)64 )65 end66 ensure_cell_count(rows)67 rows68 end69 def ensure_cell_count(rows)70 return if rows.empty?71 cell_count = rows[0].cells.length72 rows.each do |row|73 if row.cells.length != cell_count74 location = {line: row.location.line, column: row.location.column}75 raise AstBuilderException.new("inconsistent cell count within the table", location)76 end77 end78 end79 def get_cells(table_row_token)80 table_row_token.matched_items.map do |cell_item|81 Cucumber::Messages::GherkinDocument::Feature::TableRow::TableCell.new(82 location: get_location(table_row_token, cell_item.column),83 value: cell_item.text84 )85 end86 end87 def get_description(node)88 node.get_single(:Description)89 end90 def get_steps(node)91 node.get_items(:Step)92 end93 def transform_node(node)94 case node.rule_type95 when :Step96 step_line = node.get_token(:StepLine)97 data_table = node.get_single(:DataTable)98 doc_string = node.get_single(:DocString)99 Cucumber::Messages::GherkinDocument::Feature::Step.new(100 location: get_location(step_line, 0),101 keyword: step_line.matched_keyword,102 text: step_line.matched_text,103 data_table: data_table,104 doc_string: doc_string,105 id: @id_generator.new_id106 )107 when :DocString108 separator_token = node.get_tokens(:DocStringSeparator)[0]109 media_type = separator_token.matched_text == '' ? nil : separator_token.matched_text110 line_tokens = node.get_tokens(:Other)111 content = line_tokens.map { |t| t.matched_text }.join("\n")112 Cucumber::Messages::GherkinDocument::Feature::Step::DocString.new(113 location: get_location(separator_token, 0),114 content: content,115 delimiter: separator_token.matched_keyword,116 media_type: media_type,117 )118 when :DataTable119 rows = get_table_rows(node)120 Cucumber::Messages::GherkinDocument::Feature::Step::DataTable.new(121 location: rows[0].location,122 rows: rows,123 )124 when :Background125 background_line = node.get_token(:BackgroundLine)126 description = get_description(node)127 steps = get_steps(node)128 Cucumber::Messages::GherkinDocument::Feature::Background.new(129 id: @id_generator.new_id,130 location: get_location(background_line, 0),131 keyword: background_line.matched_keyword,132 name: background_line.matched_text,133 description: description,134 steps: steps135 )136 when :ScenarioDefinition137 tags = get_tags(node)138 scenario_node = node.get_single(:Scenario)139 scenario_line = scenario_node.get_token(:ScenarioLine)140 description = get_description(scenario_node)141 steps = get_steps(scenario_node)142 examples = scenario_node.get_items(:ExamplesDefinition)143 Cucumber::Messages::GherkinDocument::Feature::Scenario.new(144 id: @id_generator.new_id,145 tags: tags,146 location: get_location(scenario_line, 0),147 keyword: scenario_line.matched_keyword,148 name: scenario_line.matched_text,149 description: description,150 steps: steps,151 examples: examples152 )153 when :ExamplesDefinition154 tags = get_tags(node)155 examples_node = node.get_single(:Examples)156 examples_line = examples_node.get_token(:ExamplesLine)157 description = get_description(examples_node)158 rows = examples_node.get_single(:ExamplesTable)159 table_header = rows.nil? ? nil : rows.first160 table_body = rows.nil? ? nil : rows[1..-1]161 Cucumber::Messages::GherkinDocument::Feature::Scenario::Examples.new(162 id: @id_generator.new_id,163 tags: tags,164 location: get_location(examples_line, 0),165 keyword: examples_line.matched_keyword,166 name: examples_line.matched_text,167 description: description,168 table_header: table_header,169 table_body: table_body,170 )171 when :ExamplesTable172 get_table_rows(node)173 when :Description174 line_tokens = node.get_tokens(:Other)175 # Trim trailing empty lines176 last_non_empty = line_tokens.rindex { |token| !token.line.trimmed_line_text.empty? }177 description = line_tokens[0..last_non_empty].map { |token| token.matched_text }.join("\n")178 return description179 when :Feature180 header = node.get_single(:FeatureHeader)181 return unless header182 tags = get_tags(header)183 feature_line = header.get_token(:FeatureLine)184 return unless feature_line185 children = []186 background = node.get_single(:Background)187 children.push(Cucumber::Messages::GherkinDocument::Feature::FeatureChild.new(background: background)) if background188 node.get_items(:ScenarioDefinition).each do |scenario|...
get_tokens
Using AI Code Generation
1 feature_file = File.open('test.feature', 'r')2 tokens = Gherkin::Lexer.new.lex(feature_file)3 feature_file = File.open('test.feature', 'r')4 tokens = Gherkin::Lexer.new.lex(feature_file)5/Users/rahul/.rvm/gems/ruby-2.0.0-p247/gems/gherkin-2.11.7/lib/gherkin/lexer/en.rb:1:in `require': cannot load such file -- gherkin/lexer/en (LoadError)6 from /Users/rahul/.rvm/gems/ruby-2.0.0-p247/gems/gherkin-2.11.7/lib/gherkin/lexer/en.rb:1:in `<top (required)>'
get_tokens
Using AI Code Generation
1def get_tokens(file)2 lexer = Gherkin::I18nLexer.new(File.open(file))3def print_tokens(tokens)4 formatter = Gherkin::Formatter::PrettyFormatter.new(STDOUT)5 formatter.token(token)6tokens = get_tokens('1.feature')7print_tokens(tokens)8def get_tokens(file)9 lexer = Gherkin::I18nLexer.new(File.open(file))10def print_tokens(tokens)11 formatter = Gherkin::Formatter::PrettyFormatter.new(STDOUT)12 formatter.token(token)13tokens = get_tokens('2.feature')14print_tokens(tokens)15def get_tokens(file)
get_tokens
Using AI Code Generation
1 def token(token)2parser = Gherkin::Parser::Parser.new(listener)3parser.parse(File.read('1.feature'), '1.feature')4 def initialize(line, column, value, token_type)5 def token(token)6parser = Gherkin::Parser::Parser.new(listener)7parser.parse(File.read('1.feature'), '1.feature')
get_tokens
Using AI Code Generation
1feature_file = File.open('test.feature')2tokens = gherkin.get_tokens(feature_file)3feature_file = File.open('test.feature')4tokens = gherkin.parse(feature_file)
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!!