Skip to content

Commit 88c1bfd

Browse files
committed
chore: abstract kotlin.time Instant implementation for tests
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 01cc272 commit 88c1bfd

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.flipcash.app.inject
2+
3+
import com.flipcash.app.core.time.TimeProvider
4+
import com.flipcash.app.internal.time.SystemTimeProvider
5+
import dagger.Module
6+
import dagger.Provides
7+
import dagger.hilt.InstallIn
8+
import dagger.hilt.components.SingletonComponent
9+
import javax.inject.Singleton
10+
11+
@Module
12+
@InstallIn(SingletonComponent::class)
13+
object TimeModule {
14+
15+
@Provides
16+
@Singleton
17+
fun providesTimeProvider(): TimeProvider = SystemTimeProvider()
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.flipcash.app.internal.time
2+
3+
import com.flipcash.app.core.time.TimeProvider
4+
import kotlin.time.Clock
5+
6+
class SystemTimeProvider : TimeProvider {
7+
override fun now() = Clock.System.now()
8+
override fun currentTimeMillis() = System.currentTimeMillis()
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.flipcash.app.core.time
2+
3+
import kotlin.time.Clock
4+
import kotlin.time.Duration
5+
import kotlin.time.Instant
6+
7+
class FakeTimeProvider(
8+
private var currentTime: Instant = Clock.System.now()
9+
) : TimeProvider {
10+
override fun now(): Instant = currentTime
11+
fun advanceBy(duration: Duration) { currentTime += duration }
12+
fun setTime(instant: Instant) { currentTime = instant }
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.flipcash.app.core.time
2+
3+
import kotlin.time.Instant
4+
5+
interface TimeProvider {
6+
fun now(): Instant
7+
fun currentTimeMillis(): Long = now().toEpochMilliseconds()
8+
}

0 commit comments

Comments
 (0)