Skip to content

Commit

Permalink
Add convenience method for Clock testing
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Apr 18, 2024
1 parent 92d79be commit 85d3863
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dachlatten-datetime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ plugins {
dependencies {
compileOnly(libs.kotlinx.datetime)
compileOnly(libs.android.support.annotations)

testImplementation(libs.kotlinx.datetime)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.sipgate.dachlatten.datetime

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant

fun Clock.Companion.Fixed(instant: Instant) = object : Clock {
override fun now(): Instant = instant
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.sipgate.dachlatten.datetime

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class FixedClockTest {
@Test
fun fixedClockReturnsFixedInstance() {
val instant = Instant.fromEpochSeconds(1713453869)
val fixedClock = Clock.Fixed(instant)

val instantFromClock = fixedClock.now()

assertEquals(instant, instantFromClock)
}

@Test
fun fixedClockReturnsFixedInstanceWhenCalledMultipleTimes() {
val instant = Instant.fromEpochSeconds(1713453869)
val fixedClock = Clock.Fixed(instant)

val firstInstantFromClock = fixedClock.now()
val secondInstantFromClock = fixedClock.now()

assertEquals(instant, firstInstantFromClock)
assertEquals(firstInstantFromClock, secondInstantFromClock)
}
}

0 comments on commit 85d3863

Please sign in to comment.