DataTableTypeRegistryTableConverter implements TableConverter { @Override public <T> List<T> toList(DataTable dataTable, Type itemType) { requireNonNull(dataTable, "dataTable may not be null"); requireNonNull(itemType, "itemType may not be null"); if (dataTable.isEmpty()) { return emptyList(); } ListOrProblems<T> result = toListOrProblems(dataTable, itemType); if (result.hasList()) { return unmodifiableList(result.getList()); } throw listNoConverterDefined( itemType, result.getProblems() ); } DataTableTypeRegistryTableConverter(DataTableTypeRegistry registry); @Override T convert(DataTable dataTable, Type type); @Override @SuppressWarnings("unchecked") T convert(DataTable dataTable, Type type, boolean transposed); @Override List<T> toList(DataTable dataTable, Type itemType); @Override @SuppressWarnings("unchecked") List<List<T>> toLists(DataTable dataTable, Type itemType); @Override Map<K, V> toMap(DataTable dataTable, Type keyType, Type valueType); @Override @SuppressWarnings("unchecked") List<Map<K, V>> toMaps(DataTable dataTable, Type keyType, Type valueType);  }
@Test void to_list__single_column__throws_exception__register_transformer() { DataTable table = parse("", "| ♘ |", "| ♝ |" ); CucumberDataTableException exception = assertThrows( CucumberDataTableException.class, () -> converter.toList(table, Piece.class) ); assertThat(exception.getMessage(), is("" + "Can't convert DataTable to List<io.cucumber.datatable.DataTableTypeRegistryTableConverterTest$Piece>.\n" + "Please review these problems:\n" + "\n" + " - There was no table entry or table row transformer registered for io.cucumber.datatable.DataTableTypeRegistryTableConverterTest$Piece.\n" + " Please consider registering a table entry or row transformer.\n" + "\n" + " - There was no table cell transformer registered for io.cucumber.datatable.DataTableTypeRegistryTableConverterTest$Piece.\n" + " Please consider registering a table cell transformer.\n" + "\n" + " - There was no default table entry transformer registered to transform io.cucumber.datatable.DataTableTypeRegistryTableConverterTest$Piece.\n" + " Please consider registering a default table entry transformer.\n" + "\n" + " - There was no default table cell transformer registered to transform io.cucumber.datatable.DataTableTypeRegistryTableConverterTest$Piece.\n" + " Please consider registering a default table cell transformer.\n" + "\n" + "Note: Usually solving one is enough" )); }
@Test void to_list_of_unknown_type__throws_exception() { DataTable table = parse("", " | firstName | lastName | birthDate |", " | Annie M. G. | Schmidt | 1911-03-20 |", " | Roald | Dahl | 1916-09-13 |", " | Astrid | Lindgren | 1907-11-14 |" ); CucumberDataTableException exception = assertThrows( CucumberDataTableException.class, () -> converter.toList(table, Author.class) ); assertThat(exception.getMessage(), is("" + "Can't convert DataTable to List<io.cucumber.datatable.DataTableTypeRegistryTableConverterTest$Author>.\n" + "Please review these problems:\n" + "\n" + " - There was no table entry or table row transformer registered for io.cucumber.datatable.DataTableTypeRegistryTableConverterTest$Author.\n" + " Please consider registering a table entry or row transformer.\n" + "\n" + " - There was no default table entry transformer registered to transform io.cucumber.datatable.DataTableTypeRegistryTableConverterTest$Author.\n" + " Please consider registering a default table entry transformer.\n" + "\n" + "Note: Usually solving one is enough" )); }
@Test void convert_to_optional_list() { DataTable table = parse("", "| 11.22 |", "| 255.999 |", "| |" ); List<Optional<BigDecimal>> expected = asList( Optional.of(new BigDecimal("11.22")), Optional.of(new BigDecimal("255.999")), Optional.empty() ); assertEquals(expected, converter.toList(table, OPTIONAL_BIG_DECIMAL)); }
@Test void convert_to_list__double_column__throws_exception() { DataTable table = parse("", "| 3 | 5 |", "| 6 | 7 |" ); CucumberDataTableException exception = assertThrows( CucumberDataTableException.class, () -> converter.toList(table, Integer.class) ); assertThat(exception.getMessage(), is("" + "Can't convert DataTable to List<java.lang.Integer>.\n" + "Please review these problems:\n" + "\n" + " - There was a table cell transformer for java.lang.Integer but the table was too wide to use it.\n" + " Please reduce the table width to use this converter.\n" + "\n" + " - There was no table entry or table row transformer registered for java.lang.Integer.\n" + " Please consider registering a table entry or row transformer.\n" + "\n" + " - There was no default table entry transformer registered to transform java.lang.Integer.\n" + " Please consider registering a default table entry transformer.\n" + "\n" + "Note: Usually solving one is enough" )); }
@Test void convert_to_list__double_column__single_row__throws_exception() { DataTable table = parse("", "| 3 | 5 |" ); CucumberDataTableException exception = assertThrows( CucumberDataTableException.class, () -> converter.toList(table, Integer.class) ); assertThat(exception.getMessage(), is("" + "Can't convert DataTable to List<java.lang.Integer>.\n" + "Please review these problems:\n" + "\n" + " - There was a table cell transformer for java.lang.Integer but the table was too wide to use it.\n" + " Please reduce the table width to use this converter.\n" + "\n" + " - There was no table entry or table row transformer registered for java.lang.Integer.\n" + " Please consider registering a table entry or row transformer.\n" + "\n" + "Note: Usually solving one is enough" )); }