2 * Sone - DurationFormatFilterTest.kt - Copyright © 2019 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 org.hamcrest.MatcherAssert.*
21 import org.hamcrest.Matchers.*
24 class DurationFormatFilterTest {
26 private val filter = DurationFormatFilter()
29 fun `random object is returned as it is`() {
30 val randomObject = Any()
31 assertThat(filter.format(null, randomObject, emptyMap()), sameInstance(randomObject))
35 fun `integer 0 is rendered as “0s”`() {
36 verifyDuration(0, "0s")
40 fun `long 0 is rendered as “0s”`() {
41 verifyDuration(0L, "0s")
45 fun `12 is rendered as “12_0s”`() {
46 verifyDuration(12, "12.0s")
50 fun `123 is rendered as “2_1m”`() {
51 verifyDuration(123, "2.1m")
55 fun `12345 is rendered as “3_4h”`() {
56 verifyDuration(12345, "3.4h")
60 fun `123456 is rendered as “1_4d”`() {
61 verifyDuration(123456, "1.4d")
65 fun `1234567 is rendered as “2_0w”`() {
66 verifyDuration(1234567, "2.0w")
70 fun `123456789 with scale ms is rendered as “1_4d”`() {
71 verifyDuration(123456789, "1.4d", "ms")
75 fun `123456789 with scale μs is rendered as “2_1m”`() {
76 verifyDuration(123456789, "2.1m", "μs")
80 fun `123456789 with scale ns is rendered as “123_5ms”`() {
81 verifyDuration(123456789, "123.5ms", "ns")
85 fun `123456 with scale ns is rendered as “123_5μs”`() {
86 verifyDuration(123456, "123.5μs", "ns")
90 fun `123 with scale ns is rendered as “123_0ns”`() {
91 verifyDuration(123, "123.0ns", "ns")
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))