kitlangton / neotype

A friendly newtype library for Scala 3
Apache License 2.0
158 stars 16 forks source link

Runtime failure for zio-test generator #94

Open loathwine opened 1 month ago

loathwine commented 1 month ago

One of my generators failed at runtime and it seems to depends on the order of the involved generators.

import zio.ZIO
import zio.test.*
import zio.test.magnolia.DeriveGen
import neotype.Newtype
import neotype.interop.ziotest.given

object Main extends ZIOSpecDefault {
  type CoolString = CoolString.Type
  object CoolString extends Newtype[String]

  // 1. Defining generator here fails at runtime.
  val genCoolString: Gen[Any, CoolString] = DeriveGen[CoolString]

  val genString                           = Gen.alphaNumericStringBounded(0, 5).map("Ayyy! " + _)
  // 2. This one works at runtime!?
  // val genCoolString: Gen[Any, CoolString] = DeriveGen[CoolString]
  given DeriveGen[String] = DeriveGen.instance(genString)

  // 3. Defining generator here succeeds at runtime.
  // val genCoolString: Gen[Any, CoolString] = DeriveGen[CoolString]

  val spec = suite("Neotype")(
    test("generate cool string")(
      check(genCoolString) (cs => 
        ZIO.debug(cs).as(assertCompletes)
      )
    )
  )
}

scastie

loathwine commented 1 month ago

Found a workaround that is simpler. By using lazy val instead of val the order doesn't matter.

  lazy val genCoolString: Gen[Any, CoolString] = DeriveGen[CoolString]

  val genString = Gen.alphaNumericStringBounded(0, 5).map("Ayyy! " + _)
  given DeriveGen[String] = DeriveGen.instance(genString)