-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for nested and inlined maps (inside other maps)
### What's done: - Added a support for nested anonymous Maps
- Loading branch information
Showing
3 changed files
with
80 additions
and
29 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
47 changes: 44 additions & 3 deletions
47
ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/NestedMapTest.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 |
---|---|---|
@@ -1,29 +1,70 @@ | ||
package com.akuleshov7.ktoml.decoders | ||
|
||
import com.akuleshov7.ktoml.Toml | ||
import com.akuleshov7.ktoml.exceptions.IllegalTypeException | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.decodeFromString | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
class NestedMapTest { | ||
@Serializable | ||
data class NestedTable( | ||
val outer: Map<String, Map<String, Long>> | ||
) | ||
|
||
@Serializable | ||
data class SimpleNestedTable( | ||
val map: Map<String, Long>, | ||
) | ||
|
||
@ExperimentalSerializationApi | ||
@Test | ||
fun nestedInvalidMapping() { | ||
val data = """ | ||
[map] | ||
[map.a] | ||
b = 1 | ||
""".trimIndent() | ||
|
||
assertFailsWith<IllegalTypeException> { | ||
Toml.decodeFromString<SimpleNestedTable>(data) | ||
} | ||
} | ||
|
||
@ExperimentalSerializationApi | ||
@Test | ||
fun testSimpleNestedMaps() { | ||
val data = """ | ||
[outer] | ||
[outer.inner1] | ||
a = 5 | ||
b = 5 | ||
""".trimIndent() | ||
|
||
val result = Toml.decodeFromString<NestedTable>(data) | ||
assertEquals(NestedTable(outer = mapOf("inner1" to mapOf("a" to 5, "b" to 5))), result) | ||
} | ||
|
||
@ExperimentalSerializationApi | ||
@Test | ||
fun testDottedKeys() { | ||
fun testNestedMaps() { | ||
val data = """ | ||
[outer] | ||
[outer.inner] | ||
[outer.inner1] | ||
a = 5 | ||
b = 5 | ||
[outer.inner2] | ||
c = 7 | ||
d = 12 | ||
""".trimIndent() | ||
|
||
val result = Toml.decodeFromString<NestedTable>(data) | ||
assertEquals(NestedTable(outer = mapOf("inner" to mapOf("a" to 5, "b" to 5))), result) | ||
assertEquals(NestedTable(outer = mapOf("inner1" to mapOf("a" to 5, "b" to 5), "inner2" to mapOf("c" to 7, "d" to 12))), result) | ||
} | ||
} |
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