ScalaCheck

March 12, 2016

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 =
  org.scalacheck.Prop.forAll { (f: Int => Int, xs: List[Int]) =>
    xs.isEmpty || f(head(xs)) == head(xs map f)
  }

headProp.check

This file is literate Scala, and can be run using Codedown:

/***
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.14.0"
*/
$ curl https://earldouglas.com/scala/scalacheck.md |
  codedown scala > script.scala
$ sbt -Dsbt.main.class=sbt.ScriptMain script.scala
+ OK, passed 100 tests.