Easiest way to access ZIO test generators from Scala REPL?

Viewed 173

I'm just starting out using ZIO in Scala. I've written some Scalacheck-style tests using ZIO's Gen type, and they appear to work, but I'd like to manually test the generators in the REPL to ensure that they're actually producing the data I expect them to.

The problem: everything in ZIO is wrapped in the ZIO monad, and I need to pass the right data into this monad to unwrap it and view the results. And there's no documentation explaining how to do this in the REPL.

I think I understand how to do it for a basic program with no environment dependencies: call zio.Runtime.default.unsafeRun. But the Gen objects expect an environment of type Random with Sized, and I don't know how to produce an instance of this.

Given a Gen[Random with Sized, T], what is the quickest way to execute it on the REPL and get a List[T] of generated values?

1 Answers

I think I've found a partial solution, but I'm not completely satisfied with it.

For just printing some samples from a Gen on the REPL, this works:

zio.Runtime.default.unsafeRun(
  yourGenerator
    .runCollectN(50)
    .provideLayer(zio.random.Random.live +!+ zio.test.Sized.live(100))
) foreach println

But I don't think this is how it's supposed to be done. provideLayer doesn't typecheck unless I provide both Random and Sized, even though Random should be part of zio.Runtime.default. I think there's something about ZLayer that I still don't understand.

Related