Skip to content
This repository was archived by the owner on Nov 23, 2023. It is now read-only.

Commit 5c48f39

Browse files
committed
Refactor PredicateXTest
Fixes #69
1 parent 30e296f commit 5c48f39

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed

src/test/java/io/github/alexengrig/lambdax/example/Box.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717
package io.github.alexengrig.lambdax.example;
1818

1919
public class Box {
20-
private Item item;
20+
private Pack pack;
2121

22-
public Box() {
22+
public Box(Pack pack) {
23+
this.pack = pack;
2324
}
2425

25-
public Box(Item item) {
26-
this.item = item;
27-
}
28-
29-
public Item getItem() {
30-
return item;
26+
public Pack getPack() {
27+
return pack;
3128
}
3229
}

src/test/java/io/github/alexengrig/lambdax/example/Holder.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@
1616

1717
package io.github.alexengrig.lambdax.example;
1818

19-
final class Holder<T> {
19+
public final class Holder<T> {
2020
private T t;
2121

22-
Holder(T t) {
22+
public Holder(T t) {
2323
this.t = t;
2424
}
2525

26-
T get() {
26+
public T get() {
2727
return t;
2828
}
29-
30-
void set(T t) {
31-
this.t = t;
32-
}
3329
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2019 LambdaX contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.alexengrig.lambdax.example;
18+
19+
public class Pack {
20+
private Item item;
21+
22+
public Pack(Item item) {
23+
this.item = item;
24+
}
25+
26+
public Item getItem() {
27+
return item;
28+
}
29+
}

src/test/java/io/github/alexengrig/lambdax/function/PredicateXTest.java

+34-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package io.github.alexengrig.lambdax.function;
1818

1919
import io.github.alexengrig.lambdax.example.Box;
20+
import io.github.alexengrig.lambdax.example.Holder;
2021
import io.github.alexengrig.lambdax.example.Item;
22+
import io.github.alexengrig.lambdax.example.Pack;
2123
import org.junit.Test;
2224

2325
import java.util.function.Predicate;
@@ -29,48 +31,66 @@ public class PredicateXTest {
2931
@Test
3032
public void checkChecker() {
3133
String value = "Coca-Cola";
32-
Box box = new Box(new Item(value));
33-
Predicate<Box> isEmptyItemName = PredicateX.of(Box::getItem).map(Item::getName).check(String::isEmpty);
34-
assertFalse(isEmptyItemName.test(box));
34+
Holder<Box> boxHolder = new Holder<>(new Box(new Pack(new Item(value))));
35+
Predicate<Holder<Box>> isEmptyItemName = PredicateX.of(Holder<Box>::get)
36+
.map(Box::getPack)
37+
.map(Pack::getItem)
38+
.map(Item::getName)
39+
.check(String::isEmpty);
40+
assertFalse(isEmptyItemName.test(boxHolder));
3541
}
3642

3743
@Test
3844
public void checkEqual() {
3945
String value = "Coca-Cola";
40-
Box box = new Box(new Item(value));
41-
Predicate<Box> equalsCocaCola = PredicateX.of(Box::getItem).map(Item::getName).equal(value);
42-
assertTrue(equalsCocaCola.test(box));
46+
Pack pack = new Pack(new Item(value));
47+
Predicate<Pack> equalsCocaCola = PredicateX.of(Pack::getItem)
48+
.map(Item::getName)
49+
.equal(value);
50+
assertTrue(equalsCocaCola.test(pack));
4351
}
4452

4553
@Test
4654
public void checkLess() {
4755
String value = "Coca-Cola";
48-
Box box = new Box(new Item(value));
49-
Predicate<Box> lessPepsi = PredicateX.of(Box::getItem).map(Item::getName).less("Pepsi");
56+
Box box = new Box(new Pack(new Item(value)));
57+
Predicate<Box> lessPepsi = PredicateX.of(Box::getPack)
58+
.map(Pack::getItem)
59+
.map(Item::getName)
60+
.less("Pepsi");
5061
assertTrue(lessPepsi.test(box));
5162
}
5263

5364
@Test
5465
public void checkGreater() {
5566
String value = "Schweppes";
56-
Box box = new Box(new Item(value));
57-
Predicate<Box> greaterCocaCola = PredicateX.of(Box::getItem).map(Item::getName).greater("Dr Pepper");
67+
Box box = new Box(new Pack(new Item(value)));
68+
Predicate<Box> greaterCocaCola = PredicateX.of(Box::getPack)
69+
.map(Pack::getItem)
70+
.map(Item::getName)
71+
.greater("Dr Pepper");
5872
assertTrue(greaterCocaCola.test(box));
5973
}
6074

6175
@Test
6276
public void checkLessOrEqual() {
6377
String value = "Fanta";
64-
Box box = new Box(new Item(value));
65-
Predicate<Box> lessPepsi = PredicateX.of(Box::getItem).map(Item::getName).lessOrEqual("Mirinda");
78+
Box box = new Box(new Pack(new Item(value)));
79+
Predicate<Box> lessPepsi = PredicateX.of(Box::getPack)
80+
.map(Pack::getItem)
81+
.map(Item::getName)
82+
.lessOrEqual("Mirinda");
6683
assertTrue(lessPepsi.test(box));
6784
}
6885

6986
@Test
7087
public void checkGreaterOrEqual() {
7188
String value = "Sprite";
72-
Box box = new Box(new Item(value));
73-
Predicate<Box> greaterCocaCola = PredicateX.of(Box::getItem).map(Item::getName).greaterOrEqual("7 Up");
89+
Box box = new Box(new Pack(new Item(value)));
90+
Predicate<Box> greaterCocaCola = PredicateX.of(Box::getPack)
91+
.map(Pack::getItem)
92+
.map(Item::getName)
93+
.greaterOrEqual("7 Up");
7494
assertTrue(greaterCocaCola.test(box));
7595
}
7696
}

0 commit comments

Comments
 (0)