Skip to content

Commit e35d757

Browse files
committed
jetCheck support.
1 parent 3c38781 commit e35d757

File tree

4 files changed

+183
-34
lines changed

4 files changed

+183
-34
lines changed

README.md

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ A handy utility for generating strings that match given regular expression crite
1111
# Supported generators
1212

1313
- [functionaljava-quickcheck](https://github.com/functionaljava/functionaljava)
14+
- [hedgehog](https://hedgehogqa.github.io/scala-hedgehog/)
15+
- [jetCheck](https://hedgehogqa.github.io/scala-hedgehog/)
1416
- [Jqwik](https://jqwik.net/)
1517
- [JUnit Quickcheck](https://pholser.github.io/junit-quickcheck)
1618
- [Kotest](https://kotest.io/)
1719
- [scalacheck](https://scalacheck.org/)
1820
- [vavr-test](https://github.com/vavr-io/vavr-test)
21+
- [zio-test](https://zio.dev/reference/test/)
1922

2023
# Usage
2124

@@ -59,6 +62,40 @@ public class MyTest {
5962
}
6063
```
6164

65+
## hedgehog-scala
66+
Include the following dependency into your project:
67+
68+
```scala
69+
libraryDependencies ++= Seq("com.github.simy4.coregex" %% "coregex-hedgehog" % Test)
70+
```
71+
72+
Use the provided `CoregexGen` class to generate a string that would match the regular expression predicate:
73+
74+
```scala
75+
object MySpec extends Properties {
76+
def tests: List[Test] = List(
77+
property("my property", myProperty),
78+
)
79+
80+
def myProperty: Property = for {
81+
str <- CoregexGen.fromRegex("[a-zA-Z]{3}".r).forAll
82+
} yield Result.assert(str.length ==== 3)
83+
}
84+
```
85+
86+
## jetCheck
87+
Include the following dependency into your project:
88+
89+
```groovy
90+
testImplementation "com.github.simy4.coregex:coregex-jetCheck"
91+
```
92+
93+
Use the provided `CoregexGenerator` class to generate a string that would match the regular expression predicate:
94+
95+
```java
96+
PropertyChecker.forAll(CoregexGenerator.of("[a-zA-Z]{3}"), str -> 3 == str.length());
97+
```
98+
6299
## Jqwik
63100
Include the following dependency into your project:
64101

@@ -96,27 +133,6 @@ public class MyTest {
96133
}
97134
```
98135

99-
## hedgehog-scala
100-
Include the following dependency into your project:
101-
102-
```scala
103-
libraryDependencies ++= Seq("com.github.simy4.coregex" %% "coregex-hedgehog" % Test)
104-
```
105-
106-
Use the provided `CoregexGen` class to generate a string that would match the regular expression predicate:
107-
108-
```scala
109-
object MySpec extends Properties {
110-
def tests: List[Test] = List(
111-
property("my property", myProperty),
112-
)
113-
114-
def myProperty: Property = for {
115-
uuid <- CoregexGen.fromRegex("[a-zA-Z]{3}".r).forAll
116-
} yield Result.assert(str.length ==== 3)
117-
}
118-
```
119-
120136
## Kotest
121137
Include the following dependency into your project:
122138

@@ -165,22 +181,17 @@ testImplementation "com.github.simy4.coregex:coregex-vavr-test"
165181
Use the provided `CoregexArbirary` class to generate a string that would match the regular expression predicate:
166182

167183
```java
168-
class MyTest {
169-
@Test
170-
void myProperty() {
171-
Property.def("my property")
172-
.forAll(CoregexArbitrary.of("[a-zA-Z]{3}"))
173-
.suchThat(str -> 3 == str.length())
174-
.check();
175-
}
176-
}
184+
Property.def("my property")
185+
.forAll(CoregexArbitrary.of("[a-zA-Z]{3}"))
186+
.suchThat(str -> 3 == str.length())
187+
.check();
177188
```
178189

179190
## ZIO test
180191
Include the following dependency into your project:
181192

182-
```groovy
183-
testImplementation "com.github.simy4.coregex:coregex-zio-test"
193+
```scala
194+
libraryDependencies ++= Seq("com.github.simy4.coregex" %% "coregex-zio-test" % Test)
184195
```
185196

186197
Use the provided `CoregexGen` class to generate a string that would match the regular expression predicate:

build.sbt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ inThisBuild(
2525
)
2626

2727
lazy val scala212 = "2.12.20"
28-
lazy val scala213 = "2.13.17"
28+
lazy val scala213 = "2.13.18"
2929
lazy val scala3 = "3.3.7"
3030
lazy val supportedScalaVersions = List(scala212, scala213, scala3)
3131

@@ -56,7 +56,18 @@ lazy val root = (project in file("."))
5656
name := "coregex-parent",
5757
publish / skip := true
5858
)
59-
.aggregate(core, functionaljavaQuickcheck, jqwik, junitQuickcheck, kotest, scalacheck, vavrTest, zioTest)
59+
.aggregate(
60+
core,
61+
functionaljavaQuickcheck,
62+
hedgehog,
63+
jetCheck,
64+
jqwik,
65+
junitQuickcheck,
66+
kotest,
67+
scalacheck,
68+
vavrTest,
69+
zioTest
70+
)
6071

6172
lazy val core = (project in file("core"))
6273
.settings(
@@ -104,6 +115,22 @@ lazy val hedgehog = (project in file("hedgehog"))
104115
.settings(jacocoSettings)
105116
.dependsOn(core)
106117

118+
lazy val jetCheck = (project in file("jetCheck"))
119+
.settings(
120+
name := "jetCheck",
121+
moduleName := "coregex-jetCheck",
122+
description := "jetCheck bindings for coregex library.",
123+
headerEndYear := Some(2025),
124+
libraryDependencies ++= Seq(
125+
"org.jetbrains" % "jetCheck" % "0.2.3" % Provided,
126+
"com.github.sbt.junit" % "jupiter-interface" % JupiterKeys.jupiterVersion.value % Test
127+
),
128+
testOptions += Tests.Argument(jupiterTestFramework, "-q", "-v")
129+
)
130+
.settings(javaLibSettings(8))
131+
.settings(jacocoSettings)
132+
.dependsOn(core)
133+
107134
lazy val jqwik = (project in file("jqwik"))
108135
.settings(
109136
name := "jqwik",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2021-2025 Alex Simkin
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 com.github.simy4.coregex.jetCheck;
18+
19+
import com.github.simy4.coregex.core.Coregex;
20+
import java.util.regex.Pattern;
21+
import org.jetbrains.jetCheck.Generator;
22+
23+
public class CoregexGenerator {
24+
public static Generator<String> of(String regex) {
25+
return of(Pattern.compile(regex));
26+
}
27+
28+
public static Generator<String> of(String regex, int flags) {
29+
return of(Pattern.compile(regex, flags));
30+
}
31+
32+
public static Generator<String> of(Pattern pattern) {
33+
Coregex coregex = Coregex.from(pattern);
34+
return Generator.from(
35+
generationEnvironment ->
36+
coregex.generate(generationEnvironment.generate(Generator.integers())));
37+
}
38+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2021-2025 Alex Simkin
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 com.github.simy4.coregex.jetCheck;
18+
19+
import java.io.UncheckedIOException;
20+
import java.net.InetAddress;
21+
import java.net.UnknownHostException;
22+
import java.time.format.DateTimeFormatter;
23+
import java.util.UUID;
24+
import java.util.stream.IntStream;
25+
import org.jetbrains.jetCheck.Generator;
26+
import org.jetbrains.jetCheck.PropertyChecker;
27+
import org.junit.jupiter.api.Test;
28+
29+
class CoregexGeneratorTest {
30+
@Test
31+
void shouldGenerateMatchingUUIDString() {
32+
PropertyChecker.forAll(
33+
CoregexGenerator.of(
34+
"[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}"),
35+
uuid -> uuid.equals(UUID.fromString(uuid).toString()));
36+
}
37+
38+
@Test
39+
void shouldGenerateMatchingIPv4String() {
40+
PropertyChecker.forAll(
41+
CoregexGenerator.of(
42+
"((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])"),
43+
ipv4 -> {
44+
try {
45+
String[] expected = ipv4.split("\\.");
46+
String[] actual = InetAddress.getByName(ipv4).getHostAddress().split("\\.");
47+
return expected.length == actual.length
48+
&& IntStream.range(0, expected.length)
49+
.allMatch(i -> Integer.parseInt(expected[i]) == Integer.parseInt(actual[i]));
50+
} catch (UnknownHostException ex) {
51+
throw new UncheckedIOException(ex);
52+
}
53+
});
54+
}
55+
56+
@Test
57+
void shouldGenerateMatchingIsoDateString() {
58+
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
59+
PropertyChecker.forAll(
60+
CoregexGenerator.of(
61+
"[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"),
62+
iso8601Date -> iso8601Date.equals(formatter.format(formatter.parse(iso8601Date))));
63+
}
64+
65+
@Test
66+
void shouldGenerateUniqueStrings() {
67+
PropertyChecker.forAll(
68+
Generator.listsOf(CoregexGenerator.of("[a-zA-Z0-9]{32,}")),
69+
strings ->
70+
strings.stream()
71+
.allMatch(s -> s.length() >= 32 && s.chars().allMatch(Character::isLetterOrDigit)));
72+
}
73+
}

0 commit comments

Comments
 (0)