CombinedStorageTest

This commit is contained in:
Him188 2021-04-24 14:50:52 +08:00
parent b91bbfd2b8
commit c35c2c97c8
3 changed files with 114 additions and 36 deletions

View File

@ -13,22 +13,12 @@ import net.mamoe.mirai.internal.test.AbstractTest
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
private data class TestComponent2(
val value: Int
) {
companion object : ComponentKey<TestComponent2>
}
internal abstract class AbstractMutableComponentStorageTest : AbstractTest() {
protected abstract fun createStorage(): MutableComponentStorage
private data class TestComponent3(
val value: Int
) {
companion object : ComponentKey<TestComponent3>
}
internal class ComponentStorageTest : AbstractTest() {
@Test
fun `can put component`() {
val storage = ConcurrentComponentStorage().apply {
val storage = createStorage().apply {
set(TestComponent2, TestComponent2(1))
}
assertEquals(1, storage.size)
@ -36,7 +26,7 @@ internal class ComponentStorageTest : AbstractTest() {
@Test
fun `can put multiple components with different key`() {
val storage = ConcurrentComponentStorage().apply {
val storage = createStorage().apply {
set(TestComponent2, TestComponent2(1))
set(TestComponent3, TestComponent3(1))
}
@ -45,7 +35,7 @@ internal class ComponentStorageTest : AbstractTest() {
@Test
fun `can get component`() {
val storage = ConcurrentComponentStorage().apply {
val storage = createStorage().apply {
set(TestComponent2, TestComponent2(1))
}
assertEquals(1, storage.size)
@ -54,31 +44,11 @@ internal class ComponentStorageTest : AbstractTest() {
@Test
fun `can override component with same key`() {
val storage = ConcurrentComponentStorage().apply {
val storage = createStorage().apply {
set(TestComponent2, TestComponent2(1))
set(TestComponent2, TestComponent2(2))
}
assertEquals(1, storage.size)
assertEquals(TestComponent2(2), storage[TestComponent2])
}
@Test
fun `test toString non debug`() {
val storage = ConcurrentComponentStorage(showAllComponents = false).apply {
set(TestComponent2, TestComponent2(1))
}
assertEquals("ConcurrentComponentStorage(size=1)", storage.toString())
}
@Test
fun `test toString debugging`() {
val storage = ConcurrentComponentStorage(showAllComponents = true).apply {
set(TestComponent2, TestComponent2(1))
}
assertEquals(
"""ConcurrentComponentStorage(size=1) {
| TestComponent2: TestComponent2(value=1)
|}""".trimMargin(), storage.toString()
)
}
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.internal.network.component
import net.mamoe.mirai.internal.test.AbstractTest
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
internal class CombinedStorageTest : AbstractTest() {
@Test
fun `can get from main`() {
val storage = ConcurrentComponentStorage().apply {
set(TestComponent2, TestComponent2(1))
} + ConcurrentComponentStorage()
assertEquals(TestComponent2(1), storage[TestComponent2])
}
@Test
fun `can get from fallback`() {
val storage = ConcurrentComponentStorage() + ConcurrentComponentStorage().apply {
set(TestComponent2, TestComponent2(1))
}
assertEquals(TestComponent2(1), storage[TestComponent2])
}
@Test
fun `size is sum of sizes of two storages`() {
val storage = ConcurrentComponentStorage().apply {
set(TestComponent3, TestComponent3(1))
} + ConcurrentComponentStorage().apply {
set(TestComponent2, TestComponent2(1))
}
assertEquals(2, storage.size)
}
@Test
fun `size can be zero`() {
val storage = ConcurrentComponentStorage() + ConcurrentComponentStorage()
assertEquals(0, storage.size)
}
@Test
fun `main prevails than fallback`() {
val storage = ConcurrentComponentStorage().apply {
set(TestComponent2, TestComponent2(1))
} + ConcurrentComponentStorage().apply {
set(TestComponent2, TestComponent2(2))
}
assertEquals(TestComponent2(1), storage[TestComponent2])
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.internal.network.component
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
internal data class TestComponent2(
val value: Int
) {
companion object : ComponentKey<TestComponent2>
}
internal data class TestComponent3(
val value: Int
) {
companion object : ComponentKey<TestComponent3>
}
internal class ConcurrentComponentStorageTest : AbstractMutableComponentStorageTest() {
override fun createStorage(): MutableComponentStorage = ConcurrentComponentStorage(showAllComponents = true)
@Test
fun `test toString non debug`() {
val storage = ConcurrentComponentStorage(showAllComponents = false).apply {
set(TestComponent2, TestComponent2(1))
}
assertEquals("ConcurrentComponentStorage(size=1)", storage.toString())
}
@Test
fun `test toString debugging`() {
val storage = ConcurrentComponentStorage(showAllComponents = true).apply {
set(TestComponent2, TestComponent2(1))
}
assertEquals(
"""ConcurrentComponentStorage(size=1) {
| TestComponent2: TestComponent2(value=1)
|}""".trimMargin(), storage.toString()
)
}
}