-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathconversation_test.go
89 lines (71 loc) · 1.53 KB
/
conversation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package slick
import (
"regexp"
"testing"
"time"
"github.com/nlopes/slack"
)
func TestListenerCheckParams(t *testing.T) {
c := Listener{
ListenUntil: time.Now(),
ListenDuration: 120 * time.Second,
}
err := c.checkParams()
if err == nil {
t.Error("checkParams shouldn't be nil")
}
}
func TestDefaultFilter(t *testing.T) {
c := &Listener{}
u := &slack.User{ID: "a_user"}
m := &Message{Msg: &slack.Msg{Text: "hello mama"}, FromUser: u}
if c.filterMessage(m) != true {
t.Error("filterMessage Failed")
}
type El struct {
c *Listener
r bool
}
tests := []El{
El{&Listener{}, true},
El{&Listener{
Contains: "moo",
}, false},
El{&Listener{
Contains: "MAMA",
}, true},
El{&Listener{
FromUser: u,
}, true},
El{&Listener{
Matches: regexp.MustCompile(`hello`),
}, true},
El{&Listener{
Matches: regexp.MustCompile(`other-message`),
}, false},
El{&Listener{
FromUser: &slack.User{ID: "another_user"},
}, false},
}
for i, el := range tests {
if el.c.filterMessage(m) != el.r {
t.Error("filterMessage Failed, index ", i)
}
}
}
func TestMatchesMessage(t *testing.T) {
c := &Listener{Matches: regexp.MustCompile(`(this) (is) (good)`)}
m := &Message{Msg: &slack.Msg{Text: "yeah this is good and all"}}
if c.filterMessage(m) != true {
t.Error("filterMessage Failed")
}
if len(m.Match) != 4 {
t.Error("didn't find 4 matches")
}
if m.Match[0] != "this is good" {
t.Error("didn't find 'this is good'")
}
if m.Match[1] != "this" {
t.Error("didn't find 'this'")
}
}