nonebot2/tests/test_rule.py

89 lines
2.4 KiB
Python
Raw Normal View History

2022-01-05 18:29:11 +08:00
from typing import Tuple, Union
2021-12-23 19:36:29 +08:00
import pytest
from nonebug import App
2022-01-05 18:29:11 +08:00
from utils import make_fake_event, make_fake_message
@pytest.mark.asyncio
@pytest.mark.parametrize(
"msg,ignorecase,type,text,expected",
[
("prefix", False, "message", "prefix_", True),
("prefix", False, "message", "Prefix_", False),
("prefix", True, "message", "prefix_", True),
("prefix", True, "message", "Prefix_", True),
],
)
async def test_startswith(
app: App,
msg: Union[str, Tuple[str, ...]],
ignorecase: bool,
type: str,
text: str,
expected: bool,
):
from nonebot.rule import StartswithRule, startswith
test_startswith = startswith(msg, ignorecase)
dependent = list(test_startswith.checkers)[0]
checker = dependent.call
assert isinstance(checker, StartswithRule)
assert checker.msg == (msg,) if isinstance(msg, str) else msg
assert checker.ignorecase == ignorecase
message = make_fake_message()(text)
event = make_fake_event(_type=type, _message=message)()
assert await dependent(event=event) == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(
"msg,ignorecase,type,text,expected",
[
("suffix", False, "message", "_suffix", True),
("suffix", False, "message", "_Suffix", False),
("suffix", True, "message", "_suffix", True),
("suffix", True, "message", "_Suffix", True),
],
)
async def test_endswith(
app: App,
msg: Union[str, Tuple[str, ...]],
ignorecase: bool,
type: str,
text: str,
expected: bool,
):
from nonebot.rule import EndswithRule, endswith
test_endswith = endswith(msg, ignorecase)
dependent = list(test_endswith.checkers)[0]
checker = dependent.call
assert isinstance(checker, EndswithRule)
assert checker.msg == (msg,) if isinstance(msg, str) else msg
assert checker.ignorecase == ignorecase
message = make_fake_message()(text)
event = make_fake_event(_type=type, _message=message)()
assert await dependent(event=event) == expected
2021-12-23 19:36:29 +08:00
@pytest.mark.asyncio
async def test_command(app: App):
2022-01-05 18:29:11 +08:00
from nonebot.rule import CommandRule, command
2021-12-23 19:36:29 +08:00
from nonebot.consts import CMD_KEY, PREFIX_KEY
test_command = command("help")
dependent = list(test_command.checkers)[0]
checker = dependent.call
assert isinstance(checker, CommandRule)
assert checker.cmds == [("help",)]
state = {PREFIX_KEY: {CMD_KEY: ("help",)}}
assert await dependent(state=state)