Skip to content
This repository has been archived by the owner on Sep 13, 2021. It is now read-only.

Commit

Permalink
datatable/java: all asX should behave consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Mar 12, 2021
1 parent 36d9c61 commit 2d561f3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
60 changes: 47 additions & 13 deletions datatable/src/main/java/io/cucumber/datatable/DataTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,30 @@ public void unorderedDiff(DataTable actual) throws TableDiffException {
*
* @return the cells of the table
*/
public List<String> asList() {
List<String> toList() {
return new ListView();
}

/**
* Converts the table to a list of {@code String}s. Contains the cells
* ordered from left to right, top to bottom starting at the top left.
*
* @return a list of strings
*
* @see TableConverter#toList(DataTable, Type)
*/
public List<String> asList() {
return asList(String.class);
}

/**
* Converts the table to a list of {@code itemType}.
*
* @param itemType the type of the list items
* @param <T> the type of the list items
* @return a List of objects
* @return a list of objects
*
* @see TableConverter#toList(DataTable, Type)
*/
public <T> List<T> asList(Type itemType) {
return tableConverter.toList(this, itemType);
Expand All @@ -186,7 +200,7 @@ public <T> List<T> asList(Type itemType) {
*
* @return the cells of the table
*/
public List<List<String>> asLists() {
List<List<String>> toLists() {
return cells();
}

Expand All @@ -195,8 +209,8 @@ public List<List<String>> asLists() {
*
* @return the cells of the table
*/
public List<List<String>> cells() {
return raw;
public List<List<String>> asLists() {
return asLists(String.class);
}

/**
Expand Down Expand Up @@ -227,14 +241,7 @@ public <K, V> Map<K, V> asMap(Type keyType, Type valueType) {
return tableConverter.toMap(this, keyType, valueType);
}

/**
* Converts the table to a list of maps of strings. For each row in the body
* of the table a map is created containing a mapping of column headers to
* the column cell of that row.
*
* @return a list of maps
*/
public List<Map<String, String>> asMaps() {
List<Map<String, String>> toMaps() {
if (raw.isEmpty()) return emptyList();

List<String> headers = raw.get(0);
Expand All @@ -258,6 +265,20 @@ public List<Map<String, String>> asMaps() {
return unmodifiableList(headersAndRows);
}

/**
* Converts the table to a list of maps of strings. For each row in the body
* of the table a map is created containing a mapping of column headers to
* the column cell of that row.
*
* @return a list of maps
*/
public List<Map<String, String>> asMaps() {
if (!hasConverter()) {
return toMaps();
}
return asMaps(String.class, String.class);
}

/**
* Converts the table to a list of maps of {@code keyType} to {@code valueType}.
* For each row in the body of the table a map is created containing a mapping
Expand All @@ -273,6 +294,19 @@ public <K, V> List<Map<K, V>> asMaps(Type keyType, Type valueType) {
return tableConverter.toMaps(this, keyType, valueType);
}

private boolean hasConverter() {
return !(tableConverter instanceof NoConverterDefined);
}

/**
* Returns the cells of the table.
*
* @return the cells of the table
*/
public List<List<String>> cells() {
return raw;
}

/**
* Returns a single table cell.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public Class<?> getOriginalTransformerType() {
public List<T> transform(List<List<String>> raw) throws Throwable {
DataTable table = DataTable.create(raw, CONVERSION_REQUIRED);
List<T> list = new ArrayList<>();
for (Map<String, String> entry : table.asMaps()) {
for (Map<String, String> entry : table.toMaps()) {
list.add(transformer.transform(entry));
}

Expand Down
20 changes: 10 additions & 10 deletions datatable/src/test/java/io/cucumber/datatable/DataTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ void columns_should_view_sub_table() {
void as_lists_should_equal_raw() {
List<List<String>> raw = asList(asList("hundred", "100"), asList("thousand", "1000"));
DataTable table = DataTable.create(raw);
assertEquals(raw, table.asLists());
assertEquals(raw, table.toLists());
}

@Test
Expand Down Expand Up @@ -414,7 +414,7 @@ void asLists_returns_raw_rows_in_order() {
asList("2", "1000")
);
DataTable table = DataTable.create(raw);
assertEquals(asList("1", "100", "2", "1000"), table.asList());
assertEquals(asList("1", "100", "2", "1000"), table.toList());
}

@Test
Expand All @@ -424,7 +424,7 @@ void asLists_throws_for_large_index() {
asList("2", "1000")
);
DataTable table = DataTable.create(raw);
assertThrows(IndexOutOfBoundsException.class, () -> table.asList().get(5));
assertThrows(IndexOutOfBoundsException.class, () -> table.toList().get(5));
}

@Test
Expand All @@ -434,7 +434,7 @@ void asLists_throws_for_negative_index() {
asList("2", "1000")
);
DataTable table = DataTable.create(raw);
assertThrows(IndexOutOfBoundsException.class, () -> table.asList().get(-1));
assertThrows(IndexOutOfBoundsException.class, () -> table.toList().get(-1));
}

@Test
Expand All @@ -458,7 +458,7 @@ void asLists_returns_raw() {
asList("2", "1000")
);
DataTable table = DataTable.create(raw);
assertEquals(raw, table.asLists());
assertEquals(raw, table.toLists());
}

@Test
Expand All @@ -477,7 +477,7 @@ void asMaps_returns_maps_of_raw() {
put("100", "1000");
}};

assertEquals(singletonList(expected), table.asMaps());
assertEquals(singletonList(expected), table.toMaps());
}

@Test
Expand All @@ -492,7 +492,7 @@ void asMaps_can_convert_table_with_null_values() {
put("2", null);
}};

assertEquals(singletonList(expected), table.asMaps());
assertEquals(singletonList(expected), table.toMaps());
}

@Test
Expand All @@ -505,7 +505,7 @@ void asMaps_cant_convert_table_with_duplicate_keys() {

CucumberDataTableException exception = assertThrows(
CucumberDataTableException.class,
table::asMaps
table::toMaps
);

assertThat(exception.getMessage(), is(format("" +
Expand All @@ -523,7 +523,7 @@ void asMaps_cant_convert_table_with_duplicate_null_keys() {

CucumberDataTableException exception = assertThrows(
CucumberDataTableException.class,
table::asMaps
table::toMaps
);
assertThat(exception.getMessage(), is(format("" +
"Can't convert DataTable to Map<%s, %s>.\n" +
Expand All @@ -538,7 +538,7 @@ void asMaps_with_nulls_returns_maps_of_raw() {
put(null, null);
}};

assertEquals(singletonList(expected), table.asMaps());
assertEquals(singletonList(expected), table.toMaps());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class DataTableTypeRegistryTableConverterTest {
}.getType();
public static final Type OPTIONAL_CHESS_BOARD_TYPE = new TypeReference<Optional<ChessBoard>>() {
}.getType();
private static final TableTransformer<ChessBoard> CHESS_BOARD_TABLE_TRANSFORMER = table -> new ChessBoard(table.subTable(1, 1).asList());
private static final TableTransformer<ChessBoard> CHESS_BOARD_TABLE_TRANSFORMER = table -> new ChessBoard(table.subTable(1, 1).toList());
private static final TableCellTransformer<Piece> PIECE_TABLE_CELL_TRANSFORMER = Piece::fromString;
private static final TableCellTransformer<AirPortCode> AIR_PORT_CODE_TABLE_CELL_TRANSFORMER = AirPortCode::new;
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
Expand Down

0 comments on commit 2d561f3

Please sign in to comment.