-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchat_test.go
More file actions
96 lines (79 loc) · 2.96 KB
/
chat_test.go
File metadata and controls
96 lines (79 loc) · 2.96 KB
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
90
91
92
93
94
95
96
package bot_test
import (
"testing"
"github.com/botopolis/bot"
"github.com/botopolis/bot/mock"
"github.com/stretchr/testify/assert"
)
func TestResponderSend(t *testing.T) {
envelope := bot.Message{Room: "general"}
message := bot.Message{Text: "hi", Room: envelope.Room, Envelope: envelope}
chat := mock.NewChat()
robot := bot.Robot{Chat: chat}
res := bot.Responder{Robot: &robot, Message: message}
chat.SendFunc = func(m bot.Message) error {
assert.Equal(t, envelope, m.Envelope, "should receive the previous message as an envelope")
assert.Equal(t, "hi", m.Text, "should receive the original parameters")
assert.Equal(t, "general", m.Room, "should default the room when empty")
return nil
}
res.Send(bot.Message{Text: "hi"})
}
func TestResponderReply(t *testing.T) {
envelope := bot.Message{Room: "general"}
message := bot.Message{Text: "hi", Envelope: envelope}
chat := mock.NewChat()
robot := bot.Robot{Chat: chat}
res := bot.Responder{Robot: &robot, Message: message}
chat.ReplyFunc = func(m bot.Message) error {
assert.Equal(t, envelope, m.Envelope, "should receive the previous message as an envelope")
assert.Equal(t, "hi", m.Text, "should receive the original parameters")
return nil
}
res.Reply("hi")
}
func TestResponderTopic(t *testing.T) {
envelope := bot.Message{Room: "general"}
message := bot.Message{Text: "hi", Envelope: envelope}
chat := mock.NewChat()
robot := bot.Robot{Chat: chat}
res := bot.Responder{Robot: &robot, Message: message}
chat.TopicFunc = func(m bot.Message) error {
assert.Equal(t, envelope, m.Envelope, "should receive the previous message as an envelope")
assert.Equal(t, "stuff", m.Topic, "should receive the original parameters")
return nil
}
res.Topic("stuff")
}
func TestUser(t *testing.T) {
name := "bob"
not := "notbob"
res := bot.Responder{Message: bot.Message{User: name}}
assert.False(t, bot.User(not)(&res), "is false for differing user")
assert.True(t, bot.User(name)(&res), "is true for matching user")
}
func TestRoom(t *testing.T) {
name := "general"
not := "notgeneral"
res := bot.Responder{Message: bot.Message{Room: name}}
assert.False(t, bot.Room(not)(&res), "is false for differing room")
assert.True(t, bot.Room(name)(&res), "is true for matching room")
}
func TestContains(t *testing.T) {
text := "hi there bob"
yes := "bob"
not := "notbob"
res := bot.Responder{Message: bot.Message{Text: text}}
assert.False(t, bot.Contains(not)(&res), "is false for excluded text")
assert.True(t, bot.Contains(yes)(&res), "is true for contained text")
}
func TestRegexp(t *testing.T) {
text := "hi there bob"
yes := "b([\\w])b"
not := "asdf(g)"
res := bot.Responder{Message: bot.Message{Text: text}}
assert.False(t, bot.Regexp(not)(&res), "is false for unmatched text")
assert.Empty(t, res.Match, "doesn't create capture groups when unmatched")
assert.True(t, bot.Regexp(yes)(&res), "is true for matched text")
assert.Equal(t, []string{"bob", "o"}, res.Match, "creates capture groups when matched")
}