Scalacheck ignores provided generators

I am trying to implement a simple property check, but Scalacheck ignores my generators. What am I doing wrong here?

object AlgorithmTest extends Properties("Algorithm") {
  property("Test") = forAll (Gen.choose(0,10)) (n => n>=0 & n<10)
}

and this is the result in SBT

[info] ! Algorithm.Test: Falsified after 12 passed tests. [info] >
ARG_0: -1 [error] Failed: : Total 1, Failed 1, Errors 0, Passed 0,
Skipped 0
+5
source share
1 answer

It appears that the Shrink instance that is passed to the method forAlldoes not use the generator when looking for smaller counter examples. If you change your property to:

property("Test") = Prop.forAllNoShrink(Gen.choose(1, 10)) (n => n >= 0 && n < 10)

Then it should normally crash:

[info] ! Algorithm.Test: Falsified after 7 passed tests.
[info] > ARG_0: 10
[error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0

One way to visualize Shrink values ​​is to use a method Prop.collect:

property("Test") = Prop.forAll(Gen.choose(1, 10)) { n =>
  Prop.collect(n) { n >= 0 && n < 10 }
}

Then the collected values ​​look like this:

[info] ! Algorithm.Test: Falsified after 40 passed tests.
[info] > ARG_0: -1
[info] > Collected test data: 
[info] 17% 3
[info] 17% 1
[info] 15% 6
[info] 12% 9
[info] 10% 2
[info] 10% 5
[info] 7% 4
[info] 7% 8
[info] 2% -1
[info] 2% 7

Where you can see that -1 was used during the compression process.

+4
source

All Articles