-
Notifications
You must be signed in to change notification settings - Fork 68
/
process_update_test.go
156 lines (125 loc) · 3.4 KB
/
process_update_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
package bot
import (
"context"
"testing"
"github.com/go-telegram/bot/models"
)
func Test_applyMiddlewares(t *testing.T) {
h := func(ctx context.Context, bot *Bot, update *models.Update) {}
middleware1 := func(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, bot *Bot, update *models.Update) {
next(ctx, bot, update)
}
}
middleware2 := func(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, bot *Bot, update *models.Update) {
next(ctx, bot, update)
}
}
wrapped := applyMiddlewares(h, middleware1, middleware2)
if wrapped == nil {
t.Fatal("Expected wrapped handler to be non-nil")
}
}
func TestProcessUpdate(t *testing.T) {
var called bool
h := func(ctx context.Context, bot *Bot, update *models.Update) {
called = true
}
bot := &Bot{
defaultHandlerFunc: h,
middlewares: []Middleware{},
notAsyncHandlers: true,
}
ctx := context.Background()
upd := &models.Update{Message: &models.Message{Text: "test"}}
bot.ProcessUpdate(ctx, upd)
if !called {
t.Fatal("Expected default handler to be called")
}
}
func TestProcessUpdate_WithMiddlewares(t *testing.T) {
var called bool
h := func(ctx context.Context, bot *Bot, update *models.Update) {
called = true
}
middleware := func(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, bot *Bot, update *models.Update) {
next(ctx, bot, update)
}
}
bot := &Bot{
defaultHandlerFunc: h,
middlewares: []Middleware{middleware},
notAsyncHandlers: true,
}
ctx := context.Background()
upd := &models.Update{Message: &models.Message{Text: "test"}}
bot.ProcessUpdate(ctx, upd)
if !called {
t.Fatal("Expected default handler to be called")
}
}
func TestProcessUpdate_WithMatchTypeFunc(t *testing.T) {
var called string
h1 := func(ctx context.Context, bot *Bot, update *models.Update) {
called = "h1"
}
h2 := func(ctx context.Context, bot *Bot, update *models.Update) {
called = "h2"
}
m := func(update *models.Update) bool {
return update.CallbackQuery.GameShortName == "game"
}
bot := &Bot{
defaultHandlerFunc: h1,
notAsyncHandlers: true,
}
bot.RegisterHandlerMatchFunc(m, h2)
ctx := context.Background()
upd := &models.Update{ID: 42, CallbackQuery: &models.CallbackQuery{ID: "test", GameShortName: "game"}}
bot.ProcessUpdate(ctx, upd)
if called != "h2" {
t.Fatalf("Expected h2 handler to be called but %s handler was called", called)
}
}
func Test_findHandler(t *testing.T) {
var called bool
h := func(ctx context.Context, bot *Bot, update *models.Update) {
called = true
}
bot := &Bot{
defaultHandlerFunc: h,
}
// Register a handler
bot.handlers = append(bot.handlers, handler{
id: "test",
handlerType: HandlerTypeMessageText,
matchType: MatchTypeExact,
pattern: "test",
handler: h,
})
ctx := context.Background()
upd := &models.Update{Message: &models.Message{Text: "test"}}
handler := bot.findHandler(upd)
handler(ctx, bot, upd)
if !called {
t.Fatal("Expected registered handler to be called")
}
}
func Test_findHandler_Default(t *testing.T) {
var called bool
h := func(ctx context.Context, bot *Bot, update *models.Update) {
called = true
}
bot := &Bot{
defaultHandlerFunc: h,
}
ctx := context.Background()
upd := &models.Update{Message: &models.Message{Text: "test"}}
handler := bot.findHandler(upd)
handler(ctx, bot, upd)
if !called {
t.Fatal("Expected default handler to be called")
}
}