Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.0: Pass output from the step definition on to the gherkin formatters (JSON formatter) #706

Merged
merged 1 commit into from
Jul 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions features/docs/formatters/json_formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
]
}
]
}
]

"""
4 changes: 4 additions & 0 deletions lib/cucumber/formatter/gherkin_formatter_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 23 additions & 2 deletions lib/cucumber/reports/legacy_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -153,6 +156,10 @@ def after
self
end

def puts(message)
@child.puts(message)
end

private

def timer
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -256,6 +265,10 @@ def after
self
end

def puts(message)
@delayed_messages << message
end

private

def print_step_container
Expand Down Expand Up @@ -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)
Expand Down