X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FDurationFormatFilterTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FDurationFormatFilterTest.kt;h=9e378a805274ec910ce8d9304a2080e48cebe0bb;hb=6579401cd867c5ade1ebf098d64d85dc2016defe;hp=0000000000000000000000000000000000000000;hpb=112c4ef993b578e7ffc572b7eadd625136550017;p=Sone.git diff --git a/src/test/kotlin/net/pterodactylus/sone/template/DurationFormatFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/DurationFormatFilterTest.kt new file mode 100644 index 0000000..9e378a8 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/DurationFormatFilterTest.kt @@ -0,0 +1,88 @@ +/** + * Sone - DurationFormatFilterTest.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template + +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import kotlin.test.* + +class DurationFormatFilterTest { + + private val filter = DurationFormatFilter() + + @Test + fun `random object is returned as it is`() { + val randomObject = Any() + assertThat(filter.format(null, randomObject, emptyMap()), sameInstance(randomObject)) + } + + @Test + fun `integer 0 is rendered as “0s”`() { + verifyDuration(0, "0s") + } + + @Test + fun `long 0 is rendered as “0s”`() { + verifyDuration(0L, "0s") + } + + @Test + fun `12 is rendered as “12_0s”`() { + verifyDuration(12, "12.0s") + } + + @Test + fun `123 is rendered as “2_1m”`() { + verifyDuration(123, "2.1m") + } + + @Test + fun `12345 is rendered as “3_4h”`() { + verifyDuration(12345, "3.4h") + } + + @Test + fun `123456 is rendered as “1_4d”`() { + verifyDuration(123456, "1.4d") + } + + @Test + fun `1234567 is rendered as “2_0w”`() { + verifyDuration(1234567, "2.0w") + } + + @Test + fun `123456789 with scale ms is rendered as “1_4d”`() { + verifyDuration(123456789, "1.4d", "ms") + } + + @Test + fun `123456789 with scale μs is rendered as “2_1m”`() { + verifyDuration(123456789, "2.1m", "μs") + } + + @Test + fun `123456789 with scale ns is rendered as “123_5ms”`() { + verifyDuration(123456789, "123.5ms", "ns") + } + + private fun verifyDuration(value: Any, expectedRendering: String, scale: String? = null) { + assertThat(filter.format(null, value, scale?.let { mapOf("scale" to scale) } ?: emptyMap()), equalTo(expectedRendering)) + } + +}