Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement Table.asList(Class itemType). Clean up imports

  • Loading branch information...
commit a84c7536002ddb55a4749781d2276cc4bdfc4b5c 1 parent 42e9c17
@aslakhellesoy aslakhellesoy authored
View
3  clojure/src/main/java/cucumber/runtime/clojure/ClojureStepDefinition.java
@@ -4,10 +4,7 @@
import cucumber.runtime.JdkPatternArgumentMatcher;
import cucumber.runtime.StepDefinition;
import cucumber.runtime.Utils;
-import cucumber.table.Table;
-import cucumber.table.TableConverter;
import gherkin.formatter.Argument;
-import gherkin.formatter.model.Row;
import gherkin.formatter.model.Step;
import java.lang.reflect.InvocationTargetException;
View
4 core/src/main/java/cucumber/runtime/StepDefinition.java
@@ -16,9 +16,9 @@
/**
* Cucumber will try to convert each Gherkin step table into a {@link List} of objects. The header row is used to
* identify property names of the objects, and each row underneath will be converted to an object.
- *
+ * <p/>
* If this method returns null, Cucumber will convert the rows into an instance of {@link cucumber.table.Table}.
- *
+ *
* @return the kind of object Cucumber should instantiate for each row, or null if no conversion should happen.
*/
Class getTypeForTableList(int argIndex);
View
42 core/src/main/java/cucumber/runtime/StepDefinitionMatch.java
@@ -8,11 +8,9 @@
import cucumber.table.TableHeaderMapper;
import gherkin.formatter.Argument;
import gherkin.formatter.model.Match;
-import gherkin.formatter.model.Row;
import gherkin.formatter.model.Step;
import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -59,8 +57,7 @@ public void runStep(Locale locale) throws Throwable {
Object[] result = new Object[argumentCount];
int n = 0;
if (step.getRows() != null) {
- TableConverter tableConverter = new TableConverter(localizedXStreams.get(locale));
- result[n] = tableArgument(step, tableConverter, n++);
+ result[n] = tableArgument(step, n++, locale);
} else if (step.getDocString() != null) {
result[n] = step.getDocString().getValue();
} else {
@@ -76,40 +73,17 @@ public void runStep(Locale locale) throws Throwable {
return result;
}
- private Object tableArgument(Step step, TableConverter tableConverter, int argIndex) {
+ private Object tableArgument(Step step, int argIndex, Locale locale) {
+ Table table = new Table(step.getRows(), new TableConverter(localizedXStreams.get(locale)), tableHeaderMapper);
+
Class listType = stepDefinition.getTypeForTableList(argIndex);
- if(listType != null) {
- return tableConverter.convert(listType, attributeNames(step.getRows()), attributeValues(step.getRows()));
+ if (listType != null) {
+ return table.asList(listType);
} else {
- return new Table(step.getRows());
+ return table;
}
}
- private List<List<String>> attributeValues(List<Row> rows) {
- List<List<String>> attributeValues = new ArrayList<List<String>>();
- List<Row> valueRows = rows.subList(1, rows.size());
- for (Row valueRow : valueRows) {
- attributeValues.add(toStrings(valueRow));
- }
- return attributeValues;
- }
-
- private List<String> attributeNames(List<Row> rows) {
- List<String> strings = new ArrayList<String>();
- for (String string : rows.get(0).getCells()) {
- strings.add(tableHeaderMapper.map(string));
- }
- return strings;
- }
-
- private List<String> toStrings(Row row) {
- List<String> strings = new ArrayList<String>();
- for (String string : row.getCells()) {
- strings.add(string);
- }
- return strings;
- }
-
public Throwable filterStacktrace(Throwable error, StackTraceElement stepLocation) {
StackTraceElement[] stackTraceElements = error.getStackTrace();
if (error.getCause() != null && error.getCause() != error) {
@@ -124,7 +98,7 @@ public Throwable filterStacktrace(Throwable error, StackTraceElement stepLocatio
break;
}
}
- if(stepLocation != null) {
+ if (stepLocation != null) {
StackTraceElement[] result = new StackTraceElement[stackLength + 1];
System.arraycopy(stackTraceElements, 0, result, 0, stackLength);
result[stackLength] = stepLocation;
View
4 core/src/main/java/cucumber/runtime/converters/ConverterWithFormat.java
@@ -19,7 +19,7 @@ public T fromString(String string) {
for (Format format : getFormats()) {
try {
return transform(format, string);
- } catch(Exception ignore) {
+ } catch (Exception ignore) {
// no worries, let's try the next format.
}
}
@@ -57,7 +57,7 @@ public String toString(Object obj) {
@Override
public boolean canConvert(Class type) {
for (Class convertibleType : convertibleTypes) {
- if(convertibleType.isAssignableFrom(type)) {
+ if (convertibleType.isAssignableFrom(type)) {
return true;
}
}
View
3  core/src/main/java/cucumber/runtime/converters/package.html
@@ -1,4 +1,5 @@
<p>
This package contains Locale-aware alternatives to some of XStream's (non-Locale-aware) built-in converters.
- This allows users to write numbers, dates etc. in Gherkin, using the Locale that is associated with the Gherkin source.
+ This allows users to write numbers, dates etc. in Gherkin, using the Locale that is associated with the Gherkin
+ source.
</p>
View
46 core/src/main/java/cucumber/table/Table.java
@@ -4,15 +4,18 @@
import java.util.ArrayList;
import java.util.List;
-import java.util.Locale;
public class Table {
private final List<List<String>> raw;
private final List<Row> gherkinRows;
+ private final TableConverter tableConverter;
+ private final TableHeaderMapper tableHeaderMapper;
- public Table(List<Row> gherkinRows) {
+ public Table(List<Row> gherkinRows, TableConverter tableConverter, TableHeaderMapper tableHeaderMapper) {
this.gherkinRows = gherkinRows;
+ this.tableConverter = tableConverter;
+ this.tableHeaderMapper = tableHeaderMapper;
this.raw = new ArrayList<List<String>>();
for (Row row : gherkinRows) {
List<String> list = new ArrayList<String>();
@@ -25,10 +28,37 @@ public Table(List<Row> gherkinRows) {
return this.raw;
}
- public <T> List<T> asList(T type) {
- throw new UnsupportedOperationException("TODO: i9mplement this method and get rid of the hashes() method");
+ public <T> List<T> asList(Class itemType) {
+ return tableConverter.convert(itemType, attributeNames(), attributeValues());
}
+
+ private List<List<String>> attributeValues() {
+ List<List<String>> attributeValues = new ArrayList<List<String>>();
+ List<Row> valueRows = gherkinRows.subList(1, gherkinRows.size());
+ for (Row valueRow : valueRows) {
+ attributeValues.add(toStrings(valueRow));
+ }
+ return attributeValues;
+ }
+
+ private List<String> attributeNames() {
+ List<String> strings = new ArrayList<String>();
+ for (String string : gherkinRows.get(0).getCells()) {
+ strings.add(tableHeaderMapper.map(string));
+ }
+ return strings;
+ }
+
+ private List<String> toStrings(Row row) {
+ List<String> strings = new ArrayList<String>();
+ for (String string : row.getCells()) {
+ strings.add(string);
+ }
+ return strings;
+ }
+
+
public void diff(Table other) {
new TableDiffer(this, other).calculateDiffs();
}
@@ -46,6 +76,14 @@ public void diff(Table other) {
return result;
}
+ TableConverter getTableConverter() {
+ return tableConverter;
+ }
+
+ TableHeaderMapper getTableHeaderMapper() {
+ return tableHeaderMapper;
+ }
+
class DiffableRow {
public final Row row;
public final List<String> convertedRow;
View
5 core/src/main/java/cucumber/table/TableConverter.java
@@ -8,13 +8,10 @@
private final XStream xStream;
public TableConverter(XStream xStream) {
- if(xStream == null) {
- throw new NullPointerException("xStream");
- }
this.xStream = xStream;
}
- public List convert(Class itemType, List<String> attributeNames, List<List<String>> attributeValues) {
+ public <T> List<T> convert(Class itemType, List<String> attributeNames, List<List<String>> attributeValues) {
return (List) xStream.unmarshal(new XStreamTableReader(itemType, attributeNames, attributeValues));
}
}
View
2  core/src/main/java/cucumber/table/TableDiffer.java
@@ -45,7 +45,7 @@ private Table createTableDiff(Map<Integer, Delta> deltasByLine) {
if (remainingDelta != null) {
addRowsToTableDiffAndReturnNumberOfRows(diffTableRows, remainingDelta);
}
- return new Table(diffTableRows);
+ return new Table(diffTableRows, orig.getTableConverter(), orig.getTableHeaderMapper());
}
private int addRowsToTableDiffAndReturnNumberOfRows(List<Row> diffTableRows, Delta delta) {
View
28 core/src/main/java/cucumber/table/XStreamTableReader.java
@@ -30,30 +30,32 @@ public XStreamTableReader(Class elementType, List<String> attributeNames, List<L
@Override
public boolean hasMoreChildren() {
- switch(depth) {
+ switch (depth) {
case 0:
return itemIterator.hasNext();
case 1:
return attributeNameIterator.hasNext();
- case 2:
+ case 2:
return false;
- default: throw new IllegalStateException("Depth is " + depth);
+ default:
+ throw new IllegalStateException("Depth is " + depth);
}
}
@Override
public void moveDown() {
depth++;
- switch(depth) {
+ switch (depth) {
case 1:
attributeNameIterator = attributeNames.iterator();
attributeValueIterator = itemIterator.next().iterator();
break;
- case 2:
+ case 2:
attributeName = attributeNameIterator.next();
attributeValue = attributeValueIterator.next();
break;
- default: throw new IllegalStateException("Depth is " + depth);
+ default:
+ throw new IllegalStateException("Depth is " + depth);
}
}
@@ -64,11 +66,15 @@ public void moveUp() {
@Override
public String getNodeName() {
- switch(depth) {
- case 0: return "list";
- case 1: return elementType.getName();
- case 2: return attributeName;
- default: throw new IllegalStateException("Depth is " + depth);
+ switch (depth) {
+ case 0:
+ return "list";
+ case 1:
+ return elementType.getName();
+ case 2:
+ return attributeName;
+ default:
+ throw new IllegalStateException("Depth is " + depth);
}
}
View
10 core/src/test/java/cucumber/runtime/converters/ConvertersTest.java
@@ -16,13 +16,13 @@ public void shouldTransformToTheRightType() {
LocalizedXStreams transformers = new LocalizedXStreams();
ConverterLookup en = transformers.get(Locale.ENGLISH).getConverterLookup();
- assertTrue((Boolean) ((SingleValueConverter)en.lookupConverterForType(Boolean.class)).fromString("true"));
- assertTrue((Boolean) ((SingleValueConverter)en.lookupConverterForType(Boolean.TYPE)).fromString("true"));
+ assertTrue((Boolean) ((SingleValueConverter) en.lookupConverterForType(Boolean.class)).fromString("true"));
+ assertTrue((Boolean) ((SingleValueConverter) en.lookupConverterForType(Boolean.TYPE)).fromString("true"));
assertEquals(3000.15f, (Float) ((SingleValueConverter) en.lookupConverterForType(Float.class)).fromString("3000.15"), 0.000001);
- assertEquals(3000.15f, (Float) ((SingleValueConverter)en.lookupConverterForType(Float.TYPE)).fromString("3000.15"), 0.000001);
- assertEquals(new BigDecimal("3000.15"), ((SingleValueConverter)en.lookupConverterForType(BigDecimal.class)).fromString("3000.15"));
+ assertEquals(3000.15f, (Float) ((SingleValueConverter) en.lookupConverterForType(Float.TYPE)).fromString("3000.15"), 0.000001);
+ assertEquals(new BigDecimal("3000.15"), ((SingleValueConverter) en.lookupConverterForType(BigDecimal.class)).fromString("3000.15"));
ConverterLookup no = transformers.get(new Locale("NO")).getConverterLookup();
- assertEquals(3000.15f, (Float) ((SingleValueConverter)no.lookupConverterForType(Float.TYPE)).fromString("3000,15"), 0.000001);
+ assertEquals(3000.15f, (Float) ((SingleValueConverter) no.lookupConverterForType(Float.TYPE)).fromString("3000,15"), 0.000001);
}
}
View
3  core/src/test/java/cucumber/runtime/converters/StandardConvertersTest.java
@@ -1,7 +1,6 @@
package cucumber.runtime.converters;
import com.thoughtworks.xstream.converters.ConversionException;
-import cucumber.runtime.CucumberException;
import org.junit.Test;
import java.math.BigDecimal;
@@ -10,7 +9,7 @@
import java.util.Date;
import java.util.Locale;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
public class StandardConvertersTest {
@Test
View
2  core/src/main/java/cucumber/table/TableParser.java → core/src/test/java/cucumber/table/TableParser.java
@@ -71,6 +71,6 @@ public void eof() {
}
});
l.scan(source);
- return new Table(rows);
+ return new Table(rows, null, null);
}
}
View
12 core/src/test/java/cucumber/table/TableTest.java
@@ -1,11 +1,15 @@
package cucumber.table;
+import com.thoughtworks.xstream.XStream;
+import cucumber.runtime.converters.LocalizedXStreams;
import gherkin.formatter.model.Comment;
import gherkin.formatter.model.Row;
import org.junit.Before;
import org.junit.Test;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
@@ -17,10 +21,10 @@
@Before
public void initSimpleTable() {
List<Row> simpleRows = new ArrayList<Row>();
- String[] firstLine = new String[]{"one", "four", "seven"};
- simpleRows.add(new Row(new ArrayList<Comment>(), asList(firstLine), 1));
+ simpleRows.add(new Row(new ArrayList<Comment>(), asList("one", "four", "seven"), 1));
simpleRows.add(new Row(new ArrayList<Comment>(), asList("4444", "55555", "666666"), 2));
- simpleTable = new Table(simpleRows);
+ XStream xStream = new LocalizedXStreams().get(Locale.UK);
+ simpleTable = new Table(simpleRows, new TableConverter(xStream), new CamelCaseHeaderMapper());
}
@Test
View
5 groovy/src/main/java/cucumber/runtime/groovy/GroovyStepDefinition.java
@@ -2,15 +2,10 @@
import cucumber.runtime.JdkPatternArgumentMatcher;
import cucumber.runtime.StepDefinition;
-import cucumber.table.Table;
-import cucumber.table.TableConverter;
import gherkin.formatter.Argument;
-import gherkin.formatter.model.Row;
import gherkin.formatter.model.Step;
import groovy.lang.Closure;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
import java.util.List;
import java.util.regex.Pattern;
View
3  rhino/src/main/java/cucumber/runtime/rhino/RhinoStepDefinition.java
@@ -1,10 +1,7 @@
package cucumber.runtime.rhino;
import cucumber.runtime.StepDefinition;
-import cucumber.table.Table;
-import cucumber.table.TableConverter;
import gherkin.formatter.Argument;
-import gherkin.formatter.model.Row;
import gherkin.formatter.model.Step;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeFunction;
Please sign in to comment.
Something went wrong with that request. Please try again.