github.com/diamondburned/arikawa/v2@v2.1.0/bot/ctx_plumb_test.go (about) 1 package bot 2 3 import ( 4 "testing" 5 6 "github.com/diamondburned/arikawa/v2/discord" 7 "github.com/diamondburned/arikawa/v2/gateway" 8 "github.com/diamondburned/arikawa/v2/state" 9 "github.com/diamondburned/arikawa/v2/state/store" 10 ) 11 12 type hasPlumb struct { 13 Ctx *Context 14 15 Plumbed bool 16 PlumbedArgs string 17 18 NotPlumbed bool 19 NotPlumbedArgs string 20 } 21 22 func (h *hasPlumb) Setup(sub *Subcommand) { 23 sub.SetPlumb(h.Plumber) 24 } 25 26 func (h *hasPlumb) Plumber(_ *gateway.MessageCreateEvent, c RawArguments) error { 27 h.NotPlumbed = false 28 h.Plumbed = true 29 h.PlumbedArgs = string(c) 30 return nil 31 } 32 33 func (h *hasPlumb) Normal(_ *gateway.MessageCreateEvent, c RawArguments) error { 34 h.Plumbed = false 35 h.NotPlumbed = true 36 h.NotPlumbedArgs = string(c) 37 return nil 38 } 39 40 func TestSubcommandPlumb(t *testing.T) { 41 var s = &state.State{ 42 Cabinet: store.NoopCabinet, 43 } 44 45 c, err := New(s, &testc{}) 46 if err != nil { 47 t.Fatal("Failed to create new context:", err) 48 } 49 c.HasPrefix = NewPrefix("") 50 51 p := &hasPlumb{} 52 53 _, err = c.RegisterSubcommand(p) 54 if err != nil { 55 t.Fatal("Failed to register hasPlumb:", err) 56 } 57 58 sendFn := func(content string) { 59 m := &gateway.MessageCreateEvent{ 60 Message: discord.Message{Content: content}, 61 } 62 63 if err := c.callCmd(m); err != nil { 64 t.Fatal("Failed to call message:", err) 65 } 66 } 67 68 // Try call exactly what's in the Plumb example: 69 sendFn("hasPlumb") 70 71 if p.NotPlumbed || !p.Plumbed { 72 t.Error("Normal method called for hasPlumb") 73 } 74 75 sendFn("hasPlumb arg1") 76 77 if p.NotPlumbed || !p.Plumbed { 78 t.Error("Normal method called for hasPlumb with arguments") 79 } 80 if p.PlumbedArgs != "arg1" { 81 t.Errorf("Incorrect plumbed argument %q", p.PlumbedArgs) 82 } 83 84 sendFn("hasPlumb plumber arg1") 85 86 if p.NotPlumbed || !p.Plumbed { 87 t.Error("Normal method called for plumber command with arguments") 88 } 89 if p.PlumbedArgs != "arg1" { 90 t.Errorf("Incorrect normal plumbed argument %q", p.PlumbedArgs) 91 } 92 93 sendFn("hasPlumb normal") 94 95 if p.Plumbed || !p.NotPlumbed { 96 t.Error("Plumbed method called for normal command") 97 } 98 99 sendFn("hasPlumb normal args") 100 101 if p.Plumbed || !p.NotPlumbed { 102 t.Error("Plumbed method called for normal command with arguments") 103 } 104 if p.NotPlumbedArgs != "args" { 105 t.Errorf("Incorrect normal argument %q", p.NotPlumbedArgs) 106 } 107 } 108 109 type onlyPlumb struct { 110 Ctx *Context 111 Plumbed string 112 } 113 114 func (h *onlyPlumb) Setup(sub *Subcommand) { 115 sub.SetPlumb("Plumber") 116 } 117 118 func (h *onlyPlumb) Plumber(_ *gateway.MessageCreateEvent, c RawArguments) error { 119 h.Plumbed = string(c) 120 return nil 121 } 122 123 func TestSubcommandOnlyPlumb(t *testing.T) { 124 var s = &state.State{ 125 Cabinet: store.NoopCabinet, 126 } 127 128 c, err := New(s, &testc{}) 129 if err != nil { 130 t.Fatal("Failed to create new context:", err) 131 } 132 c.HasPrefix = NewPrefix("") 133 134 p := &onlyPlumb{} 135 136 _, err = c.RegisterSubcommand(p) 137 if err != nil { 138 t.Fatal("Failed to register hasPlumb:", err) 139 } 140 141 // Try call exactly what's in the Plumb example: 142 m := &gateway.MessageCreateEvent{ 143 Message: discord.Message{ 144 Content: "onlyPlumb test command", 145 }, 146 } 147 148 if err := c.callCmd(m); err != nil { 149 t.Fatal("Failed to call message:", err) 150 } 151 152 if p.Plumbed != "test command" { 153 t.Fatal("Unexpected custom argument for plumbed:", p.Plumbed) 154 } 155 }