🔀 Merge branch 'release/v82'
[Sone.git] / pterodactylus / sone / text / TimeTextConverterTest.kt
1 package net.pterodactylus.sone.text
2
3 import net.pterodactylus.sone.freenet.L10nText
4 import org.hamcrest.MatcherAssert.assertThat
5 import org.hamcrest.Matchers.equalTo
6 import org.junit.Test
7 import java.util.concurrent.TimeUnit.DAYS
8 import java.util.concurrent.TimeUnit.HOURS
9 import java.util.concurrent.TimeUnit.MINUTES
10 import java.util.concurrent.TimeUnit.SECONDS
11
12 /**
13  * Unit test for [TimeTextConverter].
14  */
15 class TimeTextConverterTest {
16
17         val now = System.currentTimeMillis()
18         val converter = TimeTextConverter { now }
19
20         private fun verifyInterval(startTime: Long, end: Long, vararg expectedTimeTexts: TimeText) {
21                 assertThat(converter.getTimeText(now - startTime), equalTo(expectedTimeTexts[0]))
22                 assertThat(converter.getTimeText(now - (end - 1)), equalTo(expectedTimeTexts[1 % expectedTimeTexts.size]))
23         }
24
25         @Test
26         fun `time of zero returns the l10n key for "unknown" and a refresh time of 12 hours`() {
27                 assertThat(converter.getTimeText(0), equalTo(TimeText(L10nText("View.Sone.Text.UnknownDate"), HOURS.toMillis(12))))
28         }
29
30         @Test
31         fun `time in the future returns the correct l10n key and refresh of 5 minutes`() {
32                 assertThat(converter.getTimeText(now + 1), equalTo(TimeText(L10nText("View.Time.InTheFuture"), MINUTES.toMillis(5))))
33         }
34
35         @Test
36         fun `time of zero to twenty seconds ago returns l10n key for "a few seconds ago" and refresh of 10 seconds`() {
37                 verifyInterval(0, SECONDS.toMillis(20), TimeText(L10nText("View.Time.AFewSecondsAgo"), SECONDS.toMillis(10)))
38         }
39
40         @Test
41         fun `time of twenty to forty-five seconds ago returns l10n key for "half a minute ago" and refresh of 20 seconds`() {
42                 verifyInterval(SECONDS.toMillis(20), SECONDS.toMillis(45), TimeText(L10nText("View.Time.HalfAMinuteAgo"), SECONDS.toMillis(20)))
43         }
44
45         @Test
46         fun `time of forty-five to ninety seconds ago returns l10n key for "a minute ago" and a refresh time of 1 minute`() {
47                 verifyInterval(SECONDS.toMillis(45), SECONDS.toMillis(90), TimeText(L10nText("View.Time.AMinuteAgo"), MINUTES.toMillis(1)))
48         }
49
50         @Test
51         fun `time of ninety seconds to thirty minutes ago returns l10n key for "x minutes ago," the number of minutes, and a refresh time of 1 minute`() {
52                 verifyInterval(SECONDS.toMillis(90), MINUTES.toMillis(30),
53                                 TimeText(L10nText("View.Time.XMinutesAgo", listOf(2L)), MINUTES.toMillis(1)),
54                                 TimeText(L10nText("View.Time.XMinutesAgo", listOf(30L)), MINUTES.toMillis(1)))
55         }
56
57         @Test
58         fun `time of thirty to forty-five minutes ago returns l10n key for "half an hour ago" and a refresh time of 10 minutes`() {
59                 verifyInterval(MINUTES.toMillis(30), MINUTES.toMillis(45), TimeText(L10nText("View.Time.HalfAnHourAgo"), MINUTES.toMillis(10)))
60         }
61
62         @Test
63         fun `time of forty-five to ninety minutes ago returns l10n key for "an hour ago" and a refresh time of 1 hour`() {
64                 verifyInterval(MINUTES.toMillis(45), MINUTES.toMillis(90), TimeText(L10nText("View.Time.AnHourAgo"), HOURS.toMillis(1)))
65         }
66
67         @Test
68         fun `time of ninety minutes to twenty-one hours ago returns l10n key for "x hours ago," the number of hours, and a refresh time of 1 hour`() {
69                 verifyInterval(MINUTES.toMillis(90), HOURS.toMillis(21),
70                                 TimeText(L10nText("View.Time.XHoursAgo", listOf(2L)), HOURS.toMillis(1)),
71                                 TimeText(L10nText("View.Time.XHoursAgo", listOf(21L)), HOURS.toMillis(1)))
72         }
73
74         @Test
75         fun `time of twenty-one to forty-two hours ago returns l10n key for "a day ago" and a refresh time of 1 day`() {
76                 verifyInterval(HOURS.toMillis(21), HOURS.toMillis(42), TimeText(L10nText("View.Time.ADayAgo"), DAYS.toMillis(1)))
77         }
78
79         @Test
80         fun `time of forty-two hours to six days ago returns l10n key for "x days ago," the number of days, and a refresh time of 1 day`() {
81                 verifyInterval(HOURS.toMillis(42), DAYS.toMillis(6),
82                                 TimeText(L10nText("View.Time.XDaysAgo", listOf(2L)), DAYS.toMillis(1)),
83                                 TimeText(L10nText("View.Time.XDaysAgo", listOf(6L)), DAYS.toMillis(1)))
84         }
85
86         @Test
87         fun `time of six to eleven days ago returns l10n key for "a week ago" and a refresh time of 1 day`() {
88                 verifyInterval(DAYS.toMillis(6), DAYS.toMillis(11), TimeText(L10nText("View.Time.AWeekAgo"), DAYS.toMillis(1)))
89         }
90
91         @Test
92         fun `time of eleven to twenty-eight days ago returns l10n key for "x weeks ago," the number of weeks, and a refresh time of 1 day`() {
93                 verifyInterval(DAYS.toMillis(11), DAYS.toMillis(28),
94                                 TimeText(L10nText("View.Time.XWeeksAgo", listOf(2L)), DAYS.toMillis(1)),
95                                 TimeText(L10nText("View.Time.XWeeksAgo", listOf(4L)), DAYS.toMillis(1)))
96         }
97
98         @Test
99         fun `time of twenty-eight to forty-two days ago returns l10n key for "a month ago" and a refresh time of 1 day`() {
100                 verifyInterval(DAYS.toMillis(28), DAYS.toMillis(42), TimeText(L10nText("View.Time.AMonthAgo"), DAYS.toMillis(1)))
101         }
102
103         @Test
104         fun `time of forty-two to three hundred and thirty days ago returns l10n key for "x months ago," the number of months, and a refresh time of 1 day`() {
105                 verifyInterval(DAYS.toMillis(42), DAYS.toMillis(330),
106                                 TimeText(L10nText("View.Time.XMonthsAgo", listOf(1L)), DAYS.toMillis(1)),
107                                 TimeText(L10nText("View.Time.XMonthsAgo", listOf(11L)), DAYS.toMillis(1)))
108         }
109
110         @Test
111         fun `time of three hundred and thirty to five hundred and forty days days ago returns l10n key for "a year ago" and a refresh time of 7 days`() {
112                 verifyInterval(DAYS.toMillis(330), DAYS.toMillis(540), TimeText(L10nText("View.Time.AYearAgo"), DAYS.toMillis(7)))
113         }
114
115         @Test
116         fun `time of five hunder and forty to infinity days ago returns l10n key for "x years ago," the number of years, and a refresh time of 7 days`() {
117                 verifyInterval(DAYS.toMillis(540), DAYS.toMillis(6000),
118                                 TimeText(L10nText("View.Time.XYearsAgo", listOf(1L)), DAYS.toMillis(7)),
119                                 TimeText(L10nText("View.Time.XYearsAgo", listOf(16L)), DAYS.toMillis(7)))
120         }
121
122 }