8000 #97 Null predicate function should return empty matched set · seleniumQuery/seleniumQuery@1bb6ab1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bb6ab1

Browse files
committed
#97 Null predicate function should return empty matched set
1 parent 1e98296 commit 1bb6ab1

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/main/java/io/github/seleniumquery/functions/jquery/traversing/filtering/FilterFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626

2727
import static io.github.seleniumquery.InternalSeleniumQueryObjectFactory.instance;
28+
import static java.util.Collections.emptyList;
2829

2930
public class FilterFunction {
3031

@@ -39,7 +40,7 @@ public SeleniumQueryObject filter(SeleniumQueryObject seleniumQueryObject, Predi
3940

4041
private List<WebElement> filterWebElements(List<WebElement> unfiltered, Predicate<WebElement> filterFunction) {
4142
if (filterFunction == null) {
42-
return unfiltered;
43+
return emptyList();
4344
}
4445
Iterable<WebElement> filter = Iterables.filter(unfiltered, filterFunction);
4546
return Lists.newArrayList(filter);

src/test/java/io/github/seleniumquery/functions/jquery/traversing/filtering/FilterFunctionTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818

1919
import com.google.common.base.Predicate;
2020
import io.github.seleniumquery.SeleniumQueryObject;
21+
import org.hamcrest.collection.IsEmptyCollection;
2122
import org.junit.Test;
2223
import org.openqa.selenium.WebElement;
2324

2425
import static org.hamcrest.CoreMatchers.is;
26+
import static org.hamcrest.collection.IsEmptyCollection.empty;
2527
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
2628
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
2729
import static org.junit.Assert.assertThat;
2830
import static testinfrastructure.testdouble.Dummies.createDummyWebElement;
2931
import static testinfrastructure.testdouble.SeleniumQueryObjectMother.createStubSeleniumQueryObject;
32+
import static testinfrastructure.testdouble.SeleniumQueryObjectMother.createStubSeleniumQueryObjectWithAtLeastOneElement;
3033
import static testinfrastructure.testdouble.SeleniumQueryObjectMother.createStubSeleniumQueryObjectWithElements;
3134
import static testinfrastructure.testdouble.Stubs.createStubWebElementWithTag;
3235

@@ -37,15 +40,13 @@ public class FilterFunctionTest {
3740
FilterFunction filterFunction = new FilterFunction();
3841

3942
@Test
40-
public void null_predicate__should_return_same_elements() {
43+
public void null_predicate__should_return_EMPTY_elements() {
4144
// given
42-
WebElement dummyWebElement = createDummyWebElement();
43-
WebElement dummyWebElement2 = createDummyWebElement();
44-
SeleniumQueryObject targetSQO = createStubSeleniumQueryObjectWithElements(dummyWebElement, dummyWebElement2);
45+
SeleniumQueryObject targetSQO = createStubSeleniumQueryObjectWithAtLeastOneElement();
4546
// when
4647
SeleniumQueryObject resultSQO = filterFunction.filter(targetSQO, NULL_PREDICATE);
4748
// then
48-
assertThat(resultSQO.get(), containsInAnyOrder(dummyWebElement, dummyWebElement2));
49+
assertThat(resultSQO.get(), empty());
4950
}
5051

5152
@Test
@@ -98,8 +99,4 @@ public boolean apply(WebElement webElement) {
9899
assertThat(resultSQO.get(), contains(spanOne, spanTwo));
99100
}
100101

101-
// TODO filter(this, null) --> should return empty ==> CHANGE THIS FOR THE PREDICATE VERSION ALSO, it is returning the original matched set
102-
// TODO filter(this, "") --> should return empty
103-
// TODO filter(this, "selector") --> should keep everyone that matches the isFunction("selector")
104-
105102
}

src/test/java/testinfrastructure/testdouble/SeleniumQueryObjectMother.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public static SeleniumQueryObject createStubSeleniumQueryObjectWithElements(WebE
4242
return createStubSeleniumQueryObject(new SeleniumQueryFunctions(), asList(elements));
4343
}
4444

45+
public static SeleniumQueryObject createStubSeleniumQueryObjectWithAtLeastOneElement() {
46+
return createStubSeleniumQueryObjectWithElements(createDummyWebElement(), createDummyWebElement());
47+
}
48+
4549
public static SeleniumQueryObject createStubSeleniumQueryObject() {
4650
return createStubSeleniumQueryObjectWithElements();
4751
}

0 commit comments

Comments
 (0)
0