Last active
February 4, 2016 16:21
-
-
Save asflierl/b780b61550ed6aaa2cb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalacheck._, Prop.forAll, Gen._, Arbitrary._ | |
object MeepSpec extends Properties("Meep") { | |
case class Meep(id: Int, name: String) | |
implicit lazy val arbMeep: Arbitrary[Meep] = Arbitrary(for (i <- arbitrary[Int]; n <- alphaStr) yield Meep(i, n)) | |
// using ScalaCheck 1.12.5, this outlandish property would have passed: | |
property("meep1") = forAll { (f: Meep => Meep, m1: Meep, m2: Meep) => f(m1) == f(m2) } | |
// using ScalaCheck 1.13.0, we have to define how function input influences function output via an instance of Cogen: | |
implicit lazy val coMeep: Cogen[Meep] = Cogen(_.id) | |
// `coMeep` is used to create `f`, so that in this example, only the `id` of the Meep input is "mixed" into the seed | |
// of the deterministic random generator used to generate the Meep output (of `f`), effectively emulating functions | |
// that do nothing with the `name` field of a Meep: | |
property("meep2") = forAll { (f: Meep => Meep, m: Meep, s: String) => f(m) == f(Meep(m.id, s)) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment