Skip to content

Commit

Permalink
Merge pull request #760 from cucumber/deep_clone_step_args
Browse files Browse the repository at this point in the history
Each time a step_match is invoked it uses the same object as the step argument
  • Loading branch information
mattwynne committed Dec 22, 2014
2 parents 91657b1 + 1b9ab0f commit cf0c23f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/cucumber/step_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ def initialize(step_definition, name_to_match, name_to_report, step_arguments)
end

def args
@step_arguments.map{|g| g.val.freeze }
@step_arguments.map{|g| g.val }
end

def name
@name_to_report
end

def invoke(multiline_arg)
all_args = args.dup
all_args = deep_clone_args
multiline_arg.append_to(all_args)
@step_definition.invoke(all_args)
end
Expand Down Expand Up @@ -80,6 +80,11 @@ def replace_arguments(string, step_arguments, format, &proc)
def inspect #:nodoc:
sprintf("#<%s:0x%x>", self.class, self.object_id)
end

private
def deep_clone_args
Marshal.load( Marshal.dump( args ) )
end
end

class NoStepMatch #:nodoc:
Expand Down
15 changes: 11 additions & 4 deletions spec/cucumber/rb_support/rb_step_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ module RbSupport
end

def run_step(text)
support_code.step_match(text).invoke(MultilineArgument::None.new)
step_match(text).invoke(MultilineArgument::None.new)
end

def step_match(text)
support_code.step_match(text)
end

it "allows calling of other steps" do
Expand Down Expand Up @@ -111,14 +115,17 @@ def run_step(text)
}).to raise_error(Cucumber::ArityMismatchError)
end

it "does not allow modification of args since it messes up pretty formatting" do
it "does not modify the step_match arg when arg is modified in a step" do
dsl.Given(/My car is (.*)/) do |colour|
colour << "xxx"
end

step_name = "My car is white"
step_args = step_match(step_name).args

expect(-> {
run_step "My car is white"
}).to raise_error(RuntimeError, /can't modify frozen String/i)
run_step step_name
}).not_to change{ step_args.first }
end

it "allows puts" do
Expand Down

0 comments on commit cf0c23f

Please sign in to comment.