Skip to content
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

Merged
merged 1 commit into from Mar 30, 2015

Conversation

johnzeringue
Copy link
Contributor

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.

@hastebrot
Copy link
Member

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 TableViewMatchersTest. And maybe table cells with null value to catch edge-cases.

@hastebrot
Copy link
Member

We could improve the cohesion and readability with the introduction of hasItemValue() in TableViewMatchers:

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 TableViewMatchersTests with tests for the toString() calls.

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));
    }

}

@johnzeringue
Copy link
Contributor Author

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.
@johnzeringue johnzeringue changed the title hasTableCell matches the string value of cell item (feat) hasTableCell matches the string value of cell item Mar 29, 2015
@hastebrot hastebrot changed the title (feat) hasTableCell matches the string value of cell item (feat) hasTableCell(): Match the string value of cell item. Mar 30, 2015
hastebrot added a commit that referenced this pull request Mar 30, 2015
(feat) `hasTableCell()`: Match the string value of cell item.
@hastebrot hastebrot merged commit e15989b into TestFX:master Mar 30, 2015
@hastebrot
Copy link
Member

Your rebased commit passed the tests, so I merged this. Very good work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants