typelevel / typelevel/scalacheck
Gen.suchThat not respected by shrinking after Gen.map
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 2k
- Forks
- 393
- Avg merge
- 6h 42m
- Merged PRs (30d)
- 4
Description
My team has a library which contains a bunch of predefined generators. Here's a simple one:
lazy val genNonEmptyAlphaStr: Gen[String] = Gen.nonEmptyListOf(Gen.alphaChar).map(_.mkString)
Unfortunately when I go to use it in practice, this doesn't get respected at all. (We use specs2 along with ScalaCheck, so bear with me.)
import org.scalacheck.Prop._
import org.specs2.{ScalaCheck, SpecificationLike}
import com.paypal.cascade.common.tests.scalacheck._
class ShrinkTestSpecs
extends SpecificationLike
with ScalaCheck { def is = s2"""
lalal ${test}
"""
def fail = throw new RuntimeException("aaa!")
def test = {
forAll(genNonEmptyAlphaStr) { s =>
fail
true must beTrue
}
}
}
This results in some output like
Testing started at 1:37 PM ...
A counter-example is '': java.lang.RuntimeException: aaa! (after 0 try)
java.lang.RuntimeException: aaa!
If I use forAllNoShrink, or if I write genNonEmptyAlphaStr.suchThat(_.size > 0), then there are no problems and I get single-letter strings. If I use Gen.nonEmptyListOf(Gen.alphaChar) (in other words, I don't map on it,) then I also have no problems. It's only after I .map(_.mkString) that it will shrink to the empty string.
tl;dr Gen.map invalidates suchThat.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the shrinking behavior with Gen.nonEmptyListOf(Gen.alphaChar).map(_.mkString), comparing it with the unmapped generator and with suchThat. Inspect the Gen.map, suchThat, and shrinking behavior in ScalaCheck; done means mapped generators no longer shrink to values that violate the original constraint, while the existing examples continue to pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100