Skip to content

Commit

Permalink
Merge pull request #328 from theiterators/kebs-scalacheck-magnolia-#327
Browse files Browse the repository at this point in the history
Kebs-scalacheck - derive Arbitrary instance using Magnolia instead of Shapeless
  • Loading branch information
pk044 committed Oct 31, 2023
2 parents 65c183a + 71c091c commit d2d3da0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
5 changes: 2 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ val circeParser = "io.circe" %% "circe-parser" % circeV
val jsonschema = "com.github.andyglow" %% "scala-jsonschema" % "0.7.11"

val scalacheck = "org.scalacheck" %% "scalacheck" % "1.17.0" % "test"
val scalacheckShapeless = "com.github.alexarchambault" %% "scalacheck-shapeless_1.15" % "1.3.0"
val scalacheckMagnolify = "com.spotify" % "magnolify-scalacheck" % "0.6.4"
val scalacheckDerived = "io.github.martinhh" %% "scalacheck-derived" % "0.4.2"
val scalacheckEnumeratum = "com.beachape" %% "enumeratum-scalacheck" % "1.7.3"

Expand Down Expand Up @@ -262,10 +262,9 @@ lazy val jsonschemaSettings = commonSettings ++ Seq(
lazy val scalacheckSettings = commonSettings ++ Seq(
libraryDependencies += scalacheck.cross(CrossVersion.for3Use2_13),
libraryDependencies += scalacheckEnumeratum.cross(CrossVersion.for3Use2_13),
libraryDependencies += scalacheckShapeless.cross(CrossVersion.for3Use2_13),
) ++ Seq(
libraryDependencies ++= (if (scalaVersion.value.startsWith("3")) Seq(scalacheckDerived)
else Nil))
else Nil)) ++ Seq(libraryDependencies ++= (if(scalaVersion.value.startsWith("2")) Seq(scalacheckMagnolify.cross(CrossVersion.for3Use2_13)) else Nil))

lazy val taggedSettings = commonSettings ++ Seq(
libraryDependencies += optionalSlick.cross(CrossVersion.for3Use2_13),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package pl.iterators.kebs.scalacheck

import enumeratum.ScalacheckInstances
import org.scalacheck.{Arbitrary, Gen, ScalacheckShapeless}
import enumeratum.{ScalacheckInstances => EnumScalacheckInstances}
import org.scalacheck.Arbitrary
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 {
trait CommonArbitrarySupport extends EnumScalacheckInstances with ScalacheckInstancesSupport {

implicit def caseClass1RepArbitraryPredef[T, A](
implicit rep: CaseClass1Rep[T, A],
arbitrary: Arbitrary[A]
): Arbitrary[T] =
Arbitrary(arbitrary.arbitrary.map(rep.apply(_)))
}
arb: Arbitrary[A]
): Arbitrary[T] = Arbitrary(arb.arbitrary.map(rep.apply(_)))

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

import org.scalacheck._

trait ScalacheckInstancesSupport {
implicit def arb[T]: Arbitrary[T] = macro magnolify.scalacheck.auto.genArbitraryMacro[T]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ 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 {
import enumeratum.{ScalacheckInstances => EnumScalacheckInstances}
trait CommonArbitrarySupport extends EnumScalacheckInstances {
implicit def caseClass1RepArbitraryPredef[T, A](
implicit rep: CaseClass1Rep[T, A],
arbitrary: Arbitrary[A]
): Arbitrary[T] =
Arbitrary(arbitrary.arbitrary.map(rep.apply(_)))
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pl.iterators.kebs.scalacheck

import enumeratum.ScalacheckInstances
import org.scalacheck.{Arbitrary, Gen, ScalacheckShapeless}
import pl.iterators.kebs.macros.CaseClass1Rep
import org.scalacheck.{Arbitrary, Gen}

import java.net.{URI, URL}
import java.time.temporal.ChronoUnit
Expand Down Expand Up @@ -93,10 +91,10 @@ trait KebsArbitraryPredefs {

implicit val arbUri: Arbitrary[URI] = Arbitrary {
for {
protocol <- Gen.oneOf("http", "https", "ftp", "file")
domain <- Gen.alphaNumStr
protocol <- Gen.oneOf("http", "https", "ftp", "file")
domain <- Gen.alphaNumStr
subdomain <- Gen.alphaNumStr
path <- Gen.alphaNumStr
path <- Gen.alphaNumStr
} yield new URI(s"$protocol://$subdomain.$domain.test/$path")
}

Expand Down

0 comments on commit d2d3da0

Please sign in to comment.