lundi 22 mars 2021

scalacheck: lift custom random generator

Is it possible to lift custom generating function into Gen?

For example, generating ObjectIds for mongo.

import org.mongodb.scala.bson.ObjectId
import org.scalacheck.Gen

val genObjectId: Gen[ObjectId] = Gen.lift(() => new ObjectId)

The only possible solution I've found is to hack the generator like:

val genObjectId: Gen[ObjectId] = Gen.numChar.map(_ => new ObjectId)

Generating ObjectIds using Gen.hexChar is irrelevant because:

  • I need unique value each time
  • Mongo could treat some of the generated hex strings as invalid

Aucun commentaire:

Enregistrer un commentaire