The rewritten specs2 testing framework for Scala integrates automated testing with scalacheck. The examples given in the specs2 documentation on how to use scalacheck together with specs2 either use integers or more complicated custom generators as in eric's json example.
While trying to get a slightly less complicated example working, I'm struggling because I don't know how one would use specs2 and scalacheck if I want to generate String arguments instead of Integers. How would this Quickstart example
import org.scalacheck._
object StringSpecification extends Properties("String") {
property("startsWith") = Prop.forAll((a: String, b: String)
=> (a+b).startsWith(a))
property("endsWith") = Prop.forAll((a: String, b: String)
=> (a+b).endsWith(b))
// Is this really always true?
property("concat") = Prop.forAll((a: String, b: String) =>
(a+b).length > a.length && (a+b).length > b.length
)
property("substring") = Prop.forAll((a: String, b: String) =>
(a+b).substring(a.length) == b
)
property("substring") = Prop.forAll((a: String, b: String, c: String) =>
(a+b+c).substring(a.length, a.length+b.length) == b
)
}
taken from the scalacheck homepage look, if it was written as Specs2 specification using the scalacheck integration?
A very direct translation is using the check
method and simple functions:
package test
import org.specs2._
import org.scalacheck._
class ScalaCheckExamples extends Specification with ScalaCheck { def is =
"startsWith" ! check { (a: String, b: String) => (a+b).startsWith(a) } ^
"endsWith" ! check { (a: String, b: String) => (a+b).endsWith(b) } ^
"concat" ! check { (a: String, b: String) => (a+b).length > a.length && (a+b).length > b.length } ^
"substring" ! check { (a: String, b: String) => (a+b).substring(a.length) == b } ^
"substring" ! check { (a: String, b: String, c: String) => (a+b+c).substring(a.length, a.length+b.length) == b } ^
end
}
And the output actually shows that the concat
property is not correct:
[info] + startsWith
[info] + endsWith
[error] x concat
[error] A counter-example is ['', ''] (after 0 try)
[error] the value is false
[error] (ScalaCheckExamplesSpec.scala:6)
[info] + substring
[info] + substring
[info]
[info] Total for specification ScalaCheckExamplesSpec
[info] Finished in 7 seconds, 547 ms
[info] 5 examples, 401 expectations, 1 failure, 0 error
[info]
Eric.