Skip to content

Commit

Permalink
enhance clues with predicate info
Browse files Browse the repository at this point in the history
  • Loading branch information
James Santucci committed Jan 27, 2022
1 parent 87ef2bd commit 19c5ecf
Showing 1 changed file with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ import scala.util.Try

object GenJdk8Properties extends Properties("Java 8 Generators") {

case class LabeledPredicate[T](label: String, fn: T => Boolean)

def predicateClue[T](predicate: LabeledPredicate[T]): String =
s"Predicate label: ${predicate.label}"

// Guards against generating values well outside of expected ranges, as users may run into JDK bugs
implicit val yearRange: YearRange = YearRange.between(0, 10000)

Expand Down Expand Up @@ -63,7 +68,9 @@ object GenJdk8Properties extends Properties("Java 8 Generators") {
forAll((_: Instant) => passed)
}

val granularitiesAndPredicates: List[(Granularity[ZonedDateTime], ZonedDateTime => Boolean)] = {
val granularitiesAndPredicates: List[
(Granularity[ZonedDateTime], LabeledPredicate[ZonedDateTime])
] = {

import java.time.temporal.ChronoField._

Expand Down Expand Up @@ -99,24 +106,31 @@ object GenJdk8Properties extends Properties("Java 8 Generators") {
timezoneSwitch(dt) || (zeroHours(dt) && dt.get(DAY_OF_YEAR) == 1)

List(
(granularity.seconds, zeroNanos),
(granularity.minutes, zeroSeconds),
(granularity.hours, zeroMinutes),
(granularity.days, zeroHours),
(granularity.years, firstDay)
(granularity.seconds, LabeledPredicate[ZonedDateTime]("zero nanos", zeroNanos)),
(granularity.minutes, LabeledPredicate[ZonedDateTime]("zero seconds", zeroSeconds)),
(granularity.hours, LabeledPredicate[ZonedDateTime]("zero minutes", zeroMinutes)),
(granularity.days, LabeledPredicate[ZonedDateTime]("zero hours", zeroHours)),
(granularity.years, LabeledPredicate[ZonedDateTime]("first day", firstDay))
)
}

val granularitiesAndPredicatesWithDefault: List[
(Granularity[ZonedDateTime], ZonedDateTime => Boolean)
(Granularity[ZonedDateTime], LabeledPredicate[ZonedDateTime])
] =
(Granularity.identity[ZonedDateTime], (_: ZonedDateTime) => true) :: granularitiesAndPredicates
(
Granularity.identity[ZonedDateTime],
LabeledPredicate[ZonedDateTime]("identity", (_: ZonedDateTime) => true)
) :: granularitiesAndPredicates

property("genZonedDateTime with a granularity generates appropriate ZonedDateTimes") =
forAll(Gen.oneOf(granularitiesAndPredicates)) { case (granularity, predicate) =>
implicit val generatedGranularity: Granularity[ZonedDateTime] = granularity

forAll(genZonedDateTime)(dt => predicate(dt) :| s"${granularity.description}: $dt")
forAll(genZonedDateTime)(dt =>
predicate.fn(
dt
) :| s"Granularity: ${granularity.description}; ${predicateClue(predicate)}; $dt"
)
}

property("arbitrary generation with a granularity generates appropriate ZonedDateTimes") =
Expand All @@ -125,7 +139,9 @@ object GenJdk8Properties extends Properties("Java 8 Generators") {

implicit val generatedGranularity: Granularity[ZonedDateTime] = granularity

forAll((dt: ZonedDateTime) => predicate(dt) :| s"${granularity.description}: $dt")
forAll((dt: ZonedDateTime) =>
predicate.fn(dt) :| s"${granularity.description}; ${predicateClue(predicate)}; $dt"
)
}

property("arbitrary generation with a granularity generates appropriate LocalDateTimes") =
Expand All @@ -135,7 +151,8 @@ object GenJdk8Properties extends Properties("Java 8 Generators") {
implicit val generatedGranularity: Granularity[ZonedDateTime] = granularity

forAll { (dt: LocalDateTime) =>
predicate(dt.atZone(ZoneOffset.UTC)) :| s"${granularity.description}: $dt"
predicate
.fn(dt.atZone(ZoneOffset.UTC)) :| s"${granularity.description}; ${predicateClue(predicate)}; $dt"
}
}

Expand All @@ -146,7 +163,9 @@ object GenJdk8Properties extends Properties("Java 8 Generators") {
implicit val generatedGranularity: Granularity[ZonedDateTime] = granularity

forAll { (dt: LocalDate) =>
predicate(dt.atStartOfDay(ZoneOffset.UTC)) :| s"${granularity.description}: $dt"
predicate.fn(
dt.atStartOfDay(ZoneOffset.UTC)
) :| s"${granularity.description}; ${predicateClue(predicate)}; $dt"
}
}

Expand All @@ -157,7 +176,9 @@ object GenJdk8Properties extends Properties("Java 8 Generators") {
implicit val generatedGranularity: Granularity[ZonedDateTime] = granularity

forAll { (instant: Instant) =>
predicate(instant.atZone(ZoneOffset.UTC)) :| s"${granularity.description}: $instant"
predicate.fn(
instant.atZone(ZoneOffset.UTC)
) :| s"${granularity.description}; ${predicateClue(predicate)}; $instant"
}
}

Expand Down Expand Up @@ -211,7 +232,7 @@ object GenJdk8Properties extends Properties("Java 8 Generators") {
.isBefore(generated) || granularity.normalize(lowerBound).isEqual(generated)) &&
(upperBound.isAfter(generated) || upperBound.isEqual(generated))

val granularityCheck = predicate(generated)
val granularityCheck = predicate.fn(generated)

val prop = rangeCheck && granularityCheck

Expand Down

0 comments on commit 19c5ecf

Please sign in to comment.