fix comments in beat, add tests.
This commit is contained in:
parent
27a0e4c780
commit
49d01cb205
@ -17,12 +17,12 @@ func (h *Beat) Descr() string {
|
|||||||
return "Print the current [beat time](https://en.wikipedia.org/wiki/Swatch_Internet_Time)."
|
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 {
|
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 {
|
func (h *Beat) Match(user, msg string) bool {
|
||||||
re := regexp.MustCompile(h.Re())
|
re := regexp.MustCompile(h.Re())
|
||||||
return re.MatchString(msg)
|
return re.MatchString(msg)
|
||||||
@ -31,7 +31,7 @@ func (h *Beat) Match(user, msg string) bool {
|
|||||||
// SetStore we don't need a store here
|
// SetStore we don't need a store here
|
||||||
func (h *Beat) SetStore(s PluginStore) {}
|
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) {
|
func (h *Beat) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
||||||
n := time.Now()
|
n := time.Now()
|
||||||
utc1 := n.Unix() + 3600
|
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)))
|
SendText(c, ev.RoomID, fmt.Sprintf("@%03d", int32(bt)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name hi
|
// Name beat
|
||||||
func (h *Beat) Name() string {
|
func (h *Beat) Name() string {
|
||||||
return "Beat"
|
return "Beat"
|
||||||
}
|
}
|
||||||
|
22
plugins/beat_test.go
Normal file
22
plugins/beat_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user