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

Let the Java JSON Formatter handle output and embeddings in hooks #289

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion java/src/main/java/gherkin/JSONParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public void parse(String src) {
formatter.uri(getString(o, "uri"));
new Feature(comments(o), tags(o), keyword(o), name(o), description(o), line(o), id(o)).replay(formatter);
for (Map featureElement : (List<Map>) getList(o, "elements")) {
featureElement(featureElement).replay(formatter);
BasicStatement element = featureElement(featureElement);
for (Map hook : (List<Map>) getList(featureElement, "before")) {
before(hook);
}
element.replay(formatter);
for (Map step : (List<Map>) getList(featureElement, "steps")) {
step(step);
}
Expand Down Expand Up @@ -75,6 +76,8 @@ private BasicStatement featureElement(Map o) {
}

private void before(Map o) {
handleEmbeddings(o);
handleOutput(o);
Map m = (Map) o.get("match");
Match match = new Match(arguments(m), location(m));
Map r = (Map) o.get("result");
Expand All @@ -83,6 +86,8 @@ private void before(Map o) {
}

private void after(Map o) {
handleEmbeddings(o);
handleOutput(o);
Map m = (Map) o.get("match");
Match match = new Match(arguments(m), location(m));
Map r = (Map) o.get("result");
Expand Down Expand Up @@ -110,11 +115,17 @@ private void step(Map o) {
new Match(arguments(m), location(m)).replay(reporter);
}

handleEmbeddings(o);

handleOutput(o);

if (o.containsKey("result")) {
Map r = (Map) o.get("result");
new Result(status(r), duration(r), errorMessage(r)).replay(reporter);
}
}

private void handleEmbeddings(Map o) {
if (o.containsKey("embeddings")) {
List<Map> embeddings = (List<Map>) o.get("embeddings");
for (Map embedding : embeddings) {
Expand All @@ -125,7 +136,9 @@ private void step(Map o) {
}
}
}
}

private void handleOutput(Map o) {
if (o.containsKey("output")) {
List<String> output = (List<String>) o.get("output");
for (String text : output) {
Expand Down
79 changes: 53 additions & 26 deletions java/src/main/java/gherkin/formatter/JSONFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class JSONFormatter implements Reporter, Formatter {

private Map<String, Object> featureMap;
private String uri;
private List<Map> beforeHooks = new ArrayList<Map>();
private Map<Object, List<Map>> beforeHookScenario = new HashMap<Object, List<Map>>();
private Map<String, Object> currentHook = new HashMap<String, Object>();

private enum Phase {step, match, embedding, output, result};

Expand Down Expand Up @@ -61,15 +62,18 @@ private enum Phase {step, match, embedding, output, result};
*/
private Map getCurrentStep(Phase phase) {
String target = phase.ordinal() <= Phase.match.ordinal()?Phase.match.name():Phase.result.name();
Map lastWithValue = null;
for (Map stepOrHook : getSteps()) {
if (stepOrHook.get(target) == null) {
return stepOrHook;
} else {
lastWithValue = stepOrHook;
Map<Object, List<Map>> currentScenario = getFeatureElement();
if (currentScenario != null) {
List<Map> steps = currentScenario.get("steps");
if (steps != null) {
for (Map step : steps) {
if (step.get(target) == null) {
return step;
}
}
}
}
return lastWithValue;
return null;
}


Expand Down Expand Up @@ -97,9 +101,9 @@ public void background(Background background) {
@Override
public void scenario(Scenario scenario) {
getFeatureElements().add(scenario.toMap());
if (beforeHooks.size() > 0) {
getFeatureElement().put("before", beforeHooks);
beforeHooks = new ArrayList<Map>();
if (beforeHookScenario.get("before") != null) {
getFeatureElement().put("before", beforeHookScenario.get("before"));
beforeHookScenario.remove("before");
}
}

Expand Down Expand Up @@ -128,12 +132,22 @@ 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);
Map currentStep = getCurrentStep(Phase.embedding);
if (currentStep != null) {
getEmbeddings(currentStep).add(embedding);
} else {
getEmbeddings(currentHook).add(embedding);
}
}

@Override
public void write(String text) {
getOutput().add(text);
Map currentStep = getCurrentStep(Phase.output);
if (currentStep != null) {
getOutput(currentStep).add(text);
} else {
getOutput(currentHook).add(text);
}
}

@Override
Expand All @@ -143,28 +157,41 @@ public void result(Result result) {

@Override
public void before(Match match, Result result) {
beforeHooks.add(buildHookMap(match,result));
addHook(beforeHookScenario, match, result, "before");
}

@Override
public void after(Match match, Result result) {
List<Map> hooks = getFeatureElement().get("after");
addHook(getFeatureElement(), match, result, "after");
}

private void addHook(final Map<Object, List<Map>> currentScenario, final Match match, final Result result, final String hook) {
List<Map> hooks = currentScenario.get(hook);
if (hooks == null) {
hooks = new ArrayList<Map>();
getFeatureElement().put("after", hooks);
currentScenario.put(hook, hooks);
}
hooks.add(buildHookMap(match,result));
}

private Map buildHookMap(final Match match, final Result result) {
final Map hookMap = new HashMap();
hookMap.put("match", match.toMap());
hookMap.put("result", result.toMap());
currentHook.put("match", match.toMap());
currentHook.put("result", result.toMap());
final Map hookMap = currentHook;
currentHook = new HashMap<String, Object>();
return hookMap;
}

public void appendDuration(final int timestamp) {
final Map result = (Map) getCurrentStep(Phase.result).get("result");
Map result = null;
for (Map step : getFeatureElement().get("steps")) {
if (step.get("result") == null) {
break;
} else {
result = (Map) step.get("result");
}
}

// check to make sure result exists (scenario outlines do not have results yet)
if (result != null) {
//convert to nanoseconds
Expand Down Expand Up @@ -240,20 +267,20 @@ private List<Map> getSteps() {
return steps;
}

private List<Map<String, String>> getEmbeddings() {
List<Map<String, String>> embeddings = (List<Map<String, String>>) getCurrentStep(Phase.embedding).get("embeddings");
private List<Map<String, String>> getEmbeddings(final Map currentStep) {
List<Map<String, String>> embeddings = (List<Map<String, String>>) currentStep.get("embeddings");
if (embeddings == null) {
embeddings = new ArrayList<Map<String, String>>();
getCurrentStep(Phase.embedding).put("embeddings", embeddings);
currentStep.put("embeddings", embeddings);
}
return embeddings;
}

private List<String> getOutput() {
List<String> output = (List<String>) getCurrentStep(Phase.output).get("output");
private List<String> getOutput(final Map currentStep) {
List<String> output = (List<String>) currentStep.get("output");
if (output == null) {
output = new ArrayList<String>();
getCurrentStep(Phase.output).put("output", output);
currentStep.put("output", output);
}
return output;
}
Expand Down
Loading