|
| 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 | +} |
0 commit comments