Best Gherkin-ruby code snippet using Gherkin.location
case_spec.rb
Source:case_spec.rb
...97 compile [gherkin], receiver98 end99 end100 end101 describe "#location" do102 context "created from a scenario" do103 it "takes its location from the location of the scenario" do104 gherkin = gherkin('features/foo.feature') do105 feature do106 scenario do107 step108 end109 end110 end111 receiver = double.as_null_object112 expect( receiver ).to receive(:test_case) do |test_case|113 expect( test_case.location.to_s ).to eq 'features/foo.feature:3'114 end115 compile([gherkin], receiver)116 end117 end118 context "created from a scenario outline example" do119 it "takes its location from the location of the scenario outline example row" do120 gherkin = gherkin('features/foo.feature') do121 feature do122 scenario_outline do123 step 'passing with arg'124 examples do125 row 'arg'126 row '1'127 row '2'128 end129 end130 end131 end132 receiver = double.as_null_object133 expect( receiver ).to receive(:test_case) do |test_case|134 expect( test_case.location.to_s ).to eq 'features/foo.feature:8'135 end.once.ordered136 expect( receiver ).to receive(:test_case) do |test_case|137 expect( test_case.location.to_s ).to eq 'features/foo.feature:9'138 end.once.ordered139 compile [gherkin], receiver140 end141 end142 end143 describe "#tags" do144 it "includes all tags from the parent feature" do145 gherkin = gherkin do146 feature tags: ['@a', '@b'] do147 scenario tags: ['@c'] do148 step149 end150 scenario_outline tags: ['@d'] do151 step 'passing with arg'152 examples tags: ['@e'] do153 row 'arg'154 row 'x'155 end156 end157 end158 end159 receiver = double.as_null_object160 expect( receiver ).to receive(:test_case) do |test_case|161 expect( test_case.tags.map(&:name) ).to eq ['@a', '@b', '@c']162 end.once.ordered163 expect( receiver ).to receive(:test_case) do |test_case|164 expect( test_case.tags.map(&:name) ).to eq ['@a', '@b', '@d', '@e']165 end.once.ordered166 compile [gherkin], receiver167 end168 end169 describe "matching tags" do170 it "matches boolean expressions of tags" do171 gherkin = gherkin do172 feature tags: ['@a', '@b'] do173 scenario tags: ['@c'] do174 step175 end176 end177 end178 receiver = double.as_null_object179 expect( receiver ).to receive(:test_case) do |test_case|180 expect( test_case.match_tags?('@a') ).to be_truthy181 end182 compile [gherkin], receiver183 end184 end185 describe "matching names" do186 it "matches names against regexp" do187 gherkin = gherkin do188 feature 'first feature' do189 scenario 'scenario' do190 step 'missing'191 end192 end193 end194 receiver = double.as_null_object195 expect( receiver ).to receive(:test_case) do |test_case|196 expect( test_case.match_name?(/feature/) ).to be_truthy197 end198 compile [gherkin], receiver199 end200 end201 describe "#language" do202 it 'takes its language from the feature' do203 gherkin = Gherkin::Document.new('features/treasure.feature', %{# language: en-pirate204 Ahoy matey!: Treasure map205 Heave to: Find the treasure206 Gangway!: a map207 })208 receiver = double.as_null_object209 expect( receiver ).to receive(:test_case) do |test_case|210 expect( test_case.language.iso_code ).to eq 'en-pirate'211 end212 compile([gherkin], receiver)213 end214 end215 describe "matching location" do216 let(:file) { 'features/path/to/the.feature' }217 let(:test_cases) do218 receiver = double.as_null_object219 result = []220 allow( receiver ).to receive(:test_case) { |test_case| result << test_case }221 compile [source], receiver222 result223 end224 context "for a scenario" do225 let(:source) do226 Gherkin::Document.new(file, <<-END.unindent)227 Feature:228 Scenario: one229 Given one a230 #Â comment231 @tags232 Scenario: two233 Given two a234 And two b235 Scenario: three236 Given three b237 Scenario: with docstring238 Given a docstring239 """240 this is a docstring241 """242 Scenario: with a table243 Given a table244 | a | b |245 | 1 | 2 |246 Scenario: empty247 END248 end249 let(:test_case) do250 test_cases.find { |c| c.name == 'two' }251 end252 it 'matches the precise location of the scenario' do253 location = Ast::Location.new(file, 8)254 expect( test_case.match_locations?([location]) ).to be_truthy255 end256 it 'matches the precise location of an empty scenario' do257 empty_scenario_test_case = test_cases.find { |c| c.name == 'empty' }258 location = Ast::Location.new(file, 26)259 expect( empty_scenario_test_case.match_locations?([location]) ).to be_truthy260 end261 it 'matches multiple locations' do262 good_location = Ast::Location.new(file, 8)263 bad_location = Ast::Location.new(file, 5)264 expect( test_case.match_locations?([good_location, bad_location]) ).to be_truthy265 end266 it 'matches a location on the last step of the scenario' do267 location = Ast::Location.new(file, 10)268 expect( test_case.match_locations?([location]) ).to be_truthy269 end270 it "matches a location on the scenario's comment" do271 location = Ast::Location.new(file, 6)272 expect( test_case.match_locations?([location]) ).to be_truthy273 end274 it "matches a location on the scenario's tags" do275 location = Ast::Location.new(file, 7)276 expect( test_case.match_locations?([location]) ).to be_truthy277 end278 it "doesn't match a location after the last step of the scenario" do279 location = Ast::Location.new(file, 11)280 expect( test_case.match_locations?([location]) ).to be_falsey281 end282 it "doesn't match a location before the scenario" do283 location = Ast::Location.new(file, 5)284 expect( test_case.match_locations?([location]) ).to be_falsey285 end286 context "with a docstring" do287 let(:test_case) do288 test_cases.find { |c| c.name == 'with docstring' }289 end290 it "matches a location at the start the docstring" do291 location = Ast::Location.new(file, 17)292 expect( test_case.match_locations?([location]) ).to be_truthy293 end294 it "matches a location in the middle of the docstring" do295 location = Ast::Location.new(file, 18)296 expect( test_case.match_locations?([location]) ).to be_truthy297 end298 end299 context "with a table" do300 let(:test_case) do301 test_cases.find { |c| c.name == 'with a table' }302 end303 it "matches a location on the first table row" do304 location = Ast::Location.new(file, 23)305 expect( test_case.match_locations?([location]) ).to be_truthy306 end307 end308 end309 context "for a scenario outline" do310 let(:source) do311 Gherkin::Document.new(file, <<-END.unindent)312 Feature:313 Scenario: one314 Given one a315 #Â comment on line 6316 @tags-on-line-7317 Scenario Outline: two318 Given two a319 And two <arg>320 #Â comment on line 12321 @tags-on-line-13322 Examples: x1323 | arg |324 | b |325 Examples: x2326 | arg |327 | c |328 Scenario: three329 Given three b330 END331 end332 let(:test_case) do333 test_cases.find { |c| c.name == "two, x1 (#1)" }334 end335 it 'matches the precise location of the scenario outline examples table row' do336 location = Ast::Location.new(file, 16)337 expect( test_case.match_locations?([location]) ).to be_truthy338 end339 it 'matches a location on a step of the scenario outline' do340 location = Ast::Location.new(file, 10)341 expect( test_case.match_locations?([location]) ).to be_truthy342 end343 it "matches a location on the scenario outline's comment" do344 location = Ast::Location.new(file, 6)345 expect( test_case.match_locations?([location]) ).to be_truthy346 end347 it "matches a location on the scenario outline's tags" do348 location = Ast::Location.new(file, 7)349 expect( test_case.match_locations?([location]) ).to be_truthy350 end351 it "doesn't match a location after the last row of the examples table" do352 location = Ast::Location.new(file, 17)353 expect( test_case.match_locations?([location]) ).to be_falsey354 end355 it "doesn't match a location before the scenario outline" do356 location = Ast::Location.new(file, 5)357 expect( test_case.match_locations?([location]) ).to be_falsey358 end359 end360 end361 end362 end363 end364end...
parser_spec.rb
Source:parser_spec.rb
...13 expect(ast).to eq({14 feature: {15 type: :Feature,16 tags: [],17 location: {line: 1, column: 1},18 language: "en",19 keyword: "Feature",20 name: "test",21 children: []22 },23 comments: [],24 type: :GherkinDocument25 })26 end27 it "parses a complex feature" do28 # This should include every significant type of thing within the language29 feature_text = "@feature_tag\n" +30 "Feature: feature name\n" +31 " feature description\n" +32 "\n" +33 " Background: background name\n" +34 " background description\n" +35 " * a step\n" +36 "\n" +37 " @scenario_tag\n" +38 " Scenario: scenario name\n" +39 " scenario description\n" +40 " * a step with a table\n" +41 " | a table |\n" +42 "\n" +43 " @outline_tag\n" +44 " Scenario Outline: outline name\n" +45 " outline description\n" +46 " * a step with a doc string\n" +47 " \"\"\" content_type\n" +48 " lots of text\n" +49 " \"\"\"\n" +50 " # Random file comment\n" +51 " @example_tag\n" +52 " Examples: examples name\n" +53 " examples description\n" +54 " | param |\n" +55 " | value |\n"56 parser = Parser.new57 scanner = TokenScanner.new(feature_text)58 ast = parser.parse(scanner)59 expect(ast).to eq({60 feature: {type: :Feature,61 tags: [{type: :Tag,62 location: {line: 1, column: 1},63 name: "@feature_tag"}],64 location: {line: 2, column: 1},65 language: "en",66 keyword: "Feature",67 name: "feature name",68 description: " feature description",69 children: [{type: :Background,70 location: {line: 5, column: 4},71 keyword: "Background",72 name: "background name",73 description: " background description",74 steps: [{type: :Step,75 location: {line: 7, column: 5},76 keyword: "* ",77 text: "a step"}]},78 {type: :Scenario,79 tags: [{type: :Tag,80 location: {line: 9, column: 3},81 name: "@scenario_tag"}],82 location: {line: 10, column: 3},83 keyword: "Scenario",84 name: "scenario name",85 description: " scenario description",86 steps: [{type: :Step,87 location: {line: 12, column: 5},88 keyword: "* ",89 text: "a step with a table",90 argument: {type: :DataTable,91 location: {line: 13, column: 7},92 rows: [{type: :TableRow,93 location: {line: 13, column: 7},94 cells: [{type: :TableCell,95 location: {line: 13, column: 9},96 value: "a table"}]}]}}]},97 {type: :ScenarioOutline,98 tags: [{type: :Tag,99 location: {line: 15, column: 3},100 name: "@outline_tag"}],101 location: {line: 16, column: 3},102 keyword: "Scenario Outline",103 name: "outline name",104 description: " outline description",105 steps: [{type: :Step,106 location: {line: 18, column: 5},107 keyword: "* ",108 text: "a step with a doc string",109 argument: {type: :DocString,110 location: {line: 19, column: 7},111 contentType: "content_type",112 content: " lots of text"}}],113 examples: [{type: :Examples,114 tags: [{type: :Tag,115 location: {line: 23, column: 3},116 name: "@example_tag"}],117 location: {line: 24, column: 3},118 keyword: "Examples",119 name: "examples name",120 description: " examples description",121 tableHeader: {type: :TableRow,122 location: {line: 26, column: 5},123 cells: [{type: :TableCell,124 location: {line: 26, column: 7},125 value: "param"}]},126 tableBody: [{type: :TableRow,127 location: {line: 27, column: 5},128 cells: [{type: :TableCell,129 location: {line: 27, column: 7},130 value: "value"}]}]}]}],131 },132 comments: [{type: :Comment,133 location: {line: 22, column: 1},134 text: " # Random file comment"}],135 type: :GherkinDocument}136 )137 end138 it "parses string feature" do139 parser = Parser.new140 ast = parser.parse("Feature: test")141 expect(ast).to eq({142 feature: {143 type: :Feature,144 tags: [],145 location: {line: 1, column: 1},146 language: "en",147 keyword: "Feature",148 name: "test",149 children: []150 },151 comments: [],152 type: :GherkinDocument153 })154 end155 it "parses io feature" do156 parser = Parser.new157 ast = parser.parse(StringIO.new("Feature: test"))158 expect(ast).to eq({159 feature: {160 type: :Feature,161 tags: [],162 location: {line: 1, column: 1},163 language: "en",164 keyword: "Feature",165 name: "test",166 children: []167 },168 comments: [],169 type: :GherkinDocument170 })171 end172 it "can parse multiple features" do173 parser = Parser.new174 ast1 = parser.parse(TokenScanner.new("Feature: test"))175 ast2 = parser.parse(TokenScanner.new("Feature: test2"))176 expect(ast1).to eq({177 feature: {178 type: :Feature,179 tags: [],180 location: {line: 1, column: 1},181 language: "en",182 keyword: "Feature",183 name: "test",184 children: []185 },186 comments: [],187 type: :GherkinDocument188 })189 expect(ast2).to eq({190 feature: {191 type: :Feature,192 tags: [],193 location: {line: 1, column: 1},194 language: "en",195 keyword: "Feature",196 name: "test2",197 children: []198 },199 comments: [],200 type: :GherkinDocument201 })202 end203 it "can parse feature after parse error" do204 parser = Parser.new205 matcher = TokenMatcher.new206 expect { parser.parse(TokenScanner.new("# a comment\n" +207 "Feature: Foo\n" +208 " Scenario: Bar\n" +209 " Given x\n" +210 " ```\n" +211 " unclosed docstring\n"),212 matcher)213 }.to raise_error(ParserError)214 ast = parser.parse(TokenScanner.new("Feature: Foo\n" +215 " Scenario: Bar\n" +216 " Given x\n" +217 ' """' + "\n" +218 " closed docstring\n" +219 ' """' + "\n"),220 matcher)221 expect(ast).to eq({222 feature: {223 type: :Feature,224 tags: [],225 location: {line: 1, column: 1},226 language: "en",227 keyword: "Feature",228 name: "Foo",229 children: [{230 :type=>:Scenario,231 :tags=>[],232 :location=>{:line=>2, :column=>3},233 :keyword=>"Scenario",234 :name=>"Bar",235 :steps=>[{236 :type=>:Step,237 :location=>{:line=>3, :column=>5},238 :keyword=>"Given ",239 :text=>"x",240 :argument=>{:type=>:DocString,241 :location=>{:line=>4, :column=>7},242 :content=>"closed docstring"}}]}]243 },244 comments: [],245 type: :GherkinDocument246 })247 end248 it "can change the default language" do249 parser = Parser.new250 matcher = TokenMatcher.new("no")251 scanner = TokenScanner.new("Egenskap: i18n support")252 ast = parser.parse(scanner, matcher)253 expect(ast).to eq({254 feature: {255 type: :Feature,256 tags: [],257 location: {line: 1, column: 1},258 language: "no",259 keyword: "Egenskap",260 name: "i18n support",261 children: []262 },263 comments: [],264 type: :GherkinDocument265 })266 end267 end268end...
ast_builder.rb
Source:ast_builder.rb
...19 end20 def build(token)21 if token.matched_type == :Comment22 @comments.push(Cucumber::Messages::GherkinDocument::Comment.new(23 location: get_location(token, 0),24 text: token.matched_text25 ))26 else27 current_node.add(token.matched_type, token)28 end29 end30 def get_result31 current_node.get_single(:GherkinDocument)32 end33 def current_node34 @stack.last35 end36 def get_location(token, column)37 column = column == 0 ? token.location[:column] : column38 Cucumber::Messages::Location.new(39 line: token.location[:line],40 column: column41 )42 end43 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|189 children.push(Cucumber::Messages::GherkinDocument::Feature::FeatureChild.new(scenario: scenario))190 end191 node.get_items(:Rule).each do |rule|192 children.push(Cucumber::Messages::GherkinDocument::Feature::FeatureChild.new(rule: rule))193 end194 description = get_description(header)195 language = feature_line.matched_gherkin_dialect196 Cucumber::Messages::GherkinDocument::Feature.new(197 tags: tags,198 location: get_location(feature_line, 0),199 language: language,200 keyword: feature_line.matched_keyword,201 name: feature_line.matched_text,202 description: description,203 children: children,204 )205 when :Rule206 header = node.get_single(:RuleHeader)207 return unless header208 rule_line = header.get_token(:RuleLine)209 return unless rule_line210 children = []211 background = node.get_single(:Background)212 children.push(Cucumber::Messages::GherkinDocument::Feature::FeatureChild::RuleChild.new(background: background)) if background213 node.get_items(:ScenarioDefinition).each do |scenario|214 children.push(Cucumber::Messages::GherkinDocument::Feature::FeatureChild::RuleChild.new(scenario: scenario))215 end216 description = get_description(header)217 Cucumber::Messages::GherkinDocument::Feature::FeatureChild::Rule.new(218 id: @id_generator.new_id,219 location: get_location(rule_line, 0),220 keyword: rule_line.matched_keyword,221 name: rule_line.matched_text,222 description: description,223 children: children,224 )225 when :GherkinDocument226 feature = node.get_single(:Feature)227 {228 feature: feature,229 comments: @comments230 }231 else232 return node233 end...
ast_lookup.rb
Source:ast_lookup.rb
...15 def gherkin_document(uri)16 @gherkin_documents[uri]17 end18 def scenario_source(test_case)19 uri = test_case.location.file20 @test_case_lookups[uri] ||= TestCaseLookupBuilder.new(gherkin_document(uri)).lookup_hash21 @test_case_lookups[uri][test_case.location.lines.max]22 end23 def step_source(test_step)24 uri = test_step.location.file25 @test_step_lookups[uri] ||= TestStepLookupBuilder.new(gherkin_document(uri)).lookup_hash26 @test_step_lookups[uri][test_step.location.lines.min]27 end28 def snippet_step_keyword(test_step)29 uri = test_step.location.file30 document = gherkin_document(uri)31 dialect = ::Gherkin::Dialect.for(document.feature.language)32 given_when_then_keywords = [dialect.given_keywords, dialect.when_keywords, dialect.then_keywords].flatten.uniq.reject { |kw| kw == '* ' }33 keyword_lookup = step_keyword_lookup(uri)34 keyword = nil35 node = keyword_lookup[test_step.location.lines.min]36 while keyword.nil?37 if given_when_then_keywords.include?(node.keyword)38 keyword = node.keyword39 break40 end41 break if node.previous_node.nil?42 node = node.previous_node43 end44 keyword = dialect.given_keywords.reject { |kw| kw == '* ' }[0] if keyword.nil?45 keyword = Cucumber::Gherkin::I18n.code_keyword_for(keyword)46 keyword47 end48 ScenarioSource = Struct.new(:type, :scenario)49 ScenarioOutlineSource = Struct.new(:type, :scenario_outline, :examples, :row)50 StepSource = Struct.new(:type, :step)51 private52 def step_keyword_lookup(uri)53 @step_keyword_lookups[uri] ||= KeywordLookupBuilder.new(gherkin_document(uri)).lookup_hash54 end55 class TestCaseLookupBuilder56 attr_reader :lookup_hash57 def initialize(gherkin_document)58 @lookup_hash = {}59 process_scenario_container(gherkin_document.feature)60 end61 private62 def process_scenario_container(container)63 container.children.each do |child|64 if child.respond_to?(:rule) && child.rule65 process_scenario_container(child.rule)66 elsif child.respond_to?(:scenario) && child.scenario67 process_scenario(child)68 end69 end70 end71 def process_scenario(child)72 if child.scenario.examples.empty?73 @lookup_hash[child.scenario.location.line] = ScenarioSource.new(:Scenario, child.scenario)74 else75 child.scenario.examples.each do |examples|76 examples.table_body.each do |row|77 @lookup_hash[row.location.line] = ScenarioOutlineSource.new(:ScenarioOutline, child.scenario, examples, row)78 end79 end80 end81 end82 end83 class TestStepLookupBuilder84 attr_reader :lookup_hash85 def initialize(gherkin_document)86 @lookup_hash = {}87 process_scenario_container(gherkin_document.feature)88 end89 private90 def process_scenario_container(container)91 container.children.each do |child|92 if child.respond_to?(:rule) && child.rule93 process_scenario_container(child.rule)94 elsif child.respond_to?(:scenario) && child.scenario95 child.scenario.steps.each do |step|96 @lookup_hash[step.location.line] = StepSource.new(:Step, step)97 end98 elsif !child.background.nil?99 child.background.steps.each do |step|100 @lookup_hash[step.location.line] = StepSource.new(:Step, step)101 end102 end103 end104 end105 end106 KeywordSearchNode = Struct.new(:keyword, :previous_node)107 class KeywordLookupBuilder108 attr_reader :lookup_hash109 def initialize(gherkin_document)110 @lookup_hash = {}111 process_scenario_container(gherkin_document.feature, nil)112 end113 private114 def process_scenario_container(container, original_previous_node)115 container.children.each do |child|116 previous_node = original_previous_node117 if child.respond_to?(:rule) && child.rule118 process_scenario_container(child.rule, original_previous_node)119 elsif child.respond_to?(:scenario) && child.scenario120 child.scenario.steps.each do |step|121 node = KeywordSearchNode.new(step.keyword, previous_node)122 @lookup_hash[step.location.line] = node123 previous_node = node124 end125 elsif child.respond_to?(:background) && child.background126 child.background.steps.each do |step|127 node = KeywordSearchNode.new(step.keyword, previous_node)128 @lookup_hash[step.location.line] = node129 previous_node = node130 original_previous_node = previous_node131 end132 end133 end134 end135 end136 end137 end138end...
query_spec.rb
Source:query_spec.rb
...55 end.not_to raise_exception56 end57 end58 end59 describe '#location' do60 before do61 messages.each { |message| subject.update(message) }62 end63 let(:background) { find_message_by_attribute(gherkin_document.feature.children, :background) }64 let(:rule) { find_message_by_attribute(gherkin_document.feature.children, :rule) }65 let(:scenarios) { filter_messages_by_attribute(gherkin_document.feature.children, :scenario) }66 let(:scenario) { scenarios.first }67 it 'raises an exception when the AST node ID is unknown' do68 expect { subject.location("this-id-may-not-exist-for-real") }.to raise_exception(Gherkin::AstNodeNotLocatedException)69 end70 it 'provides the location of a scenario' do71 expect(subject.location(scenario.id)).to eq(scenario.location)72 end73 it 'provides the location of an examples table row' do74 node = scenarios.last.examples.first.table_body.first75 expect(subject.location(node.id)).to eq(node.location)76 end77 context 'when querying steps' do78 let(:background_step) { background.steps.first }79 let(:scenario_step) { scenario.steps.first }80 it 'provides the location of a background step' do81 expect(subject.location(background_step.id)).to eq(background_step.location)82 end83 it 'provides the location of a scenario step' do84 expect(subject.location(scenario_step.id)).to eq(scenario_step.location)85 end86 end87 context 'when querying tags' do88 let(:feature_tag) { gherkin_document.feature.tags.first }89 let(:rule_tag) { rule.tags.first }90 let(:scenario_tag) { scenario.tags.first }91 let(:examples_tag) { scenarios.last.examples.first.tags.first }92 it 'provides the location of a feature tags' do93 expect(subject.location(feature_tag.id)).to eq(feature_tag.location)94 end95 it 'provides the location of a scenario tags' do96 expect(subject.location(scenario_tag.id)).to eq(scenario_tag.location)97 end98 it 'provides the location of scenario examples tags' do99 expect(subject.location(examples_tag.id)).to eq(examples_tag.location)100 end101 it 'provides the location of a rule tag' do102 expect(subject.location(rule_tag.id)).to eq(rule_tag.location)103 end104 end105 context 'when children are scoped in a Rule' do106 let(:rule_background) { find_message_by_attribute(rule.children, :background) }107 let(:rule_background_step) { rule_background.steps.first }108 let(:rule_scenario) { find_message_by_attribute(rule.children, :scenario) }109 let(:rule_scenario_step) { rule_scenario.steps.first }110 let(:rule_scenario_tag) { rule_scenario.tags.first }111 it 'provides the location of a background step' do112 expect(subject.location(rule_background_step.id)).to eq(rule_background_step.location)113 end114 it 'provides the location of a scenario' do115 expect(subject.location(rule_scenario.id)).to eq(rule_scenario.location)116 end117 it 'provides the location of a scenario tag' do118 expect(subject.location(rule_scenario_tag.id)).to eq(rule_scenario_tag.location)119 end120 it 'provides the location of a scenario step' do121 expect(subject.location(rule_scenario_step.id)).to eq(rule_scenario_step.location)122 end123 end124 end125end...
parser_message_stream.rb
Source:parser_message_stream.rb
...46 errors.each do |err|47 parse_error = Cucumber::Messages::ParseError.new(48 source: Cucumber::Messages::SourceReference.new(49 uri: uri,50 location: Cucumber::Messages::Location.new(51 line: err.location[:line],52 column: err.location[:column]53 )54 ),55 message: err.message56 )57 y.yield(Cucumber::Messages::Envelope.new(parse_error: parse_error))58 end59 end60 def sources61 Enumerator.new do |y|62 @paths.each do |path|63 source = Cucumber::Messages::Source.new(64 uri: path,65 data: File.open(path, 'r:UTF-8', &:read),66 media_type: 'text/x.cucumber.gherkin+plain'...
gherkin_events.rb
Source:gherkin_events.rb
...49 type: 'attachment',50 source: {51 uri: uri,52 start: {53 line: error.location[:line],54 column: error.location[:column]55 }56 },57 data: error.message,58 media: {59 encoding: 'utf-8',60 type: 'text/x.cucumber.stacktrace+plain'61 }62 })63 end64 end65 end66 end67end...
token_scanner.rb
Source:token_scanner.rb
...21 fail ArgumentError, "Please a pass String, StringIO or IO. I got a #{source_or_io.class}"22 end23 end24 def read25 location = {line: @line_number += 1, column: 0}26 if @io.nil? || line = @io.gets27 gherkin_line = line ? GherkinLine.new(line, location[:line]) : nil28 Token.new(gherkin_line, location)29 else30 @io.close unless @io.closed? # ARGF closes the last file after final gets31 @io = nil32 Token.new(nil, location)33 end34 end35 end36end...
location
Using AI Code Generation
1parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new)2feature = parser.parse(File.read('1.feature'), '1.feature')3parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new)4feature = parser.parse(File.read('2.feature'), '2.feature')5parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new)6feature = parser.parse(File.read('3.feature'), '3.feature')
location
Using AI Code Generation
1l = g.location(1, 2)2parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::PrettyFormatter.new(STDOUT))3parser.parse(File.read("my_gherkin.feature"), "my_gherkin.feature", 0)
location
Using AI Code Generation
1parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new)2feature = parser.parse(File.read('1.feature'), '1.feature')3parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new)4feature = parser.parse(File.read('2.feature'), '2.feature')5parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new)6feature = parser.parse(File.read('3.feature'), '3.feature')
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!!