diff --git a/simbot-api/src/commonMain/kotlin/love/forte/simbot/event/EventListenerRegistrar.kt b/simbot-api/src/commonMain/kotlin/love/forte/simbot/event/EventListenerRegistrar.kt index f01bc8a3f..b6a65f66a 100644 --- a/simbot-api/src/commonMain/kotlin/love/forte/simbot/event/EventListenerRegistrar.kt +++ b/simbot-api/src/commonMain/kotlin/love/forte/simbot/event/EventListenerRegistrar.kt @@ -117,8 +117,8 @@ public inline fun EventListenerRegistrar.process( * @param typeMismatchResult 事件类型与 [E] 不匹配时的默认返回值 */ public inline fun EventListenerRegistrar.process( - crossinline defaultResult: () -> EventResult = { EventResult.empty() }, propertiesConsumer: ConfigurerFunction? = null, + crossinline defaultResult: () -> EventResult = { EventResult.empty() }, crossinline typeMismatchResult: EventListenerContext.() -> EventResult = { invalid() }, crossinline listenerFunction: suspend EventListenerContext.(E) -> Unit, ): EventListenerRegistrationHandle = register( diff --git a/simbot-commons/simbot-common-atomic/src/commonTest/kotlin/AtomicTests.kt b/simbot-commons/simbot-common-atomic/src/commonTest/kotlin/AtomicTests.kt index eda161a79..dbe6b9e6e 100644 --- a/simbot-commons/simbot-common-atomic/src/commonTest/kotlin/AtomicTests.kt +++ b/simbot-commons/simbot-common-atomic/src/commonTest/kotlin/AtomicTests.kt @@ -139,12 +139,14 @@ class AtomicTests { @Test fun compareAsyncTest() = runTest { val times = 1000 - coroutineScope { - launch(Dispatchers.Default) { checkAtomicInt(times) } - launch(Dispatchers.Default) { checkAtomicLong(times) } - launch(Dispatchers.Default) { checkAtomicUInt(times) } - launch(Dispatchers.Default) { checkAtomicULong(times) } - launch(Dispatchers.Default) { checkAtomicRef(times) } + withContext(Dispatchers.Default) { + coroutineScope { + launch(Dispatchers.Default) { checkAtomicInt(times) } + launch(Dispatchers.Default) { checkAtomicLong(times) } + launch(Dispatchers.Default) { checkAtomicUInt(times) } + launch(Dispatchers.Default) { checkAtomicULong(times) } + launch(Dispatchers.Default) { checkAtomicRef(times) } + } } } diff --git a/simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StageLoopTests.kt b/simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StageLoopTests.kt index d20de1573..7ca19797a 100644 --- a/simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StageLoopTests.kt +++ b/simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StageLoopTests.kt @@ -23,7 +23,9 @@ package love.forte.simbot.common.stageloop +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.withContext import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -37,26 +39,30 @@ class StageLoopTests { @Test fun stageLoopTest() = runTest { - val loop = DefaultStageLoop() - var l1 = 0 - var l2 = 0 - var d1Done = false - var d2Done = false - val st = Start(onLoop1 = { l1 = it }, onLoop2 = { l2 = it }) - loop.appendStage(st) - loop.loop(condition = { - if (it is Done1) { - d1Done = true - } - if (it is Done2) { - d2Done = true - } - it != null - }) - assertEquals(l1, 3) - assertEquals(l2, 3) - assertTrue(d1Done) - assertTrue(d2Done) + withContext(Dispatchers.Default) { + val loop = DefaultStageLoop() + var l1 = 0 + var l2 = 0 + var d1Done = false + var d2Done = false + val st = Start(onLoop1 = { l1 = it }, onLoop2 = { l2 = it }) + loop.appendStage(st) + + loop.loop(condition = { + if (it is Done1) { + d1Done = true + } + if (it is Done2) { + d2Done = true + } + it != null + }) + + assertEquals(l1, 3) + assertEquals(l2, 3) + assertTrue(d1Done) + assertTrue(d2Done) + } } sealed class TestStage : Stage() diff --git a/simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StateTests.kt b/simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StateTests.kt index a29067b66..56b0cafe1 100644 --- a/simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StateTests.kt +++ b/simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StateTests.kt @@ -23,7 +23,9 @@ package love.forte.simbot.common.stageloop +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.withContext import kotlin.test.Test import kotlin.test.assertEquals @@ -35,16 +37,18 @@ import kotlin.test.assertEquals class StateTests { @Test fun stateLoopTest() = runTest { - var time = 0 - val last = Start.loop(onEach = { - if (it is Loop) { - time = it.time - } - true - }) + withContext(Dispatchers.Default) { + var time = 0 + val last = Start.loop(onEach = { + if (it is Loop) { + time = it.time + } + true + }) - assertEquals(3, time) - assertEquals(Done, last) + assertEquals(3, time) + assertEquals(Done, last) + } } sealed class TestState : State() diff --git a/simbot-commons/simbot-common-suspend-runner/src/commonTest/kotlin/love/forte/simbot/suspendrunner/CommonReserveTests.kt b/simbot-commons/simbot-common-suspend-runner/src/commonTest/kotlin/love/forte/simbot/suspendrunner/CommonReserveTests.kt index 77145b7e6..6da5a4f8a 100644 --- a/simbot-commons/simbot-common-suspend-runner/src/commonTest/kotlin/love/forte/simbot/suspendrunner/CommonReserveTests.kt +++ b/simbot-commons/simbot-common-suspend-runner/src/commonTest/kotlin/love/forte/simbot/suspendrunner/CommonReserveTests.kt @@ -23,8 +23,10 @@ package love.forte.simbot.suspendrunner +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.withContext import love.forte.simbot.annotations.InternalSimbotAPI import love.forte.simbot.suspendrunner.reserve.deferred import love.forte.simbot.suspendrunner.reserve.suspendReserve @@ -42,9 +44,11 @@ class CommonReserveTests { @OptIn(InternalSimbotAPI::class) @Test fun commonReserveTest() = runTest { - val reserve = suspendReserve(this, EmptyCoroutineContext) { run() } - val value = reserve.transform(deferred()).await() - assertEquals(1, value) + withContext(Dispatchers.Default) { + val reserve = suspendReserve(this, EmptyCoroutineContext) { run() } + val value = reserve.transform(deferred()).await() + assertEquals(1, value) + } } private suspend fun run(): Int { diff --git a/simbot-cores/simbot-core/src/commonTest/kotlin/love/forte/simbot/core/application/ApplicationEventDispatcherTests.kt b/simbot-cores/simbot-core/src/commonTest/kotlin/love/forte/simbot/core/application/ApplicationEventDispatcherTests.kt new file mode 100644 index 000000000..77d498d4d --- /dev/null +++ b/simbot-cores/simbot-core/src/commonTest/kotlin/love/forte/simbot/core/application/ApplicationEventDispatcherTests.kt @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2024. ForteScarlet. + * + * Project https://github.com/simple-robot/simpler-robot + * Email ForteScarlet@163.com + * + * This file is part of the Simple Robot Library. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + * + */ + +package love.forte.simbot.core.application + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.toList +import kotlinx.coroutines.test.runTest +import love.forte.simbot.annotations.ExperimentalSimbotAPI +import love.forte.simbot.application.listeners +import love.forte.simbot.common.id.ID +import love.forte.simbot.common.id.UUID +import love.forte.simbot.common.time.Timestamp +import love.forte.simbot.event.* +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertIs +import kotlin.test.assertNull + +/** + * + * @author ForteScarlet + */ +class ApplicationEventDispatcherTests { + + @Test + fun launchTest() = runTest { + val app = launchSimpleApplication { + config { + coroutineContext = Dispatchers.Default + } + } + + app.listeners { + // first + register({ + priority = 0 + }) { + assertIs(event) + EventResult.of(1) + } + // second + listen({ + priority = 1 + }) { e -> + assertIs(event) + assertEquals(event, e) + EventResult.of(2) + } + // third + process({ + priority = 2 + }) { e -> + assertIs(event) + assertEquals(event, e) + } + // forth error + process({ + priority = 3 + }) { e -> + assertIs(event) + assertEquals(event, e) + throw TestErr() + } + } + + with(app.eventDispatcher.push(MyEvent()).toList()) { + assertEquals(4, size) + with(get(0)) { + assertIs(this) + assertEquals(1, content) + } + with(get(1)) { + assertIs(this) + assertEquals(2, content) + } + with(get(2)) { + assertIs(this) + assertNull(content) + } + with(get(3)) { + assertIs(this) + assertIs(content) + } + + } + + + } + + private class MyEvent : Event { + override val id: ID = UUID.random() + + @OptIn(ExperimentalSimbotAPI::class) + override val time: Timestamp = Timestamp.now() + } + + private class TestErr : RuntimeException() +} diff --git a/simbot-cores/simbot-core/src/commonTest/kotlin/love/forte/simbot/core/application/ApplicationTests.kt b/simbot-cores/simbot-core/src/commonTest/kotlin/love/forte/simbot/core/application/ApplicationTests.kt deleted file mode 100644 index 007fe3038..000000000 --- a/simbot-cores/simbot-core/src/commonTest/kotlin/love/forte/simbot/core/application/ApplicationTests.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2024. ForteScarlet. - * - * Project https://github.com/simple-robot/simpler-robot - * Email ForteScarlet@163.com - * - * This file is part of the Simple Robot Library. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - * - */ - -package love.forte.simbot.core.application - -import kotlin.test.Test - -/** - * - * @author ForteScarlet - */ -class ApplicationTests { - - @Test - fun launchTest() { - - - - } - -}