Skip to content

Commit

Permalink
Add scenario outline support to the JSON formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
jarib committed Jun 3, 2010
1 parent 866fcb8 commit e47e31a
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 5 deletions.
10 changes: 10 additions & 0 deletions examples/json/features/outline.feature
@@ -0,0 +1,10 @@
Feature: A scenario outline

Scenario Outline:
Given I add <a> and <b>
Then I the result should be <c>

Examples:
| a | b | c |
| 1 | 2 | 3 |
| 2 | 3 | 4 |
9 changes: 9 additions & 0 deletions examples/json/features/step_definitions/steps.rb
Expand Up @@ -9,3 +9,12 @@
Given /a pending step/ do
pending
end

Given /^I add (\d+) and (\d+)$/ do |a,b|
@result = a.to_i + b.to_i
end

Then /^I the result should be (\d+)$/ do |c|
@result.should == c.to_i
end

101 changes: 101 additions & 0 deletions features/json_formatter.feature
Expand Up @@ -69,3 +69,104 @@ Feature: JSON output formatter
]
}
"""

Scenario: Scenario Outline
Given I am in json
When I run cucumber --format json_pretty features/outline.feature
Then it should fail with JSON
"""
{
"features": [
{
"file": "features/outline.feature",
"name": "A scenario outline",
"tags": [
],
"elements": [
{
"tags": [
],
"keyword": "Scenario Outline",
"name": "",
"file_colon_line": "features/outline.feature:3",
"steps": [
{
"status": "skipped",
"name": "Given I add <a> and <b>",
"file_colon_line": "features/step_definitions/steps.rb:13"
},
{
"status": "skipped",
"name": "Then I the result should be <c>",
"file_colon_line": "features/step_definitions/steps.rb:17"
}
],
"examples": {
"name": "Examples ",
"table": [
{
"values": [
{
"value": "a",
"status": "skipped_param"
},
{
"value": "b",
"status": "skipped_param"
},
{
"value": "c",
"status": "skipped_param"
}
]
},
{
"values": [
{
"value": "1",
"status": "passed"
},
{
"value": "2",
"status": "passed"
},
{
"value": "3",
"status": "passed"
}
]
},
{
"values": [
{
"value": "2",
"status": "passed"
},
{
"value": "3",
"status": "passed"
},
{
"value": "4",
"status": "failed"
}
],
"exception": {
"class": "Spec::Expectations::ExpectationNotMetError",
"message": "expected: 4,\n got: 5 (using ==)\n\n Diff:\n@@ -1,2 +1,2 @@\n-4\n+5\n",
"backtrace": [
"./features/step_definitions/steps.rb:18:in `/^I the result should be (\\d+)$/'",
"features/outline.feature:5:in `Then I the result should be <c>'"
]
}
}
]
}
}
]
}
]
}
"""
45 changes: 40 additions & 5 deletions lib/cucumber/formatter/json.rb
Expand Up @@ -50,11 +50,7 @@ def before_step(step)

def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
if exception
@current_step[:exception] = {
:class => exception.class.name,
:message => exception.message,
:backtrace => exception.backtrace
}
@current_step[:exception] = exception_hash_for(exception)
end
end

Expand All @@ -68,6 +64,34 @@ def after_step(step)
@current_step = nil
end

def before_examples(examples)
@current_object[:examples] = {}
end

def examples_name(keyword, name)
@current_object[:examples][:name] = "#{keyword} #{name}"
end

def before_outline_table(*args)
@current_object[:examples][:table] = []
end

def before_table_row(row)
@current_row = {:values => []}
@current_object[:examples][:table] << @current_row
end

def table_cell_value(value, status)
@current_row[:values] << {:value => value, :status => status}
end

def after_table_row(row)
if row.exception
@current_row[:exception] = exception_hash_for(row.exception)
end
@current_row = nil
end

def after_feature_element(feature_element)
# change current object back to the last feature
@current_object = @json[:features].last
Expand All @@ -78,10 +102,21 @@ def after_features(features)
@io.flush
end

private

def json_string
@json.to_json
end

def exception_hash_for(e)
{
:class => e.class.name,
:message => e.message,
:backtrace => e.backtrace
}
end

end # Json
end # Formatter
end # Cucumber

0 comments on commit e47e31a

Please sign in to comment.