🐛 fix command whitespace if no arg (#1975)

This commit is contained in:
Ju4tCode 2023-05-04 14:25:09 +08:00 committed by GitHub
parent 6cfdbbe597
commit dc2c5e3c80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 11 deletions

View File

@ -376,11 +376,12 @@ class CommandRule:
async def __call__( async def __call__(
self, self,
cmd: Optional[Tuple[str, ...]] = Command(), cmd: Optional[Tuple[str, ...]] = Command(),
cmd_arg: Optional[Message] = CommandArg(),
cmd_whitespace: Optional[str] = CommandWhitespace(), cmd_whitespace: Optional[str] = CommandWhitespace(),
) -> bool: ) -> bool:
if cmd not in self.cmds: if cmd not in self.cmds:
return False return False
if self.force_whitespace is None: if self.force_whitespace is None or not cmd_arg:
return True return True
if isinstance(self.force_whitespace, str): if isinstance(self.force_whitespace, str):
return self.force_whitespace == cmd_whitespace return self.force_whitespace == cmd_whitespace

View File

@ -273,22 +273,34 @@ async def test_keyword(
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize( @pytest.mark.parametrize(
"cmds, cmd, force_whitespace, whitespace, expected", "cmds, force_whitespace, cmd, whitespace, arg_text, expected",
[ [
[(("help",),), ("help",), None, None, True], # command tests
[(("help",),), ("foo",), None, None, False], [(("help",),), None, ("help",), None, None, True],
[(("help", "foo"),), ("help", "foo"), True, " ", True], [(("help",),), None, ("foo",), None, None, False],
[(("help",), ("foo",)), ("help",), " ", " ", True], [(("help", "foo"),), None, ("help", "foo"), None, None, True],
[(("help",),), ("help",), False, " ", False], [(("help", "foo"),), None, ("help", "bar"), None, None, False],
[(("help",),), ("help",), True, None, False], [(("help",), ("foo",)), None, ("help",), None, None, True],
[(("help",),), ("help",), "\n", " ", False], [(("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( async def test_command(
cmds: Tuple[Tuple[str, ...]], cmds: Tuple[Tuple[str, ...]],
cmd: Tuple[str, ...],
force_whitespace: Optional[Union[str, bool]], force_whitespace: Optional[Union[str, bool]],
cmd: Tuple[str, ...],
whitespace: Optional[str], whitespace: Optional[str],
arg_text: Optional[str],
expected: bool, expected: bool,
): ):
test_command = command(*cmds, force_whitespace=force_whitespace) test_command = command(*cmds, force_whitespace=force_whitespace)
@ -298,7 +310,10 @@ async def test_command(
assert isinstance(checker, CommandRule) assert isinstance(checker, CommandRule)
assert checker.cmds == cmds 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 assert await dependent(state=state) == expected