Created
March 29, 2012 00:55
-
-
Save etorreborre/2231981 to your computer and use it in GitHub Desktop.
Using Scalaz to get the applicative syntax for ScalaCheck generators
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 scalaz.Apply | |
import org.scalacheck.Gen | |
import org.scalacheck.Arbitrary._ | |
/** | |
* Use this trait to get applicative syntax when building generators. This | |
* is especially useful when building generators for case classes. | |
* | |
*/ | |
trait Data { | |
/** | |
* import scalaz.Scalaz._ | |
* case class MyClass(a: A, b: B, c: C) | |
* | |
* val MyCaseClassGenerator: Gen[MyCaseClass] = (genA |@| genB |@| genC)(MyCaseClass) | |
* | |
*/ | |
implicit def genIsApply: Apply[Gen] = new Apply[Gen] { | |
def ap[A, B](fa: => Gen[A])(f: => Gen[(A) => B]) = fa.map2(f)((v, function) => function(v)) | |
def map[A, B](fa: Gen[A])(f: (A) => B) = fa map f | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That was supposed to be clear from the comment :-). Something like that:
Then, in specs2 you can write properties like: