summaryrefslogtreecommitdiff
path: root/formats/json-tests
diff options
context:
space:
mode:
authoraSemy <897017+aSemy@users.noreply.github.com>2023-03-01 16:50:49 +0100
committerGitHub <noreply@github.com>2023-03-01 18:50:49 +0300
commit6246e0abf7bac21b45665dbf2aa6f67fc56f8606 (patch)
tree8828d895a02fc5dc69e0719481e51851c44c5b15 /formats/json-tests
parentcbd8ce3dc35c86805b94503ff7b1cfd7f0d3df7f (diff)
downloadkotlinx.serialization-6246e0abf7bac21b45665dbf2aa6f67fc56f8606.tar.gz
Add functions for creating JsonPrimitives from unsigned numbers (#2160)
Diffstat (limited to 'formats/json-tests')
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/json/serializers/JsonPrimitiveSerializerTest.kt80
1 files changed, 80 insertions, 0 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/serializers/JsonPrimitiveSerializerTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/serializers/JsonPrimitiveSerializerTest.kt
index 6f26d2ab..72f8a4fb 100644
--- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/serializers/JsonPrimitiveSerializerTest.kt
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/serializers/JsonPrimitiveSerializerTest.kt
@@ -121,4 +121,84 @@ class JsonPrimitiveSerializerTest : JsonTestBase() {
assertEquals(string, jvmExpectedString)
}
}
+
+ /**
+ * Helper function for [testJsonPrimitiveUnsignedNumbers]
+ *
+ * Asserts that an [unsigned number][actual] can be used to create a [JsonPrimitive][actualPrimitive],
+ * which can be decoded correctly.
+ *
+ * @param expected the expected string value of [actual]
+ * @param actual the unsigned number
+ * @param T should be an unsigned number
+ */
+ private inline fun <reified T> assertUnsignedNumberEncoding(
+ expected: String,
+ actual: T,
+ actualPrimitive: JsonPrimitive,
+ ) {
+ assertEquals(
+ expected,
+ actualPrimitive.toString(),
+ "expect ${T::class.simpleName} $actual can be used to create a JsonPrimitive"
+ )
+
+ parametrizedTest { mode ->
+ assertEquals(
+ expected,
+ default.encodeToString(JsonElement.serializer(), actualPrimitive, mode),
+ "expect ${T::class.simpleName} primitive can be decoded",
+ )
+ }
+ }
+
+ @Test
+ fun testJsonPrimitiveUnsignedNumbers() {
+
+ val expectedActualUBytes: Map<String, UByte> = mapOf(
+ "0" to 0u,
+ "1" to 1u,
+ "255" to UByte.MAX_VALUE,
+ )
+
+ expectedActualUBytes.forEach { (expected, actual) ->
+ assertUnsignedNumberEncoding(expected, actual, JsonPrimitive(actual))
+ }
+
+ val expectedActualUShorts: Map<String, UShort> = mapOf(
+ "0" to 0u,
+ "1" to 1u,
+ "255" to UByte.MAX_VALUE.toUShort(),
+ "65535" to UShort.MAX_VALUE,
+ )
+
+ expectedActualUShorts.forEach { (expected, actual) ->
+ assertUnsignedNumberEncoding(expected, actual, JsonPrimitive(actual))
+ }
+
+ val expectedActualUInts: Map<String, UInt> = mapOf(
+ "0" to 0u,
+ "1" to 1u,
+ "255" to UByte.MAX_VALUE.toUInt(),
+ "65535" to UShort.MAX_VALUE.toUInt(),
+ "4294967295" to UInt.MAX_VALUE,
+ )
+
+ expectedActualUInts.forEach { (expected, actual) ->
+ assertUnsignedNumberEncoding(expected, actual, JsonPrimitive(actual))
+ }
+
+ val expectedActualULongs: Map<String, ULong> = mapOf(
+ "0" to 0u,
+ "1" to 1u,
+ "255" to UByte.MAX_VALUE.toULong(),
+ "65535" to UShort.MAX_VALUE.toULong(),
+ "4294967295" to UInt.MAX_VALUE.toULong(),
+ "18446744073709551615" to ULong.MAX_VALUE,
+ )
+
+ expectedActualULongs.forEach { (expected, actual) ->
+ assertUnsignedNumberEncoding(expected, actual, JsonPrimitive(actual))
+ }
+ }
}