From 3774ba38b4c293cbc5669ad4138ce6a0dafb3cda Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Tue, 19 Sep 2023 00:06:53 -0700 Subject: [PATCH] Rewrites SafeParseTest with matchers Issue #2745 --- .../info/nightscout/shared/SafeParseTest.kt | 116 +++++++++--------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/app-wear-shared/shared/src/test/java/info/nightscout/shared/SafeParseTest.kt b/app-wear-shared/shared/src/test/java/info/nightscout/shared/SafeParseTest.kt index 791347e426..495ea437d4 100644 --- a/app-wear-shared/shared/src/test/java/info/nightscout/shared/SafeParseTest.kt +++ b/app-wear-shared/shared/src/test/java/info/nightscout/shared/SafeParseTest.kt @@ -1,6 +1,6 @@ package info.nightscout.shared -import org.junit.jupiter.api.Assertions +import com.google.common.truth.Truth.assertThat import org.junit.jupiter.api.Test @Suppress("SpellCheckingInspection") @@ -8,105 +8,105 @@ class SafeParseTest { @Test fun stringToDoubleTest() { // correct values - Assertions.assertEquals(0.1234, SafeParse.stringToDouble("0.1234"), 0.001) - Assertions.assertEquals(0.1234, SafeParse.stringToDouble("0,1234"), 0.001) - Assertions.assertEquals(0.5436564812, SafeParse.stringToDouble(".5436564812"), 0.001) - Assertions.assertEquals(0.5436564812, SafeParse.stringToDouble(",5436564812"), 0.001) - Assertions.assertEquals(1000500100900.0, SafeParse.stringToDouble("1000500100900"), 0.001) - Assertions.assertEquals(42.0, SafeParse.stringToDouble("42"), 0.001) + assertThat(SafeParse.stringToDouble("0.1234")).isWithin(0.001).of(0.1234) + assertThat(SafeParse.stringToDouble("0,1234")).isWithin(0.001).of(0.1234) + assertThat(SafeParse.stringToDouble(".5436564812")).isWithin(0.001).of(0.5436564812) + assertThat(SafeParse.stringToDouble(",5436564812")).isWithin(0.001).of(0.5436564812) + assertThat(SafeParse.stringToDouble("1000500100900")).isWithin(0.001).of(1000500100900.0) + assertThat(SafeParse.stringToDouble("42")).isWithin(0.001).of(42.0) // units or other extra values are not permitted - Assertions.assertEquals(0.0, SafeParse.stringToDouble("12 U/h"), 0.001) + assertThat(SafeParse.stringToDouble("12 U/h")).isWithin(0.001).of(0.0) // strings are not parsable - Assertions.assertEquals(0.0, SafeParse.stringToDouble("ala ma kota"), 0.001) + assertThat(SafeParse.stringToDouble("ala ma kota")).isWithin(0.001).of(0.0) // separator errors - Assertions.assertEquals(0.0, SafeParse.stringToDouble("0.1234.5678"), 0.001) - Assertions.assertEquals(0.0, SafeParse.stringToDouble("0,1234,5678"), 0.001) + assertThat(SafeParse.stringToDouble("0.1234.5678")).isWithin(0.001).of(0.0) + assertThat(SafeParse.stringToDouble("0,1234,5678")).isWithin(0.001).of(0.0) // various emptiness - Assertions.assertEquals(0.0, SafeParse.stringToDouble(""), 0.001) - Assertions.assertEquals(0.0, SafeParse.stringToDouble(" "), 0.001) - Assertions.assertEquals(0.0, SafeParse.stringToDouble("\n\r"), 0.001) + assertThat(SafeParse.stringToDouble("")).isWithin(0.001).of(0.0) + assertThat(SafeParse.stringToDouble(" ")).isWithin(0.001).of(0.0) + assertThat(SafeParse.stringToDouble("\n\r")).isWithin(0.001).of(0.0) } @Test fun stringToIntTest() { // correct values - Assertions.assertEquals(1052934, SafeParse.stringToInt("1052934")) - Assertions.assertEquals(-42, SafeParse.stringToInt("-42")) - Assertions.assertEquals(2147483647, SafeParse.stringToInt("2147483647")) - Assertions.assertEquals(-2147483648, SafeParse.stringToInt("-2147483648")) + assertThat(SafeParse.stringToInt("1052934")).isEqualTo(1052934) + assertThat(SafeParse.stringToInt("-42")).isEqualTo(-42) + assertThat(SafeParse.stringToInt("2147483647")).isEqualTo(2147483647) + assertThat(SafeParse.stringToInt("-2147483648")).isEqualTo(-2147483648) // outside Integer range - Assertions.assertEquals(0, SafeParse.stringToInt("2147483648")) - Assertions.assertEquals(0, SafeParse.stringToInt("-2147483649")) + assertThat(SafeParse.stringToInt("2147483648")).isEqualTo(0) + assertThat(SafeParse.stringToInt("-2147483649")).isEqualTo(0) // units or other extra values are not permitted - Assertions.assertEquals(0, SafeParse.stringToInt("12 U/h")) - Assertions.assertEquals(0, SafeParse.stringToInt("0.1234")) - Assertions.assertEquals(0, SafeParse.stringToInt("0,1234")) - Assertions.assertEquals(0, SafeParse.stringToInt(".5436564812")) - Assertions.assertEquals(0, SafeParse.stringToInt(",5436564812")) - Assertions.assertEquals(0, SafeParse.stringToInt("42.1234")) - Assertions.assertEquals(0, SafeParse.stringToInt("42,1234")) - Assertions.assertEquals(0, SafeParse.stringToInt("3212.5436564812")) - Assertions.assertEquals(0, SafeParse.stringToInt("3212,5436564812")) - Assertions.assertEquals(0, SafeParse.stringToInt("1000500100900")) + assertThat(SafeParse.stringToInt("12 U/h")).isEqualTo(0) + assertThat(SafeParse.stringToInt("0.1234")).isEqualTo(0) + assertThat(SafeParse.stringToInt("0,1234")).isEqualTo(0) + assertThat(SafeParse.stringToInt(".5436564812")).isEqualTo(0) + assertThat(SafeParse.stringToInt(",5436564812")).isEqualTo(0) + assertThat(SafeParse.stringToInt("42.1234")).isEqualTo(0) + assertThat(SafeParse.stringToInt("42,1234")).isEqualTo(0) + assertThat(SafeParse.stringToInt("3212.5436564812")).isEqualTo(0) + assertThat(SafeParse.stringToInt("3212,5436564812")).isEqualTo(0) + assertThat(SafeParse.stringToInt("1000500100900")).isEqualTo(0) // strings are not parsable - Assertions.assertEquals(0, SafeParse.stringToInt("ala ma kota")) + assertThat(SafeParse.stringToInt("ala ma kota")).isEqualTo(0) // various emptiness - Assertions.assertEquals(0, SafeParse.stringToInt("")) - Assertions.assertEquals(0, SafeParse.stringToInt(" ")) - Assertions.assertEquals(0, SafeParse.stringToInt("\n\r")) + assertThat(SafeParse.stringToInt("")).isEqualTo(0) + assertThat(SafeParse.stringToInt(" ")).isEqualTo(0) + assertThat(SafeParse.stringToInt("\n\r")).isEqualTo(0) } @Test fun stringToLongTest() { // correct values - Assertions.assertEquals(1052934L, SafeParse.stringToLong("1052934")) - Assertions.assertEquals(-42L, SafeParse.stringToLong("-42")) - Assertions.assertEquals(2147483647L, SafeParse.stringToLong("2147483647")) - Assertions.assertEquals(-2147483648L, SafeParse.stringToLong("-2147483648")) - Assertions.assertEquals(1000500100900L, SafeParse.stringToLong("1000500100900")) + assertThat(SafeParse.stringToLong("1052934")).isEqualTo(1052934L) + assertThat(SafeParse.stringToLong("-42")).isEqualTo(-42L) + assertThat(SafeParse.stringToLong("2147483647")).isEqualTo(2147483647L) + assertThat(SafeParse.stringToLong("-2147483648")).isEqualTo(-2147483648L) + assertThat(SafeParse.stringToLong("1000500100900")).isEqualTo(1000500100900L) // outside Integer range - Assertions.assertEquals(2147483648L, SafeParse.stringToLong("2147483648")) - Assertions.assertEquals(-2147483649L, SafeParse.stringToLong("-2147483649")) + assertThat(SafeParse.stringToLong("2147483648")).isEqualTo(2147483648L) + assertThat(SafeParse.stringToLong("-2147483649")).isEqualTo(-2147483649L) // units or other extra values are not permitted - Assertions.assertEquals(0L, SafeParse.stringToLong("12 U/h")) - Assertions.assertEquals(0L, SafeParse.stringToLong("0.1234")) - Assertions.assertEquals(0L, SafeParse.stringToLong("0,1234")) - Assertions.assertEquals(0L, SafeParse.stringToLong(".5436564812")) - Assertions.assertEquals(0L, SafeParse.stringToLong(",5436564812")) - Assertions.assertEquals(0L, SafeParse.stringToLong("42.1234")) - Assertions.assertEquals(0L, SafeParse.stringToLong("42,1234")) - Assertions.assertEquals(0L, SafeParse.stringToLong("3212.5436564812")) - Assertions.assertEquals(0L, SafeParse.stringToLong("3212,5436564812")) + assertThat(SafeParse.stringToLong("12 U/h")).isEqualTo(0L) + assertThat(SafeParse.stringToLong("0.1234")).isEqualTo(0L) + assertThat(SafeParse.stringToLong("0,1234")).isEqualTo(0L) + assertThat(SafeParse.stringToLong(".5436564812")).isEqualTo(0L) + assertThat(SafeParse.stringToLong(",5436564812")).isEqualTo(0L) + assertThat(SafeParse.stringToLong("42.1234")).isEqualTo(0L) + assertThat(SafeParse.stringToLong("42,1234")).isEqualTo(0L) + assertThat(SafeParse.stringToLong("3212.5436564812")).isEqualTo(0L) + assertThat(SafeParse.stringToLong("3212,5436564812")).isEqualTo(0L) // strings are not parsable - Assertions.assertEquals(0L, SafeParse.stringToLong("ala ma kota")) + assertThat(SafeParse.stringToLong("ala ma kota")).isEqualTo(0L) // various emptiness - Assertions.assertEquals(0L, SafeParse.stringToLong("")) - Assertions.assertEquals(0L, SafeParse.stringToLong(" ")) - Assertions.assertEquals(0L, SafeParse.stringToLong("\n\r")) + assertThat(SafeParse.stringToLong("")).isEqualTo(0L) + assertThat(SafeParse.stringToLong(" ")).isEqualTo(0L) + assertThat(SafeParse.stringToLong("\n\r")).isEqualTo(0L) } @Test fun stringToDoubleNullTest() { - Assertions.assertEquals(0.0, SafeParse.stringToDouble(null), 0.001) + assertThat(SafeParse.stringToDouble(null)).isWithin(0.001).of(0.0) } @Test fun stringToIntNullTest() { - Assertions.assertEquals(0, SafeParse.stringToInt(null)) + assertThat(SafeParse.stringToInt(null)).isEqualTo(0) } @Test fun stringToLongNullTest() { - Assertions.assertEquals(0L, SafeParse.stringToLong(null)) + assertThat(SafeParse.stringToLong(null)).isEqualTo(0L) } -} \ No newline at end of file +}