Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate time using scalacheck generators?

Tags:

scalacheck

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 .

like image 801
trupti rath Avatar asked Jan 06 '23 20:01

trupti rath


2 Answers

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))
}
like image 189
Aravind Yarram Avatar answered Jan 08 '23 09:01

Aravind Yarram


For joda time, I have used like that:

lazy val localDateGen: Gen[LocalDate] = Gen.calendar map LocalDate.fromCalendarFields
like image 28
Дмитрий Завориин Avatar answered Jan 08 '23 11:01

Дмитрий Завориин