2023-01-19 15:59:16 +08:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-telegram/bot/models"
|
|
|
|
)
|
|
|
|
|
2024-09-16 16:38:22 +08:00
|
|
|
func findHandler(b *Bot, id string) *handler {
|
|
|
|
b.handlersMx.RLock()
|
|
|
|
defer b.handlersMx.RUnlock()
|
|
|
|
|
|
|
|
for _, h := range b.handlers {
|
|
|
|
if h.id == id {
|
|
|
|
return &h
|
|
|
|
}
|
2023-01-19 15:59:16 +08:00
|
|
|
}
|
|
|
|
|
2024-09-16 16:38:22 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_match_func(t *testing.T) {
|
|
|
|
b := &Bot{}
|
|
|
|
|
2023-01-19 15:59:16 +08:00
|
|
|
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)
|
|
|
|
|
2024-09-16 16:38:22 +08:00
|
|
|
h := findHandler(b, id)
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
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) {
|
2024-09-16 16:38:22 +08:00
|
|
|
b := &Bot{}
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
id := b.RegisterHandler(HandlerTypeMessageText, "xxx", MatchTypeExact, nil)
|
|
|
|
|
2024-09-16 16:38:22 +08:00
|
|
|
h := findHandler(b, id)
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-23 16:04:26 +08:00
|
|
|
func Test_match_caption_exact(t *testing.T) {
|
|
|
|
b := &Bot{}
|
|
|
|
|
|
|
|
id := b.RegisterHandler(HandlerTypePhotoCaption, "xxx", MatchTypeExact, nil)
|
|
|
|
|
|
|
|
h := findHandler(b, id)
|
|
|
|
|
|
|
|
res := h.match(&models.Update{Message: &models.Message{Caption: "zzz"}})
|
|
|
|
if res {
|
|
|
|
t.Error("unexpected true result")
|
|
|
|
}
|
|
|
|
|
|
|
|
res = h.match(&models.Update{Message: &models.Message{Caption: "xxx"}})
|
|
|
|
if !res {
|
|
|
|
t.Error("unexpected false result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 15:59:16 +08:00
|
|
|
func Test_match_prefix(t *testing.T) {
|
2024-09-16 16:38:22 +08:00
|
|
|
b := &Bot{}
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
id := b.RegisterHandler(HandlerTypeCallbackQueryData, "abc", MatchTypePrefix, nil)
|
|
|
|
|
2024-09-16 16:38:22 +08:00
|
|
|
h := findHandler(b, id)
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
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) {
|
2024-09-16 16:38:22 +08:00
|
|
|
b := &Bot{}
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
id := b.RegisterHandler(HandlerTypeCallbackQueryData, "abc", MatchTypeContains, nil)
|
|
|
|
|
2024-09-16 16:38:22 +08:00
|
|
|
h := findHandler(b, id)
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
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) {
|
2024-09-16 16:38:22 +08:00
|
|
|
b := &Bot{}
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
re := regexp.MustCompile("^[a-z]+")
|
|
|
|
|
|
|
|
id := b.RegisterHandlerRegexp(HandlerTypeCallbackQueryData, re, nil)
|
|
|
|
|
2024-09-16 16:38:22 +08:00
|
|
|
h := findHandler(b, id)
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
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) {
|
2024-09-16 16:38:22 +08:00
|
|
|
b := &Bot{}
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
id := b.RegisterHandler(-1, "", -1, nil)
|
|
|
|
|
2024-09-16 16:38:22 +08:00
|
|
|
h := findHandler(b, id)
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "123abc"}})
|
|
|
|
if res {
|
|
|
|
t.Error("unexpected true result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBot_RegisterUnregisterHandler(t *testing.T) {
|
2024-09-16 16:38:22 +08:00
|
|
|
b := &Bot{}
|
2023-01-19 15:59:16 +08:00
|
|
|
|
|
|
|
id1 := b.RegisterHandler(HandlerTypeCallbackQueryData, "", MatchTypeExact, nil)
|
|
|
|
id2 := b.RegisterHandler(HandlerTypeCallbackQueryData, "", MatchTypeExact, nil)
|
|
|
|
|
|
|
|
if len(b.handlers) != 2 {
|
|
|
|
t.Fatalf("unexpected handlers len")
|
|
|
|
}
|
2024-09-16 16:38:22 +08:00
|
|
|
if h := findHandler(b, id1); h == nil {
|
2023-01-19 15:59:16 +08:00
|
|
|
t.Fatalf("handler not found")
|
|
|
|
}
|
2024-09-16 16:38:22 +08:00
|
|
|
if h := findHandler(b, id2); h == nil {
|
2023-01-19 15:59:16 +08:00
|
|
|
t.Fatalf("handler not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.UnregisterHandler(id1)
|
|
|
|
if len(b.handlers) != 1 {
|
|
|
|
t.Fatalf("unexpected handlers len")
|
|
|
|
}
|
2024-09-16 16:38:22 +08:00
|
|
|
if h := findHandler(b, id1); h != nil {
|
2023-01-19 15:59:16 +08:00
|
|
|
t.Fatalf("handler found")
|
|
|
|
}
|
2024-09-16 16:38:22 +08:00
|
|
|
if h := findHandler(b, id2); h == nil {
|
2023-01-19 15:59:16 +08:00
|
|
|
t.Fatalf("handler not found")
|
|
|
|
}
|
|
|
|
}
|
2024-09-16 16:16:03 +08:00
|
|
|
|
|
|
|
func Test_match_exact_game(t *testing.T) {
|
2024-09-16 16:38:22 +08:00
|
|
|
b := &Bot{}
|
2024-09-16 16:16:03 +08:00
|
|
|
|
|
|
|
id := b.RegisterHandler(HandlerTypeCallbackQueryGameShortName, "xxx", MatchTypeExact, nil)
|
|
|
|
|
2024-09-16 16:38:22 +08:00
|
|
|
h := findHandler(b, id)
|
2024-09-16 16:16:03 +08:00
|
|
|
u := models.Update{
|
|
|
|
ID: 42,
|
|
|
|
CallbackQuery: &models.CallbackQuery{
|
|
|
|
ID: "1000",
|
|
|
|
GameShortName: "xxx",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
res := h.match(&u)
|
|
|
|
if !res {
|
|
|
|
t.Error("unexpected true result")
|
|
|
|
}
|
|
|
|
}
|