-
Notifications
You must be signed in to change notification settings - Fork 1
/
PredicateTest.java
102 lines (84 loc) · 3.28 KB
/
PredicateTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package dev.boiarshinov.api.util.function;
import java.util.function.Predicate;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class PredicateTest {
@Test
public void testTest() {
final Predicate<Boolean> simplePredicate = b -> b;
Assert.assertTrue(simplePredicate.test(true));
Assert.assertFalse(simplePredicate.test(false));
}
@Test(dataProvider = "numberForAndProvider")
public void testAnd(Integer number, boolean expectedResult, boolean predicateShouldBeLazy) {
final StringBuilder lazyMarkChecker = new StringBuilder();
final Predicate<Integer> moreThanTen = num -> num > 10;
Predicate<Integer> isEven = num -> {
lazyMarkChecker.append("not lazy");
return num % 2 == 0;
};
final boolean actualResult = moreThanTen.and(isEven).test(number);
Assert.assertEquals(actualResult, expectedResult);
if (predicateShouldBeLazy) {
Assert.assertTrue(lazyMarkChecker.toString().isEmpty());
} else {
Assert.assertEquals(lazyMarkChecker.toString(), "not lazy");
}
}
@DataProvider
private Object[][] numberForAndProvider() {
return new Object[][]{
{12, true, false},
{6, false, true},
{11, false, false}
};
}
@Test(dataProvider = "numberForOrProvider")
public void testOr(Integer number, boolean expectedResult, boolean predicateShouldBeLazy) {
final StringBuilder lazyMarkChecker = new StringBuilder();
final Predicate<Integer> moreThanTen = num -> num > 10;
Predicate<Integer> isEven = num -> {
lazyMarkChecker.append("not lazy");
return num % 2 == 0;
};
final boolean actualResult = moreThanTen.or(isEven).test(number);
Assert.assertEquals(actualResult, expectedResult);
if (predicateShouldBeLazy) {
Assert.assertTrue(lazyMarkChecker.toString().isEmpty());
} else {
Assert.assertEquals(lazyMarkChecker.toString(), "not lazy");
}
}
@DataProvider
private Object[][] numberForOrProvider() {
return new Object[][]{
{8, true, false},
{11, true, true},
{9, false, false}
};
}
@Test
public void testOrAndAndFail() {
Predicate<Boolean> predicate = b -> b;
Predicate<Boolean> nullPredicate = null;
Assert.assertThrows(NullPointerException.class,
() -> predicate.or(nullPredicate).test(true));
Assert.assertThrows(NullPointerException.class,
() -> predicate.and(nullPredicate).test(false));
}
@Test
public void testNegate() {
final Predicate<Boolean> predicate = b -> b;
final Predicate<Boolean> negatePredicate = predicate.negate();
Assert.assertTrue(predicate.test(true));
Assert.assertFalse(negatePredicate.test(true));
}
@Test
public void testIsEqual() {
final Object customObject = new Object();
final Predicate<Object> equalsToCustomObject = Predicate.isEqual(customObject);
Assert.assertTrue(equalsToCustomObject.test(customObject));
Assert.assertFalse(equalsToCustomObject.test(new Object()));
}
}