-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmux_test.go
126 lines (111 loc) · 3.54 KB
/
mux_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
package telemux_test
import (
"errors"
"os"
"reflect"
"testing"
tm "github.com/and3rson/telemux/v2"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func ExampleNewMux() {
// This part is a boilerplate from go-telegram-bot-api library.
bot, _ := tgbotapi.NewBotAPI(os.Getenv("TG_TOKEN"))
bot.Debug = true
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
// Create a multiplexer with two handlers: one for command and one for all messages.
// If a handler cannot handle the update (fails the filter),
// multiplexer will proceed to the next handler.
mux := tm.NewMux().
AddHandler(tm.NewHandler(
tm.IsCommandMessage("start"),
func(u *tm.Update) {
bot.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "Hello! Say something. :)"))
},
)).
AddHandler(tm.NewHandler(
tm.Any(),
func(u *tm.Update) {
bot.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "You said: "+u.Message.Text))
},
))
// Dispatch all telegram updates to multiplexer
for update := range updates {
mux.Dispatch(bot, update)
}
}
func TestMuxDispatch(t *testing.T) {
NewTGUpdate := func(text string) tgbotapi.Update {
u := tgbotapi.Update{}
u.Message = &tgbotapi.Message{}
u.Message.Text = text
return u
}
stack := []string{}
mux := tm.NewMux().
AddHandler(tm.NewMessageHandler(tm.HasRegex("^1"), func(u *tm.Update) { stack = append(stack, "1") })).
AddMux(
tm.NewMux().
SetGlobalFilter(tm.HasRegex("^2")).
AddHandler(tm.NewMessageHandler(tm.HasRegex("^21"), func(u *tm.Update) { stack = append(stack, "21") })).
AddHandler(tm.NewMessageHandler(tm.HasRegex("^22"), func(u *tm.Update) { stack = append(stack, "22") })),
)
assert(mux.Dispatch(nil, NewTGUpdate("1")), t, "Dispatch 1")
assert(reflect.DeepEqual(stack, []string{"1"}), t, "Check 1")
stack = []string{}
assert(!mux.Dispatch(nil, NewTGUpdate("2")), t, "Dispatch 2")
assert(mux.Dispatch(nil, NewTGUpdate("21")), t, "Dispatch 21")
assert(reflect.DeepEqual(stack, []string{"21"}), t, "Check 21")
stack = []string{}
assert(mux.Dispatch(nil, NewTGUpdate("22")), t, "Dispatch 22")
assert(reflect.DeepEqual(stack, []string{"22"}), t, "Check 22")
stack = []string{}
assert(!mux.Dispatch(nil, NewTGUpdate("23")), t, "Dispatch 23")
assert(reflect.DeepEqual(stack, []string{}), t, "Check 22")
assert(!mux.Dispatch(nil, NewTGUpdate("33")), t, "Dispatch 33")
assert(reflect.DeepEqual(stack, []string{}), t, "Check 33")
}
func TestMuxRecover(t *testing.T) {
NewTGUpdate := func(text string) tgbotapi.Update {
u := tgbotapi.Update{}
u.Message = &tgbotapi.Message{}
u.Message.Text = text
return u
}
recovered := struct {
e error
s string
}{}
mux := tm.NewMux().
AddHandler(tm.NewMessageHandler(nil, func(u *tm.Update) {
if u.EffectiveMessage().Text == "panic_string" {
panic("boom")
} else if u.EffectiveMessage().Text == "panic_error" {
panic(errors.New("boom"))
}
})).
SetRecover(func(u *tm.Update, e error, s string) {
recovered.e = e
recovered.s = s
})
mux.Dispatch(nil, NewTGUpdate("keep_calm"))
assert(recovered.e == nil, t)
assert(recovered.s == "", t)
mux.Dispatch(nil, NewTGUpdate("panic_string"))
assert(recovered.e.Error() == "boom", t)
assert(recovered.s != "", t)
mux.Dispatch(nil, NewTGUpdate("panic_error"))
assert(recovered.e.Error() == "boom", t)
assert(recovered.s != "", t)
mux.SetRecover(nil)
func() {
defer func() {
r := recover()
if r == nil || r.(error).Error() != "boom" {
t.Error("Expected unhandled panic")
}
}()
mux.Dispatch(nil, NewTGUpdate("panic_error"))
}()
}