Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kebs-scalacheck - derive Arbitrary instance using Magnolia instead of Shapeless #328

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading