Skip to content

Commit 4439178

Browse files
committed
Merge pull request cucumber#544 from brasmusson/pretty-formatter-test
Merge pull request cucumber#544 Test case for the PrettyFormatter Update the History.md
2 parents dbc6ce1 + 09c71de commit 4439178

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

History.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
* [Core] Throw exception when unsupported command line options are used. ([#482](https://github.com/cucumber/cucumber-jvm/pull/482), [#463](https://github.com/cucumber/cucumber-jvm/issues/463) Klaus Bayrhammer)
1818
* [Scala] Release cucumber-scala for the two most recent minor releases (currently 2.10.2 and 2.9.3) ([#432](https://github.com/cucumber/cucumber-jvm/issues/432), [#462](https://github.com/cucumber/cucumber-jvm/pull/462) Chris Turner)
1919
* [Core] JUnitFormatter: Fix indentation, hook handling and support all-steps-first execution ([#556](https://github.com/cucumber/cucumber-jvm/pull/556) Björn Rasmusson)
20-
* [Core] Make the PrettyFormatter work by revering to all-steps-first execution ([#557](https://github.com/cucumber/cucumber-jvm/pull/557) Björn Rasmusson)
20+
* [Core] Make the PrettyFormatter work by revering to all-steps-first execution ([#491](https://github.com/cucumber/cucumber-jvm/issues/491), [#557](https://github.com/cucumber/cucumber-jvm/pull/557) Björn Rasmusson)
21+
* [Core] Test case for the PrettyFormatter. ([#544](https://github.com/cucumber/cucumber-jvm/pull/544) Björn Rasmusson)
2122

2223
## [1.1.3](https://github.com/cucumber/cucumber-jvm/compare/v1.1.2...v1.1.3) (2013-03-10)
2324

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+
}

0 commit comments

Comments
 (0)