2020-01-26 05:43:42 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
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"
|
2020-01-26 05:43:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type hasPlumb struct {
|
|
|
|
Ctx *Context
|
|
|
|
|
2020-11-30 22:26:53 +00:00
|
|
|
Plumbed bool
|
|
|
|
PlumbedArgs string
|
|
|
|
|
|
|
|
NotPlumbed bool
|
|
|
|
NotPlumbedArgs string
|
2020-01-26 05:43:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
func (h *hasPlumb) Setup(sub *Subcommand) {
|
2020-11-30 22:26:53 +00:00
|
|
|
sub.SetPlumb(h.Plumber)
|
2020-05-10 08:45:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 22:26:53 +00:00
|
|
|
func (h *hasPlumb) Plumber(_ *gateway.MessageCreateEvent, c RawArguments) error {
|
|
|
|
h.NotPlumbed = false
|
|
|
|
h.Plumbed = true
|
|
|
|
h.PlumbedArgs = string(c)
|
2020-01-26 05:43:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 22:26:53 +00:00
|
|
|
func (h *hasPlumb) Normal(_ *gateway.MessageCreateEvent, c RawArguments) error {
|
|
|
|
h.Plumbed = false
|
|
|
|
h.NotPlumbed = true
|
|
|
|
h.NotPlumbedArgs = string(c)
|
2020-01-26 05:43:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubcommandPlumb(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-26 05:43:42 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-11-30 22:26:53 +00:00
|
|
|
sendFn := func(content string) {
|
|
|
|
m := &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{Content: content},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.callCmd(m); err != nil {
|
|
|
|
t.Fatal("Failed to call message:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 05:43:42 +00:00
|
|
|
// Try call exactly what's in the Plumb example:
|
2020-11-30 22:26:53 +00:00
|
|
|
sendFn("hasPlumb")
|
|
|
|
|
|
|
|
if p.NotPlumbed || !p.Plumbed {
|
|
|
|
t.Error("Normal method called for hasPlumb")
|
2020-01-26 05:43:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 22:26:53 +00:00
|
|
|
sendFn("hasPlumb arg1")
|
|
|
|
|
|
|
|
if p.NotPlumbed || !p.Plumbed {
|
|
|
|
t.Error("Normal method called for hasPlumb with arguments")
|
|
|
|
}
|
|
|
|
if p.PlumbedArgs != "arg1" {
|
|
|
|
t.Errorf("Incorrect plumbed argument %q", p.PlumbedArgs)
|
2020-01-26 05:43:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 23:00:21 +00:00
|
|
|
sendFn("hasPlumb plumber arg1")
|
|
|
|
|
|
|
|
if p.NotPlumbed || !p.Plumbed {
|
|
|
|
t.Error("Normal method called for plumber command with arguments")
|
|
|
|
}
|
|
|
|
if p.PlumbedArgs != "arg1" {
|
|
|
|
t.Errorf("Incorrect normal plumbed argument %q", p.PlumbedArgs)
|
|
|
|
}
|
|
|
|
|
2020-11-30 22:26:53 +00:00
|
|
|
sendFn("hasPlumb normal")
|
|
|
|
|
|
|
|
if p.Plumbed || !p.NotPlumbed {
|
|
|
|
t.Error("Plumbed method called for normal command")
|
|
|
|
}
|
|
|
|
|
|
|
|
sendFn("hasPlumb normal args")
|
|
|
|
|
|
|
|
if p.Plumbed || !p.NotPlumbed {
|
|
|
|
t.Error("Plumbed method called for normal command with arguments")
|
|
|
|
}
|
|
|
|
if p.NotPlumbedArgs != "args" {
|
|
|
|
t.Errorf("Incorrect normal argument %q", p.NotPlumbedArgs)
|
2020-01-26 05:43:42 +00:00
|
|
|
}
|
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-11-30 01:19:59 +00:00
|
|
|
Cabinet: store.NoopCabinet,
|
2020-05-13 04:37:06 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|