From b541d34c0c9e8e8c29062d4aff1623c38bf0890b Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 4 Feb 2023 12:51:37 +0200 Subject: [PATCH] better fix, added tests --- aiogram/filters/command.py | 5 ++++- tests/test_filters/test_command.py | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/aiogram/filters/command.py b/aiogram/filters/command.py index f3df8bfc..914ea26f 100644 --- a/aiogram/filters/command.py +++ b/aiogram/filters/command.py @@ -92,6 +92,9 @@ class Command(Filter): if not items: raise ValueError("At least one command should be specified") + if ignore_case: + items = map(str.casefold, items) + self.commands = tuple(items) self.prefix = prefix self.ignore_case = ignore_case @@ -167,7 +170,7 @@ class Command(Filter): command_name = command.command if self.ignore_case: - command_name, allowed_command = map(str.casefold, (command_name, allowed_command)) + command_name = command_name.casefold() if command_name == allowed_command: # String return command diff --git a/tests/test_filters/test_command.py b/tests/test_filters/test_command.py index 1bb7d0a4..feca2763 100644 --- a/tests/test_filters/test_command.py +++ b/tests/test_filters/test_command.py @@ -35,6 +35,22 @@ class TestCommandFilter: assert cmd.commands[0] == "start" # assert cmd == Command(commands=["start"]) + @pytest.mark.parametrize( + "commands,checklist", + [ + [("Test1", "tEst2", "teSt3"), ("test1", "test2", "test3")], + [("12TeSt", "3t4Est", "5TE6sT"), ("12test", "3t4est", "5te6st")], + [[BotCommand(command="Test", description="Test1")], ("test",)], + [[BotCommand(command="tEsT", description="Test2")], ("test",)], + ], + ) + def test_init_casefold(self, commands, checklist): + command = Command(*commands, ignore_case=True) + assert command.commands == checklist + + command = Command(*commands, ignore_case=False) + assert command.commands != checklist + @pytest.mark.parametrize( "text,command,result", [ @@ -72,10 +88,15 @@ class TestCommandFilter: ["/start test", CommandStart(deep_link=True), True], ["/start test", CommandStart(deep_link=True, deep_link_encoded=True), False], ["/start dGVzdA", CommandStart(deep_link=True, deep_link_encoded=True), True], + ["/TeSt", Command("test", ignore_case=True), True], + ["/TeSt", Command("TeSt", ignore_case=True), True], + ["/test", Command("TeSt", ignore_case=True), True], + ["/TeSt", Command("test", ignore_case=False), False], + ["/test", Command("TeSt", ignore_case=False), False], + ["/TeSt", Command("TeSt", ignore_case=False), True], ], ) async def test_parse_command(self, bot: MockedBot, text: str, result: bool, command: Command): - # TODO: test ignore case # TODO: test ignore mention message = Message(