Search in sources :

Example 1 with DataTable

use of cucumber.api.DataTable in project cucumber-jvm by cucumber.

the class TableConverter method convert.

/**
     * This method converts a {@link cucumber.api.DataTable} to abother type.
     * When a Step Definition is passed a Gherkin Data Table, the runtime will use this method to convert the
     * {@link cucumber.api.DataTable} to the declared type before invoking the Step Definition.
     * <p/>
     * This method uses reflection to inspect the type and delegates to the appropriate {@code toXxx} method.
     *
     * @param dataTable  the table to convert
     * @param type       the type to convert to
     * @param transposed whether the table should be transposed first.
     * @return the transformed object.
     */
public <T> T convert(DataTable dataTable, Type type, boolean transposed) {
    if (transposed) {
        dataTable = dataTable.transpose();
    }
    if (type == null || (type instanceof Class && ((Class) type).isAssignableFrom(DataTable.class))) {
        return (T) dataTable;
    }
    Type mapKeyType = mapKeyType(type);
    if (mapKeyType != null) {
        Type mapValueType = mapValueType(type);
        return (T) toMap(dataTable, mapKeyType, mapValueType);
    }
    Type itemType = listItemType(type);
    if (itemType == null) {
        throw new CucumberException("Not a Map or List type: " + type);
    }
    Type listItemType = listItemType(itemType);
    if (listItemType != null) {
        return (T) toLists(dataTable, listItemType);
    } else {
        SingleValueConverter singleValueConverter = xStream.getSingleValueConverter(itemType);
        if (singleValueConverter != null) {
            return (T) toList(dataTable, singleValueConverter);
        } else {
            if (itemType instanceof Class) {
                if (Map.class.equals(itemType)) {
                    // Non-generic map
                    return (T) toMaps(dataTable, String.class, String.class);
                } else {
                    return (T) toListOfComplexType(dataTable, (Class) itemType);
                }
            } else {
                return (T) toMaps(dataTable, mapKeyType(itemType), mapValueType(itemType));
            }
        }
    }
}
Also used : DataTable(cucumber.api.DataTable) Utils.mapKeyType(cucumber.runtime.Utils.mapKeyType) Utils.listItemType(cucumber.runtime.Utils.listItemType) Utils.mapValueType(cucumber.runtime.Utils.mapValueType) Type(java.lang.reflect.Type) SingleValueConverter(cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter) CucumberException(cucumber.runtime.CucumberException)

Example 2 with DataTable

use of cucumber.api.DataTable in project cucumber-jvm by cucumber.

the class TableDiffer method createTableDiff.

private DataTable createTableDiff(Map<Integer, Delta> deltasByLine) {
    List<DataTableRow> diffTableRows = new ArrayList<DataTableRow>();
    List<List<String>> rows = from.raw();
    for (int i = 0; i < rows.size(); i++) {
        Delta delta = deltasByLine.get(i);
        if (delta == null) {
            diffTableRows.add(from.getGherkinRows().get(i));
        } else {
            addRowsToTableDiff(diffTableRows, delta);
            // skipping lines involved in a delta
            if (delta.getType() == Delta.TYPE.CHANGE || delta.getType() == Delta.TYPE.DELETE) {
                i += delta.getOriginal().getLines().size() - 1;
            } else {
                diffTableRows.add(from.getGherkinRows().get(i));
            }
        }
    }
    // Can have new lines at end
    Delta remainingDelta = deltasByLine.get(rows.size());
    if (remainingDelta != null) {
        addRowsToTableDiff(diffTableRows, remainingDelta);
    }
    return new DataTable(diffTableRows, from.getTableConverter());
}
Also used : DataTable(cucumber.api.DataTable) Delta(cucumber.deps.difflib.Delta) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) DataTableRow(gherkin.formatter.model.DataTableRow)

Example 3 with DataTable

use of cucumber.api.DataTable in project cucumber-jvm by cucumber.

the class StepDefinitionMatch method tableArgument.

private Object tableArgument(Step step, int argIndex, LocalizedXStreams.LocalizedXStream xStream) {
    ParameterInfo parameterInfo = getParameterType(argIndex, DataTable.class);
    TableConverter tableConverter = new TableConverter(xStream, parameterInfo);
    DataTable table = new DataTable(step.getRows(), tableConverter);
    Type type = parameterInfo.getType();
    return tableConverter.convert(table, type, parameterInfo.isTransposed());
}
Also used : DataTable(cucumber.api.DataTable) Type(java.lang.reflect.Type) TableConverter(cucumber.runtime.table.TableConverter)

Example 4 with DataTable

use of cucumber.api.DataTable in project cucumber-jvm by cucumber.

the class TableConverterTest method converts_table_to_list_of_pojo_and_almost_back.

@Test
public void converts_table_to_list_of_pojo_and_almost_back() {
    DataTable table = TableParser.parse("|Birth Date|Death Cal|\n|1957-05-10|1979-02-02|\n", PARAMETER_INFO);
    List<UserPojo> converted = table.asList(UserPojo.class);
    assertEquals(sidsBirthday(), converted.get(0).birthDate);
    assertEquals(sidsDeathcal(), converted.get(0).deathCal);
    assertEquals("      | birthDate  | deathCal   |\n      | 1957-05-10 | 1979-02-02 |\n", table.toTable(converted).toString());
}
Also used : DataTable(cucumber.api.DataTable) Test(org.junit.Test)

Example 5 with DataTable

use of cucumber.api.DataTable in project cucumber-jvm by cucumber.

the class TableConverterTest method converts_to_list_of_map_of_string.

@Test
public void converts_to_list_of_map_of_string() {
    DataTable table = TableParser.parse("|Birth Date|Death Cal|\n|1957-05-10|1979-02-02|\n", null);
    List<Map<String, String>> converted = table.asMaps(String.class, String.class);
    assertEquals("1957-05-10", converted.get(0).get("Birth Date"));
}
Also used : DataTable(cucumber.api.DataTable) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

DataTable (cucumber.api.DataTable)33 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)9 List (java.util.List)6 DataTableRow (gherkin.formatter.model.DataTableRow)4 Arrays.asList (java.util.Arrays.asList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)3 LocalizedXStreams (cucumber.runtime.xstream.LocalizedXStreams)2 Type (java.lang.reflect.Type)2 SingleValueConverter (cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter)1 Delta (cucumber.deps.difflib.Delta)1 CucumberException (cucumber.runtime.CucumberException)1 Utils.listItemType (cucumber.runtime.Utils.listItemType)1 Utils.mapKeyType (cucumber.runtime.Utils.mapKeyType)1 Utils.mapValueType (cucumber.runtime.Utils.mapValueType)1 TableConverter (cucumber.runtime.table.TableConverter)1 En (gherkin.lexer.En)1 Lexer (gherkin.lexer.Lexer)1 Listener (gherkin.lexer.Listener)1