Skip to content

Commit 505a1c8

Browse files
unaryopssksamuel
authored andcommitted
Fix issue kotest#2369 with Arb.localDate (kotest#2370)
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.
1 parent f9f7bf6 commit 505a1c8

File tree

2 files changed

+11
-2
lines changed
  • kotest-property/src
    • jvmMain/kotlin/io/kotest/property/arbitrary
    • jvmTest/kotlin/com/sksamuel/kotest/property/arbitrary

2 files changed

+11
-2
lines changed

kotest-property/src/jvmMain/kotlin/io/kotest/property/arbitrary/dates.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ fun Arb.Companion.localDate(
4848
maxDate: LocalDate = LocalDate.of(2030, 12, 31)
4949
): Arb<LocalDate> {
5050

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

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

kotest-property/src/jvmTest/kotlin/com/sksamuel/kotest/property/arbitrary/DateTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.sksamuel.kotest.property.arbitrary
33
import io.kotest.core.spec.style.WordSpec
44
import io.kotest.matchers.collections.shouldContain
55
import io.kotest.matchers.collections.shouldContainAll
6+
import io.kotest.matchers.date.shouldBeAfter
67
import io.kotest.matchers.shouldBe
78
import io.kotest.property.Arb
89
import io.kotest.property.RandomSource
@@ -41,6 +42,12 @@ class DateTest : WordSpec({
4142
days shouldBe (1..31).toSet()
4243
}
4344

45+
"generate LocalDates after the minYear" {
46+
checkAll(100_000, Arb.localDate(LocalDate.of(2021, 7, 17))) {
47+
it shouldBeAfter LocalDate.of(2021, 7, 16)
48+
}
49+
}
50+
4451
"Contain Feb 29th if leap year" {
4552
val leapYear = 2016
4653
Arb.localDate(LocalDate.of(leapYear, 1, 1), LocalDate.of(leapYear, 12, 31)).edgecases().toList() shouldContain LocalDate.of(2016, 2, 29)

0 commit comments

Comments
 (0)