-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests on calculated default value of properties depending on ot…
…her properties Tests for internal and external serializable classes. The default values for constructor properties, transient properties, body properties are checked
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,7 @@ kotlin { | |
} | ||
} | ||
} | ||
|
||
compileTestKotlinJsLegacy { | ||
exclude '**/PropertyInitializerTest.kt' | ||
} |
105 changes: 105 additions & 0 deletions
105
formats/json/commonTest/src/kotlinx/serialization/features/PropertyInitializerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
@file:Suppress("MayBeConstant") | ||
|
||
package kotlinx.serialization.features | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.Json | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
internal val globalVar: Int = 4 | ||
|
||
internal fun globalFun(): Int { | ||
return 7 | ||
} | ||
|
||
internal const val PROPERTY_INITIALIZER_JSON = """{ | ||
"valProperty": 1, | ||
"varProperty": 2, | ||
"literalConst": 3, | ||
"globalVarRef": 4, | ||
"computed": 5, | ||
"doubleRef": 6, | ||
"globalFun": 7, | ||
"globalFunExpr": 8, | ||
"itExpr": 9, | ||
"transientRefFromProp": 10, | ||
"bodyProp": 11, | ||
"dependBodyProp": 12, | ||
"getterDepend": 13 | ||
}""" | ||
|
||
@Suppress("MemberVisibilityCanBePrivate", "unused", "ComplexRedundantLet") | ||
class PropertyInitializerTest { | ||
@Serializable | ||
data class InternalClass( | ||
val valProperty: Int, | ||
var varProperty: Int, | ||
val literalConst: Int = 3, | ||
val globalVarRef: Int = globalVar, | ||
val computed: Int = valProperty + varProperty + 2, | ||
val doubleRef: Int = literalConst + literalConst, | ||
var globalFun: Int = globalFun(), | ||
var globalFunExpr: Int = globalFun() + 1, | ||
val itExpr: Int = literalConst.let { it + 6 }, | ||
@Transient val constTransient: Int = 6, | ||
@Transient val serializedRefTransient: Int = varProperty + 1, | ||
@Transient val refTransient: Int = serializedRefTransient, | ||
val transientRefFromProp: Int = constTransient + 4, | ||
) { | ||
val valGetter: Int get() { return 5 } | ||
var bodyProp: Int = 11 | ||
var dependBodyProp: Int = bodyProp + 1 | ||
var getterDepend: Int = valGetter + 8 | ||
} | ||
|
||
private val format = Json { encodeDefaults = true; prettyPrint = true } | ||
|
||
data class ExternalClass( | ||
val valProperty: Int, | ||
var varProperty: Int, | ||
val literalConst: Int = 3, | ||
val globalVarRef: Int = globalVar, | ||
val computed: Int = valProperty + varProperty + 2, | ||
val doubleRef: Int = literalConst + literalConst, | ||
var globalFun: Int = globalFun(), | ||
var globalFunExpr: Int = globalFun() + 1, | ||
val itExpr: Int = literalConst.let { it + 6 }, | ||
@Transient val constTransient: Int = 6, | ||
@Transient val serializedRefTransient: Int = varProperty + 1, | ||
@Transient val refTransient: Int = serializedRefTransient, | ||
val transientRefFromProp: Int = constTransient + 4, | ||
) { | ||
val valGetter: Int get() { return 5 } | ||
var bodyProp: Int = 11 | ||
var dependBodyProp: Int = bodyProp + 1 | ||
var getterDepend: Int = valGetter + 8 | ||
} | ||
|
||
@Serializer(ExternalClass::class) | ||
object ExternalSerializer | ||
|
||
@Test | ||
fun testInternalSerializeDefault() { | ||
val encoded = format.encodeToString(InternalClass(1, 2)) | ||
assertEquals(PROPERTY_INITIALIZER_JSON, encoded) | ||
} | ||
|
||
@Test | ||
fun testInternalDeserializeDefault() { | ||
val decoded = format.decodeFromString<InternalClass>("""{"valProperty": 5, "varProperty": 6}""") | ||
assertEquals(InternalClass(5, 6), decoded) | ||
} | ||
|
||
@Test | ||
fun testExternalSerializeDefault() { | ||
val encoded = format.encodeToString(ExternalSerializer, ExternalClass(1, 2)) | ||
assertEquals(PROPERTY_INITIALIZER_JSON, encoded) | ||
} | ||
|
||
@Test | ||
fun testExternalDeserializeDefault() { | ||
val decoded = format.decodeFromString(ExternalSerializer,"""{"valProperty": 5, "varProperty": 6}""") | ||
assertEquals(ExternalClass(5, 6), decoded) | ||
} | ||
} |