Skip to content

Commit 9b4498f

Browse files
committed
[Core] Json Formatter: include the content type of doc strings
1 parent d3f85c0 commit 9b4498f

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

core/src/main/java/cucumber/runtime/formatter/JSONFormatter.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import cucumber.api.formatter.Formatter;
1717
import cucumber.api.formatter.NiceAppendable;
1818
import gherkin.ast.Background;
19+
import gherkin.ast.DocString;
1920
import gherkin.ast.Feature;
2021
import gherkin.ast.ScenarioDefinition;
2122
import gherkin.ast.Step;
@@ -231,15 +232,15 @@ private Map<String, Object> createTestStep(TestStep testStep) {
231232
Map<String, Object> stepMap = new HashMap<String, Object>();
232233
stepMap.put("name", testStep.getStepText());
233234
stepMap.put("line", testStep.getStepLine());
235+
TestSourcesModel.AstNode astNode = testSources.getAstNode(currentFeatureFile, testStep.getStepLine());
234236
if (!testStep.getStepArgument().isEmpty()) {
235237
Argument argument = testStep.getStepArgument().get(0);
236238
if (argument instanceof PickleString) {
237-
stepMap.put("doc_string", createDocStringMap(argument));
239+
stepMap.put("doc_string", createDocStringMap(argument, astNode));
238240
} else if (argument instanceof PickleTable) {
239241
stepMap.put("rows", createDataTableList(argument));
240242
}
241243
}
242-
TestSourcesModel.AstNode astNode = testSources.getAstNode(currentFeatureFile, testStep.getStepLine());
243244
if (astNode != null) {
244245
Step step = (Step) astNode.node;
245246
stepMap.put("keyword", step.getKeyword());
@@ -248,11 +249,14 @@ private Map<String, Object> createTestStep(TestStep testStep) {
248249
return stepMap;
249250
}
250251

251-
private Map<String, Object> createDocStringMap(Argument argument) {
252+
private Map<String, Object> createDocStringMap(Argument argument, TestSourcesModel.AstNode astNode) {
252253
Map<String, Object> docStringMap = new HashMap<String, Object>();
253254
PickleString docString = ((PickleString)argument);
254255
docStringMap.put("value", docString.getContent());
255256
docStringMap.put("line", docString.getLocation().getLine());
257+
if (astNode != null) {
258+
docStringMap.put("content_type", ((DocString)((Step)astNode.node).getArgument()).getContentType());
259+
}
256260
return docStringMap;
257261
}
258262

core/src/test/java/cucumber/runtime/formatter/JSONFormatterTest.java

+63
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,69 @@ public void should_format_scenario_with_a_step_with_a_doc_string() throws Throwa
769769
assertPrettyJsonEquals(expected, formatterOutput);
770770
}
771771

772+
@Test
773+
public void should_format_scenario_with_a_step_with_a_doc_string_and_content_type() throws Throwable {
774+
CucumberFeature feature = TestHelper.feature("path/test.feature", "" +
775+
"Feature: Banana party\n" +
776+
"\n" +
777+
" Scenario: Monkey eats bananas\n" +
778+
" Given there are bananas\n" +
779+
" \"\"\"doc\n" +
780+
" doc string content\n" +
781+
" \"\"\"\n");
782+
Map<String, Result> stepsToResult = new HashMap<String, Result>();
783+
stepsToResult.put("there are bananas", result("passed"));
784+
Map<String, String> stepsToLocation = new HashMap<String, String>();
785+
stepsToLocation.put("there are bananas", "StepDefs.there_are_bananas()");
786+
Long stepDuration = milliSeconds(1);
787+
788+
String formatterOutput = runFeatureWithJSONPrettyFormatter(feature, stepsToResult, stepsToLocation, stepDuration);
789+
790+
String expected = "" +
791+
"[\n" +
792+
" {\n" +
793+
" \"id\": \"banana-party\",\n" +
794+
" \"uri\": \"path/test.feature\",\n" +
795+
" \"keyword\": \"Feature\",\n" +
796+
" \"name\": \"Banana party\",\n" +
797+
" \"line\": 1,\n" +
798+
" \"description\": \"\",\n" +
799+
" \"elements\": [\n" +
800+
" {\n" +
801+
" \"id\": \"banana-party;monkey-eats-bananas\",\n" +
802+
" \"keyword\": \"Scenario\",\n" +
803+
" \"name\": \"Monkey eats bananas\",\n" +
804+
" \"line\": 3,\n" +
805+
" \"description\": \"\",\n" +
806+
" \"type\": \"scenario\",\n" +
807+
" \"steps\": [\n" +
808+
" {\n" +
809+
" \"keyword\": \"Given \",\n" +
810+
" \"name\": \"there are bananas\",\n" +
811+
" \"line\": 4,\n" +
812+
" \"doc_string\": {\n" +
813+
" \"content_type\": \"doc\",\n" +
814+
" \"value\": \"doc string content\",\n" +
815+
" \"line\": 5\n" +
816+
" },\n" +
817+
" \"match\": {\n" +
818+
" \"location\": \"StepDefs.there_are_bananas()\"\n" +
819+
" },\n" +
820+
" \"result\": {\n" +
821+
" \"status\": \"passed\",\n" +
822+
" \"duration\": 1000000\n" +
823+
" }\n" +
824+
" }\n" +
825+
" ]\n" +
826+
" }\n" +
827+
" ],\n" +
828+
" \"tags\": []\n" +
829+
" }\n" +
830+
"]";
831+
assertPrettyJsonEquals(expected, formatterOutput);
832+
}
833+
834+
772835
@Test
773836
public void should_format_scenario_with_a_step_with_a_data_table() throws Throwable {
774837
CucumberFeature feature = TestHelper.feature("path/test.feature", "" +

0 commit comments

Comments
 (0)