Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

pretty printing support for json formatter #243

Closed
wants to merge 8 commits into from
Closed
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
245 changes: 244 additions & 1 deletion features/.cucumber/stepdefs.json
Original file line number Diff line number Diff line change
@@ -1 +1,244 @@
[{"steps":[{"args":[],"name":"they have arrived"}],"source":"^they have arrived$","file_colon_line":"features/step_definitions/eyeball_steps.rb:1","flags":""},{"steps":[],"source":"^a \"(ruby|native)\" \"([^\\\"]*)\" parser$","file_colon_line":"features/step_definitions/gherkin_steps.rb:1","flags":""},{"steps":[{"args":[],"name":"the following text is parsed:"}],"source":"^the\\ following\\ text\\ is\\ parsed:$","file_colon_line":"features/step_definitions/gherkin_steps.rb:5","flags":""},{"steps":[{"args":[],"name":"there should be no parse errors"}],"source":"^there\\ should\\ be\\ no\\ parse\\ errors$","file_colon_line":"features/step_definitions/gherkin_steps.rb:9","flags":""},{"steps":[{"args":[{"offset":33,"val":"line 1"}],"name":"there should be a parse error on line 1"}],"source":"^there should be a parse error on (line \\d+)$","file_colon_line":"features/step_definitions/gherkin_steps.rb:13","flags":""},{"steps":[{"args":[{"offset":32,"val":"lines 1 and 2"}],"name":"there should be parse errors on lines 1 and 2"},{"args":[{"offset":32,"val":"lines 1 through 3"}],"name":"there should be parse errors on lines 1 through 3"},{"args":[{"offset":32,"val":"lines 10 and 17"}],"name":"there should be parse errors on lines 10 and 17"},{"args":[{"offset":32,"val":"lines 3 and 5"}],"name":"there should be parse errors on lines 3 and 5"},{"args":[{"offset":32,"val":"lines 5, 10 and 12"}],"name":"there should be parse errors on lines 5, 10 and 12"}],"source":"^there should be parse errors on (lines .*)$","file_colon_line":"features/step_definitions/gherkin_steps.rb:17","flags":""},{"steps":[],"source":"^a JSON formatter$","file_colon_line":"features/step_definitions/json_formatter_steps.rb:13","flags":""},{"steps":[{"args":[],"name":"the outputted JSON should be:"}],"source":"^the outputted JSON should be:$","file_colon_line":"features/step_definitions/json_formatter_steps.rb:18","flags":""},{"steps":[],"source":"^a PrettyFormatter$","file_colon_line":"features/step_definitions/json_parser_steps.rb:6","flags":""},{"steps":[],"source":"^a JSON lexer$","file_colon_line":"features/step_definitions/json_parser_steps.rb:11","flags":""},{"steps":[{"args":[],"name":"the following JSON is parsed:"}],"source":"^the following JSON is parsed:$","file_colon_line":"features/step_definitions/json_parser_steps.rb:15","flags":""},{"steps":[{"args":[],"name":"the outputted text should be:"}],"source":"^the outputted text should be:$","file_colon_line":"features/step_definitions/json_parser_steps.rb:19","flags":""},{"steps":[{"args":[],"name":"I have Cucumber's source code next to Gherkin's"}],"source":"^I have Cucumber's source code next to Gherkin's$","file_colon_line":"features/step_definitions/pretty_formatter_steps.rb:50","flags":""},{"steps":[{"args":[],"name":"I find all of the .feature files"}],"source":"^I find all of the \\.feature files$","file_colon_line":"features/step_definitions/pretty_formatter_steps.rb:55","flags":""},{"steps":[{"args":[{"offset":45,"val":"json"}],"name":"I send each prettified original through the \"json\" machinery"},{"args":[{"offset":45,"val":"pretty"}],"name":"I send each prettified original through the \"pretty\" machinery"}],"source":"^I send each prettified original through the \"([^\"]*)\" machinery$","file_colon_line":"features/step_definitions/pretty_formatter_steps.rb:59","flags":""},{"steps":[{"args":[],"name":"the machinery output should be identical to the prettified original"}],"source":"^the machinery output should be identical to the prettified original$","file_colon_line":"features/step_definitions/pretty_formatter_steps.rb:83","flags":""}]
[
{
"source": "^they have arrived$",
"flags": "",
"file_colon_line": "features/step_definitions/eyeball_steps.rb:1",
"steps": [
{
"name": "they have arrived",
"args": [

]
}
]
},
{
"source": "^a \"(ruby|native)\" \"([^\\\"]*)\" parser$",
"flags": "",
"file_colon_line": "features/step_definitions/gherkin_steps.rb:1",
"steps": [

]
},
{
"source": "^the\\ following\\ text\\ is\\ parsed:$",
"flags": "",
"file_colon_line": "features/step_definitions/gherkin_steps.rb:5",
"steps": [
{
"name": "the following text is parsed:",
"args": [

]
}
]
},
{
"source": "^there\\ should\\ be\\ no\\ parse\\ errors$",
"flags": "",
"file_colon_line": "features/step_definitions/gherkin_steps.rb:9",
"steps": [
{
"name": "there should be no parse errors",
"args": [

]
}
]
},
{
"source": "^there should be a parse error on (line \\d+)$",
"flags": "",
"file_colon_line": "features/step_definitions/gherkin_steps.rb:13",
"steps": [
{
"name": "there should be a parse error on line 1",
"args": [
{
"offset": 33,
"val": "line 1"
}
]
}
]
},
{
"source": "^there should be parse errors on (lines .*)$",
"flags": "",
"file_colon_line": "features/step_definitions/gherkin_steps.rb:17",
"steps": [
{
"name": "there should be parse errors on lines 1 and 2",
"args": [
{
"offset": 32,
"val": "lines 1 and 2"
}
]
},
{
"name": "there should be parse errors on lines 1 through 3",
"args": [
{
"offset": 32,
"val": "lines 1 through 3"
}
]
},
{
"name": "there should be parse errors on lines 10 and 17",
"args": [
{
"offset": 32,
"val": "lines 10 and 17"
}
]
},
{
"name": "there should be parse errors on lines 3 and 5",
"args": [
{
"offset": 32,
"val": "lines 3 and 5"
}
]
},
{
"name": "there should be parse errors on lines 5, 10 and 12",
"args": [
{
"offset": 32,
"val": "lines 5, 10 and 12"
}
]
}
]
},
{
"source": "^a JSON formatter$",
"flags": "",
"file_colon_line": "features/step_definitions/json_formatter_steps.rb:13",
"steps": [

]
},
{
"source": "^the outputted JSON should be:$",
"flags": "",
"file_colon_line": "features/step_definitions/json_formatter_steps.rb:18",
"steps": [
{
"name": "the outputted JSON should be:",
"args": [

]
}
]
},
{
"source": "^a PrettyFormatter$",
"flags": "",
"file_colon_line": "features/step_definitions/json_parser_steps.rb:6",
"steps": [

]
},
{
"source": "^a JSON lexer$",
"flags": "",
"file_colon_line": "features/step_definitions/json_parser_steps.rb:11",
"steps": [

]
},
{
"source": "^the following JSON is parsed:$",
"flags": "",
"file_colon_line": "features/step_definitions/json_parser_steps.rb:15",
"steps": [
{
"name": "the following JSON is parsed:",
"args": [

]
}
]
},
{
"source": "^the outputted text should be:$",
"flags": "",
"file_colon_line": "features/step_definitions/json_parser_steps.rb:19",
"steps": [
{
"name": "the outputted text should be:",
"args": [

]
}
]
},
{
"source": "^I have Cucumber's source code next to Gherkin's$",
"flags": "",
"file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:50",
"steps": [
{
"name": "I have Cucumber's source code next to Gherkin's",
"args": [

]
}
]
},
{
"source": "^I find all of the \\.feature files$",
"flags": "",
"file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:55",
"steps": [
{
"name": "I find all of the .feature files",
"args": [

]
}
]
},
{
"source": "^I send each prettified original through the \"([^\"]*)\" machinery$",
"flags": "",
"file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:59",
"steps": [
{
"name": "I send each prettified original through the \"json\" machinery",
"args": [
{
"offset": 45,
"val": "json"
}
]
},
{
"name": "I send each prettified original through the \"pretty\" machinery",
"args": [
{
"offset": 45,
"val": "pretty"
}
]
}
]
},
{
"source": "^the machinery output should be identical to the prettified original$",
"flags": "",
"file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:83",
"steps": [
{
"name": "the machinery output should be identical to the prettified original",
"args": [

]
}
]
}
]
14 changes: 12 additions & 2 deletions java/src/main/java/gherkin/formatter/JSONFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import gherkin.formatter.model.Step;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSONFormatter implements Reporter, Formatter {
private final List<Map<String, Object>> featureMaps = new ArrayList<Map<String, Object>>();
private final NiceAppendable out;
private final boolean isPretty;

private Map<String, Object> featureMap;
private String uri;
Expand Down Expand Up @@ -77,9 +79,13 @@ private Map getCurrentStep(Phase phase){
return lastWithValue;
}


public JSONFormatter(Appendable out) {
this(out, Collections.emptyMap());
}

public JSONFormatter(Appendable out, Map options) {
this.out = new NiceAppendable(out);
this.isPretty = options.containsKey("pretty") && (Boolean) options.get("pretty");
}

@Override
Expand Down Expand Up @@ -246,6 +252,10 @@ private List<String> getOutput() {
}

protected Gson gson() {
return new GsonBuilder().create();
final GsonBuilder gb = new GsonBuilder().serializeNulls();
if (isPretty) {
gb.setPrettyPrinting();
}
return gb.create();
}
}
9 changes: 7 additions & 2 deletions lib/gherkin/formatter/json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ class JSONFormatter

include Base64

def initialize(io)
def initialize(io, options = {})
raise "Must be writeable" unless io.respond_to?(:write)
@io = io
@dump_options = options
@feature_hashes = []
@current_step_or_hook = nil
end

def done
@io.write(MultiJson.dump(@feature_hashes))
@io.write(MultiJson.dump(@feature_hashes, dump_options))
end

def uri(uri)
Expand Down Expand Up @@ -127,6 +128,10 @@ def encode64s(data)
# Strip newlines
Base64.encode64(data).gsub(/\n/, '')
end

def dump_options
@dump_options
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/gherkin/native/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def javaify(arg)
arg.map{|a| javaify(a)}
else
case(arg)
when Hash
h = {}
arg.each_pair {|k, v| h[javaify(k)] = javaify(v) }
h
when Regexp
java.util.regex.Pattern.compile(arg.source)
when Symbol
Expand Down
19 changes: 19 additions & 0 deletions spec/gherkin/formatter/json_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ module Formatter
MultiJson.load(expected).should == MultiJson.load(io.string)
end

it "pretty prints results" do
io = StringIO.new
f = JSONFormatter.new(io, :pretty => true)
# f.uri("f.feature")
f.feature(Model::Feature.new([], [], "Feature", nil, nil, nil, nil))
f.eof
f.done

expected = %{
[
{
"keyword": "Feature",
"uri": null
}
]
}
io.string.should == MultiJson.dump(MultiJson.load(expected), :pretty => true)
end

it 'supports append_duration' do
io = StringIO.new
f = JSONFormatter.new(io)
Expand Down