4

The following use of Prop.collect((a, b)) isn't printing the statistics as expected even though the test runs successfully.

import org.scalacheck.Prop
import org.scalatest.{GivenWhenThen, PropSpec}
import org.scalatest.prop.{Checkers, GeneratorDrivenPropertyChecks}

import org.scalacheck.Prop.AnyOperators

class AccountSpecWithMarkup extends PropSpec with Checkers with GeneratorDrivenPropertyChecks {

  property("max") {
    check({
      (a:Int, b:Int) => {

        Prop.collect((a, b)) {

          a != b || a == b
        }
      }
    })
  }
}
Alfredo Gimenez
  • 2,174
  • 1
  • 14
  • 19
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327

1 Answers1

1

An old question, probably long since answered or worked around, but since I'm a scalacheck newbie trying to figure out the ropes, I'll volunteer a response.

Not sure why your example doesn't work as expected, but this simpler test does the trick (in Scalacheck 1.14):

import org.scalacheck.Properties
import org.scalacheck.Prop.{forAll,collect}

object AccountSpecWithMarkup extends Properties("AccountSpecWithMarkup") {

  property("max") = forAll { (a: Int, b: Int) =>
    collect((a, b)) {
      a != b || a == b
    }
  }
}

Note that the test is now an object extending Properties. The Properties.property val is of type PropertySpecifier, which encapsulates mutable state (a collection of Props) and has an update method. This line

property("max") = forAll { ... }

is syntactic sugar for

property.update("max", forAll{ ... })

where the forAll generates a value of type Prop. This Prop value is accumulated for later evaluation using implicit Arbitrary[Int] generators to generate test values for a and b, which are collected by collect for reporting in the test results.

Here's the first few lines of a successful test run

+ AccountsSpectWithMarkup.max: OK, passed 100 tests.
> Collected test data: 
4% (-2147483648,2147483647)
2% (1,2147483647)
2% (-1,-1)
2% (-1,0)
1% (-1,-1128775662)
1% (501893471,-2147483648)
1% (0,0)
1% (1529964222,-1507103054)
1% (36753817,-2147483648)
1% (2147483647,535423354
Paul
  • 66
  • 4