diff options
author | igoriakovlev <54274820+igoriakovlev@users.noreply.github.com> | 2023-09-12 15:54:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-12 15:54:16 +0200 |
commit | 7ae7a2313828470118e7ee294a424941ac60a409 (patch) | |
tree | 55d10d20eeb080703f4b21799aea5d9b49876590 /formats/json-tests | |
parent | f4c3a70f23a7f3932a105064d26e29085a560c65 (diff) | |
download | kotlinx.serialization-7ae7a2313828470118e7ee294a424941ac60a409.tar.gz |
Add support of WASM target to mainline (#2410)
* Implement wasm target
* Slightly fix test data to suite wasm fp parser
Wasm fp parser has a different algorithm, so fix the tests to have similar results for all platforms
* Update okio version to 3.5.0
* Kotlin 1.9.30+ support
Diffstat (limited to 'formats/json-tests')
7 files changed, 94 insertions, 11 deletions
diff --git a/formats/json-tests/build.gradle.kts b/formats/json-tests/build.gradle.kts index 9ae4247b..95b9e1c0 100644 --- a/formats/json-tests/build.gradle.kts +++ b/formats/json-tests/build.gradle.kts @@ -2,7 +2,7 @@ * Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ import Java9Modularity.configureJava9ModuleInfo -import org.jetbrains.kotlin.gradle.tasks.* +import org.jetbrains.kotlin.gradle.targets.js.testing.* plugins { kotlin("multiplatform") @@ -43,3 +43,25 @@ kotlin { } project.configureJava9ModuleInfo() + +// Right now it is used for conditional support of kotlin 1.9.0 and 1.9.20+ +// TODO: Remove this after okio will be updated to the version with 1.9.20 stdlib dependency +val kotlin_version: String by project +val isNewWasmTargetEnabled = isKotlinVersionAtLeast(kotlin_version, 1, 9, 20) +if (isNewWasmTargetEnabled) { + configurations.all { + resolutionStrategy.eachDependency { + if (requested.name == "kotlin-stdlib-wasm") { + useTarget("org.jetbrains.kotlin:kotlin-stdlib-wasm-js:${requested.version}") + } + } + } +} + +// TODO: Remove this after default kotlin will be updated to 1.9.20 +// https://youtrack.jetbrains.com/issue/KT-60212 +if (!isNewWasmTargetEnabled) { + tasks.named("wasmD8Test", KotlinJsTest::class) { + filter.excludePatterns += "kotlinx.serialization.features.EmojiTest" + } +}
\ No newline at end of file diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/UmbrellaTypes.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/UmbrellaTypes.kt index 52ab0f27..f878c633 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/UmbrellaTypes.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/UmbrellaTypes.kt @@ -84,3 +84,24 @@ val umbrellaInstance = TypesUmbrella( arrayOf(IntData(1), IntData(2)) ) ) + +val umbrellaInstance2 = TypesUmbrella( + Unit, true, 10, 20, 30, 40, 50.5f, 60.5, 'A', "Str0", Attitude.POSITIVE, IntData(70), + null, null, 11, 21, 31, 41, 51.5f, 61.5, 'B', "Str1", Attitude.NEUTRAL, null, + listOf(1, 2, 3), + listOf(4, 5, null), + setOf(6, 7, 8), + mutableSetOf(null, 9, 10), + listOf(listOf(Attitude.NEGATIVE, null)), + listOf(IntData(1), IntData(2), IntData(3)), + mutableListOf(IntData(1), null, IntData(3)), + Tree("root", Tree("left"), Tree("right", Tree("right.left"), Tree("right.right"))), + mapOf("one" to 1, "two" to 2, "three" to 3), + mapOf(0 to null, 1 to "first", 2 to "second"), + ArraysUmbrella( + arrayOf(1, 2, 3), + arrayOf(100, 200, 300), + arrayOf(null, -1, -2), + arrayOf(IntData(1), IntData(2)) + ) +) diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphicOnClassesTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphicOnClassesTest.kt index ea59f32a..77004dbc 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphicOnClassesTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphicOnClassesTest.kt @@ -136,7 +136,7 @@ class PolymorphicOnClassesTest { fun testSerializerLookupForInterface() { // On JVM and JS IR it can be supported via reflection/runtime hacks // on Native, unfortunately, only with intrinsics. - if (isNative()) return + if (isNative() || isWasm()) return val msgSer = serializer<IMessage>() assertEquals(IMessage::class, (msgSer as AbstractPolymorphicSerializer).baseClass) } diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/BasicTypesSerializationTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/BasicTypesSerializationTest.kt index dee87fd6..4959b7e2 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/BasicTypesSerializationTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/BasicTypesSerializationTest.kt @@ -5,24 +5,37 @@ package kotlinx.serialization.json import kotlinx.serialization.* +import kotlinx.serialization.test.* import kotlin.test.* class BasicTypesSerializationTest : JsonTestBase() { val goldenValue = """ - {"unit":{},"boolean":true,"byte":10,"short":20,"int":30,"long":40,"float":50.1,"double":60.1,"char":"A","string":"Str0","enum":"POSITIVE","intData":{"intV":70},"unitN":null,"booleanN":null,"byteN":11,"shortN":21,"intN":31,"longN":41,"floatN":51.1,"doubleN":61.1,"charN":"B","stringN":"Str1","enumN":"NEUTRAL","intDataN":null,"listInt":[1,2,3],"listIntN":[4,5,null],"listNInt":[6,7,8],"listNIntN":[null,9,10],"listListEnumN":[["NEGATIVE",null]],"listIntData":[{"intV":1},{"intV":2},{"intV":3}],"listIntDataN":[{"intV":1},null,{"intV":3}],"tree":{"name":"root","left":{"name":"left","left":null,"right":null},"right":{"name":"right","left":{"name":"right.left","left":null,"right":null},"right":{"name":"right.right","left":null,"right":null}}},"mapStringInt":{"one":1,"two":2,"three":3},"mapIntStringN":{"0":null,"1":"first","2":"second"},"arrays":{"arrByte":[1,2,3],"arrInt":[100,200,300],"arrIntN":[null,-1,-2],"arrIntData":[{"intV":1},{"intV":2}]}} + {"unit":{},"boolean":true,"byte":10,"short":20,"int":30,"long":40,"float":50.1,"double":60.1,"char":"A","string":"Str0","enum":"POSITIVE","intData":{"intV":70},"unitN":null,"booleanN":null,"byteN":11,"shortN":21,"intN":31,"longN":41,"floatN":51.1,"doubleN":61.1,"charN":"B","stringN":"Str1","enumN":"NEUTRAL","intDataN":null,"listInt":[1,2,3],"listIntN":[4,5,null],"listNInt":[6,7,8],"listNIntN":[null,9,10],"listListEnumN":[["NEGATIVE",null]],"listIntData":[{"intV":1},{"intV":2},{"intV":3}],"listIntDataN":[{"intV":1},null,{"intV":3}],"tree":{"name":"root","left":{"name":"left","left":null,"right":null},"right":{"name":"right","left":{"name":"right.left","left":null,"right":null},"right":{"name":"right.right","left":null,"right":null}}},"mapStringInt":{"one":1,"two":2,"three":3},"mapIntStringN":{"0":null,"1":"first","2":"second"},"arrays":{"arrByte":[1,2,3],"arrInt":[100,200,300],"arrIntN":[null,-1,-2],"arrIntData":[{"intV":1},{"intV":2}]}} """.trimIndent() - @Test - fun testSerialization() = parametrizedTest { jsonTestingMode -> - val json = default.encodeToString(TypesUmbrella.serializer(), umbrellaInstance) + val goldenValue2 = """ + {"unit":{},"boolean":true,"byte":10,"short":20,"int":30,"long":40,"float":50.5,"double":60.5,"char":"A","string":"Str0","enum":"POSITIVE","intData":{"intV":70},"unitN":null,"booleanN":null,"byteN":11,"shortN":21,"intN":31,"longN":41,"floatN":51.5,"doubleN":61.5,"charN":"B","stringN":"Str1","enumN":"NEUTRAL","intDataN":null,"listInt":[1,2,3],"listIntN":[4,5,null],"listNInt":[6,7,8],"listNIntN":[null,9,10],"listListEnumN":[["NEGATIVE",null]],"listIntData":[{"intV":1},{"intV":2},{"intV":3}],"listIntDataN":[{"intV":1},null,{"intV":3}],"tree":{"name":"root","left":{"name":"left","left":null,"right":null},"right":{"name":"right","left":{"name":"right.left","left":null,"right":null},"right":{"name":"right.right","left":null,"right":null}}},"mapStringInt":{"one":1,"two":2,"three":3},"mapIntStringN":{"0":null,"1":"first","2":"second"},"arrays":{"arrByte":[1,2,3],"arrInt":[100,200,300],"arrIntN":[null,-1,-2],"arrIntData":[{"intV":1},{"intV":2}]}} + """.trimIndent() + + private fun testSerializationImpl(typesUmbrella: TypesUmbrella, goldenValue: String) = parametrizedTest { jsonTestingMode -> + val json = default.encodeToString(TypesUmbrella.serializer(), typesUmbrella) assertEquals(goldenValue, json) val instance = default.decodeFromString(TypesUmbrella.serializer(), json, jsonTestingMode) - assertEquals(umbrellaInstance, instance) - assertNotSame(umbrellaInstance, instance) + assertEquals(typesUmbrella, instance) + assertNotSame(typesUmbrella, instance) + } + + @Test + fun testSerialization() { + if (isWasm()) return //https://youtrack.jetbrains.com/issue/KT-59118/WASM-floating-point-toString-inconsistencies + testSerializationImpl(umbrellaInstance, goldenValue) } @Test + fun testSerialization2() = testSerializationImpl(umbrellaInstance2, goldenValue2) + + @Test fun testTopLevelPrimitive() = parametrizedTest { jsonTestingMode -> testPrimitive(Unit, "{}", jsonTestingMode) testPrimitive(false, "false", jsonTestingMode) @@ -30,8 +43,8 @@ class BasicTypesSerializationTest : JsonTestBase() { testPrimitive(2.toShort(), "2", jsonTestingMode) testPrimitive(3, "3", jsonTestingMode) testPrimitive(4L, "4", jsonTestingMode) - testPrimitive(5.1f, "5.1", jsonTestingMode) - testPrimitive(6.1, "6.1", jsonTestingMode) + testPrimitive(2.5f, "2.5", jsonTestingMode) + testPrimitive(3.5, "3.5", jsonTestingMode) testPrimitive('c', "\"c\"", jsonTestingMode) testPrimitive("string", "\"string\"", jsonTestingMode) } diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/test/CurrentPlatform.common.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/test/CurrentPlatform.common.kt index 92cf087f..8c3633b4 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/test/CurrentPlatform.common.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/test/CurrentPlatform.common.kt @@ -5,7 +5,7 @@ package kotlinx.serialization.test enum class Platform { - JVM, JS, NATIVE + JVM, JS, NATIVE, WASM } public expect val currentPlatform: Platform @@ -13,3 +13,4 @@ public expect val currentPlatform: Platform public fun isJs(): Boolean = currentPlatform == Platform.JS public fun isJvm(): Boolean = currentPlatform == Platform.JVM public fun isNative(): Boolean = currentPlatform == Platform.NATIVE +public fun isWasm(): Boolean = currentPlatform == Platform.WASM
\ No newline at end of file diff --git a/formats/json-tests/wasmTest/src/kotlinx/serialization/test/CurrentPlatform.kt b/formats/json-tests/wasmTest/src/kotlinx/serialization/test/CurrentPlatform.kt new file mode 100644 index 00000000..fd359b72 --- /dev/null +++ b/formats/json-tests/wasmTest/src/kotlinx/serialization/test/CurrentPlatform.kt @@ -0,0 +1,7 @@ +/* + * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.serialization.test + +public actual val currentPlatform: Platform = Platform.WASM
\ No newline at end of file diff --git a/formats/json-tests/wasmTest/src/kotlinx/serialization/test/JsonHelpers.kt b/formats/json-tests/wasmTest/src/kotlinx/serialization/test/JsonHelpers.kt new file mode 100644 index 00000000..6102e586 --- /dev/null +++ b/formats/json-tests/wasmTest/src/kotlinx/serialization/test/JsonHelpers.kt @@ -0,0 +1,19 @@ +package kotlinx.serialization.test + +import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.SerializationStrategy +import kotlinx.serialization.json.Json + +actual fun <T> Json.encodeViaStream( + serializer: SerializationStrategy<T>, + value: T +): String { + TODO("supported on JVM only") +} + +actual fun <T> Json.decodeViaStream( + serializer: DeserializationStrategy<T>, + input: String +): T { + TODO("supported on JVM only") +}
\ No newline at end of file |