This repository has been archived by the owner. It is now read-only.

Automatically derive instances of scalacheck's Shrink #16

Merged
merged 1 commit into from May 16, 2014

Conversation

2 participants
@stew
Member

stew commented May 15, 2014

Add a TypeClass instance for org.scalacheck.Shrink. Shrink is a useful feature of scalacheck whereby upon finding a failing example, scalacheck will shrink the example down to a "smaller" example.

Add a TypeClass instance for org.scalacheck.Shrink. Shrink is a usefu…
…l feature

of scalacheck whereby upon finding a failing example, scalacheck will shrink
the example down to a "smaller" example.
val emptyShrinkTest = ShrinkTest("","")
property("derived shrink") = forAll {(shrinkMe: ShrinkTest) =>
shrinkMe == emptyShrinkTest || shrinkClosure(shrinkMe).contains(emptyShrinkTest)

This comment has been minimized.

@larsrh

larsrh May 16, 2014

Member

Could you explain what this test does?

@larsrh

larsrh May 16, 2014

Member

Could you explain what this test does?

This comment has been minimized.

@stew

stew May 16, 2014

Member

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.

@stew

stew May 16, 2014

Member

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.

This comment has been minimized.

@larsrh

larsrh May 16, 2014

Member

Okay, I can follow that :-)

@larsrh

larsrh May 16, 2014

Member

Okay, I can follow that :-)

@larsrh

This comment has been minimized.

Show comment
Hide comment
@larsrh

larsrh May 16, 2014

Member

Nice, thanks!

Member

larsrh commented May 16, 2014

Nice, thanks!

larsrh added a commit that referenced this pull request May 16, 2014

Merge pull request #16 from stew/master
Automatically derive instances of scalacheck's Shrink

@larsrh larsrh merged commit 792c609 into typelevel:master May 16, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.