Skip to content

Commit

Permalink
Make 69th Nice; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rcy committed Sep 22, 2023
1 parent 616fae2 commit b2a0159
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
8 changes: 8 additions & 0 deletions bot/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ type Handler struct {
action HandlerFunction
}

func NewHandler(pattern string, regexp regexp.Regexp, action HandlerFunction) *Handler {
return &Handler{pattern, regexp, action}
}

func (h Handler) Regexp() *regexp.Regexp {
return &h.regexp
}

func (h Handler) String() string {
strs := strings.Split(runtime.FuncForPC(reflect.ValueOf(h.action).Pointer()).Name(), ".")
return fmt.Sprintf("%-32s %s", h.pattern, strs[len(strs)-1])
Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func main() {
log.Fatal(err)
}

addHandlers(b)

b.Loop()
}

func addHandlers(b *bot.Bot) {
b.Repeat(10*time.Second, handlers.DoRemind)

b.IdleRepeatAfterReset(8*time.Hour, handlers.POM)
Expand All @@ -38,13 +44,11 @@ func main() {
b.Handle(`^!feedme`, handlers.FeedMe)
b.Handle(`(https?://\S+)`, handlers.Link)
b.Handle(`^!day`, handlers.NationalDay)
b.Handle(`\b69\b`, handlers.Nice)
b.Handle(`\b69[^0-9]*\b`, handlers.Nice)
b.Handle(`^!odds`, mlb.PlayoffOdds)
b.Handle(`^!pom`, handlers.POM)
b.Handle(`^("[^"]+)$`, handlers.Quote)
b.Handle(`^!remindme ([^\s]+) (.+)$`, handlers.RemindMe)
b.Handle(`^\?(\S+)`, handlers.Seen)
b.Handle(`world.?cup`, handlers.Worldcup)

b.Loop()
}
62 changes: 62 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"goirc/bot"
"strings"
"testing"
)

func TestMain(t *testing.T) {
var b bot.Bot

addHandlers(&b)

for _, tc := range []struct {
message string
want []string
}{
{
"69",
[]string{"Nice"},
},
{
"691",
nil,
},
{
"169",
nil,
},
{
"69.",
[]string{"Nice"},
},
{
"69th",
[]string{"Nice"},
},
{
"x69",
nil,
},
} {
t.Run(tc.message, func(t *testing.T) {
var got []string
for _, handler := range b.Handlers {
matches := handler.Regexp().FindStringSubmatch(tc.message)
if len(matches) > 0 {
got = append(got, strings.Fields(handler.String())[1])
}
}
if len(tc.want) != len(got) {
t.Fatalf("wanted %v, got %v", tc.want, got)

}
for i := range tc.want {
if tc.want[i] != got[i] {
t.Fatalf("wanted %v, got %v", tc.want, got)
}
}
})
}
}

0 comments on commit b2a0159

Please sign in to comment.