@@ -17,48 +17,77 @@ import scala.concurrent.duration.{Duration, FiniteDuration}
17
17
import util .Buildable
18
18
import util .SerializableCanBuildFroms ._
19
19
20
-
21
- sealed abstract class Arbitrary [T ] extends Serializable {
22
- def arbitrary : Gen [T ]
23
- }
24
-
25
- /** Defines implicit [[org.scalacheck.Arbitrary ]] instances for common types.
26
- * <p>
27
- * ScalaCheck
28
- * uses implicit [[org.scalacheck.Arbitrary ]] instances when creating properties
29
- * out of functions with the `Prop.property` method, and when
30
- * the `Arbitrary.arbitrary` method is used. For example, the
31
- * following code requires that there exists an implicit
32
- * `Arbitrary[MyClass]` instance:
33
- * </p>
20
+ /**
21
+ * Define an arbitrary generator for properties
22
+ *
23
+ * The [[Arbitrary ]] module defines implicit generator instances for
24
+ * common types.
25
+ *
26
+ * The implicit definitions of [[Arbitrary ]] provide type-directed
27
+ * [[Gen ]]s so they are available for properties, generators, or other
28
+ * definitions of [[Arbitrary ]].
29
+ *
30
+ * ScalaCheck expects an implicit [[Arbitrary ]] instance is in scope
31
+ * for [[Prop ]]s that are defined with functions, like [[Prop$.forAll[T1,P](g1*
32
+ * Prop.forAll]] and so on.
33
+ *
34
+ * For instance, the definition for `Arbitrary[Boolean]` is used by
35
+ * `Prop.forAll` to automatically provide a `Gen[Boolean]` when one
36
+ * of the parameters is a `Boolean`:
34
37
*
35
38
* {{{
36
- * val myProp = Prop.forAll { myClass: MyClass =>
37
- * ...
39
+ * Prop.forAll { (b: Boolean) =>
40
+ * b || !b
38
41
* }
42
+ * }}}
39
43
*
40
- * val myGen = Arbitrary.arbitrary[MyClass]
44
+ * Thanks to `Arbitrary`, you don't need to provide an explicit
45
+ * `Gen` instance to `Prop.forAll`. For instance, this is
46
+ * unnecessary:
47
+ *
48
+ * {{{
49
+ * val genBool: Gen[Boolean] = Gen.oneOf(true,false)
50
+ * Prop.forAll(genBool) { (b: Boolean) =>
51
+ * b || !b
52
+ * }
41
53
* }}}
42
54
*
43
- * <p>
44
- * The required implicit definition could look like this:
45
- * </p>
55
+ * Since an arbitrary `Gen` for `Boolean` is defined in `Arbitrary`,
56
+ * it can be summoned with `Arbitrary.arbitrary` in cases where you
57
+ * need to provide one explicitly:
46
58
*
47
59
* {{{
48
- * implicit val arbMyClass: Arbitrary[MyClass] = Arbitrary(...)
60
+ * val genBool: Gen[Boolean] = Arbitrary.arbitrary[Boolean]
61
+ * val genSmallInt: Gen[Int] = Gen.choose(0, 9)
62
+ * Prop.forAll(genBool, genSmallInt) { (b: Boolean, i: Int) =>
63
+ * i < 10 && b || !b
64
+ * }
49
65
* }}}
50
66
*
51
- * <p>
52
- * The factory method `Arbitrary(...)` takes a generator of type
53
- * `Gen[T]` and returns an instance of `Arbitrary[T]`.
54
- * </p>
67
+ * For a user-defined `MyClass`, writing the following requires that
68
+ * there exists an implicit `Arbitrary[MyClass]` instance:
55
69
*
56
- * <p>
57
- * The `Arbitrary` module defines implicit [[org.scalacheck.Arbitrary ]]
58
- * instances for common types, for convenient use in your properties and
59
- * generators.
60
- * </p>
70
+ * {{{
71
+ * Prop.forAll { (myClass: MyClass) =>
72
+ * ...
73
+ * }
74
+ * }}}
75
+ *
76
+ * The implicit definition of `Arbitrary[MyClass]` would look like:
77
+ *
78
+ * {{{
79
+ * implicit val arbMyClass: Arbitrary[MyClass] = Arbitrary {
80
+ * ...
81
+ * }
82
+ * }}}
83
+ *
84
+ * The factory method `Arbitrary(...)` expects a generator of type
85
+ * `Gen[MyClass]` then it will return an instance of `Arbitrary[MyClass]`.
61
86
*/
87
+ sealed abstract class Arbitrary [T ] extends Serializable {
88
+ def arbitrary : Gen [T ]
89
+ }
90
+
62
91
object Arbitrary extends ArbitraryLowPriority with ArbitraryArities with time.JavaTimeArbitrary {
63
92
64
93
/** Arbitrary instance of the Function0 type. */
0 commit comments