Skip to content

Commit a119485

Browse files
committed
More API changes to function java extension - support for providing both Gen and Shrink as a pair.
1 parent 66e1314 commit a119485

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ Use the provided `CoregexArbirary` class to generate a string that would match t
3030
```java
3131
@RunWith(PropertyTestRunner.class)
3232
public class MyTest {
33+
private static final Pattern PATTERN = Pattern.compile("[a-zA-Z]{3}");
34+
3335
public Property myProperty() {
34-
return property(CoregexArbitrary.of("[a-zA-Z]{3}"), str -> prop(3 == str.length()));
36+
return property(CoregexArbitrary.gen(PATTERN), CoregexArbitrary.shrink(PATTERN), str -> prop(3 == str.length()));
3537
}
3638
}
3739
```

functionaljava-quickcheck/src/main/java/com/github/simy4/coregex/functionaljava/quickcheck/CoregexArbitrary.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.github.simy4.coregex.functionaljava.quickcheck;
1818

1919
import com.github.simy4.coregex.core.Coregex;
20+
import fj.P;
21+
import fj.P2;
2022
import fj.data.Option;
2123
import fj.data.Stream;
2224
import fj.test.Gen;
@@ -26,25 +28,21 @@
2628

2729
public final class CoregexArbitrary {
2830

29-
public static Gen<String> gen(String regex) {
30-
return gen(regex, 0);
31+
public static P2<Gen<String>, Shrink<String>> arbitrary(Pattern pattern) {
32+
return P.p(gen(pattern), shrink(pattern));
3133
}
3234

33-
public static Gen<String> gen(String regex, int flags) {
34-
Coregex coregex = Coregex.from(Pattern.compile(regex, flags));
35-
return Gen.gen(size -> rand -> coregex.generate(rand.choose(Long.MIN_VALUE, Long.MAX_VALUE)));
35+
public static Gen<String> gen(Pattern pattern) {
36+
Coregex coregex = Coregex.from(pattern);
37+
return Gen.gen(__ -> rand -> coregex.generate(rand.choose(Long.MIN_VALUE, Long.MAX_VALUE)));
3638
}
3739

38-
public static Shrink<String> shrink(String regex) {
39-
return shrink(regex, 0);
40-
}
41-
42-
public static Shrink<String> shrink(String regex, int flags) {
40+
public static Shrink<String> shrink(Pattern pattern) {
4341
Option<Stream<Coregex>> shrinks =
4442
Stream.sequenceOption(
4543
Stream.iterate(
4644
opt -> opt.bind(coregex -> fromOptional(coregex.shrink())),
47-
fromOptional(Coregex.from(Pattern.compile(regex, flags)).shrink()))
45+
fromOptional(Coregex.from(pattern).shrink()))
4846
.takeWhile(Option::isSome));
4947
return Shrink.shrink(
5048
str -> shrinks.orSome(Stream.nil()).map(coregex -> coregex.generate(str.length())));

functionaljava-quickcheck/src/test/java/com/github/simy4/coregex/functionaljava/quickcheck/CoregexArbitraryTest.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,46 @@
1717
package com.github.simy4.coregex.functionaljava.quickcheck;
1818

1919
import static fj.test.Property.prop;
20-
import static fj.test.Property.property;
2120

2221
import fj.Equal;
22+
import fj.F;
23+
import fj.Ord;
24+
import fj.P2;
2325
import fj.Try;
2426
import fj.data.List;
2527
import fj.test.Arbitrary;
28+
import fj.test.Gen;
2629
import fj.test.Property;
30+
import fj.test.Shrink;
2731
import fj.test.runner.PropertyTestRunner;
2832
import java.net.InetAddress;
2933
import java.time.format.DateTimeFormatter;
3034
import java.util.UUID;
35+
import java.util.regex.Pattern;
3136
import org.junit.runner.RunWith;
3237

3338
@RunWith(PropertyTestRunner.class)
3439
public class CoregexArbitraryTest {
3540
private static final Equal<String> stringEqual = Equal.stringEqual;
36-
private static final Equal<List<String>> listEqual = Equal.listEqual(stringEqual);
41+
private static final Ord<Integer> intOrd = Ord.intOrd;
42+
43+
private static <T> Property property(P2<Gen<T>, Shrink<T>> arbitrary, F<T, Property> property) {
44+
return Property.property(arbitrary._1(), arbitrary._2(), property);
45+
}
3746

3847
public Property shouldGenerateMatchingUUIDString() {
3948
return property(
40-
CoregexArbitrary.gen(
41-
"[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}"),
49+
CoregexArbitrary.arbitrary(
50+
Pattern.compile(
51+
"[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}")),
4252
uuid -> prop(stringEqual.eq(uuid, UUID.fromString(uuid).toString())));
4353
}
4454

4555
public Property shouldGenerateMatchingIPv4String() {
4656
return property(
47-
CoregexArbitrary.gen(
48-
"((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])"),
57+
CoregexArbitrary.arbitrary(
58+
Pattern.compile(
59+
"((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])")),
4960
ipv4 -> {
5061
String[] expected = ipv4.split("\\.");
5162
String[] actual =
@@ -67,19 +78,21 @@ public Property shouldGenerateMatchingIPv4String() {
6778
public Property shouldGenerateMatchingIsoDateString() {
6879
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
6980
return property(
70-
CoregexArbitrary.gen(
71-
"[12]\\d{3}-(?:0[1-9]|1[012])-(?:0[1-9]|1\\d|2[0-8])T(?:1\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d{2}[1-9])?Z"),
81+
CoregexArbitrary.arbitrary(
82+
Pattern.compile(
83+
"[12]\\d{3}-(?:0[1-9]|1[012])-(?:0[1-9]|1\\d|2[0-8])T(?:1\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d{2}[1-9])?Z")),
7284
iso8601Date ->
7385
prop(stringEqual.eq(iso8601Date, formatter.format(formatter.parse(iso8601Date)))));
7486
}
7587

7688
public Property shouldGenerateUniqueStrings() {
7789
return property(
78-
Arbitrary.arbList(CoregexArbitrary.gen("[a-zA-Z0-9]{32,}")),
90+
CoregexArbitrary.arbitrary(Pattern.compile("[a-zA-Z0-9]{32,}"))
91+
.split(Arbitrary::arbList, Shrink::shrinkList),
7992
strings ->
8093
strings.foldLeft(
8194
(acc, s) ->
82-
acc.and(prop(s.length() >= 32))
95+
acc.and(prop(intOrd.isLessThanOrEqualTo(32, s.length())))
8396
.and(prop(s.chars().allMatch(Character::isLetterOrDigit))),
8497
prop(true)));
8598
}

functionaljava-quickcheck/src/test/java/com/github/simy4/coregex/functionaljava/quickcheck/FunctionJavaQuickcheckTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import fj.test.Property;
1010
import fj.test.Rand;
1111
import fj.test.runner.PropertyTestRunner;
12-
import java.util.Random;
1312
import java.util.regex.Pattern;
1413
import org.junit.runner.RunWith;
1514

@@ -20,11 +19,10 @@ public Property shrinkingTest() {
2019
return property(
2120
arbitraryRegex(),
2221
Arbitrary.arbInteger,
23-
Arbitrary.arbLong.map(Random::new),
24-
(regex, size, random) -> {
25-
String sample =
26-
CoregexArbitrary.gen(regex.pattern(), regex.flags()).gen(size, Rand.standard);
27-
return CoregexArbitrary.shrink(regex.pattern(), regex.flags())
22+
Arbitrary.arbLong,
23+
(regex, size, seed) -> {
24+
String sample = CoregexArbitrary.gen(regex).gen(size, Rand.standard.reseed(seed));
25+
return CoregexArbitrary.shrink(regex)
2826
.shrink(sample)
2927
.map(shrink -> prop(regex.matcher(shrink).matches()))
3028
.foldLeft(Property::and, prop(true));

0 commit comments

Comments
 (0)