-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #896 from simple-robot/optimize-895
优化统一MergedBinder对null结果、失败结果的处理
- Loading branch information
Showing
4 changed files
with
228 additions
and
7 deletions.
There are no files selected for viewing
178 changes: 178 additions & 0 deletions
178
...e-spring-boot-starter/src/test/kotlin/love/forte/simbot/spring/test/DefaultBinderTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* Project https://github.com/simple-robot/simpler-robot | ||
* Email [email protected] | ||
* | ||
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.). | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package love.forte.simbot.spring.test | ||
|
||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.runBlocking | ||
import love.forte.simbot.application.Application | ||
import love.forte.simbot.event.* | ||
import love.forte.simbot.quantcat.common.annotations.Filter | ||
import love.forte.simbot.quantcat.common.annotations.FilterValue | ||
import love.forte.simbot.quantcat.common.annotations.Listener | ||
import love.forte.simbot.spring.EnableSimbot | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.stereotype.Component | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFails | ||
import kotlin.test.assertNull | ||
|
||
|
||
/** | ||
* 测试来源:https://github.com/simple-robot/simpler-robot/issues/895 | ||
* | ||
* @author ForteScarlet | ||
*/ | ||
@SpringBootTest( | ||
classes = [ | ||
DefaultBinderTests::class, | ||
TestListenerContainer::class, | ||
] | ||
) | ||
@EnableSimbot | ||
open class DefaultBinderTests { | ||
|
||
@Test | ||
fun binderTest1( | ||
@Autowired application: Application | ||
) { | ||
fun Event.push(): List<EventResult> { | ||
return runBlocking { | ||
application.eventDispatcher.push(this@push) | ||
.throwIfError() | ||
.filterNotInvalid() | ||
.toList() | ||
} | ||
} | ||
|
||
// test1: 应当得到 page=1, 因为匹配内容 page 实际不存在,使用默认值 | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_1 1" | ||
assertEquals(1, push().first().content) | ||
} | ||
|
||
// test2(1): 符合匹配结果,应当得到 1 | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_2 1" | ||
assertEquals("1", push().first().content) | ||
} | ||
|
||
// test2(2): 不符合匹配结果、不是required=false,理应报错 | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_2" | ||
assertFails { push() } | ||
} | ||
|
||
// test3(1): 不符合匹配结果、不是required=false,但参数是可选的,使用默认值,即得到 null | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_3" | ||
assertNull(push().first().content) | ||
} | ||
|
||
// test3(2): 符合匹配结果、得到 "1" | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_3 1" | ||
assertEquals("1", push().first().content) | ||
} | ||
|
||
// test4(1): 不符合匹配结果、是required=false,参数是可选的,使用默认值,即得到 null | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_4" | ||
assertNull(push().first().content) | ||
} | ||
|
||
// test4(2): 符合匹配结果、得到 1 | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_4 1" | ||
assertEquals(1, push().first().content) | ||
} | ||
|
||
// test5(1): 不符合匹配结果、不是required=false,参数是可选的,使用默认值,即得到 1 | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_5" | ||
assertEquals(1, push().first().content) | ||
} | ||
|
||
// test5(2): 符合匹配结果、得到 2 | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_5 2" | ||
assertEquals(2, push().first().content) | ||
} | ||
|
||
// test6(1): 不符合匹配结果、是required=false,参数是可选的,但是参数是可以为null的,因此会填充 null 而不是默认值 | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_6" | ||
assertNull(push().first().content) | ||
} | ||
|
||
// test6(2): 符合匹配结果、得到 2 | ||
mockk<MessageEvent>(relaxed = true) { | ||
every { messageContent.plainText } returns "test_6 2" | ||
assertEquals(2, push().first().content) | ||
} | ||
} | ||
|
||
} | ||
|
||
@Component | ||
class TestListenerContainer { | ||
@Listener | ||
@Filter("^test_1(\\s+(?<page>\\d+))?") | ||
fun MessageEvent.handle1( | ||
@FilterValue("page", false) page: Int = 1 | ||
): Int = page | ||
|
||
@Listener | ||
@Filter("^test_2(\\s+(?<page>\\d+))?") | ||
fun MessageEvent.handle2( | ||
@FilterValue("page") page: String? | ||
): String? = page | ||
|
||
@Listener | ||
@Filter("^test_3(\\s+(?<page>\\d+))?") | ||
fun MessageEvent.handle3( | ||
@FilterValue("page") page: String? = null | ||
): String? = page | ||
|
||
@Listener | ||
@Filter("^test_4(\\s+(?<page>\\d+))?") | ||
fun MessageEvent.handle4( | ||
@FilterValue("page", false) page: Int? = null | ||
): Int? = page | ||
|
||
@Listener | ||
@Filter("^test_5(\\s+(?<page>\\d+))?") | ||
fun MessageEvent.handle5( | ||
@FilterValue("page") page: Int = 1 | ||
): Int = page | ||
|
||
@Listener | ||
@Filter("^test_6(\\s+(?<page>\\d+))?") | ||
fun MessageEvent.handle6( | ||
@FilterValue("page", false) page: Int? = 1 | ||
): Int? = page | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* Project https://github.com/simple-robot/simpler-robot | ||
* Email [email protected] | ||
* | ||
* This file is part of the Simple Robot Library. | ||
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.). | ||
* | ||
* 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 | ||
|
@@ -26,14 +26,17 @@ package love.forte.simbot.quantcat.common.annotations | |
import love.forte.simbot.quantcat.common.filter.FilterValueProperties | ||
|
||
/** | ||
* 指定一个参数,此参数为通过 [love.forte.simbot.quantcat.annotations.Filter] | ||
* 指定一个参数,此参数为通过 [love.forte.simbot.quantcat.common.annotations.Filter] | ||
* 解析而得到的动态参数提取器中的内容。 | ||
* | ||
* 参数提取格式基于正则匹配模式,参考 [Filter.value] 中的相关说明。 | ||
* | ||
* @param value 所需动态参数的key。 | ||
* @param required 对于参数绑定器来讲其是否为必须的。 | ||
* 如果不是必须的,则在无法获取参数后传递null作为结果,否则将会抛出异常并交由后续绑定器处理。 | ||
* 如果不是必须的,则在无法获取参数后传递 `null` 作为结果 | ||
* (此结果被视为正确结果。换言之如果参数为 `nullable`, | ||
* 但是存在一个不是 `null` 的默认值,则最终的参数值依然为 `null`), | ||
* 否则将会抛出异常并交由后续绑定器处理。 | ||
*/ | ||
@Target(AnnotationTarget.VALUE_PARAMETER) | ||
public annotation class FilterValue(val value: String, val required: Boolean = true) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* Project https://github.com/simple-robot/simpler-robot | ||
* Email [email protected] | ||
* | ||
* This file is part of the Simple Robot Library. | ||
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.). | ||
* | ||
* 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 | ||
|
@@ -30,6 +30,10 @@ import love.forte.simbot.event.EventListenerContext | |
* | ||
* 对于一个可执行函数的参数 `KParameter` 所需的结果获取器。 | ||
* | ||
* 没有任何绑定器时, | ||
* 通常会使用 [EmptyBinder][love.forte.simbot.quantcat.common.binder.impl.EmptyBinder], | ||
* 当存在多个绑定器时,通常会使用 [MergedBinder][love.forte.simbot.quantcat.common.binder.impl.MergedBinder]。 | ||
* | ||
*/ | ||
public interface ParameterBinder { | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters