ScalaTest with ScalaCheck
Scala Community ING
By Tim Soethout
$\forall s : String \rightarrow s.reverse.reverse \equiv s$
or:
forAll((s: String) => s.reverse.reverse == s)
+ String.should be the same after reversing twice: OK, passed 100 tests.
Checking properties on random datasets created by generators
def reverseStrings(list: List[String]): List[String] = {
list match {
case Nil => Nil
case (x :: xs) => reverseStrings(xs) ++ List(x)
}
}
property("should be the same after reversing twice") =
forAll((ss: List[String]) => reverseStrings(reverseStrings(ss)) == ss)
behavior of "reverseStrings"
it should "give the same after reversing twice" in {
forAll {
(ss: List[String]) =>
reverseStrings(reverseStrings(ss)) should equal(ss)
}
}
Generators for primitive types and case classes are provided
def bbans: Gen[String] =
for {
n <- Gen.chooseNum(0, 9999999)
} yield n.toString