Skip to content

Commit 87efd50

Browse files
committed
test(router): fix predicates and update tests to use Context
1 parent c7f4425 commit 87efd50

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

predicates_test.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestCommandName(t *testing.T) {
2626

2727
func TestCommandPredicate(t *testing.T) {
2828
// Untargeted command always matches.
29-
plain := &Update{Message: &Message{Text: "/start hi"}}
29+
plain := &Context{Update: &Update{Message: &Message{Text: "/start hi"}}}
3030
if !Command("start")(plain) || !Command("/start")(plain) {
3131
t.Fatal("Command should match with and without slash")
3232
}
@@ -35,54 +35,56 @@ func TestCommandPredicate(t *testing.T) {
3535
t.Fatal("Command should not match a different command")
3636
}
3737

38-
if Command("start")(&Update{CallbackQuery: &CallbackQuery{}}) {
38+
nonMsg := &Context{Update: &Update{CallbackQuery: &CallbackQuery{}}}
39+
if Command("start")(nonMsg) {
3940
t.Fatal("Command should not match a non-message update")
4041
}
4142

4243
// Targeted command matches only when the @target is this bot (case-insensitive).
43-
mine := &Update{Message: &Message{Text: "/start@MyBot hi"}, botUsername: "mybot"}
44+
// Если поле botUsername находится в Update:
45+
mine := &Context{Update: &Update{Message: &Message{Text: "/start@MyBot hi"}, botUsername: "mybot"}}
4446
if !Command("start")(mine) {
4547
t.Fatal("Command should match when targeted at this bot")
4648
}
4749

48-
other := &Update{Message: &Message{Text: "/start@other_bot hi"}, botUsername: "mybot"}
50+
other := &Context{Update: &Update{Message: &Message{Text: "/start@other_bot hi"}, botUsername: "mybot"}}
4951
if Command("start")(other) {
5052
t.Fatal("Command should not match when targeted at another bot")
5153
}
5254

5355
// Targeted command with an unknown bot username does not match.
54-
unknown := &Update{Message: &Message{Text: "/start@mybot hi"}}
56+
unknown := &Context{Update: &Update{Message: &Message{Text: "/start@mybot hi"}}}
5557
if Command("start")(unknown) {
5658
t.Fatal("Command should not match a targeted command when the bot username is unknown")
5759
}
5860
}
5961

6062
func TestTextAndChatPredicates(t *testing.T) {
61-
u := &Update{Message: &Message{Text: "hello world", Chat: Chat{Type: ChatTypePrivate}}}
62-
if !HasPrefix("hello")(u) || !HasText()(u) || !Regex(`^hello`)(u) {
63+
c := &Context{Update: &Update{Message: &Message{Text: "hello world", Chat: Chat{Type: ChatTypePrivate}}}}
64+
if !HasPrefix("hello")(c) || !HasText()(c) || !Regex(`^hello`)(c) {
6365
t.Fatal("text predicates should match")
6466
}
6567

66-
if !ChatTypeIs(ChatTypePrivate)(u) || ChatTypeIs(ChatTypeChannel)(u) {
68+
if !ChatTypeIs(ChatTypePrivate)(c) || ChatTypeIs(ChatTypeChannel)(c) {
6769
t.Fatal("ChatTypeIs mismatch")
6870
}
6971

70-
if !Not(TextEquals("nope"))(u) {
72+
if !Not(TextEquals("nope"))(c) {
7173
t.Fatal("Not should invert")
7274
}
7375
}
7476

7577
func TestCallbackPredicates(t *testing.T) {
76-
u := &Update{CallbackQuery: &CallbackQuery{Data: "vote:42"}}
77-
if !CallbackPrefix("vote:")(u) || !CallbackData("vote:42")(u) {
78+
c := &Context{Update: &Update{CallbackQuery: &CallbackQuery{Data: "vote:42"}}}
79+
if !CallbackPrefix("vote:")(c) || !CallbackData("vote:42")(c) {
7880
t.Fatal("callback predicates should match")
7981
}
8082

81-
if !Or(CallbackData("x"), CallbackPrefix("vote:"))(u) {
83+
if !Or(CallbackData("x"), CallbackPrefix("vote:"))(c) {
8284
t.Fatal("Or should match when one matches")
8385
}
8486

85-
if u.Text() != "vote:42" {
86-
t.Fatalf("Update.Text for callback = %q", u.Text())
87+
if c.Update.Text() != "vote:42" {
88+
t.Fatalf("Update.Text for callback = %q", c.Update.Text())
8789
}
8890
}

0 commit comments

Comments
 (0)