From 95f013bc4bb75c53559731cfd6cb639ea9a38123 Mon Sep 17 00:00:00 2001 From: Oleg Sukhodolsky Date: Thu, 28 Feb 2013 00:56:23 +0400 Subject: [PATCH] jruby impementation of JSONFormatter.append_duration() added --- .../java/gherkin/formatter/JSONFormatter.java | 32 +++++++---- spec/gherkin/formatter/json_formatter_spec.rb | 55 ++++++++++++++++++- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/java/src/main/java/gherkin/formatter/JSONFormatter.java b/java/src/main/java/gherkin/formatter/JSONFormatter.java index bd7ba827..5d1dd5a8 100644 --- a/java/src/main/java/gherkin/formatter/JSONFormatter.java +++ b/java/src/main/java/gherkin/formatter/JSONFormatter.java @@ -25,11 +25,11 @@ public class JSONFormatter implements Reporter, Formatter { private String uri; private enum Phase {step, match, result, embedding, output}; - + /** * In order to handle steps being added all at once, this method determines allows methods to - * opperator correctly if - * + * opperator correctly if + * * step * match * result @@ -40,9 +40,9 @@ private enum Phase {step, match, result, embedding, output}; * result * embedding * output - * - * or if - * + * + * or if + * * step * step * match @@ -53,9 +53,9 @@ private enum Phase {step, match, result, embedding, output}; * result * embedding * output - * + * * is called - * + * * @return the correct step for the current operation based on past method calls to the formatter interface */ private Map getCurrentStep(Phase phase){ @@ -76,8 +76,8 @@ private Map getCurrentStep(Phase phase){ } return lastWithValue; } - - + + public JSONFormatter(Appendable out) { this.out = new NiceAppendable(out); } @@ -164,6 +164,16 @@ private void addHook(final Match match, final Result result, String hook) { hooks.add(hookMap); } + public void appendDuration(final int timestamp) { + final Map result = (Map) getCurrentStep(Phase.result).get("result"); + // check to make sure result exists (scenario outlines do not have results yet) + if (result != null) { + //convert to nanoseconds + final long nanos = timestamp * 1000000000L; + result.put("duration", nanos); + } + } + @Override public void eof() { } @@ -238,4 +248,4 @@ private List getOutput() { protected Gson gson() { return new GsonBuilder().create(); } -} \ No newline at end of file +} diff --git a/spec/gherkin/formatter/json_formatter_spec.rb b/spec/gherkin/formatter/json_formatter_spec.rb index 8d89e1a2..121af36e 100644 --- a/spec/gherkin/formatter/json_formatter_spec.rb +++ b/spec/gherkin/formatter/json_formatter_spec.rb @@ -25,7 +25,7 @@ module Formatter f.eof f.done - + expected = %{ [ { @@ -85,7 +85,58 @@ module Formatter } ] } - JSON.parse(expected).should == JSON.parse(io.string) + JSON.parse(io.string).should == JSON.parse(expected) + end + + it 'supports append_duration' do + io = StringIO.new + f = JSONFormatter.new(io) + f.uri("f.feature") + f.feature(Model::Feature.new([], [], "Feature", "ff", "", 1, "ff")) + f.scenario(Model::Scenario.new([], [], "Scenario", "ss", "", 2, "ff/ss")) + f.step(Model::Step.new([], "Given ", "g", 3, nil, nil)) + f.match(Model::Match.new([], "def.rb:33")) + f.result(Model::Result.new(:passed, 3, nil)) + f.append_duration(1) + f.eof + f.done + expected = %{ + [ + { + "id": "ff", + "uri": "f.feature", + "keyword": "Feature", + "name": "ff", + "line": 1, + "description": "", + "elements": [ + { + "id": "ff/ss", + "keyword": "Scenario", + "name": "ss", + "line": 2, + "description": "", + "type": "scenario", + "steps": [ + { + "keyword": "Given ", + "name": "g", + "line": 3, + "match": { + "location": "def.rb:33" + }, + "result": { + "status": "passed", + "duration": 1000000000 + } + } + ] + } + ] + } + ] + } + JSON.parse(io.string).should == JSON.parse(expected) end end end