Skip to content

Commit

Permalink
Make LocalizedTests reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobras committed Aug 20, 2024
1 parent 67b0e6f commit 193135e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package nl.jacobras.humanreadable

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.time.Duration

Expand All @@ -21,8 +22,11 @@ object HumanReadable {
* @param instant The [Instant] to format.
* @return a formatted string
*/
fun timeAgo(instant: Instant): String {
return safelyTranslate { formatTimeAgo(instant) }
fun timeAgo(
instant: Instant,
baseInstant: Instant = Clock.System.now()
): String {
return safelyTranslate { formatTimeAgo(instant, baseInstant) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package nl.jacobras.humanreadable

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

/**
* Returns the difference between now and [instant], in human-readable format.
* Returns the difference between [baseInstant] and [instant], in human-readable format.
* Also supports instants in the future.
*
* @param instant The [Instant] to compare with [baseInstant].
* @param baseInstant The base/starting [Instant], usually "now".
*/
internal fun formatTimeAgo(instant: Instant): String {
val now = Clock.System.now()
val diff = now - instant
internal fun formatTimeAgo(
instant: Instant,
baseInstant: Instant
): String {
val diff = baseInstant - instant
val secondsAgo = diff.inWholeSeconds

return when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import nl.jacobras.humanreadable.HumanReadable
import kotlin.test.Test
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
Expand All @@ -17,20 +16,13 @@ import kotlin.time.Duration.Companion.seconds
*/
class LocalizedTests {

private val now: Instant
get() = Clock.System.now()
private val oneMinute: Duration
get() = 1.minutes
private val oneMinuteAgo: Instant
get() = now - oneMinute
private val oneMinuteFromNow: Instant
get() = now + oneMinute
private val twoMonths: Duration
get() = 60.days
private val twoMonthsAgo: Instant
get() = now - twoMonths
private val twoMonthsFromNow: Instant
get() = now + twoMonths
private val now: Instant = Clock.System.now()
private val oneMinute = 1.minutes
private val oneMinuteAgo = now - oneMinute
private val oneMinuteFromNow = now + oneMinute
private val twoMonths = 60.days
private val twoMonthsAgo = now - twoMonths
private val twoMonthsFromNow = now + twoMonths

@Test
fun cs() {
Expand All @@ -44,8 +36,8 @@ class LocalizedTests {
assertThat(HumanReadable.duration(2.seconds)).isEqualTo("2 Sekunden")

assertThat(HumanReadable.duration(twoMonths)).isEqualTo("2 Monate")
assertThat(HumanReadable.timeAgo(twoMonthsAgo)).isEqualTo("vor 2 Monaten")
assertThat(HumanReadable.timeAgo(twoMonthsFromNow)).isEqualTo("in 2 Monaten")
assertThat(HumanReadable.timeAgo(twoMonthsAgo, baseInstant = now)).isEqualTo("vor 2 Monaten")
assertThat(HumanReadable.timeAgo(twoMonthsFromNow, baseInstant = now)).isEqualTo("in 2 Monaten")
}

@Test
Expand Down Expand Up @@ -108,8 +100,8 @@ class LocalizedTests {
assertThat(HumanReadable.duration(2.seconds)).isEqualTo("2 sekundy")

assertThat(HumanReadable.duration(oneMinute)).isEqualTo("1 minuta")
assertThat(HumanReadable.timeAgo(oneMinuteAgo)).isEqualTo("1 minutę temu")
assertThat(HumanReadable.timeAgo(oneMinuteFromNow)).isEqualTo("za 1 minutę")
assertThat(HumanReadable.timeAgo(oneMinuteAgo, baseInstant = now)).isEqualTo("1 minutę temu")
assertThat(HumanReadable.timeAgo(oneMinuteFromNow, baseInstant = now)).isEqualTo("za 1 minutę")
}

@Test
Expand All @@ -126,8 +118,8 @@ class LocalizedTests {
assertThat(HumanReadable.duration(2.seconds)).isEqualTo("2 секунды")

assertThat(HumanReadable.duration(oneMinute)).isEqualTo("1 минута")
assertThat(HumanReadable.timeAgo(oneMinuteAgo)).isEqualTo("1 минуту назад")
assertThat(HumanReadable.timeAgo(oneMinuteFromNow)).isEqualTo("через 1 минуту")
assertThat(HumanReadable.timeAgo(oneMinuteAgo, baseInstant = now)).isEqualTo("1 минуту назад")
assertThat(HumanReadable.timeAgo(oneMinuteFromNow, baseInstant = now)).isEqualTo("через 1 минуту")
}

@Test
Expand Down

0 comments on commit 193135e

Please sign in to comment.