Skip to content

Call all formatters, also when using the IntelliJ's internal formatter. #842

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion core/src/main/java/cucumber/runtime/RuntimeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,14 @@ public <T> T pluginProxy(ClassLoader classLoader, final Class<T> type) {
public Object invoke(Object target, Method method, Object[] args) throws Throwable {
for (Object plugin : getPlugins()) {
if (type.isInstance(plugin)) {
Utils.invoke(plugin, method, 0, args);
try {
Utils.invoke(plugin, method, 0, args);
} catch (Throwable t) {
if (!method.getName().equals("startOfScenarioLifeCycle") && !method.getName().equals("endOfScenarioLifeCycle")) {
// IntelliJ has its own formatter which doesn't yet implement these methods.
throw t;
}
}
}
}
return null;
Expand Down
12 changes: 2 additions & 10 deletions core/src/main/java/cucumber/runtime/model/CucumberScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,15 @@ public CucumberBackground getCucumberBackground() {
public void run(Formatter formatter, Reporter reporter, Runtime runtime) {
Set<Tag> tags = tagsAndInheritedTags();
runtime.buildBackendWorlds(reporter, tags, scenario);
try {
formatter.startOfScenarioLifeCycle((Scenario) getGherkinModel());
} catch (Throwable ignore) {
// IntelliJ has its own formatter which doesn't yet implement this.
}
formatter.startOfScenarioLifeCycle((Scenario) getGherkinModel());
runtime.runBeforeHooks(reporter, tags);

runBackground(formatter, reporter, runtime);
format(formatter);
runSteps(reporter, runtime);

runtime.runAfterHooks(reporter, tags);
try {
formatter.endOfScenarioLifeCycle((Scenario) getGherkinModel());
} catch (Throwable ignore) {
// IntelliJ has its own formatter which doesn't yet implement this.
}
formatter.endOfScenarioLifeCycle((Scenario) getGherkinModel());
runtime.disposeBackendWorlds();
}

Expand Down
124 changes: 122 additions & 2 deletions core/src/test/java/cucumber/runtime/RuntimeOptionsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package cucumber.runtime;

import gherkin.formatter.Reporter;
import gherkin.formatter.model.Background;
import gherkin.formatter.model.Examples;
import gherkin.formatter.model.Feature;
import gherkin.formatter.model.Match;
import gherkin.formatter.model.Result;
import gherkin.formatter.model.ScenarioOutline;
import gherkin.formatter.model.Step;
import cucumber.runtime.formatter.FormatterSpy;
import cucumber.api.SnippetType;
import cucumber.runtime.formatter.ColorAware;
import cucumber.runtime.formatter.PluginFactory;
Expand All @@ -13,10 +22,9 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -296,6 +304,35 @@ public void applies_line_filters_only_to_own_feature() throws Exception {
assertOnlyScenarioName(features.get(1), "scenario_2_2");
}

@Test
public void handles_formatters_missing_startOfScenarioLifeCycle_endOfScenarioLifeCycle() throws Throwable {
CucumberFeature feature = TestHelper.feature("path/test.feature", "" +
"Feature: feature name\n" +
" Scenario: scenario name\n" +
" Given step\n");

FormatterSpy formatterSpy = new FormatterSpy();
RuntimeOptions runtimeOptions = new RuntimeOptions("");
runtimeOptions.addPlugin(new FormatterMissingLifecycleMethods());
runtimeOptions.addPlugin(formatterSpy);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
TestHelper.runFeatureWithFormatter(feature, new HashMap<String, String>(),
runtimeOptions.formatter(classLoader), runtimeOptions.reporter(classLoader));

assertEquals("" +
"uri\n" +
"feature\n" +
" startOfScenarioLifeCycle\n" +
" scenario\n" +
" step\n" +
" match\n" +
" result\n" +
" endOfScenarioLifeCycle\n" +
"eof\n" +
"done\n" +
"close\n", formatterSpy.toString());
}

private void assertOnlyScenarioName(CucumberFeature feature, String scenarioName) {
assertEquals("Wrong number of scenarios loaded for feature", 1, feature.getFeatureElements().size());
assertEquals("Scenario: " + scenarioName, feature.getFeatureElements().get(0).getVisualName());
Expand All @@ -309,3 +346,86 @@ private void mockResource(ResourceLoader resourceLoader, String featurePath, Str
when(resourceLoader.resources(featurePath, ".feature")).thenReturn(asList(resource1));
}
}

class FormatterMissingLifecycleMethods implements Formatter, Reporter {
@Override
public void startOfScenarioLifeCycle(gherkin.formatter.model.Scenario arg0) {
throw new NoSuchMethodError(); // simulate that this method is not implemented
}

@Override
public void endOfScenarioLifeCycle(gherkin.formatter.model.Scenario arg0) {
throw new NoSuchMethodError(); // simulate that this method is not implemented
}

@Override
public void after(Match arg0, Result arg1) {
}

@Override
public void before(Match arg0, Result arg1) {
}

@Override
public void embedding(String arg0, byte[] arg1) {
}

@Override
public void match(Match arg0) {
}

@Override
public void result(Result arg0) {
}

@Override
public void write(String arg0) {
}

@Override
public void background(Background arg0) {
}

@Override
public void close() {
}

@Override
public void done() {
}

@Override
public void eof() {
}

@Override
public void examples(Examples arg0) {
}

@Override
public void feature(Feature arg0) {

}

@Override
public void scenario(gherkin.formatter.model.Scenario arg0) {

}

@Override
public void scenarioOutline(ScenarioOutline arg0) {
}

@Override
public void step(Step arg0) {
}

@Override
public void syntaxError(String arg0, String arg1, List<String> arg2, String arg3, Integer arg4) {
}

@Override
public void uri(String arg0) {
}

}