Generation of arbitrary case classes / ADTs with scalacheck and shapeless
Add to your build.sbt
resolvers += Resolver.sonatypeRepo("releases")
libraryDependencies +=
"com.github.alexarchambault" %% "scalacheck-shapeless_1.12" % "0.1.0"
If you are using scala 2.10.x, also add the macro paradise plugin to your build,
libraryDependencies +=
compilerPlugin("org.scalamacros" % "paradise" % "2.0.1" cross CrossVersion.full)
Import the content of org.scalacheck.Shapeless
close to where you want
Arbitrary
type classes to be automatically available for case classes
/ sealed hierarchies,
import org.scalacheck.Shapeless._
// If you defined:
// case class Foo(i: Int, s: String, blah: Boolean)
// case class Bar(foo: Foo, other: String)
// sealed trait Base
// case class BaseIntString(i: Int, s: String) extends Base
// case class BaseDoubleBoolean(d: Double, b: Boolean) extends Base
// then you can now do
implicitly[Arbitrary[Foo]]
implicitly[Arbitrary[Bar]]
implicitly[Arbitrary[Base]]
and in particular, while writing property-based tests,
property("some property about Foo") {
forAll { foo: Foo =>
// Ensure foo has the required property
}
}
without having to define yourself an Arbitrary
for Foo
.
For the development version, add instead to your build.sbt
resolvers += Resolver.sonatypeRepo("snapshots")
libraryDependencies +=
"com.github.alexarchambault" %% "scalacheck-shapeless_1.12" % "0.1.1-SNAPSHOT"
(Macro paradise plugin also necessary with scala 2.10, see above.)
Available for scala 2.10 and 2.11. Uses scalacheck 1.12.1 and shapeless 2.1.0-RC1.