Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods for Step Definitions #168

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions lib/cucumber/rb_support/rb_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def register_rb_transform(regexp, proc)
@rb_language.register_rb_transform(regexp, proc)
end

def register_rb_step_definition(regexp, proc)
@rb_language.register_rb_step_definition(regexp, proc)
def register_rb_step_definition(regexp, proc_or_sym, options = {})
@rb_language.register_rb_step_definition(regexp, proc_or_sym, options)
end
end

Expand Down Expand Up @@ -98,12 +98,19 @@ def AfterConfiguration(&proc)
# also to the i18n translations whenever a feature of a
# new language is loaded.
#
# The +&proc+ gets executed in the context of a <tt>World</tt>
# object, which is defined by #World. A new <tt>World</tt>
# object is created for each scenario and is shared across
# step definitions within that scenario.
def register_rb_step_definition(regexp, &proc)
RbDsl.register_rb_step_definition(regexp, proc)
# If provided, the +symbol+ is sent to the <tt>World</tt> object
# as defined by #World. A new <tt>World</tt> object is created
# for each scenario and is shared across step definitions within
# that scenario. If the +options+ hash contains an <tt>:on</tt>
# key, the value for this is assumed to be a proc. This proc
# will be executed in the context of the <tt>World</tt> object
# and then sent the +symbol+.
#
# If no +symbol+ if provided then the +&proc+ gets executed in
# the context of the <tt>World</tt> object.
def register_rb_step_definition(regexp, symbol = nil, options = {}, &proc)
proc_or_sym = symbol || proc
RbDsl.register_rb_step_definition(regexp, proc_or_sym, options)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cucumber/rb_support/rb_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def register_rb_transform(regexp, proc)
add_transform(RbTransform.new(self, regexp, proc))
end

def register_rb_step_definition(regexp, proc)
step_definition = RbStepDefinition.new(self, regexp, proc)
def register_rb_step_definition(regexp, proc_or_sym, options)
step_definition = RbStepDefinition.new(self, regexp, proc_or_sym, options)
@step_definitions << step_definition
step_definition
end
Expand Down
22 changes: 17 additions & 5 deletions lib/cucumber/rb_support/rb_step_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@ class RbStepDefinition

class MissingProc < StandardError
def message
"Step definitions must always have a proc"
"Step definitions must always have a proc or symbol"
end
end

def initialize(rb_language, regexp, proc)
raise MissingProc if proc.nil?
def initialize(rb_language, regexp, proc_or_sym, options)
raise MissingProc if proc_or_sym.nil?
if String === regexp
p = Regexp.escape(regexp)
p = p.gsub(/\\\$\w+/, '(.*)') # Replace $var with (.*)
regexp = Regexp.new("^#{p}$")
end
@rb_language, @regexp, @proc = rb_language, regexp, proc
@rb_language, @regexp, @proc = rb_language, regexp, proc_or_sym
if @proc.kind_of? Symbol
@proc = lambda do |*args|
target = options[:on] ? instance_exec(&options[:on]) : self
target.send(proc_or_sym, *args)
end
end

@rb_language.available_step_definition(regexp_source, file_colon_line)
end

Expand Down Expand Up @@ -71,7 +78,12 @@ def backtrace_line
end

def file_colon_line
@proc.file_colon_line
case @proc
when Proc
@proc.file_colon_line
when Symbol
":#{@proc}"
end
end

def file
Expand Down
18 changes: 17 additions & 1 deletion spec/cucumber/rb_support/rb_step_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ module RbSupport
$inside.should == 'inside'
end

it "should call a method on the world when specified with a symbol" do
rb.current_world.should_receive(:with_symbol)
dsl.Given /With symbol/, :with_symbol

support_code.step_match("With symbol").invoke(nil)
end

it "should call a method on a specified object" do
target = double('target')
target.should_receive(:with_symbol)
rb.current_world.stub!(:target).and_return(target)
dsl.Given /With symbol on block/, :with_symbol, :on => lambda { target }

support_code.step_match("With symbol on block").invoke(nil)
end

it "should raise Undefined when inside step is not defined" do
dsl.Given /Outside/ do
step 'Inside'
Expand Down Expand Up @@ -91,7 +107,7 @@ module RbSupport
end

it "should have a JSON representation of the signature" do
RbStepDefinition.new(rb, /I CAN HAZ (\d+) CUKES/i, lambda{}).to_hash.should == {'source' => "I CAN HAZ (\\d+) CUKES", 'flags' => 'i'}
RbStepDefinition.new(rb, /I CAN HAZ (\d+) CUKES/i, lambda{}, {}).to_hash.should == {'source' => "I CAN HAZ (\\d+) CUKES", 'flags' => 'i'}
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/cucumber/step_match_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Cucumber
end

def stepdef(regexp)
RbSupport::RbStepDefinition.new(@rb_language, regexp, lambda{})
RbSupport::RbStepDefinition.new(@rb_language, regexp, lambda{}, {})
end

def step_match(regexp, name)
Expand Down Expand Up @@ -66,4 +66,4 @@ def step_match(regexp, name)
m.format_args("<span>%s</span>").should == "running<span> 5 times</span> <span>10</span> meters"
end
end
end
end