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
|
|
|
|
*/
|
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
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
|
2020-09-12 13:31:34 +08:00
|
|
|
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
|
|
|
|
|
2020-09-12 13:31:34 +08:00
|
|
|
@OptIn(ConsoleInternalApi::class)
|
2020-08-22 01:36:22 +08:00
|
|
|
internal class PluginDataTest {
|
2020-09-18 20:53:08 +08:00
|
|
|
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-06-24 03:45:27 +08:00
|
|
|
}
|
|
|
|
|
2020-11-08 09:52:29 +08:00
|
|
|
@Suppress("unused")
|
2020-08-01 21:53:24 +08:00
|
|
|
private val jsonPrettyPrint = Json { prettyPrint = true }
|
|
|
|
private val json = Json {}
|
2020-06-24 03:45:27 +08:00
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testStringify() {
|
2020-08-22 01:36:22 +08:00
|
|
|
val data = MyPluginData()
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
var string = json.encodeToString(data.updaterSerializer, Unit)
|
2020-06-24 03:45:27 +08:00
|
|
|
assertEquals("""{"int":1,"map":{},"map2":{}}""", string)
|
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
data.int = 2
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
string = json.encodeToString(data.updaterSerializer, Unit)
|
2020-06-24 03:45:27 +08:00
|
|
|
assertEquals("""{"int":2,"map":{},"map2":{}}""", string)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testParseUpdate() {
|
2020-08-22 01:36:22 +08:00
|
|
|
val data = MyPluginData()
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
assertEquals(1, data.int)
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-01 21:53:24 +08:00
|
|
|
json.decodeFromString(
|
2020-08-22 01:36:22 +08:00
|
|
|
data.updaterSerializer, """
|
2020-08-01 21:53:24 +08:00
|
|
|
{"int":3,"map":{},"map2":{}}
|
|
|
|
""".trimIndent()
|
2020-06-24 03:45:27 +08:00
|
|
|
)
|
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
assertEquals(3, data.int)
|
2020-06-24 03:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testNestedParseUpdate() {
|
2020-08-22 01:36:22 +08:00
|
|
|
val data = MyPluginData()
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
fun delegation() = data.map
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
val refBefore = data.map
|
2020-06-24 03:45:27 +08:00
|
|
|
fun reference() = refBefore
|
|
|
|
|
2020-06-24 04:31:30 +08:00
|
|
|
assertEquals(mutableMapOf(), delegation()) // delegation
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-01 21:53:24 +08:00
|
|
|
json.decodeFromString(
|
2020-08-22 01:36:22 +08:00
|
|
|
data.updaterSerializer, """
|
2020-08-01 21:53:24 +08:00
|
|
|
{"int":1,"map":{"t":"test"},"map2":{}}
|
|
|
|
""".trimIndent()
|
2020-06-24 03:45:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2020-06-24 03:45:27 +08:00
|
|
|
fun testDeepNestedParseUpdate() {
|
2020-08-22 01:36:22 +08:00
|
|
|
val data = MyPluginData()
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
fun delegation() = data.map2
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
val refBefore = data.map2
|
2020-06-24 03:45:27 +08:00
|
|
|
fun reference() = refBefore
|
|
|
|
|
|
|
|
assertEquals(mutableMapOf(), delegation()) // delegation
|
|
|
|
|
2020-08-01 21:53:24 +08:00
|
|
|
json.decodeFromString(
|
2020-08-22 01:36:22 +08:00
|
|
|
data.updaterSerializer, """
|
2020-08-01 21:53:24 +08:00
|
|
|
{"int":1,"map":{},"map2":{"t":{"f":"test"}}}
|
|
|
|
""".trimIndent()
|
2020-06-24 03:45:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
2020-08-22 01:36:22 +08:00
|
|
|
val data = MyPluginData()
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
data.map2["t"] = mutableMapOf()
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
fun delegation() = data.map2["t"]!!
|
2020-06-24 03:45:27 +08:00
|
|
|
|
2020-08-22 01:36:22 +08:00
|
|
|
val refBefore = data.map2["t"]!!
|
2020-06-24 03:45:27 +08:00
|
|
|
fun reference() = refBefore
|
|
|
|
|
|
|
|
assertEquals(mutableMapOf(), delegation()) // delegation
|
|
|
|
|
2020-08-01 21:53:24 +08:00
|
|
|
json.decodeFromString(
|
2020-08-22 01:36:22 +08:00
|
|
|
data.updaterSerializer, """
|
2020-08-01 21:53:24 +08:00
|
|
|
{"int":1,"map":{},"map2":{"t":{"f":"test"}}}
|
|
|
|
""".trimIndent()
|
2020-06-24 03:45:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|