forked from go-telegram/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_test.go
169 lines (133 loc) · 3.55 KB
/
handlers_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package bot
import (
"regexp"
"sync"
"testing"
"github.com/go-telegram/bot/models"
)
func Test_match_func(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
var called bool
id := b.RegisterHandlerMatchFunc(func(update *models.Update) bool {
called = true
if update.ID != 42 {
t.Error("invalid update id")
}
return true
}, nil)
h := b.handlers[id]
res := h.match(&models.Update{ID: 42})
if !called {
t.Error("not called")
}
if !res {
t.Error("unexpected false result")
}
}
func Test_match_exact(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
id := b.RegisterHandler(HandlerTypeMessageText, "xxx", MatchTypeExact, nil)
h := b.handlers[id]
res := h.match(&models.Update{Message: &models.Message{Text: "zzz"}})
if res {
t.Error("unexpected true result")
}
res = h.match(&models.Update{Message: &models.Message{Text: "xxx"}})
if !res {
t.Error("unexpected false result")
}
}
func Test_match_prefix(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
id := b.RegisterHandler(HandlerTypeCallbackQueryData, "abc", MatchTypePrefix, nil)
h := b.handlers[id]
res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xabcdef"}})
if res {
t.Error("unexpected true result")
}
res = h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "abcdef"}})
if !res {
t.Error("unexpected false result")
}
}
func Test_match_contains(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
id := b.RegisterHandler(HandlerTypeCallbackQueryData, "abc", MatchTypeContains, nil)
h := b.handlers[id]
res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xxabxx"}})
if res {
t.Error("unexpected true result")
}
res = h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xxabcdef"}})
if !res {
t.Error("unexpected false result")
}
}
func Test_match_regexp(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
re := regexp.MustCompile("^[a-z]+")
id := b.RegisterHandlerRegexp(HandlerTypeCallbackQueryData, re, nil)
h := b.handlers[id]
res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "123abc"}})
if res {
t.Error("unexpected true result")
}
res = h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "abcdef"}})
if !res {
t.Error("unexpected false result")
}
}
func Test_match_invalid_type(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
id := b.RegisterHandler(-1, "", -1, nil)
h := b.handlers[id]
res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "123abc"}})
if res {
t.Error("unexpected true result")
}
}
func TestBot_RegisterUnregisterHandler(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
id1 := b.RegisterHandler(HandlerTypeCallbackQueryData, "", MatchTypeExact, nil)
id2 := b.RegisterHandler(HandlerTypeCallbackQueryData, "", MatchTypeExact, nil)
if len(b.handlers) != 2 {
t.Fatalf("unexpected handlers len")
}
if _, ok := b.handlers[id1]; !ok {
t.Fatalf("handler not found")
}
if _, ok := b.handlers[id2]; !ok {
t.Fatalf("handler not found")
}
b.UnregisterHandler(id1)
if len(b.handlers) != 1 {
t.Fatalf("unexpected handlers len")
}
if _, ok := b.handlers[id1]; ok {
t.Fatalf("handler found")
}
if _, ok := b.handlers[id2]; !ok {
t.Fatalf("handler not found")
}
}