|
1 | 1 | package com.baeldung.javaslang; |
2 | 2 |
|
3 | | - |
4 | 3 | import javaslang.CheckedFunction1; |
5 | 4 | import javaslang.collection.Stream; |
6 | 5 | import javaslang.test.Arbitrary; |
7 | 6 | import javaslang.test.CheckResult; |
8 | 7 | import javaslang.test.Property; |
9 | 8 | import org.junit.Test; |
10 | 9 |
|
| 10 | +import java.util.function.Predicate; |
| 11 | + |
| 12 | +import static javaslang.API.*; |
| 13 | + |
11 | 14 | public class PropertyBasedTest { |
12 | 15 |
|
13 | | - public Stream<String> stringsSupplier() { |
14 | | - return Stream.from(0).map(i -> { |
15 | | - boolean divByTwo = i % 2 == 0; |
16 | | - boolean divByFive = i % 5 == 0; |
| 16 | + private static Predicate<Integer> divisibleByTwo = i -> i % 2 == 0; |
| 17 | + private static Predicate<Integer> divisibleByFive = i -> i % 5 == 0; |
17 | 18 |
|
18 | | - if(divByFive && divByTwo){ |
19 | | - return "DividedByTwoAndFiveWithoutRemainder"; |
20 | | - }else if(divByFive){ |
21 | | - return "DividedByFiveWithoutRemainder"; |
22 | | - }else if(divByTwo){ |
23 | | - return "DividedByTwoWithoutRemainder"; |
24 | | - } |
25 | | - return ""; |
26 | | - }); |
| 19 | + private Stream<String> stringsSupplier() { |
| 20 | + return Stream.from(0).map(i -> Match(i).of( |
| 21 | + Case($(divisibleByFive.and(divisibleByTwo)), "DividedByTwoAndFiveWithoutRemainder"), |
| 22 | + Case($(divisibleByFive), "DividedByFiveWithoutRemainder"), |
| 23 | + Case($(divisibleByTwo), "DividedByTwoWithoutRemainder"), |
| 24 | + Case($(), ""))); |
27 | 25 | } |
28 | 26 |
|
29 | 27 | @Test |
30 | 28 | public void givenArbitrarySeq_whenCheckThatEverySecondElementIsEqualToString_thenTestPass() { |
31 | 29 | //given |
32 | | - Arbitrary<Integer> multiplesOf2 = Arbitrary.integer() |
33 | | - .filter(i -> i > 0) |
34 | | - .filter(i -> i % 2 == 0 && i % 5 != 0); |
| 30 | + Arbitrary<Integer> multiplesOf2 = Arbitrary |
| 31 | + .integer() |
| 32 | + .filter(i -> i > 0) |
| 33 | + .filter(i -> i % 2 == 0 && i % 5 != 0); |
35 | 34 |
|
36 | 35 | //when |
37 | | - CheckedFunction1<Integer, Boolean> mustEquals = |
38 | | - i -> stringsSupplier().get(i).equals("DividedByTwoWithoutRemainder"); |
39 | | - |
| 36 | + CheckedFunction1<Integer, Boolean> mustEquals = i -> stringsSupplier() |
| 37 | + .get(i) |
| 38 | + .equals("DividedByTwoWithoutRemainder"); |
40 | 39 |
|
41 | 40 | //then |
42 | 41 | CheckResult result = Property |
43 | | - .def("Every second element must equal to DividedByTwoWithoutRemainder") |
44 | | - .forAll(multiplesOf2) |
45 | | - .suchThat(mustEquals) |
46 | | - .check(10_000, 100); |
| 42 | + .def("Every second element must equal to DividedByTwoWithoutRemainder") |
| 43 | + .forAll(multiplesOf2) |
| 44 | + .suchThat(mustEquals) |
| 45 | + .check(10_000, 100); |
47 | 46 |
|
48 | 47 | result.assertIsSatisfied(); |
49 | 48 | } |
50 | 49 |
|
51 | 50 | @Test |
52 | 51 | public void givenArbitrarySeq_whenCheckThatEveryFifthElementIsEqualToString_thenTestPass() { |
53 | 52 | //given |
54 | | - Arbitrary<Integer> multiplesOf5 = Arbitrary.integer() |
55 | | - .filter(i -> i > 0) |
56 | | - .filter(i -> i % 5 == 0 && i % 2 == 0); |
| 53 | + Arbitrary<Integer> multiplesOf5 = Arbitrary |
| 54 | + .integer() |
| 55 | + .filter(i -> i > 0) |
| 56 | + .filter(i -> i % 5 == 0 && i % 2 == 0); |
57 | 57 |
|
58 | 58 | //when |
59 | | - CheckedFunction1<Integer, Boolean> mustEquals = i -> |
60 | | - stringsSupplier().get(i).endsWith("DividedByTwoAndFiveWithoutRemainder"); |
| 59 | + CheckedFunction1<Integer, Boolean> mustEquals = i -> stringsSupplier() |
| 60 | + .get(i) |
| 61 | + .endsWith("DividedByTwoAndFiveWithoutRemainder"); |
61 | 62 |
|
62 | 63 | //then |
63 | | - Property.def("Every fifth element must equal to DividedByTwoAndFiveWithoutRemainder") |
64 | | - .forAll(multiplesOf5) |
65 | | - .suchThat(mustEquals) |
66 | | - .check(10_000, 1_000) |
67 | | - .assertIsSatisfied(); |
| 64 | + Property |
| 65 | + .def("Every fifth element must equal to DividedByTwoAndFiveWithoutRemainder") |
| 66 | + .forAll(multiplesOf5) |
| 67 | + .suchThat(mustEquals) |
| 68 | + .check(10_000, 1_000) |
| 69 | + .assertIsSatisfied(); |
68 | 70 | } |
69 | 71 | } |
0 commit comments