Skip to content

Commit

Permalink
Restore support for matching a scenario by multiline step argument li…
Browse files Browse the repository at this point in the history
…ne numbers (#239)

* match tables and docstrings too.

* refactor.

* rename #select_down to #merge, and remove unnecessary conditional: multiline_arg is always truthy.
  • Loading branch information
botandrose-machine authored Oct 25, 2022
1 parent b9be5fc commit d58eb01
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/cucumber/core/test/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def matching_locations
[
location,
tags.map(&:location),
test_steps.map(&:location),
test_steps.map(&:matching_locations)
].flatten
end

Expand Down
4 changes: 4 additions & 0 deletions lib/cucumber/core/test/data_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def map(&block)
self.class.new(new_raw)
end

def lines_count
raw.count
end

def ==(other)
other.class == self.class && raw == other.raw
end
Expand Down
5 changes: 4 additions & 1 deletion lib/cucumber/core/test/doc_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def to_step_definition_arg
self
end

def lines_count
lines.count + 2
end

def ==(other)
if other.respond_to?(:content_type)
return false unless content_type == other.content_type
Expand All @@ -68,7 +72,6 @@ def inspect
%{ """>}
].join("\n")
end

end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cucumber/core/test/empty_multiline_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def map(&block)
self
end

def all_locations
[]
def lines_count
0
end

def inspect
Expand Down
7 changes: 7 additions & 0 deletions lib/cucumber/core/test/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def to_str
to_s
end

def merge(multiline_arg)
new_lines = (0..multiline_arg.lines_count).map do |offset|
lines.min + offset
end
Location.new(file, new_lines)
end

def on_line(new_line)
Location.new(file, new_line)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/cucumber/core/test/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def action_location
@action.location
end

def matching_locations
[location.merge(multiline_arg)]
end

def inspect
"#<#{self.class}: #{location}>"
end
Expand Down
54 changes: 54 additions & 0 deletions spec/cucumber/core/test/filters/locations_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,60 @@ def test_case_named(name)
expect(receiver.test_case_locations).to eq []
end

context "with a docstring" do
let(:test_case) do
test_cases.find { |c| c.name == 'with docstring' }
end

it "matches a location at the start the docstring" do
location = Test::Location.new(file, 17)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('with docstring').location]
end

it "matches a location in the middle of the docstring" do
location = Test::Location.new(file, 18)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('with docstring').location]
end

it "matches a location at the end of the docstring" do
location = Test::Location.new(file, 19)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('with docstring').location]
end
end

context "with a table" do
let(:test_case) do
test_cases.find { |c| c.name == 'with a table' }
end

it "matches a location at the start of the table" do
location = Test::Location.new(file, 23)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('with a table').location]
end

it "matches a location at the middle of the table" do
location = Test::Location.new(file, 24)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('with a table').location]
end

it "matches a location at the end of the table" do
location = Test::Location.new(file, 25)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('with a table').location]
end
end

context "with duplicate locations in the filter" do
it "matches each test case only once" do
location_tc_two = test_case_named('two').location
Expand Down

0 comments on commit d58eb01

Please sign in to comment.