-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
1 deletion.
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
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
41 changes: 41 additions & 0 deletions
41
yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/decoder/ContextualTest.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,41 @@ | ||
package net.mamoe.yamlkt.decoder | ||
|
||
import kotlinx.serialization.Contextual | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.modules.SerializersModule | ||
import net.mamoe.yamlkt.Yaml | ||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
|
||
internal class ContextualTest { | ||
interface Fruit | ||
|
||
@Serializable | ||
data class Banana( | ||
val prop: String | ||
) : Fruit | ||
|
||
@Serializable | ||
data class Poly( | ||
val fruit: @Contextual Fruit | ||
) | ||
|
||
val yaml = Yaml { | ||
serializersModule = SerializersModule { | ||
contextual(Banana::class, Banana.serializer()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `test simple dump`() { | ||
val poly = Poly(Banana("prop")) | ||
val result = yaml.encodeToString(poly) | ||
assertEquals( | ||
""" | ||
fruit: | ||
prop: prop | ||
""".trimIndent(), result | ||
) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/decoder/PolymorphismTest.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,40 @@ | ||
package net.mamoe.yamlkt.decoder | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.polymorphic | ||
import net.mamoe.yamlkt.Yaml | ||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
|
||
internal class PolymorphismTest { | ||
interface Fruit | ||
|
||
@Serializable | ||
data class Banana( | ||
val prop: String | ||
) : Fruit | ||
|
||
@Serializable | ||
data class Poly( | ||
val fruit: Fruit | ||
) | ||
|
||
val yaml = Yaml { | ||
serializersModule = SerializersModule { | ||
polymorphic(Fruit::class) { | ||
subclass(Banana::class, Banana.serializer()) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test simple dump`() { | ||
val poly = Poly(Banana("prop")) | ||
val result = yaml.encodeToString(poly) | ||
println(result) | ||
val decode = yaml.decodeFromString(Poly.serializer(), result) | ||
assertEquals(poly, decode) | ||
} | ||
} |