2020-01-26 05:43:42 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v2/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v2/state"
|
2020-01-26 05:43:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type hasPlumb struct {
|
|
|
|
Ctx *Context
|
|
|
|
|
|
|
|
Plumbed string
|
|
|
|
NotPlumbed bool
|
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
func (h *hasPlumb) Setup(sub *Subcommand) {
|
|
|
|
sub.SetPlumb("Plumber")
|
|
|
|
}
|
|
|
|
|
2020-01-26 05:43:42 +00:00
|
|
|
func (h *hasPlumb) Normal(_ *gateway.MessageCreateEvent) error {
|
|
|
|
h.NotPlumbed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
func (h *hasPlumb) Plumber(_ *gateway.MessageCreateEvent, c RawArguments) error {
|
2020-01-26 05:43:42 +00:00
|
|
|
h.Plumbed = string(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubcommandPlumb(t *testing.T) {
|
2020-07-29 23:29:01 +00:00
|
|
|
var s = &state.State{
|
2020-01-26 05:43:42 +00:00
|
|
|
Store: state.NewDefaultStore(nil),
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
c, err := New(s, &testc{})
|
2020-01-26 05:43:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create new context:", err)
|
|
|
|
}
|
2020-04-06 20:31:27 +00:00
|
|
|
c.HasPrefix = NewPrefix("")
|
2020-01-26 05:43:42 +00:00
|
|
|
|
|
|
|
p := &hasPlumb{}
|
|
|
|
|
|
|
|
_, err = c.RegisterSubcommand(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to register hasPlumb:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try call exactly what's in the Plumb example:
|
|
|
|
m := &gateway.MessageCreateEvent{
|
2020-04-12 23:24:28 +00:00
|
|
|
Message: discord.Message{
|
2020-05-13 04:37:06 +00:00
|
|
|
Content: "hasPlumb",
|
2020-04-12 23:24:28 +00:00
|
|
|
},
|
2020-01-26 05:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.callCmd(m); err != nil {
|
|
|
|
t.Fatal("Failed to call message:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.NotPlumbed {
|
|
|
|
t.Fatal("Normal method called for hasPlumb")
|
|
|
|
}
|
2020-05-13 04:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type onlyPlumb struct {
|
|
|
|
Ctx *Context
|
|
|
|
Plumbed string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *onlyPlumb) Setup(sub *Subcommand) {
|
|
|
|
sub.SetPlumb("Plumber")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *onlyPlumb) Plumber(_ *gateway.MessageCreateEvent, c RawArguments) error {
|
|
|
|
h.Plumbed = string(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubcommandOnlyPlumb(t *testing.T) {
|
2020-07-29 23:29:01 +00:00
|
|
|
var s = &state.State{
|
2020-05-13 04:37:06 +00:00
|
|
|
Store: state.NewDefaultStore(nil),
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
c, err := New(s, &testc{})
|
2020-05-13 04:37:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create new context:", err)
|
|
|
|
}
|
|
|
|
c.HasPrefix = NewPrefix("")
|
|
|
|
|
|
|
|
p := &onlyPlumb{}
|
|
|
|
|
|
|
|
_, err = c.RegisterSubcommand(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to register hasPlumb:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try call exactly what's in the Plumb example:
|
|
|
|
m := &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
Content: "onlyPlumb test command",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.callCmd(m); err != nil {
|
|
|
|
t.Fatal("Failed to call message:", err)
|
|
|
|
}
|
2020-01-26 05:43:42 +00:00
|
|
|
|
2020-05-04 05:57:44 +00:00
|
|
|
if p.Plumbed != "test command" {
|
2020-01-26 05:43:42 +00:00
|
|
|
t.Fatal("Unexpected custom argument for plumbed:", p.Plumbed)
|
|
|
|
}
|
|
|
|
}
|