From 3a4f8f69444fe94c0216bfb444ea000cfe4db7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rasmusson?= Date: Mon, 30 Jun 2014 16:08:31 +0200 Subject: [PATCH] Handle more than one Examples table in a Scenario Outline --- features/json_formatter.feature | 65 ++++++++++++++++--- .../formatter/gherkin_formatter_adapter.rb | 43 ++++++------ 2 files changed, 80 insertions(+), 28 deletions(-) diff --git a/features/json_formatter.feature b/features/json_formatter.feature index ae3ac0f222..e9fbb25d77 100644 --- a/features/json_formatter.feature +++ b/features/json_formatter.feature @@ -79,11 +79,15 @@ Feature: JSON output formatter Scenario Outline: outline Given a step - Examples: examples + Examples: examples1 | type | | passing | | failing | + Examples: examples2 + | type | + | passing | + """ # Need to investigate why this won't pass in-process. error_message doesn't get det? @@ -453,31 +457,54 @@ Feature: JSON output formatter "examples": [ { "keyword": "Examples", - "name": "examples", + "name": "examples1", "line": 6, "description": "", - "id": "an-outline-feature;outline;examples", + "id": "an-outline-feature;outline;examples1", "rows": [ { "cells": [ "type" ], "line": 7, - "id": "an-outline-feature;outline;examples;1" + "id": "an-outline-feature;outline;examples1;1" }, { "cells": [ "passing" ], "line": 8, - "id": "an-outline-feature;outline;examples;2" + "id": "an-outline-feature;outline;examples1;2" }, { "cells": [ "failing" ], "line": 9, - "id": "an-outline-feature;outline;examples;3" + "id": "an-outline-feature;outline;examples1;3" + } + ] + }, + { + "keyword": "Examples", + "name": "examples2", + "line": 11, + "description": "", + "id": "an-outline-feature;outline;examples2", + "rows": [ + { + "cells": [ + "type" + ], + "line": 12, + "id": "an-outline-feature;outline;examples2;1" + }, + { + "cells": [ + "passing" + ], + "line": 13, + "id": "an-outline-feature;outline;examples2;2" } ] } @@ -503,7 +530,7 @@ Feature: JSON output formatter "description": "", "elements": [ { - "id": "an-outline-feature;outline;examples;2", + "id": "an-outline-feature;outline;examples1;2", "keyword": "Scenario Outline", "name": "outline", "line": 8, @@ -525,7 +552,7 @@ Feature: JSON output formatter ] }, { - "id": "an-outline-feature;outline;examples;3", + "id": "an-outline-feature;outline;examples1;3", "keyword": "Scenario Outline", "name": "outline", "line": 9, @@ -546,6 +573,28 @@ Feature: JSON output formatter } } ] + }, + { + "id": "an-outline-feature;outline;examples2;2", + "keyword": "Scenario Outline", + "name": "outline", + "line": 13, + "description": "", + "type": "scenario", + "steps": [ + { + "keyword": "Given ", + "name": "a passing step", + "line": 4, + "match": { + "location": "features/step_definitions/steps.rb:1" + }, + "result": { + "status": "passed", + "duration": 1 + } + } + ] } ] } diff --git a/lib/cucumber/formatter/gherkin_formatter_adapter.rb b/lib/cucumber/formatter/gherkin_formatter_adapter.rb index 05f51a9690..15b165c7ee 100644 --- a/lib/cucumber/formatter/gherkin_formatter_adapter.rb +++ b/lib/cucumber/formatter/gherkin_formatter_adapter.rb @@ -30,6 +30,7 @@ def before_feature_element(feature_element) when Ast::ScenarioOutline @outline = true if @options[:expand] + @in_instantiated_scenario = false @current_scenario_hash = to_hash(feature_element.gherkin_statement) else @gf.scenario_outline(feature_element.gherkin_statement) @@ -41,19 +42,23 @@ def before_feature_element(feature_element) def scenario_name(keyword, name, file_colon_line, source_indent) if @outline and @options[:expand] - @example_row = @example_row ? @example_row + 1 : 0 - if in_instantiated_scenario? - example_row_hash = @current_example_rows[@example_row].to_hash - scenario = Gherkin::Formatter::Model::Scenario.new( - @current_scenario_hash['comments'], - @current_scenario_hash['tags'], - @current_scenario_hash['keyword'], - @current_scenario_hash['name'], - @current_scenario_hash['description'], - example_row_hash['line'], - example_row_hash['id']) - @gf.scenario(scenario) + return if not @in_instantiated_scenario + if @new_example_table + @example_row = 1 + @new_example_table = false + else + @example_row += 1 end + example_row_hash = @current_example_rows[@example_row].to_hash + scenario = Gherkin::Formatter::Model::Scenario.new( + @current_scenario_hash['comments'], + @current_scenario_hash['tags'], + @current_scenario_hash['keyword'], + @current_scenario_hash['name'], + @current_scenario_hash['description'], + example_row_hash['line'], + example_row_hash['id']) + @gf.scenario(scenario) end end @@ -61,7 +66,7 @@ def before_step(step) unless @outline and @options[:expand] @gf.step(step.gherkin_statement) else - if in_instantiated_scenario? + if @in_instantiated_scenario @current_step_hash = to_hash(step.gherkin_statement) end end @@ -93,7 +98,7 @@ def before_step_result(keyword, step_match, multiline_arg, status, exception, so unless @outline @gf.result(Gherkin::Formatter::Model::Result.new(status, nil, error_message)) else - if @options[:expand] and in_instantiated_scenario? + if @options[:expand] and @in_instantiated_scenario @current_match = match @current_result = Gherkin::Formatter::Model::Result.new(status, nil, error_message) end @@ -101,7 +106,7 @@ def before_step_result(keyword, step_match, multiline_arg, status, exception, so end def step_name(keyword, step_match, status, source_indent, background, file_colon_line) - if @outline and @options[:expand] and in_instantiated_scenario? + if @outline and @options[:expand] and @in_instantiated_scenario @gf.step(Gherkin::Formatter::Model::Step.new( @current_step_hash['comments'], @current_step_hash['keyword'], @@ -118,6 +123,8 @@ def before_examples(examples) unless @options[:expand] @gf.examples(examples.gherkin_statement) else + @in_instantiated_scenario = true + @new_example_table = true @current_example_rows = to_hash(examples.gherkin_statement)['rows'] end end @@ -125,7 +132,7 @@ def before_examples(examples) #used for capturing duration def after_step(step) step_finish = (Time.now - @step_time) - unless @outline and @options[:expand] and not in_instantiated_scenario? + unless @outline and @options[:expand] and not @in_instantiated_scenario @gf.append_duration(step_finish) end end @@ -152,10 +159,6 @@ def puts(message) private - def in_instantiated_scenario? - @example_row > 0 - end - def to_hash(gherkin_statement) if defined?(JRUBY_VERSION) gherkin_statement.toMap()