fa62b50a99f1304f2f9fa52c8c68dfde90213377
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / DurationFormatFilterTest.kt
1 /**
2  * Sone - DurationFormatFilterTest.kt - Copyright © 2019 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 org.hamcrest.MatcherAssert.*
21 import org.hamcrest.Matchers.*
22 import kotlin.test.*
23
24 class DurationFormatFilterTest {
25
26         private val filter = DurationFormatFilter()
27
28         @Test
29         fun `random object is returned as it is`() {
30                 val randomObject = Any()
31                 assertThat(filter.format(null, randomObject, emptyMap()), sameInstance(randomObject))
32         }
33
34         @Test
35         fun `integer 0 is rendered as “0s”`() {
36                 verifyDuration(0, "0s")
37         }
38
39         @Test
40         fun `long 0 is rendered as “0s”`() {
41                 verifyDuration(0L, "0s")
42         }
43
44         @Test
45         fun `12 is rendered as “12_0s”`() {
46                 verifyDuration(12, "12.0s")
47         }
48
49         @Test
50         fun `123 is rendered as “2_1m”`() {
51                 verifyDuration(123, "2.1m")
52         }
53
54         @Test
55         fun `12345 is rendered as “3_4h”`() {
56                 verifyDuration(12345, "3.4h")
57         }
58
59         @Test
60         fun `123456 is rendered as “1_4d”`() {
61                 verifyDuration(123456, "1.4d")
62         }
63
64         @Test
65         fun `1234567 is rendered as “2_0w”`() {
66                 verifyDuration(1234567, "2.0w")
67         }
68
69         @Test
70         fun `123456789 with scale ms is rendered as “1_4d”`() {
71                 verifyDuration(123456789, "1.4d", "ms")
72         }
73
74         @Test
75         fun `123456789 with scale μs is rendered as “2_1m”`() {
76                 verifyDuration(123456789, "2.1m", "μs")
77         }
78
79         @Test
80         fun `123456789 with scale ns is rendered as “123_5ms”`() {
81                 verifyDuration(123456789, "123.5ms", "ns")
82         }
83
84         @Test
85         fun `123456 with scale ns is rendered as “123_5μs”`() {
86                 verifyDuration(123456, "123.5μs", "ns")
87         }
88
89         @Test
90         fun `123 with scale ns is rendered as “123_0ns”`() {
91                 verifyDuration(123, "123.0ns", "ns")
92         }
93
94         private fun verifyDuration(value: Any, expectedRendering: String, scale: String? = null) {
95                 assertThat(filter.format(null, value, scale?.let { mapOf("scale" to scale) } ?: emptyMap()), equalTo<Any>(expectedRendering))
96         }
97
98 }