ScalaCheck integration
Weaver comes with basic ScalaCheck integration, allowing for property-based testing.
Installation
You'll need to install an additional dependency in order to use ScalaCheck with Weaver.
SBT
libraryDependencies += "com.disneystreaming" %% "weaver-scalacheck" % "0.0-0e0ca96-SNAPSHOT" % Test
Mill
object test extends Tests {
def ivyDeps = Agg(
ivy"com.disneystreaming::weaver-scalacheck:0.0-0e0ca96-SNAPSHOT"
)
}
Usage
Add the weaver.scalacheck.Checkers
mixin to use ScalaCheck within your test suite.
import org.scalacheck.Gen
import weaver._
import weaver.scalacheck._
object ForallExamples extends SimpleIOSuite with Checkers {
// Using a single `Gen` instance
test("Single Gen form") {
// Takes a single, explicit `Gen` instance
forall(Gen.posNum[Int]) { a =>
expect(a > 0)
}
}
// There is only one overload for the `forall` that takes an explicit `Gen` parameter
// To use multiple `Gen` instances, compose them monadically before passing to `forall`
test("Multiple Gen form") {
// Compose into a single `Gen[(Int, Int)]`
val gen = for {
a <- Gen.posNum[Int]
b <- Gen.posNum[Int]
} yield (a, b)
// Unapply the tuple to access individual members
forall(gen) { case (a, b) =>
expect(a > 0) and expect(b > 0)
}
}
// Using a number of `Arbitrary` instances
test("Arbitrary form") {
// There are 6 overloads, to pass 1-6 parameters
forall { (a1: Int, a2: Int, a3: Int) =>
expect(a1 * a2 * a3 == a3 * a2 * a1)
}
}
test("Failure example") {
// There are 6 overloads, to pass 1-6 parameters
forall { (a1: Int, a2: Int) =>
expect(a1 + a2 % 2 == 0)
}
}
}
repl.MdocSessionMdocAppForallExamples + Single Gen form 95ms + Multiple Gen form 78ms + Arbitrary form 89ms - Failure example 37ms *************FAILURES************* repl.MdocSessionMdocAppForallExamples - Failure example 37ms
[0] Property test failed on try 2 with seed Seed.fromBase64("e9eBvB0Mn-Xp4zc70sJ7UI3NUjkJtjzGcEOEp3OjItN=") and input (1729485212,-2147483648). (modules/scalacheck/shared/src/main/scala/weaver/scalacheck/Checkers.scala:167)
[0] You can reproduce this by adding the following override to your suite:
[0]
[0] override def checkConfig = super.checkConfig.withInitialSeed(Seed.fromBase64("e9eBvB0Mn-Xp4zc70sJ7UI3NUjkJtjzGcEOEp3OjItN=").toOption)
[1] assertion failed (scalacheck.md:53)
[1]
[1] expect(a1 + a2 % 2 == 0)
[1] | | | | |
[1] | | | 0 false
[1] | | -2147483648
[1] | 1729485212
[1] 1729485212 Total 4, Failed 1, Passed 3, Ignored 0, Cancelled 0