Skip to content

Commit

Permalink
Fixing #309 (#397)
Browse files Browse the repository at this point in the history
* Fixing #309

* fixing compat with Scala 2
  • Loading branch information
luksow authored Sep 19, 2024
1 parent 96effd3 commit 24c7522
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pl.iterators.kebs.scalacheck

import pl.iterators.kebs.scalacheck.macros.KebsScalacheckGeneratorsMacro

trait KebsScalacheckGenerators {
trait KebsScalacheckGenerators extends CommonArbitrarySupport {
// format: off
implicit def allGenerators[T]: pl.iterators.kebs.scalacheck.AllGenerators[T] =
macro KebsScalacheckGeneratorsMacro.materializeGenerators[T]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
package pl.iterators.kebs.scalacheck

import pl.iterators.kebs.scalacheck.macros.KebsScalacheckGeneratorsMacro
import org.scalacheck.Arbitrary
import scala.deriving.Mirror

trait KebsScalacheckGenerators {
inline implicit final def allGenerators[T](using inline m: Mirror.Of[T]): AllGenerators[T] =
KebsScalacheckGeneratorsMacro.materializeGenerators[T]
trait KebsScalacheckGenerators extends CommonArbitrarySupport {
inline implicit final def allGenerators[T](using inline m: Mirror.Of[T]): AllGenerators[T] = {
new AllGenerators[T] {

override val minimal: Generator[T] = {
import MinimalArbitrarySupport._
val arbitrary = summon[Arbitrary[T]]
new Generator[T] {
def ArbT: Arbitrary[T] = arbitrary
}
}
override val maximal: Generator[T] = {
import MaximalArbitrarySupport._
val arbitrary = summon[Arbitrary[T]]
new Generator[T] {
def ArbT: Arbitrary[T] = arbitrary
}
}
override val normal: Generator[T] = {
val arbitrary = summon[Arbitrary[T]]
new Generator[T] {
def ArbT: Arbitrary[T] = arbitrary
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
package pl.iterators.kebs.scalacheck

trait ScalacheckInstancesSupport
import org.scalacheck._
import io.github.martinhh.derived.scalacheck._
import scala.deriving.Mirror

trait ScalacheckInstancesSupport {
inline implicit def arb[T](using inline mirror: Mirror.Of[T]): Arbitrary[T] = deriveArbitrary[T]
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ trait MinimalArbitrarySupport {
Arbitrary(Gen.const(Map.empty[T, U]))
}

object MinimalArbitrarySupport extends MinimalArbitrarySupport

trait MaximalArbitrarySupport {
implicit def someOption[T: Arbitrary]: Arbitrary[Option[T]] =
Arbitrary(Gen.some(Arbitrary.arbitrary[T]))
Expand All @@ -55,6 +57,8 @@ trait MaximalArbitrarySupport {
Arbitrary(Gen.mapOfN(1 + Random.nextInt(3), Arbitrary.arbitrary[(T, U)]))
}

object MaximalArbitrarySupport extends MaximalArbitrarySupport

trait KebsArbitraryPredefs {
implicit val arbAlphaString: Arbitrary[String] =
Arbitrary(Gen.alphaNumStr)
Expand Down Expand Up @@ -99,5 +103,6 @@ trait KebsArbitraryPredefs {
}

implicit val arbUrl: Arbitrary[URL] = Arbitrary(arbUri.arbitrary.map(_.toURL))

}

object KebsArbitraryPredefs extends KebsArbitraryPredefs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ trait CommonArbitrarySupport extends ScalacheckInstancesSupport {
): Arbitrary[T] =
Arbitrary(arbitrary.arbitrary.map(rep.apply(_)))
}

object CommonArbitrarySupport extends CommonArbitrarySupport
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package pl.iterators.kebs.scalacheck
import org.scalacheck.rng.Seed
import org.scalacheck.{Arbitrary, Gen}

trait Generator[T] extends CommonArbitrarySupport {
trait Generator[T] {
def ArbT: Arbitrary[T]

def generate: T = ArbT.arbitrary.pureApply(Gen.Parameters.default, Seed.random())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package object model {

case class WrappedInt(int: Int)
case class WrappedIntAnyVal(int: Int) extends AnyVal

case class BasicSample(
someNumber: Int,
someText: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class OpaqueGeneratorsTests extends AnyFunSuite with Matchers {
test("Basic sample with opaque type test") {
import KebsProtocol._

noException should be thrownBy allGenerators[BasicSampleWithOpaque].normal.generate
noException should be thrownBy allGenerators[BasicSample].normal.generate
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package pl.iterators.kebs.scalacheck

import pl.iterators.kebs.opaque.Opaque
import pl.iterators.kebs.core.enums.ValueEnumLikeEntry
import java.time._

package object model {
case class WrappedInt(int: Int)

enum Greeting {
case Hello, GoodBye, Hi, Bye
}
Expand All @@ -17,18 +16,17 @@ package object model {
case Bye extends LongGreeting(3L)
}

opaque type OpaqueInt = Int
object OpaqueInt extends Opaque[OpaqueInt, Int] {
opaque type WrappedInt = Int
object WrappedInt extends Opaque[WrappedInt, Int] {
override def apply(value: Int) = value
}

case class BasicSampleWithOpaque(
case class BasicSample(
someNumber: Int,
someText: String,
wrappedNumber: WrappedInt,
opaqueInt: OpaqueInt,
opaqueInt: WrappedInt,
greeting: Greeting,
longGreeting: LongGreeting
)

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package pl.iterators.kebs.scalacheck

import org.scalacheck.Arbitrary
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.scalacheck._

case class CollectionsSample(
listOfNumbers: List[Int],
Expand Down Expand Up @@ -50,9 +52,24 @@ class GeneratorsTests extends AnyFunSuite with Matchers {
maximal.optionOfNumber shouldNot be(empty)
maximal.mapOfNumberString shouldNot be(empty)
}

test("Non standard types sample test") {
import KebsProtocolWithFancyPredefs._

noException should be thrownBy allGenerators[NonStandardTypesSample].normal.generate
}

test("Issue 309") {
import KebsProtocol._
import model._

implicit val wrappedInt: Arbitrary[WrappedInt] = Arbitrary {
WrappedInt(42)
}

val generator = allGenerators[BasicSample]
val basicSample = generator.normal.generate

basicSample.wrappedNumber shouldBe WrappedInt(42)
}
}

0 comments on commit 24c7522

Please sign in to comment.