mirai/backend/mirai-console/test/data/SettingTest.kt

132 lines
3.7 KiB
Kotlin
Raw Normal View History

2020-06-21 15:27:19 +08:00
/*
2020-08-16 23:36:24 +08:00
* Copyright 2019-2020 Mamoe Technologies and contributors.
2020-06-21 15:27:19 +08:00
*
2020-08-23 17:46:51 +08:00
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link.
2020-06-21 15:27:19 +08:00
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.data
2020-06-21 15:27:19 +08:00
2020-08-01 23:13:49 +08:00
import kotlinx.serialization.json.Json
import net.mamoe.mirai.console.util.ConsoleInternalApi
2020-08-01 23:13:49 +08:00
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertSame
@OptIn(ConsoleInternalApi::class)
internal class PluginDataTest {
class MyPluginData : AutoSavePluginData("test") {
2020-08-25 22:43:31 +08:00
var int by value(1)
val map: MutableMap<String, String> by value()
val map2: MutableMap<String, MutableMap<String, String>> by value()
}
2020-11-08 09:52:29 +08:00
@Suppress("unused")
2020-12-18 16:52:39 +08:00
private val jsonPrettyPrint = Json {
prettyPrint = true
encodeDefaults = true
}
private val json = Json { encodeDefaults = true }
@Test
fun testStringify() {
val data = MyPluginData()
var string = json.encodeToString(data.updaterSerializer, Unit)
assertEquals("""{"int":1,"map":{},"map2":{}}""", string)
data.int = 2
string = json.encodeToString(data.updaterSerializer, Unit)
assertEquals("""{"int":2,"map":{},"map2":{}}""", string)
}
@Test
fun testParseUpdate() {
val data = MyPluginData()
assertEquals(1, data.int)
2020-08-01 21:53:24 +08:00
json.decodeFromString(
data.updaterSerializer, """
2020-08-01 21:53:24 +08:00
{"int":3,"map":{},"map2":{}}
""".trimIndent()
)
assertEquals(3, data.int)
}
@Test
fun testNestedParseUpdate() {
val data = MyPluginData()
fun delegation() = data.map
val refBefore = data.map
fun reference() = refBefore
2020-06-24 04:31:30 +08:00
assertEquals(mutableMapOf(), delegation()) // delegation
2020-08-01 21:53:24 +08:00
json.decodeFromString(
data.updaterSerializer, """
2020-08-01 21:53:24 +08:00
{"int":1,"map":{"t":"test"},"map2":{}}
""".trimIndent()
)
assertEquals(mapOf("t" to "test").toString(), delegation().toString())
assertEquals(mapOf("t" to "test").toString(), reference().toString())
assertSame(reference(), delegation()) // check shadowing
2020-06-21 22:48:34 +08:00
}
2020-06-21 15:27:19 +08:00
@Test
fun testDeepNestedParseUpdate() {
val data = MyPluginData()
fun delegation() = data.map2
val refBefore = data.map2
fun reference() = refBefore
assertEquals(mutableMapOf(), delegation()) // delegation
2020-08-01 21:53:24 +08:00
json.decodeFromString(
data.updaterSerializer, """
2020-08-01 21:53:24 +08:00
{"int":1,"map":{},"map2":{"t":{"f":"test"}}}
""".trimIndent()
)
assertEquals(mapOf("t" to mapOf("f" to "test")).toString(), delegation().toString())
assertEquals(mapOf("t" to mapOf("f" to "test")).toString(), reference().toString())
assertSame(reference(), delegation()) // check shadowing
}
@Test
fun testDeepNestedTrackingParseUpdate() {
val data = MyPluginData()
data.map2["t"] = mutableMapOf()
fun delegation() = data.map2["t"]!!
val refBefore = data.map2["t"]!!
fun reference() = refBefore
assertEquals(mutableMapOf(), delegation()) // delegation
2020-08-01 21:53:24 +08:00
json.decodeFromString(
data.updaterSerializer, """
2020-08-01 21:53:24 +08:00
{"int":1,"map":{},"map2":{"t":{"f":"test"}}}
""".trimIndent()
)
assertEquals(mapOf("f" to "test").toString(), delegation().toString())
assertEquals(mapOf("f" to "test").toString(), reference().toString())
assertSame(reference(), delegation()) // check shadowing
2020-06-21 15:27:19 +08:00
}
2020-08-01 23:05:03 +08:00
}