Skip to content

Commit 39e2432

Browse files
committed
Add a test case for the PrettyFormatter
Add a test case for the PrettyFormatter to verify that the pretty formatter aligns the location strings correct, when a scenario is executed with Cucumber-JVM.
1 parent 11ac9ff commit 39e2432

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@Ignore
1414
public class TestHelper {
15-
static CucumberFeature feature(final String path, final String source) throws IOException {
15+
public static CucumberFeature feature(final String path, final String source) throws IOException {
1616
ArrayList<CucumberFeature> cucumberFeatures = new ArrayList<CucumberFeature>();
1717
FeatureBuilder featureBuilder = new FeatureBuilder(cucumberFeatures);
1818
featureBuilder.parse(new Resource() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cucumber.runtime.formatter;
2+
3+
import static cucumber.runtime.TestHelper.feature;
4+
import static java.util.Arrays.asList;
5+
import static org.hamcrest.CoreMatchers.containsString;
6+
import static org.junit.Assert.assertThat;
7+
import static org.mockito.Matchers.any;
8+
import static org.mockito.Matchers.anyString;
9+
import static org.mockito.Matchers.argThat;
10+
import static org.mockito.Mockito.mock;
11+
import static org.mockito.Mockito.when;
12+
import gherkin.I18n;
13+
import gherkin.formatter.model.Step;
14+
15+
import java.io.IOException;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.Properties;
19+
20+
import org.junit.Test;
21+
22+
import cucumber.runtime.Backend;
23+
import cucumber.runtime.Runtime;
24+
import cucumber.runtime.RuntimeGlue;
25+
import cucumber.runtime.RuntimeOptions;
26+
import cucumber.runtime.StepDefinitionMatch;
27+
import cucumber.runtime.io.ClasspathResourceLoader;
28+
import cucumber.runtime.model.CucumberFeature;
29+
30+
public class CucumberPrettyFormatterTest {
31+
32+
@Test
33+
public void should_align_the_indentation_of_location_strings() throws IOException {
34+
CucumberFeature feature = feature("path/test.feature",
35+
"Feature: feature name\n" +
36+
" Scenario: scenario name\n" +
37+
" Given first step\n" +
38+
" When second step\n" +
39+
" Then third step\n");
40+
Map<String, String> stepsToLocation = new HashMap<String, String>();
41+
stepsToLocation.put("first step", "path/step_definitions.java:3");
42+
stepsToLocation.put("second step", "path/step_definitions.java:7");
43+
stepsToLocation.put("third step", "path/step_definitions.java:11");
44+
45+
String formatterOutput = runFeatureWithPrettyFormatter(feature, stepsToLocation);
46+
47+
assertThat(formatterOutput, containsString(
48+
" Scenario: scenario name # path/test.feature:2\n" +
49+
" Given first step # path/step_definitions.java:3\n" +
50+
" When second step # path/step_definitions.java:7\n" +
51+
" Then third step # path/step_definitions.java:11\n"));
52+
}
53+
54+
private String runFeatureWithPrettyFormatter(final CucumberFeature feature, final Map<String, String> stepsToLocation) throws IOException {
55+
final RuntimeOptions runtimeOptions = new RuntimeOptions(new Properties());
56+
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
57+
final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader);
58+
final RuntimeGlue glue = createMockedRuntimeGlueThatMatchesTheSteps(stepsToLocation);
59+
final Runtime runtime = new Runtime(resourceLoader, classLoader, asList(mock(Backend.class)), runtimeOptions, glue);
60+
final StringBuilder out = new StringBuilder();
61+
final CucumberPrettyFormatter prettyFormatter = new CucumberPrettyFormatter(out);
62+
prettyFormatter.setMonochrome(true);
63+
64+
feature.run(prettyFormatter, prettyFormatter, runtime);
65+
66+
return out.toString();
67+
}
68+
69+
private RuntimeGlue createMockedRuntimeGlueThatMatchesTheSteps(Map<String, String> stepsToLocation) {
70+
RuntimeGlue glue = mock(RuntimeGlue.class);
71+
for (String stepName : stepsToLocation.keySet()) {
72+
StepDefinitionMatch matchStep = mock(StepDefinitionMatch.class);
73+
when(matchStep.getLocation()).thenReturn(stepsToLocation.get(stepName));
74+
when(glue.stepDefinitionMatch(anyString(), stepWithName(stepName), (I18n)any())).thenReturn(matchStep);
75+
}
76+
return glue;
77+
}
78+
79+
private Step stepWithName(String name) {
80+
return argThat(new StepMatcher(name));
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cucumber.runtime.formatter;
2+
3+
import gherkin.formatter.model.Step;
4+
5+
import org.mockito.ArgumentMatcher;
6+
7+
public class StepMatcher extends ArgumentMatcher<Step> {
8+
private final String nameToMatch;
9+
10+
public StepMatcher(String name) {
11+
this.nameToMatch = name;
12+
}
13+
14+
@Override
15+
public boolean matches(Object argument) {
16+
return argument instanceof Step && nameToMatch.endsWith(((Step)argument).getName());
17+
}
18+
}

0 commit comments

Comments
 (0)