diff --git a/bot/irc.go b/bot/irc.go index 47b84bf..8d854a7 100644 --- a/bot/irc.go +++ b/bot/irc.go @@ -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]) diff --git a/main.go b/main.go index bcd20f3..68985b1 100644 --- a/main.go +++ b/main.go @@ -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) @@ -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() } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..6198b71 --- /dev/null +++ b/main_test.go @@ -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) + } + } + }) + } +}