New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(feat) hasTableCell()
: Match the string value of cell item.
#210
Conversation
Thanks for the PR. Never mind the failing tests as long as the test run with framebuffer passes. We could add a table with integers to |
We could improve the cohesion and readability with the introduction of private static boolean hasCellValue(Cell cell,
Object 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);
} Here is a modified public class TableViewMatchersTest extends FxRobot {
//---------------------------------------------------------------------------------------------
// FIELDS.
//---------------------------------------------------------------------------------------------
@Rule
public ExpectedException exception = ExpectedException.none();
public TableView<Map> tableView;
//---------------------------------------------------------------------------------------------
// FIXTURE METHODS.
//---------------------------------------------------------------------------------------------
@BeforeClass
public static void setupSpec() throws Exception {
FxToolkit.registerPrimaryStage();
}
@Before
public void setup() throws Exception {
FxToolkit.setupSceneRoot(() -> {
tableView = new TableView<>();
tableView.setItems(observableArrayList(
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"));
TableColumn<Map, Integer> tableColumn1 = new TableColumn<>("age");
tableColumn1.setCellValueFactory(new MapValueFactory<>("age"));
tableView.getColumns().setAll(tableColumn0, tableColumn1);
return new StackPane(tableView);
});
FxToolkit.showStage();
}
//---------------------------------------------------------------------------------------------
// FEATURE METHODS.
//---------------------------------------------------------------------------------------------
@Test
public void hasTableCell() {
// expect:
assertThat(tableView, TableViewMatchers.hasTableCell("alice"));
assertThat(tableView, TableViewMatchers.hasTableCell("bob"));
}
@Test
public void hasTableCell_fails() {
// expect:
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has table cell \"foobar\"\n");
assertThat(tableView, TableViewMatchers.hasTableCell("foobar"));
}
@Test
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 \"null\"\n");
assertThat(tableView, TableViewMatchers.hasTableCell(null));
}
@Test
public void hasItems() {
// expect:
assertThat(tableView, TableViewMatchers.hasItems(4));
}
@Test
public void hasItems_fails() {
// expect:
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has 0 items\n");
assertThat(tableView, TableViewMatchers.hasItems(0));
}
} |
dcb71ca
to
3b65895
Compare
I've added in your tests and made your readability fixes. I've also made another pull request to address the Travis issue (at least as I understand it). If your happy with that one, we can merge it first and I can rebase the changes into this one (so the Travis build passes) before we merge. |
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.
3b65895
to
345fe39
Compare
hasTableCell()
: Match the string value of cell item.
(feat) `hasTableCell()`: Match the string value of cell item.
Your rebased commit passed the tests, so I merged this. Very good work. |
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.