Skip to content

Commit faea5eb

Browse files
authored
Refactor Javaslang samples (eugenp#1469)
1 parent 60bbd6d commit faea5eb

2 files changed

Lines changed: 37 additions & 35 deletions

File tree

‎javaslang/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<plugin>
3939
<groupId>org.apache.maven.plugins</groupId>
4040
<artifactId>maven-compiler-plugin</artifactId>
41-
<version>3.3</version>
41+
<version>3.5.1</version>
4242
<configuration>
4343
<source>1.8</source>
4444
<target>1.8</target>
Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,71 @@
11
package com.baeldung.javaslang;
22

3-
43
import javaslang.CheckedFunction1;
54
import javaslang.collection.Stream;
65
import javaslang.test.Arbitrary;
76
import javaslang.test.CheckResult;
87
import javaslang.test.Property;
98
import org.junit.Test;
109

10+
import java.util.function.Predicate;
11+
12+
import static javaslang.API.*;
13+
1114
public class PropertyBasedTest {
1215

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;
1718

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($(), "")));
2725
}
2826

2927
@Test
3028
public void givenArbitrarySeq_whenCheckThatEverySecondElementIsEqualToString_thenTestPass() {
3129
//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);
3534

3635
//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");
4039

4140
//then
4241
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);
4746

4847
result.assertIsSatisfied();
4948
}
5049

5150
@Test
5251
public void givenArbitrarySeq_whenCheckThatEveryFifthElementIsEqualToString_thenTestPass() {
5352
//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);
5757

5858
//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");
6162

6263
//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();
6870
}
6971
}

0 commit comments

Comments
 (0)