parent
107f6d05dd
commit
e793620b6f
|
@ -1,7 +1,7 @@
|
|||
package info.nightscout.core.utils
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.json.JSONObject
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
@Suppress("SpellCheckingInspection")
|
||||
|
@ -13,67 +13,67 @@ class JsonHelperTest {
|
|||
fun safeGetObjectTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
val o = Any()
|
||||
Assertions.assertEquals(o, JsonHelper.safeGetObject(null, "x", o))
|
||||
Assertions.assertEquals(o, JsonHelper.safeGetObject(json, "x", o))
|
||||
Assertions.assertNotEquals(o, JsonHelper.safeGetObject(json, "d", o))
|
||||
assertThat(JsonHelper.safeGetObject(null, "x", o)).isEqualTo(o)
|
||||
assertThat(JsonHelper.safeGetObject(json, "x", o)).isEqualTo(o)
|
||||
assertThat(JsonHelper.safeGetObject(json, "d", o)).isNotEqualTo(o)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetJSONObjectTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
val o = JSONObject()
|
||||
Assertions.assertEquals(o, JsonHelper.safeGetJSONObject(null, "x", o))
|
||||
Assertions.assertTrue(JsonHelper.safeGetJSONObject(json, "j", o)!!.has("a"))
|
||||
Assertions.assertEquals(o, JsonHelper.safeGetJSONObject(json, "d", o))
|
||||
assertThat(JsonHelper.safeGetJSONObject(null, "x", o)).isEqualTo(o)
|
||||
assertThat(JsonHelper.safeGetJSONObject(json, "j", o)!!.has("a")).isTrue()
|
||||
assertThat(JsonHelper.safeGetJSONObject(json, "d", o)).isEqualTo(o)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetStringTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assertions.assertNull(JsonHelper.safeGetString(null, "s"))
|
||||
Assertions.assertNull(JsonHelper.safeGetString(json, "notexisting"))
|
||||
Assertions.assertEquals("5", JsonHelper.safeGetString(json, "s"))
|
||||
Assertions.assertEquals("default", JsonHelper.safeGetString(null, "notexisting", "default"))
|
||||
Assertions.assertEquals("default", JsonHelper.safeGetString(json, "notexisting", "default"))
|
||||
Assertions.assertEquals("5", JsonHelper.safeGetString(json, "s", "default"))
|
||||
Assertions.assertEquals("default", JsonHelper.safeGetStringAllowNull(null, "notexisting", "default"))
|
||||
Assertions.assertEquals("default", JsonHelper.safeGetStringAllowNull(json, "notexisting", "default"))
|
||||
Assertions.assertNull(JsonHelper.safeGetStringAllowNull(json, "notexisting", null))
|
||||
Assertions.assertEquals("5", JsonHelper.safeGetStringAllowNull(json, "s", "default"))
|
||||
assertThat(JsonHelper.safeGetString(null, "s")).isNull()
|
||||
assertThat(JsonHelper.safeGetString(json, "notexisting")).isNull()
|
||||
assertThat(JsonHelper.safeGetString(json, "s")).isEqualTo("5")
|
||||
assertThat(JsonHelper.safeGetString(null, "notexisting", "default")).isEqualTo("default")
|
||||
assertThat(JsonHelper.safeGetString(json, "notexisting", "default")).isEqualTo("default")
|
||||
assertThat(JsonHelper.safeGetString(json, "s", "default")).isEqualTo("5")
|
||||
assertThat(JsonHelper.safeGetStringAllowNull(null, "notexisting", "default")).isEqualTo("default")
|
||||
assertThat(JsonHelper.safeGetStringAllowNull(json, "notexisting", "default")).isEqualTo("default")
|
||||
assertThat(JsonHelper.safeGetStringAllowNull(json, "notexisting", null)).isNull()
|
||||
assertThat(JsonHelper.safeGetStringAllowNull(json, "s", "default")).isEqualTo("5")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetDoubleTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assertions.assertEquals(0.0, JsonHelper.safeGetDouble(json, "notexisting"), 0.0)
|
||||
Assertions.assertEquals(0.0, JsonHelper.safeGetDouble(null, "notexisting"), 0.0)
|
||||
Assertions.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d"), 0.000001)
|
||||
Assertions.assertEquals(6.0, JsonHelper.safeGetDouble(null, "notexisting", 6.0), 0.0)
|
||||
Assertions.assertEquals(6.0, JsonHelper.safeGetDouble(json, "notexisting", 6.0), 0.0)
|
||||
Assertions.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d", 6.0), 0.0)
|
||||
assertThat(JsonHelper.safeGetDouble(json, "notexisting")).isWithin(0.0).of(0.0)
|
||||
assertThat(JsonHelper.safeGetDouble(null, "notexisting")).isWithin(0.0).of(0.0)
|
||||
assertThat(JsonHelper.safeGetDouble(json, "d")).isWithin(0.000001).of(3.0)
|
||||
assertThat(JsonHelper.safeGetDouble(null, "notexisting", 6.0)).isWithin(0.0).of(6.0)
|
||||
assertThat(JsonHelper.safeGetDouble(json, "notexisting", 6.0)).isWithin(0.0).of(6.0)
|
||||
assertThat(JsonHelper.safeGetDouble(json, "d", 6.0)).isWithin(0.0).of(3.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetLntTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assertions.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong())
|
||||
Assertions.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong())
|
||||
Assertions.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong())
|
||||
assertThat(JsonHelper.safeGetInt(null, "notexisting").toLong()).isEqualTo(0)
|
||||
assertThat(JsonHelper.safeGetInt(json, "notexisting").toLong()).isEqualTo(0)
|
||||
assertThat(JsonHelper.safeGetInt(json, "i").toLong()).isEqualTo(4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetLongTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assertions.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong())
|
||||
Assertions.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong())
|
||||
Assertions.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong())
|
||||
assertThat(JsonHelper.safeGetInt(null, "notexisting").toLong()).isEqualTo(0)
|
||||
assertThat(JsonHelper.safeGetInt(json, "notexisting").toLong()).isEqualTo(0)
|
||||
assertThat(JsonHelper.safeGetInt(json, "i").toLong()).isEqualTo(4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetBooleanTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assertions.assertFalse(JsonHelper.safeGetBoolean(null, "notexisting"))
|
||||
Assertions.assertFalse(JsonHelper.safeGetBoolean(json, "notexisting"))
|
||||
Assertions.assertTrue(JsonHelper.safeGetBoolean(json, "b"))
|
||||
assertThat(JsonHelper.safeGetBoolean(null, "notexisting")).isFalse()
|
||||
assertThat(JsonHelper.safeGetBoolean(json, "notexisting")).isFalse()
|
||||
assertThat(JsonHelper.safeGetBoolean(json, "b")).isTrue()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue