mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-11 02:50:15 +08:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
7bdeacf62a
@ -37,6 +37,8 @@ public abstract class AbstractCommand
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
Command.checkCommandName(primaryName)
|
Command.checkCommandName(primaryName)
|
||||||
|
@Suppress("LeakingThis")
|
||||||
|
Command.checkCommandOwner(this)
|
||||||
secondaryNames.forEach(Command.Companion::checkCommandName)
|
secondaryNames.forEach(Command.Companion::checkCommandName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,5 +117,19 @@ public interface Command {
|
|||||||
name.contains('.') -> throw IllegalArgumentException("'.' is forbidden in command name.")
|
name.contains('.') -> throw IllegalArgumentException("'.' is forbidden in command name.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查指令 [owner] 的合法性, 在非法时抛出 [IllegalArgumentException]
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
@Throws(IllegalArgumentException::class)
|
||||||
|
public fun checkCommandOwner(command: Command) {
|
||||||
|
val owner = command.owner
|
||||||
|
if (owner is ConsoleCommandOwner) {
|
||||||
|
if (command.javaClass.enclosingClass != BuiltInCommands::class.java) {
|
||||||
|
throw IllegalArgumentException("ConsoleCommandOwner is not allowed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ import org.junit.jupiter.api.Test
|
|||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
object TestCompositeCommand : CompositeCommand(
|
object TestCompositeCommand : CompositeCommand(
|
||||||
ConsoleCommandOwner,
|
owner,
|
||||||
"testComposite", "tsC"
|
"testComposite", "tsC"
|
||||||
) {
|
) {
|
||||||
@SubCommand
|
@SubCommand
|
||||||
@ -49,7 +49,7 @@ object TestCompositeCommand : CompositeCommand(
|
|||||||
}
|
}
|
||||||
|
|
||||||
object TestRawCommand : RawCommand(
|
object TestRawCommand : RawCommand(
|
||||||
ConsoleCommandOwner,
|
owner,
|
||||||
"testRaw"
|
"testRaw"
|
||||||
) {
|
) {
|
||||||
override suspend fun CommandSender.onCommand(args: MessageChain) {
|
override suspend fun CommandSender.onCommand(args: MessageChain) {
|
||||||
@ -65,7 +65,9 @@ object TestSimpleCommand : RawCommand(owner, "testSimple", "tsS") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal val sender by lazy { ConsoleCommandSender }
|
internal val sender by lazy { ConsoleCommandSender }
|
||||||
internal val owner by lazy { ConsoleCommandOwner }
|
|
||||||
|
internal object TestUnitCommandOwner : CommandOwner by ConsoleCommandOwner
|
||||||
|
internal val owner by lazy { TestUnitCommandOwner }
|
||||||
|
|
||||||
|
|
||||||
@OptIn(ExperimentalCommandDescriptors::class)
|
@OptIn(ExperimentalCommandDescriptors::class)
|
||||||
@ -88,12 +90,13 @@ internal class TestCommand {
|
|||||||
fun testRegister() {
|
fun testRegister() {
|
||||||
try {
|
try {
|
||||||
unregisterAllCommands(ConsoleCommandOwner) // builtins
|
unregisterAllCommands(ConsoleCommandOwner) // builtins
|
||||||
|
unregisterAllCommands(owner) // testing unit
|
||||||
unregisterCommand(TestSimpleCommand)
|
unregisterCommand(TestSimpleCommand)
|
||||||
|
|
||||||
assertTrue(TestCompositeCommand.register())
|
assertTrue(TestCompositeCommand.register())
|
||||||
assertFalse(TestCompositeCommand.register())
|
assertFalse(TestCompositeCommand.register())
|
||||||
|
|
||||||
assertEquals(1, getRegisteredCommands(ConsoleCommandOwner).size)
|
assertEquals(1, getRegisteredCommands(owner).size)
|
||||||
|
|
||||||
assertEquals(1, CommandManagerImpl._registeredCommands.size)
|
assertEquals(1, CommandManagerImpl._registeredCommands.size)
|
||||||
assertEquals(2,
|
assertEquals(2,
|
||||||
@ -198,7 +201,7 @@ internal class TestCommand {
|
|||||||
fun `composite sub command resolution conflict`() {
|
fun `composite sub command resolution conflict`() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val composite = object : CompositeCommand(
|
val composite = object : CompositeCommand(
|
||||||
ConsoleCommandOwner,
|
owner,
|
||||||
"tr"
|
"tr"
|
||||||
) {
|
) {
|
||||||
@Suppress("UNUSED_PARAMETER")
|
@Suppress("UNUSED_PARAMETER")
|
||||||
@ -233,7 +236,7 @@ internal class TestCommand {
|
|||||||
)
|
)
|
||||||
|
|
||||||
val composite = object : CompositeCommand(
|
val composite = object : CompositeCommand(
|
||||||
ConsoleCommandOwner,
|
owner,
|
||||||
"test22",
|
"test22",
|
||||||
overrideContext = buildCommandArgumentContext {
|
overrideContext = buildCommandArgumentContext {
|
||||||
add(object : CommandValueArgumentParser<MyClass> {
|
add(object : CommandValueArgumentParser<MyClass> {
|
||||||
@ -291,7 +294,7 @@ internal class TestCommand {
|
|||||||
fun `test optional argument command`() {
|
fun `test optional argument command`() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val optionCommand = object : CompositeCommand(
|
val optionCommand = object : CompositeCommand(
|
||||||
ConsoleCommandOwner,
|
owner,
|
||||||
"testOptional"
|
"testOptional"
|
||||||
) {
|
) {
|
||||||
@SubCommand
|
@SubCommand
|
||||||
@ -315,7 +318,7 @@ internal class TestCommand {
|
|||||||
fun `test vararg`() {
|
fun `test vararg`() {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val optionCommand = object : CompositeCommand(
|
val optionCommand = object : CompositeCommand(
|
||||||
ConsoleCommandOwner,
|
owner,
|
||||||
"test"
|
"test"
|
||||||
) {
|
) {
|
||||||
@SubCommand
|
@SubCommand
|
||||||
|
Loading…
Reference in New Issue
Block a user