Skip to content
This repository has been archived by the owner on Sep 20, 2018. It is now read-only.

Automatically derive instances of scalacheck's Shrink #16

Merged
merged 1 commit into from May 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion scalacheck/main/scala/package.scala
Expand Up @@ -2,7 +2,7 @@ package shapeless.contrib

import shapeless._

import org.scalacheck.{Gen, Arbitrary}
import org.scalacheck.{Gen, Arbitrary, Shrink}

package object scalacheck {

Expand Down Expand Up @@ -40,7 +40,34 @@ package object scalacheck {

}

implicit def ShrinkI: TypeClass[Shrink] = new TypeClass[Shrink] {

def emptyProduct = Shrink(_ => Stream.empty)

def product[F, T <: HList](f: Shrink[F], t: Shrink[T]) = Shrink { case a :: b ⇒
f.shrink(a).map( _ :: b) append
t.shrink(b).map(a :: _)
}

def project[A, B](b: => Shrink[B], ab: A => B, ba: B => A) = Shrink { a =>
b.shrink(ab(a)).map(ba)
}

def coproduct[L, R <: Coproduct](sl: => Shrink[L], sr: => Shrink[R]) = Shrink { lr =>
lr match {
case Inl(l) ⇒ sl.shrink(l).map(Inl.apply)
case Inr(r) ⇒ sr.shrink(r).map(Inr.apply)
}
}

def emptyCoproduct: Shrink[CNil] = Shrink(_ => Stream.empty)

}

implicit def deriveArbitrary[T](implicit ev: TypeClass[Arbitrary]): Arbitrary[T] =
macro GenericMacros.deriveInstance[Arbitrary, T]


implicit def deriveShrink[T](implicit ev: TypeClass[Shrink]): Shrink[T] =
macro GenericMacros.deriveInstance[Shrink, T]
}
24 changes: 24 additions & 0 deletions scalacheck/test/scala/ShrinkSpec.scala
@@ -0,0 +1,24 @@
package shapeless.contrib.scalacheck

import org.scalacheck.{Arbitrary,Gen,Properties,Shrink,Test}
import org.scalacheck.Prop.forAll
import shapeless._
import shapeless.ops.coproduct._

object ShrinkSpec extends Properties("Shrink") {

case class ShrinkTest(one: String,
two: String)

private def shrinkClosure[T : Shrink](x: T): Stream[T] = {
val xs = Shrink.shrink[T](x)
if(xs.isEmpty) xs
else xs.append(xs.take(1).map(shrinkClosure[T]).flatten)
}

val emptyShrinkTest = ShrinkTest("","")

property("derived shrink") = forAll {(shrinkMe: ShrinkTest) =>
shrinkMe == emptyShrinkTest || shrinkClosure(shrinkMe).contains(emptyShrinkTest)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain what this test does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forAll(a: A) takes Shrink[A] instance as an implicit parameter, Shrink[A] is essentially a A => Stream[A], where the stream is a finite list of "smaller" examples derived from the failing example which may also fail. If forAll finds an example of an A which causes a test failure, it will use the Shrink instance to generate a stream of more possible test cases, and test those.

For example, if it finds that "asdf" cases a test failure, it will generate a Stream("sdf",'adf", "asf" , "asd") and if any of those fail, it will recurse on the failed case.

There is a default Shrink instance for Any which will just genrate an empty stream.

This test ensures that a Shrink instance other than this default was found for our Product type. It ensures that this shrink instance works by eventually finding the smallest possible case (the product of two empty strings), and ensures the the streams generated don't contain the seed element, which would cause an infinite stream to be generated.

I had originally written a more complex product involving a List that we could expect to be shrunk to Nil, but this was combinatorially too complex, and made the test take 90 seconds to successfully complete.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I can follow that :-)

}
}