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 .
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))
}
For joda time, I have used like that:
lazy val localDateGen: Gen[LocalDate] = Gen.calendar map LocalDate.fromCalendarFields
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With