in ratatool-scalacheck/src/main/scala/com/spotify/ratatool/scalacheck/GenTestUtils.scala [36:77]
def withGen[A, T](genA: Gen[A])(
fn: A => T
)(implicit line: sourcecode.Line, name: sourcecode.FileName): Option[T] =
withGen(genA, Seed.random())(fn)(line, name)
/**
* Generate an instance of `A` from `genA` using a the seed specified by `base64Seed`.
*
* @return
* The result of `fn` applied to the generated `A`, or `None` on generation failure.
*/
def withGen[A, T](genA: Gen[A], base64Seed: String)(
fn: A => T
)(implicit line: sourcecode.Line, name: sourcecode.FileName): Option[T] =
withGen(genA, Seed.fromBase64(base64Seed).get)(fn)(line, name)
/**
* Generate an instance of `A` from `genA` using a the seed specified by `seed`.
*
* @return
* The result of `fn` applied to the generated `A`, or `None` on generation failure.
*/
def withGen[A, T](genA: Gen[A], seed: Seed)(
fn: A => T
)(implicit line: sourcecode.Line, name: sourcecode.FileName): Option[T] = {
genA.apply(Gen.Parameters.default, seed) match {
case None =>
logger.error(
s"Failed to generate a valid value at ${name.value}:${line.value}. " +
s"This can occur when Gen instances are are not guaranteed to produce a value, " +
s"e.g. when candidate values are restricted with filter() or suchThat(). " +
s"Seed: ${seed.toBase64}"
)
None
case Some(a) =>
val r = Try(fn(a)).map(Some(_))
if (r.isFailure) {
logger.error(s"Failure at ${name.value}:${line.value}. Seed: ${seed.toBase64}")
}
r.get
}
}