Skip to content

Commit

Permalink
Looseing the ambigious match / guess logic from SupportCode
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwynne committed Oct 3, 2015
1 parent e8bae25 commit 54e93e3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 39 deletions.
4 changes: 1 addition & 3 deletions lib/cucumber/formatter/legacy_api/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 14 additions & 16 deletions lib/cucumber/runtime/support_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/cucumber/rb_support/rb_step_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 10 additions & 19 deletions spec/cucumber/runtime/support_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -85,36 +76,36 @@ 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

it "picks right step definition when an equal number of capture groups" do
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
general = dsl.Given(/Three (.*) mice ran (.*)/) {|disability|}
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

Expand Down
6 changes: 6 additions & 0 deletions spec/cucumber/step_match_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 54e93e3

Please sign in to comment.