Skip to content

Commit d211c81

Browse files
committed
Make Prop.label argument by-name.
1 parent a96d9cd commit d211c81

File tree

8 files changed

+59
-35
lines changed

8 files changed

+59
-35
lines changed

core/jvm/src/test/scala/org/scalacheck/GenSpecification.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
582582
seed = s1
583583
}
584584
val avg = sum / n
585-
s"average = $avg" |: avg >= 0.49 && avg <= 0.51
585+
(avg >= 0.49 && avg <= 0.51).labelImpl2(s"average = $avg")
586586
}
587587

588588
property("uniform long #209") = {
@@ -598,7 +598,7 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
598598
seed = s1
599599
}
600600
val avg = sum / n
601-
s"average = $avg" |: avg >= 0.49 && avg <= 0.51
601+
(avg >= 0.49 && avg <= 0.51).labelImpl2(s"average = $avg")
602602
}
603603
}
604604
////
@@ -651,7 +651,7 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
651651
val s0 = (1 to 30).map(_ => g(params0, Seed.random())).toSet
652652
val s1 = (1 to 30).map(_ => g(params1, Seed.random())).toSet
653653
val s2 = (1 to 30).map(_ => g(params0, seed)).toSet
654-
(s"$s0" |: s0.size > 1) && (s"$s1" |: s1.size == 1) && (s"$s2" |: s2.size == 1)
654+
(s0.size > 1).labelImpl2(s"$s0") && (s1.size == 1).labelImpl2(s"$s1") && (s2.size == 1).labelImpl2(s"$s2")
655655
}
656656

657657
property("arbitrary[Boolean] is deterministic") =

core/jvm/src/test/scala/org/scalacheck/PropertyFilterSpecification.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ object PropertyFilterSpecification extends Properties("PropertyFilter") {
4343

4444
def props = actualNames.forall(expectedNames.contains)
4545

46-
(lengthProp && props) :| diff(filter, actualNames, expectedNames)
46+
(lengthProp && props).labelImpl2(diff(filter, actualNames, expectedNames))
4747
}
4848

4949
property("filter properties by predicate") =

core/jvm/src/test/scala/org/scalacheck/ShrinkSpecificationJVM.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ object ShrinkSpecificationJVM extends Properties("Shrink JVM") {
2525
property("non-empty list") = forAll { (l: List[Int]) =>
2626
(!l.isEmpty && l != List(0)) ==> {
2727
val ls = shrinkClosure(l)
28-
ls.toList.toString |: (ls.contains(Nil) && ls.contains(List(0)))
28+
(ls.contains(Nil) && ls.contains(List(0))).labelImpl2(ls.toList.toString)
2929
}
3030
}
3131

3232
property("xmap vector from list") = forAll { (v: Vector[Int]) =>
3333
(!v.isEmpty && v != Vector(0)) ==> {
3434
val vs = shrinkClosure(v)
35-
Vector(vs: _*).toString |: (vs.contains(Vector.empty) && vs.contains(Vector(0)))
35+
(vs.contains(Vector.empty) && vs.contains(Vector(0))).labelImpl2(Vector(vs: _*).toString)
3636
}
3737
}
3838

core/jvm/src/test/scala/org/scalacheck/TestSpecification.scala

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ object TestSpecification extends Properties("Test") {
5353
private def resultInvariant(f: (Test.Parameters, Test.Result) => Boolean): Prop =
5454
forAll { (prms: Test.Parameters, p: Prop) =>
5555
val r = Test.check(prms, p)
56-
s"${r.status}, s=${r.succeeded}, d=${r.discarded}, " +
57-
s"minSuccessful=${prms.minSuccessfulTests}, " +
58-
s"maxDiscardRatio=${prms.maxDiscardRatio}, " +
59-
s"actualDiscardRatio=${r.discarded.toFloat / r.succeeded}, " +
60-
s"workers=${prms.workers}" |: f(prms, r)
56+
f(prms, r).labelImpl2(
57+
s"${r.status}, s=${r.succeeded}, d=${r.discarded}, " +
58+
s"minSuccessful=${prms.minSuccessfulTests}, " +
59+
s"maxDiscardRatio=${prms.maxDiscardRatio}, " +
60+
s"actualDiscardRatio=${r.discarded.toFloat / r.succeeded}, " +
61+
s"workers=${prms.workers}"
62+
)
6163
}
6264

6365
property("stopCondition") = resultInvariant { (prms, r) =>
@@ -110,8 +112,8 @@ object TestSpecification extends Properties("Test") {
110112

111113
property("propGenException") = forAll { (prms: Test.Parameters) =>
112114
Test.check(prms, genException).status match {
113-
case x: PropException => true :| x.toString
114-
case x => false :| x.toString
115+
case x: PropException => true.labelImpl2(x.toString)
116+
case x => false.labelImpl2(x.toString)
115117
}
116118
}
117119

@@ -185,8 +187,8 @@ object TestSpecification extends Properties("Test") {
185187
val res = Test.check(prms, prop)
186188
val n = xs.size
187189
val unique = xs.toSet
188-
val p0 = Prop(unique(expected)) :| s"did not see $expected in $unique"
189-
val p1 = Prop(unique.size > 1) :| s"saw $n duplicate values: $unique"
190+
val p0 = Prop(unique(expected)).labelImpl2(s"did not see $expected in $unique")
191+
val p1 = Prop(unique.size > 1).labelImpl2(s"saw $n duplicate values: $unique")
190192
p0 && p1
191193
}
192194

@@ -209,8 +211,8 @@ object TestSpecification extends Properties("Test") {
209211
Test.check_(prms, prop)
210212
val n = xs.size
211213
val unique = xs.toSet
212-
val p0 = Prop(unique(expected)) :| s"did not see $expected in $unique"
213-
val p1 = Prop(unique.size > 1) :| s"saw $n duplicate values: $unique"
214+
val p0 = Prop(unique(expected)).labelImpl2(s"did not see $expected in $unique")
215+
val p1 = Prop(unique.size > 1).labelImpl2(s"saw $n duplicate values: $unique")
214216
p0 && p1
215217
}
216218
}

core/shared/src/main/scala/org/scalacheck/Prop.scala

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ sealed class PropFromFun(f: Gen.Parameters => Prop.Result) extends Prop {
2323
@Platform.EnableReflectiveInstantiation
2424
sealed abstract class Prop extends Serializable { self =>
2525

26-
import Prop.{Result, True, False, Undecided, provedToTrue, mergeRes}
26+
import Prop.{Exception, Proof, Result, True, False, Undecided, provedToTrue, mergeRes}
2727

2828
def apply(prms: Gen.Parameters): Result
2929

@@ -148,21 +148,43 @@ sealed abstract class Prop extends Serializable { self =>
148148

149149
override def toString = "Prop"
150150

151+
private[scalacheck] def labelImpl1(l: String) = map(_.label(l))
152+
153+
/** Put a label on the property to make test reports clearer */
154+
private[scalacheck] def label(l: String) = labelImpl1(l)
155+
156+
/** Put a label on the property to make test reports clearer */
157+
private[scalacheck] def :|(l: String) = labelImpl1(l)
158+
159+
/** Put a label on the property to make test reports clearer */
160+
private[scalacheck] def |:(l: String) = labelImpl1(l)
161+
162+
/** Put a label on the property to make test reports clearer */
163+
private[scalacheck] def :|(l: Symbol) = labelImpl1(l.name)
164+
151165
/** Put a label on the property to make test reports clearer */
152-
def label(l: String) = map(_.label(l))
166+
private[scalacheck] def |:(l: Symbol) = labelImpl1(l.name)
167+
168+
private[scalacheck] def labelImpl2(l: => String) =
169+
map(r => r.status match {
170+
case False | Exception(_) => r.label(l)
171+
case Proof | True | Undecided => r
172+
})
153173

154174
/** Put a label on the property to make test reports clearer */
155-
def :|(l: String) = label(l)
175+
def label(l: => String) = labelImpl2(l)
156176

157177
/** Put a label on the property to make test reports clearer */
158-
def |:(l: String) = label(l)
178+
def :|(l: => String) = labelImpl2(l)
159179

160180
/** Put a label on the property to make test reports clearer */
161-
def :|(l: Symbol) = label(l.name)
181+
def |:(l: => String) = labelImpl2(l)
162182

163183
/** Put a label on the property to make test reports clearer */
164-
def |:(l: Symbol) = label(l.name)
184+
def :|(l: => Symbol)(implicit d: DummyImplicit) = labelImpl2(l.name)
165185

186+
/** Put a label on the property to make test reports clearer */
187+
def |:(l: => Symbol)(implicit d: DummyImplicit) = labelImpl2(l.name)
166188
}
167189

168190
object Prop {
@@ -341,16 +363,16 @@ object Prop {
341363
def ==>(p: => Prop) = Prop(b) ==> p
342364

343365
/** See the documentation for [[org.scalacheck.Prop]] */
344-
def :|(l: String) = Prop(b) :| l
366+
def :|(l: String) = Prop(b).labelImpl1(l)
345367

346368
/** See the documentation for [[org.scalacheck.Prop]] */
347-
def |:(l: String) = l |: Prop(b)
369+
def |:(l: String) = Prop(b).labelImpl1(l)
348370

349371
/** See the documentation for [[org.scalacheck.Prop]] */
350-
def :|(l: Symbol) = Prop(b) :| l
372+
def :|(l: Symbol) = Prop(b).labelImpl1(l.name)
351373

352374
/** See the documentation for [[org.scalacheck.Prop]] */
353-
def |:(l: Symbol) = l |: Prop(b)
375+
def |:(l: Symbol) = Prop(b).labelImpl1(l.name)
354376
}
355377

356378
/** Implicit method that makes a number of property operators on values of type `Any` available in the current scope.
@@ -399,7 +421,7 @@ object Prop {
399421
*/
400422
def ?=[T](x: T, y: T)(implicit pp: T => Pretty): Prop =
401423
if (x == y) proved
402-
else falsified :| {
424+
else falsified.labelImpl2 {
403425
val exp = Pretty.pretty[T](y, Pretty.Params(0))
404426
val act = Pretty.pretty[T](x, Pretty.Params(0))
405427
"Expected " + exp + " but got " + act

core/shared/src/main/scala/org/scalacheck/commands/Commands.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,13 @@ trait Commands {
364364
try {
365365
val (p1, s, rs1) = runSeqCmds(sut, as.s, as.seqCmds)
366366
val l1 = s"Initial State:\n ${as.s}\nSequential Commands:\n${prettyCmdsRes(as.seqCmds zip rs1, maxLength)}"
367-
if (as.parCmds.isEmpty) p1 :| l1
367+
if (as.parCmds.isEmpty) p1.labelImpl2(l1)
368368
else propAnd(
369-
p1.flatMap { r => if (!r.success) finalize; Prop(_ => r) } :| l1, {
369+
p1.flatMap { r => if (!r.success) finalize; Prop(_ => r) }.labelImpl2(l1), {
370370
try {
371371
val (p2, rs2) = runParCmds(sut, s, as.parCmds)
372372
val l2 = rs2.map(prettyCmdsRes(_, maxLength)).mkString("\n\n")
373-
p2 :| l1 :| s"Parallel Commands (starting in state = ${s})\n$l2"
373+
p2.labelImpl2(l1).labelImpl2(s"Parallel Commands (starting in state = ${s})\n$l2")
374374
} finally finalize
375375
}
376376
)

core/shared/src/test/scala/org/scalacheck/PropSpecification.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ object PropSpecification extends Properties("Prop") {
218218
val params = Gen.Parameters.default.withInitialSeed(999L)
219219
val x = p(params).success
220220
val set = (1 to 10).map(_ => p(params).success).toSet
221-
Prop(set == Set(x)).label(s"$set == Set($x)")
221+
Prop(set == Set(x)).labelImpl2(s"$set == Set($x)")
222222
}
223223

224224
property("prop.useSeed is deterministic") =
@@ -227,7 +227,7 @@ object PropSpecification extends Properties("Prop") {
227227
val p = p0.useSeed(rng.Seed(n))
228228
val x = p(params).success
229229
val set = (1 to 10).map(_ => p(params).success).toSet
230-
Prop(set == Set(x)).label(s"$set == Set($x)")
230+
Prop(set == Set(x)).labelImpl2(s"$set == Set($x)")
231231
}
232232

233233
property("prop.useSeed is deterministic (pt. 2)") =
@@ -237,7 +237,7 @@ object PropSpecification extends Properties("Prop") {
237237
val p = p0.useSeed(rng.Seed(n))
238238
val r1 = p(params).success
239239
val r2 = p(params).success
240-
Prop(r1 == r2).label(s"$r1 == $r2")
240+
Prop(r1 == r2).labelImpl2(s"$r1 == $r2")
241241
}
242242

243243
property("disabling shrinking works") = {

core/shared/src/test/scala/org/scalacheck/StatsSpecification.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ object StatsSpecification extends Properties("Stats") {
8787

8888
case class Bounds(min: Double, max: Double) {
8989
def contains(x: Double): Prop =
90-
Prop(min <= x && x <= max) :| s"($min <= $x <= $max) was false"
90+
Prop(min <= x && x <= max).labelImpl2(s"($min <= $x <= $max) was false")
9191
}
9292

9393
implicit class MakeBounds(val n: Double) extends AnyVal {

0 commit comments

Comments
 (0)