Skip to content

Commit

Permalink
Tests for fixes for #KT-62522 and #KT-62215
Browse files Browse the repository at this point in the history
JVM works already, Native should work with 2.0.
  • Loading branch information
sandwwraith committed Dec 10, 2024
1 parent 7204a36 commit bd6f74b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
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,
)
Original file line number Diff line number Diff line change
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)"
}
}
Original file line number Diff line number Diff line change
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)
}
}

0 comments on commit bd6f74b

Please sign in to comment.