2 * Sone - DurationFormatFilterTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.template
20 import net.pterodactylus.sone.test.OverrideLocale
21 import org.hamcrest.MatcherAssert.*
22 import org.hamcrest.Matchers.*
24 import java.util.Locale.ENGLISH
27 class DurationFormatFilterTest {
29 private val filter = DurationFormatFilter()
32 val overrideLocale = OverrideLocale(ENGLISH)
35 fun `random object is returned as it is`() {
36 val randomObject = Any()
37 assertThat(filter.format(null, randomObject, emptyMap()), sameInstance(randomObject))
41 fun `integer 0 is rendered as “0s”`() {
42 verifyDuration(0, "0s")
46 fun `long 0 is rendered as “0s”`() {
47 verifyDuration(0L, "0s")
51 fun `12 is rendered as “12_0s”`() {
52 verifyDuration(12, "12.0s")
56 fun `123 is rendered as “2_1m”`() {
57 verifyDuration(123, "2.1m")
61 fun `12345 is rendered as “3_4h”`() {
62 verifyDuration(12345, "3.4h")
66 fun `123456 is rendered as “1_4d”`() {
67 verifyDuration(123456, "1.4d")
71 fun `1234567 is rendered as “2_0w”`() {
72 verifyDuration(1234567, "2.0w")
76 fun `123456789 with scale ms is rendered as “1_4d”`() {
77 verifyDuration(123456789, "1.4d", "ms")
81 fun `123456789 with scale μs is rendered as “2_1m”`() {
82 verifyDuration(123456789, "2.1m", "μs")
86 fun `123456789 with scale ns is rendered as “123_5ms”`() {
87 verifyDuration(123456789, "123.5ms", "ns")
91 fun `123456 with scale ns is rendered as “123_5μs”`() {
92 verifyDuration(123456, "123.5μs", "ns")
96 fun `123 with scale ns is rendered as “123_0ns”`() {
97 verifyDuration(123, "123.0ns", "ns")
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))