Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Created August 15, 2013 06:09
Show Gist options
  • Select an option

  • Save etorreborre/6238633 to your computer and use it in GitHub Desktop.

Select an option

Save etorreborre/6238633 to your computer and use it in GitHub Desktop.
A checkAll method to check ScalaCheck Properties with immutable specifications
import org.specs2._
import org.scalacheck.Properties
import org.specs2.matcher._
/**
* [info] TestSpec
* [info] MyProps must satisfy
* [info]
* [info] + MyProps.myProp1
* [info] + MyProps.myProp2
* [info] x MyProps.myProp3
* [error] A counter-example is [] (after 0 try)
* [info]
* [info]
* [info] Total for specification TestSpec
* [info] Finished in 14 ms
* [info] 3 examples, 201 expectations, 1 failure, 0 error
*/
class TestSpec extends Specification with ScalaCheckMatchers { def is =
checkAll(MyProps)
def checkAll(props: Properties)(implicit p: Parameters) = s2"""
${props.name} must satisfy
${ props.properties.map { case (name, prop) =>
s2""" ${name ! check(prop)(p)} """
}.reduce(_ append _)
}"""
}
import org.scalacheck.Prop._
object MyProps extends Properties("MyProps") {
property("myProp1") = forAll { (n:Int, m:Int) =>
n+m == m+n
}
property("myProp2") = forAll { (n:Int, m:Int) =>
n+m == m+n
}
property("myProp3") = (0/1) throws classOf[ArithmeticException]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment