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
|
2020-01-24 03:17:03 +00:00
|
|
|
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 {
|
2020-01-24 03:17:03 +00:00
|
|
|
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
|
2020-01-25 22:07:49 +00:00
|
|
|
if len(this.Arguments) != 1 {
|
|
|
|
t.Fatal("arguments should be 1 for custom")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:08:12 +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")
|
|
|
|
}
|
|
|
|
|
2020-01-24 16:08:12 +00:00
|
|
|
case "noop", "getCounter":
|
2020-01-19 06:06:00 +00:00
|
|
|
// Found, but whatever
|
|
|
|
|
|
|
|
default:
|
2020-01-24 03:17:03 +00:00
|
|
|
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{})
|
|
|
|
}
|
|
|
|
}
|