scalacheck-shapeless icon indicating copy to clipboard operation
scalacheck-shapeless copied to clipboard

Generator fails on nested case classes with sealed traits

Open calippo opened this issue 8 years ago • 0 comments

I'm having trouble trying to generate samples from nested case classes containing sealed traits. When I try to generate an instance of a nested case class that contains sealed traits, the generator often fails, returning None.

Gen is supposed to fail when filtering is wrong. In my experiments Gen is failing even though I'm not applying any filter (also, it looks like gen.retryUntil(_ => true) is ignored and None is often returned).

I was not able to isolate the problem. Here is my experiment :)

package object models {
  case class C(e: String, f: OffsetDateTime, g: OffsetDateTime, c: Option[String], big: BigInt)
  case class B(e: String, f: OffsetDateTime, g: OffsetDateTime, outcome: Planet, outcome2: Planet, c: C)
  case class A(a: String, b: String, c: Option[String], d: B, outcome: Planet, outcome2: Planet)

  sealed abstract trait Planet
  object Planet {
    case object Earth extends Planet
    case object Sun extends Planet
    case object Moon extends Planet
  }
}

object Test extends App {
  import org.scalacheck.Shapeless._
  import org.scalacheck._
  import models._

  implicit val offsetDateTimeArbitrary: Arbitrary[OffsetDateTime] = Arbitrary(Gen.const(OffsetDateTime.now()))
  val gen = implicitly[Arbitrary[A]].arbitrary

  val n = 1000
  println(Seq.fill(n)(gen.retryUntil(_ => true).sample).flatten.length == n)
}

note: I'm using scala 2.11.7 and "scalacheck-shapeless_1.13" % "1.1.0-RC1".

calippo avatar May 27 '16 08:05 calippo