Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bugfix: Missing matches and results in JSONFormatter output. Closes #129

.
  • Loading branch information...
commit 09668d9e2aea5d0cd0eba38ac85a6d127e393c28 1 parent 3e234cd
@aslakhellesoy aslakhellesoy authored
View
1  History.md
@@ -67,6 +67,7 @@ Previously it would be represented in JSON as:
### Bugfixes
* Java JSONFormatter produces invalid JSON ([#128](https://github.com/cucumber/gherkin/issues/128) Aslak Hellesøy)
+* Missing matches and results in JSONFormatter output ([#129](https://github.com/cucumber/gherkin/issues/129) Aslak Hellesøy)
## [2.4.21](https://github.com/cucumber/gherkin/compare/v2.4.20...v2.4.21)
View
15 java/src/main/java/gherkin/formatter/JSONFormatter.java
@@ -12,6 +12,7 @@
public class JSONFormatter implements Reporter, Formatter {
private final NiceAppendable out;
private Map<Object, Object> featureHash;
+ private int stepIndex = 0;
public JSONFormatter(Appendable out) {
this.out = new NiceAppendable(out);
@@ -29,16 +30,19 @@ public void feature(Feature feature) {
@Override
public void background(Background background) {
getFeatureElements().add(background.toMap());
+ stepIndex = 0;
}
@Override
public void scenario(Scenario scenario) {
getFeatureElements().add(scenario.toMap());
+ stepIndex = 0;
}
@Override
public void scenarioOutline(ScenarioOutline scenarioOutline) {
getFeatureElements().add(scenarioOutline.toMap());
+ stepIndex = 0;
}
@Override
@@ -53,12 +57,13 @@ public void step(Step step) {
@Override
public void match(Match match) {
- getLastStep().put("match", match.toMap());
+ getStepAt(stepIndex).put("match", match.toMap());
}
@Override
public void result(Result result) {
- getLastStep().put("result", result.toMap());
+ getStepAt(stepIndex).put("result", result.toMap());
+ stepIndex++;
}
@Override
@@ -112,7 +117,11 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
}
private Map<Object, Object> getLastStep() {
- return (Map) getSteps().get(getSteps().size() - 1);
+ return getStepAt(getSteps().size() - 1);
+ }
+
+ private Map<Object, Object> getStepAt(int index) {
+ return (Map) getSteps().get(index);
}
private List getEmbeddings() {
View
16 lib/gherkin/formatter/json_formatter.rb
@@ -29,14 +29,17 @@ def feature(feature)
def background(background)
feature_elements << background.to_hash
+ @step_index = 0
end
def scenario(scenario)
feature_elements << scenario.to_hash
+ @step_index = 0
end
def scenario_outline(scenario_outline)
feature_elements << scenario_outline.to_hash
+ @step_index = 0
end
def examples(examples)
@@ -48,11 +51,16 @@ def step(step)
end
def match(match)
- last_step['match'] = match.to_hash
+ current_steps[@step_index]['match'] = match.to_hash
end
def result(result)
- last_step['result'] = result.to_hash
+ current_steps[@step_index]['result'] = result.to_hash
+ @step_index += 1
+ end
+
+ def last_step
+ current_steps[-1]
end
def embedding(mime_type, data)
@@ -81,10 +89,6 @@ def current_steps
feature_element['steps'] ||= []
end
- def last_step
- current_steps[-1]
- end
-
def embeddings
last_step['embeddings'] ||= []
end
View
74 spec/gherkin/formatter/json_formatter_spec.rb
@@ -0,0 +1,74 @@
+require 'spec_helper'
+require 'stringio'
+require 'gherkin/formatter/json_formatter'
+require 'gherkin/formatter/model'
+
+module Gherkin
+ module Formatter
+ describe JSONFormatter do
+ it "renders results" do
+ io = StringIO.new
+ f = JSONFormatter.new(io)
+ f.uri("f.feature")
+ f.feature(Model::Feature.new([], [], "Feature", "f", "", 1))
+ f.scenario(Model::Scenario.new([], [], "Feature", "f", "", 2))
+ f.step(Model::Step.new([], "Given ", "g", 3))
+ f.step(Model::Step.new([], "When ", "w", 4))
+
+ f.match(Model::Match.new([], "def.rb:33"))
+ f.result(Model::Result.new(:passed, 1, nil))
+
+ f.match(Model::Match.new([], "def.rb:44"))
+ f.result(Model::Result.new(:passed, 1, nil))
+
+ f.eof
+
+ expected = %{
+ {
+ "keyword": "Feature",
+ "name": "f",
+ "line": 1,
+ "description": "",
+ "elements": [
+ {
+ "keyword": "Feature",
+ "name": "f",
+ "line": 2,
+ "description": "",
+ "type": "scenario",
+ "steps": [
+ {
+ "keyword": "Given ",
+ "name": "g",
+ "line": 3,
+ "match": {
+ "location": "def.rb:33"
+ },
+ "result": {
+ "status": "passed",
+ "duration": 1
+ }
+ },
+ {
+ "keyword": "When ",
+ "name": "w",
+ "line": 4,
+ "match": {
+ "location": "def.rb:44"
+ },
+ "result": {
+ "status": "passed",
+ "duration": 1
+ }
+ }
+ ]
+ }
+ ]
+ }
+ }
+
+ JSON.parse(expected).should == JSON.parse(io.string)
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.