Skip to content

Commit

Permalink
Json Formatter: handle comments, fix the handling of tags.
Browse files Browse the repository at this point in the history
Add the comments from the feature file to the Json output. Test cases
from Scenario Outlines have the comments from the Scenario Outline,
the Examples table and the Examples table row.
All tags applied to a test case are added to the tags of the test case,
tags on the Feature are added to all test cases, and test cases from
Scenario Outlines also have the tags for both the Scenario Outline and
the Examples table.
  • Loading branch information
brasmusson committed Jun 7, 2015
1 parent b24c2b4 commit adb8d81
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 17 deletions.
16 changes: 16 additions & 0 deletions features/docs/formatters/json_formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ Feature: JSON output formatter
"line": 5,
"description": "",
"tags": [
{
"name": "@a",
"line": 1
},
{
"name": "@b",
"line": 4
Expand Down Expand Up @@ -159,6 +163,10 @@ Feature: JSON output formatter
"line": 9,
"description": "",
"tags": [
{
"name": "@a",
"line": 1
},
{
"name": "@c",
"line": 8
Expand Down Expand Up @@ -214,6 +222,10 @@ Feature: JSON output formatter
"line": 5,
"description": "",
"tags": [
{
"name": "@a",
"line": 1
},
{
"name": "@b",
"line": 4
Expand Down Expand Up @@ -242,6 +254,10 @@ Feature: JSON output formatter
"line": 9,
"description": "",
"tags": [
{
"name": "@a",
"line": 1
},
{
"name": "@c",
"line": 8
Expand Down
33 changes: 31 additions & 2 deletions lib/cucumber/formatter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def create_step_hash(step_source)
name: step_source.name,
line: step_source.location.line
}
step_hash[:comments] = Formatter.create_comments_array(step_source.comments) unless step_source.comments.empty?
step_hash[:doc_string] = create_doc_string_hash(step_source.multiline_arg) if step_source.multiline_arg.doc_string?
step_hash
end
Expand Down Expand Up @@ -215,7 +216,15 @@ def feature(feature)
description: feature.description,
line: feature.location.line
}
@feature_hash[:tags] = create_tags_array(feature.tags) unless feature.tags.empty?
unless feature.tags.empty?
@feature_hash[:tags] = create_tags_array(feature.tags)
if @test_case_hash[:tags]
@test_case_hash[:tags] = @feature_hash[:tags] + @test_case_hash[:tags]
else
@test_case_hash[:tags] = @feature_hash[:tags]
end
end
@feature_hash[:comments] = Formatter.create_comments_array(feature.comments) unless feature.comments.empty?
@test_case_hash[:id].insert(0, @feature_hash[:id] + ';')
end

Expand All @@ -227,6 +236,7 @@ def background(background)
line: background.location.line,
type: 'background'
}
@background_hash[:comments] = Formatter.create_comments_array(background.comments) unless background.comments.empty?
end

def scenario(scenario)
Expand All @@ -239,6 +249,7 @@ def scenario(scenario)
type: 'scenario'
}
@test_case_hash[:tags] = create_tags_array(scenario.tags) unless scenario.tags.empty?
@test_case_hash[:comments] = Formatter.create_comments_array(scenario.comments) unless scenario.comments.empty?
end

def scenario_outline(scenario)
Expand All @@ -250,17 +261,29 @@ def scenario_outline(scenario)
line: @row.location.line,
type: 'scenario'
}
@test_case_hash[:tags] = create_tags_array(scenario.tags) unless scenario.tags.empty?
tags = []
tags += create_tags_array(scenario.tags) unless scenario.tags.empty?
tags += @examples_table_tags if @examples_table_tags
@test_case_hash[:tags] = tags unless tags.empty?
comments = []
comments += Formatter.create_comments_array(scenario.comments) unless scenario.comments.empty?
comments += @examples_table_comments if @examples_table_comments
comments += @row_comments if @row_comments
@test_case_hash[:comments] = comments unless comments.empty?
end

def examples_table(examples_table)
# the json file have traditionally used the header row as row 1,
# wheras cucumber-ruby-core used the first example row as row 1.
@example_id = create_id(examples_table) + ";#{@row.number + 1}"

@examples_table_tags = create_tags_array(examples_table.tags) unless examples_table.tags.empty?
@examples_table_comments = Formatter.create_comments_array(examples_table.comments) unless examples_table.comments.empty?
end

def examples_table_row(row)
@row = row
@row_comments = Formatter.create_comments_array(row.comments) unless row.comments.empty?
end

private
Expand All @@ -276,5 +299,11 @@ def create_tags_array(tags)
end
end
end

def self.create_comments_array(comments)
comments_array = []
comments.each { |comment| comments_array << { value: comment.to_s.strip, line: comment.location.line } }
comments_array
end
end
end
147 changes: 132 additions & 15 deletions spec/cucumber/formatter/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ module Formatter
end
end

describe "with a tags in the feature file" do
describe "with tags in the feature file" do
define_feature <<-FEATURE
@f
Feature: Banana party
Expand Down Expand Up @@ -267,7 +267,9 @@ module Formatter
"name": "Monkey eats bananas",
"line": 5,
"description": "",
"tags": [{"name": "@s",
"tags": [{"name": "@f",
"line": 1},
{"name": "@s",
"line": 4}],
"type": "scenario",
"steps":
Expand All @@ -282,8 +284,12 @@ module Formatter
"name": "Monkey eats bananas",
"line": 15,
"description": "",
"tags": [{"name": "@so",
"line": 8}],
"tags": [{"name": "@f",
"line": 1},
{"name": "@so",
"line": 8},
{"name": "@ex",
"line": 12}],
"type": "scenario",
"steps":
[{"keyword": "Given ",
Expand All @@ -295,6 +301,117 @@ module Formatter
end
end

describe "with comments in the feature file" do
define_feature <<-FEATURE
#feature comment
Feature: Banana party
#background comment
Background: There are bananas
Given there are bananas
#scenario comment
Scenario: Monkey eats bananas
#step comment1
Then the monkey eats bananas
#scenario outline comment
Scenario Outline: Monkey eats bananas
#step comment2
Then the monkey eats <fruit>
#examples table comment
Examples: Fruit Table
| fruit |
#examples table row comment
| bananas |
FEATURE

define_steps do
Given(/^there are bananas$/) {}
Then(/^the monkey eats bananas$/) {}
end

it "the comments are included in the json data" do
expect(load_normalised_json(@out)).to eq MultiJson.load(%{
[{"id": "banana-party",
"uri": "spec.feature",
"keyword": "Feature",
"name": "Banana party",
"line": 2,
"description": "",
"comments": [{"value": "#feature comment",
"line": 1}],
"elements":
[{"keyword": "Background",
"name": "There are bananas",
"line": 5,
"description": "",
"comments": [{"value": "#background comment",
"line": 4}],
"type": "background",
"steps":
[{"keyword": "Given ",
"name": "there are bananas",
"line": 6,
"match": {"location": "spec/cucumber/formatter/json_spec.rb:331"},
"result": {"status": "passed",
"duration": 1}}]},
{"id": "banana-party;monkey-eats-bananas",
"keyword": "Scenario",
"name": "Monkey eats bananas",
"line": 9,
"description": "",
"comments": [{"value": "#scenario comment",
"line": 8}],
"type": "scenario",
"steps":
[{"keyword": "Then ",
"name": "the monkey eats bananas",
"line": 11,
"comments": [{"value": "#step comment1",
"line": 10}],
"match": {"location": "spec/cucumber/formatter/json_spec.rb:332"},
"result": {"status": "passed",
"duration": 1}}]},
{"keyword": "Background",
"name": "There are bananas",
"line": 5,
"description": "",
"comments": [{"value": "#background comment",
"line": 4}],
"type": "background",
"steps":
[{"keyword": "Given ",
"name": "there are bananas",
"line": 6,
"match": {"location": "spec/cucumber/formatter/json_spec.rb:331"},
"result": {"status": "passed",
"duration": 1}}]},
{"id": "banana-party;monkey-eats-bananas;fruit-table;2",
"keyword": "Scenario Outline",
"name": "Monkey eats bananas",
"line": 22,
"description": "",
"comments": [{"value": "#scenario outline comment",
"line": 13},
{"value": "#examples table comment",
"line": 18},
{"value": "#examples table row comment",
"line": 21}],
"type": "scenario",
"steps":
[{"keyword": "Then ",
"name": "the monkey eats bananas",
"line": 22,
"comments": [{"value": "#step comment2",
"line": 15}],
"match": {"location": "spec/cucumber/formatter/json_spec.rb:332"},
"result": {"status": "passed",
"duration": 1}}]}]}]})
end
end

describe "with a scenario with a step with a doc string" do
define_feature <<-FEATURE
Feature: Banana party
Expand Down Expand Up @@ -332,7 +449,7 @@ module Formatter
"doc_string": {"value": "the doc string",
"content_type": "",
"line": 5},
"match": {"location": "spec/cucumber/formatter/json_spec.rb:310"},
"match": {"location": "spec/cucumber/formatter/json_spec.rb:427"},
"result": {"status": "passed",
"duration": 1}}]}]}]})
end
Expand Down Expand Up @@ -370,7 +487,7 @@ module Formatter
"name": "there are bananas",
"line": 4,
"output": ["from step"],
"match": {"location": "spec/cucumber/formatter/json_spec.rb:350"},
"match": {"location": "spec/cucumber/formatter/json_spec.rb:467"},
"result": {"status": "passed",
"duration": 1}}]}]}]})
end
Expand Down Expand Up @@ -482,7 +599,7 @@ module Formatter
"line": 4,
"embeddings": [{"mime_type": "mime-type",
"data": "YWJj"}],
"match": {"location": "spec/cucumber/formatter/json_spec.rb:460"},
"match": {"location": "spec/cucumber/formatter/json_spec.rb:577"},
"result": {"status": "passed",
"duration": 1}}]}]}]})
end
Expand Down Expand Up @@ -527,7 +644,7 @@ module Formatter
"line": 4,
"embeddings": [{"mime_type": "image/png",
"data": "Zm9v"}],
"match": {"location": "spec/cucumber/formatter/json_spec.rb:500"},
"match": {"location": "spec/cucumber/formatter/json_spec.rb:617"},
"result": {"status": "passed",
"duration": 1}}]}]}]})
end
Expand Down Expand Up @@ -568,31 +685,31 @@ module Formatter
"description": "",
"type": "scenario",
"before":
[{"match": {"location": "spec/cucumber/formatter/json_spec.rb:545"},
[{"match": {"location": "spec/cucumber/formatter/json_spec.rb:662"},
"result": {"status": "passed",
"duration": 1}},
{"match": {"location": "spec/cucumber/formatter/json_spec.rb:546"},
{"match": {"location": "spec/cucumber/formatter/json_spec.rb:663"},
"result": {"status": "passed",
"duration": 1}}],
"steps":
[{"keyword": "Given ",
"name": "there are bananas",
"line": 4,
"match": {"location": "spec/cucumber/formatter/json_spec.rb:552"},
"match": {"location": "spec/cucumber/formatter/json_spec.rb:669"},
"result": {"status": "passed",
"duration": 1},
"after":
[{"match": {"location": "spec/cucumber/formatter/json_spec.rb:549"},
[{"match": {"location": "spec/cucumber/formatter/json_spec.rb:666"},
"result": {"status": "passed",
"duration": 1}},
{"match": {"location": "spec/cucumber/formatter/json_spec.rb:550"},
{"match": {"location": "spec/cucumber/formatter/json_spec.rb:667"},
"result": {"status": "passed",
"duration": 1}}]}],
"after":
[{"match": {"location": "spec/cucumber/formatter/json_spec.rb:548"},
[{"match": {"location": "spec/cucumber/formatter/json_spec.rb:665"},
"result": {"status": "passed",
"duration": 1}},
{"match": {"location": "spec/cucumber/formatter/json_spec.rb:547"},
{"match": {"location": "spec/cucumber/formatter/json_spec.rb:664"},
"result": {"status": "passed",
"duration": 1}}]}]}]})
end
Expand Down

0 comments on commit adb8d81

Please sign in to comment.