Skip to content

Commit 20114f7

Browse files
committed
Use PyList instead of PyArray when converting DataTable for Jython. Ref cucumber#602.
1 parent 0829b89 commit 20114f7

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## [1-1-6-SNAPSHOT (Git master)](https://github.com/cucumber/cucumber-jvm/compare/v1.1.5...master)
22

3+
* [Jython] Fix for DataTable as parameter in Jython steps ([#602](https://github.com/cucumber/cucumber-jvm/pull/602) lggroapa, Aslak Hellesøy)
34
* [Core] Fix and improve CamelCaseFunctionNameSanitizer ([#603](https://github.com/cucumber/cucumber-jvm/pull/603) Frieder Bluemle)
45
* [Core, Java] Log a warning when more than one IoC dependency is found in the classpath ([#594](https://github.com/cucumber/cucumber-jvm/pull/594) Ariel Kogan)
56
* [JUnit,TestNG] Report summaries and `.cucumber/stepdefs.json` in the same way as the CLI (Aslak Hellesøy)

jython/src/main/java/cucumber/runtime/jython/JythonBackend.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cucumber.runtime.jython;
22

3-
import cucumber.api.Scenario;
43
import cucumber.api.DataTable;
4+
import cucumber.api.Scenario;
55
import cucumber.runtime.Backend;
66
import cucumber.runtime.CucumberException;
77
import cucumber.runtime.Glue;
@@ -11,7 +11,12 @@
1111
import cucumber.runtime.snippets.FunctionNameSanitizer;
1212
import cucumber.runtime.snippets.SnippetGenerator;
1313
import gherkin.formatter.model.Step;
14-
import org.python.core.*;
14+
import org.python.core.Py;
15+
import org.python.core.PyException;
16+
import org.python.core.PyInstance;
17+
import org.python.core.PyList;
18+
import org.python.core.PyObject;
19+
import org.python.core.PyString;
1520
import org.python.util.PythonInterpreter;
1621

1722
import java.io.IOException;
@@ -27,7 +32,7 @@ public class JythonBackend implements Backend {
2732
private PyObject pyWorld;
2833
private Glue glue;
2934

30-
public JythonBackend(ResourceLoader resourceLoader, PythonInterpreter jython) {
35+
public JythonBackend(ResourceLoader resourceLoader, PythonInterpreter jython) {
3136
this.resourceLoader = resourceLoader;
3237
this.jython = jython;
3338
jython.set("backend", this);
@@ -109,27 +114,26 @@ private String getStacktrace(PyException e) {
109114
e.printStackTrace(pw);
110115
return sw.getBuffer().toString();
111116
}
112-
113-
private PyObject argToPyObject(Object arg){
114-
if (arg instanceof DataTable)
115-
return dataTableToPyArray((DataTable)arg);
116-
return new PyString((String)arg);
117-
}
118-
119-
private PyArray dataTableToPyArray(DataTable table){
120-
List<List<String>> rawTable = table.raw();
121-
PyArray pyDataTable = new PyArray( PyArray.class , 0);
122-
PyArray pyDataRow ;
123-
for(List<String> row : rawTable){
124-
pyDataRow = new PyArray ( PyString.class , 0 );
125-
for(String cell : row){
126-
pyDataRow.append(new PyString(cell));
117+
118+
private PyObject argToPyObject(Object arg) {
119+
if (arg instanceof DataTable) {
120+
return dataTableToPyArray((DataTable) arg);
121+
}
122+
return new PyString((String) arg);
123+
}
124+
125+
private PyObject dataTableToPyArray(DataTable table) {
126+
PyList pyTable = new PyList();
127+
for (List<String> row : table.raw()) {
128+
PyList pyRow = new PyList();
129+
for (String cell : row) {
130+
pyRow.append(new PyString(cell));
127131
}
128-
pyDataTable.append(pyDataRow);
132+
pyTable.append(pyRow);
129133
}
130-
return pyDataTable;
134+
return pyTable;
131135
}
132-
136+
133137
public void execute(PyInstance stepdef, Object[] args) throws Throwable {
134138

135139
PyObject[] pyArgs = new PyObject[args.length + 1];

jython/src/test/resources/cucumber/runtime/jython/step_definitions/cuke_steps.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ def I_have_cukes_in_my_belly(self, arg1):
2727
@After()
2828
def we_can_get_the_scenario(self, scenario):
2929
if(scenario.getStatus() != 'passed'):
30-
print("Oh no!")
30+
print(Exception("Oh no!"))
3131

3232
@Given('^the following users exist:$')
33-
def the_following_users_exit(self,dataTable):
34-
for row in dataTable:
35-
for cell in row:
36-
print("Checking if cell: "+cell+" exists")
33+
def the_following_users_exit(self, dataTable):
34+
expected = [
35+
["name", "email", "phone"],
36+
["Aslak", "aslak@email.com", "123"],
37+
["Matt", "matt@email.com", "234"],
38+
["Joe", "joe@email.org", "456"]
39+
]
40+
if (expected != dataTable):
41+
raise(Exception("Oh no!"))

0 commit comments

Comments
 (0)