How to use Specs2 with Scalacheck to automate testing of String arguments?
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.
For more information on using the ScalaCheck library in specs2, check out the ScalaCheck Guide in the specs2 documentation.