From dc2c5e3c800bfbb8849674268530b74e453facef Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Thu, 4 May 2023 14:25:09 +0800 Subject: [PATCH] :bug: fix command whitespace if no arg (#1975) --- nonebot/rule.py | 3 ++- tests/test_rule.py | 35 +++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/nonebot/rule.py b/nonebot/rule.py index 0d534dd2..8974e7ce 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -376,11 +376,12 @@ class CommandRule: async def __call__( self, cmd: Optional[Tuple[str, ...]] = Command(), + cmd_arg: Optional[Message] = CommandArg(), cmd_whitespace: Optional[str] = CommandWhitespace(), ) -> bool: if cmd not in self.cmds: return False - if self.force_whitespace is None: + if self.force_whitespace is None or not cmd_arg: return True if isinstance(self.force_whitespace, str): return self.force_whitespace == cmd_whitespace diff --git a/tests/test_rule.py b/tests/test_rule.py index d2b12c7f..0622215a 100644 --- a/tests/test_rule.py +++ b/tests/test_rule.py @@ -273,22 +273,34 @@ async def test_keyword( @pytest.mark.asyncio @pytest.mark.parametrize( - "cmds, cmd, force_whitespace, whitespace, expected", + "cmds, force_whitespace, cmd, whitespace, arg_text, expected", [ - [(("help",),), ("help",), None, None, True], - [(("help",),), ("foo",), None, None, False], - [(("help", "foo"),), ("help", "foo"), True, " ", True], - [(("help",), ("foo",)), ("help",), " ", " ", True], - [(("help",),), ("help",), False, " ", False], - [(("help",),), ("help",), True, None, False], - [(("help",),), ("help",), "\n", " ", False], + # command tests + [(("help",),), None, ("help",), None, None, True], + [(("help",),), None, ("foo",), None, None, False], + [(("help", "foo"),), None, ("help", "foo"), None, None, True], + [(("help", "foo"),), None, ("help", "bar"), None, None, False], + [(("help",), ("foo",)), None, ("help",), None, None, True], + [(("help",), ("foo",)), None, ("bar",), None, None, False], + # whitespace tests + [(("help",),), True, ("help",), " ", "arg", True], + [(("help",),), True, ("help",), None, "arg", False], + [(("help",),), True, ("help",), None, None, True], + [(("help",),), False, ("help",), " ", "arg", False], + [(("help",),), False, ("help",), None, "arg", True], + [(("help",),), False, ("help",), None, None, True], + [(("help",),), " ", ("help",), " ", "arg", True], + [(("help",),), " ", ("help",), "\n", "arg", False], + [(("help",),), " ", ("help",), None, "arg", False], + [(("help",),), " ", ("help",), None, None, True], ], ) async def test_command( cmds: Tuple[Tuple[str, ...]], - cmd: Tuple[str, ...], force_whitespace: Optional[Union[str, bool]], + cmd: Tuple[str, ...], whitespace: Optional[str], + arg_text: Optional[str], expected: bool, ): test_command = command(*cmds, force_whitespace=force_whitespace) @@ -298,7 +310,10 @@ async def test_command( assert isinstance(checker, CommandRule) assert checker.cmds == cmds - state = {PREFIX_KEY: {CMD_KEY: cmd, CMD_WHITESPACE_KEY: whitespace}} + arg = arg_text if arg_text is None else make_fake_message()(arg_text) + state = { + PREFIX_KEY: {CMD_KEY: cmd, CMD_WHITESPACE_KEY: whitespace, CMD_ARG_KEY: arg} + } assert await dependent(state=state) == expected