-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* split implementations to scala 2 and 3 * Add Scala 3 version of kebs-scalacheck * extend missing ScalacheckInstances enumeratum trait * remove jdk8 build --------- Co-authored-by: Paweł Kiersznowski <pkiersznowski@iteratorshq.com>
- Loading branch information
Showing
14 changed files
with
190 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
scalacheck/src/main/scala-2/pl/iterators/kebs/scalacheck/CommonArbitrarySupport.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package pl.iterators.kebs.scalacheck | ||
|
||
import enumeratum.ScalacheckInstances | ||
import org.scalacheck.{Arbitrary, Gen, ScalacheckShapeless} | ||
import pl.iterators.kebs.macros.CaseClass1Rep | ||
|
||
import java.net.{URI, URL} | ||
import java.time.temporal.ChronoUnit | ||
import java.time._ | ||
import java.util.concurrent.TimeUnit | ||
import scala.reflect.ClassTag | ||
import scala.util.Random | ||
|
||
trait CommonArbitrarySupport extends ScalacheckShapeless with ScalacheckInstances { | ||
implicit def caseClass1RepArbitraryPredef[T, A]( | ||
implicit rep: CaseClass1Rep[T, A], | ||
arbitrary: Arbitrary[A] | ||
): Arbitrary[T] = | ||
Arbitrary(arbitrary.arbitrary.map(rep.apply(_))) | ||
} |
19 changes: 19 additions & 0 deletions
19
scalacheck/src/main/scala-2/pl/iterators/kebs/scalacheck/Generators.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package pl.iterators.kebs.scalacheck | ||
|
||
import org.scalacheck.Arbitrary | ||
import pl.iterators.kebs.scalacheck.CommonArbitrarySupport | ||
|
||
trait Generator[T] extends CommonArbitrarySupport { | ||
def ArbT: Arbitrary[T] | ||
|
||
def generate: T = ArbT.arbitrary.sample.get | ||
} | ||
|
||
trait AllGenerators[T] { | ||
|
||
val normal: Generator[T] | ||
|
||
val minimal: Generator[T] | ||
|
||
val maximal: Generator[T] | ||
} |
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions
20
scalacheck/src/main/scala-3/pl/iterators/kebs/scalacheck/CommonArbitrarySupport.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package pl.iterators.kebs.scalacheck | ||
|
||
import org.scalacheck.{Arbitrary, Gen} | ||
import pl.iterators.kebs.macros.CaseClass1Rep | ||
|
||
import java.net.{URI, URL} | ||
import java.time.temporal.ChronoUnit | ||
import java.time._ | ||
import java.util.concurrent.TimeUnit | ||
import scala.reflect.ClassTag | ||
import scala.util.Random | ||
import io.github.martinhh.derived.scalacheck.given | ||
import enumeratum.ScalacheckInstances | ||
trait CommonArbitrarySupport extends ScalacheckInstances { | ||
implicit def caseClass1RepArbitraryPredef[T, A]( | ||
implicit rep: CaseClass1Rep[T, A], | ||
arbitrary: Arbitrary[A] | ||
): Arbitrary[T] = | ||
Arbitrary(arbitrary.arbitrary.map(rep.apply(_))) | ||
} |
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
scalacheck/src/main/scala-3/pl/iterators/kebs/scalacheck/KebsScalacheckGenerators.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package pl.iterators.kebs.scalacheck | ||
|
||
import pl.iterators.kebs.scalacheck.macros.KebsScalacheckGeneratorsMacro | ||
import scala.quoted.Quotes | ||
import scala.deriving.Mirror | ||
import pl.iterators.kebs.scalacheck.AllGenerators | ||
trait KebsScalacheckGenerators { | ||
inline implicit final def allGenerators[T](using inline m: Mirror.Of[T]): AllGenerators[T] = KebsScalacheckGeneratorsMacro.materializeGenerators[T] | ||
} |
51 changes: 51 additions & 0 deletions
51
.../src/main/scala-3/pl/iterators/kebs/scalacheck/macros/KebsScalacheckGeneratorsMacro.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package pl.iterators.kebs.scalacheck.macros | ||
|
||
import pl.iterators.kebs.scalacheck._ | ||
import scala.quoted.* | ||
import org.scalacheck.Arbitrary | ||
import scala.deriving.Mirror | ||
import io.github.martinhh.derived.scalacheck.deriveArbitrary | ||
object KebsScalacheckGeneratorsMacro { | ||
|
||
inline implicit def materializeGenerators[T](using inline m: Mirror.Of[T]): AllGenerators[T] = { | ||
|
||
new AllGenerators[T] { | ||
|
||
trait GeneratorCreator | ||
extends CommonArbitrarySupport { | ||
|
||
def create: Generator[T] | ||
} | ||
|
||
object MinimalGeneratorCreator | ||
extends GeneratorCreator | ||
with MinimalArbitrarySupport { | ||
|
||
override def create = new Generator[T] { | ||
def ArbT: Arbitrary[T] = deriveArbitrary[T] | ||
} | ||
} | ||
|
||
object NormalGeneratorCreator | ||
extends GeneratorCreator { | ||
|
||
override def create = new Generator[T] { | ||
def ArbT: Arbitrary[T] = deriveArbitrary[T] | ||
} | ||
} | ||
|
||
object MaximalGeneratorCreator | ||
extends GeneratorCreator | ||
with MaximalArbitrarySupport { | ||
|
||
override def create = new Generator[T] { | ||
def ArbT: Arbitrary[T] = deriveArbitrary[T] | ||
} | ||
} | ||
|
||
override val minimal: Generator[T] = MinimalGeneratorCreator.create | ||
override val maximal: Generator[T] = MaximalGeneratorCreator.create | ||
override val normal: Generator[T] = NormalGeneratorCreator.create | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
scalacheck/src/test/scala-2/pl/iterators/kebs/scalacheck/AnyValGeneratorsTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
import pl.iterators.kebs.scalacheck._ | ||
import java.net.{URI, URL} | ||
import java.time.{Duration, Instant, LocalDate, LocalDateTime, LocalTime, ZonedDateTime} | ||
|
||
case class WrappedInt(int: Int) | ||
case class WrappedIntAnyVal(int: Int) extends AnyVal | ||
case class BasicSample( | ||
someNumber: Int, | ||
someText: String, | ||
wrappedNumber: WrappedInt, | ||
wrappedNumberAnyVal: WrappedIntAnyVal, | ||
) | ||
|
||
class AnyValGeneratorsTests extends AnyFunSuite with Matchers { | ||
|
||
object KebsProtocol extends KebsScalacheckGenerators | ||
|
||
object KebsProtocolWithFancyPredefs extends KebsScalacheckGenerators with KebsArbitraryPredefs | ||
|
||
test("Basic sample test") { | ||
import KebsProtocol._ | ||
|
||
noException should be thrownBy allGenerators[BasicSample].normal.generate | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package pl.iterators.kebs.scalacheck | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
import java.net.{URI, URL} | ||
import java.time.{Duration, Instant, LocalDate, LocalDateTime, LocalTime, ZonedDateTime} | ||
import pl.iterators.kebs.opaque.Opaque | ||
|
||
|
||
case class WrappedInt(int: Int) | ||
|
||
opaque type OpaqueInt = Int | ||
object OpaqueInt extends Opaque[OpaqueInt, Int] { | ||
override def apply(value: Int) = value | ||
} | ||
|
||
case class BasicSampleWithOpaque( | ||
someNumber: Int, | ||
someText: String, | ||
wrappedNumber: WrappedInt, | ||
opaqueInt: OpaqueInt | ||
) | ||
|
||
class OpaqueGeneratorsTests extends AnyFunSuite with Matchers { | ||
|
||
object KebsProtocol extends KebsScalacheckGenerators | ||
|
||
object KebsProtocolWithFancyPredefs extends KebsScalacheckGenerators with KebsArbitraryPredefs | ||
|
||
test("Basic sample with opaque type test") { | ||
import KebsProtocol._ | ||
|
||
noException should be thrownBy allGenerators[BasicSampleWithOpaque].normal.generate | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters