Skip to content

Commit

Permalink
Fixes for suggestions by @hastebrot for ComboBoxMatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
brcolow committed Nov 29, 2016
1 parent d066512 commit 03efef8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
package org.testfx.matcher.control;

import java.util.Arrays;
import java.util.Objects;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Labeled;
import javafx.scene.control.TableView;

import org.hamcrest.Factory;
import org.hamcrest.Matcher;
Expand All @@ -45,9 +42,9 @@ public static Matcher<Node> hasItems(int amount) {

@Factory
@Unstable(reason = "is missing apidocs")
public static <T> Matcher<Node> hasSelection(T selection) {
public static <T> Matcher<Node> hasSelectedItem(T selection) {
String descriptionText = "has selection " + selection;
return typeSafeMatcher(ComboBox.class, descriptionText, node -> hasSelection(node, selection));
return typeSafeMatcher(ComboBox.class, descriptionText, node -> hasSelectedItem(node, selection));
}

@Factory
Expand Down Expand Up @@ -87,8 +84,8 @@ private static boolean hasItems(ComboBox<?> comboBox,
return comboBox.getItems().size() == amount;
}

private static <T> boolean hasSelection(ComboBox<?> comboBox,
T selection) {
private static <T> boolean hasSelectedItem(ComboBox<?> comboBox,
T selection) {
return selection.equals(comboBox.getSelectionModel().getSelectedItem());
}

Expand All @@ -108,26 +105,33 @@ private static boolean containsItemsInOrder(ComboBox<?> comboBox,
int index = 0;

// find start of matching sub-sequence
while (!comboBox.getItems().get(index).equals(items[0]))
{
index++;
}

// make sure sub-sequence matches
for (Object item : items) {
if (!comboBox.getItems().get(index).equals(item)) {
while (!comboBox.getItems().get(index).equals(items[0])) {
if (items.length >= comboBox.getItems().size() - index) {
return false;
}
index++;
}
return true;

// make sure sub-sequence matches
return matchSubSequenceInOrder(comboBox, index, items);
}

private static boolean containsExactlyItemsInOrder(ComboBox<?> comboBox,
Object... items) {
int index = 0;
return matchSubSequenceInOrder(comboBox, 0, items);
}

/**
* If startIndex = 0, this method effectively matches the entire sequence.
*/
private static boolean matchSubSequenceInOrder(ComboBox<?> comboBox,
int startIndex,
Object... items)
{
int index = startIndex;
for (Object item : items) {
if (!comboBox.getItems().get(index).equals(item)) {
if (index >= comboBox.getItems().size() ||
!comboBox.getItems().get(index).equals(item)) {
return false;
}
index++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,13 @@ public void hasItems_fails() {

@Test
public void hasSelection() {
assertThat(comboBox, ComboBoxMatchers.hasSelection("alice"));
assertThat(comboBox, ComboBoxMatchers.hasSelectedItem("alice"));

clickOn(".combo-box-base");
type(KeyCode.DOWN);
type(KeyCode.ENTER);

press(KeyCode.DOWN);
release(KeyCode.DOWN);

press(KeyCode.ENTER);
release(KeyCode.ENTER);

assertThat(comboBox, ComboBoxMatchers.hasSelection("bob"));
assertThat(comboBox, ComboBoxMatchers.hasSelectedItem("bob"));
}

@Test
Expand All @@ -102,7 +98,7 @@ public void hasSelection_fails() {
exception.expect(AssertionError.class);
exception.expectMessage("Expected: ComboBox has selection bob\n");

assertThat(comboBox, ComboBoxMatchers.hasSelection("bob"));
assertThat(comboBox, ComboBoxMatchers.hasSelectedItem("bob"));
}

@Test
Expand Down

0 comments on commit 03efef8

Please sign in to comment.