Skip to content

Commit 8e232b4

Browse files
committed
Add tests to specify the formatter call sequences.
Add tests to specify the differences between the command line runner and the JUnit runner, with respect to the calls the formatters experiences.
1 parent 488222d commit 8e232b4

File tree

6 files changed

+561
-34
lines changed

6 files changed

+561
-34
lines changed

core/src/test/java/cucumber/runtime/RuntimeTest.java

+134
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cucumber.api.Scenario;
55
import cucumber.api.StepDefinitionReporter;
66
import cucumber.runtime.formatter.CucumberJSONFormatter;
7+
import cucumber.runtime.formatter.FormatterSpy;
78
import cucumber.runtime.io.ClasspathResourceLoader;
89
import cucumber.runtime.io.Resource;
910
import cucumber.runtime.io.ResourceLoader;
@@ -22,10 +23,13 @@
2223
import java.io.ByteArrayOutputStream;
2324
import java.io.IOException;
2425
import java.io.PrintStream;
26+
import java.util.AbstractMap.SimpleEntry;
2527
import java.util.Arrays;
2628
import java.util.Collection;
2729
import java.util.Collections;
30+
import java.util.HashMap;
2831
import java.util.List;
32+
import java.util.Map;
2933

3034
import static cucumber.runtime.TestHelper.feature;
3135
import static java.util.Arrays.asList;
@@ -391,6 +395,136 @@ public void should_make_scenario_id_available_to_hooks() throws Throwable {
391395
assertEquals("feature-name;scenario-name", capturedScenario.getValue().getId());
392396
}
393397

398+
@Test
399+
public void should_call_formatter_for_two_scenarios_with_background() throws Throwable {
400+
CucumberFeature feature = TestHelper.feature("path/test.feature", "" +
401+
"Feature: feature name\n" +
402+
" Background: background\n" +
403+
" Given first step\n" +
404+
" Scenario: scenario_1 name\n" +
405+
" When second step\n" +
406+
" Then third step\n" +
407+
" Scenario: scenario_2 name\n" +
408+
" Then second step\n");
409+
Map<String, String> stepsToResult = new HashMap<String, String>();
410+
stepsToResult.put("first step", "passed");
411+
stepsToResult.put("second step", "passed");
412+
stepsToResult.put("third step", "passed");
413+
414+
String formatterOutput = runFeatureWithFormatterSpy(feature, stepsToResult);
415+
416+
assertEquals("" +
417+
"uri\n" +
418+
"feature\n" +
419+
" startOfScenarioLifeCycle\n" +
420+
" background\n" +
421+
" step\n" +
422+
" match\n" +
423+
" result\n" +
424+
" scenario\n" +
425+
" step\n" +
426+
" step\n" +
427+
" match\n" +
428+
" result\n" +
429+
" match\n" +
430+
" result\n" +
431+
" endOfScenarioLifeCycle\n" +
432+
" startOfScenarioLifeCycle\n" +
433+
" background\n" +
434+
" step\n" +
435+
" match\n" +
436+
" result\n" +
437+
" scenario\n" +
438+
" step\n" +
439+
" match\n" +
440+
" result\n" +
441+
" endOfScenarioLifeCycle\n" +
442+
"eof\n" +
443+
"done\n" +
444+
"close\n", formatterOutput);
445+
}
446+
447+
@Test
448+
public void should_call_formatter_for_scenario_outline_with_two_examples_table_and_background() throws Throwable {
449+
CucumberFeature feature = TestHelper.feature("path/test.feature", "" +
450+
"Feature: feature name\n" +
451+
" Background: background\n" +
452+
" Given first step\n" +
453+
" Scenario Outline: scenario outline name\n" +
454+
" When <x> step\n" +
455+
" Then <y> step\n" +
456+
" Examples: examples 1 name\n" +
457+
" | x | y |\n" +
458+
" | second | third |\n" +
459+
" | second | third |\n" +
460+
" Examples: examples 2 name\n" +
461+
" | x | y |\n" +
462+
" | second | third |\n");
463+
Map<String, String> stepsToResult = new HashMap<String, String>();
464+
stepsToResult.put("first step", "passed");
465+
stepsToResult.put("second step", "passed");
466+
stepsToResult.put("third step", "passed");
467+
468+
String formatterOutput = runFeatureWithFormatterSpy(feature, stepsToResult);
469+
470+
assertEquals("" +
471+
"uri\n" +
472+
"feature\n" +
473+
" scenarioOutline\n" +
474+
" step\n" +
475+
" step\n" +
476+
" examples\n" +
477+
" startOfScenarioLifeCycle\n" +
478+
" background\n" +
479+
" step\n" +
480+
" match\n" +
481+
" result\n" +
482+
" scenario\n" +
483+
" step\n" +
484+
" step\n" +
485+
" match\n" +
486+
" result\n" +
487+
" match\n" +
488+
" result\n" +
489+
" endOfScenarioLifeCycle\n" +
490+
" startOfScenarioLifeCycle\n" +
491+
" background\n" +
492+
" step\n" +
493+
" match\n" +
494+
" result\n" +
495+
" scenario\n" +
496+
" step\n" +
497+
" step\n" +
498+
" match\n" +
499+
" result\n" +
500+
" match\n" +
501+
" result\n" +
502+
" endOfScenarioLifeCycle\n" +
503+
" examples\n" +
504+
" startOfScenarioLifeCycle\n" +
505+
" background\n" +
506+
" step\n" +
507+
" match\n" +
508+
" result\n" +
509+
" scenario\n" +
510+
" step\n" +
511+
" step\n" +
512+
" match\n" +
513+
" result\n" +
514+
" match\n" +
515+
" result\n" +
516+
" endOfScenarioLifeCycle\n" +
517+
"eof\n" +
518+
"done\n" +
519+
"close\n", formatterOutput);
520+
}
521+
522+
private String runFeatureWithFormatterSpy(CucumberFeature feature, Map<String, String> stepsToResult) throws Throwable {
523+
FormatterSpy formatterSpy = new FormatterSpy();
524+
TestHelper.runFeatureWithFormatter(feature, stepsToResult, Collections.<SimpleEntry<String, String>>emptyList(), 0L, formatterSpy, formatterSpy);
525+
return formatterSpy.toString();
526+
}
527+
394528
private StepDefinitionMatch createExceptionThrowingMatch(Exception exception) throws Throwable {
395529
StepDefinitionMatch match = mock(StepDefinitionMatch.class);
396530
doThrow(exception).when(match).runStep((I18n) any());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package cucumber.runtime.formatter;
2+
3+
import gherkin.formatter.Formatter;
4+
import gherkin.formatter.Reporter;
5+
import gherkin.formatter.model.Background;
6+
import gherkin.formatter.model.Examples;
7+
import gherkin.formatter.model.Feature;
8+
import gherkin.formatter.model.Match;
9+
import gherkin.formatter.model.Result;
10+
import gherkin.formatter.model.Scenario;
11+
import gherkin.formatter.model.ScenarioOutline;
12+
import gherkin.formatter.model.Step;
13+
14+
import java.util.List;
15+
16+
17+
public class FormatterSpy implements Formatter, Reporter {
18+
StringBuilder calls = new StringBuilder();
19+
20+
@Override
21+
public void after(Match arg0, Result arg1) {
22+
calls.append("after\n");
23+
}
24+
25+
@Override
26+
public void before(Match arg0, Result arg1) {
27+
calls.append("before\n");
28+
}
29+
30+
@Override
31+
public void embedding(String arg0, byte[] arg1) {
32+
calls.append(" embedding\n");
33+
}
34+
35+
@Override
36+
public void match(Match arg0) {
37+
calls.append(" match\n");
38+
}
39+
40+
@Override
41+
public void result(Result arg0) {
42+
calls.append(" result\n");
43+
}
44+
45+
@Override
46+
public void write(String arg0) {
47+
calls.append(" write\n");
48+
}
49+
50+
@Override
51+
public void background(Background arg0) {
52+
calls.append(" background\n");
53+
}
54+
55+
@Override
56+
public void close() {
57+
calls.append("close\n");
58+
}
59+
60+
@Override
61+
public void done() {
62+
calls.append("done\n");
63+
}
64+
65+
@Override
66+
public void endOfScenarioLifeCycle(Scenario arg0) {
67+
calls.append(" endOfScenarioLifeCycle\n");
68+
}
69+
70+
@Override
71+
public void eof() {
72+
calls.append("eof\n");
73+
}
74+
75+
@Override
76+
public void examples(Examples arg0) {
77+
calls.append(" examples\n");
78+
}
79+
80+
@Override
81+
public void feature(Feature arg0) {
82+
calls.append("feature\n");
83+
}
84+
85+
@Override
86+
public void scenario(Scenario arg0) {
87+
calls.append(" scenario\n");
88+
}
89+
90+
@Override
91+
public void scenarioOutline(ScenarioOutline arg0) {
92+
calls.append(" scenarioOutline\n");
93+
}
94+
95+
@Override
96+
public void startOfScenarioLifeCycle(Scenario arg0) {
97+
calls.append(" startOfScenarioLifeCycle\n");
98+
}
99+
100+
@Override
101+
public void step(Step arg0) {
102+
calls.append(" step\n");
103+
}
104+
105+
@Override
106+
public void syntaxError(String arg0, String arg1, List<String> arg2,
107+
String arg3, Integer arg4) {
108+
calls.append("syntaxError\n");
109+
}
110+
111+
@Override
112+
public void uri(String arg0) {
113+
calls.append("uri\n");
114+
}
115+
116+
@Override
117+
public String toString() {
118+
return calls.toString();
119+
}
120+
}

junit/src/test/java/cucumber/runtime/junit/ExecutionUnitRunnerTest.java

+1-34
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void shouldIncludeScenarioNameAsClassNameInStepDescriptions() throws Exce
7575

7676
@Test
7777
public void shouldPopulateRunnerStepsWithStepsUsedInStepDescriptions() throws Exception {
78-
CucumberFeature cucumberFeature = feature("featurePath", "" +
78+
CucumberFeature cucumberFeature = TestFeatureBuilder.feature("featurePath", "" +
7979
"Feature: feature name\n" +
8080
" Background:\n" +
8181
" Given background step\n" +
@@ -105,37 +105,4 @@ private void assertDescriptionHasStepAsUniqueId(Description stepDescription, Ste
105105
assertEquals(stepDescription, Description.createTestDescription("", "", step));
106106
}
107107

108-
private CucumberFeature feature(final String path, final String source) throws IOException {
109-
ArrayList<CucumberFeature> cucumberFeatures = new ArrayList<CucumberFeature>();
110-
FeatureBuilder featureBuilder = new FeatureBuilder(cucumberFeatures);
111-
featureBuilder.parse(new Resource() {
112-
@Override
113-
public String getPath() {
114-
return path;
115-
}
116-
117-
@Override
118-
public String getAbsolutePath() {
119-
throw new UnsupportedOperationException();
120-
}
121-
122-
@Override
123-
public InputStream getInputStream() {
124-
try {
125-
return new ByteArrayInputStream(source.getBytes("UTF-8"));
126-
} catch (UnsupportedEncodingException e) {
127-
throw new RuntimeException(e);
128-
}
129-
}
130-
131-
@Override
132-
public String getClassName(String extension) {
133-
throw new UnsupportedOperationException();
134-
}
135-
}, new ArrayList<Object>());
136-
featureBuilder.close();
137-
return cucumberFeatures.get(0);
138-
}
139-
140-
141108
}

0 commit comments

Comments
 (0)