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

Commit

Permalink
jruby impementation of JSONFormatter.append_duration() added
Browse files Browse the repository at this point in the history
  • Loading branch information
os97673 committed Feb 27, 2013
1 parent 1145c56 commit 95f013b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 13 deletions.
32 changes: 21 additions & 11 deletions java/src/main/java/gherkin/formatter/JSONFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,9 +40,9 @@ private enum Phase {step, match, result, embedding, output};
* result
* embedding
* output
*
* or if
*
*
* or if
*
* step
* step
* match
Expand All @@ -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){
Expand All @@ -76,8 +76,8 @@ private Map getCurrentStep(Phase phase){
}
return lastWithValue;
}


public JSONFormatter(Appendable out) {
this.out = new NiceAppendable(out);
}
Expand Down Expand Up @@ -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() {
}
Expand Down Expand Up @@ -238,4 +248,4 @@ private List<String> getOutput() {
protected Gson gson() {
return new GsonBuilder().create();
}
}
}
55 changes: 53 additions & 2 deletions spec/gherkin/formatter/json_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Formatter

f.eof
f.done

expected = %{
[
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 95f013b

Please sign in to comment.