Skip to content

Commit 403c45f

Browse files
committed
datatable/java: all asX should behave consistently
Fixes: #2262
1 parent d7337a1 commit 403c45f

File tree

4 files changed

+59
-25
lines changed

4 files changed

+59
-25
lines changed

java/datatable/src/main/java/io/cucumber/datatable/DataTable.java

+47-13
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,30 @@ public void unorderedDiff(DataTable actual) throws TableDiffException {
166166
*
167167
* @return the cells of the table
168168
*/
169-
public List<String> asList() {
169+
List<String> toList() {
170170
return new ListView();
171171
}
172172

173+
/**
174+
* Converts the table to a list of {@code String}s. Contains the cells
175+
* ordered from left to right, top to bottom starting at the top left.
176+
*
177+
* @return a list of strings
178+
*
179+
* @see TableConverter#toList(DataTable, Type)
180+
*/
181+
public List<String> asList() {
182+
return asList(String.class);
183+
}
184+
173185
/**
174186
* Converts the table to a list of {@code itemType}.
175187
*
176188
* @param itemType the type of the list items
177189
* @param <T> the type of the list items
178-
* @return a List of objects
190+
* @return a list of objects
191+
*
192+
* @see TableConverter#toList(DataTable, Type)
179193
*/
180194
public <T> List<T> asList(Type itemType) {
181195
return tableConverter.toList(this, itemType);
@@ -186,7 +200,7 @@ public <T> List<T> asList(Type itemType) {
186200
*
187201
* @return the cells of the table
188202
*/
189-
public List<List<String>> asLists() {
203+
List<List<String>> toLists() {
190204
return cells();
191205
}
192206

@@ -195,8 +209,8 @@ public List<List<String>> asLists() {
195209
*
196210
* @return the cells of the table
197211
*/
198-
public List<List<String>> cells() {
199-
return raw;
212+
public List<List<String>> asLists() {
213+
return asLists(String.class);
200214
}
201215

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

230-
/**
231-
* Converts the table to a list of maps of strings. For each row in the body
232-
* of the table a map is created containing a mapping of column headers to
233-
* the column cell of that row.
234-
*
235-
* @return a list of maps
236-
*/
237-
public List<Map<String, String>> asMaps() {
244+
List<Map<String, String>> toMaps() {
238245
if (raw.isEmpty()) return emptyList();
239246

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

268+
/**
269+
* Converts the table to a list of maps of strings. For each row in the body
270+
* of the table a map is created containing a mapping of column headers to
271+
* the column cell of that row.
272+
*
273+
* @return a list of maps
274+
*/
275+
public List<Map<String, String>> asMaps() {
276+
if (!hasConverter()) {
277+
return toMaps();
278+
}
279+
return asMaps(String.class, String.class);
280+
}
281+
261282
/**
262283
* Converts the table to a list of maps of {@code keyType} to {@code valueType}.
263284
* For each row in the body of the table a map is created containing a mapping
@@ -273,6 +294,19 @@ public <K, V> List<Map<K, V>> asMaps(Type keyType, Type valueType) {
273294
return tableConverter.toMaps(this, keyType, valueType);
274295
}
275296

297+
private boolean hasConverter() {
298+
return !(tableConverter instanceof NoConverterDefined);
299+
}
300+
301+
/**
302+
* Returns the cells of the table.
303+
*
304+
* @return the cells of the table
305+
*/
306+
public List<List<String>> cells() {
307+
return raw;
308+
}
309+
276310
/**
277311
* Returns a single table cell.
278312
*

java/datatable/src/main/java/io/cucumber/datatable/DataTableType.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public Class<?> getOriginalTransformerType() {
328328
public List<T> transform(List<List<String>> raw) throws Throwable {
329329
DataTable table = DataTable.create(raw, CONVERSION_REQUIRED);
330330
List<T> list = new ArrayList<>();
331-
for (Map<String, String> entry : table.asMaps()) {
331+
for (Map<String, String> entry : table.toMaps()) {
332332
list.add(transformer.transform(entry));
333333
}
334334

java/datatable/src/test/java/io/cucumber/datatable/DataTableTest.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ void columns_should_view_sub_table() {
313313
void as_lists_should_equal_raw() {
314314
List<List<String>> raw = asList(asList("hundred", "100"), asList("thousand", "1000"));
315315
DataTable table = DataTable.create(raw);
316-
assertEquals(raw, table.asLists());
316+
assertEquals(raw, table.toLists());
317317
}
318318

319319
@Test
@@ -414,7 +414,7 @@ void asLists_returns_raw_rows_in_order() {
414414
asList("2", "1000")
415415
);
416416
DataTable table = DataTable.create(raw);
417-
assertEquals(asList("1", "100", "2", "1000"), table.asList());
417+
assertEquals(asList("1", "100", "2", "1000"), table.toList());
418418
}
419419

420420
@Test
@@ -424,7 +424,7 @@ void asLists_throws_for_large_index() {
424424
asList("2", "1000")
425425
);
426426
DataTable table = DataTable.create(raw);
427-
assertThrows(IndexOutOfBoundsException.class, () -> table.asList().get(5));
427+
assertThrows(IndexOutOfBoundsException.class, () -> table.toList().get(5));
428428
}
429429

430430
@Test
@@ -434,7 +434,7 @@ void asLists_throws_for_negative_index() {
434434
asList("2", "1000")
435435
);
436436
DataTable table = DataTable.create(raw);
437-
assertThrows(IndexOutOfBoundsException.class, () -> table.asList().get(-1));
437+
assertThrows(IndexOutOfBoundsException.class, () -> table.toList().get(-1));
438438
}
439439

440440
@Test
@@ -458,7 +458,7 @@ void asLists_returns_raw() {
458458
asList("2", "1000")
459459
);
460460
DataTable table = DataTable.create(raw);
461-
assertEquals(raw, table.asLists());
461+
assertEquals(raw, table.toLists());
462462
}
463463

464464
@Test
@@ -477,7 +477,7 @@ void asMaps_returns_maps_of_raw() {
477477
put("100", "1000");
478478
}};
479479

480-
assertEquals(singletonList(expected), table.asMaps());
480+
assertEquals(singletonList(expected), table.toMaps());
481481
}
482482

483483
@Test
@@ -492,7 +492,7 @@ void asMaps_can_convert_table_with_null_values() {
492492
put("2", null);
493493
}};
494494

495-
assertEquals(singletonList(expected), table.asMaps());
495+
assertEquals(singletonList(expected), table.toMaps());
496496
}
497497

498498
@Test
@@ -505,7 +505,7 @@ void asMaps_cant_convert_table_with_duplicate_keys() {
505505

506506
CucumberDataTableException exception = assertThrows(
507507
CucumberDataTableException.class,
508-
table::asMaps
508+
table::toMaps
509509
);
510510

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

524524
CucumberDataTableException exception = assertThrows(
525525
CucumberDataTableException.class,
526-
table::asMaps
526+
table::toMaps
527527
);
528528
assertThat(exception.getMessage(), is(format("" +
529529
"Can't convert DataTable to Map<%s, %s>.\n" +
@@ -538,7 +538,7 @@ void asMaps_with_nulls_returns_maps_of_raw() {
538538
put(null, null);
539539
}};
540540

541-
assertEquals(singletonList(expected), table.asMaps());
541+
assertEquals(singletonList(expected), table.toMaps());
542542
}
543543

544544
@Test

java/datatable/src/test/java/io/cucumber/datatable/DataTableTypeRegistryTableConverterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class DataTableTypeRegistryTableConverterTest {
9595
}.getType();
9696
public static final Type OPTIONAL_CHESS_BOARD_TYPE = new TypeReference<Optional<ChessBoard>>() {
9797
}.getType();
98-
private static final TableTransformer<ChessBoard> CHESS_BOARD_TABLE_TRANSFORMER = table -> new ChessBoard(table.subTable(1, 1).asList());
98+
private static final TableTransformer<ChessBoard> CHESS_BOARD_TABLE_TRANSFORMER = table -> new ChessBoard(table.subTable(1, 1).toList());
9999
private static final TableCellTransformer<Piece> PIECE_TABLE_CELL_TRANSFORMER = Piece::fromString;
100100
private static final TableCellTransformer<AirPortCode> AIR_PORT_CODE_TABLE_CELL_TRANSFORMER = AirPortCode::new;
101101
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

0 commit comments

Comments
 (0)