0

The official scalacheck documentation gives the following example:

  property("stringLength") = Prop.forAll { s: String =>
    val len = s.length
    (s+s).length == len+len
  }

I read that this can also be written as:

  val stringLength = Prop.forAll { s: String =>
    val len = s.length
    (s+s).length == len+len
  }

How can I run the second form of test code? When I execute sbt test, nothing happens with the second version.

Pavel
  • 3,303
  • 24
  • 31
  • This was originally asked under http://stackoverflow.com/questions/35886206/no-implicit-view-available-from-anyval-org-scalacheck-prop-error-property/38283963 but I split off the second question here as it was not directly related to the first problem. – Pavel Jul 09 '16 at 16:49

1 Answers1

0

The problem is that the second version is simply declaring a val that holds a reference to the property in question but that isn't enough to get scalacheck to evaluate it. This style of declaring a property is useful for example if you want to compose a new property out of other basic properties. You can check it directly or to assign it the special property setter in order to get it to be evaluated as part of the test run:

val stringLength = Prop.forAll { s: String =>
  val len = s.length
  (s+s).length == len+len
}

// invoke directly:
stringLength.check

// alternatively, just declare it the usual way
property("stringLength") = stringLength

This isn't very useful by itself, the way it is meant to be used is perhaps something like this:

property("composite") = Prop.all(stringLength, prop2, prop3, ...)
Pavel
  • 3,303
  • 24
  • 31