Refactor Javaslang samples (#1469)

This commit is contained in:
Grzegorz Piwowarek 2017-03-21 16:49:29 +01:00 committed by GitHub
parent 60bbd6de56
commit faea5eb510
2 changed files with 37 additions and 35 deletions

View File

@ -38,7 +38,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version> <version>3.5.1</version>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>

View File

@ -1,6 +1,5 @@
package com.baeldung.javaslang; package com.baeldung.javaslang;
import javaslang.CheckedFunction1; import javaslang.CheckedFunction1;
import javaslang.collection.Stream; import javaslang.collection.Stream;
import javaslang.test.Arbitrary; import javaslang.test.Arbitrary;
@ -8,42 +7,42 @@ import javaslang.test.CheckResult;
import javaslang.test.Property; import javaslang.test.Property;
import org.junit.Test; import org.junit.Test;
import java.util.function.Predicate;
import static javaslang.API.*;
public class PropertyBasedTest { public class PropertyBasedTest {
public Stream<String> stringsSupplier() { private static Predicate<Integer> divisibleByTwo = i -> i % 2 == 0;
return Stream.from(0).map(i -> { private static Predicate<Integer> divisibleByFive = i -> i % 5 == 0;
boolean divByTwo = i % 2 == 0;
boolean divByFive = i % 5 == 0;
if(divByFive && divByTwo){ private Stream<String> stringsSupplier() {
return "DividedByTwoAndFiveWithoutRemainder"; return Stream.from(0).map(i -> Match(i).of(
}else if(divByFive){ Case($(divisibleByFive.and(divisibleByTwo)), "DividedByTwoAndFiveWithoutRemainder"),
return "DividedByFiveWithoutRemainder"; Case($(divisibleByFive), "DividedByFiveWithoutRemainder"),
}else if(divByTwo){ Case($(divisibleByTwo), "DividedByTwoWithoutRemainder"),
return "DividedByTwoWithoutRemainder"; Case($(), "")));
}
return "";
});
} }
@Test @Test
public void givenArbitrarySeq_whenCheckThatEverySecondElementIsEqualToString_thenTestPass() { public void givenArbitrarySeq_whenCheckThatEverySecondElementIsEqualToString_thenTestPass() {
//given //given
Arbitrary<Integer> multiplesOf2 = Arbitrary.integer() Arbitrary<Integer> multiplesOf2 = Arbitrary
.filter(i -> i > 0) .integer()
.filter(i -> i % 2 == 0 && i % 5 != 0); .filter(i -> i > 0)
.filter(i -> i % 2 == 0 && i % 5 != 0);
//when //when
CheckedFunction1<Integer, Boolean> mustEquals = CheckedFunction1<Integer, Boolean> mustEquals = i -> stringsSupplier()
i -> stringsSupplier().get(i).equals("DividedByTwoWithoutRemainder"); .get(i)
.equals("DividedByTwoWithoutRemainder");
//then //then
CheckResult result = Property CheckResult result = Property
.def("Every second element must equal to DividedByTwoWithoutRemainder") .def("Every second element must equal to DividedByTwoWithoutRemainder")
.forAll(multiplesOf2) .forAll(multiplesOf2)
.suchThat(mustEquals) .suchThat(mustEquals)
.check(10_000, 100); .check(10_000, 100);
result.assertIsSatisfied(); result.assertIsSatisfied();
} }
@ -51,19 +50,22 @@ public class PropertyBasedTest {
@Test @Test
public void givenArbitrarySeq_whenCheckThatEveryFifthElementIsEqualToString_thenTestPass() { public void givenArbitrarySeq_whenCheckThatEveryFifthElementIsEqualToString_thenTestPass() {
//given //given
Arbitrary<Integer> multiplesOf5 = Arbitrary.integer() Arbitrary<Integer> multiplesOf5 = Arbitrary
.filter(i -> i > 0) .integer()
.filter(i -> i % 5 == 0 && i % 2 == 0); .filter(i -> i > 0)
.filter(i -> i % 5 == 0 && i % 2 == 0);
//when //when
CheckedFunction1<Integer, Boolean> mustEquals = i -> CheckedFunction1<Integer, Boolean> mustEquals = i -> stringsSupplier()
stringsSupplier().get(i).endsWith("DividedByTwoAndFiveWithoutRemainder"); .get(i)
.endsWith("DividedByTwoAndFiveWithoutRemainder");
//then //then
Property.def("Every fifth element must equal to DividedByTwoAndFiveWithoutRemainder") Property
.forAll(multiplesOf5) .def("Every fifth element must equal to DividedByTwoAndFiveWithoutRemainder")
.suchThat(mustEquals) .forAll(multiplesOf5)
.check(10_000, 1_000) .suchThat(mustEquals)
.assertIsSatisfied(); .check(10_000, 1_000)
.assertIsSatisfied();
} }
} }