sageserpent-open / americium

Generation of test case data for Scala and Java, in the spirit of QuickCheck. When your test fails, it gives you a minimised failing test case and a way of reproducing the failure immediately.
MIT License
15 stars 1 forks source link

Add `withFilter` to the Scala API for `Trials` #62

Closed sageserpent-open closed 1 year ago

sageserpent-open commented 1 year ago

Why? So that filtration in a for-comprehension can be supported.

Example:

  val testCases: java.Trials[TestCase] = (for
    core <- coreValues.several[Vector[Int]].filter(2 < _.size)

    coreSize = core.size

    interleaveForBase <- sizes(coreSize).flatMap(
      additionalValues.lotsOfSize[Vector[Int]]
    )
    base <- trialsApi.pickAlternatelyFrom(
      shrinkToRoundRobin = true,
      core,
      interleaveForBase
    )
    interleaveForLeft <- sizes(coreSize).flatMap(
      additionalValues.lotsOfSize[Vector[Int]]
    )
    left <- trialsApi.pickAlternatelyFrom(
      shrinkToRoundRobin = true,
      core,
      interleaveForLeft
    )
    interleaveForRight <- sizes(coreSize).flatMap(
      additionalValues.lotsOfSize[Vector[Int]]
    )
    right <- trialsApi.pickAlternatelyFrom(
      shrinkToRoundRobin = true,
      core,
      interleaveForRight
    )
    // **** FILTRATION ****
    if core != base || core != left || core != right
  yield TestCase(core, base, left, right))

This doesn't have to be fancy - a delegation to .filter will do as Trials is not a collection type; it evaluates filtration lazily.

sageserpent-open commented 1 year ago

This went out in release 1.13.2, Git commit SHA: c05d554c979f4a5d29ceaf46e6cf2e2021ee08d5.