Permalink
Browse files

Support scenario outlines

  • Loading branch information...
1 parent b6217d7 commit dd0eaac12efbeb500d7885fc81fd65f748b8d7f8 @jnicklas committed Oct 31, 2011
Showing with 86 additions and 7 deletions.
  1. +10 −0 examples/scenario_outline.feature
  2. +16 −0 examples/steps.rb
  3. +2 −2 lib/turnip.rb
  4. +27 −4 lib/turnip/builder.rb
  5. +30 −0 spec/builder_spec.rb
  6. +1 −1 spec/integration_spec.rb
@@ -0,0 +1,10 @@
+Feature: using scenario outlines
+ Scenario Outline: a simple outline
+ Given there is a monster with <hp> hitpoints
+ When I attack the monster and do <damage> points damage
+ Then the monster should be <state>
+
+ Examples:
+ | hp | damage | state |
+ | 10 | 13 | dead |
+ | 8 | 5 | alive |
View
@@ -40,6 +40,22 @@
@monkeys.should == [:blue, :blue, :blue]
end
+step "there is a monster with :count hitpoints" do |count|
+ @monster = count
+end
+
+step "I attack the monster and do :count points damage" do |count|
+ @monster -= count
+end
+
+step "the monster should be alive" do
+ @monster.should > 0
+end
+
+step "the monster should be dead" do
+ @monster.should <= 0
+end
+
placeholder :count do
match /\d+/ do |count|
count.to_i
View
@@ -19,14 +19,14 @@ def run(content)
feature.backgrounds.each do |background|
before do
background.steps.each do |step|
- Turnip::StepDefinition.execute(self, step.name)
+ Turnip::StepDefinition.execute(self, step)
end
end
end
feature.scenarios.each do |scenario|
it scenario.name, scenario.metadata_hash do
scenario.steps.each do |step|
- Turnip::StepDefinition.execute(self, step.name)
+ Turnip::StepDefinition.execute(self, step)
end
end
end
View
@@ -49,19 +49,35 @@ class Scenario
include Tags
include Name
- attr_reader :steps
+ attr_accessor :steps
def initialize(raw)
@raw = raw
@steps = []
end
end
- class Step
+ class ScenarioOutline
+ include Tags
include Name
+ attr_reader :steps
+
def initialize(raw)
@raw = raw
+ @steps = []
+ end
+
+ def to_scenarios(examples)
+ rows = examples.rows.map(&:cells)
+ headers = rows.shift
+ rows.map do |row|
+ Scenario.new(@raw).tap do |scenario|
+ scenario.steps = steps.map do |step|
+ step.gsub(/<([^>]*)>/) { |_| Hash[headers.zip(row)][$1] }
+ end
+ end
+ end
end
end
@@ -96,9 +112,16 @@ def scenario(scenario)
@current_feature.scenarios << @current_step_context
end
+ def scenario_outline(outline)
+ @current_step_context = ScenarioOutline.new(outline)
+ end
+
+ def examples(examples)
+ @current_feature.scenarios.push(*@current_step_context.to_scenarios(examples))
+ end
+
def step(step)
- @current_step = Step.new(step)
- @current_step_context.steps << @current_step
+ @current_step_context.steps << step.name
end
def eof
View
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe Turnip::Builder do
+ context "with scenario outlines" do
+ let(:gherkin) { File.read(File.expand_path('../examples/scenario_outline.feature', File.dirname(__FILE__))) }
+ let(:builder) { Turnip::Builder.build(gherkin) }
+ let(:feature) { builder.features.first }
+
+
+ it "extracts scenarios" do
+ feature.scenarios.map(&:name).should eq([
+ 'a simple outline',
+ 'a simple outline'
+ ])
+ end
+
+ it "replaces placeholders in steps" do
+ feature.scenarios[0].steps.should eq([
+ "there is a monster with 10 hitpoints",
+ "I attack the monster and do 13 points damage",
+ "the monster should be dead"
+ ])
+ feature.scenarios[1].steps.should eq([
+ "there is a monster with 8 hitpoints",
+ "I attack the monster and do 5 points damage",
+ "the monster should be alive"
+ ])
+ end
+ end
+end
View
@@ -11,6 +11,6 @@
end
it "prints out failures and successes" do
- @result.should include('9 examples, 1 failure, 1 pending')
+ @result.should include('11 examples, 1 failure, 1 pending')
end
end

0 comments on commit dd0eaac

Please sign in to comment.