github.com/diamondburned/arikawa@v1.3.14/bot/ctx_plumb_test.go (about) 1 package bot 2 3 import ( 4 "testing" 5 6 "github.com/diamondburned/arikawa/discord" 7 "github.com/diamondburned/arikawa/gateway" 8 "github.com/diamondburned/arikawa/state" 9 ) 10 11 type hasPlumb struct { 12 Ctx *Context 13 14 Plumbed string 15 NotPlumbed bool 16 } 17 18 func (h *hasPlumb) Setup(sub *Subcommand) { 19 sub.SetPlumb("Plumber") 20 } 21 22 func (h *hasPlumb) Normal(_ *gateway.MessageCreateEvent) error { 23 h.NotPlumbed = true 24 return nil 25 } 26 27 func (h *hasPlumb) Plumber(_ *gateway.MessageCreateEvent, c RawArguments) error { 28 h.Plumbed = string(c) 29 return nil 30 } 31 32 func TestSubcommandPlumb(t *testing.T) { 33 var s = &state.State{ 34 Store: state.NewDefaultStore(nil), 35 } 36 37 c, err := New(s, &testc{}) 38 if err != nil { 39 t.Fatal("Failed to create new context:", err) 40 } 41 c.HasPrefix = NewPrefix("") 42 43 p := &hasPlumb{} 44 45 _, err = c.RegisterSubcommand(p) 46 if err != nil { 47 t.Fatal("Failed to register hasPlumb:", err) 48 } 49 50 // Try call exactly what's in the Plumb example: 51 m := &gateway.MessageCreateEvent{ 52 Message: discord.Message{ 53 Content: "hasPlumb", 54 }, 55 } 56 57 if err := c.callCmd(m); err != nil { 58 t.Fatal("Failed to call message:", err) 59 } 60 61 if p.NotPlumbed { 62 t.Fatal("Normal method called for hasPlumb") 63 } 64 } 65 66 type onlyPlumb struct { 67 Ctx *Context 68 Plumbed string 69 } 70 71 func (h *onlyPlumb) Setup(sub *Subcommand) { 72 sub.SetPlumb("Plumber") 73 } 74 75 func (h *onlyPlumb) Plumber(_ *gateway.MessageCreateEvent, c RawArguments) error { 76 h.Plumbed = string(c) 77 return nil 78 } 79 80 func TestSubcommandOnlyPlumb(t *testing.T) { 81 var s = &state.State{ 82 Store: state.NewDefaultStore(nil), 83 } 84 85 c, err := New(s, &testc{}) 86 if err != nil { 87 t.Fatal("Failed to create new context:", err) 88 } 89 c.HasPrefix = NewPrefix("") 90 91 p := &onlyPlumb{} 92 93 _, err = c.RegisterSubcommand(p) 94 if err != nil { 95 t.Fatal("Failed to register hasPlumb:", err) 96 } 97 98 // Try call exactly what's in the Plumb example: 99 m := &gateway.MessageCreateEvent{ 100 Message: discord.Message{ 101 Content: "onlyPlumb test command", 102 }, 103 } 104 105 if err := c.callCmd(m); err != nil { 106 t.Fatal("Failed to call message:", err) 107 } 108 109 if p.Plumbed != "test command" { 110 t.Fatal("Unexpected custom argument for plumbed:", p.Plumbed) 111 } 112 }