Skip to content

Commit

Permalink
Merge pull request jnicklas#68 from simplybusiness/tables_support_exa…
Browse files Browse the repository at this point in the history
…mple_variables

Tables support example variables
  • Loading branch information
jnicklas committed Aug 26, 2012
2 parents e811d0e + 0bca446 commit e3be114
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
12 changes: 12 additions & 0 deletions examples/scenario_outline_table_substitution.feature
@@ -0,0 +1,12 @@
Feature: using scenario outlines
Scenario Outline: a simple outline
Given there is a monster with hitpoints:
| hit_points |
| <hp> |
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 |
14 changes: 12 additions & 2 deletions lib/turnip/builder.rb
Expand Up @@ -81,12 +81,22 @@ def to_scenarios(examples)
rows.map do |row|
Scenario.new(@raw).tap do |scenario|
scenario.steps = steps.map do |step|
new_description = step.description.gsub(/<([^>]*)>/) { |_| Hash[headers.zip(row)][$1] }
Step.new(new_description, step.extra_args, step.line)
new_description = substitute(step.description, headers, row)
new_extra_args = step.extra_args.map do |ea|
next ea unless ea.instance_of?(Turnip::Table)
Turnip::Table.new(ea.map {|t_row| t_row.map {|t_col| substitute(t_col, headers, row) } })
end
Step.new(new_description, new_extra_args, step.line)
end
end
end
end

private

def substitute(text, headers, row)
text.gsub(/<([^>]*)>/) { |_| Hash[headers.zip(row)][$1] }
end
end

class Step < Struct.new(:description, :extra_args, :line)
Expand Down
19 changes: 19 additions & 0 deletions spec/builder_spec.rb
Expand Up @@ -27,4 +27,23 @@
])
end
end

context "with example tables in scenario outlines" do
let(:feature_file) { File.expand_path('../examples/scenario_outline_table_substitution.feature', File.dirname(__FILE__)) }
let(:builder) { Turnip::Builder.build(feature_file) }
let(:feature) { builder.features.first }

it "replaces placeholders in tables in steps" do
feature.scenarios[0].steps.map(&:description).should eq([
"there is a monster with hitpoints:",
"I attack the monster and do 13 points damage",
"the monster should be dead"
])
table = feature.scenarios[0].steps[0].extra_args.find {|a| a.instance_of?(Turnip::Table)}
table.hashes[0]['hit_points'].should == '10'
table = feature.scenarios[1].steps[0].extra_args.find {|a| a.instance_of?(Turnip::Table)}
table.hashes[0]['hit_points'].should == '8'
end

end
end
2 changes: 1 addition & 1 deletion spec/integration_spec.rb
Expand Up @@ -11,7 +11,7 @@
end

it "prints out failures and successes" do
@result.should include('33 examples, 3 failures, 3 pending')
@result.should include('35 examples, 3 failures, 5 pending')
end

it "includes features in backtraces" do
Expand Down
6 changes: 6 additions & 0 deletions spec/table_spec.rb
Expand Up @@ -8,6 +8,11 @@
it 'returns the raw table' do
table.raw.should == [['foo', 'bar'], ['quox', '42']]
end

it 'reflects changes in the raw table' do
table.raw[1][1] = '55'
table.raw.should == [['foo', 'bar'], ['quox', '55']]
end
end

describe '#to_a' do
Expand Down Expand Up @@ -71,4 +76,5 @@
table.map(&:first).should == ['moo', 'quox']
end
end

end

0 comments on commit e3be114

Please sign in to comment.