Is there a way to generate random dates for property tests using Scalacheck . I want to generate both future and past dates . But the existing Scalacheck.Gen class does not provide any predefined method to do so .
3
votes
3 Answers
6
votes
The following will generate what you are looking for
implicit val localDateArb = Arbitrary(localDateGen)
def localDateGen: Gen[LocalDate] = {
val rangeStart = LocalDate.MIN.toEpochDay
val currentYear = LocalDate.now(UTC).getYear
val rangeEnd = LocalDate.of(currentYear, 1, 1).toEpochDay
Gen.choose(rangeStart, rangeEnd).map(i => LocalDate.ofEpochDay(i))
}
2
votes