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