Skip to content

Commit

Permalink
Fixing Arb.localDate when minDate == maxDate (kotest#3860)
Browse files Browse the repository at this point in the history
Now simply returns a constant Arb for the given date.

Fixes kotest#3829

<!-- 
If this PR updates documentation, please update all relevant versions of
the docs, see:
https://github.com/kotest/kotest/tree/master/documentation/versioned_docs
The documentation at
https://github.com/kotest/kotest/tree/master/documentation/docs is the
documentation for the next minor or major version _TO BE RELEASED_
-->
  • Loading branch information
Kantis authored Feb 4, 2024
1 parent 42ff081 commit eac08ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,19 @@ fun Arb.Companion.localDate() = Arb.Companion.localDate(LocalDate.of(1970, 1, 1)
fun Arb.Companion.localDate(
minDate: LocalDate = LocalDate.of(1970, 1, 1),
maxDate: LocalDate = LocalDate.of(2030, 12, 31)
): Arb<LocalDate> {
): Arb<LocalDate> = when {
minDate > maxDate -> throw IllegalArgumentException("minDate must be before maxDate")
minDate == maxDate -> Arb.constant(minDate)
else -> {
val leapYears = (minDate.year..maxDate.year).filter { isLeap(it.toLong()) }

val leapYears = (minDate.year..maxDate.year).filter { isLeap(it.toLong()) }
val february28s = leapYears.map { LocalDate.of(it, 2, 28) }
val february29s = february28s.map { it.plusDays(1) }

val february28s = leapYears.map { LocalDate.of(it, 2, 28) }
val february29s = february28s.map { it.plusDays(1) }

return arbitrary(february28s + february29s + minDate + maxDate) {
minDate.plusDays(it.random.nextLong(ChronoUnit.DAYS.between(minDate, maxDate)))
}.filter { it in minDate..maxDate }
arbitrary(february28s + february29s + minDate + maxDate) {
minDate.plusDays(it.random.nextLong(ChronoUnit.DAYS.between(minDate, maxDate)))
}.filter { it in minDate..maxDate }
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sksamuel.kotest.property.arbitrary

import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.WordSpec
import io.kotest.inspectors.forAll
import io.kotest.matchers.collections.shouldContain
Expand Down Expand Up @@ -82,6 +83,20 @@ class DateTest : WordSpec({
}
}

"Arb.localDate(minDate, maxDate)" should {
"Work when min date == max date" {
val date = of(2021, 1, 1)
Arb.localDate(date, date).take(10).toList() shouldBe List(10) { date }
}

"Throw when min date > max date" {
shouldThrow<IllegalArgumentException> {
val minDate = of(2021, 1, 1)
Arb.localDate(minDate, minDate.minusDays(1))
}.message shouldBe "minDate must be before maxDate"
}
}

"Arb.localTime()" should {
"generate N valid LocalTimes(no exceptions)" {
Arb.localTime().generate(RandomSource.default()).take(10_000).toList()
Expand Down

0 comments on commit eac08ab

Please sign in to comment.