diff --git a/History.md b/History.md index ce4eb06a67..1aef2747ed 100644 --- a/History.md +++ b/History.md @@ -4,6 +4,7 @@ ### Bugfixes +* Let the JSON formatter include data tables in the JSON file ([#948](https://github.com/cucumber/cucumber-ruby/issues/948) @brasmusson) * Stringifying location in the JSON formatter for more consistent json parsing ([949](https://github.com/cucumber/cucumber-ruby/pull/949), [945](https://github.com/cucumber/cucumber-ruby/issues/945) @larryprice) ### Refactoring diff --git a/lib/cucumber/formatter/json.rb b/lib/cucumber/formatter/json.rb index a85381877c..bd3a1a18b6 100644 --- a/lib/cucumber/formatter/json.rb +++ b/lib/cucumber/formatter/json.rb @@ -153,6 +153,7 @@ def create_step_hash(step_source) } 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[:rows] = create_data_table_value(step_source.multiline_arg) if step_source.multiline_arg.data_table? step_hash end @@ -165,6 +166,12 @@ def create_doc_string_hash(doc_string) } end + def create_data_table_value(data_table) + data_table.raw.map do |row| + { cells: row } + end + end + def add_match_and_result(test_step, result) @step_or_hook_hash[:match] = create_match_hash(test_step, result) @step_or_hook_hash[:result] = create_result_hash(result) diff --git a/spec/cucumber/formatter/json_spec.rb b/spec/cucumber/formatter/json_spec.rb index 6b7e7b6ee1..79ad080e79 100644 --- a/spec/cucumber/formatter/json_spec.rb +++ b/spec/cucumber/formatter/json_spec.rb @@ -757,6 +757,48 @@ module Formatter "duration": 1}}]}]}]}) end end + + describe "with a scenario with a step with a data table" do + define_feature <<-FEATURE + Feature: Banana party + + Scenario: Monkey eats bananas + Given there are bananas + | aa | bb | + | cc | dd | + FEATURE + + define_steps do + Given(/^there are bananas$/) { |s| s } + end + + it "includes the doc string 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": 1, + "description": "", + "elements": + [{"id": "banana-party;monkey-eats-bananas", + "keyword": "Scenario", + "name": "Monkey eats bananas", + "line": 3, + "description": "", + "type": "scenario", + "steps": + [{"keyword": "Given ", + "name": "there are bananas", + "line": 4, + "rows": + [{"cells": ["aa", "bb"]}, + {"cells": ["cc", "dd"]}], + "match": {"location": "spec/cucumber/formatter/json_spec.rb:772"}, + "result": {"status": "passed", + "duration": 1}}]}]}]}) + end + end end def load_normalised_json(out)