Skip to content

Commit

Permalink
Restore support for matching a scenario by its tags' and steps' line …
Browse files Browse the repository at this point in the history
…numbers. (#238)

* Restore support for matching a scenario by its steps' line numbers.

Ref cucumber/cucumber-ruby#1469

* Restore support for matching a scenario by its tags' line numbers.

* clean up some details per PR review feedback.

* add tests to cover background step location matching.
  • Loading branch information
botandrose-machine authored Aug 23, 2022
1 parent 9b3c892 commit b9be5fc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO

### Fixed

* Restore support for matching a scenario by tag and step line numbers.

### Dependencies

## [11.0.0](https://github.com/cucumber/cucumber-ruby-core/compare/v10.1.1...v11.0.0)
Expand Down Expand Up @@ -362,4 +364,4 @@ Changes were not logged.
Changes were not logged.


[Unreleased]: https://github.com/cucumber/cucumber-ruby-core/compare/v10.1.1...main
[Unreleased]: https://github.com/cucumber/cucumber-ruby-core/compare/v10.1.1...main
12 changes: 11 additions & 1 deletion lib/cucumber/core/test/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,20 @@ def match_name?(name_regexp)

def match_locations?(queried_locations)
queried_locations.any? do |queried_location|
queried_location.match? location
matching_locations.any? do |location|
queried_location.match? location
end
end
end

def matching_locations
[
location,
tags.map(&:location),
test_steps.map(&:location),
].flatten
end

def inspect
"#<#{self.class}: #{location}>"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/core/test/filters/locations_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def done
def sorted_test_cases
filter_locations.map { |filter_location|
test_cases[filter_location.file].select { |test_case|
filter_location.match?(test_case.location)
test_case.match_locations?([filter_location])
}
}.flatten.uniq
end
Expand Down
63 changes: 48 additions & 15 deletions spec/cucumber/core/test/filters/locations_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ module Cucumber::Core
let(:doc) do
Gherkin::Document.new(file, <<-END)
Feature:
Background:
Given background
Scenario: one
Given one a
Expand Down Expand Up @@ -105,6 +107,13 @@ def test_case_named(name)
test_cases.find { |c| c.name == name }
end

it 'matches the location on a background step to all scenarios' do
location = Test::Location.new(file, 3)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq test_cases.map(&:location)
end

it 'matches the precise location of the scenario' do
location = test_case_named('two').location
filter = Test::LocationsFilter.new([location])
Expand All @@ -113,22 +122,36 @@ def test_case_named(name)
end

it 'matches multiple locations' do
good_location = Test::Location.new(file, 8)
bad_location = Test::Location.new(file, 5)
good_location = Test::Location.new(file, 10)
bad_location = Test::Location.new(file, 7)
filter = Test::LocationsFilter.new([good_location, bad_location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('two').location]
end

it "doesn't match a location after the scenario line" do
it 'matches a location on the first step of the scenario' do
location = Test::Location.new(file, 11)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('two').location]
end

it 'matches a location on the last step of the scenario' do
location = Test::Location.new(file, 12)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('two').location]
end

it "matches a location on the scenario's tags" do
location = Test::Location.new(file, 9)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq []
expect(receiver.test_case_locations).to eq [test_case_named('two').location]
end

it "doesn't match a location before the scenario line" do
location = Test::Location.new(file, 7)
it "does not return a matched location on a whitespace line" do
location = Test::Location.new(file, 13)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq []
Expand All @@ -138,7 +161,7 @@ def test_case_named(name)
it "matches each test case only once" do
location_tc_two = test_case_named('two').location
location_tc_one = test_case_named('one').location
location_last_step_tc_two = Test::Location.new(file, 10)
location_last_step_tc_two = Test::Location.new(file, 12)
filter = Test::LocationsFilter.new([location_tc_two, location_tc_one, location_last_step_tc_two])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case_named('two').location, location_tc_one = test_case_named('one').location]
Expand Down Expand Up @@ -184,19 +207,29 @@ def test_case_named(name)
end

it "matches row location to the test case of the row" do
locations = [
Test::Location.new(file, 19),
]
filter = Test::LocationsFilter.new(locations)
location = Test::Location.new(file, 19)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations).to eq [test_case.location]
end

it "matches outline location with the all test cases of all the tables" do
locations = [
Test::Location.new(file, 8),
]
filter = Test::LocationsFilter.new(locations)
location = Test::Location.new(file, 8)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations.map(&:line)).to eq [19, 23, 24]
end

it "matches a location on a step of the scenario outline with all test cases of all the tables" do
location = Test::Location.new(file, 10)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations.map(&:line)).to eq [19, 23, 24]
end

it "matches a location on the scenario outline's tags with all test cases of all the tables" do
location = Test::Location.new(file, 7)
filter = Test::LocationsFilter.new([location])
compile [doc], receiver, [filter]
expect(receiver.test_case_locations.map(&:line)).to eq [19, 23, 24]
end
Expand Down

0 comments on commit b9be5fc

Please sign in to comment.