Permalink
Browse files

Dominic: expose scenario name as part of cucumber.api.Scenario

  • Loading branch information...
1 parent 72c7fc7 commit 9b3e174e8f75b2ab35c041a4ac3079501950fb4d @poetix poetix committed Feb 20, 2014
View
@@ -45,4 +45,6 @@
* @param text what to put in the report.
*/
void write(String text);
+
+ String getName();
}
@@ -3,7 +3,6 @@
import cucumber.api.Pending;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.model.CucumberFeature;
-import cucumber.runtime.model.CurrentScenario;
import cucumber.runtime.xstream.LocalizedXStreams;
import gherkin.I18n;
import gherkin.formatter.Argument;
@@ -128,14 +127,14 @@ private void writeStepdefsJson() {
glue.writeStepdefsJson(resourceLoader, runtimeOptions.getFeaturePaths(), runtimeOptions.getDotCucumber());
}
- public void buildBackendWorlds(Reporter reporter, Set<Tag> tags) {
+ public void buildBackendWorlds(Reporter reporter, Set<Tag> tags, String scenarioName) {
for (Backend backend : backends) {
backend.buildWorld();
}
undefinedStepsTracker.reset();
//TODO: this is the initial state of the state machine, it should not go here, but into something else
skipNextStep = false;
- scenarioResult = new ScenarioImpl(reporter, tags);
+ scenarioResult = new ScenarioImpl(reporter, tags, scenarioName);
}
public void disposeBackendWorlds() {
@@ -320,12 +319,4 @@ private void addHookToCounterAndResult(Result result) {
scenarioResult.add(result);
stats.addHookTime(result.getDuration());
}
-
- public void setScenario(Scenario scenario) {
- CurrentScenario.set(scenario);
- }
-
- public void clearScenario() {
- CurrentScenario.set(null);
- }
}
@@ -18,10 +18,12 @@
private final List<Result> stepResults = new ArrayList<Result>();
private final Reporter reporter;
private final Set<Tag> tags;
+ private final String scenarioName;
- public ScenarioImpl(Reporter reporter, Set<Tag> tags) {
+ public ScenarioImpl(Reporter reporter, Set<Tag> tags, String scenarioName) {
this.reporter = reporter;
this.tags = tags;
+ this.scenarioName = scenarioName;
}
void add(Result result) {
@@ -61,4 +63,9 @@ public void embed(byte[] data, String mimeType) {
public void write(String text) {
reporter.write(text);
}
+
+ @Override
+ public String getName() {
+ return scenarioName;
+ }
}
@@ -34,14 +34,13 @@ public CucumberBackground getCucumberBackground() {
@Override
public void run(Formatter formatter, Reporter reporter, Runtime runtime) {
Set<Tag> tags = tagsAndInheritedTags();
- runtime.buildBackendWorlds(reporter, tags);
+ runtime.buildBackendWorlds(reporter, tags, scenario.getName());
try {
formatter.startOfScenarioLifeCycle((Scenario) getGherkinModel());
} catch (Throwable ignore) {
// IntelliJ has its own formatter which doesn't yet implement this.
}
- runtime.setScenario(scenario);
runtime.runBeforeHooks(reporter, tags);
runBackground(formatter, reporter, runtime);
@@ -54,7 +53,6 @@ public void run(Formatter formatter, Reporter reporter, Runtime runtime) {
} catch (Throwable ignore) {
// IntelliJ has its own formatter which doesn't yet implement this.
}
- runtime.clearScenario();
runtime.disposeBackendWorlds();
}
@@ -1,16 +0,0 @@
-package cucumber.runtime.model;
-
-import gherkin.formatter.model.Scenario;
-
-public class CurrentScenario {
-
- private static final ThreadLocal<Scenario> threadLocal = new ThreadLocal<Scenario>();
-
- public static Scenario get() {
- return threadLocal.get();
- }
-
- public static void set(Scenario scenario) {
- threadLocal.set(scenario);
- }
-}
@@ -31,7 +31,7 @@ public void buildMockWorld() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
RuntimeOptions runtimeOptions = new RuntimeOptions("");
runtime = new Runtime(mock(ResourceLoader.class), classLoader, asList(mock(Backend.class)), runtimeOptions);
- runtime.buildBackendWorlds(null, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(null, Collections.<Tag>emptySet(), "mock scenario");
glue = runtime.getGlue();
}
@@ -208,7 +208,7 @@ public void should_add_passed_result_to_the_summary_counter() throws Exception {
StepDefinitionMatch match = mock(StepDefinitionMatch.class);
Runtime runtime = createRuntimeWithMockedGlue(match, "--monochrome");
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
runStep(reporter, runtime);
runtime.disposeBackendWorlds();
runtime.printStats(new PrintStream(baos));
@@ -225,7 +225,7 @@ public void should_add_pending_result_to_the_summary_counter() throws Throwable
StepDefinitionMatch match = createExceptionThrowingMatch(new PendingException());
Runtime runtime = createRuntimeWithMockedGlue(match, "--monochrome");
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
runStep(reporter, runtime);
runtime.disposeBackendWorlds();
runtime.printStats(new PrintStream(baos));
@@ -242,7 +242,7 @@ public void should_add_failed_result_to_the_summary_counter() throws Throwable {
StepDefinitionMatch match = createExceptionThrowingMatch(new Exception());
Runtime runtime = createRuntimeWithMockedGlue(match, "--monochrome");
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
runStep(reporter, runtime);
runtime.disposeBackendWorlds();
runtime.printStats(new PrintStream(baos));
@@ -258,7 +258,7 @@ public void should_add_ambiguous_match_as_failed_result_to_the_summary_counter()
Reporter reporter = mock(Reporter.class);
Runtime runtime = createRuntimeWithMockedGlueWithAmbiguousMatch("--monochrome");
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
runStep(reporter, runtime);
runtime.disposeBackendWorlds();
runtime.printStats(new PrintStream(baos));
@@ -275,7 +275,7 @@ public void should_add_skipped_result_to_the_summary_counter() throws Throwable
StepDefinitionMatch match = createExceptionThrowingMatch(new Exception());
Runtime runtime = createRuntimeWithMockedGlue(match, "--monochrome");
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
runStep(reporter, runtime);
runStep(reporter, runtime);
runtime.disposeBackendWorlds();
@@ -292,7 +292,7 @@ public void should_add_undefined_result_to_the_summary_counter() throws Throwabl
Reporter reporter = mock(Reporter.class);
Runtime runtime = createRuntimeWithMockedGlue(null, "--monochrome");
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
runStep(reporter, runtime);
runtime.disposeBackendWorlds();
runtime.printStats(new PrintStream(baos));
@@ -310,7 +310,7 @@ public void should_fail_the_scenario_if_before_fails() throws Throwable {
HookDefinition hook = createExceptionThrowingHook();
Runtime runtime = createRuntimeWithMockedGlue(match, hook, true, "--monochrome");
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
runtime.runBeforeHooks(reporter, Collections.<Tag>emptySet());
runStep(reporter, runtime);
runtime.disposeBackendWorlds();
@@ -329,7 +329,7 @@ public void should_fail_the_scenario_if_after_fails() throws Throwable {
HookDefinition hook = createExceptionThrowingHook();
Runtime runtime = createRuntimeWithMockedGlue(match, hook, false, "--monochrome");
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
runStep(reporter, runtime);
runtime.runAfterHooks(reporter, Collections.<Tag>emptySet());
runtime.disposeBackendWorlds();
@@ -14,7 +14,7 @@
public class ScenarioResultTest {
private Reporter reporter = mock(Reporter.class);
- private ScenarioImpl s = new ScenarioImpl(reporter, Collections.<Tag>emptySet());
+ private ScenarioImpl s = new ScenarioImpl(reporter, Collections.<Tag>emptySet(), "test scenario");
@Test
public void no_steps_is_passed() throws Exception {
@@ -69,7 +69,7 @@ public void throws_ambiguous_when_two_matches_are_found() throws Throwable {
backend.addStepDefinition(THREE_BLIND_ANIMALS.getAnnotation(Given.class), THREE_BLIND_ANIMALS);
Reporter reporter = mock(Reporter.class);
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
Tag tag = new Tag("@foo", 0);
runtime.runBeforeHooks(reporter, asSet(tag));
runtime.runStep("some.feature", new Step(NO_COMMENTS, "Given ", "three blind mice", 1, null, null), reporter, ENGLISH);
@@ -113,7 +113,7 @@ public void embedding(String mimeType, byte[] data) {
public void write(String text) {
}
};
- runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet());
+ runtime.buildBackendWorlds(reporter, Collections.<Tag>emptySet(), "test scenario");
Tag tag = new Tag("@foo", 0);
Set<Tag> tags = asSet(tag);
runtime.runBeforeHooks(reporter, tags);
@@ -1,24 +1,30 @@
package cucumber.runtime.java.test;
+import cucumber.api.Scenario;
+import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
-import cucumber.runtime.model.CurrentScenario;
import static junit.framework.Assert.assertEquals;
public class ScenarioStepDefs {
private String scenarioName = "";
+ @Before
+ public void get_scenario_name(Scenario scenario) {
+ scenarioName = scenario.getName();
+ }
+
@Given("^I am running a scenario$")
public void i_am_running_a_scenario() {
}
@When("^I try to get the scenario name$")
public void i_try_to_get_the_scenario_name() {
- scenarioName = CurrentScenario.get().getName();
+
}
@Then("^The scenario name is \"([^\"]*)\"$")

0 comments on commit 9b3e174

Please sign in to comment.