I had the same question today & ended up here so thought I'd add my solution which was to generate Prop
s of my special cases prior to using a Gen
, like so:
import org.scalacheck.Gen.{alphaChar, const}
import org.scalacheck.Prop.{forAll, passed}
import org.scalacheck.{Gen, Prop}
// evaluate fn first with some initial values, then with some generated ones
def forAllAfter[A](init: A*)(subsequent: Gen[A])(fn: A => Prop): Prop =
init.foldLeft(passed) { case (p, i) => p && forAll(const(i))(fn) } && forAll(subsequent)(fn)
// example of usage
val prop = forAllAfter('a', 'b', 'c')(alphaChar) { c =>
println(c)
passed
}
The forAllAfter
function here first creates Prop
s using Gen.const
for each value that must be tested then combines them with props created using a generator of subsequent values to test.
If you're using ScalaTest
is that you then need to mix the Checkers
trait into your test to evaluate the resultant Prop
like so:
import org.scalatest.WordSpec
import org.scalatest.prop.Checkers
import org.scalacheck.Gen.{alphaChar, const}
import org.scalacheck.Prop.{forAll, passed}
import org.scalacheck.{Gen, Prop}
class TestExample extends WordSpec with Checkers {
def forAllAfter[A](init: A*)(subsequent: Gen[A])(fn: A => Prop): Prop =
init.foldLeft(passed) { case (p, i) => p && forAll(const(i))(fn) } && forAll(subsequent)(fn)
val prop: Prop = forAllAfter('a', 'b', 'c')(alphaChar) { c =>
println(c)
passed
}
"Test example" should {
"Work correctly" in {
check(prop)
}
}
}