Skip to content

Commit

Permalink
Fix issue #2369 with Arb.localDate (#2370)
Browse files Browse the repository at this point in the history
Arb.localDate doesn’t take into account the date/month portion of the
specified minDatex. So when you specify minDate that is after the
default edge case date, it will (intermittently) fail. Here’s an example
Arb.localDate(minDate = LocalDate.now().plusDays(10)) will generate a
2021-02-28 date.
  • Loading branch information
unaryops authored and sksamuel committed Aug 24, 2021
1 parent f9f7bf6 commit 505a1c8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ fun Arb.Companion.localDate(
maxDate: LocalDate = LocalDate.of(2030, 12, 31)
): Arb<LocalDate> {

val yearRange = (minDate.year..maxDate.year)
val feb28Date = LocalDate.of(yearRange.random(), 2, 28)
val feb28DateThisYear = LocalDate.of(LocalDate.now().year, 2, 28)
val minDateYear = if (minDate.isBefore(feb28DateThisYear)) minDate.year else minDate.year + 1
val yearRange = (minDateYear..maxDate.year)

val feb28Date = LocalDate.of(yearRange.random(), 2, 28)
val feb29Year = yearRange.firstOrNull { Year.of(it).isLeap }
val feb29Date = feb29Year?.let { LocalDate.of(it, 2, 29) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sksamuel.kotest.property.arbitrary
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.collections.shouldContainAll
import io.kotest.matchers.date.shouldBeAfter
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.RandomSource
Expand Down Expand Up @@ -41,6 +42,12 @@ class DateTest : WordSpec({
days shouldBe (1..31).toSet()
}

"generate LocalDates after the minYear" {
checkAll(100_000, Arb.localDate(LocalDate.of(2021, 7, 17))) {
it shouldBeAfter LocalDate.of(2021, 7, 16)
}
}

"Contain Feb 29th if leap year" {
val leapYear = 2016
Arb.localDate(LocalDate.of(leapYear, 1, 1), LocalDate.of(leapYear, 12, 31)).edgecases().toList() shouldContain LocalDate.of(2016, 2, 29)
Expand Down

0 comments on commit 505a1c8

Please sign in to comment.