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"
}
}
@@ -25,24 +25,72 @@ public class SerializationFirJsBoxTestGenerated extends AbstractSerializationFir
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/kotlinx-serialization/testData/boxIr"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@Test
@TestMetadata("annotationsOnFile.kt")
public void testAnnotationsOnFile() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/annotationsOnFile.kt");
}
@Test
@TestMetadata("caching.kt")
public void testCaching() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/caching.kt");
}
@Test
@TestMetadata("clashBetweenSerializableAndNonSerializableProperty.kt")
public void testClashBetweenSerializableAndNonSerializableProperty() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/clashBetweenSerializableAndNonSerializableProperty.kt");
}
@Test
@TestMetadata("classSerializerAsObject.kt")
public void testClassSerializerAsObject() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/classSerializerAsObject.kt");
}
@Test
@TestMetadata("constValInSerialName.kt")
public void testConstValInSerialName() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/constValInSerialName.kt");
}
@Test
@TestMetadata("contextualByDefault.kt")
public void testContextualByDefault() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualByDefault.kt");
}
@Test
@TestMetadata("contextualFallback.kt")
public void testContextualFallback() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualFallback.kt");
}
@Test
@TestMetadata("contextualWithTypeParameters.kt")
public void testContextualWithTypeParameters() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualWithTypeParameters.kt");
}
@Test
@TestMetadata("delegatedInterface.kt")
public void testDelegatedInterface() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/delegatedInterface.kt");
}
@Test
@TestMetadata("delegatedProperty.kt")
public void testDelegatedProperty() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/delegatedProperty.kt");
}
@Test
@TestMetadata("enumsAreCached.kt")
public void testEnumsAreCached() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/enumsAreCached.kt");
}
@Test
@TestMetadata("excludedFromExport.kt")
public void testExcludedFromExport() throws Exception {
@@ -55,12 +103,78 @@ public class SerializationFirJsBoxTestGenerated extends AbstractSerializationFir
runTest("plugins/kotlinx-serialization/testData/boxIr/excludedFromFileExport.kt");
}
@Test
@TestMetadata("genericBaseClassMultiple.kt")
public void testGenericBaseClassMultiple() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/genericBaseClassMultiple.kt");
}
@Test
@TestMetadata("genericBaseClassSimple.kt")
public void testGenericBaseClassSimple() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/genericBaseClassSimple.kt");
}
@Test
@TestMetadata("generics.kt")
public void testGenerics() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/generics.kt");
}
@Test
@TestMetadata("interfaces.kt")
public void testInterfaces() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/interfaces.kt");
}
@Test
@TestMetadata("metaSerializable.kt")
public void testMetaSerializable() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/metaSerializable.kt");
}
@Test
@TestMetadata("multimoduleInheritanceJs.kt")
public void testMultimoduleInheritanceJs() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/multimoduleInheritanceJs.kt");
}
@Test
@TestMetadata("multipleGenericsPolymorphic.kt")
public void testMultipleGenericsPolymorphic() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/multipleGenericsPolymorphic.kt");
}
@Test
@TestMetadata("sealedClassMultifile.kt")
public void testSealedClassMultifile() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/sealedClassMultifile.kt");
}
@Test
@TestMetadata("sealedInterfaces.kt")
public void testSealedInterfaces() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/sealedInterfaces.kt");
}
@Test
@TestMetadata("serializableFromAnotherModule.kt")
public void testSerializableFromAnotherModule() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule.kt");
}
@Test
@TestMetadata("serializableFromAnotherModule_multipleFields.kt")
public void testSerializableFromAnotherModule_multipleFields() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule_multipleFields.kt");
}
@Test
@TestMetadata("serializableOnPropertyType.kt")
public void testSerializableOnPropertyType() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/serializableOnPropertyType.kt");
}
@Test
@TestMetadata("serializerFactory.kt")
public void testSerializerFactory() throws Exception {
@@ -72,4 +186,22 @@ public class SerializationFirJsBoxTestGenerated extends AbstractSerializationFir
public void testSerializerFactoryInUserDefined() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/serializerFactoryInUserDefined.kt");
}
@Test
@TestMetadata("starProjections.kt")
public void testStarProjections() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/starProjections.kt");
}
@Test
@TestMetadata("useSerializersChain.kt")
public void testUseSerializersChain() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/useSerializersChain.kt");
}
@Test
@TestMetadata("userDefinedSerializerInCompanion.kt")
public void testUserDefinedSerializerInCompanion() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/userDefinedSerializerInCompanion.kt");
}
}
@@ -25,24 +25,72 @@ public class SerializationIrJsBoxTestGenerated extends AbstractSerializationIrJs
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/kotlinx-serialization/testData/boxIr"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@Test
@TestMetadata("annotationsOnFile.kt")
public void testAnnotationsOnFile() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/annotationsOnFile.kt");
}
@Test
@TestMetadata("caching.kt")
public void testCaching() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/caching.kt");
}
@Test
@TestMetadata("clashBetweenSerializableAndNonSerializableProperty.kt")
public void testClashBetweenSerializableAndNonSerializableProperty() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/clashBetweenSerializableAndNonSerializableProperty.kt");
}
@Test
@TestMetadata("classSerializerAsObject.kt")
public void testClassSerializerAsObject() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/classSerializerAsObject.kt");
}
@Test
@TestMetadata("constValInSerialName.kt")
public void testConstValInSerialName() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/constValInSerialName.kt");
}
@Test
@TestMetadata("contextualByDefault.kt")
public void testContextualByDefault() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualByDefault.kt");
}
@Test
@TestMetadata("contextualFallback.kt")
public void testContextualFallback() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualFallback.kt");
}
@Test
@TestMetadata("contextualWithTypeParameters.kt")
public void testContextualWithTypeParameters() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualWithTypeParameters.kt");
}
@Test
@TestMetadata("delegatedInterface.kt")
public void testDelegatedInterface() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/delegatedInterface.kt");
}
@Test
@TestMetadata("delegatedProperty.kt")
public void testDelegatedProperty() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/delegatedProperty.kt");
}
@Test
@TestMetadata("enumsAreCached.kt")
public void testEnumsAreCached() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/enumsAreCached.kt");
}
@Test
@TestMetadata("excludedFromExport.kt")
public void testExcludedFromExport() throws Exception {
@@ -55,12 +103,78 @@ public class SerializationIrJsBoxTestGenerated extends AbstractSerializationIrJs
runTest("plugins/kotlinx-serialization/testData/boxIr/excludedFromFileExport.kt");
}
@Test
@TestMetadata("genericBaseClassMultiple.kt")
public void testGenericBaseClassMultiple() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/genericBaseClassMultiple.kt");
}
@Test
@TestMetadata("genericBaseClassSimple.kt")
public void testGenericBaseClassSimple() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/genericBaseClassSimple.kt");
}
@Test
@TestMetadata("generics.kt")
public void testGenerics() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/generics.kt");
}
@Test
@TestMetadata("interfaces.kt")
public void testInterfaces() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/interfaces.kt");
}
@Test
@TestMetadata("metaSerializable.kt")
public void testMetaSerializable() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/metaSerializable.kt");
}
@Test
@TestMetadata("multimoduleInheritanceJs.kt")
public void testMultimoduleInheritanceJs() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/multimoduleInheritanceJs.kt");
}
@Test
@TestMetadata("multipleGenericsPolymorphic.kt")
public void testMultipleGenericsPolymorphic() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/multipleGenericsPolymorphic.kt");
}
@Test
@TestMetadata("sealedClassMultifile.kt")
public void testSealedClassMultifile() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/sealedClassMultifile.kt");
}
@Test
@TestMetadata("sealedInterfaces.kt")
public void testSealedInterfaces() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/sealedInterfaces.kt");
}
@Test
@TestMetadata("serializableFromAnotherModule.kt")
public void testSerializableFromAnotherModule() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule.kt");
}
@Test
@TestMetadata("serializableFromAnotherModule_multipleFields.kt")
public void testSerializableFromAnotherModule_multipleFields() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule_multipleFields.kt");
}
@Test
@TestMetadata("serializableOnPropertyType.kt")
public void testSerializableOnPropertyType() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/serializableOnPropertyType.kt");
}
@Test
@TestMetadata("serializerFactory.kt")
public void testSerializerFactory() throws Exception {
@@ -72,4 +186,22 @@ public class SerializationIrJsBoxTestGenerated extends AbstractSerializationIrJs
public void testSerializerFactoryInUserDefined() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/serializerFactoryInUserDefined.kt");
}
@Test
@TestMetadata("starProjections.kt")
public void testStarProjections() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/starProjections.kt");
}
@Test
@TestMetadata("useSerializersChain.kt")
public void testUseSerializersChain() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/useSerializersChain.kt");
}
@Test
@TestMetadata("userDefinedSerializerInCompanion.kt")
public void testUserDefinedSerializerInCompanion() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/userDefinedSerializerInCompanion.kt");
}
}