Permalink
Browse files

Added step argument transformations

  • Loading branch information...
1 parent 2a0d6be commit 873b512089556e8117a883d1f25465ddb07b7aee @larrytheliquid larrytheliquid committed Sep 11, 2009
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit 873b512

Please sign in to comment.