Skip to content

Commit

Permalink
Added java.time.Year arbitrary support (kotest#3842)
Browse files Browse the repository at this point in the history
<!-- 
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_
-->

resolve kotest#3838
  • Loading branch information
kshired authored Jan 22, 2024
1 parent 87ef4a8 commit 15413a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kotest-property/api/kotest-property.api
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,8 @@ public final class io/kotest/property/arbitrary/DatesKt {
public static final fun period (Lio/kotest/property/Arb$Companion;I)Lio/kotest/property/Arb;
public static synthetic fun period$default (Lio/kotest/property/Arb$Companion;IILjava/lang/Object;)Lio/kotest/property/Arb;
public static final fun random (Lkotlin/ranges/ClosedRange;Lkotlin/random/Random;)Ljava/time/Instant;
public static final fun year (Lio/kotest/property/Arb$Companion;Ljava/time/Year;Ljava/time/Year;)Lio/kotest/property/Arb;
public static synthetic fun year$default (Lio/kotest/property/Arb$Companion;Ljava/time/Year;Ljava/time/Year;ILjava/lang/Object;)Lio/kotest/property/Arb;
public static final fun yearMonth (Lio/kotest/property/Arb$Companion;Ljava/time/YearMonth;Ljava/time/YearMonth;)Lio/kotest/property/Arb;
public static synthetic fun yearMonth$default (Lio/kotest/property/Arb$Companion;Ljava/time/YearMonth;Ljava/time/YearMonth;ILjava/lang/Object;)Lio/kotest/property/Arb;
public static final fun zonedDateTime (Lio/kotest/property/Arb$Companion;Ljava/time/LocalDateTime;Ljava/time/LocalDateTime;Lio/kotest/property/Arb;)Lio/kotest/property/Arb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.Period
import java.time.Year
import java.time.Year.isLeap
import java.time.YearMonth
import java.time.ZoneId
Expand All @@ -18,6 +19,7 @@ import java.time.temporal.TemporalQueries.localDate
import java.time.temporal.TemporalQueries.localTime
import java.util.*
import kotlin.random.Random
import kotlin.random.nextInt

/**
* Arberates a random [Period]s.
Expand Down Expand Up @@ -169,6 +171,20 @@ fun Arb.Companion.localDateTime(
)
}

/**
* Arberates a stream of random Year
*
* This generator creates randomly generated Year, in the range [[minYear, maxYear]].
*/
fun Arb.Companion.year(
minYear: Year = Year.of(1970),
maxYear: Year = Year.of(2030)
): Arb<Year> {
return arbitrary(listOf(minYear, maxYear)) {
Year.of(it.random.nextInt(minYear.value..maxYear.value))
}
}

/**
* Arberates a stream of random YearMonth
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import io.kotest.property.arbitrary.of
import io.kotest.property.arbitrary.offsetDateTime
import io.kotest.property.arbitrary.period
import io.kotest.property.arbitrary.take
import io.kotest.property.arbitrary.year
import io.kotest.property.arbitrary.yearMonth
import io.kotest.property.arbitrary.zonedDateTime
import io.kotest.property.checkAll
Expand All @@ -30,6 +31,7 @@ import java.time.LocalDate.of
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.Period
import java.time.Year
import java.time.YearMonth
import java.time.ZoneId
import java.time.ZoneOffset
Expand Down Expand Up @@ -229,6 +231,24 @@ class DateTest : WordSpec({
}
}

"Arb.year(minYear, maxYear)" should {
"generate valid years (no exceptions)" {
shouldNotThrowAny {
Arb.year().take(10_000).toList()
}
}

"generate years between minYear and maxYear" {
val years = mutableSetOf<Year>()

checkAll(10_000, Arb.year(Year.of(1998), Year.of(1999))) {
years += it
}

years shouldBe setOf(Year.of(1998), Year.of(1999))
}
}

"Arb.yearMonth(minYearMonth, maxYearMonth)" should {
"generate valid YearMonths (no exceptions)" {
shouldNotThrowAny {
Expand Down

0 comments on commit 15413a7

Please sign in to comment.