diff --git a/lib/cucumber/formatter/legacy_api/adapter.rb b/lib/cucumber/formatter/legacy_api/adapter.rb index fc80469ca3..0cd6bc4beb 100644 --- a/lib/cucumber/formatter/legacy_api/adapter.rb +++ b/lib/cucumber/formatter/legacy_api/adapter.rb @@ -188,9 +188,7 @@ def build_step_invocation(indent, support_code, config, messages, embeddings) private def step_match(support_code) - support_code.step_match(step.name) - rescue Cucumber::Undefined - NoStepMatch.new(step, step.name) + support_code.find_match(step) || NoStepMatch.new(step, step.name) end end diff --git a/lib/cucumber/runtime/support_code.rb b/lib/cucumber/runtime/support_code.rb index 19180b6f69..ba5daa8a49 100644 --- a/lib/cucumber/runtime/support_code.rb +++ b/lib/cucumber/runtime/support_code.rb @@ -73,7 +73,7 @@ def invoke_dynamic_steps(steps_text, i18n, location) # These are commonly called nested steps. def invoke_dynamic_step(step_name, multiline_argument, location=nil) begin - step_match(step_name).invoke(multiline_argument) + step_matches(step_name).first.invoke(multiline_argument) rescue Undefined => exception raise UndefinedDynamicStep, step_name end @@ -110,16 +110,6 @@ def step_definitions @ruby.step_definitions end - def find_match(test_step) - begin - match = step_match(test_step.name) - rescue Cucumber::Undefined - return nil - end - - match - end - def find_after_step_hooks(test_case) scenario = RunningTestCase.new(test_case) hooks = @ruby.hooks_for(:after_step, scenario) @@ -148,12 +138,20 @@ def find_around_hooks(test_case) end end - def step_match(step_name) + def find_match(test_step) + begin + step_matches(test_step.name).first + rescue Cucumber::Undefined + return nil + end + end + + def step_matches(step_name) matches = matches(step_name) raise Undefined.new(step_name) if matches.empty? matches = best_matches(step_name, matches) if matches.size > 1 && guess_step_matches? raise Ambiguous.new(step_name, matches, guess_step_matches?) if matches.size > 1 - matches[0] + matches end private @@ -193,11 +191,11 @@ def log require 'delegate' class CachesStepMatch < SimpleDelegator - def step_match(step_name) #:nodoc: + def step_matches(step_name) #:nodoc: @match_cache ||= {} - match = @match_cache[step_name] - return match if match + matches = @match_cache[step_name] + return matches if matches @match_cache[step_name] = super(step_name) end diff --git a/spec/cucumber/rb_support/rb_step_definition_spec.rb b/spec/cucumber/rb_support/rb_step_definition_spec.rb index 31cf1a21a4..23c43498eb 100644 --- a/spec/cucumber/rb_support/rb_step_definition_spec.rb +++ b/spec/cucumber/rb_support/rb_step_definition_spec.rb @@ -23,7 +23,7 @@ def run_step(text) end def step_match(text) - support_code.step_match(text) + support_code.step_matches(text).first end it "allows calling of other steps" do diff --git a/spec/cucumber/runtime/support_code_spec.rb b/spec/cucumber/runtime/support_code_spec.rb index 4a4f24729e..c8377aaa0b 100644 --- a/spec/cucumber/runtime/support_code_spec.rb +++ b/spec/cucumber/runtime/support_code_spec.rb @@ -11,25 +11,16 @@ module Cucumber Object.new.extend(RbSupport::RbDsl) end - it "formats step names" do - dsl.Given(/it (.*) in (.*)/) { |what, month| } - dsl.Given(/nope something else/) { |what, month| } - - format = subject.step_match("it snows in april").format_args("[%s]") - - expect(format).to eq "it [snows] in [april]" - end - context 'caching' do let(:cached) { Runtime::SupportCode::CachesStepMatch.new(subject) } it "caches step match results" do dsl.Given(/it (.*) in (.*)/) { |what, month| } - step_match = cached.step_match("it snows in april") + step_match = cached.step_matches("it snows in april").first expect(@rb).not_to receive(:step_matches) - second_step_match = cached.step_match("it snows in april") + second_step_match = cached.step_matches("it snows in april").first expect(step_match).to equal(second_step_match) end @@ -49,7 +40,7 @@ module Cucumber dsl.Given(/Three blind (.*)/) {|animal|} expect(-> { - subject.step_match("Three blind mice") + subject.step_matches("Three blind mice").first }).to raise_error(Ambiguous, /#{expected_error}/) end @@ -67,7 +58,7 @@ module Cucumber dsl.Given(/Three cute (.*)/) {|animal|} expect(-> { - subject.step_match("Three cute mice") + subject.step_matches("Three cute mice").first }).to raise_error(Ambiguous, /#{expected_error}/) end @@ -76,7 +67,7 @@ module Cucumber dsl.Given(/Three (.*)/) {|animal|} expect(-> { - subject.step_match("Three blind mice") + subject.step_matches("Three blind mice").first }).not_to raise_error end @@ -85,7 +76,7 @@ module Cucumber dsl.Given(/Three (.*)?/) {|animal|} expect(-> { - subject.step_match("Three blind mice") + subject.step_matches("Three blind mice").first }).not_to raise_error end @@ -93,14 +84,14 @@ module Cucumber right = dsl.Given(/Three (.*) mice/) {|disability|} wrong = dsl.Given(/Three (.*)/) {|animal|} - expect(subject.step_match("Three blind mice").step_definition).to eq right + expect(subject.step_matches("Three blind mice").first.step_definition).to eq right end it "picks right step definition when an unequal number of capture groups" do right = dsl.Given(/Three (.*) mice ran (.*)/) {|disability|} wrong = dsl.Given(/Three (.*)/) {|animal|} - expect(subject.step_match("Three blind mice ran far").step_definition).to eq right + expect(subject.step_matches("Three blind mice ran far").first.step_definition).to eq right end it "picks most specific step definition when an unequal number of capture groups" do @@ -108,13 +99,13 @@ module Cucumber specific = dsl.Given(/Three blind mice ran far/) do; end more_specific = dsl.Given(/^Three blind mice ran far$/) do; end - expect(subject.step_match("Three blind mice ran far").step_definition).to eq more_specific + expect(subject.step_matches("Three blind mice ran far").first.step_definition).to eq more_specific end end it "raises Undefined error when no step definitions match" do expect(-> { - subject.step_match("Three blind mice") + subject.step_matches("Three blind mice").first }).to raise_error(Undefined) end diff --git a/spec/cucumber/step_match_spec.rb b/spec/cucumber/step_match_spec.rb index 33ad53ccd8..3bde4b9a07 100644 --- a/spec/cucumber/step_match_spec.rb +++ b/spec/cucumber/step_match_spec.rb @@ -20,6 +20,12 @@ def step_match(regexp, name) StepMatch.new(stepdef, name, stepdef.arguments_from(name)) end + it "formats step names" do + m = step_match(/it (.*) in (.*)/, "it snows in april") + format = m.format_args("[%s]") + expect(format).to eq "it [snows] in [april]" + end + it "formats one group when we use Unicode" do m = step_match(/I (#{WORD}+) ok/, "I æøåÆØÅæøåÆØÅæøåÆØÅæøåÆØÅ ok")