Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
313 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# frozen_string_literal: true | ||
|
||
# Note: this may move to cucumber-query later on | ||
|
||
module Cucumber | ||
module Core | ||
module Gherkin | ||
class LocationQuery | ||
def process(message) | ||
if message.gherkinDocument | ||
process_gherkin_document(message.gherkinDocument) | ||
elsif message.pickle | ||
process_pickle(message.pickle) | ||
end | ||
end | ||
|
||
def pickles | ||
@pickles ||= [] | ||
end | ||
|
||
def pickle_locations(pickle) | ||
[ | ||
scenario_by_id[pickle.sourceIds[0]], | ||
table_row_by_id[pickle.sourceIds[1]] | ||
].compact.map(&:location) | ||
end | ||
|
||
def pickle_step_locations(pickle_step) | ||
[ | ||
scenario_step_by_id[pickle_step.sourceIds[0]], | ||
table_row_by_id[pickle_step.sourceIds[1]] | ||
].compact.map(&:location) | ||
end | ||
|
||
def pickle_step_argument_location(pickle_step) | ||
scenario_step = scenario_step_by_id[pickle_step.sourceIds[0]] | ||
if scenario_step | ||
case scenario_step.argument | ||
when :doc_string | ||
return scenario_step.doc_string.location | ||
when :data_table | ||
return scenario_step.data_table.location | ||
end | ||
end | ||
end | ||
|
||
def pickle_tag_location(pickle_tag) | ||
tag_by_id[pickle_tag.sourceId].location | ||
end | ||
|
||
private | ||
|
||
def process_gherkin_document(document) | ||
if document.feature | ||
process_children(document.feature.children) | ||
process_tags(document.feature.tags) | ||
end | ||
end | ||
|
||
def process_children(children) | ||
children.each do |child| | ||
process_scenario(child.scenario) if child.scenario | ||
process_children(child.rule.children) if child.respond_to?(:rule) && child.rule | ||
end | ||
end | ||
|
||
def process_scenario(scenario) | ||
scenario_by_id[scenario.id] = scenario | ||
process_tags(scenario.tags) | ||
scenario.steps.each do |step| | ||
scenario_step_by_id[step.id] = step | ||
end | ||
process_examples(scenario.examples) if scenario.examples | ||
end | ||
|
||
def process_examples(examples) | ||
examples.each do |example| | ||
process_tags(example.tags) | ||
example.table_body.each do |table_row| | ||
table_row_by_id[table_row.id] = table_row | ||
end | ||
end | ||
end | ||
|
||
def process_tags(tags) | ||
tags.each do |tag| | ||
tag_by_id[tag.id] = tag | ||
end | ||
end | ||
|
||
def process_pickle(pickle) | ||
pickles << pickle | ||
end | ||
|
||
def scenario_by_id | ||
@scenario_by_id ||= {} | ||
end | ||
|
||
def scenario_step_by_id | ||
@scenario_step_by_id ||= {} | ||
end | ||
|
||
def table_row_by_id | ||
@table_row_by_id ||= {} | ||
end | ||
|
||
def tag_by_id | ||
@tag_by_id ||= {} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
# -*- encoding: utf-8 -*- | ||
# frozen_string_literal: true | ||
require 'cucumber/core/gherkin/location_query' | ||
require 'gherkin' | ||
|
||
module Cucumber | ||
module Core | ||
module Gherkin | ||
describe LocationQuery do | ||
let(:subject) { | ||
lq = LocationQuery.new | ||
messages.each do |message| | ||
lq.process(message) | ||
end | ||
lq | ||
} | ||
|
||
let(:content) { | ||
<<-FEATURE | ||
Feature: my simple feature | ||
Scenario: A scenario | ||
Given a passed step | ||
FEATURE | ||
} | ||
|
||
let(:messages) { ::Gherkin.from_source( | ||
'some.feature', | ||
content, { | ||
include_gherkin_document: true, | ||
include_pickles: true | ||
} | ||
)} | ||
|
||
let(:pickle) { | ||
subject.pickles.first | ||
} | ||
|
||
describe 'pickle_locations' do | ||
it 'returns the Location of source scenario' do | ||
location = subject.pickle_locations(pickle).first | ||
|
||
expect(location.line).to eq(3) | ||
end | ||
|
||
context 'when generated from a Examples table' do | ||
let(:content) { | ||
<<-FEATURE | ||
Feature: my simple feature | ||
Scenario: A scenario | ||
Given a <status> step | ||
Examples: | ||
| status | | ||
| passed | | ||
| failed | | ||
FEATURE | ||
} | ||
|
||
it 'returns the Locations of the source scenario and example used' do | ||
locations = subject.pickle_locations(pickle) | ||
|
||
expect(locations[0].line).to eq(3) | ||
expect(locations[1].line).to eq(8) | ||
end | ||
end | ||
end | ||
|
||
describe 'pickle_step_locations' do | ||
let(:pickle_step) { pickle.steps.first } | ||
|
||
it 'returns the Location of the scenario step' do | ||
expect(subject.pickle_step_locations(pickle_step).first.line).to eq(4) | ||
end | ||
|
||
context 'when generated from a Examples table' do | ||
let(:content) { | ||
<<-FEATURE | ||
Feature: my simple feature | ||
Scenario: A scenario | ||
Given a <status> step | ||
Examples: | ||
| status | | ||
| passed | | ||
| failed | | ||
FEATURE | ||
} | ||
|
||
it 'returns the Locations of the scenario step and example used' do | ||
locations = subject.pickle_step_locations(pickle_step) | ||
|
||
expect(locations[0].line).to eq(4) | ||
expect(locations[1].line).to eq(8) | ||
end | ||
end | ||
end | ||
|
||
describe 'pickle_step_argument_location' do | ||
let(:pickle_step) { pickle.steps.first } | ||
|
||
context 'when there is a docstring argument' do | ||
let(:content) { | ||
<<-FEATURE | ||
Feature: my simple feature | ||
Scenario: A scenario | ||
Given a passed step | ||
""" | ||
This is my DocString | ||
""" | ||
FEATURE | ||
} | ||
|
||
it 'returns the location of the doc string' do | ||
expect(subject.pickle_step_argument_location(pickle_step).line).to eq(5) | ||
end | ||
end | ||
|
||
context 'when there is a datatable argument' do | ||
let(:content) { | ||
<<-FEATURE | ||
Feature: my simple feature | ||
Scenario: A scenario | ||
Given a passed step | ||
| name | value | | ||
| plic | ploc | | ||
FEATURE | ||
} | ||
|
||
it 'returns the location of the datatable' do | ||
expect(subject.pickle_step_argument_location(pickle_step).line).to eq(5) | ||
end | ||
end | ||
|
||
context 'when there is no argument' do | ||
it 'returns nil' do | ||
expect(subject.pickle_step_argument_location(pickle_step)).to be_nil | ||
end | ||
end | ||
end | ||
|
||
describe 'pickle_tag_location' do | ||
let(:content) { | ||
<<-FEATURE | ||
Feature: With tags | ||
@acceptance | ||
Scenario: With a single tag | ||
Given a passed step | ||
FEATURE | ||
} | ||
let(:pickle_tag) { pickle.tags.first } | ||
|
||
it 'returns the Location of the tag' do | ||
expect(subject.pickle_tag_location(pickle_tag).line).to eq(3) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters