Skip to content
Browse files

Optional parts of words in step definitions.

To make feature files read better, without duplicating step defitions,
you can now specify optional parts of words, like this:

  step "cool monster(s)" do |count|
    @monsters = Array.new(count) { Monster.new }
  end

That will match either "cool monster" or "cool monsters".
  • Loading branch information...
1 parent 3b2d82f commit c3d5e1f43eff6271295e95355058cc0f52312ac7 @cjse cjse committed Oct 26, 2011
Showing with 13 additions and 5 deletions.
  1. +3 −3 README.md
  2. +5 −2 lib/turnip/step_definition.rb
  3. +5 −0 spec/step_definition_spec.rb
View
6 README.md
@@ -92,15 +92,15 @@ Given there is a monster called Jonas
And there is a monster called "Jonas Nicklas"
```
-You can also specify alternative words, like this:
+You can also specify alternative words and optional parts of words, like this:
``` ruby
-step "there is/are :count monsters" do |count|
+step "there is/are :count monster(s)" do |count|
@monsters = Array.new(count) { Monster.new }
end
```
-That will match either "there is X monsters" or "there are X monsters".
+That will match both "there is X monster" or "there are X monsters".
## Defining placeholders
View
7 lib/turnip/step_definition.rb
@@ -56,8 +56,11 @@ def match(description)
def compile_regexp
regexp = Regexp.escape(expression)
- regexp = regexp.gsub(/:([\w]+)/) do |_|
- "(?<#{$1}>#{Placeholder.resolve($1.to_sym)})"
+ regexp = regexp.gsub(/\\\(([a-z]+)\\\)/) do |_|
+ "(?:#{$1})?"
+ end
+ regexp = regexp.gsub(/(\s):([\w]+)/) do |_|
+ "#{$1}(?<#{$2}>#{Placeholder.resolve($2.to_sym)})"
end
regexp = regexp.gsub(/(\w+)\/(\w+)/) do |_|
"(?:#{$1}|#{$2})"
View
5 spec/step_definition_spec.rb
@@ -5,5 +5,10 @@
step.match("there are monsters").should_not be_nil
step.match("there is monsters").should_not be_nil
end
+
+ it "matches optional parts of words" do
+ step = Turnip::StepDefinition.new("there is/are X monster(s)") {}
+ step.match("there is X monster").should_not be_nil
+ end
end
end

0 comments on commit c3d5e1f

Please sign in to comment.
Something went wrong with that request. Please try again.