diff --git a/examples/scenario_outline_table_substitution.feature b/examples/scenario_outline_table_substitution.feature new file mode 100644 index 0000000..7a7d9b7 --- /dev/null +++ b/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 | + | | + When I attack the monster and do points damage + Then the monster should be + + Examples: + | hp | damage | state | + | 10 | 13 | dead | + | 8 | 5 | alive | diff --git a/lib/turnip/builder.rb b/lib/turnip/builder.rb index aa74053..0337f50 100644 --- a/lib/turnip/builder.rb +++ b/lib/turnip/builder.rb @@ -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) diff --git a/spec/builder_spec.rb b/spec/builder_spec.rb index 142b6d1..f1e8c89 100644 --- a/spec/builder_spec.rb +++ b/spec/builder_spec.rb @@ -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 diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index b9cdabf..4a04ddb 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -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 diff --git a/spec/table_spec.rb b/spec/table_spec.rb index 912e7ed..82ba2d9 100644 --- a/spec/table_spec.rb +++ b/spec/table_spec.rb @@ -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 @@ -71,4 +76,5 @@ table.map(&:first).should == ['moo', 'quox'] end end + end