2020-01-19 06:06:00 +00:00
|
|
|
package bot
|
|
|
|
|
2020-05-04 01:35:51 +00:00
|
|
|
import (
|
2020-05-14 03:42:31 +00:00
|
|
|
"strings"
|
2020-05-04 01:35:51 +00:00
|
|
|
"testing"
|
|
|
|
)
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
func TestUnderline(t *testing.T) {
|
|
|
|
HelpUnderline = false
|
|
|
|
if underline("astolfo") != "astolfo" {
|
|
|
|
t.Fatal("Unexpected underlining with HelpUnderline = false")
|
|
|
|
}
|
|
|
|
|
|
|
|
HelpUnderline = true
|
|
|
|
if underline("arikawa hime") != "__arikawa hime__" {
|
|
|
|
t.Fatal("Unexpected normal style with HelpUnderline = true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
func TestNewSubcommand(t *testing.T) {
|
2020-05-04 01:33:24 +00:00
|
|
|
_, err := NewSubcommand(&testc{})
|
2020-01-19 06:06:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create new subcommand:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubcommand(t *testing.T) {
|
2020-05-04 01:33:24 +00:00
|
|
|
var given = &testc{}
|
2020-01-19 06:06:00 +00:00
|
|
|
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-05-13 04:37:06 +00:00
|
|
|
if len(sub.Commands) < 8 {
|
|
|
|
t.Fatal("too low sub.Methods len", len(sub.Commands))
|
|
|
|
}
|
|
|
|
if len(sub.Events) < 1 {
|
|
|
|
t.Fatal("No events found.")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
foundSend bool
|
|
|
|
foundCustom bool
|
|
|
|
foundNoArgs bool
|
|
|
|
)
|
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
t.Run("init commands", func(t *testing.T) {
|
|
|
|
ctx := &Context{}
|
|
|
|
if err := sub.InitCommands(ctx); err != nil {
|
|
|
|
t.Fatal("Failed to init commands:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-05 04:29:45 +00:00
|
|
|
t.Run("help commands", func(t *testing.T) {
|
2020-05-14 03:42:31 +00:00
|
|
|
h := sub.Help()
|
|
|
|
if h == "" {
|
2020-02-05 04:29:45 +00:00
|
|
|
t.Fatal("Empty subcommand help?")
|
|
|
|
}
|
2020-05-14 03:42:31 +00:00
|
|
|
|
|
|
|
if strings.Contains(h, "hidden") {
|
|
|
|
t.Fatal("Hidden command shown in help:\n", h)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("change command", func(t *testing.T) {
|
|
|
|
sub.ChangeCommandInfo("Noop", "crossdressing", "best")
|
|
|
|
if h := sub.Help(); !strings.Contains(h, "crossdressing: best") {
|
|
|
|
t.Fatal("Changed command is not in help.")
|
|
|
|
}
|
2020-02-05 04:29:45 +00:00
|
|
|
})
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSubcommandConstructor(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-05-04 01:33:24 +00:00
|
|
|
NewSubcommand(&testc{})
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
}
|