From 49d01cb205c32bf05efa9bf437bff55f6a830846 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Mon, 9 Mar 2020 07:43:48 -0600 Subject: [PATCH] fix comments in beat, add tests. --- plugins/beat.go | 10 +++++----- plugins/beat_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 plugins/beat_test.go diff --git a/plugins/beat.go b/plugins/beat.go index 18a2f2f..a9e8005 100644 --- a/plugins/beat.go +++ b/plugins/beat.go @@ -17,12 +17,12 @@ func (h *Beat) Descr() string { return "Print the current [beat time](https://en.wikipedia.org/wiki/Swatch_Internet_Time)." } -// Re is the regex for matching hi messages. +// Re is the regex for matching beat messages. func (h *Beat) Re() string { - return `(?i)^\.beat$|^what time is it\?$|^beat( )?time:?\??$` + return `(?i)^\.beat$|^what time is it[\?!]+$|^beat( )?time:?\??$` } -// Match determines if we are highfiving +// Match determines if we are asking for a beat func (h *Beat) Match(user, msg string) bool { re := regexp.MustCompile(h.Re()) return re.MatchString(msg) @@ -31,7 +31,7 @@ func (h *Beat) Match(user, msg string) bool { // SetStore we don't need a store here func (h *Beat) SetStore(s PluginStore) {} -// RespondText to hi events +// RespondText to beat request events func (h *Beat) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { n := time.Now() utc1 := n.Unix() + 3600 @@ -40,7 +40,7 @@ func (h *Beat) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post st SendText(c, ev.RoomID, fmt.Sprintf("@%03d", int32(bt))) } -// Name hi +// Name beat func (h *Beat) Name() string { return "Beat" } diff --git a/plugins/beat_test.go b/plugins/beat_test.go new file mode 100644 index 0000000..96a4674 --- /dev/null +++ b/plugins/beat_test.go @@ -0,0 +1,22 @@ +package plugins + +import ( + "testing" +) + +func TestMatch(t *testing.T) { + testStrings := make(map[string]bool) + testStrings["what time is it??!!?!"] = true + testStrings["what time is it"] = false + testStrings["man: 2 pledge"] = false + testStrings[".beat"] = true + testStrings["beattime?"] = true + testStrings["beat time?"] = true + + b := &Beat{} + for msg, should := range testStrings { + if b.Match("", msg) != should { + t.Errorf("Beat expected to match %q (%t); but doesn't\n", msg, should) + } + } +}