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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :-)