Skip to content

Commit

Permalink
Plus or minus for instant draft (#7) (#3784)
Browse files Browse the repository at this point in the history
#3747 - support `Instant`

Co-authored-by: Sam <sam@sksamuel.com>
  • Loading branch information
AlexCue987 and sksamuel committed Nov 18, 2023
1 parent 99061c1 commit bdbabd2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ public final class io/kotest/matchers/date/InstantKt {
public static final fun before (Ljava/time/Instant;)Lio/kotest/matchers/Matcher;
public static final fun between (Ljava/time/Instant;Ljava/time/Instant;)Lio/kotest/matchers/Matcher;
public static final fun closeTo-HG0u8IE (Ljava/time/Instant;J)Lio/kotest/matchers/Matcher;
public static final fun plusOrMinus-HG0u8IE (Ljava/time/Instant;J)Lio/kotest/matchers/Matcher;
public static final fun shouldBeAfter (Ljava/time/Instant;Ljava/time/Instant;)Ljava/lang/Object;
public static final fun shouldBeBefore (Ljava/time/Instant;Ljava/time/Instant;)Ljava/lang/Object;
public static final fun shouldBeBetween (Ljava/time/Instant;Ljava/time/Instant;Ljava/time/Instant;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNot
import io.kotest.matchers.shouldNotBe
import java.time.Instant
import java.time.LocalDateTime
import kotlin.time.Duration
import kotlin.time.toKotlinDuration

Expand Down Expand Up @@ -100,3 +101,5 @@ fun Instant.shouldBeCloseTo(anotherInstant: Instant, duration: Duration) =
* */
fun Instant.shouldNotBeCloseTo(anotherInstant: Instant, duration: Duration) =
this shouldNotBe closeTo(anotherInstant, duration)

infix fun Instant.plusOrMinus(tolerance: Duration) = closeTo(this, tolerance)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sksamuel.kotest.matchers.date

import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.date.plusOrMinus
import io.kotest.matchers.date.shouldBeAfter
import io.kotest.matchers.date.shouldBeBefore
import io.kotest.matchers.date.shouldBeBetween
Expand Down Expand Up @@ -94,10 +95,12 @@ class InstantMatcherTest : FreeSpec() {

"current instant and 5 nanos ago instant should be close to 5 nanoseconds each other" {
val currentInstant = Instant.now()
val hundredNanosAgoInstant = currentInstant.minusNanos(5L)
val fiveNanosAgoInstant = currentInstant.minusNanos(5L)

currentInstant.shouldBeCloseTo(hundredNanosAgoInstant, 5L.nanoseconds)
hundredNanosAgoInstant.shouldBeCloseTo(currentInstant, 5L.nanoseconds)
currentInstant.shouldBeCloseTo(fiveNanosAgoInstant, 5L.nanoseconds)
fiveNanosAgoInstant.shouldBeCloseTo(currentInstant, 5L.nanoseconds)
currentInstant shouldBe (fiveNanosAgoInstant plusOrMinus 5L.nanoseconds)
fiveNanosAgoInstant shouldBe (currentInstant plusOrMinus 5L.nanoseconds)
}

"current instant and 1500 millis ago instant should not be close to 1000 millis each other" {
Expand All @@ -106,6 +109,8 @@ class InstantMatcherTest : FreeSpec() {

currentInstant.shouldNotBeCloseTo(someMillisAgoInstant, 1000L.milliseconds)
someMillisAgoInstant.shouldNotBeCloseTo(currentInstant, 1000L.milliseconds)
currentInstant shouldNotBe (someMillisAgoInstant plusOrMinus 1000L.nanoseconds)
someMillisAgoInstant shouldNotBe (currentInstant plusOrMinus 1000L.nanoseconds)
}

}
Expand Down

0 comments on commit bdbabd2

Please sign in to comment.