Skip to content

Instantly share code, notes, and snippets.

@leigh-perry
Created May 2, 2020 01:37
Show Gist options
  • Select an option

  • Save leigh-perry/beef42fc8bc25a9ba78b6ff090f7d2e6 to your computer and use it in GitHub Desktop.

Select an option

Save leigh-perry/beef42fc8bc25a9ba78b6ff090f7d2e6 to your computer and use it in GitHub Desktop.
import zio.random.Random
import zio.test.Gen.alphaNumericChar
import zio.test.{ Gen, Sized }
object Generators {
val anyIntX: Gen[Random, Int] =
Gen.oneOf(
Gen.oneOf(
Gen.const(0),
Gen.const(1),
Gen.const(-1),
Gen.const(Int.MaxValue),
Gen.const(Int.MaxValue - 1),
Gen.const(Int.MinValue),
Gen.const(Int.MinValue + 1)
),
Gen.anyInt
)
val anyLongX: Gen[Random, Long] =
Gen.oneOf(
Gen.oneOf(
Gen.const(0L),
Gen.const(1L),
Gen.const(-1L),
Gen.const(Long.MaxValue),
Gen.const(Long.MaxValue - 1L),
Gen.const(Long.MinValue),
Gen.const(Long.MinValue + 1L)
),
Gen.anyLong
)
def intX(min: Int, max: Int): Gen[Random, Int] =
Gen.oneOf(
Gen.oneOf(
Gen.const(min),
Gen.const(Math.min(min + 1, max)),
Gen.const(max),
Gen.const(Math.max(min, max - 1))
),
Gen.int(min, max)
)
def longX(min: Long, max: Long): Gen[Random, Long] =
Gen.oneOf(
Gen.oneOf(
Gen.const(min),
Gen.const(Math.min(min + 1L, max)),
Gen.const(max),
Gen.const(Math.max(min, max - 1L))
),
Gen.long(min, max)
)
////
def stringN(min: Int, max: Int): Gen[Random with Sized, String] =
for {
n <- intX(min, max)
s <- Gen.stringN(n)(alphaNumericChar)
} yield s
def stringTuple2(min: Int, max: Int): Gen[Random with Sized, (String, String)] =
for {
s1 <- stringN(min, max)
s2 <- stringN(min, max)
} yield (s1, s2)
def listN[A](min: Int, max: Int)(gen: Gen[Random with Sized, A]): Gen[Random with Sized, List[A]] =
for {
n <- intX(min, max)
l <- Gen.listOfN(n)(gen)
} yield l
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment