-
Notifications
You must be signed in to change notification settings - Fork 5
/
router_test.go
148 lines (130 loc) · 2.86 KB
/
router_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
package govkbot
import (
"testing"
)
func baseHandler(m *Message) (reply string) {
return m.Body
}
func errorHandler(msg *Message, err error) {
return
}
func TestHandleMessage(t *testing.T) {
HandleMessage("/help", baseHandler)
if len(Bot.msgRoutes) == 0 {
t.Error("Error adding message handler")
}
}
func TestHandleAction(t *testing.T) {
HandleAction("/help", baseHandler)
if len(Bot.actionRoutes) == 0 {
t.Error("Error adding action handler")
}
}
func TestHandleError(t *testing.T) {
HandleError(errorHandler)
if Bot.errorHandler == nil {
t.Error("Error set error handler")
}
}
func TestSetAPI(t *testing.T) {
token := "12345"
SetAPI(token, "https://vk.com/api/", "5.131")
if API.Token != token {
t.Error("Error setup API")
}
SetLang("ru")
if API.Lang != "ru" {
t.Error("Error setup Lang")
}
SetLang("")
}
func TestSetToken(t *testing.T) {
token := "12345"
SetToken(token)
if API.Token != token {
t.Error("Error setup API")
}
}
func TestSetAutoFriend(t *testing.T) {
SetAutoFriend(true)
if !Bot.autoFriend {
t.Error("Error set auto friend")
}
SetAutoFriend(false)
}
func TestSetDebug(t *testing.T) {
SetDebug(true)
if !API.DEBUG {
t.Error("Error set debug mode")
}
SetDebug(false)
}
func TestRouteMessage(t *testing.T) {
HandleMessage("/help", baseHandler)
SetAPI("", "test", "")
m := Message{Body: "/help"}
replies, err := Bot.RouteMessage(&m)
if err != nil {
t.Error(err.Error())
}
if replies[0].Msg != "/help" {
t.Error(wrongValueReturned)
}
}
func TestRouteAction(t *testing.T) {
HandleAction("friend_add", baseHandler)
SetAPI("", "test", "")
m := Message{Action: "friend_add", Body: "ok"}
replies, err := Bot.RouteAction(&m)
if err != nil {
t.Error(err.Error())
}
if replies[0] != "ok" {
t.Error(wrongValueReturned)
}
}
func TestRouteMessages(t *testing.T) {
HandleMessage("/help", baseHandler)
SetAPI("", "test", "")
var messages []*Message
m1 := Message{Body: "/help"}
messages = append(messages, &m1)
m2 := Message{Action: "friend_add", Body: "ok"}
messages = append(messages, &m2)
m3 := Message{Body: "skip"}
messages = append(messages, &m3)
replies := Bot.RouteMessages(messages)
if replies[&m1][0].Msg != "/help" {
t.Error(wrongValueReturned)
}
if replies[&m2][0].Msg != "ok" {
t.Error(wrongValueReturned)
}
}
func TestGetMessages(t *testing.T) {
SetAPI("", "test", "")
messages, err := Bot.GetMessages()
if err != nil {
t.Error(err.Error())
}
if len(messages) == 0 {
t.Error("No messages")
}
}
func TestCheckFriends(t *testing.T) {
SetAPI("", "test", "")
Bot.CheckFriends()
}
func TestMainRoute(t *testing.T) {
SetAPI("", "test", "")
HandleError(errorHandler)
poller := NewUserLongPollServer(false, longPollVersion, API.RequestInterval)
Bot.MainRoute(poller)
}
func TestNotifyAdmin(t *testing.T) {
SetAPI("", "test", "")
err := NotifyAdmin("ok")
if err != nil {
t.Error(err.Error())
}
}