Skip to content

Commit

Permalink
feat: Add comprehensive tests for JsonUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
syxc committed Aug 25, 2024
1 parent 72b6336 commit 4576d67
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions library/src/test/java/com/ding1ding/jsbridge/JsonUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class JsonUtilsTest {
fun setup() {
}

// 测试基本数据类型的序列化
@Test
fun testToJsonPrimitives() {
assertEquals("null", JsonUtils.toJson(null))
Expand All @@ -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(
Expand Down Expand Up @@ -71,6 +74,7 @@ class JsonUtilsTest {
assertEquals(originalArray, resultArray)
}

// 测试集合的序列化
@Test
fun testToJsonCollection() {
val list = listOf("a", 1, true)
Expand All @@ -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)
Expand All @@ -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}"
Expand All @@ -105,6 +113,7 @@ class JsonUtilsTest {
assertEquals(42, result["number"])
}

// 测试 JSON 数组的反序列化
@Test
fun testFromJsonArray() {
val json = "[\"a\",1,true]"
Expand All @@ -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"))
Expand All @@ -133,13 +145,15 @@ class JsonUtilsTest {
assertEquals(BigInteger("9223372036854775808"), JsonUtils.fromJson("9223372036854775808"))
}

// 测试日期的反序列化
@Test
fun testFromJsonDate() {
val dateString = "\"2021-01-01T00:00:00.000Z\""
val result = JsonUtils.fromJson(dateString) as Date
assertEquals(1609459200000, result.time)
}

// 测试复杂对象的序列化和反序列化一致性
@Test
fun testRoundTripConversion() {
val original = mapOf(
Expand Down Expand Up @@ -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<Int>)

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<Any>(),
"emptyMap" to emptyMap<Any, Any>(),
)
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"))
}
}

0 comments on commit 4576d67

Please sign in to comment.