Skip to content

Instantly share code, notes, and snippets.

@kpb
Created April 14, 2015 17:04
Show Gist options
  • Save kpb/776d5b9fbbd407552a57 to your computer and use it in GitHub Desktop.
Save kpb/776d5b9fbbd407552a57 to your computer and use it in GitHub Desktop.
Using ScalaCheck forAll with a custom Generator that creates a tuple
// Instead of nesting forAll calls, we could have defined a custom generator in the following way:
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen.choose
val genStringWithN = for {
s <- arbitrary[String]
n <- choose(0, s.length)
} yield (s,n)
// We can now specify the property with only one forAll call:
import org.scalacheck.Prop.forAll
val propPrefix = forAll(genStringWithN) { case (s,n) =>
val prefix = s.substring(0, n)
s.startsWith(s)
}
// Notice that we have to use a case-expression since our property takes one tuple as
// its argument, not two separate arguments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment