1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-11 07:54:58 +00:00
arikawa/bot/subcommand_test.go

99 lines
1.9 KiB
Go
Raw Normal View History

2020-01-20 11:15:41 +00:00
// +build unit
2020-01-19 06:06:00 +00:00
package bot
import "testing"
func TestNewSubcommand(t *testing.T) {
_, err := NewSubcommand(&testCommands{})
if err != nil {
t.Fatal("Failed to create new subcommand:", err)
}
}
func TestSubcommand(t *testing.T) {
var given = &testCommands{}
var sub = &Subcommand{
command: given,
}
t.Run("reflect commands", func(t *testing.T) {
if err := sub.reflectCommands(); err != nil {
t.Fatal("Failed to reflect commands:", err)
}
})
t.Run("parse commands", func(t *testing.T) {
if err := sub.parseCommands(); err != nil {
t.Fatal("Failed to parse commands:", err)
}
// !!! CHANGE ME
if len(sub.Commands) != 5 {
2020-01-19 06:06:00 +00:00
t.Fatal("invalid ctx.commands len", len(sub.Commands))
}
var (
foundSend bool
foundCustom bool
foundNoArgs bool
)
for _, this := range sub.Commands {
switch this.Command {
2020-01-19 06:06:00 +00:00
case "send":
foundSend = true
2020-01-24 03:22:24 +00:00
if len(this.Arguments) != 1 {
t.Fatal("invalid arguments len", len(this.Arguments))
2020-01-19 06:06:00 +00:00
}
case "custom":
foundCustom = true
if len(this.Arguments) != 1 {
t.Fatal("arguments should be 1 for custom")
2020-01-19 06:06:00 +00:00
}
case "noArgs":
2020-01-19 06:06:00 +00:00
foundNoArgs = true
2020-01-24 03:22:24 +00:00
if len(this.Arguments) != 0 {
2020-01-19 06:06:00 +00:00
t.Fatal("expected 0 arguments, got non-zero")
}
case "noop", "getCounter":
2020-01-19 06:06:00 +00:00
// Found, but whatever
default:
t.Fatal("Unexpected command:", this.Command)
2020-01-19 06:06:00 +00:00
}
if this.event != typeMessageCreate {
t.Fatal("invalid event type:", this.event.String())
}
}
if !foundSend {
t.Fatal("missing send")
}
if !foundCustom {
t.Fatal("missing custom")
}
if !foundNoArgs {
t.Fatal("missing noargs")
}
})
2020-02-05 04:29:45 +00:00
t.Run("help commands", func(t *testing.T) {
if h := sub.Help("", "", false); h == "" {
t.Fatal("Empty subcommand help?")
}
})
2020-01-19 06:06:00 +00:00
}
func BenchmarkSubcommandConstructor(b *testing.B) {
for i := 0; i < b.N; i++ {
NewSubcommand(&testCommands{})
}
}