Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Kotlin 2.0] Tests for fixes for #KT-62522 and #KT-62215 #2474

Draft
wants to merge 1 commit into
base: migrate-to-default-hmpp
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -28,3 +28,24 @@ abstract class NotInConstructorBase {
val b = "val b"
val a = "val a"
}

@Serializable
open class GenericBox<E> {
var contents: Map<String, E>? = null
}

// From #1264
@Serializable
sealed class TypedSealedClass<T>(val a: T) {
@Serializable
class Child(val y: Int) : TypedSealedClass<String>("10") {
override fun toString(): String = "Child($a, $y)"
}
}

// From #KT-43910
@Serializable
open class ValidatableValue<T : Any, V: Any>(
var value: T? = null,
var error: V? = null,
)
Expand Up @@ -60,3 +60,17 @@ class NotInConstructorTest : NotInConstructorBase() {
return a.hashCode() * 31 + b.hashCode() * 31 + c.hashCode()
}
}

@Serializable
data class TestData(val field: String)


@Serializable
class TestClass(): GenericBox<TestData>()

@Serializable
class Email<E : Any> : ValidatableValue<String, E>() {
override fun toString(): String {
return "Email($value, $error)"
}
}
Expand Up @@ -4,6 +4,7 @@

package sample

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
Expand Down Expand Up @@ -73,4 +74,31 @@ class AbstractBaseTest {
fun testPropertiesNotInConstructor() {
assertStringFormAndRestored("""{"b":"val b","a":"val a","c":"val c"}""", NotInConstructorTest(), NotInConstructorTest.serializer())
}

@Test
fun testSimpleChild() {
val encodedChild = """{"a":"11","y":42}"""
val decodedChild = Json.decodeFromString<TypedSealedClass.Child>(encodedChild)
assertEquals("Child(11, 42)", decodedChild.toString())
assertEquals(encodedChild, Json.encodeToString(decodedChild))
}

@Test
fun testDoubleGeneric() {
val email = Email<Int>().apply {
value = "foo"
error = 1
}
val encodedEmail = Json.encodeToString(email)
assertEquals("""{"value":"foo","error":1}""", encodedEmail)
assertEquals("Email(foo, 1)", Json.decodeFromString<Email<Int>>(encodedEmail).toString())
}

@Test
fun test() {
val t = TestClass().also { it.contents = mapOf("a" to TestData("data")) }
val s = Json.encodeToString(t)
assertEquals("""{"contents":{"a":{"field":"data"}}}""", s)
assertEquals("data", Json.decodeFromString<TestClass>(s).contents?.get("a")?.field)
}
}