-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of decoding map in the root of HOCON config (#1106)
Fixes #1046
- Loading branch information
Showing
3 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
81 changes: 81 additions & 0 deletions
81
formats/hocon/src/test/kotlin/kotlinx/serialization/hocon/HoconRootObjectsTest.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,81 @@ | ||
/* | ||
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.hocon | ||
|
||
import com.typesafe.config.ConfigFactory | ||
import kotlinx.serialization.Serializable | ||
import org.junit.Ignore | ||
import org.junit.Test | ||
import kotlin.test.* | ||
|
||
class HoconRootMapTest { | ||
private val configForRootMap = """ | ||
key1 { | ||
a = "text1" | ||
b = 11 | ||
} | ||
key2 { | ||
a = "text2" | ||
b = 12 | ||
} | ||
key3 { | ||
a = "text3" | ||
b = 13 | ||
} | ||
""" | ||
private val configWithEmptyObject = "{}" | ||
private val configWithRootList = "[foo, bar]" | ||
private val emptyConfig = "" | ||
|
||
@Serializable | ||
data class CompositeValue( | ||
val a: String, | ||
val b: Int | ||
) | ||
|
||
@Test | ||
fun testConfigWithRootMap() { | ||
val config = ConfigFactory.parseString(configForRootMap) | ||
val obj = Hocon.decodeFromConfig<Map<String, CompositeValue>>(config) | ||
|
||
assertEquals(CompositeValue("text1", 11), obj["key1"]) | ||
assertEquals(CompositeValue("text2", 12), obj["key2"]) | ||
assertEquals(CompositeValue("text3", 13), obj["key3"]) | ||
} | ||
|
||
@Test | ||
fun testEmptyObjectDecode() { | ||
val config = ConfigFactory.parseString(configWithEmptyObject) | ||
// non-null map decoded from empty object as empty map | ||
val map = Hocon.decodeFromConfig<Map<String, String>>(config) | ||
assertTrue(map.isEmpty()) | ||
|
||
// nullable map decoded from empty object as null - not obvious | ||
assertNull(Hocon.decodeFromConfig<Map<String, String>?>(config)) | ||
|
||
// root-level list in config not supported but nullable list can be decoded from empty object | ||
assertNull(Hocon.decodeFromConfig<List<String>?>(config)) | ||
} | ||
|
||
@Ignore | ||
@Test | ||
fun testErrors() { | ||
// because com.typesafe:config lib not support list in root we can't decode non-null list | ||
val config = ConfigFactory.parseString(configWithEmptyObject) | ||
assertFailsWith<NoSuchElementException> { | ||
Hocon.decodeFromConfig<List<String>>(config) | ||
} | ||
|
||
// because com.typesafe:config lib not support list in root it fails while parsing | ||
assertFailsWith<Exception> { | ||
ConfigFactory.parseString(configWithRootList) | ||
} | ||
|
||
// com.typesafe:config lib parse empty config as empty object | ||
ConfigFactory.parseString(emptyConfig) | ||
} | ||
} |