Skip to content

Commit 8689ae7

Browse files
authored
Merge pull request #910 from AurelienRichez/fix/fix-909
fix wrong match in shrinker (fixes #909)
2 parents 4867326 + 4df1e67 commit 8689ae7

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ object Prop {
773773
/*
774774
* Returns the first failed result in Left or success in Right.
775775
*/
776-
def getFirstFailure(xs: Stream[T], exceptionFilter: Option[Class[_]]): Either[(T,Result),(T,Result)] = {
776+
def getFirstFailure(xs: Stream[T], exceptionFilter: Option[Class[_ <: Throwable]]): Either[(T,Result),(T,Result)] = {
777777
assert(!xs.isEmpty, "Stream cannot be empty")
778778
val results = xs.map(x => (x, result(x)))
779779
results.dropWhile {
@@ -788,7 +788,7 @@ object Prop {
788788
def shrinker(x: T, r: Result, shrinks: Int, orig: T): Result = {
789789
val xs = shrink(x)
790790
val res = r.addArg(Arg(labels,x,shrinks,orig,pp(x),pp(orig)))
791-
val originalException = Some(r.status).collect { case NonFatal(e) => e.getClass() }
791+
val originalException = Some(r.status).collect { case Prop.Exception(e) => e.getClass }
792792
if(xs.isEmpty) res else getFirstFailure(xs, originalException) match {
793793
case Right((x2,r2)) => res
794794
case Left((x2,r2)) => shrinker(x2, replOrig(r,r2), shrinks+1, orig)

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,30 @@ object PropSpecification extends Properties("Prop") {
269269
})
270270
}
271271

272+
property("shrinking process always take the first value returned by the Shrink[T]") = {
273+
// this shrinker split a string in 2
274+
implicit val stringShrink: Shrink[String] = Shrink[String] {
275+
def split(s: String): Stream[String] = {
276+
if (s.length == 1) Stream.empty
277+
else {
278+
s.take(s.length / 2) #:: s.drop(s.length / 2) #:: Stream.empty
279+
}
280+
}
281+
split
282+
}
283+
284+
// shrinked value will be "1234", then shrinked to "12" because it also fails, and finally "1"
285+
val prop = forAll(Gen.const("12345678")) { (a: String) =>
286+
throw new RuntimeException("expected exception")
287+
}
288+
289+
val result = prop(Gen.Parameters.default)
290+
Prop(result.status match {
291+
case Exception(_) => result.args.head.arg == "1"
292+
case _ => false
293+
})
294+
}
295+
272296
// make sure the two forAlls are seeing independent values
273297
property("regression #530: failure to slide seed") =
274298
forAll((x: Int) => (x >= 0) ==> true) &&

0 commit comments

Comments
 (0)