From 4576d67efc6890b5c2d2c58341cfb4f2f363523a Mon Sep 17 00:00:00 2001 From: nian1 Date: Mon, 26 Aug 2024 04:46:01 +0800 Subject: [PATCH] feat: Add comprehensive tests for JsonUtils --- .../com/ding1ding/jsbridge/JsonUtilsTest.kt | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/library/src/test/java/com/ding1ding/jsbridge/JsonUtilsTest.kt b/library/src/test/java/com/ding1ding/jsbridge/JsonUtilsTest.kt index 83b9107..32f6ad3 100644 --- a/library/src/test/java/com/ding1ding/jsbridge/JsonUtilsTest.kt +++ b/library/src/test/java/com/ding1ding/jsbridge/JsonUtilsTest.kt @@ -20,6 +20,7 @@ class JsonUtilsTest { fun setup() { } + // 测试基本数据类型的序列化 @Test fun testToJsonPrimitives() { assertEquals("null", JsonUtils.toJson(null)) @@ -30,12 +31,14 @@ class JsonUtilsTest { assertEquals("3.14", JsonUtils.toJson(3.14)) } + // 测试日期的序列化 @Test fun testToJsonDate() { val date = Date(1609459200000) // 2021-01-01T00:00:00.000Z assertEquals("\"2021-01-01T00:00:00.000Z\"", JsonUtils.toJson(date)) } + // 测试复杂 Map 的序列化和反序列化 @Test fun testToJsonMap() { val original = mapOf( @@ -71,6 +74,7 @@ class JsonUtilsTest { assertEquals(originalArray, resultArray) } + // 测试集合的序列化 @Test fun testToJsonCollection() { val list = listOf("a", 1, true) @@ -79,11 +83,13 @@ class JsonUtilsTest { enum class TestEnum { VALUE } + // 测试枚举的序列化 @Test fun testToJsonEnum() { assertEquals("\"VALUE\"", JsonUtils.toJson(TestEnum.VALUE)) } + // 测试自定义对象的序列化 @Test fun testToJsonCustomObject() { data class TestObject(val name: String, val age: Int) @@ -92,11 +98,13 @@ class JsonUtilsTest { assertEquals("{\"name\":\"John\",\"age\":30}", JsonUtils.toJson(obj)) } + // 测试 null 值的反序列化 @Test fun testFromJsonNull() { assertNull(JsonUtils.fromJson("null")) } + // 测试 JSON 对象的反序列化 @Test fun testFromJsonObject() { val json = "{\"key\":\"value\",\"number\":42}" @@ -105,6 +113,7 @@ class JsonUtilsTest { assertEquals(42, result["number"]) } + // 测试 JSON 数组的反序列化 @Test fun testFromJsonArray() { val json = "[\"a\",1,true]" @@ -114,17 +123,20 @@ class JsonUtilsTest { assertEquals(true, result[2]) } + // 测试字符串的反序列化 @Test fun testFromJsonString() { assertEquals("hello", JsonUtils.fromJson("\"hello\"")) } + // 测试布尔值的反序列化 @Test fun testFromJsonBoolean() { assertEquals(true, JsonUtils.fromJson("true")) assertEquals(false, JsonUtils.fromJson("false")) } + // 测试数字的反序列化 @Test fun testFromJsonNumber() { assertEquals(42, JsonUtils.fromJson("42")) @@ -133,6 +145,7 @@ class JsonUtilsTest { assertEquals(BigInteger("9223372036854775808"), JsonUtils.fromJson("9223372036854775808")) } + // 测试日期的反序列化 @Test fun testFromJsonDate() { val dateString = "\"2021-01-01T00:00:00.000Z\"" @@ -140,6 +153,7 @@ class JsonUtilsTest { assertEquals(1609459200000, result.time) } + // 测试复杂对象的序列化和反序列化一致性 @Test fun testRoundTripConversion() { val original = mapOf( @@ -182,9 +196,114 @@ class JsonUtilsTest { } } + // 测试无效 JSON 的处理 @Test fun testParseInvalidJson() { val invalidJson = "invalid json" assertEquals(invalidJson, JsonUtils.fromJson(invalidJson)) } + + // 测试复杂嵌套对象的序列化和反序列化 + @Test + fun testComplexNestedObject() { + data class Inner(val value: String) + data class Outer(val inner: Inner, val list: List) + + val complex = Outer(Inner("nested"), listOf(1, 2, 3)) + val json = JsonUtils.toJson(complex) + val result = JsonUtils.fromJson(json) as? Map<*, *> + + assertNotNull(result) + val innerMap = result?.get("inner") as? Map<*, *> + assertNotNull(innerMap) + assertEquals("nested", innerMap?.get("value")) + assertEquals(listOf(1, 2, 3), result?.get("list")) + } + + // 测试空集合和空 Map 的处理 + @Test + fun testEmptyCollectionsAndMaps() { + val empty = mapOf( + "emptyList" to emptyList(), + "emptyMap" to emptyMap(), + ) + val json = JsonUtils.toJson(empty) + val result = JsonUtils.fromJson(json) as? Map<*, *> + + assertNotNull(result) + assertTrue((result?.get("emptyList") as? List<*>)?.isEmpty() == true) + assertTrue((result?.get("emptyMap") as? Map<*, *>)?.isEmpty() == true) + } + + // 测试特殊字符的处理 + @Test + fun testSpecialCharacters() { + val special = "Hello\n\t\r\b\u000c\u0001World" + val json = JsonUtils.toJson(special) + println("JSON: $json") + val result = JsonUtils.fromJson(json) + + assertEquals(special, result) + assertEquals("\"Hello\\n\\t\\r\\b\\f\\u0001World\"", json) + } + + // 测试超大数字的处理 + @Test + fun testVeryLargeNumbers() { + val veryLarge = "9223372036854775808" // Long.MAX_VALUE + 1 + val json = JsonUtils.toJson(BigInteger(veryLarge)) + val result = JsonUtils.fromJson(json) + + assertTrue(result is BigInteger) + assertEquals(BigInteger(veryLarge), result) + } + + // 测试浮点数精度 + @Test + fun testFloatingPointPrecision() { + val precise = 1.23456789012345 + val json = JsonUtils.toJson(precise) + val result = JsonUtils.fromJson(json) + + assertTrue(result is Double) + assertEquals(precise, result as Double, 1e-15) + } + + // 测试 Unicode 字符的处理 + @Test + fun testUnicodeCharacters() { + val unicode = "Hello, 世界 ! 🌍 " + val json = JsonUtils.toJson(unicode) + val result = JsonUtils.fromJson(json) + + assertEquals(unicode, result) + } + + // 测试集合中 null 值的处理 + @Test + fun testNullValuesInCollections() { + val withNulls = listOf(1, null, "three", null) + val json = JsonUtils.toJson(withNulls) + val result = JsonUtils.fromJson(json) as? List<*> + + assertNotNull(result) + assertEquals(withNulls, result) + } + + // 测试非字符串键的 Map 处理 + @Test + fun testMapWithNonStringKeys() { + val map = mapOf( + 1 to "one", + 2.5 to "two point five", + true to "boolean", + ) + val json = JsonUtils.toJson(map) + val result = JsonUtils.fromJson(json) as? Map<*, *> + + assertNotNull(result) + assertEquals("one", result?.get("1")) + assertEquals("two point five", result?.get("2.5")) + assertEquals("boolean", result?.get("true")) + } }