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

Allow for matching a table row without specifying index. #303

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
Expand Down Expand Up @@ -65,10 +67,18 @@ public static Matcher<Node> hasItems(int amount) {

@Factory
@Unstable(reason = "is missing apidocs")
public static Matcher<Node> containsRow(int rowIndex, Object...cells) {
public static Matcher<Node> containsRowAtIndex(int rowIndex, Object...cells) {
String descriptionText = "has row: " + Arrays.toString(cells);
return typeSafeMatcher(TableView.class, descriptionText,
node -> containsRow(node, rowIndex, cells));
node -> containsRowAtIndex(node, rowIndex, cells));
}

@Factory
@Unstable(reason = "is missing apidocs")
public static Matcher<Node> containsRow(Object...cells) {
String descriptionText = "has row: " + Arrays.toString(cells);
return typeSafeMatcher(TableView.class, descriptionText,
node -> containsRow(node, cells));
}

//---------------------------------------------------------------------------------------------
Expand All @@ -89,8 +99,8 @@ private static boolean hasItems(TableView tableView,
return tableView.getItems().size() == amount;
}

private static <T> boolean containsRow(TableView<T> tableView, int rowIndex,
Object...cells) {
private static <T> boolean containsRowAtIndex(TableView<T> tableView, int rowIndex,
Object...cells) {
if (rowIndex >= tableView.getItems().size()) {
return false;
}
Expand All @@ -99,7 +109,8 @@ private static <T> boolean containsRow(TableView<T> tableView, int rowIndex,
List<ObservableValue<?>> rowValues = new ArrayList<>(tableView.getColumns().size());
for (int i = 0; i < tableView.getColumns().size(); i++) {
TableColumn<T, ?> column = tableView.getColumns().get(i);
TableColumn.CellDataFeatures cellDataFeatures = new TableColumn.CellDataFeatures<>(tableView, column, rowObject);
TableColumn.CellDataFeatures cellDataFeatures = new TableColumn.CellDataFeatures<>(
tableView, column, rowObject);
rowValues.add(i, column.getCellValueFactory().call(cellDataFeatures));
}
for (int i = 0; i < cells.length; i++) {
Expand All @@ -116,6 +127,50 @@ private static <T> boolean containsRow(TableView<T> tableView, int rowIndex,
return true;
}

private static <T> boolean containsRow(TableView<T> tableView, Object...cells) {
if (tableView.getItems().isEmpty()) {
return false;
}

Map<Integer, List<ObservableValue<?>>> rowValuesMap = new HashMap<>(tableView.getColumns().size());
for (int j = 0; j < tableView.getItems().size(); j++) {
T rowObject = tableView.getItems().get(j);
List<ObservableValue<?>> rowValues = new ArrayList<>(tableView.getColumns().size());

for (int i = 0; i < tableView.getColumns().size(); i++) {
TableColumn<T, ?> column = tableView.getColumns().get(i);
TableColumn.CellDataFeatures cellDataFeatures = new TableColumn.CellDataFeatures<>(
tableView, column, rowObject);
rowValues.add(i, column.getCellValueFactory().call(cellDataFeatures));
}
rowValuesMap.put(j, rowValues);
}

for (List<ObservableValue<?>> rowValues : rowValuesMap.values()) {
for (int i = 0; i < cells.length; i++) {
if (rowValues.get(i).getValue() == null && cells[i] != null) {
break;
} else if (cells[i] == null && rowValues.get(i).getValue() != null) {
break;
} else if (rowValues.get(i).getValue() != null && !rowValues.get(i).getValue().equals(cells[i])) {
break;
} else {
if (i == cells.length - 1) {
if (rowValues.get(i).getValue() == null && cells[i] != null) {
break;
} else if (cells[i] == null && rowValues.get(i).getValue() != null) {
break;
} else {
return true;
}
}
}
}
}

return false;
}

private static boolean hasCellValue(Cell cell,
Object value) {
return !cell.isEmpty() && hasItemValue(cell.getItem(), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import static javafx.collections.FXCollections.observableArrayList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;

public class TableViewMatchersTest extends FxRobot {

Expand Down Expand Up @@ -128,7 +129,7 @@ public void hasItems_fails() {
}

@Test
public void containsRow() {
public void containsRowAtIndex() {
tableView.setItems(observableArrayList(
ImmutableMap.of("name", "alice", "age", 30),
ImmutableMap.of("name", "bob", "age", 31),
Expand All @@ -137,43 +138,99 @@ public void containsRow() {
));

// expect:
assertThat(tableView, TableViewMatchers.containsRow(0, "alice", 30));
assertThat(tableView, TableViewMatchers.containsRow(1, "bob", 31));
assertThat(tableView, TableViewMatchers.containsRow(2, "carol", 42));
assertThat(tableView, TableViewMatchers.containsRow(3, "dave", 55));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(0, "alice", 30));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(1, "bob", 31));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(2, "carol", 42));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(3, "dave", 55));
assertThat(tableView, not(TableViewMatchers.containsRowAtIndex(0, "ebert", 49)));
assertThat(tableView, not(TableViewMatchers.containsRowAtIndex(1, "alice", 30)));
assertThat(tableView, not(TableViewMatchers.containsRowAtIndex(4, "ebert", 49)));
}

@Test
public void containsRow_with_empty_cells() {
public void containsRowAtIndex_with_empty_cells() {
tableView.setItems(observableArrayList(
ImmutableMap.of("name", "alice", "age", 30),
ImmutableMap.of("name", "bob", "age", 31),
ImmutableMap.of("name", "carol"),
ImmutableMap.of("name", "dave")
));
// expect:
assertThat(tableView, TableViewMatchers.containsRow(0, "alice", 30));
assertThat(tableView, TableViewMatchers.containsRow(1, "bob", 31));
assertThat(tableView, TableViewMatchers.containsRow(2, "carol", null));
assertThat(tableView, TableViewMatchers.containsRow(3, "dave", null));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(0, "alice", 30));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(1, "bob", 31));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(2, "carol", null));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(3, "dave", null));
assertThat(tableView, not(TableViewMatchers.containsRowAtIndex(0, "ebert", null)));
assertThat(tableView, not(TableViewMatchers.containsRowAtIndex(3, "carol", null)));
}

@Test
public void containsRow_no_such_row_fails() {
public void containsRowAtIndex_no_such_row_fails() {
// expect:
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has row: [jerry, 29]\n");

assertThat(tableView, TableViewMatchers.containsRow(0, "jerry", 29));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(0, "jerry", 29));
}

@Test
public void containsRow_out_of_bounds_fails() {
public void containsRowAtIndex_out_of_bounds_fails() {
// expect:
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has row: [tom, 54]\n");

assertThat(tableView, TableViewMatchers.containsRow(4, "tom", 54));
assertThat(tableView, TableViewMatchers.containsRowAtIndex(4, "tom", 54));
}

@Test
public void containsRowAtIndex_wrong_types_fails() {
// expect:
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has row: [63, deedee]\n");

assertThat(tableView, TableViewMatchers.containsRowAtIndex(1, 63, "deedee"));
}

@Test
public void containsRow() {
tableView.setItems(observableArrayList(
ImmutableMap.of("name", "alice", "age", 30),
ImmutableMap.of("name", "bob", "age", 31),
ImmutableMap.of("name", "carol", "age", 42),
ImmutableMap.of("name", "dave", "age", 55)
));

// expect:
assertThat(tableView, TableViewMatchers.containsRow("alice", 30));
assertThat(tableView, TableViewMatchers.containsRow("bob", 31));
assertThat(tableView, TableViewMatchers.containsRow("carol", 42));
assertThat(tableView, TableViewMatchers.containsRow("dave", 55));
assertThat(tableView, not(TableViewMatchers.containsRow("ebert", 49)));
}

@Test
public void containsRow_with_empty_cells() {
tableView.setItems(observableArrayList(
ImmutableMap.of("name", "alice", "age", 30),
ImmutableMap.of("name", "bob", "age", 31),
ImmutableMap.of("name", "carol"),
ImmutableMap.of("name", "dave")
));
// expect:
assertThat(tableView, TableViewMatchers.containsRow("alice", 30));
assertThat(tableView, TableViewMatchers.containsRow("bob", 31));
assertThat(tableView, TableViewMatchers.containsRow("carol", null));
assertThat(tableView, TableViewMatchers.containsRow("dave", null));
assertThat(tableView, not(TableViewMatchers.containsRow("ebert", null)));
}

@Test
public void containsRow_no_such_row_fails() {
// expect:
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has row: [jerry, 29]\n");

assertThat(tableView, TableViewMatchers.containsRow("jerry", 29));
}

@Test
Expand All @@ -182,6 +239,6 @@ public void containsRow_wrong_types_fails() {
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has row: [63, deedee]\n");

assertThat(tableView, TableViewMatchers.containsRow(1, 63, "deedee"));
assertThat(tableView, TableViewMatchers.containsRow(63, "deedee"));
}
}