Files
kotlin-fork/plugins/kotlinx-serialization/testData/firMembers/serializableWithCompanion.kt
T
Sergey.Shanshin 2ae2f09fee [FIR] Fixed phase for FirCompanionGenerationProcessor to COMPANION_GENERATION
Phase COMPILER_REQUIRED_ANNOTATIONS causes errors in the kotlinx serialization tests:

org.jetbrains.kotlin.fir.symbols.FirLazyResolveContractViolationException: `lazyResolveToPhase(COMPILER_REQUIRED_ANNOTATIONS)` cannot be called from a transformer with a phase COMPILER_REQUIRED_ANNOTATIONS.
`lazyResolveToPhase` can be called only from a transformer with a phase which is strictly greater than a requested phase;
 i.e., `lazyResolveToPhase(A)` may be only called from a lazy transformer with a phase B, where A < B. This is a contract of lazy resolve
2024-02-12 15:54:11 +00:00

57 lines
1.4 KiB
Kotlin
Vendored

// WITH_STDLIB
package com.example
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.descriptors.*
class NonSerializable {
companion object {
fun foo(): String {
return "OK"
}
}
}
@Serializable
data class WithCompanion(val i: Int) {
companion object {
fun foo(): String {
return "OK"
}
}
}
@Serializable
data class WithNamedCompanion(val i: Int) {
companion object Named {
fun foo(): String {
return "OK"
}
}
}
fun box(): String {
encodeAndDecode(WithCompanion.serializer(), WithCompanion(1), """{"i":1}""")?.let { return it }
encodeAndDecode(WithNamedCompanion.serializer(), WithNamedCompanion(2), """{"i":2}""")?.let { return it }
if (NonSerializable.foo() != "OK") return NonSerializable.foo()
if (WithCompanion.foo() != "OK") return WithCompanion.foo()
if (WithNamedCompanion.foo() != "OK") return WithNamedCompanion.foo()
return "OK"
}
private fun <T> encodeAndDecode(serializer: KSerializer<T>, value: T, expectedEncoded: String, expectedDecoded: T? = null): String? {
val encoded = Json.encodeToString(serializer, value)
if (encoded != expectedEncoded) return encoded
val decoded = Json.decodeFromString(serializer, encoded)
if (decoded != (expectedDecoded ?: value)) return "DECODED=${decoded.toString()}"
return null
}