mirror of https://github.com/velour/catbase.git
Update leftpad to use JAVASCRIPT and test
This commit is contained in:
parent
1254754e22
commit
c75a52e68c
|
@ -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) {
|
||||
}
|
||||
|
||||
|
|
|
@ -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())
|
||||
}
|
Loading…
Reference in New Issue