2020-01-19 06:06:00 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
2020-01-26 05:43:42 +00:00
|
|
|
"errors"
|
2020-05-04 06:48:07 +00:00
|
|
|
"fmt"
|
2020-01-19 06:06:00 +00:00
|
|
|
"reflect"
|
2020-01-24 03:17:03 +00:00
|
|
|
"strconv"
|
2020-01-19 06:06:00 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-05-04 06:48:07 +00:00
|
|
|
"time"
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state/store"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/handler"
|
2020-01-19 06:06:00 +00:00
|
|
|
)
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
type testc struct {
|
2020-01-24 03:17:03 +00:00
|
|
|
Ctx *Context
|
|
|
|
Return chan interface{}
|
|
|
|
Counter uint64
|
2020-05-14 03:42:31 +00:00
|
|
|
Typed int8
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
func (t *testc) Setup(sub *Subcommand) {
|
2020-11-13 03:02:52 +00:00
|
|
|
sub.AddMiddleware([]string{"*", "GetCounter"}, func(v interface{}) {
|
2020-05-10 08:45:00 +00:00
|
|
|
t.Counter++
|
|
|
|
})
|
|
|
|
sub.AddMiddleware("*", func(*gateway.MessageCreateEvent) {
|
|
|
|
t.Counter++
|
|
|
|
})
|
2020-05-14 03:42:31 +00:00
|
|
|
// stub middleware for testing
|
2020-11-13 03:02:52 +00:00
|
|
|
sub.AddMiddleware(t.OnTyping, func(*gateway.TypingStartEvent) {
|
2020-05-14 03:42:31 +00:00
|
|
|
t.Typed = 2
|
|
|
|
})
|
2020-11-13 03:02:52 +00:00
|
|
|
sub.Hide(t.Hidden)
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
2020-05-14 03:42:31 +00:00
|
|
|
func (t *testc) Hidden(*gateway.MessageCreateEvent) {}
|
|
|
|
func (t *testc) Noop(*gateway.MessageCreateEvent) {}
|
2020-05-10 08:45:00 +00:00
|
|
|
func (t *testc) GetCounter(*gateway.MessageCreateEvent) {
|
2020-01-24 03:17:03 +00:00
|
|
|
t.Return <- strconv.FormatUint(t.Counter, 10)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
2020-05-04 01:33:24 +00:00
|
|
|
func (t *testc) Send(_ *gateway.MessageCreateEvent, args ...string) error {
|
2020-05-03 22:59:10 +00:00
|
|
|
t.Return <- args
|
2020-01-19 06:06:00 +00:00
|
|
|
return errors.New("oh no")
|
|
|
|
}
|
2020-05-14 03:42:31 +00:00
|
|
|
func (t *testc) Custom(_ *gateway.MessageCreateEvent, c *ArgumentParts) {
|
|
|
|
t.Return <- []string(*c)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
2020-05-04 05:57:44 +00:00
|
|
|
func (t *testc) Variadic(_ *gateway.MessageCreateEvent, c ...*customParsed) {
|
2020-05-03 22:59:10 +00:00
|
|
|
t.Return <- c[len(c)-1]
|
|
|
|
}
|
2020-07-29 23:29:01 +00:00
|
|
|
func (t *testc) TrailCustom(_ *gateway.MessageCreateEvent, _ string, c ArgumentParts) {
|
2020-05-14 03:42:31 +00:00
|
|
|
t.Return <- c
|
2020-05-04 05:57:44 +00:00
|
|
|
}
|
|
|
|
func (t *testc) Content(_ *gateway.MessageCreateEvent, c RawArguments) {
|
|
|
|
t.Return <- c
|
2020-05-04 01:33:24 +00:00
|
|
|
}
|
2020-05-10 08:45:00 +00:00
|
|
|
func (t *testc) NoArgs(*gateway.MessageCreateEvent) error {
|
2020-01-19 06:06:00 +00:00
|
|
|
return errors.New("passed")
|
|
|
|
}
|
2020-05-10 08:45:00 +00:00
|
|
|
func (t *testc) OnTyping(*gateway.TypingStartEvent) {
|
2020-05-14 03:42:31 +00:00
|
|
|
t.Typed--
|
2020-01-24 16:08:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
func TestNewContext(t *testing.T) {
|
2020-07-29 23:29:01 +00:00
|
|
|
var s = &state.State{
|
2020-11-30 01:19:59 +00:00
|
|
|
Cabinet: store.NoopCabinet,
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
c, err := New(s, &testc{})
|
2020-01-19 06:06:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create new context:", err)
|
|
|
|
}
|
2020-02-05 04:29:45 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(c.Subcommands(), c.subcommands) {
|
|
|
|
t.Fatal("Subcommands mismatch.")
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext(t *testing.T) {
|
2020-05-04 01:33:24 +00:00
|
|
|
var given = &testc{}
|
2020-07-29 23:29:01 +00:00
|
|
|
var s = &state.State{
|
2020-11-30 01:19:59 +00:00
|
|
|
Cabinet: store.NoopCabinet,
|
2020-05-14 03:42:31 +00:00
|
|
|
Handler: handler.New(),
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
sub, err := NewSubcommand(given)
|
2020-01-19 06:06:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create subcommand:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctx = &Context{
|
2020-05-14 03:42:31 +00:00
|
|
|
Name: "arikawa/bot test",
|
|
|
|
Description: "Just a test.",
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
Subcommand: sub,
|
|
|
|
State: s,
|
2021-06-10 23:48:32 +00:00
|
|
|
ParseArgs: DefaultArgsParser,
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("init commands", func(t *testing.T) {
|
|
|
|
if err := ctx.Subcommand.InitCommands(ctx); err != nil {
|
|
|
|
t.Fatal("Failed to init commands:", err)
|
|
|
|
}
|
|
|
|
|
2020-11-30 01:19:59 +00:00
|
|
|
if given.Ctx != ctx {
|
|
|
|
t.Fatal("given Context field has invalid pointer")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-05 04:29:45 +00:00
|
|
|
t.Run("find commands", func(t *testing.T) {
|
2020-05-14 03:42:31 +00:00
|
|
|
cmd := ctx.FindCommand("", "NoArgs")
|
2020-02-05 04:29:45 +00:00
|
|
|
if cmd == nil {
|
|
|
|
t.Fatal("Failed to find NoArgs")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
t.Run("middleware", func(t *testing.T) {
|
2020-04-06 20:31:27 +00:00
|
|
|
ctx.HasPrefix = NewPrefix("pls do ")
|
2020-01-24 03:17:03 +00:00
|
|
|
|
|
|
|
// This should trigger the middleware first.
|
2020-05-10 08:45:00 +00:00
|
|
|
if err := expect(ctx, given, "3", "pls do getCounter"); err != nil {
|
2020-01-24 03:17:03 +00:00
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-10-29 05:49:18 +00:00
|
|
|
t.Run("derive intents", func(t *testing.T) {
|
|
|
|
intents := ctx.DeriveIntents()
|
|
|
|
|
|
|
|
assertIntents := func(target gateway.Intents, name string) {
|
|
|
|
if !intents.Has(target) {
|
|
|
|
t.Error("Derived intents do not have", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIntents(gateway.IntentGuildMessages, "guild messages")
|
|
|
|
assertIntents(gateway.IntentDirectMessages, "direct messages")
|
|
|
|
assertIntents(gateway.IntentGuildMessageTyping, "guild typing")
|
|
|
|
assertIntents(gateway.IntentDirectMessageTyping, "direct message typing")
|
|
|
|
})
|
|
|
|
|
2020-01-24 16:08:12 +00:00
|
|
|
t.Run("typing event", func(t *testing.T) {
|
|
|
|
typing := &gateway.TypingStartEvent{}
|
|
|
|
|
|
|
|
if err := ctx.callCmd(typing); err != nil {
|
|
|
|
t.Fatal("Failed to call with TypingStart:", err)
|
|
|
|
}
|
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
// -1 none ran
|
|
|
|
if given.Typed != 1 {
|
2020-01-24 16:08:12 +00:00
|
|
|
t.Fatal("Typed bool is false")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
t.Run("call command", func(t *testing.T) {
|
|
|
|
// Set a custom prefix
|
2020-04-06 20:31:27 +00:00
|
|
|
ctx.HasPrefix = NewPrefix("~")
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
var (
|
2020-07-29 23:29:01 +00:00
|
|
|
send = "hacka doll no. 3"
|
2020-05-03 22:59:10 +00:00
|
|
|
expects = []string{"hacka", "doll", "no.", "3"}
|
|
|
|
)
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
if err := expect(ctx, given, expects, "~send "+send); err.Error() != "oh no" {
|
2020-01-24 03:17:03 +00:00
|
|
|
t.Fatal("Unexpected error:", err)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-04 05:57:44 +00:00
|
|
|
t.Run("call command rawarguments", func(t *testing.T) {
|
|
|
|
ctx.HasPrefix = NewPrefix("!")
|
|
|
|
expects := RawArguments("just things")
|
|
|
|
|
2020-05-04 06:48:07 +00:00
|
|
|
if err := expect(ctx, given, expects, "!content just things"); err != nil {
|
2020-05-04 05:57:44 +00:00
|
|
|
t.Fatal("Unexpected call error:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
t.Run("call command custom manual parser", func(t *testing.T) {
|
2020-04-06 20:31:27 +00:00
|
|
|
ctx.HasPrefix = NewPrefix("!")
|
2020-05-04 01:33:24 +00:00
|
|
|
expects := []string{"arg1", ":)"}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-04 06:48:07 +00:00
|
|
|
if err := expect(ctx, given, expects, "!custom arg1 :)"); err != nil {
|
2020-01-19 06:06:00 +00:00
|
|
|
t.Fatal("Unexpected call error:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
t.Run("call command custom variadic parser", func(t *testing.T) {
|
|
|
|
ctx.HasPrefix = NewPrefix("!")
|
|
|
|
expects := &customParsed{true}
|
|
|
|
|
2020-05-04 06:48:07 +00:00
|
|
|
if err := expect(ctx, given, expects, "!variadic bruh moment"); err != nil {
|
2020-05-03 22:59:10 +00:00
|
|
|
t.Fatal("Unexpected call error:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
t.Run("call command custom trailing manual parser", func(t *testing.T) {
|
|
|
|
ctx.HasPrefix = NewPrefix("!")
|
2020-05-14 03:42:31 +00:00
|
|
|
expects := ArgumentParts{"arikawa"}
|
2020-05-04 01:33:24 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
if err := sendMsg(ctx, given, &expects, "!trailCustom hime arikawa"); err != nil {
|
2020-05-04 01:33:24 +00:00
|
|
|
t.Fatal("Unexpected call error:", err)
|
|
|
|
}
|
2020-05-14 03:42:31 +00:00
|
|
|
|
|
|
|
if expects.Length() != 1 {
|
|
|
|
t.Fatal("Unexpected ArgumentParts length.")
|
|
|
|
}
|
|
|
|
if expects.After(1)+expects.After(2)+expects.After(-1) != "" {
|
|
|
|
t.Fatal("Unexpected ArgumentsParts after.")
|
|
|
|
}
|
|
|
|
if expects.String() != "arikawa" {
|
|
|
|
t.Fatal("Unexpected ArgumentsParts string.")
|
|
|
|
}
|
|
|
|
if expects.Arg(0) != "arikawa" {
|
|
|
|
t.Fatal("Unexpected ArgumentParts arg 0")
|
|
|
|
}
|
|
|
|
if expects.Arg(1) != "" {
|
|
|
|
t.Fatal("Unexpected ArgumentParts arg 1")
|
|
|
|
}
|
2020-05-04 01:33:24 +00:00
|
|
|
})
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
testMessage := func(content string) error {
|
|
|
|
// Mock a messageCreate event
|
|
|
|
m := &gateway.MessageCreateEvent{
|
2020-04-12 23:24:28 +00:00
|
|
|
Message: discord.Message{
|
|
|
|
Content: content,
|
|
|
|
},
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.callCmd(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("call command without args", func(t *testing.T) {
|
2020-04-06 20:31:27 +00:00
|
|
|
ctx.HasPrefix = NewPrefix("")
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-01-24 16:08:12 +00:00
|
|
|
if err := testMessage("noArgs"); err.Error() != "passed" {
|
2020-01-19 06:06:00 +00:00
|
|
|
t.Fatal("unexpected error:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Test error cases
|
|
|
|
|
|
|
|
t.Run("call unknown command", func(t *testing.T) {
|
2020-04-06 20:31:27 +00:00
|
|
|
ctx.HasPrefix = NewPrefix("joe pls ")
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
err := testMessage("joe pls no")
|
|
|
|
|
2020-05-16 21:14:49 +00:00
|
|
|
if err == nil || !strings.HasPrefix(err.Error(), "unknown command:") {
|
2020-01-19 06:06:00 +00:00
|
|
|
t.Fatal("unexpected error:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Test subcommands
|
|
|
|
|
|
|
|
t.Run("register subcommand", func(t *testing.T) {
|
2020-04-06 20:31:27 +00:00
|
|
|
ctx.HasPrefix = NewPrefix("run ")
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-04 06:48:07 +00:00
|
|
|
sub := &testc{}
|
2020-05-14 03:42:31 +00:00
|
|
|
ctx.MustRegisterSubcommand(sub)
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
if err := testMessage("run testc noop"); err != nil {
|
2020-02-05 04:29:45 +00:00
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
2020-05-04 06:48:07 +00:00
|
|
|
expects := RawArguments("hackadoll no. 3")
|
|
|
|
|
|
|
|
if err := expect(ctx, sub, expects, "run testc content hackadoll no. 3"); err != nil {
|
|
|
|
t.Fatal("Unexpected call error:", err)
|
|
|
|
}
|
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
if cmd := ctx.FindCommand("testc", "Noop"); cmd == nil {
|
2020-02-05 04:29:45 +00:00
|
|
|
t.Fatal("Failed to find subcommand Noop")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
})
|
2020-05-14 03:42:31 +00:00
|
|
|
|
|
|
|
t.Run("register subcommand custom", func(t *testing.T) {
|
2020-11-13 03:02:52 +00:00
|
|
|
ctx.MustRegisterSubcommand(&testc{}, "arikawa", "a")
|
2020-05-14 03:42:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("duplicate subcommand", func(t *testing.T) {
|
2020-11-13 03:02:52 +00:00
|
|
|
_, err := ctx.RegisterSubcommand(&testc{}, "arikawa")
|
2020-05-14 03:42:31 +00:00
|
|
|
if err := err.Error(); !strings.Contains(err, "duplicate") {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
2020-11-13 03:02:52 +00:00
|
|
|
|
|
|
|
_, err = ctx.RegisterSubcommand(&testc{}, "a")
|
|
|
|
if err := err.Error(); !strings.Contains(err, "duplicate") {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("help", func(t *testing.T) {
|
|
|
|
ctx.MustRegisterSubcommand(&testc{}, "helper")
|
|
|
|
|
|
|
|
h := ctx.Help()
|
|
|
|
if h == "" {
|
|
|
|
t.Fatal("Empty help?")
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(h, "hidden") {
|
|
|
|
t.Fatal("Hidden command shown in help.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(h, "arikawa/bot test") {
|
|
|
|
t.Fatal("Name not found.")
|
|
|
|
}
|
|
|
|
if !strings.Contains(h, "Just a test.") {
|
|
|
|
t.Fatal("Description not found.")
|
|
|
|
}
|
|
|
|
if !strings.Contains(h, "**a**") {
|
|
|
|
t.Fatal("arikawa alias `a' not found.")
|
|
|
|
}
|
2020-05-14 03:42:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("start", func(t *testing.T) {
|
|
|
|
cancel := ctx.Start()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ctx.HasPrefix = NewPrefix("!")
|
|
|
|
given.Return = make(chan interface{})
|
|
|
|
|
|
|
|
ctx.Handler.Call(&gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
Content: "!content hime arikawa best trap",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if c := (<-given.Return).(RawArguments); c != "hime arikawa best trap" {
|
|
|
|
t.Fatal("Unexpected content:", c)
|
|
|
|
}
|
|
|
|
})
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 06:48:07 +00:00
|
|
|
func expect(ctx *Context, given *testc, expects interface{}, content string) (call error) {
|
2020-05-14 03:42:31 +00:00
|
|
|
var v interface{}
|
|
|
|
if call = sendMsg(ctx, given, &v, content); call != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(v, expects) {
|
|
|
|
return fmt.Errorf("returned argument is invalid: %v", v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendMsg(ctx *Context, given *testc, into interface{}, content string) (call error) {
|
2020-05-04 06:48:07 +00:00
|
|
|
// Return channel for testing
|
|
|
|
ret := make(chan interface{})
|
|
|
|
given.Return = ret
|
|
|
|
|
|
|
|
// Mock a messageCreate event
|
|
|
|
m := &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
Content: content,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var callCh = make(chan error)
|
|
|
|
go func() {
|
2020-05-14 03:42:31 +00:00
|
|
|
callCh <- ctx.Call(m)
|
2020-05-04 06:48:07 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case arg := <-ret:
|
|
|
|
call = <-callCh
|
2020-05-14 03:42:31 +00:00
|
|
|
reflect.ValueOf(into).Elem().Set(reflect.ValueOf(arg))
|
2020-05-04 06:48:07 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case call = <-callCh:
|
|
|
|
return fmt.Errorf("expected return before error: %w", call)
|
|
|
|
|
|
|
|
case <-time.After(time.Second):
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.New("timed out while waiting")
|
2020-05-04 06:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
func BenchmarkConstructor(b *testing.B) {
|
2020-07-29 23:29:01 +00:00
|
|
|
var s = &state.State{
|
2020-11-30 01:19:59 +00:00
|
|
|
Cabinet: store.NoopCabinet,
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-07-29 23:29:01 +00:00
|
|
|
_, _ = New(s, &testc{})
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCall(b *testing.B) {
|
2020-05-04 01:33:24 +00:00
|
|
|
var given = &testc{}
|
2020-07-29 23:29:01 +00:00
|
|
|
var s = &state.State{
|
2020-11-30 01:19:59 +00:00
|
|
|
Cabinet: store.NoopCabinet,
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
sub, _ := NewSubcommand(given)
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
var ctx = &Context{
|
2020-07-29 23:29:01 +00:00
|
|
|
Subcommand: sub,
|
|
|
|
State: s,
|
2020-04-06 20:31:27 +00:00
|
|
|
HasPrefix: NewPrefix("~"),
|
2021-06-10 23:48:32 +00:00
|
|
|
ParseArgs: DefaultArgsParser,
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m := &gateway.MessageCreateEvent{
|
2020-04-12 23:24:28 +00:00
|
|
|
Message: discord.Message{
|
|
|
|
Content: "~noop",
|
|
|
|
},
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ctx.callCmd(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHelp(b *testing.B) {
|
2020-05-04 01:33:24 +00:00
|
|
|
var given = &testc{}
|
2020-07-29 23:29:01 +00:00
|
|
|
var s = &state.State{
|
2020-11-30 01:19:59 +00:00
|
|
|
Cabinet: store.NoopCabinet,
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
sub, _ := NewSubcommand(given)
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
var ctx = &Context{
|
2020-07-29 23:29:01 +00:00
|
|
|
Subcommand: sub,
|
|
|
|
State: s,
|
2020-04-06 20:31:27 +00:00
|
|
|
HasPrefix: NewPrefix("~"),
|
2021-06-10 23:48:32 +00:00
|
|
|
ParseArgs: DefaultArgsParser,
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = ctx.Help()
|
|
|
|
}
|
|
|
|
}
|