-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
### What's done: - Tests - MapDecoder
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import com.akuleshov7.ktoml.decoders.TomlAbstractDecoder | ||
Check failure Code scanning / ktlint [UNUSED_IMPORT] unused imports should be removed: com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive - unused import Error
[UNUSED_IMPORT] unused imports should be removed: com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive - unused import
Check failure Code scanning / ktlint [PACKAGE_NAME_MISSING] no package name declared in a file: TomlMapDecoder.kt Error
[PACKAGE_NAME_MISSING] no package name declared in a file: TomlMapDecoder.kt
Check failure Code scanning / ktlint [UNUSED_IMPORT] unused imports should be removed: com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive - unused import Error
[UNUSED_IMPORT] unused imports should be removed: com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive - unused import
Check failure Code scanning / ktlint [PACKAGE_NAME_MISSING] no package name declared in a file: TomlMapDecoder.kt Error
[PACKAGE_NAME_MISSING] no package name declared in a file: TomlMapDecoder.kt
|
||
import com.akuleshov7.ktoml.tree.nodes.TomlKeyValue | ||
import com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive | ||
import com.akuleshov7.ktoml.tree.nodes.TomlTable | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.modules.EmptySerializersModule | ||
import kotlinx.serialization.modules.SerializersModule | ||
|
||
/** | ||
* @property rootNode | ||
*/ | ||
@ExperimentalSerializationApi | ||
public class TomlMapDecoder( | ||
private val rootNode: TomlTable, | ||
) : TomlAbstractDecoder() { | ||
override val serializersModule: SerializersModule = EmptySerializersModule() | ||
override fun decodeValue(): Any = rootNode.children.map { | ||
when(it) { | ||
Check failure Code scanning / ktlint [WRONG_WHITESPACE] incorrect usage of whitespaces for code separation: keyword 'when' should be separated from '(' with a whitespace Error
[WRONG_WHITESPACE] incorrect usage of whitespaces for code separation: keyword 'when' should be separated from '(' with a whitespace
Check failure Code scanning / ktlint [WRONG_WHITESPACE] incorrect usage of whitespaces for code separation: keyword 'when' should be separated from '(' with a whitespace Error
[WRONG_WHITESPACE] incorrect usage of whitespaces for code separation: keyword 'when' should be separated from '(' with a whitespace
|
||
is TomlKeyValue -> it.key to it.value | ||
else -> throw Exception() | ||
} | ||
} | ||
override fun decodeElementIndex(descriptor: SerialDescriptor): Int = 0 | ||
|
||
override fun decodeKeyValue(): TomlKeyValue = throw NotImplementedError("") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.akuleshov7.ktoml.decoders | ||
|
||
import com.akuleshov7.ktoml.Toml | ||
|
||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.Serializable | ||
import kotlin.test.Test | ||
|
||
class PlainMapDecoderTest { | ||
@Serializable | ||
private data class TestDataMap( | ||
val text: String, | ||
val map: Map<String, String>, | ||
val number: Int, | ||
) | ||
|
||
@Test | ||
fun testMapDecoderPositiveCase() { | ||
var data = """ | ||
text = "Test" | ||
number = 15 | ||
[map] | ||
a = 1 | ||
b = 1 | ||
c = 1 | ||
number = 31 | ||
""".trimIndent() | ||
|
||
Toml.decodeFromString<TestDataMap>(data) | ||
|
||
data = """ | ||
map = { a = 1, b = 2, c = 3 } | ||
text = "Test" | ||
number = 15 | ||
""".trimIndent() | ||
|
||
Toml.decodeFromString<TestDataMap>(data) | ||
} | ||
|
||
@Test | ||
fun testMapDecoderNegativeCases() { | ||
var data = """ | ||
a = 1 | ||
b = 1 | ||
c = 1 | ||
text = "Test" | ||
number = 15 | ||
""".trimIndent() | ||
|
||
Toml.decodeFromString<TestDataMap>(data) | ||
|
||
data = """ | ||
[map] | ||
[map.a] | ||
b = 1 | ||
[map.b] | ||
c = 1 | ||
text = "Test" | ||
number = 15 | ||
""".trimIndent() | ||
|
||
Toml.decodeFromString<TestDataMap>(data) | ||
|
||
data = """ | ||
text = "Test" | ||
number = 15 | ||
""".trimIndent() | ||
|
||
Toml.decodeFromString<TestDataMap>(data) | ||
} | ||
|
||
@Test | ||
fun testSimpleMapDecoder() { | ||
val data = TestDataMap(text = "text value", number = 7321, map = mapOf("a" to "b", "c" to "d")) | ||
val encoded = Toml.encodeToString(data) | ||
val decoded: TestDataMap = Toml.decodeFromString(encoded) // throws MissingRequiredPropertyException | ||
} | ||
} |