Expand most kotlinx.serialization tests on JS backend

to enhance and increase test coverage of the plugin.
This commit is contained in:
Leonid Startsev
2024-01-03 18:04:11 +01:00
committed by Space Team
parent 60080e8bb8
commit fba2f5ea4e
26 changed files with 363 additions and 49 deletions
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// FILE: a.kt
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
package a
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -13,4 +11,4 @@ fun box(): String {
val kind = Ref.serializer().descriptor.kind.toString()
if (kind != "CONTEXTUAL") return kind
return "OK"
}
}
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -20,4 +18,4 @@ fun box(): String {
if (d.size != 1) return "Delegation to Map failed"
if (d.data != dto.data) return "Equals failed ${d.data}"
return "OK"
}
}
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -1,4 +1,4 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JS_IR
// WITH_STDLIB
@@ -1,4 +1,4 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JS_IR
// WITH_STDLIB
@@ -43,4 +43,4 @@ fun box(): String {
val decodedEmail = Json.decodeFromString<Email<Int>>(encodedEmail)
if (decodedEmail.toString() != "Email(foo, 1)") return "DecodedEmail: $decodedEmail"
return "OK"
}
}
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -17,9 +17,6 @@ open class OpenBody {
@Serializable
abstract class AbstractConstructor(var optional: String = "foo")
// TODO: test fails for K2 if places of 'color' and 'name' are swapped
// because of https://youtrack.jetbrains.com/issue/KT-54792 (KT-20980)
// and serialization proto extension is not available in K2.
@Serializable
open class Vehicle {
var color: String? = null
@@ -87,4 +84,4 @@ fun box(): String {
return e.message!!
}
}
}
@@ -0,0 +1,89 @@
// WITH_STDLIB
// TARGET_BACKEND: JS_IR
// TARGET_BACKEND: JS_IR_ES6
// JS vs JVM difference is that we are not able to understand that we have a default value in other module (KT-62523)
// MODULE: lib
// FILE: lib.kt
package a
import kotlinx.serialization.*
@Serializable
open class OpenBody {
var optional: String? = "foo"
}
@Serializable
abstract class AbstractConstructor(var optional: String = "foo")
@Serializable
open class Vehicle {
var color: String? = null
var name: String? = null
}
// MODULE: app(lib)
// FILE: app.kt
package test
import a.*
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlin.test.assertEquals
@Serializable
class Test1: OpenBody()
@Serializable
class Test2: AbstractConstructor()
@Serializable
open class Car : Vehicle() {
var maxSpeed: Int = 100
}
fun test1() {
val string = Json.encodeToString(Test1.serializer(), Test1())
assertEquals("{\"optional\":\"foo\"}", string)
val reconstructed = Json.decodeFromString(Test1.serializer(), string)
assertEquals("foo", reconstructed.optional)
}
fun test2() {
val string = Json.encodeToString(Test2.serializer(), Test2())
assertEquals("{\"optional\":\"foo\"}", string)
val reconstructed = Json.decodeFromString(Test2.serializer(), string)
assertEquals("foo", reconstructed.optional)
}
fun test3() {
val json = Json { allowStructuredMapKeys = true; encodeDefaults = true }
val car = Car()
car.maxSpeed = 100
car.name = "ford"
val s = json.encodeToString(Car.serializer(), car)
assertEquals("""{"color":null,"name":"ford","maxSpeed":100}""", s)
val restoredCar = json.decodeFromString(Car.serializer(), s)
assertEquals(100, restoredCar.maxSpeed)
assertEquals("ford", restoredCar.name)
assertEquals(null, restoredCar.color)
}
fun box(): String {
try {
test1()
test2()
test3()
return "OK"
} catch (e: Throwable) {
e.printStackTrace()
return e.message!!
}
}
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// FILE: a.kt
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
package a
@@ -1,4 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// ISSUE: KT-57626
@@ -1,4 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// ISSUE: KT-57626
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -53,4 +51,4 @@ fun box(): String {
val s = Json.encodeToString(t)
if (s != """{"b1":"a","b2":"b#","b3":"c#","b4":"d#"}""") return s
return "OK"
}
}
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -41,4 +41,4 @@ fun box(): String {
val list = Foo.serializer().descriptor.elementDescriptors.map { it.serialName }.toList()
if (list != listOf("SerializerB", "DateSer", "kotlin.collections.ArrayList")) return list.toString()
return "OK"
}
}
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// FILE: a.kt
@@ -45,4 +43,4 @@ class Holder(
fun box(): String {
val d = Holder.serializer().descriptor.toString()
return if (d == "a.Holder(i: kotlin.Int, c: MySer<kotlin.Int>)") "OK" else d
}
}
@@ -1,5 +1,3 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
@@ -34,4 +32,4 @@ fun box(): String {
val boxDesc = Box.serializer(String.serializer()).descriptor
if (boxDesc.toString() != "Box(boxed: kotlin.String)") return boxDesc.toString()
return "OK"
}
}