From 90bf9d74e325e1a43cef5c8ba6b64e1e05d3d483 Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Sun, 3 May 2020 22:57:44 -0700 Subject: [PATCH] Bot: Fixed bug where RawArguments still have the command --- bot/ctx_call.go | 2 ++ bot/ctx_plumb_test.go | 2 +- bot/ctx_test.go | 34 ++++++++++++++++++++-------------- bot/subcommand_test.go | 4 ++-- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/bot/ctx_call.go b/bot/ctx_call.go index 0479bb2..12f7d15 100644 --- a/bot/ctx_call.go +++ b/bot/ctx_call.go @@ -380,6 +380,8 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent) error { // Seek to the string. if i := strings.Index(content, seekTo); i > -1 { + // Seek past the substring. + i += len(seekTo) content = strings.TrimSpace(content[i:]) } diff --git a/bot/ctx_plumb_test.go b/bot/ctx_plumb_test.go index 96f3c5d..a7ebb9c 100644 --- a/bot/ctx_plumb_test.go +++ b/bot/ctx_plumb_test.go @@ -62,7 +62,7 @@ func TestSubcommandPlumb(t *testing.T) { t.Fatal("Normal method called for hasPlumb") } - if p.Plumbed != "hasPlumb test command" { + if p.Plumbed != "test command" { t.Fatal("Unexpected custom argument for plumbed:", p.Plumbed) } } diff --git a/bot/ctx_test.go b/bot/ctx_test.go index 5e690e5..8753d07 100644 --- a/bot/ctx_test.go +++ b/bot/ctx_test.go @@ -19,14 +19,12 @@ type testc struct { Typed bool } -func (t *testc) MーBumpCounter(interface{}) error { +func (t *testc) MーBumpCounter(interface{}) { t.Counter++ - return nil } -func (t *testc) GetCounter(_ *gateway.MessageCreateEvent) error { +func (t *testc) GetCounter(_ *gateway.MessageCreateEvent) { t.Return <- strconv.FormatUint(t.Counter, 10) - return nil } func (t *testc) Send(_ *gateway.MessageCreateEvent, args ...string) error { @@ -34,32 +32,31 @@ func (t *testc) Send(_ *gateway.MessageCreateEvent, args ...string) error { return errors.New("oh no") } -func (t *testc) Custom(_ *gateway.MessageCreateEvent, c *customManualParsed) error { +func (t *testc) Custom(_ *gateway.MessageCreateEvent, c *customManualParsed) { t.Return <- c.args - return nil } -func (t *testc) Variadic(_ *gateway.MessageCreateEvent, c ...*customParsed) error { +func (t *testc) Variadic(_ *gateway.MessageCreateEvent, c ...*customParsed) { t.Return <- c[len(c)-1] - return nil } -func (t *testc) TrailCustom(_ *gateway.MessageCreateEvent, s string, c *customManualParsed) error { +func (t *testc) TrailCustom(_ *gateway.MessageCreateEvent, s string, c *customManualParsed) { t.Return <- c.args - return nil +} + +func (t *testc) Content(_ *gateway.MessageCreateEvent, c RawArguments) { + t.Return <- c } func (t *testc) NoArgs(_ *gateway.MessageCreateEvent) error { return errors.New("passed") } -func (t *testc) Noop(_ *gateway.MessageCreateEvent) error { - return nil +func (t *testc) Noop(_ *gateway.MessageCreateEvent) { } -func (t *testc) OnTyping(_ *gateway.TypingStartEvent) error { +func (t *testc) OnTyping(_ *gateway.TypingStartEvent) { t.Typed = true - return nil } func TestNewContext(t *testing.T) { @@ -194,6 +191,15 @@ func TestContext(t *testing.T) { } }) + t.Run("call command rawarguments", func(t *testing.T) { + ctx.HasPrefix = NewPrefix("!") + expects := RawArguments("just things") + + if err := testReturn(expects, "!content just things"); err != nil { + t.Fatal("Unexpected call error:", err) + } + }) + t.Run("call command custom manual parser", func(t *testing.T) { ctx.HasPrefix = NewPrefix("!") expects := []string{"arg1", ":)"} diff --git a/bot/subcommand_test.go b/bot/subcommand_test.go index 6f32ea2..22eda07 100644 --- a/bot/subcommand_test.go +++ b/bot/subcommand_test.go @@ -29,7 +29,7 @@ func TestSubcommand(t *testing.T) { } // !!! CHANGE ME - if len(sub.Commands) != 7 { + if len(sub.Commands) != 8 { t.Fatal("invalid ctx.commands len", len(sub.Commands)) } @@ -59,7 +59,7 @@ func TestSubcommand(t *testing.T) { t.Fatal("expected 0 arguments, got non-zero") } - case "noop", "getCounter", "variadic", "trailCustom": + case "noop", "getCounter", "variadic", "trailCustom", "content": // Found, but whatever }