-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java] Support empty strings and null values in data tables
Empty data table cells can either be considered null or empty strings. For example table below can converted to a map as `{name=Aspiring Author, first publication=}` or `{name=Aspiring Author, first publication=null}`. ```gherkin | name | first publication | | Aspiring Author | | ``` And as demonstrated #1617 there are good reasons to default the empty table cell to null. However this does not cover all cases. There are however good use cases to use the empty string. By declaring a table transformer with a replacement string it becomes possible to explicitly disambiguate between the two scenarios. For example: ```gherkin Given some authors | name | first publication | | Aspiring Author | | | Ancient Author | [blank] | ``` ```java @DataTableType(replaceWithEmptyString = "[blank]") public Author convert(Map<String, String> entry){ return new Author( entry.get("name"), entry.get("first publication") ); } @given("some authors") public void given_some_authors(List<Author> authors){ // authors = [Author(name="Aspiring Author", firstPublication=null), Author(name="Ancient Author", firstPublication=)] } ```
- Loading branch information
1 parent
d2c28f1
commit 22cebf5
Showing
14 changed files
with
283 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
java/src/main/java/io/cucumber/java/AbstractDatatableElementTransformerDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.cucumber.java; | ||
|
||
import io.cucumber.core.backend.Lookup; | ||
import io.cucumber.datatable.DataTable; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static java.util.stream.Collectors.toMap; | ||
|
||
public class AbstractDatatableElementTransformerDefinition extends AbstractGlueDefinition { | ||
private final String[] emptyPatterns; | ||
|
||
AbstractDatatableElementTransformerDefinition(Method method, Lookup lookup, String[] emptyPatterns) { | ||
super(method, lookup); | ||
this.emptyPatterns = emptyPatterns; | ||
} | ||
|
||
|
||
List<String> replaceEmptyPatternsWithEmptyString(List<String> row) { | ||
return row.stream() | ||
.map(this::replaceEmptyPatternsWithEmptyString) | ||
.collect(toList()); | ||
} | ||
|
||
DataTable replaceEmptyPatternsWithEmptyString(DataTable table) { | ||
List<List<String>> rawWithEmptyStrings = table.cells().stream() | ||
.map(this::replaceEmptyPatternsWithEmptyString) | ||
.collect(toList()); | ||
|
||
return DataTable.create(rawWithEmptyStrings); //TODO: Add converter back in | ||
} | ||
|
||
Map<String, String> replaceEmptyPatternsWithEmptyString(Map<String, String> fromValue) { | ||
return fromValue.entrySet().stream() | ||
.collect(toMap( | ||
entry -> replaceEmptyPatternsWithEmptyString(entry.getKey()), | ||
entry -> replaceEmptyPatternsWithEmptyString(entry.getValue()), | ||
(s, s2) -> { | ||
throw createDuplicateKeyAfterReplacement(fromValue); | ||
} | ||
)); | ||
} | ||
|
||
private IllegalArgumentException createDuplicateKeyAfterReplacement(Map<String, String> fromValue) { | ||
List<String> conflict = new ArrayList<>(2); | ||
for (String emptyPattern : emptyPatterns) { | ||
if (fromValue.containsKey(emptyPattern)) { | ||
conflict.add(emptyPattern); | ||
} | ||
} | ||
String msg = "After replacing %s and %s with empty strings the datatable entry contains duplicate keys: %s"; | ||
return new IllegalArgumentException(String.format(msg, conflict.get(0), conflict.get(1), fromValue)); | ||
} | ||
|
||
String replaceEmptyPatternsWithEmptyString(String t) { | ||
for (String emptyPattern : emptyPatterns) { | ||
if (t.equals(emptyPattern)) { | ||
return ""; | ||
} | ||
} | ||
return t; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.