-
Notifications
You must be signed in to change notification settings - Fork 6
/
slack_message_test.go
154 lines (141 loc) · 3.28 KB
/
slack_message_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"testing"
"time"
)
type recordableListner struct {
records []record
}
type record struct {
Type recordType
Text string
Target string
}
type recordType int
const (
rtNewLine recordType = iota
rtText
rtUser
rtChannel
rtVariable
rtLink
)
func (l *recordableListner) addRecord(rt recordType, text string, target string) {
l.records = append(l.records, record{rt, text, target})
}
func (l *recordableListner) testEquals(expected []record, t *testing.T) {
if len(l.records) != len(expected) {
t.Errorf("Wrong length of records: %v, %v", len(l.records), len(expected))
return
}
for i := 0; i < len(l.records); i++ {
ra := l.records[i]
re := expected[i]
if ra.Type != re.Type || ra.Text != re.Text || ra.Target != re.Target {
t.Errorf("Wrong record: %v, %v", ra, re)
}
}
}
func (l *recordableListner) OnNewLine() {
l.addRecord(rtNewLine, "", "")
}
func (l *recordableListner) OnText(text string) {
l.addRecord(rtText, text, "")
}
func (l *recordableListner) OnUser(userID, alt string) {
l.addRecord(rtUser, alt, userID)
}
func (l *recordableListner) OnChannel(channelID, alt string) {
l.addRecord(rtChannel, alt, channelID)
}
func (l *recordableListner) OnVariable(variableID, alt string) {
l.addRecord(rtVariable, alt, variableID)
}
func (l *recordableListner) OnLink(href, text string) {
l.addRecord(rtLink, text, href)
}
func TestSlackMessageParser(t *testing.T) {
cases := []struct {
input string
expected []record
}{
{
"",
nil,
},
{
"single line",
[]record{
{rtText, "single line", ""},
},
},
{
"multi\nline",
[]record{
{rtText, "multi", ""},
{rtNewLine, "", ""},
{rtText, "line", ""},
},
},
{
"links\n<http://example.com/> <http://example.com/|example | example>",
[]record{
{rtText, "links", ""},
{rtNewLine, "", ""},
{rtLink, "", "http://example.com/"},
{rtText, " ", ""},
{rtLink, "example | example", "http://example.com/"},
},
},
{
"user, variable, channel\n<@U11111111>, <@U22222222|bob>, and <!here|here>\n\nPlease check <#C11111111> and <#C22222222|channel2> channels",
[]record{
{rtText, "user, variable, channel", ""},
{rtNewLine, "", ""},
{rtUser, "", "U11111111"},
{rtText, ", ", ""},
{rtUser, "bob", "U22222222"},
{rtText, ", and ", ""},
{rtVariable, "here", "here"},
{rtNewLine, "", ""},
{rtNewLine, "", ""},
{rtText, "Please check ", ""},
{rtChannel, "", "C11111111"},
{rtText, " and ", ""},
{rtChannel, "channel2", "C22222222"},
{rtText, " channels", ""},
},
},
}
for _, c := range cases {
actual := new(recordableListner)
parser := NewSlackMessageParser(actual)
parser.Parse(c.input)
actual.testEquals(c.expected, t)
}
}
func TestSlackTsToTime(t *testing.T) {
cases := []struct {
input string
expected time.Time
}{
{
"0",
time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
},
{
"1463128987.000002",
time.Date(2016, 5, 13, 8, 43, 7, 0, time.UTC),
},
}
duration, _ := time.ParseDuration("1s")
for _, c := range cases {
actual := SlackTsToTime(c.input)
if actual.Before(c.expected) {
t.Errorf("Too early ts: %v, %v", actual, c.expected)
}
if actual.After(c.expected.Add(duration)) {
t.Errorf("Too late ts: %v, %v", actual, c.expected.Add(duration))
}
}
}