1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-24 20:43:33 +08:00

update GroupedCommandSubCommandAnnotationResolver and Test

This commit is contained in:
hundun 2024-08-21 14:33:07 +08:00
parent bf685fc7e6
commit 1d3e03d958
3 changed files with 72 additions and 14 deletions
mirai-console/backend/mirai-console

View File

@ -18,7 +18,6 @@ import net.mamoe.mirai.console.internal.command.CommandReflector
import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import kotlin.DeprecationLevel.*
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION

View File

@ -98,24 +98,26 @@ internal object CompositeCommandSubCommandAnnotationResolver :
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
internal object GroupedCommandSubCommandAnnotationResolver :
SubCommandAnnotationResolver<Any> {
override fun isDeclaredSubCommand(ownerCommand: Any, function: KFunction<*>) =
SubCommandAnnotationResolver<SubCommandGroup> {
override fun isDeclaredSubCommand(ownerCommand: SubCommandGroup, function: KFunction<*>) =
function.hasAnnotation<CompositeCommand.SubCommand>()
override fun getDeclaredSubCommandNames(ownerCommand: Any, function: KFunction<*>): Array<out String> {
override fun getDeclaredSubCommandNames(ownerCommand: SubCommandGroup, function: KFunction<*>): Array<out String> {
val annotated = function.findAnnotation<CompositeCommand.SubCommand>()!!.value
return if (annotated.isEmpty()) arrayOf(function.name)
else annotated
}
override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? = null
override fun getAnnotatedName(ownerCommand: SubCommandGroup, parameter: KParameter): String? =
parameter.findAnnotation<CompositeCommand.Name>()?.value
override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? = null
override fun getDescription(ownerCommand: SubCommandGroup, function: KFunction<*>): String? =
function.findAnnotation<CompositeCommand.Description>()?.value
override fun isCombinedSubCommands(command: Any, kProperty: KProperty<*>): Boolean =
override fun isCombinedSubCommands(command: SubCommandGroup, kProperty: KProperty<*>): Boolean =
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<CompositeCommand.SubCommand>()
override fun getCombinedAdditionNames(command: Any, kProperty: KProperty<*>): Array<out String> {
override fun getCombinedAdditionNames(command: SubCommandGroup, kProperty: KProperty<*>): Array<out String> {
return if (kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>()) {
emptyArray()
} else {
@ -516,4 +518,4 @@ internal class SubCommandReflector<T: Any>(
private fun KParameter.nameForCommandParameter(): String? =
annotationResolver.getAnnotatedName(owner, this) ?: this.name
}
}

View File

@ -37,12 +37,29 @@ import kotlin.test.*
import net.mamoe.mirai.console.command.SubCommandGroup.FlattenSubCommands
/**
* 测试CompositeCommand下接3种子节点AbstractSubCommandGroup下接3种子节点
*
*
* [MyUnifiedCommand : CompositeCommand]
* |
* -------------------------------------------------------
* | \ \
* [ModuleB : AbstractSubCommandGroup] [functionA0:KFunction] [ModuleE : CompositeCommand]
* |
* -----------------------------------------------------------------
* | \ \
* [ModuleC : AbstractSubCommandGroup] [ModuleD : CompositeCommand] [functionB0/functionB1:KFunction]
*
*/
class MyUnifiedCommand : CompositeCommand(
owner, "testMyUnifiedCommand", "tsMUC"
) {
class ModuleAPartB : AbstractSubCommandGroup() {
class ModuleB : AbstractSubCommandGroup() {
@FlattenSubCommands
val partC = ModuleAPartC()
val moduleC = ModuleC()
@FlattenSubCommands
val moduleDInB = ModuleD()
@SubCommand
fun functionB0(arg0: Int) {
@ -54,7 +71,7 @@ class MyUnifiedCommand : CompositeCommand(
}
}
class ModuleAPartC : AbstractSubCommandGroup() {
class ModuleC : AbstractSubCommandGroup() {
@SubCommand
fun functionC0(arg0: Int) {
Testing.ok(arg0)
@ -65,8 +82,33 @@ class MyUnifiedCommand : CompositeCommand(
}
}
@FlattenSubCommands // 新增若干, 不带前缀注册指令, 执行 `/base function10` `/base functionCustomName11`
val partB = ModuleAPartB()
class ModuleD : CompositeCommand(owner, "USELESS") {
@SubCommand
fun functionD0(arg0: Int) {
Testing.ok(arg0)
}
@SubCommand("customNameD1")
fun functionD1(arg0: Int) {
Testing.ok(arg0)
}
}
class ModuleE : CompositeCommand(owner, "USELESS") {
@SubCommand
fun functionE0(arg0: Int) {
Testing.ok(arg0)
}
@SubCommand("customNameE1")
fun functionE1(arg0: Int) {
Testing.ok(arg0)
}
}
@FlattenSubCommands
val moduleB = ModuleB()
@FlattenSubCommands
val moduleE = ModuleE()
@SubCommand
fun functionA0(arg0: Int) {
@ -558,6 +600,21 @@ internal class InstanceTestCommand : AbstractConsoleInstanceTest() {
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameC1 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "functionD0 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameD1 0"))
})
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionA0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionB0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameB1 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionC0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameC1 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionD0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameD1 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionE0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameE1 <arg0>"))
}
}