Skip to content
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

Jython Before/After Annotations #362

Merged
merged 2 commits into from
Aug 18, 2012
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
2 changes: 1 addition & 1 deletion cucumber-tck
Submodule cucumber-tck updated from 0f42d8 to 1288b2
20 changes: 16 additions & 4 deletions jython/src/main/java/cucumber/runtime/jython/JythonBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import cucumber.io.Resource;
import cucumber.io.ResourceLoader;
import cucumber.runtime.Backend;
import cucumber.runtime.CucumberException;
import cucumber.runtime.Glue;
import cucumber.runtime.UnreportedStepExecutor;
import cucumber.runtime.*;
import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Step;
import org.python.core.PyException;
Expand Down Expand Up @@ -78,7 +75,22 @@ public void registerStepdef(PyInstance stepdef, int arity) {
glue.addStepDefinition(new JythonStepDefinition(this, stepdef, arity));
}

public void addBeforeHook(PyInstance hookDefinition) {
glue.addBeforeHook(new JythonHookDefinition(this, hookDefinition));
}

public void addAfterHook(PyInstance hookDefinition) {
glue.addAfterHook(new JythonHookDefinition(this, hookDefinition));
}

public void executeHook(PyInstance hookDefinition, Object[] scenarioResults) {
PyObject[] pyArgs = new PyObject[1];
pyArgs[0] = pyWorld;
hookDefinition.invoke("execute", pyArgs);
}

public void execute(PyInstance stepdef, Object[] args) throws Throwable {

PyObject[] pyArgs = new PyObject[args.length + 1];
pyArgs[0] = pyWorld;
for (int i = 0; i < args.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cucumber.runtime.jython;

import cucumber.runtime.HookDefinition;
import cucumber.runtime.ScenarioResult;
import gherkin.TagExpression;
import gherkin.formatter.model.Tag;
import org.python.core.*;
import java.util.Collection;
import java.util.List;

public class JythonHookDefinition implements HookDefinition {
private final PyInstance hookDefinition;
private final TagExpression tagExpression;
private final JythonBackend backend;

public JythonHookDefinition(JythonBackend backend, PyInstance hookDefinition) {
this.backend = backend;
this.hookDefinition = hookDefinition;
PyTuple tags = (PyTuple)hookDefinition.__dict__.__finditem__("tags");
this.tagExpression = new TagExpression(tags);
}

@Override
public void execute(ScenarioResult scenarioResult) throws Throwable {
backend.executeHook(hookDefinition, new Object[]{scenarioResult});
}

@Override
public boolean matches(Collection<Tag> tags) {
return tagExpression.eval(tags);
}

@Override
public String getLocation(boolean detail) {
return null;
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ public boolean isDefinedAt(StackTraceElement stackTraceElement) {
public String getPattern() {
return stepdef.invoke("pattern").toString();
}
}
}
23 changes: 23 additions & 0 deletions jython/src/main/resources/cucumber/runtime/jython/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,26 @@ def pattern(self):

class World:
"""The World"""

class HookDefinition:

def __init__(self, tags, func):
self.tags = tags
self.func = func

def execute(self, *args):
self.func.__call__(*args)

class Before():
def __init__(self, *tags):
self.tags = tags

def __call__(self, func):
backend.addBeforeHook(HookDefinition(self.tags, func))

class After():
def __init__(self, *tags):
self.tags = tags

def __call__(self, func):
backend.addAfterHook(HookDefinition(self.tags, func))
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Cukes
Scenario: default cukes in the belly from a tagless Before annotation
Given I have "5" cukes in my belly

@must_have_more_cukes
@and_then_some
Scenario: more cukes in the belly from tagged and tagless Before annotations
Given I have "6" cukes in my belly

Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@ def something_in_the_belly(self, n, what):
def i_am(self, mood):
if ("happy" != mood):
raise(Exception("Not happy, only %d %s" % (self.n, self.what)))

@Before()
def default_cukes_in_the_belly(self):
self.n = 5
self.what = "cukes"

@Before('@must_have_more_cukes','@and_then_some')
def more_cukes_in_the_belly(self):
self.n = self.n + 1
self.what = "cukes"

@Given('^I have "([^"]*)" cukes in my belly$')
def I_have_cukes_in_my_belly(self, arg1):
val = int(arg1)
if (self.n != val):
raise(Exception("Default cukes were %d, not %d" % (self.n, val)))