Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring, trying to fix JSON formatting

  • Loading branch information...
commit f666f65b2030b02a704bfea7361a9f1b114d58ec 1 parent 68e602e
@aslakhellesoy aslakhellesoy authored
View
3  History.md
@@ -1,6 +1,7 @@
## [Git master](https://github.com/cucumber/gherkin/compare/v2.11.1...master)
-* [Java Reporter should take embeddings as `byte[]` and not `InputStream` ([#184](https://github.com/cucumber/gherkin/issues/184) Aslak Hellesøy)
+* [Java] JSONFormatter reports results for all steps (regression in 2.11.0) ([#165](https://github.com/cucumber/gherkin/issues/165) Aslak Hellesøy)
+* [Java] Reporter should take embeddings as `byte[]` and not `InputStream` ([#184](https://github.com/cucumber/gherkin/issues/184) Aslak Hellesøy)
* [Core] A little addition to russian translation ([#183](https://github.com/cucumber/gherkin/pull/183) Sergey Sytsevich)
## [2.11.1](https://github.com/cucumber/gherkin/compare/v2.11.0...v2.11.1)
View
6 java/pom.xml
@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
- <version>2.11.1</version>
+ <version>2.11.2</version>
<packaging>bundle</packaging>
<name>Gherkin</name>
<description>Pure Java Gherkin</description>
@@ -41,6 +41,10 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
+ <prerequisites>
+ <maven>3.0.3</maven>
+ </prerequisites>
+
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
View
82 java/src/main/java/gherkin/formatter/JSONFormatter.java
@@ -23,7 +23,9 @@
private Map<String, Object> featureMap;
private String uri;
- private Map currentStepOrHook;
+ private Map<String, Object> currentStep;
+ private int stepIndex = 0;
+ private Map<String, Object> background;
public JSONFormatter(Appendable out) {
this.out = new NiceAppendable(out);
@@ -39,20 +41,24 @@ public void feature(Feature feature) {
featureMap = feature.toMap();
featureMap.put("uri", uri);
featureMaps.add(featureMap);
+ background = null;
}
@Override
public void background(Background background) {
- getFeatureElements().add(background.toMap());
+ this.background = background.toMap();
+ getFeatureElements().add(this.background);
}
@Override
public void scenario(Scenario scenario) {
+ this.background = null;
getFeatureElements().add(scenario.toMap());
}
@Override
public void scenarioOutline(ScenarioOutline scenarioOutline) {
+ this.background = null;
getFeatureElements().add(scenarioOutline.toMap());
}
@@ -63,31 +69,28 @@ public void examples(Examples examples) {
@Override
public void step(Step step) {
- currentStepOrHook = step.toMap();
- getSteps().add(currentStepOrHook);
+ getSteps().add(step.toMap());
}
@Override
public void match(Match match) {
- currentStepOrHook.put("match", match.toMap());
- }
-
- @Override
- public void embedding(String mimeType, byte[] data) {
- final Map<String, String> embedding = new HashMap<String, String>();
- embedding.put("mime_type", mimeType);
- embedding.put("data", Base64.encodeBytes(data));
- getEmbeddings().add(embedding);
+ if (background != null) {
+ throw new IllegalStateException("Can't match a background step. This is a bug in library using gherkin.");
+ }
+ currentStep = nextStep();
+ currentStep.put("match", match.toMap());
}
- @Override
- public void write(String text) {
- getOutput().add(text);
+ private Map<String, Object> nextStep() {
+ return getSteps().get(stepIndex++);
}
@Override
public void result(Result result) {
- currentStepOrHook.put("result", result.toMap());
+ if (background != null) {
+ throw new IllegalStateException("Can't add result to a background step. This is a bug in library using gherkin.");
+ }
+ currentStep.put("result", result.toMap());
}
@Override
@@ -101,9 +104,9 @@ public void after(Match match, Result result) {
}
private void addHook(final Match match, final Result result, String hook) {
- List<Map> hooks = getFeatureElement().get(hook);
+ List<Map<String, Object>> hooks = (List<Map<String, Object>>) getFeatureElement().get(hook);
if (hooks == null) {
- hooks = new ArrayList<Map>();
+ hooks = new ArrayList<Map<String, Object>>();
getFeatureElement().put(hook, hooks);
}
Map hookMap = new HashMap();
@@ -113,6 +116,19 @@ private void addHook(final Match match, final Result result, String hook) {
}
@Override
+ public void embedding(String mimeType, byte[] data) {
+ final Map<String, String> embedding = new HashMap<String, String>();
+ embedding.put("mime_type", mimeType);
+ embedding.put("data", Base64.encodeBytes(data));
+ getEmbeddings().add(embedding);
+ }
+
+ @Override
+ public void write(String text) {
+ getOutput().add(text);
+ }
+
+ @Override
public void eof() {
}
@@ -143,42 +159,46 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
return featureElements;
}
- private Map<Object, List<Map>> getFeatureElement() {
- return (Map) getFeatureElements().get(getFeatureElements().size() - 1);
+ private Map<String, Object> getFeatureElement() {
+ if (background != null) {
+ return background;
+ } else {
+ return (Map) getFeatureElements().get(getFeatureElements().size() - 1);
+ }
}
- private List<Map> getAllExamples() {
- List<Map> allExamples = getFeatureElement().get("examples");
+ private List<Map<String, Object>> getAllExamples() {
+ List<Map<String, Object>> allExamples = (List<Map<String, Object>>) getFeatureElement().get("examples");
if (allExamples == null) {
- allExamples = new ArrayList<Map>();
+ allExamples = new ArrayList<Map<String, Object>>();
getFeatureElement().put("examples", allExamples);
}
return allExamples;
}
- private List<Map> getSteps() {
- List<Map> steps = getFeatureElement().get("steps");
+ private List<Map<String, Object>> getSteps() {
+ List<Map<String, Object>> steps = (List<Map<String, Object>>) getFeatureElement().get("steps");
if (steps == null) {
- steps = new ArrayList<Map>();
+ steps = new ArrayList<Map<String, Object>>();
getFeatureElement().put("steps", steps);
}
return steps;
}
private List<Map<String, String>> getEmbeddings() {
- List<Map<String, String>> embeddings = (List<Map<String, String>>) currentStepOrHook.get("embeddings");
+ List<Map<String, String>> embeddings = (List<Map<String, String>>) currentStep.get("embeddings");
if (embeddings == null) {
embeddings = new ArrayList<Map<String, String>>();
- currentStepOrHook.put("embeddings", embeddings);
+ currentStep.put("embeddings", embeddings);
}
return embeddings;
}
private List<String> getOutput() {
- List<String> output = (List<String>) currentStepOrHook.get("output");
+ List<String> output = (List<String>) currentStep.get("output");
if (output == null) {
output = new ArrayList<String>();
- currentStepOrHook.put("output", output);
+ currentStep.put("output", output);
}
return output;
}
View
1  java/src/main/java/gherkin/formatter/PrettyFormatter.java
@@ -18,7 +18,6 @@
import gherkin.formatter.model.TagStatement;
import gherkin.util.Mapper;
-import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
View
1  java/src/main/java/gherkin/formatter/Reporter.java
@@ -9,6 +9,7 @@
* Reporter implementations (such as Cucumber's JUnitReporter) only cares about results,
*/
public interface Reporter {
+
void before(Match match, Result result);
void result(Result result);
Please sign in to comment.
Something went wrong with that request. Please try again.