9
9
10
10
package org .scalacheck
11
11
12
- import scala .annotation .tailrec
13
-
14
12
import rng .Seed
15
13
import util .{Pretty , ConsoleReporter }
16
14
17
15
/** Helper class to satisfy ScalaJS compilation. Do not use this directly, use `Prop.apply` instead.
18
16
*/
19
17
sealed class PropFromFun (f : Gen .Parameters => Prop .Result ) extends Prop {
18
+
19
+ /** Evaluate this property by applying the function. */
20
20
def apply (prms : Gen .Parameters ) = f(prms)
21
21
}
22
22
23
23
@ Platform .EnableReflectiveInstantiation
24
- sealed abstract class Prop extends Serializable { self =>
24
+ sealed abstract class Prop extends Serializable {
25
25
26
26
import Prop .{Result , True , False , Undecided , provedToTrue , mergeRes }
27
27
28
+ /** Evaluate this property. */
28
29
def apply (prms : Gen .Parameters ): Result
29
30
30
31
def viewSeed (name : String ): Prop =
@@ -36,7 +37,7 @@ sealed abstract class Prop extends Serializable { self =>
36
37
val sd = Seed .random()
37
38
(prms0.withInitialSeed(sd), sd)
38
39
}
39
- val res = self (prms)
40
+ val res = apply (prms)
40
41
if (res.failure) println(s " failing seed for $name is ${seed.toBase64}" )
41
42
res
42
43
}
@@ -46,7 +47,7 @@ sealed abstract class Prop extends Serializable { self =>
46
47
useSeed(seed)
47
48
48
49
def useSeed (seed : Seed ): Prop =
49
- Prop (prms0 => self (prms0.withInitialSeed(seed)))
50
+ Prop (prms0 => apply (prms0.withInitialSeed(seed)))
50
51
51
52
def contramap (f : Gen .Parameters => Gen .Parameters ): Prop =
52
53
new PropFromFun (params => apply(f(params)))
@@ -197,14 +198,12 @@ object Prop {
197
198
labels : Set [String ] = Set .empty
198
199
) {
199
200
def success = status match {
200
- case True => true
201
- case Proof => true
201
+ case True | Proof => true
202
202
case _ => false
203
203
}
204
204
205
205
def failure = status match {
206
- case False => true
207
- case Exception (_) => true
206
+ case False | Exception (_) => true
208
207
case _ => false
209
208
}
210
209
@@ -276,6 +275,10 @@ object Prop {
276
275
case (Proof , _) => mergeRes(this , r, r.status)
277
276
case (True , _) => mergeRes(this , r, r.status)
278
277
}
278
+
279
+ def flatMap (f : Result => Result ): Result = if (success) f(this ) else this
280
+
281
+ def recover (f : Result => Result ): Result = if (failure) f(this ) else this
279
282
}
280
283
281
284
sealed trait Status
@@ -1274,14 +1277,12 @@ object Prop {
1274
1277
/** Ensures that the property expression passed in completes within the given space of time.
1275
1278
*/
1276
1279
def within (maximumMs : Long )(wrappedProp : => Prop ): Prop = {
1277
- @ tailrec def attempt (prms : Gen .Parameters , endTime : Long ): Result = {
1280
+ def attempt (prms : Gen .Parameters , endTime : Long ): Result = {
1278
1281
val result = wrappedProp.apply(prms)
1279
- if (System .currentTimeMillis > endTime) {
1280
- (if (result.failure) result else Result (status = False )).label(" Timeout" )
1281
- } else {
1282
- if (result.success) result
1283
- else attempt(prms, endTime)
1284
- }
1282
+ if (System .currentTimeMillis > endTime)
1283
+ result.flatMap(_ => Result (status = False )).label(" Timeout" )
1284
+ else
1285
+ result.recover(_ => attempt(prms, endTime))
1285
1286
}
1286
1287
Prop .apply(prms => attempt(prms, System .currentTimeMillis + maximumMs))
1287
1288
}
0 commit comments