Skip to content

Commit d555fc5

Browse files
committed
调整一些测试
1 parent 7514e21 commit d555fc5

File tree

7 files changed

+174
-80
lines changed

7 files changed

+174
-80
lines changed

simbot-api/src/commonMain/kotlin/love/forte/simbot/event/EventListenerRegistrar.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ public inline fun EventListenerRegistrar.process(
117117
* @param typeMismatchResult 事件类型与 [E] 不匹配时的默认返回值
118118
*/
119119
public inline fun <reified E : Event> EventListenerRegistrar.process(
120-
crossinline defaultResult: () -> EventResult = { EventResult.empty() },
121120
propertiesConsumer: ConfigurerFunction<EventListenerRegistrationProperties>? = null,
121+
crossinline defaultResult: () -> EventResult = { EventResult.empty() },
122122
crossinline typeMismatchResult: EventListenerContext.() -> EventResult = { invalid() },
123123
crossinline listenerFunction: suspend EventListenerContext.(E) -> Unit,
124124
): EventListenerRegistrationHandle = register(

simbot-commons/simbot-common-atomic/src/commonTest/kotlin/AtomicTests.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,14 @@ class AtomicTests {
139139
@Test
140140
fun compareAsyncTest() = runTest {
141141
val times = 1000
142-
coroutineScope {
143-
launch(Dispatchers.Default) { checkAtomicInt(times) }
144-
launch(Dispatchers.Default) { checkAtomicLong(times) }
145-
launch(Dispatchers.Default) { checkAtomicUInt(times) }
146-
launch(Dispatchers.Default) { checkAtomicULong(times) }
147-
launch(Dispatchers.Default) { checkAtomicRef(times) }
142+
withContext(Dispatchers.Default) {
143+
coroutineScope {
144+
launch(Dispatchers.Default) { checkAtomicInt(times) }
145+
launch(Dispatchers.Default) { checkAtomicLong(times) }
146+
launch(Dispatchers.Default) { checkAtomicUInt(times) }
147+
launch(Dispatchers.Default) { checkAtomicULong(times) }
148+
launch(Dispatchers.Default) { checkAtomicRef(times) }
149+
}
148150
}
149151
}
150152

simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StageLoopTests.kt

+26-20
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
package love.forte.simbot.common.stageloop
2525

26+
import kotlinx.coroutines.Dispatchers
2627
import kotlinx.coroutines.test.runTest
28+
import kotlinx.coroutines.withContext
2729
import kotlin.test.Test
2830
import kotlin.test.assertEquals
2931
import kotlin.test.assertTrue
@@ -37,26 +39,30 @@ class StageLoopTests {
3739

3840
@Test
3941
fun stageLoopTest() = runTest {
40-
val loop = DefaultStageLoop<TestStage>()
41-
var l1 = 0
42-
var l2 = 0
43-
var d1Done = false
44-
var d2Done = false
45-
val st = Start(onLoop1 = { l1 = it }, onLoop2 = { l2 = it })
46-
loop.appendStage(st)
47-
loop.loop(condition = {
48-
if (it is Done1) {
49-
d1Done = true
50-
}
51-
if (it is Done2) {
52-
d2Done = true
53-
}
54-
it != null
55-
})
56-
assertEquals(l1, 3)
57-
assertEquals(l2, 3)
58-
assertTrue(d1Done)
59-
assertTrue(d2Done)
42+
withContext(Dispatchers.Default) {
43+
val loop = DefaultStageLoop<TestStage>()
44+
var l1 = 0
45+
var l2 = 0
46+
var d1Done = false
47+
var d2Done = false
48+
val st = Start(onLoop1 = { l1 = it }, onLoop2 = { l2 = it })
49+
loop.appendStage(st)
50+
51+
loop.loop(condition = {
52+
if (it is Done1) {
53+
d1Done = true
54+
}
55+
if (it is Done2) {
56+
d2Done = true
57+
}
58+
it != null
59+
})
60+
61+
assertEquals(l1, 3)
62+
assertEquals(l2, 3)
63+
assertTrue(d1Done)
64+
assertTrue(d2Done)
65+
}
6066
}
6167

6268
sealed class TestStage : Stage<TestStage>()

simbot-commons/simbot-common-stage-loop/src/commonTest/kotlin/love/forte/simbot/common/stageloop/StateTests.kt

+13-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
package love.forte.simbot.common.stageloop
2525

26+
import kotlinx.coroutines.Dispatchers
2627
import kotlinx.coroutines.test.runTest
28+
import kotlinx.coroutines.withContext
2729
import kotlin.test.Test
2830
import kotlin.test.assertEquals
2931

@@ -35,16 +37,18 @@ import kotlin.test.assertEquals
3537
class StateTests {
3638
@Test
3739
fun stateLoopTest() = runTest {
38-
var time = 0
39-
val last = Start.loop(onEach = {
40-
if (it is Loop) {
41-
time = it.time
42-
}
43-
true
44-
})
40+
withContext(Dispatchers.Default) {
41+
var time = 0
42+
val last = Start.loop(onEach = {
43+
if (it is Loop) {
44+
time = it.time
45+
}
46+
true
47+
})
4548

46-
assertEquals(3, time)
47-
assertEquals(Done, last)
49+
assertEquals(3, time)
50+
assertEquals(Done, last)
51+
}
4852
}
4953

5054
sealed class TestState : State<TestState>()

simbot-commons/simbot-common-suspend-runner/src/commonTest/kotlin/love/forte/simbot/suspendrunner/CommonReserveTests.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
package love.forte.simbot.suspendrunner
2525

26+
import kotlinx.coroutines.Dispatchers
2627
import kotlinx.coroutines.delay
2728
import kotlinx.coroutines.test.runTest
29+
import kotlinx.coroutines.withContext
2830
import love.forte.simbot.annotations.InternalSimbotAPI
2931
import love.forte.simbot.suspendrunner.reserve.deferred
3032
import love.forte.simbot.suspendrunner.reserve.suspendReserve
@@ -42,9 +44,11 @@ class CommonReserveTests {
4244
@OptIn(InternalSimbotAPI::class)
4345
@Test
4446
fun commonReserveTest() = runTest {
45-
val reserve = suspendReserve(this, EmptyCoroutineContext) { run() }
46-
val value = reserve.transform(deferred()).await()
47-
assertEquals(1, value)
47+
withContext(Dispatchers.Default) {
48+
val reserve = suspendReserve(this, EmptyCoroutineContext) { run() }
49+
val value = reserve.transform(deferred()).await()
50+
assertEquals(1, value)
51+
}
4852
}
4953

5054
private suspend fun run(): Int {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library.
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.core.application
25+
26+
import kotlinx.coroutines.Dispatchers
27+
import kotlinx.coroutines.flow.toList
28+
import kotlinx.coroutines.test.runTest
29+
import love.forte.simbot.annotations.ExperimentalSimbotAPI
30+
import love.forte.simbot.application.listeners
31+
import love.forte.simbot.common.id.ID
32+
import love.forte.simbot.common.id.UUID
33+
import love.forte.simbot.common.time.Timestamp
34+
import love.forte.simbot.event.*
35+
import kotlin.test.Test
36+
import kotlin.test.assertEquals
37+
import kotlin.test.assertIs
38+
import kotlin.test.assertNull
39+
40+
/**
41+
*
42+
* @author ForteScarlet
43+
*/
44+
class ApplicationEventDispatcherTests {
45+
46+
@Test
47+
fun launchTest() = runTest {
48+
val app = launchSimpleApplication {
49+
config {
50+
coroutineContext = Dispatchers.Default
51+
}
52+
}
53+
54+
app.listeners {
55+
// first
56+
register({
57+
priority = 0
58+
}) {
59+
assertIs<MyEvent>(event)
60+
EventResult.of(1)
61+
}
62+
// second
63+
listen<MyEvent>({
64+
priority = 1
65+
}) { e ->
66+
assertIs<MyEvent>(event)
67+
assertEquals(event, e)
68+
EventResult.of(2)
69+
}
70+
// third
71+
process<MyEvent>({
72+
priority = 2
73+
}) { e ->
74+
assertIs<MyEvent>(event)
75+
assertEquals(event, e)
76+
}
77+
// forth error
78+
process<MyEvent>({
79+
priority = 3
80+
}) { e ->
81+
assertIs<MyEvent>(event)
82+
assertEquals(event, e)
83+
throw TestErr()
84+
}
85+
}
86+
87+
with(app.eventDispatcher.push(MyEvent()).toList()) {
88+
assertEquals(4, size)
89+
with(get(0)) {
90+
assertIs<StandardEventResult.Simple>(this)
91+
assertEquals(1, content)
92+
}
93+
with(get(1)) {
94+
assertIs<StandardEventResult.Simple>(this)
95+
assertEquals(2, content)
96+
}
97+
with(get(2)) {
98+
assertIs<StandardEventResult.Empty>(this)
99+
assertNull(content)
100+
}
101+
with(get(3)) {
102+
assertIs<StandardEventResult.Error>(this)
103+
assertIs<TestErr>(content)
104+
}
105+
106+
}
107+
108+
109+
}
110+
111+
private class MyEvent : Event {
112+
override val id: ID = UUID.random()
113+
114+
@OptIn(ExperimentalSimbotAPI::class)
115+
override val time: Timestamp = Timestamp.now()
116+
}
117+
118+
private class TestErr : RuntimeException()
119+
}

simbot-cores/simbot-core/src/commonTest/kotlin/love/forte/simbot/core/application/ApplicationTests.kt

-41
This file was deleted.

0 commit comments

Comments
 (0)