Created
April 14, 2015 17:04
-
-
Save kpb/776d5b9fbbd407552a57 to your computer and use it in GitHub Desktop.
Using ScalaCheck forAll with a custom Generator that creates a tuple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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