Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More work on Java JSON Formatter. Failing one spec.

  • Loading branch information...
commit be38391ff30ed05bb136e9e365db2e9647f3199f 1 parent db984c2
@aslakhellesoy aslakhellesoy authored
View
132 features/.cucumber/stepdefs.json
@@ -1,243 +1,243 @@
[
{
- "file_colon_line": "features/step_definitions/eyeball_steps.rb:1",
- "flags": "",
"source": "^they have arrived$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/eyeball_steps.rb:1",
"steps": [
{
+ "name": "they have arrived",
"args": [
- ],
- "name": "they have arrived"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/gherkin_steps.rb:1",
- "flags": "",
"source": "^a \"(ruby|native)\" \"([^\\\"]*)\" parser$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/gherkin_steps.rb:1",
"steps": [
]
},
{
- "file_colon_line": "features/step_definitions/gherkin_steps.rb:5",
- "flags": "",
"source": "^the\\ following\\ text\\ is\\ parsed:$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/gherkin_steps.rb:5",
"steps": [
{
+ "name": "the following text is parsed:",
"args": [
- ],
- "name": "the following text is parsed:"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/gherkin_steps.rb:9",
- "flags": "",
"source": "^there\\ should\\ be\\ no\\ parse\\ errors$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/gherkin_steps.rb:9",
"steps": [
{
+ "name": "there should be no parse errors",
"args": [
- ],
- "name": "there should be no parse errors"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/gherkin_steps.rb:13",
- "flags": "",
"source": "^there should be a parse error on (line \\d+)$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/gherkin_steps.rb:13",
"steps": [
{
+ "name": "there should be a parse error on line 1",
"args": [
{
"offset": 33,
"val": "line 1"
}
- ],
- "name": "there should be a parse error on line 1"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/gherkin_steps.rb:17",
- "flags": "",
"source": "^there should be parse errors on (lines .*)$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/gherkin_steps.rb:17",
"steps": [
{
+ "name": "there should be parse errors on lines 1 and 2",
"args": [
{
"offset": 32,
"val": "lines 1 and 2"
}
- ],
- "name": "there should be parse errors on lines 1 and 2"
+ ]
},
{
+ "name": "there should be parse errors on lines 1 through 3",
"args": [
{
"offset": 32,
"val": "lines 1 through 3"
}
- ],
- "name": "there should be parse errors on lines 1 through 3"
+ ]
},
{
+ "name": "there should be parse errors on lines 10 and 17",
"args": [
{
"offset": 32,
"val": "lines 10 and 17"
}
- ],
- "name": "there should be parse errors on lines 10 and 17"
+ ]
},
{
+ "name": "there should be parse errors on lines 3 and 5",
"args": [
{
"offset": 32,
"val": "lines 3 and 5"
}
- ],
- "name": "there should be parse errors on lines 3 and 5"
+ ]
},
{
+ "name": "there should be parse errors on lines 5, 10 and 12",
"args": [
{
"offset": 32,
"val": "lines 5, 10 and 12"
}
- ],
- "name": "there should be parse errors on lines 5, 10 and 12"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/json_formatter_steps.rb:13",
- "flags": "",
"source": "^a JSON formatter$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/json_formatter_steps.rb:13",
"steps": [
]
},
{
- "file_colon_line": "features/step_definitions/json_formatter_steps.rb:18",
- "flags": "",
"source": "^the outputted JSON should be:$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/json_formatter_steps.rb:18",
"steps": [
{
+ "name": "the outputted JSON should be:",
"args": [
- ],
- "name": "the outputted JSON should be:"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/json_parser_steps.rb:5",
- "flags": "",
"source": "^a PrettyFormatter$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/json_parser_steps.rb:5",
"steps": [
]
},
{
- "file_colon_line": "features/step_definitions/json_parser_steps.rb:10",
- "flags": "",
"source": "^a JSON lexer$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/json_parser_steps.rb:10",
"steps": [
]
},
{
- "file_colon_line": "features/step_definitions/json_parser_steps.rb:14",
- "flags": "",
"source": "^the following JSON is parsed:$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/json_parser_steps.rb:14",
"steps": [
{
+ "name": "the following JSON is parsed:",
"args": [
- ],
- "name": "the following JSON is parsed:"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/json_parser_steps.rb:18",
- "flags": "",
"source": "^the outputted text should be:$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/json_parser_steps.rb:18",
"steps": [
{
+ "name": "the outputted text should be:",
"args": [
- ],
- "name": "the outputted text should be:"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:50",
- "flags": "",
"source": "^I have Cucumber's source code next to Gherkin's$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:50",
"steps": [
{
+ "name": "I have Cucumber's source code next to Gherkin's",
"args": [
- ],
- "name": "I have Cucumber's source code next to Gherkin's"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:55",
- "flags": "",
"source": "^I find all of the \\.feature files$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:55",
"steps": [
{
+ "name": "I find all of the .feature files",
"args": [
- ],
- "name": "I find all of the .feature files"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:59",
- "flags": "",
"source": "^I send each prettified original through the \"([^\"]*)\" machinery$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:59",
"steps": [
{
+ "name": "I send each prettified original through the \"json\" machinery",
"args": [
{
"offset": 45,
"val": "json"
}
- ],
- "name": "I send each prettified original through the \"json\" machinery"
+ ]
},
{
+ "name": "I send each prettified original through the \"pretty\" machinery",
"args": [
{
"offset": 45,
"val": "pretty"
}
- ],
- "name": "I send each prettified original through the \"pretty\" machinery"
+ ]
}
]
},
{
- "file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:83",
- "flags": "",
"source": "^the machinery output should be identical to the prettified original$",
+ "flags": "",
+ "file_colon_line": "features/step_definitions/pretty_formatter_steps.rb:83",
"steps": [
{
+ "name": "the machinery output should be identical to the prettified original",
"args": [
- ],
- "name": "the machinery output should be identical to the prettified original"
+ ]
}
]
}
View
10 java/src/main/java/gherkin/JSONParser.java
@@ -114,11 +114,6 @@ private void step(Map o) {
new Match(arguments(m), location(m)).replay(reporter);
}
- if (o.containsKey("result")) {
- Map r = (Map) o.get("result");
- new Result(status(r), duration(r), errorMessage(r)).replay(reporter);
- }
-
if (o.containsKey("embeddings")) {
List<Map> embeddings = (List<Map>) o.get("embeddings");
for (Map embedding : embeddings) {
@@ -136,6 +131,11 @@ private void step(Map o) {
reporter.write(text);
}
}
+
+ if (o.containsKey("result")) {
+ Map r = (Map) o.get("result");
+ new Result(status(r), duration(r), errorMessage(r)).replay(reporter);
+ }
}
private List<DataTableRow> dataTableRows(List o) {
View
101 java/src/main/java/gherkin/formatter/JSONFormatter.java
@@ -17,16 +17,14 @@
import java.util.List;
import java.util.Map;
-public class JSONFormatter implements Reporter, Formatter {
+public class JSONFormatter implements Formatter, Reporter {
private final List<Map<String, Object>> featureMaps = new ArrayList<Map<String, Object>>();
private final NiceAppendable out;
private Map<String, Object> featureMap;
private String uri;
- private Map<String, Object> currentStep;
- private int stepIndex = 0;
private Map<String, Object> background;
- private Match currentMatch;
+ private List<Map<String, Object>> steps = new ArrayList<Map<String, Object>>();
public JSONFormatter(Appendable out) {
this.out = new NiceAppendable(out);
@@ -70,30 +68,41 @@ public void examples(Examples examples) {
@Override
public void step(Step step) {
- getSteps().add(step.toMap());
+ Map<String, Object> stepMap = step.toMap();
+ steps.add(stepMap);
+ getSteps().add(stepMap);
}
@Override
- public void match(Match match) {
- if (background != null) {
- currentMatch = match;
- } else {
- currentStep = nextStep();
- currentStep.put("match", match.toMap());
- }
+ public void eof() {
}
- private Map<String, Object> nextStep() {
- return getSteps().get(stepIndex++);
+ @Override
+ public void done() {
+ out.append(gson().toJson(featureMaps));
+ // We're *not* closing the stream here.
+ // https://github.com/cucumber/gherkin/issues/151
+ // https://github.com/cucumber/cucumber-jvm/issues/96
+ }
+
+ @Override
+ public void close() {
+ out.close();
+ }
+
+ @Override
+ public void syntaxError(String state, String event, List<String> legalEvents, String uri, Integer line) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void match(Match match) {
+ steps.get(0).put("match", match.toMap());
}
@Override
public void result(Result result) {
- if (background != null) {
- hook("background", currentMatch, result);
- } else {
- currentStep.put("result", result.toMap());
- }
+ steps.remove(0).put("result", result.toMap());
}
@Override
@@ -118,31 +127,27 @@ public void embedding(String mimeType, byte[] data) {
getEmbeddings().add(embedding);
}
- @Override
- public void write(String text) {
- getOutput().add(text);
- }
-
- @Override
- public void eof() {
- }
-
- @Override
- public void done() {
- out.append(gson().toJson(featureMaps));
- // We're *not* closing the stream here.
- // https://github.com/cucumber/gherkin/issues/151
- // https://github.com/cucumber/cucumber-jvm/issues/96
+ private List<Map<String, String>> getEmbeddings() {
+ List<Map<String, String>> embeddings = (List<Map<String, String>>) steps.get(0).get("embeddings");
+ if (embeddings == null) {
+ embeddings = new ArrayList<Map<String, String>>();
+ steps.get(0).put("embeddings", embeddings);
+ }
+ return embeddings;
}
@Override
- public void close() {
- out.close();
+ public void write(String text) {
+ getOutput().add(text);
}
- @Override
- public void syntaxError(String state, String event, List<String> legalEvents, String uri, Integer line) {
- throw new UnsupportedOperationException();
+ private List<String> getOutput() {
+ List<String> output = (List<String>) steps.get(0).get("output");
+ if (output == null) {
+ output = new ArrayList<String>();
+ steps.get(0).put("output", output);
+ }
+ return output;
}
private List<Map<String, Object>> getFeatureElements() {
@@ -180,24 +185,6 @@ public void syntaxError(String state, String event, List<String> legalEvents, St
return steps;
}
- private List<Map<String, String>> getEmbeddings() {
- List<Map<String, String>> embeddings = (List<Map<String, String>>) currentStep.get("embeddings");
- if (embeddings == null) {
- embeddings = new ArrayList<Map<String, String>>();
- currentStep.put("embeddings", embeddings);
- }
- return embeddings;
- }
-
- private List<String> getOutput() {
- List<String> output = (List<String>) currentStep.get("output");
- if (output == null) {
- output = new ArrayList<String>();
- currentStep.put("output", output);
- }
- return output;
- }
-
protected Gson gson() {
return new GsonBuilder().create();
}
View
3  java/src/main/java/gherkin/formatter/model/Examples.java
@@ -30,6 +30,7 @@ public Builder(List<Comment> comments, List<Tag> tags, String keyword, String na
this.id = id;
}
+ @Override
public void row(List<Comment> comments, List<String> cells, Integer line, String id) {
if (rows == null) {
rows = new ArrayList<ExamplesTableRow>();
@@ -37,10 +38,12 @@ public void row(List<Comment> comments, List<String> cells, Integer line, String
rows.add(new ExamplesTableRow(comments, cells, line, id));
}
+ @Override
public void replay(Formatter formatter) {
new Examples(comments, tags, keyword, name, description, line, id, rows).replay(formatter);
}
+ @Override
public void docString(DocString docString) {
throw new IllegalStateException("Can't have DocString in Examples");
}
View
3  java/src/main/java/gherkin/formatter/model/Step.java
@@ -30,6 +30,7 @@ public Builder(List<Comment> comments, String keyword, String name, Integer line
this.line = line;
}
+ @Override
public void row(List<Comment> comments, List<String> cells, Integer line, String id) {
if (rows == null) {
rows = new ArrayList<DataTableRow>();
@@ -37,10 +38,12 @@ public void row(List<Comment> comments, List<String> cells, Integer line, String
rows.add(new DataTableRow(comments, cells, line));
}
+ @Override
public void replay(Formatter formatter) {
new Step(comments, keyword, name, line, rows, doc_string).replay(formatter);
}
+ @Override
public void docString(DocString docString) {
doc_string = docString;
}
View
11 lib/gherkin/formatter/json_formatter.rb
@@ -19,10 +19,6 @@ def initialize(io)
@current_step_or_hook = nil
end
- def done
- @io.write(@feature_hashes.to_json)
- end
-
def uri(uri)
@uri = uri
end
@@ -78,6 +74,13 @@ def write(text)
def eof
end
+ def done
+ end
+
+ def close
+ @io.write(@feature_hashes.to_json)
+ end
+
private
def feature_elements
View
15 spec/gherkin/formatter/json_formatter_spec.rb
@@ -14,12 +14,7 @@ module Formatter
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, 1, nil))
f.step(Model::Step.new([], "When ", "w", 4, nil, nil))
- f.match(Model::Match.new([], "def.rb:44"))
- f.result(Model::Result.new(:passed, 1, nil))
- f.hook('after', Model::Match.new([], "def.rb:55"), Model::Result.new(:passed, 22, nil))
# f.scenario_outline(Model::ScenarioOutline.new([], [], "Scenario Outline", "so", "", 102, "ff/so"))
# f.step(Model::Step.new([], "Given ", "a <foo> and <bar>", 103, nil, nil))
@@ -29,6 +24,16 @@ module Formatter
f.eof
f.done
+
+ 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.hook('after', Model::Match.new([], "def.rb:55"), Model::Result.new(:passed, 22, nil))
+
+ f.close
expected = %{
[
Please sign in to comment.
Something went wrong with that request. Please try again.