Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gen.nonEmptyListOf...map generates empty list #372

Closed
AntonIvanov87 opened this issue Nov 22, 2017 · 1 comment
Closed

Gen.nonEmptyListOf...map generates empty list #372

AntonIvanov87 opened this issue Nov 22, 2017 · 1 comment

Comments

@AntonIvanov87
Copy link

import org.scalacheck.Gen
import org.scalacheck.Prop.forAll

object Main extends App {

  val charGen = Gen.oneOf('A', 'B')
  val charsGen = Gen.nonEmptyListOf(charGen)
  val stringGen = charsGen.map(_.mkString)

  forAll(stringGen) { (string: String) =>
    if (string.isEmpty) {
      throw new IllegalArgumentException("empty string!")
    }
    assert(false, "should see this message")
    true
  }.check

}

charsGen should generate non empty list of chars, that is why IllegalArgumenException("empty string!") should be never thown.
Unfortunately if an iteration fails, you see:

! Exception raised on property evaluation.
> ARG_0: ""
> ARG_0_ORIGINAL: "A"
> Exception: java.lang.IllegalArgumentException: empty string!

Empty string is very confusing, because it hides the real cause and contradicts with generator definition (nonEmptyListOf).

If you substitute stringGen with charsGen the output is correct:

! Exception raised on property evaluation.
> ARG_0: List(" ")
> ARG_0_ORIGINAL: List("B")
> Exception: java.lang.AssertionError: assertion failed: should see this me
  ssage

It looks like any map operation on a generator of lists causes this problem.

@ashawley
Copy link
Contributor

You'd need to use forAllNoShrink.

That constraints in generators aren't respected in shrinking is known issue, see #129.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants