Skip to content

Issue #458: Groovy should throw exception if more then one World registred #464

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<artifactId>cobertura</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
15 changes: 5 additions & 10 deletions groovy/src/main/java/cucumber/runtime/groovy/GroovyBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class GroovyBackend implements Backend {
private final ClasspathResourceLoader classpathResourceLoader;

private Closure worldClosure;
private Object groovyWorld;
private Object world;
private Glue glue;

private static GroovyShell createShell() {
Expand Down Expand Up @@ -97,6 +97,7 @@ public void setUnreportedStepExecutor(UnreportedStepExecutor executor) {

@Override
public void buildWorld() {
world = worldClosure == null ? new Object() : worldClosure.call();
}

private Script parse(Resource resource) {
Expand All @@ -113,7 +114,7 @@ private boolean isScript(Script script) {

@Override
public void disposeWorld() {
this.groovyWorld = null;
this.world = null;
}

@Override
Expand All @@ -126,6 +127,7 @@ public void addStepDefinition(Pattern regexp, int timeoutMillis, Closure body) {
}

public void registerWorld(Closure closure) {
if (worldClosure != null) throw new CucumberException("World is already set");
worldClosure = closure;
}

Expand All @@ -138,21 +140,14 @@ public void addAfterHook(TagExpression tagExpression, int timeoutMillis, Closure
}

public void invoke(Closure body, Object[] args) throws Throwable {
body.setDelegate(getGroovyWorld());
body.setDelegate(world);
try {
body.call(args);
} catch (InvokerInvocationException e) {
throw e.getCause();
}
}

private Object getGroovyWorld() {
if (groovyWorld == null) {
groovyWorld = worldClosure == null ? new Object() : worldClosure.call();
}
return groovyWorld;
}

private static StackTraceElement currentLocation() {
Throwable t = new Throwable();
StackTraceElement[] stackTraceElements = t.getStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cucumber.runtime.groovy;

import cucumber.runtime.CucumberException;
import cucumber.runtime.io.ResourceLoader;
import groovy.lang.Closure;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.verify;


@RunWith(MockitoJUnitRunner.class)
public class GroovyBackendTest {
@Mock
ResourceLoader resourceLoader;
@Mock
Closure closure;

GroovyBackend backend;

@Before
public void setUp() throws Exception {
backend = new GroovyBackend(resourceLoader);
}

@Test
public void builds_world_by_calling_closure() {
backend.registerWorld(closure);
backend.buildWorld();

verify(closure).call();
}

@Test
public void builds_default_wold_if_wold_closer_does_not_set() {
backend.buildWorld();
}

@Test(expected = CucumberException.class)
public void raises_exception_for_two_wolds() {
backend.registerWorld(closure);
backend.registerWorld(closure);
}
}