summaryrefslogtreecommitdiff
path: root/formats/json-tests
diff options
context:
space:
mode:
authorSergey Shanshin <sergey.shanshin@jetbrains.com>2022-09-22 20:53:25 +0300
committerGitHub <noreply@github.com>2022-09-22 20:53:25 +0300
commite311eb8f229ef3af494a7e9281dd667122f244a9 (patch)
treea69df0a3b5e1a4d4b9d4ef3d30750257ec7c9ed0 /formats/json-tests
parent9658ea77b6acba798f7ea614aa27406c7fa20800 (diff)
downloadkotlinx.serialization-e311eb8f229ef3af494a7e9281dd667122f244a9.tar.gz
Added support for the unsigned primitives and arrays as built-in
Fixes #1745 Fixes #1480 Resolves #1989
Diffstat (limited to 'formats/json-tests')
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/SerializersLookupTest.kt15
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/features/inline/UnsignedIntegersTest.kt83
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonTestBase.kt18
3 files changed, 116 insertions, 0 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/SerializersLookupTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/SerializersLookupTest.kt
index c5dcf201..95b458c9 100644
--- a/formats/json-tests/commonTest/src/kotlinx/serialization/SerializersLookupTest.kt
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/SerializersLookupTest.kt
@@ -52,6 +52,21 @@ class SerializersLookupTest : JsonTestBase() {
assertSerializedWithType("""["a","b","c"]""", myArr)
}
+ @Test
+ fun testUnsigned() = noLegacyJs {
+ assertSame(UByte.serializer(), serializer<UByte>())
+ assertSame(UShort.serializer(), serializer<UShort>())
+ assertSame(UInt.serializer(), serializer<UInt>())
+ assertSame(ULong.serializer(), serializer<ULong>())
+ }
+ @Test
+ @OptIn(ExperimentalUnsignedTypes::class)
+ fun testUnsignedArrays() {
+ assertSame(UByteArraySerializer(), serializer<UByteArray>())
+ assertSame(UShortArraySerializer(), serializer<UShortArray>())
+ assertSame(UIntArraySerializer(), serializer<UIntArray>())
+ assertSame(ULongArraySerializer(), serializer<ULongArray>())
+ }
@Test
fun testPrimitiveSet() {
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/features/inline/UnsignedIntegersTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/features/inline/UnsignedIntegersTest.kt
index 3611fc27..c01d1540 100644
--- a/formats/json-tests/commonTest/src/kotlinx/serialization/features/inline/UnsignedIntegersTest.kt
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/features/inline/UnsignedIntegersTest.kt
@@ -5,6 +5,7 @@
package kotlinx.serialization.features.inline
import kotlinx.serialization.*
+import kotlinx.serialization.builtins.*
import kotlinx.serialization.json.*
import kotlin.test.*
@@ -20,6 +21,37 @@ class UnsignedIntegersTest : JsonTestBase() {
val double: Double
)
+// TODO uncomment when Kotlin 1.8.0 is released
+// @Serializable
+// data class UnsignedArrays(
+// val uByte: UByteArray,
+// val uShort: UShortArray,
+// val uInt: UIntArray,
+// val uLong: ULongArray
+// ) {
+// override fun equals(other: Any?): Boolean {
+// if (this === other) return true
+// if (other == null || this::class != other::class) return false
+//
+// other as UnsignedArrays
+//
+// if (!uByte.contentEquals(other.uByte)) return false
+// if (!uShort.contentEquals(other.uShort)) return false
+// if (!uInt.contentEquals(other.uInt)) return false
+// if (!uLong.contentEquals(other.uLong)) return false
+//
+// return true
+// }
+//
+// override fun hashCode(): Int {
+// var result = uByte.contentHashCode()
+// result = 31 * result + uShort.contentHashCode()
+// result = 31 * result + uInt.contentHashCode()
+// result = 31 * result + uLong.contentHashCode()
+// return result
+// }
+// }
+
@Serializable
data class UnsignedWithoutLong(val uInt: UInt, val uByte: UByte, val uShort: UShort)
@@ -54,4 +86,55 @@ class UnsignedIntegersTest : JsonTestBase() {
"""{"uInt":2147483657,"uByte":239,"uShort":65000}""",
)
}
+
+ @Test
+ fun testRoot() {
+ assertJsonFormAndRestored(UByte.serializer(), 220U, "220")
+ assertJsonFormAndRestored(UShort.serializer(), 65000U, "65000")
+ assertJsonFormAndRestored(UInt.serializer(), 2147483657U, "2147483657")
+ assertJsonFormAndRestored(ULong.serializer(), 9223372036854775817U, "9223372036854775817")
+ }
+
+ @OptIn(ExperimentalUnsignedTypes::class)
+ @Test
+ fun testRootArrays() = parametrizedTest {
+ assertJsonFormAndRestoredCustom(
+ UByteArraySerializer(),
+ ubyteArrayOf(1U, 220U),
+ "[1,220]"
+ ) { l, r -> l.contentEquals(r) }
+
+ assertJsonFormAndRestoredCustom(
+ UShortArraySerializer(),
+ ushortArrayOf(1U, 65000U),
+ "[1,65000]"
+ ) { l, r -> l.contentEquals(r) }
+
+ assertJsonFormAndRestoredCustom(
+ UIntArraySerializer(),
+ uintArrayOf(1U, 2147483657U),
+ "[1,2147483657]"
+ ) { l, r -> l.contentEquals(r) }
+
+ assertJsonFormAndRestoredCustom(
+ ULongArraySerializer(),
+ ulongArrayOf(1U, 9223372036854775817U),
+ "[1,9223372036854775817]"
+ ) { l, r -> l.contentEquals(r) }
+ }
+
+// TODO uncomment when Kotlin 1.8.0 is released
+// @OptIn(ExperimentalUnsignedTypes::class)
+// fun testArrays() {
+// val data = UnsignedArrays(
+// ubyteArrayOf(1U, 220U),
+// ushortArrayOf(1U, 65000U),
+// uintArrayOf(1U, 2147483657U),
+// ulongArrayOf(1U, 9223372036854775817U)
+// )
+// val json = """{"uByte":[1,220],uShort:[1,65000],uInt:[1,2147483657],uLong:[1,9223372036854775817]}"""
+//
+// assertJsonFormAndRestored(UnsignedArrays.serializer(), data, json)
+// }
+
}
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonTestBase.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonTestBase.kt
index 59726d41..ebcc3d07 100644
--- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonTestBase.kt
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonTestBase.kt
@@ -13,6 +13,7 @@ import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.test.*
import kotlin.test.assertEquals
import okio.*
+import kotlin.test.assertTrue
enum class JsonTestingMode {
@@ -155,4 +156,21 @@ abstract class JsonTestBase {
assertEquals(data, deserialized, "Failed with streaming = $jsonTestingMode")
}
}
+ /**
+ * Same as [assertStringFormAndRestored], but tests both json converters (streaming and tree)
+ * via [parametrizedTest]. Use custom checker for deserialized value.
+ */
+ internal fun <T> assertJsonFormAndRestoredCustom(
+ serializer: KSerializer<T>,
+ data: T,
+ expected: String,
+ check: (T, T) -> Boolean
+ ) {
+ parametrizedTest { jsonTestingMode ->
+ val serialized = Json.encodeToString(serializer, data, jsonTestingMode)
+ assertEquals(expected, serialized, "Failed with streaming = $jsonTestingMode")
+ val deserialized: T = Json.decodeFromString(serializer, serialized, jsonTestingMode)
+ assertTrue("Failed with streaming = $jsonTestingMode\n\tsource value =$data\n\tdeserialized value=$deserialized") { check(data, deserialized) }
+ }
+ }
}