diff --git a/mirai-console/backend/mirai-console/test/command/CommandContextTest.kt b/mirai-console/backend/mirai-console/test/command/CommandContextTest.kt index 1a59e80b605..2da0fb6da5e 100644 --- a/mirai-console/backend/mirai-console/test/command/CommandContextTest.kt +++ b/mirai-console/backend/mirai-console/test/command/CommandContextTest.kt @@ -36,13 +36,39 @@ internal class CommandContextTest : AbstractCommandTest() { companion object Key : AbstractMessageKey({ it.safeCast() }) } - /////////////////////////////////////////////////////////////////////////// // RawCommand /////////////////////////////////////////////////////////////////////////// @TestFactory fun `can execute with sender`(): List { + val containerCompositeCommand = object : CompositeCommand(owner, "test") { + @ChildCommand + val childFoo = object : CompositeCommand(owner, "useless") { + @SubCommand + fun CommandContext.childSub(arg: MessageChain) { + Testing.ok(arg) + } + } + @SubCommand + fun CommandContext.containerSub(arg: MessageChain) { + Testing.ok(arg) + } + } + val jContainerCompositeCommand = object : JCompositeCommand(owner, "test") { + @ChildCommand + val childFoo = object : JCompositeCommand(owner, "useless") { + @SubCommand + fun childSub(context: CommandContext, arg: MessageChain) { + Testing.ok(arg) + } + } + @SubCommand + fun containerSub(context: CommandContext, arg: MessageChain) { + Testing.ok(arg) + } + } + return listOf( object : RawCommand(owner, "test") { override suspend fun CommandContext.onCommand(args: MessageChain) { @@ -61,6 +87,8 @@ internal class CommandContextTest : AbstractCommandTest() { Testing.ok(arg) } } to "/test sub foo", + containerCompositeCommand to "/test childSub foo", + containerCompositeCommand to "/test containerSub foo", object : JRawCommand(owner, "test") { override fun onCommand(context: CommandContext, args: MessageChain) { @@ -79,6 +107,8 @@ internal class CommandContextTest : AbstractCommandTest() { Testing.ok(arg) } } to "/test sub foo", + jContainerCompositeCommand to "/test childSub foo", + jContainerCompositeCommand to "/test containerSub foo", ).map { (instance, cmd) -> DynamicTest.dynamicTest(instance::class.supertypes.first().classifierAsKClass().simpleName) { runBlocking { @@ -97,6 +127,33 @@ internal class CommandContextTest : AbstractCommandTest() { @TestFactory fun `RawCommand can execute and get original chain`(): List { + val containerCompositeCommand = object : CompositeCommand(owner, "test") { + @ChildCommand + val childFoo = object : CompositeCommand(owner, "useless") { + @SubCommand + fun CommandContext.childSub(arg: MessageChain) { + Testing.ok(originalMessage) + } + } + @SubCommand + fun CommandContext.containerSub(arg: MessageChain) { + Testing.ok(originalMessage) + } + } + val jContainerCompositeCommand = object : JCompositeCommand(owner, "test") { + @ChildCommand + val childFoo = object : JCompositeCommand(owner, "useless") { + @SubCommand + fun childSub(context: CommandContext, arg: MessageChain) { + Testing.ok(context.originalMessage) + } + } + @SubCommand + fun containerSub(context: CommandContext, arg: MessageChain) { + Testing.ok(context.originalMessage) + } + } + return listOf( object : RawCommand(owner, "test") { override suspend fun CommandContext.onCommand(args: MessageChain) { @@ -115,6 +172,8 @@ internal class CommandContextTest : AbstractCommandTest() { Testing.ok(originalMessage) } } to "/test sub foo", + containerCompositeCommand to "/test childSub foo", + containerCompositeCommand to "/test containerSub foo", object : JRawCommand(owner, "test") { override fun onCommand(context: CommandContext, args: MessageChain) { @@ -133,6 +192,8 @@ internal class CommandContextTest : AbstractCommandTest() { Testing.ok(context.originalMessage) } } to "/test sub foo", + jContainerCompositeCommand to "/test childSub foo", + jContainerCompositeCommand to "/test containerSub foo", ).map { (instance, cmd) -> DynamicTest.dynamicTest(instance::class.supertypes.first().classifierAsKClass().simpleName) { runBlocking { @@ -151,6 +212,33 @@ internal class CommandContextTest : AbstractCommandTest() { @TestFactory fun `can execute and get metadata`(): List { + val containerCompositeCommand = object : CompositeCommand(owner, "test") { + @ChildCommand + val childFoo = object : CompositeCommand(owner, "useless") { + @SubCommand + fun CommandContext.childSub(arg: MessageChain) { + Testing.ok(originalMessage[MyMetadata]) + } + } + @SubCommand + fun CommandContext.containerSub(arg: MessageChain) { + Testing.ok(originalMessage[MyMetadata]) + } + } + val jContainerCompositeCommand = object : JCompositeCommand(owner, "test") { + @ChildCommand + val childFoo = object : JCompositeCommand(owner, "useless") { + @SubCommand + fun childSub(context: CommandContext, arg: MessageChain) { + Testing.ok(context.originalMessage[MyMetadata]) + } + } + @SubCommand + fun containerSub(context: CommandContext, arg: MessageChain) { + Testing.ok(context.originalMessage[MyMetadata]) + } + } + val metadata = MyMetadata() return listOf( object : RawCommand(owner, "test") { @@ -170,7 +258,8 @@ internal class CommandContextTest : AbstractCommandTest() { Testing.ok(originalMessage[MyMetadata]) } } to messageChainOf(PlainText("/test"), PlainText("sub"), metadata, PlainText("foo")), - + containerCompositeCommand to messageChainOf(PlainText("/test"), PlainText("childSub"), metadata, PlainText("foo")), + containerCompositeCommand to messageChainOf(PlainText("/test"), PlainText("containerSub"), metadata, PlainText("foo")), object : JRawCommand(owner, "test") { override fun onCommand(context: CommandContext, args: MessageChain) { @@ -189,6 +278,8 @@ internal class CommandContextTest : AbstractCommandTest() { Testing.ok(context.originalMessage[MyMetadata]) } } to messageChainOf(PlainText("/test"), PlainText("sub"), metadata, PlainText("foo")), + jContainerCompositeCommand to messageChainOf(PlainText("/test"), PlainText("childSub"), metadata, PlainText("foo")), + jContainerCompositeCommand to messageChainOf(PlainText("/test"), PlainText("containerSub"), metadata, PlainText("foo")), ).map { (instance, cmd) -> DynamicTest.dynamicTest(instance::class.supertypes.first().classifierAsKClass().simpleName) { runBlocking { @@ -207,6 +298,33 @@ internal class CommandContextTest : AbstractCommandTest() { @TestFactory fun `RawCommand can execute and get chain including metadata`(): List { + val containerCompositeCommand = object : CompositeCommand(owner, "test") { + @ChildCommand + val childFoo = object : CompositeCommand(owner, "useless") { + @SubCommand + fun CommandContext.childSub(arg: MessageChain) { + Testing.ok(originalMessage) + } + } + @SubCommand + fun CommandContext.containerSub(arg: MessageChain) { + Testing.ok(originalMessage) + } + } + val jContainerCompositeCommand = object : JCompositeCommand(owner, "test") { + @ChildCommand + val childFoo = object : JCompositeCommand(owner, "useless") { + @SubCommand + fun childSub(context: CommandContext, arg: MessageChain) { + Testing.ok(context.originalMessage) + } + } + @SubCommand + fun containerSub(context: CommandContext, arg: MessageChain) { + Testing.ok(context.originalMessage) + } + } + val metadata = MyMetadata() return listOf( object : RawCommand(owner, "test") { @@ -226,6 +344,8 @@ internal class CommandContextTest : AbstractCommandTest() { Testing.ok(originalMessage) } } to messageChainOf(PlainText("/test"), PlainText("sub"), metadata, PlainText("foo")), + containerCompositeCommand to messageChainOf(PlainText("/test"), PlainText("childSub"), metadata, PlainText("foo")), + containerCompositeCommand to messageChainOf(PlainText("/test"), PlainText("containerSub"), metadata, PlainText("foo")), object : JRawCommand(owner, "test") { @@ -245,6 +365,8 @@ internal class CommandContextTest : AbstractCommandTest() { Testing.ok(context.originalMessage) } } to messageChainOf(PlainText("/test"), PlainText("sub"), metadata, PlainText("foo")), + jContainerCompositeCommand to messageChainOf(PlainText("/test"), PlainText("childSub"), metadata, PlainText("foo")), + jContainerCompositeCommand to messageChainOf(PlainText("/test"), PlainText("containerSub"), metadata, PlainText("foo")), ).map { (instance, cmd) -> DynamicTest.dynamicTest(instance::class.supertypes.first().classifierAsKClass().simpleName) { runBlocking {