Skip to content

Commit

Permalink
[Core] Convert empty pickle table cells to null
Browse files Browse the repository at this point in the history
Fixes: #1617
  • Loading branch information
mpkorstanje committed Aug 2, 2019
1 parent b54ec4f commit cf64eff
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.cucumber.core.stepexpression;


@FunctionalInterface
interface DocStringTransformer<T> {
T transform(String docString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ private PickleTableConverter() {
}

static List<List<String>> toTable(PickleTable pickleTable) {
List<List<String>> table = new ArrayList<List<String>>();
List<List<String>> table = new ArrayList<>();
for (PickleRow pickleRow : pickleTable.getRows()) {
List<String> row = new ArrayList<String>();
List<String> row = new ArrayList<>();
for (PickleCell pickleCell : pickleRow.getCells()) {
row.add(pickleCell.getValue());
String value = pickleCell.getValue();
row.add(value.isEmpty() ? null : value);
}
table.add(row);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

@FunctionalInterface
interface RawTableTransformer<T> {
T transform(List<List<String>> raw);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ public final class StepExpressionFactory {
private final io.cucumber.cucumberexpressions.ExpressionFactory expressionFactory;
private final DataTableTypeRegistryTableConverter tableConverter;

private static final DocStringTransformer<String> DOC_STRING_IDENTITY = new DocStringTransformer<String>() {
@Override
public String transform(String input) {
return input;
}
};

public StepExpressionFactory(TypeRegistry registry) {
this.expressionFactory = new io.cucumber.cucumberexpressions.ExpressionFactory(registry.parameterTypeRegistry());
this.tableConverter = new DataTableTypeRegistryTableConverter(registry.dataTableTypeRegistry());
Expand All @@ -32,13 +25,9 @@ public StepExpression createExpression(String expressionString) {
if (expressionString == null) throw new NullPointerException("expression can not be null");
Expression expression = expressionFactory.createExpression(expressionString);

RawTableTransformer<DataTable> toDataTable = new RawTableTransformer<DataTable>() {
@Override
public DataTable transform(List<List<String>> raw) {
return DataTable.create(raw, tableConverter);
}
};
return new StepExpression(expression, DOC_STRING_IDENTITY, toDataTable);
RawTableTransformer<DataTable> toDataTable = raw -> DataTable.create(raw, tableConverter);
DocStringTransformer<Object> toDocString = (String input) -> input;
return new StepExpression(expression, toDocString, toDataTable);
}

public StepExpression createExpression(String expressionString, Type tableOrDocStringType) {
Expand All @@ -64,26 +53,20 @@ public StepExpression createExpression(String expressionString, final TypeResolv
throw registerTypeInConfiguration(expressionString, e);
}

RawTableTransformer<?> tableTransform = new RawTableTransformer<Object>() {
@Override
public Object transform(List<List<String>> raw) {
DataTable dataTable = DataTable.create(raw, StepExpressionFactory.this.tableConverter);
Type targetType = tableOrDocStringType.resolve();
return dataTable.convert(Object.class.equals(targetType) ? DataTable.class : targetType, transpose);
}
RawTableTransformer<?> tableTransform = (List<List<String>> raw) -> {
DataTable dataTable = DataTable.create(raw, StepExpressionFactory.this.tableConverter);
Type targetType = tableOrDocStringType.resolve();
return dataTable.convert(Object.class.equals(targetType) ? DataTable.class : targetType, transpose);
};

DocStringTransformer<?> docStringTransform = new DocStringTransformer<Object>() {
@Override
public Object transform(String docString) {
Type targetType = tableOrDocStringType.resolve();
if (Object.class.equals(targetType)) {
return docString;
}

List<List<String>> raw = singletonList(singletonList(docString));
return DataTable.create(raw, StepExpressionFactory.this.tableConverter).convert(targetType, transpose);
DocStringTransformer<?> docStringTransform = (String docString) -> {
Type targetType = tableOrDocStringType.resolve();
if (Object.class.equals(targetType)) {
return docString;
}

List<List<String>> raw = singletonList(singletonList(docString));
return DataTable.create(raw, StepExpressionFactory.this.tableConverter).convert(targetType, transpose);
};
return new StepExpression(expression, docStringTransform, tableTransform);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public void should_apply_identity_transform_to_data_table_when_target_type_is_ob
assertEquals(DataTable.create(singletonList(singletonList("content"))), arguments.get(0).getValue());
}

@Test
public void should_convert_empty_pickle_table_cells_to_null_values() {
StubStepDefinition stub = new StubStepDefinition("I have some step", Object.class);
CoreStepDefinition stepDefinition = new CoreStepDefinition(stub, typeRegistry);

PickleTable table = new PickleTable(singletonList(new PickleRow(singletonList(new PickleCell(null, "")))));
List<Argument> arguments = stepDefinition.matchedArguments(new PickleStep("I have some step", singletonList(table), emptyList()));
assertEquals(DataTable.create(singletonList(singletonList(null))), arguments.get(0).getValue());
}


public static class StepDefs {
public void listOfListOfDoubles(List<List<Double>> listOfListOfDoubles) {
Expand Down

0 comments on commit cf64eff

Please sign in to comment.