From c75a52e68c5d3905cdb618faf26ec50c2f8eaded Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 30 Mar 2016 23:09:35 -0400 Subject: [PATCH] Update leftpad to use JAVASCRIPT and test --- plugins/leftpad/leftpad.go | 46 +++++-------------- plugins/leftpad/leftpad_test.go | 80 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 36 deletions(-) create mode 100644 plugins/leftpad/leftpad_test.go diff --git a/plugins/leftpad/leftpad.go b/plugins/leftpad/leftpad.go index e1550be..86bf0ac 100644 --- a/plugins/leftpad/leftpad.go +++ b/plugins/leftpad/leftpad.go @@ -4,13 +4,10 @@ package leftpad import ( - "encoding/json" - "io/ioutil" - "log" - "net/http" - "net/url" + "strconv" "strings" + "github.com/jamescun/leftpad" "github.com/velour/catbase/bot" ) @@ -38,36 +35,16 @@ func (p *LeftpadPlugin) Message(message bot.Message) bool { parts := strings.Split(message.Body, " ") if len(parts) > 3 && parts[0] == "leftpad" { padchar := parts[1] - length := parts[2] + length, err := strconv.Atoi(parts[2]) + if err != nil { + p.bot.SendMessage(message.Channel, "Invalid padding number") + return true + } text := strings.Join(parts[3:], " ") - url, _ := url.Parse("https://api.left-pad.io") - q := url.Query() - q.Set("str", text) - q.Set("len", length) - q.Set("ch", padchar) - url.RawQuery = q.Encode() - log.Printf("Requesting leftpad url: %s", url) - resp, err := http.Get(url.String()) - if err != nil { - p.bot.SendMessage(message.Channel, err.Error()) - return true - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - p.bot.SendMessage(message.Channel, "I can't leftpad right now :(") - log.Printf("Error decoding leftpad: %s", err) - return true - } - r := leftpadResp{} - err = json.Unmarshal(body, &r) - if err != nil { - p.bot.SendMessage(message.Channel, "I can't leftpad right now :(") - log.Printf("Error decoding leftpad: %s", err) - return true - } - p.bot.SendMessage(message.Channel, r.Str) + res := leftpad.LeftPad(text, length, padchar) + + p.bot.SendMessage(message.Channel, res) return true } @@ -82,9 +59,6 @@ func (p *LeftpadPlugin) BotMessage(message bot.Message) bool { return false } -func (p *LeftpadPlugin) LoadData() { -} - func (p *LeftpadPlugin) Help(e string, m []string) { } diff --git a/plugins/leftpad/leftpad_test.go b/plugins/leftpad/leftpad_test.go new file mode 100644 index 0000000..a505f18 --- /dev/null +++ b/plugins/leftpad/leftpad_test.go @@ -0,0 +1,80 @@ +// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors. + +package leftpad + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/velour/catbase/bot" + "github.com/velour/catbase/plugins/counter" +) + +func makeMessage(payload string) bot.Message { + isCmd := strings.HasPrefix(payload, "!") + if isCmd { + payload = payload[1:] + } + return bot.Message{ + User: &bot.User{Name: "tester"}, + Channel: "test", + Body: payload, + Command: isCmd, + } +} + +func makePlugin(t *testing.T) (*LeftpadPlugin, *bot.MockBot) { + mb := bot.NewMockBot() + counter.New(mb) + p := New(mb) + assert.NotNil(t, p) + return p, mb +} + +func TestLeftpad(t *testing.T) { + p, mb := makePlugin(t) + p.Message(makeMessage("!leftpad test 8 test")) + assert.Contains(t, mb.Messages[0], "testtest") + assert.Len(t, mb.Messages, 1) +} + +func TestBadNumber(t *testing.T) { + p, mb := makePlugin(t) + p.Message(makeMessage("!leftpad test fuck test")) + assert.Contains(t, mb.Messages[0], "Invalid") + assert.Len(t, mb.Messages, 1) +} + +func TestNotCommand(t *testing.T) { + p, mb := makePlugin(t) + p.Message(makeMessage("leftpad test fuck test")) + assert.Len(t, mb.Messages, 0) +} + +func TestNotPadding(t *testing.T) { + p, mb := makePlugin(t) + p.Message(makeMessage("!lololol")) + assert.Len(t, mb.Messages, 0) +} + +func TestHelp(t *testing.T) { + p, mb := makePlugin(t) + p.Help("channel", []string{}) + assert.Len(t, mb.Messages, 0) +} + +func TestBotMessage(t *testing.T) { + p, _ := makePlugin(t) + assert.False(t, p.BotMessage(makeMessage("test"))) +} + +func TestEvent(t *testing.T) { + p, _ := makePlugin(t) + assert.False(t, p.Event("dummy", makeMessage("test"))) +} + +func TestRegisterWeb(t *testing.T) { + p, _ := makePlugin(t) + assert.Nil(t, p.RegisterWeb()) +}