✅ Fix tests failing with non-English locales
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / DurationFormatFilterTest.kt
1 /**
2  * Sone - DurationFormatFilterTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.template
19
20 import net.pterodactylus.sone.test.OverrideLocale
21 import org.hamcrest.MatcherAssert.*
22 import org.hamcrest.Matchers.*
23 import org.junit.Rule
24 import java.util.Locale.ENGLISH
25 import kotlin.test.*
26
27 class DurationFormatFilterTest {
28
29         private val filter = DurationFormatFilter()
30
31         @get:Rule
32         val overrideLocale = OverrideLocale(ENGLISH)
33
34         @Test
35         fun `random object is returned as it is`() {
36                 val randomObject = Any()
37                 assertThat(filter.format(null, randomObject, emptyMap()), sameInstance(randomObject))
38         }
39
40         @Test
41         fun `integer 0 is rendered as “0s”`() {
42                 verifyDuration(0, "0s")
43         }
44
45         @Test
46         fun `long 0 is rendered as “0s”`() {
47                 verifyDuration(0L, "0s")
48         }
49
50         @Test
51         fun `12 is rendered as “12_0s”`() {
52                 verifyDuration(12, "12.0s")
53         }
54
55         @Test
56         fun `123 is rendered as “2_1m”`() {
57                 verifyDuration(123, "2.1m")
58         }
59
60         @Test
61         fun `12345 is rendered as “3_4h”`() {
62                 verifyDuration(12345, "3.4h")
63         }
64
65         @Test
66         fun `123456 is rendered as “1_4d”`() {
67                 verifyDuration(123456, "1.4d")
68         }
69
70         @Test
71         fun `1234567 is rendered as “2_0w”`() {
72                 verifyDuration(1234567, "2.0w")
73         }
74
75         @Test
76         fun `123456789 with scale ms is rendered as “1_4d”`() {
77                 verifyDuration(123456789, "1.4d", "ms")
78         }
79
80         @Test
81         fun `123456789 with scale μs is rendered as “2_1m”`() {
82                 verifyDuration(123456789, "2.1m", "μs")
83         }
84
85         @Test
86         fun `123456789 with scale ns is rendered as “123_5ms”`() {
87                 verifyDuration(123456789, "123.5ms", "ns")
88         }
89
90         @Test
91         fun `123456 with scale ns is rendered as “123_5μs”`() {
92                 verifyDuration(123456, "123.5μs", "ns")
93         }
94
95         @Test
96         fun `123 with scale ns is rendered as “123_0ns”`() {
97                 verifyDuration(123, "123.0ns", "ns")
98         }
99
100         private fun verifyDuration(value: Any, expectedRendering: String, scale: String? = null) {
101                 assertThat(filter.format(null, value, scale?.let { mapOf("scale" to scale) } ?: emptyMap()), equalTo<Any>(expectedRendering))
102         }
103
104 }