diff --git a/features/docs/formatters/json_formatter.feature b/features/docs/formatters/json_formatter.feature index a09915afc2..6a318197a3 100644 --- a/features/docs/formatters/json_formatter.feature +++ b/features/docs/formatters/json_formatter.feature @@ -23,6 +23,10 @@ Feature: JSON output formatter File.open("screenshot.png", "w") { |file| file << "foo" } embed "screenshot.png", "image/png" end + + Given /^I print from step definition/ do + puts "from step definition" + end """ And a file named "features/embed.feature" with: """ @@ -32,6 +36,15 @@ Feature: JSON output formatter Given I embed a screenshot """ + And a file named "features/print_from_step_definition.feature" with: + """ + Feature: A print from step definition feature + + Scenario: + Given I print from step definition + And I print from step definition + + """ # Need to investigate why this won't pass in-process. error_message doesn't get det? @spawn @@ -310,3 +323,62 @@ Feature: JSON output formatter ] """ + + Scenario: print from step definition + When I run `cucumber --format json features/print_from_step_definition.feature` + Then it should pass with JSON: + """ + [ + { + "uri": "features/print_from_step_definition.feature", + "id": "a-print-from-step-definition-feature", + "keyword": "Feature", + "name": "A print from step definition feature", + "line": 1, + "description": "", + "elements": [ + { + "id": "a-print-from-step-definition-feature;", + "keyword": "Scenario", + "name": "", + "line": 3, + "description": "", + "type": "scenario", + "steps": [ + { + "keyword": "Given ", + "name": "I print from step definition", + "line": 4, + "output": [ + "from step definition" + ], + "match": { + "location": "features/step_definitions/json_steps.rb:6" + }, + "result": { + "status": "passed", + "duration": 1 + } + }, + { + "keyword": "And ", + "name": "I print from step definition", + "line": 5, + "output": [ + "from step definition" + ], + "match": { + "location": "features/step_definitions/json_steps.rb:6" + }, + "result": { + "status": "passed", + "duration": 1 + } + } + ] + } + ] + } + ] + + """ diff --git a/lib/cucumber/formatter/gherkin_formatter_adapter.rb b/lib/cucumber/formatter/gherkin_formatter_adapter.rb index 0e8718d9b0..598c9fb55c 100644 --- a/lib/cucumber/formatter/gherkin_formatter_adapter.rb +++ b/lib/cucumber/formatter/gherkin_formatter_adapter.rb @@ -93,6 +93,10 @@ def embed(file, mime_type, label) @gf.embedding(mime_type, data) end end + + def puts(message) + @gf.write(message) + end end end end diff --git a/lib/cucumber/reports/legacy_formatter.rb b/lib/cucumber/reports/legacy_formatter.rb index 26aa010b1c..b7e60ccc6e 100644 --- a/lib/cucumber/reports/legacy_formatter.rb +++ b/lib/cucumber/reports/legacy_formatter.rb @@ -65,8 +65,7 @@ def initialize(runtime, formatters) def_delegators :formatter, :embed, - :ask, - :puts + :ask def before_test_case(test_case) printer.before_test_case(test_case) @@ -85,6 +84,10 @@ def after_test_case(test_case, result) printer.after_test_case(test_case, result) end + def puts(message) + printer.puts(message) + end + def done printer.after end @@ -153,6 +156,10 @@ def after self end + def puts(message) + @child.puts(message) + end + private def timer @@ -189,6 +196,7 @@ def before Legacy::Ast::Comments.new(node.comments).accept(formatter) Legacy::Ast::Tags.new(node.tags).accept(formatter) formatter.feature_name node.keyword, indented(node.name) # TODO: change the core's new AST to return name and description separately instead of this lumped-together field + @delayed_messages = [] self end @@ -209,6 +217,7 @@ def after_test_step(test_step, result) def after_test_case(*args) if current_test_step_source.step_result.nil? print_step_container + @delayed_messages = [] end @child.after_test_case @previous_test_case_background = @current_test_case_background @@ -256,6 +265,10 @@ def after self end + def puts(message) + @delayed_messages << message + end + private def print_step_container @@ -292,6 +305,14 @@ def print_step return unless current_test_step_source.step_result print_step_container @child.step(current_test_step_source.step, current_test_step_source.step_result) + print_messages + end + + def print_messages + @delayed_messages.each do |message| + formatter.puts(message) + end + @delayed_messages = [] end def set_child_calling_before(child)