Rewrites SafeParseTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-19 00:06:53 -07:00
parent 8664bc7170
commit 3774ba38b4

View file

@ -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)
}
}
}