Skip to content

Commit

Permalink
(feat) hasTableCell matches the string value of cell item
Browse files Browse the repository at this point in the history
In some cases, it is useful to match a string value against a table
cell's value, regardless of type. For example, when testing a table
of doubles, the string comparison might allow for small discrepancies
in the digital representation that are imperceptible to the user.
This is consistent with BDD principles: implementation details aren't
relevant in testing.

In testfx-legacy, this functionality is provided in the
containsCell method. Without adding a suitable replacement in
testfx-core, it will be more difficult for some developers to
migrate to TestFX 4.
  • Loading branch information
johnzeringue committed Mar 29, 2015
1 parent dbc94fe commit 345fe39
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
Expand Up @@ -78,7 +78,12 @@ private static boolean hasItems(TableView tableView,

private static boolean hasCellValue(Cell cell,
Object value) {
return !cell.isEmpty() && Objects.equals(cell.getItem(), value);
return !cell.isEmpty() && hasItemValue(cell.getItem(), value);
}

private static boolean hasItemValue(Object item,
Object value) {
return Objects.equals(item, value) || Objects.equals(item.toString(), value);
}

}
Expand Up @@ -59,14 +59,16 @@ public void setup() throws Exception {
FxToolkit.setupSceneRoot(() -> {
tableView = new TableView<>();
tableView.setItems(observableArrayList(
ImmutableMap.of("name", "alice"),
ImmutableMap.of("name", "bob"),
ImmutableMap.of("name", "carol"),
ImmutableMap.of("name", "dave")
));
ImmutableMap.of("name", "alice", "age", 30),
ImmutableMap.of("name", "bob", "age", 31),
ImmutableMap.of("name", "carol"),
ImmutableMap.of("name", "dave")
));
TableColumn<Map, String> tableColumn0 = new TableColumn<>("name");
tableColumn0.setCellValueFactory(new MapValueFactory<>("name"));
tableView.getColumns().add(tableColumn0);
TableColumn<Map, Integer> tableColumn1 = new TableColumn<>("age");
tableColumn1.setCellValueFactory(new MapValueFactory<>("age"));
tableView.getColumns().setAll(tableColumn0, tableColumn1);
return new StackPane(tableView);
});
FxToolkit.showStage();
Expand All @@ -80,24 +82,34 @@ public void setup() throws Exception {
public void hasTableCell() {
// expect:
assertThat(tableView, TableViewMatchers.hasTableCell("alice"));
assertThat(tableView, TableViewMatchers.hasTableCell("bob"));
}

@Test
public void hasTableCell_with_null_fails() {
public void hasTableCell_fails() {
// expect:
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has table cell \"null\"\n");
exception.expectMessage("Expected: TableView has table cell \"foobar\"\n");

assertThat(tableView, TableViewMatchers.hasTableCell(null));
assertThat(tableView, TableViewMatchers.hasTableCell("foobar"));
}

@Test
public void hasTableCell_fails() {
public void hasTableCell_with_toString() {
// expect:
assertThat(tableView, TableViewMatchers.hasTableCell("30"));

// and:
assertThat(tableView, TableViewMatchers.hasTableCell(31));
}

@Test
public void hasTableCell_with_null_fails() {
// expect:
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has table cell \"foobar\"\n");
exception.expectMessage("Expected: TableView has table cell \"null\"\n");

assertThat(tableView, TableViewMatchers.hasTableCell("foobar"));
assertThat(tableView, TableViewMatchers.hasTableCell(null));
}

@Test
Expand Down

0 comments on commit 345fe39

Please sign in to comment.