Skip to content

Commit

Permalink
Added step argument transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
larrytheliquid committed Sep 11, 2009
1 parent 2a0d6be commit 873b512
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
9 changes: 9 additions & 0 deletions examples/self_test/features/step_definitions/sample_steps.rb
Expand Up @@ -28,6 +28,7 @@ def flunker
raise "We already have #{@cukes} cukes!" if @cukes
@cukes = cukes
end

Then /^I should have '(.+)' cukes$/ do |cukes|
@cukes.should == cukes
end
Expand Down Expand Up @@ -79,3 +80,11 @@ def flunker
FileUtils.mkdir_p(File.dirname(after_file))
FileUtils.touch(after_file)
end

Then /^I should not transform ('\d+') to an Integer$/ do |string|
string.should be_kind_of(String)
end

Then /^I should transform ('\d+' to an Integer)$/ do |integer|
integer.should be_kind_of(Integer)
end
4 changes: 4 additions & 0 deletions examples/self_test/features/support/env.rb
Expand Up @@ -16,3 +16,7 @@
After('@background_tagged_before_on_outline') do
@cukes.should == '888'
end

Cucumber::StepMother.register_transform /'\d+' to an Integer/ do |group|
/'(\d+)' to an Integer/.match(group)[0].to_i
end
7 changes: 7 additions & 0 deletions examples/self_test/features/transform_sample.feature
@@ -0,0 +1,7 @@
Feature: Step argument transformations

Scenario: transform with matches
Then I should transform '10' to an Integer

Scenario: transform without matches
Then I should not transform '10' to an Integer
2 changes: 1 addition & 1 deletion lib/cucumber/core_ext/instance_exec.rb
Expand Up @@ -21,7 +21,7 @@ def cucumber_instance_exec(check_arity, pseudo_method, *args, &block)
)
end
else
instance_exec(*args, &block)
instance_exec(*Cucumber::StepMother.transform_arguments(args), &block)
end
end
end
Expand Down
19 changes: 17 additions & 2 deletions lib/cucumber/step_mother.rb
Expand Up @@ -50,8 +50,8 @@ def initialize(step_def_1, step_def_2)

# This is the meaty part of Cucumber that ties everything together.
class StepMother
include Constantize

include Constantize
@@transforms = {}
attr_writer :options, :visitor, :log

def initialize
Expand All @@ -60,6 +60,21 @@ def initialize
@language_map = {}
load_natural_language('en')
end

def self.register_transform(pattern, &transform)
raise 'Transform must be registered with a block' unless block_given?
@@transforms[pattern] = transform.to_proc
end

def self.transform_arguments(args)
args.map do |arg|
if pattern = @@transforms.keys.detect {|pattern| arg =~ pattern }
@@transforms[pattern].call(arg)
else
arg
end
end
end

def load_plain_text_features(feature_files)
features = Ast::Features.new
Expand Down

0 comments on commit 873b512

Please sign in to comment.