ScalaCheckFunctions forAll vs. ScalaCheck's Prop.forAll

32 views
Skip to first unread message

Daniel Wellman

unread,
Feb 8, 2009, 10:39:43 AM2/8/09
to specs-users
Today while using ScalaCheck and Specs I encountered a compiler error
that I was able to solve, but I didn't understand the intent of the
forAll method in ScalaCheckFunctions. I'm using ScalaCheck 1.5, Specs
1.4.2, and Scala 2.7.3.

Given the following imports to my Spec test object:

import com.domainlanguage.time.Duration
import org.specs.Specification
import org.specs.ScalaCheck
import org.specs.runner.{ConsoleRunner, JUnit4}
import org.scalacheck.Prop
// Need to import Prop._ to get implicit functions to support adding
==> to Booleans
import org.scalacheck.Prop._


When I have this specification using forAll without the explicit Prop
qualifier:

"convert all Ints to Durations" in {
val conversion = forAll{ x: Int =>
x >= 0 ==> (x.seconds == Duration.seconds(x))
}
conversion must pass
}

The compile fails with this message:

[WARNING] found : (Int) => org.scalacheck.Prop
[WARNING] required: org.scalacheck.Gen[?]
[WARNING] val conversion = forAll{ x: Int =>
[WARNING] ^
[WARNING] one error found

If I change the use of forAll to explicitly name Prop.forAll

val conversion = Prop.forAll{ x: Int => // ...

The code compiles and the specs pass. It looks like
ScalaCheckFunctions also defines a forAll method, and the compiler
picks this over Prop.forAll in my first example. The ScalaDoc for
ScalaCheckFunctions says "This trait is used to facilitate testing by
mocking ScalaCheck functionalities".

How is ScalaCheckFunctions forAll meant to be used? If you need to
use ==> in ScalaCheck, the documentation recommends importing
org.scalacheck.Prop._, which causes the name conflict. Is there some
other way to resolve this?

Thanks for your advice!

Cheers,
Dan





Full source code here:

package com.danielwellman.timeandmoneysugar

import com.domainlanguage.time.Duration
import org.specs.Specification
import org.specs.ScalaCheck
import org.specs.runner.{ConsoleRunner, JUnit4}
import org.scalacheck.Prop
// Need to import Prop._ to get implicit functions to support adding
==> to Booleans
import org.scalacheck.Prop._

// For Maven Surefire plugin to see the tests
class DurationSpecTest extends JUnit4(DurationSpec)

object DurationSpec extends Specification with ScalaCheck {
// Import the implicit conversion function
import com.danielwellman.timeandmoneysugar.RichDuration._

"RichDuration" should {

"convert all Ints to Durations" in {
val conversion = Prop.forAll{ x: Int =>
x >= 0 ==> (x.seconds == Duration.seconds(x))
}
conversion must pass
}
}
}

etorreborre

unread,
Feb 8, 2009, 9:23:05 PM2/8/09
to specs-users
Hi Daniel,

That's a very good catch. I'm using the ScalaCheckFunctions trait
indeed to be able to override the forAll function from ScalaCheck in
my code and do some testing. But this introduces an unnecessary
conflict with the forAll method from ScalaCheck in general
specification code.

I changed that part and uploaded a new version of specs in specs-1.4.3-
SNAPSHOT available on the Maven repository. Please use that one.

Thanks,

Eric.
> }- Hide quoted text -
>
> - Show quoted text -

Daniel Wellman

unread,
Feb 9, 2009, 10:45:15 PM2/9/09
to specs-users
Hi Eric,

Thanks for looking into this and the quick turnaround on the patch!

Cheers,
Dan

David Pollak

unread,
Feb 10, 2009, 1:50:26 PM2/10/09
to specs...@googlegroups.com
Eric,

This addresses a Lift-related issue as well.

Please let the Lift community know when 1.4.3 graduates from SNAPSHOT.

Thanks,

David
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
Reply all
Reply to author
Forward
0 new messages