|
| 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 (Alias: simple-robot, simbot, etc.). |
| 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.spring.test |
| 25 | + |
| 26 | +import io.mockk.every |
| 27 | +import io.mockk.mockk |
| 28 | +import kotlinx.coroutines.flow.toList |
| 29 | +import kotlinx.coroutines.runBlocking |
| 30 | +import love.forte.simbot.application.Application |
| 31 | +import love.forte.simbot.event.* |
| 32 | +import love.forte.simbot.quantcat.common.annotations.Filter |
| 33 | +import love.forte.simbot.quantcat.common.annotations.FilterValue |
| 34 | +import love.forte.simbot.quantcat.common.annotations.Listener |
| 35 | +import love.forte.simbot.spring.EnableSimbot |
| 36 | +import org.springframework.beans.factory.annotation.Autowired |
| 37 | +import org.springframework.boot.test.context.SpringBootTest |
| 38 | +import org.springframework.stereotype.Component |
| 39 | +import kotlin.test.Test |
| 40 | +import kotlin.test.assertEquals |
| 41 | +import kotlin.test.assertFails |
| 42 | +import kotlin.test.assertNull |
| 43 | + |
| 44 | + |
| 45 | +/** |
| 46 | + * 测试来源:https://github.com/simple-robot/simpler-robot/issues/895 |
| 47 | + * |
| 48 | + * @author ForteScarlet |
| 49 | + */ |
| 50 | +@SpringBootTest( |
| 51 | + classes = [ |
| 52 | + DefaultBinderTests::class, |
| 53 | + TestListenerContainer::class, |
| 54 | + ] |
| 55 | +) |
| 56 | +@EnableSimbot |
| 57 | +open class DefaultBinderTests { |
| 58 | + |
| 59 | + @Test |
| 60 | + fun binderTest1( |
| 61 | + @Autowired application: Application |
| 62 | + ) { |
| 63 | + fun Event.push(): List<EventResult> { |
| 64 | + return runBlocking { |
| 65 | + application.eventDispatcher.push(this@push) |
| 66 | + .throwIfError() |
| 67 | + .filterNotInvalid() |
| 68 | + .toList() |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // test1: 应当得到 page=1, 因为匹配内容 page 实际不存在,使用默认值 |
| 73 | + mockk<MessageEvent>(relaxed = true) { |
| 74 | + every { messageContent.plainText } returns "test_1 1" |
| 75 | + assertEquals(1, push().first().content) |
| 76 | + } |
| 77 | + |
| 78 | + // test2(1): 符合匹配结果,应当得到 1 |
| 79 | + mockk<MessageEvent>(relaxed = true) { |
| 80 | + every { messageContent.plainText } returns "test_2 1" |
| 81 | + assertEquals("1", push().first().content) |
| 82 | + } |
| 83 | + |
| 84 | + // test2(2): 不符合匹配结果、不是required=false,理应报错 |
| 85 | + mockk<MessageEvent>(relaxed = true) { |
| 86 | + every { messageContent.plainText } returns "test_2" |
| 87 | + assertFails { push() } |
| 88 | + } |
| 89 | + |
| 90 | + // test3(1): 不符合匹配结果、不是required=false,但参数是可选的,使用默认值,即得到 null |
| 91 | + mockk<MessageEvent>(relaxed = true) { |
| 92 | + every { messageContent.plainText } returns "test_3" |
| 93 | + assertNull(push().first().content) |
| 94 | + } |
| 95 | + |
| 96 | + // test3(2): 符合匹配结果、得到 "1" |
| 97 | + mockk<MessageEvent>(relaxed = true) { |
| 98 | + every { messageContent.plainText } returns "test_3 1" |
| 99 | + assertEquals("1", push().first().content) |
| 100 | + } |
| 101 | + |
| 102 | + // test4(1): 不符合匹配结果、是required=false,参数是可选的,使用默认值,即得到 null |
| 103 | + mockk<MessageEvent>(relaxed = true) { |
| 104 | + every { messageContent.plainText } returns "test_4" |
| 105 | + assertNull(push().first().content) |
| 106 | + } |
| 107 | + |
| 108 | + // test4(2): 符合匹配结果、得到 1 |
| 109 | + mockk<MessageEvent>(relaxed = true) { |
| 110 | + every { messageContent.plainText } returns "test_4 1" |
| 111 | + assertEquals(1, push().first().content) |
| 112 | + } |
| 113 | + |
| 114 | + // test5(1): 不符合匹配结果、不是required=false,参数是可选的,使用默认值,即得到 1 |
| 115 | + mockk<MessageEvent>(relaxed = true) { |
| 116 | + every { messageContent.plainText } returns "test_5" |
| 117 | + assertEquals(1, push().first().content) |
| 118 | + } |
| 119 | + |
| 120 | + // test5(2): 符合匹配结果、得到 2 |
| 121 | + mockk<MessageEvent>(relaxed = true) { |
| 122 | + every { messageContent.plainText } returns "test_5 2" |
| 123 | + assertEquals(2, push().first().content) |
| 124 | + } |
| 125 | + |
| 126 | + // test6(1): 不符合匹配结果、是required=false,参数是可选的,但是参数是可以为null的,因此会填充 null 而不是默认值 |
| 127 | + mockk<MessageEvent>(relaxed = true) { |
| 128 | + every { messageContent.plainText } returns "test_6" |
| 129 | + assertNull(push().first().content) |
| 130 | + } |
| 131 | + |
| 132 | + // test6(2): 符合匹配结果、得到 2 |
| 133 | + mockk<MessageEvent>(relaxed = true) { |
| 134 | + every { messageContent.plainText } returns "test_6 2" |
| 135 | + assertEquals(2, push().first().content) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +@Component |
| 142 | +class TestListenerContainer { |
| 143 | + @Listener |
| 144 | + @Filter("^test_1(\\s+(?<page>\\d+))?") |
| 145 | + fun MessageEvent.handle1( |
| 146 | + @FilterValue("page", false) page: Int = 1 |
| 147 | + ): Int = page |
| 148 | + |
| 149 | + @Listener |
| 150 | + @Filter("^test_2(\\s+(?<page>\\d+))?") |
| 151 | + fun MessageEvent.handle2( |
| 152 | + @FilterValue("page") page: String? |
| 153 | + ): String? = page |
| 154 | + |
| 155 | + @Listener |
| 156 | + @Filter("^test_3(\\s+(?<page>\\d+))?") |
| 157 | + fun MessageEvent.handle3( |
| 158 | + @FilterValue("page") page: String? = null |
| 159 | + ): String? = page |
| 160 | + |
| 161 | + @Listener |
| 162 | + @Filter("^test_4(\\s+(?<page>\\d+))?") |
| 163 | + fun MessageEvent.handle4( |
| 164 | + @FilterValue("page", false) page: Int? = null |
| 165 | + ): Int? = page |
| 166 | + |
| 167 | + @Listener |
| 168 | + @Filter("^test_5(\\s+(?<page>\\d+))?") |
| 169 | + fun MessageEvent.handle5( |
| 170 | + @FilterValue("page") page: Int = 1 |
| 171 | + ): Int = page |
| 172 | + |
| 173 | + @Listener |
| 174 | + @Filter("^test_6(\\s+(?<page>\\d+))?") |
| 175 | + fun MessageEvent.handle6( |
| 176 | + @FilterValue("page", false) page: Int? = 1 |
| 177 | + ): Int? = page |
| 178 | +} |
0 commit comments