-
Notifications
You must be signed in to change notification settings - Fork 0
/
lungfish_test.go
87 lines (73 loc) · 2.15 KB
/
lungfish_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
package lungfish
import "testing"
func TestCreateEvent_Success(t *testing.T) {
conn := NewConnection("dummytoken")
conn.userId = "someUserId"
data := map[string]interface{}{
"type": "message",
"user": "aaaaaa",
"text": "<@someUserId>: command_name arg1 arg2",
}
e, err := conn.parseEvent(data)
if err != nil {
t.Fatalf("expected nil error, got: %s", err)
}
if e.rawText != data["text"] {
t.Fatalf("expected: %s, got: %s", data["text"], e.rawText)
}
if e.Type != data["type"] {
t.Fatalf("expected: %s, got: %s", data["type"], e.Type)
}
if e.UserId != data["user"] {
t.Fatalf("expected: %s, got: %s", data["user"], e.UserId)
}
if !e.isMention {
t.Fatalf("expected: true, got: %t", e.isMention)
}
if e.trigger.keyword != "command_name" {
t.Fatalf("expected: %s, got: %s", "command_name", e.trigger.keyword)
}
if len(e.trigger.args) != 2 {
t.Fatalf("expected slice with: %d elements, got: %d", 2, len(e.trigger.args))
}
}
func TestCreateEvent_Unsupported(t *testing.T) {
conn := NewConnection("dummytoken")
data := map[string]interface{}{
"type": "something_unsupported",
}
_, err := conn.parseEvent(data)
if err != ErrUnsupportedEventType {
t.Fatalf("expected %s error, got: %s", ErrUnsupportedEventType, err)
}
}
func TestCreateEvent_NotEnoughArgs(t *testing.T) {
conn := NewConnection("dummytoken")
data := map[string]interface{}{
"type": "message",
"text": "@botname",
}
e, err := conn.parseEvent(data)
if err != nil {
t.Fatalf("expected nil error, got: %s", err)
}
// trigger should at least not be a nil pointer
if len(e.trigger.args) != 0 {
t.Fatalf("expected slice with: %d elements, got: %d", 0, len(e.trigger.args))
}
}
func TestRegisterChannel(t *testing.T) {
conn := NewConnection("dummytoken")
conn.RegisterChannel("#general")
if conn.slackChannel != "#general" {
t.Fatalf("expected: #general, got: %s", conn.slackChannel)
}
}
func TestRegisterReaction(t *testing.T) {
trigger := "hello"
conn := NewConnection("dummytoken")
conn.RegisterReaction(trigger, func(e *Event) { return })
if _, ok := conn.reactions["hello"]; !ok {
t.Fatalf("expected key named %s to be set in c.reactions", trigger)
}
}