Skip to content

Commit 5d27540

Browse files
committed
Tests for fixes for #KT-62522 and #KT-62215
JVM works already, Native should work with 2.0.
1 parent c487e78 commit 5d27540

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

integration-test/src/commonMain/kotlin/sample/MultiFileHierarchyModuleA.kt

+21
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,24 @@ abstract class NotInConstructorBase {
2828
val b = "val b"
2929
val a = "val a"
3030
}
31+
32+
@Serializable
33+
open class GenericBox<E> {
34+
var contents: Map<String, E>? = null
35+
}
36+
37+
// From #1264
38+
@Serializable
39+
sealed class TypedSealedClass<T>(val a: T) {
40+
@Serializable
41+
class Child(val y: Int) : TypedSealedClass<String>("10") {
42+
override fun toString(): String = "Child($a, $y)"
43+
}
44+
}
45+
46+
// From #KT-43910
47+
@Serializable
48+
open class ValidatableValue<T : Any, V: Any>(
49+
var value: T? = null,
50+
var error: V? = null,
51+
)

integration-test/src/commonTest/kotlin/sample/MultiFileHierarchyModuleB.kt

+14
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,17 @@ class NotInConstructorTest : NotInConstructorBase() {
6060
return a.hashCode() * 31 + b.hashCode() * 31 + c.hashCode()
6161
}
6262
}
63+
64+
@Serializable
65+
data class TestData(val field: String)
66+
67+
68+
@Serializable
69+
class TestClass(): GenericBox<TestData>()
70+
71+
@Serializable
72+
class Email<E : Any> : ValidatableValue<String, E>() {
73+
override fun toString(): String {
74+
return "Email($value, $error)"
75+
}
76+
}

integration-test/src/commonTest/kotlin/sample/MultiFileHierarchyTest.kt

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package sample
66

7+
import kotlinx.serialization.encodeToString
78
import kotlinx.serialization.json.Json
89
import kotlinx.serialization.modules.SerializersModule
910
import kotlinx.serialization.modules.polymorphic
@@ -73,4 +74,31 @@ class AbstractBaseTest {
7374
fun testPropertiesNotInConstructor() {
7475
assertStringFormAndRestored("""{"b":"val b","a":"val a","c":"val c"}""", NotInConstructorTest(), NotInConstructorTest.serializer())
7576
}
77+
78+
@Test
79+
fun testSimpleChild() {
80+
val encodedChild = """{"a":"11","y":42}"""
81+
val decodedChild = Json.decodeFromString<TypedSealedClass.Child>(encodedChild)
82+
assertEquals("Child(11, 42)", decodedChild.toString())
83+
assertEquals(encodedChild, Json.encodeToString(decodedChild))
84+
}
85+
86+
@Test
87+
fun testDoubleGeneric() {
88+
val email = Email<Int>().apply {
89+
value = "foo"
90+
error = 1
91+
}
92+
val encodedEmail = Json.encodeToString(email)
93+
assertEquals("""{"value":"foo","error":1}""", encodedEmail)
94+
assertEquals("Email(foo, 1)", Json.decodeFromString<Email<Int>>(encodedEmail).toString())
95+
}
96+
97+
@Test
98+
fun test() {
99+
val t = TestClass().also { it.contents = mapOf("a" to TestData("data")) }
100+
val s = Json.encodeToString(t)
101+
assertEquals("""{"contents":{"a":{"field":"data"}}}""", s)
102+
assertEquals("data", Json.decodeFromString<TestClass>(s).contents?.get("a")?.field)
103+
}
76104
}

0 commit comments

Comments
 (0)