Adam Jarvis
09/15/2024, 7:00 PM// BACKWARD time - defaults to 'ZonedDateTime.now()'
createdAt shouldBeWithinLast Duration.ofSeconds(1)
// FORWARD time - defaults to 'ZonedDateTime.now()'
deletedAt shouldBeWithinNext Duration.ofHour(1)
// BACKWARD time - setting custom 'base' time
val yesterdayAtNoon = ZonedDateTime.now()
.minusDays(1)
.with(LocalTime.of(12, 0)
updatedAt shouldBeWithin Duration.ofDay(1) from yesterdayAtNoon
// FORWARD time - setting custom 'base' time
val tomorrowAtNoon = ZonedDateTime.now()
.plusDays(1)
.with(LocalTime.of(12, 0)
updatedAt shouldBeWithin Duration.ofDay(1) to tomorrowAtNoon
What do you folks think? I'm happy to open a PR to bring it in. Not quite sure which package it should go in thoughAdam Jarvis
09/15/2024, 7:02 PMExpected time (2024-09-15T18:53:03.060038Z) to be within 1 milliseconds to now, but the difference was 167 milliseconds
Emil Kantis
09/15/2024, 7:02 PMAdam Jarvis
09/15/2024, 7:03 PMAdam Jarvis
09/15/2024, 7:03 PMEmil Kantis
09/15/2024, 7:06 PMAdam Jarvis
09/15/2024, 7:07 PMEmil Kantis
09/15/2024, 7:07 PMEmil Kantis
09/15/2024, 7:07 PMAdam Jarvis
09/15/2024, 7:13 PMcreatedAt plusOrMinus 10.days
I guess I didn't notice the plusOrMinus
because I was expecting should
as a prefixAdam Jarvis
09/15/2024, 7:14 PMplusOrMinus
doesn't allow you to specify another base time to compare against
But that might not be super useful I guessAdam Jarvis
09/15/2024, 7:20 PMcreatedAt shouldBe now plusOrMinus 1.seconds
Where now = ZonedDateTime.now()
Adam Jarvis
09/15/2024, 7:20 PMEmil Kantis
09/15/2024, 7:20 PMAdam Jarvis
09/15/2024, 7:21 PMcreatedAt plusOrMinus 1.seconds
is syntactically correct
Perhaps it's just that I'm new to Kotlin 😄 (as you can maybe tell from my old Java usage of Duration.ofSeconds
)Emil Kantis
09/15/2024, 7:23 PMAdam Jarvis
09/15/2024, 7:24 PMnow
as a helper?
A lot of my comparisons are against now
so repeating
createdAt shouldBe now plusOrMinus 1.seconds
Vs something like
createdAt shouldBe nowPlusOrMinus 1.seconds
(Happy to take suggestions on naming!)Emil Kantis
09/15/2024, 7:29 PMnow plusOrMinus
with a GitHub search for instance?Adam Jarvis
09/15/2024, 7:36 PM"shouldBe" "now" " plusOrMinus" language:Kotlin
I'm happy to just keep it as an internal (to our codebase) extension function if you'd preferAdam Jarvis
09/15/2024, 7:40 PMinfix fun ZonedDateTime.nowPlusOrMinus(duration: Duration): ZonedDateTimeToleranceMatcher =
this shouldBe ZonedDateTime.now(this.zone) plusOrMinus(duration)
in our code now 👍Emil Kantis
09/15/2024, 7:41 PMAdam Jarvis
09/15/2024, 8:00 PMcreatedAt shouldBe now plusOrMinus 1.seconds
Am I using this wrong?
io.kotest.assertions.AssertionFailedError: expected:<2024-09-15T19:58:14.160949Z> but was:<2024-09-15T19:58:13.974671Z>
By my maths, they're ~ 0.1 second from each other?Emil Kantis
09/15/2024, 8:02 PMEmil Kantis
09/15/2024, 8:02 PMAdam Jarvis
09/15/2024, 8:03 PMcreatedAt shouldBe (now plusOrMinus 1.seconds)
Emil Kantis
09/15/2024, 8:04 PMAdam Jarvis
09/15/2024, 8:05 PMinfix fun ZonedDateTime.plusOrMinus(tolerance: Duration): ZonedDateTimeToleranceMatcher =
ZonedDateTimeToleranceMatcher(this, tolerance)
Emil Kantis
09/15/2024, 8:06 PM@CheckReturnValue
on plusOrMinus to avoid that.. since it only works with JVM types, it should be fine to use the JVM-only annotationAdam Jarvis
09/15/2024, 8:09 PM@CheckReturnValue
infix fun ZonedDateTime.plusOrMinus(tolerance: Duration): ZonedDateTimeToleranceMatcher =
ZonedDateTimeToleranceMatcher(this, tolerance)
Am I just using the function incorrectly?Adam Jarvis
09/15/2024, 8:14 PMAlex Kuznetsov
09/16/2024, 3:27 PM