Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support scenario outlines

  • Loading branch information...
commit dd0eaac12efbeb500d7885fc81fd65f748b8d7f8 1 parent b6217d7
@jnicklas authored
View
10 examples/scenario_outline.feature
@@ -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
16 examples/steps.rb
@@ -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
4 lib/turnip.rb
@@ -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
31 lib/turnip/builder.rb
@@ -49,7 +49,7 @@ class Scenario
include Tags
include Name
- attr_reader :steps
+ attr_accessor :steps
def initialize(raw)
@raw = raw
@@ -57,11 +57,27 @@ def initialize(raw)
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
30 spec/builder_spec.rb
@@ -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
2  spec/integration_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.