Skip to content

Commit 409e428

Browse files
committed
add scala 2.12 support for zio-test and scalacheck.
1 parent 211aa24 commit 409e428

File tree

4 files changed

+94
-25
lines changed

4 files changed

+94
-25
lines changed

build.sbt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ inThisBuild(
2424
)
2525
)
2626

27+
lazy val scala212 = "2.12.20"
2728
lazy val scala213 = "2.13.16"
2829
lazy val scala3 = "3.3.6"
29-
lazy val supportedScalaVersions = List(scala213, scala3)
30+
lazy val supportedScalaVersions = List(scala212, scala213, scala3)
3031

3132
def javaLibSettings(release: Int) = Seq(
3233
crossPaths := false,
@@ -146,10 +147,16 @@ lazy val scalacheck = (project in file("scalacheck"))
146147
moduleName := "coregex-scalacheck",
147148
description := "ScalaCheck bindings for coregex library.",
148149
headerEndYear := Some(2025),
149-
libraryDependencies ++= Seq(
150-
"org.scalacheck" %% "scalacheck" % "1.18.1" % Provided
151-
),
150+
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.18.1" % Provided,
152151
crossScalaVersions := supportedScalaVersions,
152+
Compile / unmanagedSourceDirectories ++= (CrossVersion.partialVersion(scalaVersion.value) match {
153+
case Some((2, 13) | (3, _)) => Seq("scala-2.13+")
154+
case Some((2, 12)) => Seq.empty
155+
}).map(baseDirectory.value / "src" / "main" / _),
156+
Test / unmanagedSourceDirectories ++= (CrossVersion.partialVersion(scalaVersion.value) match {
157+
case Some((2, 13) | (3, _)) => Seq("scala-2.13+")
158+
case Some((2, 12)) => Seq("scala-2.12")
159+
}).map(baseDirectory.value / "src" / "test" / _),
153160
Test / tpolecatExcludeOptions += org.typelevel.scalacoptions.ScalacOptions.warnNonUnitStatement
154161
)
155162
.settings(jacocoSettings)

scalacheck/src/main/scala/com/github/simy4/coregex/scalacheck/CoregexInstances.scala renamed to scalacheck/src/main/scala-2.13+/com/github/simy4/coregex/scalacheck/CoregexInstances.scala

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.github.simy4.coregex.scalacheck
17+
package com.github.simy4.coregex
18+
package scalacheck
1819

19-
import com.github.simy4.coregex.core.Coregex
20+
import core.Coregex
2021
import org.scalacheck.{ Arbitrary, Gen, Shrink }
2122

2223
import java.util.regex.Pattern
2324

2425
trait CoregexInstances {
2526
import scala.jdk.CollectionConverters._
2627

27-
type Matching[A <: String, Regex <: String with Singleton] <: A
28+
type StringMatching[Regex <: String with Singleton] <: String
2829

29-
implicit def arbitraryInputStringMatchingRegexStringWithSingleton[
30-
A <: String,
31-
Regex <: String with Singleton
32-
](implicit regex: ValueOf[Regex]): Arbitrary[Matching[A, Regex]] =
33-
Arbitrary(CoregexGen.fromPattern(Pattern.compile(regex.value)).asInstanceOf[Gen[Matching[A, Regex]]])
30+
implicit def arbitraryStringMatchingRegexStringWithSingleton[
31+
Regex <: String with Singleton: ValueOf
32+
]: Arbitrary[StringMatching[Regex]] =
33+
Arbitrary(CoregexGen.fromPattern(Pattern.compile(valueOf[Regex])).asInstanceOf[Gen[StringMatching[Regex]]])
3434

35-
implicit def shrinkInputStringMatchingRegexStringWithSingleton[
36-
A <: String,
37-
Regex <: String with Singleton
38-
](implicit regex: ValueOf[Regex]): Shrink[Matching[A, Regex]] = {
39-
val shrinks = LazyList.from(Coregex.from(Pattern.compile(regex.value)).shrink().iterator().asScala)
35+
implicit def shrinkStringMatchingRegexStringWithSingleton[
36+
Regex <: String with Singleton: ValueOf
37+
]: Shrink[StringMatching[Regex]] = {
38+
val shrinks = Coregex.from(Pattern.compile(valueOf[Regex])).shrink().iterator().asScala.to(LazyList)
4039
Shrink.withLazyList { larger =>
4140
shrinks
42-
.map(coregex => coregex.generate(larger.length.toLong).asInstanceOf[Matching[A, Regex]])
41+
.map(coregex => coregex.generate(larger.length.toLong).asInstanceOf[StringMatching[Regex]])
4342
.filter(string => string.length < larger.length)
4443
}
4544
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.scalacheck
18+
19+
import org.scalacheck.{ Gen, Properties }
20+
import org.scalacheck.Prop._
21+
22+
import java.net.InetAddress
23+
import java.time.format.DateTimeFormatter
24+
import java.util.UUID
25+
import java.util.regex.Pattern
26+
27+
object CoregexSpecification extends Properties("Coregex") {
28+
property("should generate matching UUID string") = forAll(
29+
CoregexGen.fromPattern(Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}"))
30+
) { uuid =>
31+
uuid =? UUID.fromString(uuid).toString
32+
}
33+
34+
property("should generate matching IPv4 string") = forAll(
35+
CoregexGen.fromPattern(Pattern.compile("((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])"))
36+
) { ipv4 =>
37+
all(
38+
ipv4
39+
.split('.')
40+
.zip(InetAddress.getByName(ipv4).getHostAddress.split('.'))
41+
.map { case (expected, actual) => expected.toInt =? actual.toInt }
42+
.toIndexedSeq: _*
43+
)
44+
}
45+
46+
property("should generate matching ISO-8601 date string") = forAll(
47+
CoregexGen.fromPattern(
48+
Pattern.compile(
49+
"[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"
50+
)
51+
)
52+
) { iso8601Date =>
53+
val formatter = DateTimeFormatter.ISO_INSTANT
54+
(iso8601Date: String) =? formatter.format(formatter.parse(iso8601Date))
55+
}
56+
57+
property("should generate unique strings") =
58+
forAll(Gen.listOf(CoregexGen.fromPattern(Pattern.compile("[a-zA-Z0-9]{32,}")))) { strings =>
59+
strings.forall { s =>
60+
s.length >= 32 && s.forall(_.isLetterOrDigit)
61+
} && (strings.size =? strings.toSet.size)
62+
}
63+
}

scalacheck/src/test/scala/com/github/simy4/coregex/scalacheck/CoregexSpecification.scala renamed to scalacheck/src/test/scala-2.13+/com/github/simy4/coregex/scalacheck/CoregexSpecification.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ import java.util.UUID
2525

2626
object CoregexSpecification extends Properties("Coregex") with CoregexInstances {
2727
property("should generate matching UUID string") = forAll {
28-
(uuid: String Matching "[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}") =>
29-
uuid.toString =? UUID.fromString(uuid).toString
28+
(uuid: StringMatching["[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}"]) =>
29+
(uuid: String) =? UUID.fromString(uuid).toString
3030
}
3131

3232
property("should generate matching IPv4 string") = forAll {
33-
(ipv4: String Matching "((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])") =>
33+
(ipv4: StringMatching["((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])"]) =>
3434
all(
3535
ipv4
3636
.split('.')
@@ -41,13 +41,13 @@ object CoregexSpecification extends Properties("Coregex") with CoregexInstances
4141
}
4242

4343
property("should generate matching ISO-8601 date string") = forAll {
44-
(iso8601Date: String Matching
45-
"[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") =>
44+
(iso8601Date: StringMatching[
45+
"[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"]) =>
4646
val formatter = DateTimeFormatter.ISO_INSTANT
47-
iso8601Date.toString =? formatter.format(formatter.parse(iso8601Date))
47+
(iso8601Date: String) =? formatter.format(formatter.parse(iso8601Date))
4848
}
4949

50-
property("should generate unique strings") = forAll { (strings: List[String Matching "[a-zA-Z0-9]{32,}"]) =>
50+
property("should generate unique strings") = forAll { (strings: List[StringMatching["[a-zA-Z0-9]{32,}"]]) =>
5151
strings.forall { s =>
5252
s.length >= 32 && s.forall(_.isLetterOrDigit)
5353
} && (strings.size =? strings.toSet.size)

0 commit comments

Comments
 (0)