Skip to content

Commit

Permalink
add Arb<Date> support
Browse files Browse the repository at this point in the history
  • Loading branch information
vladeekkk committed Jun 4, 2023
1 parent 9d31ad7 commit 093c898
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
4 changes: 4 additions & 0 deletions kotest-property/api/kotest-property.api
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,10 @@ public final class io/kotest/property/arbitrary/DatesKt {
public static final fun instant (Lio/kotest/property/Arb$Companion;Ljava/time/Instant;Ljava/time/Instant;)Lio/kotest/property/Arb;
public static final fun instant (Lio/kotest/property/Arb$Companion;Lkotlin/ranges/ClosedRange;)Lio/kotest/property/Arb;
public static synthetic fun instant$default (Lio/kotest/property/Arb$Companion;Ljava/time/Instant;Ljava/time/Instant;ILjava/lang/Object;)Lio/kotest/property/Arb;
public static final fun javaDate (Lio/kotest/property/Arb$Companion;Ljava/lang/String;Ljava/lang/String;Lio/kotest/property/Arb;)Lio/kotest/property/Arb;
public static final fun javaDate (Lio/kotest/property/Arb$Companion;Ljava/util/Date;Ljava/util/Date;Lio/kotest/property/Arb;)Lio/kotest/property/Arb;
public static synthetic fun javaDate$default (Lio/kotest/property/Arb$Companion;Ljava/lang/String;Ljava/lang/String;Lio/kotest/property/Arb;ILjava/lang/Object;)Lio/kotest/property/Arb;
public static synthetic fun javaDate$default (Lio/kotest/property/Arb$Companion;Ljava/util/Date;Ljava/util/Date;Lio/kotest/property/Arb;ILjava/lang/Object;)Lio/kotest/property/Arb;
public static final fun localDate (Lio/kotest/property/Arb$Companion;)Lio/kotest/property/Arb;
public static final fun localDate (Lio/kotest/property/Arb$Companion;Ljava/time/LocalDate;Ljava/time/LocalDate;)Lio/kotest/property/Arb;
public static synthetic fun localDate$default (Lio/kotest/property/Arb$Companion;Ljava/time/LocalDate;Ljava/time/LocalDate;ILjava/lang/Object;)Lio/kotest/property/Arb;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kotest.property.arbitrary

import io.kotest.property.Arb
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
Expand All @@ -15,6 +16,7 @@ import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit
import java.time.temporal.TemporalQueries.localDate
import java.time.temporal.TemporalQueries.localTime
import java.util.Date
import kotlin.random.Random

/**
Expand Down Expand Up @@ -247,3 +249,30 @@ fun Arb.Companion.zonedDateTime(
localDateTime(minValue, maxValue),
zoneId
) { time, zone -> time.atZone(zone) }

/**
* Arberates a stream of random [Date]
*/
fun Arb.Companion.javaDate(
minDate: String = "1970-01-01",
maxDate: String = "2050-12-31",
zoneId: Arb<ZoneId> = zoneId()
): Arb<Date> {
return Arb.bind(
localDate(LocalDate.parse(minDate), LocalDate.parse(maxDate)),
zoneId
) { localDate, zone -> Date.from(localDate.atStartOfDay(zone).toInstant()) }
}

fun Arb.Companion.javaDate(
minDate: Date,
maxDate: Date,
zoneId: Arb<ZoneId> = zoneId()
): Arb<Date> {
val dateFormat = SimpleDateFormat("yyyy-mm-dd")
return javaDate(
minDate = dateFormat.format(minDate),
maxDate = dateFormat.format(maxDate),
zoneId = zoneId
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.Period
import java.util.Date
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.isSubclassOf
Expand All @@ -22,6 +23,7 @@ actual inline fun <reified A> targetDefaultForClass(): Arb<A>? = targetDefaultFo
fun targetDefaultForType(providedArbs: Map<KClass<*>, Arb<*>> = emptyMap(), type: KType): Arb<*>? {
when (type) {
typeOf<Instant>() -> Arb.instant()
typeOf<Date>() -> Arb.javaDate()
typeOf<LocalDate>() -> Arb.localDate()
typeOf<LocalDateTime>() -> Arb.localDateTime()
typeOf<LocalTime>() -> Arb.localTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ import io.kotest.inspectors.forAll
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.date.shouldNotBeAfter
import io.kotest.matchers.date.shouldNotBeBefore
import io.kotest.matchers.ints.shouldBeGreaterThan
import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual
import io.kotest.matchers.ints.shouldBeLessThan
import io.kotest.matchers.ints.shouldBeLessThanOrEqual
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.RandomSource
import io.kotest.property.arbitrary.javaDate
import io.kotest.property.arbitrary.edgecases
import io.kotest.property.arbitrary.localDate
import io.kotest.property.arbitrary.localDateTime
import io.kotest.property.arbitrary.localTime
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.yearMonth
import io.kotest.property.arbitrary.zonedDateTime
import io.kotest.property.checkAll
import io.kotest.property.forAll
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.LocalDate.of
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.Period
import java.time.YearMonth
import java.time.ZoneId
Expand Down Expand Up @@ -289,4 +289,52 @@ class DateTest : WordSpec({
months.sorted() shouldBe (1..12).toSet()
}
}

"Arb.javaDate(minDate: String, maxDate: String, zoneId)" should {
"generate Dates between with default values" {
val days = mutableSetOf<Int>()
val months = mutableSetOf<Int>()
val years = mutableSetOf<Int>()

val zoneId = ZoneId.systemDefault()
val testedArb = Arb.javaDate(zoneId = Arb.of(zoneId))

checkAll(5000, testedArb) { date ->
val localDate = LocalDate.ofInstant(date.toInstant(), zoneId)
days.add(localDate.dayOfMonth)
months.add(localDate.monthValue)
years.add(localDate.year)
}

days.sorted() shouldBe (1..31).toSet()
months.sorted() shouldBe (1..12).toSet()
years.sorted() shouldBe (1970 .. 2050).toSet()
}
}

"Arb.javaDate(minDate: Date, maxDate: Date, zoneId)" should {
"generate Dates between with default values" {
val days = mutableSetOf<Int>()
val months = mutableSetOf<Int>()
val years = mutableSetOf<Int>()

val zoneId = ZoneId.systemDefault()
val testedArb = Arb.javaDate(
minDate = SimpleDateFormat("yyyy-mm-dd").parse("1970-01-01"),
maxDate = SimpleDateFormat("yyyy-mm-dd").parse("2050-12-31"),
zoneId = Arb.of(zoneId)
)

checkAll(5000, testedArb) { date ->
val localDate = LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault())
days.add(localDate.dayOfMonth)
months.add(localDate.monthValue)
years.add(localDate.year)
}

days.sorted() shouldBe (1..31).toSet()
months.sorted() shouldBe (1..12).toSet()
years.sorted() shouldBe (1970 .. 2050).toSet()
}
}
})

0 comments on commit 093c898

Please sign in to comment.