🐛 Fix bug with nanosecond display
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 1 Aug 2019 05:21:29 +0000 (07:21 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 1 Aug 2019 05:21:29 +0000 (07:21 +0200)
src/main/kotlin/net/pterodactylus/sone/template/DurationFormatFilter.kt
src/test/kotlin/net/pterodactylus/sone/template/DurationFormatFilterTest.kt

index 559c9da..db49ef1 100644 (file)
@@ -53,7 +53,7 @@ private enum class FixedDuration {
                override val symbol = "ms"
        },
        MICROS {
                override val symbol = "ms"
        },
        MICROS {
-               override fun number(duration: Duration) = duration.nano / 1_1000.0
+               override fun number(duration: Duration) = duration.nano / 1_000.0
                override val symbol = "μs"
        },
        NANOS {
                override val symbol = "μs"
        },
        NANOS {
index 9e378a8..fa62b50 100644 (file)
@@ -81,6 +81,16 @@ class DurationFormatFilterTest {
                verifyDuration(123456789, "123.5ms", "ns")
        }
 
                verifyDuration(123456789, "123.5ms", "ns")
        }
 
+       @Test
+       fun `123456 with scale ns is rendered as “123_5μs”`() {
+               verifyDuration(123456, "123.5μs", "ns")
+       }
+
+       @Test
+       fun `123 with scale ns is rendered as “123_0ns”`() {
+               verifyDuration(123, "123.0ns", "ns")
+       }
+
        private fun verifyDuration(value: Any, expectedRendering: String, scale: String? = null) {
                assertThat(filter.format(null, value, scale?.let { mapOf("scale" to scale) } ?: emptyMap()), equalTo<Any>(expectedRendering))
        }
        private fun verifyDuration(value: Any, expectedRendering: String, scale: String? = null) {
                assertThat(filter.format(null, value, scale?.let { mapOf("scale" to scale) } ?: emptyMap()), equalTo<Any>(expectedRendering))
        }