Given some code to test:
def head[X](xs: List[X]): X = xs.head
We can test it with ScalaCheck by describing properties about it that should be true for arbitrary values:
val headProp =
.scalacheck.Prop.forAll { (f: Int => Int, xs: List[Int]) =>
org.isEmpty || f(head(xs)) == head(xs map f)
xs}
.check headProp
This file is literate Scala, and can be run using Codedown:
/***
+= "org.scalacheck" %% "scalacheck" % "1.14.0"
libraryDependencies */
$ curl -s https://earldouglas.com/posts/scala/scalacheck.md |
codedown scala > script.scala
$ sbt -Dsbt.main.class=sbt.ScriptMain script.scala
+ OK, passed 100 tests.