Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
hundun000 committed Jun 30, 2022
1 parent a86891d commit 826dbe7
Showing 1 changed file with 124 additions and 2 deletions.
126 changes: 124 additions & 2 deletions mirai-console/backend/mirai-console/test/command/CommandContextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,39 @@ internal class CommandContextTest : AbstractCommandTest() {
companion object Key : AbstractMessageKey<MyMetadata>({ it.safeCast() })
}


///////////////////////////////////////////////////////////////////////////
// RawCommand
///////////////////////////////////////////////////////////////////////////

@TestFactory
fun `can execute with sender`(): List<DynamicTest> {
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) {
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -97,6 +127,33 @@ internal class CommandContextTest : AbstractCommandTest() {

@TestFactory
fun `RawCommand can execute and get original chain`(): List<DynamicTest> {
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) {
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -151,6 +212,33 @@ internal class CommandContextTest : AbstractCommandTest() {

@TestFactory
fun `can execute and get metadata`(): List<DynamicTest> {
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") {
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -207,6 +298,33 @@ internal class CommandContextTest : AbstractCommandTest() {

@TestFactory
fun `RawCommand can execute and get chain including metadata`(): List<DynamicTest> {
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") {
Expand All @@ -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") {
Expand All @@ -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 {
Expand Down

0 comments on commit 826dbe7

Please sign in to comment.