Introduce ClockMark.hasPassedNow, hasNotPassedNow functions
Makes it more clear to use them than comparing elapsed with Duration.ZERO.
This commit is contained in:
@@ -52,6 +52,23 @@ public abstract class ClockMark {
|
||||
* The returned clock mark is more _early_ when the [duration] is positive, and more _late_ when the [duration] is negative.
|
||||
*/
|
||||
public open operator fun minus(duration: Duration): ClockMark = plus(-duration)
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if this clock mark has passed according to the clock from which this mark was taken.
|
||||
*
|
||||
* Note that the value returned by this function can change on subsequent invocations.
|
||||
* If the clock is monotonic, it can change only from `false` to `true`, namely, when the clock mark becomes behind the current point of the clock.
|
||||
*/
|
||||
public fun hasPassedNow(): Boolean = !elapsedNow().isNegative()
|
||||
|
||||
/**
|
||||
* Returns false if this clock mark has not passed according to the clock from which this mark was taken.
|
||||
*
|
||||
* Note that the value returned by this function can change on subsequent invocations.
|
||||
* If the clock is monotonic, it can change only from `true` to `false`, namely, when the clock mark becomes behind the current point of the clock.
|
||||
*/
|
||||
public fun hasNotPassedNow(): Boolean = elapsedNow().isNegative()
|
||||
}
|
||||
|
||||
@ExperimentalTime
|
||||
|
||||
@@ -14,12 +14,19 @@ class ClockMarkTest {
|
||||
fun adjustment() {
|
||||
val clock = TestClock()
|
||||
|
||||
val mark = clock.markNow()
|
||||
val markFuture1 = mark + 1.milliseconds
|
||||
val markFuture2 = mark - (-1).milliseconds
|
||||
fun ClockMark.assertHasPassed(hasPassed: Boolean) {
|
||||
assertEquals(!hasPassed, this.hasNotPassedNow(), "Expected mark in the future")
|
||||
assertEquals(hasPassed, this.hasPassedNow(), "Expected mark in the past")
|
||||
|
||||
val markPast1 = mark - 1.milliseconds
|
||||
val markPast2 = markFuture1 + (-2).milliseconds
|
||||
assertEquals(!hasPassed, this.elapsedNow() < Duration.ZERO, "Mark elapsed: ${this.elapsedNow()}, expected hasPassed: $hasPassed")
|
||||
}
|
||||
|
||||
val mark = clock.markNow()
|
||||
val markFuture1 = (mark + 1.milliseconds).apply { assertHasPassed(false) }
|
||||
val markFuture2 = (mark - (-1).milliseconds).apply { assertHasPassed(false) }
|
||||
|
||||
val markPast1 = (mark - 1.milliseconds).apply { assertHasPassed(true) }
|
||||
val markPast2 = (markFuture1 + (-2).milliseconds).apply { assertHasPassed(true) }
|
||||
|
||||
clock += 500_000.nanoseconds
|
||||
|
||||
@@ -33,5 +40,14 @@ class ClockMarkTest {
|
||||
|
||||
assertEquals(elapsedFromPast, markPast1.elapsedNow())
|
||||
assertEquals(elapsedFromPast, markPast2.elapsedNow())
|
||||
|
||||
markFuture1.assertHasPassed(false)
|
||||
markPast1.assertHasPassed(true)
|
||||
|
||||
clock += 1.milliseconds
|
||||
|
||||
markFuture1.assertHasPassed(true)
|
||||
markPast1.assertHasPassed(true)
|
||||
|
||||
}
|
||||
}
|
||||
+2
@@ -5288,6 +5288,8 @@ public abstract interface class kotlin/time/Clock {
|
||||
public abstract class kotlin/time/ClockMark {
|
||||
public fun <init> ()V
|
||||
public abstract fun elapsedNow ()D
|
||||
public final fun hasNotPassedNow ()Z
|
||||
public final fun hasPassedNow ()Z
|
||||
public fun minus-LRDsOJo (D)Lkotlin/time/ClockMark;
|
||||
public fun plus-LRDsOJo (D)Lkotlin/time/ClockMark;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user