mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-12-01 01:25:07 +08:00
commit
1ee1ed80c0
@ -446,7 +446,7 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
return
|
return
|
||||||
if matcher.get_receive(id):
|
if matcher.get_receive(id):
|
||||||
return
|
return
|
||||||
raise RejectedException
|
await matcher.reject()
|
||||||
|
|
||||||
_parameterless = [params.Depends(_receive), *(parameterless or [])]
|
_parameterless = [params.Depends(_receive), *(parameterless or [])]
|
||||||
|
|
||||||
@ -490,9 +490,7 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
return
|
return
|
||||||
if matcher.get_arg(key):
|
if matcher.get_arg(key):
|
||||||
return
|
return
|
||||||
if prompt is not None:
|
await matcher.reject(prompt)
|
||||||
await matcher.send(prompt)
|
|
||||||
raise RejectedException
|
|
||||||
|
|
||||||
_parameterless = [
|
_parameterless = [
|
||||||
params.Depends(_key_getter),
|
params.Depends(_key_getter),
|
||||||
@ -579,7 +577,9 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def reject(
|
async def reject(
|
||||||
cls, prompt: Optional[Union[str, Message, MessageSegment]] = None, **kwargs
|
cls,
|
||||||
|
prompt: Optional[Union[str, Message, MessageSegment, MessageTemplate]] = None,
|
||||||
|
**kwargs,
|
||||||
) -> NoReturn:
|
) -> NoReturn:
|
||||||
"""
|
"""
|
||||||
:说明:
|
:说明:
|
||||||
@ -600,7 +600,7 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
async def reject_arg(
|
async def reject_arg(
|
||||||
cls,
|
cls,
|
||||||
key: str,
|
key: str,
|
||||||
prompt: Optional[Union[str, Message, MessageSegment]] = None,
|
prompt: Optional[Union[str, Message, MessageSegment, MessageTemplate]] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> NoReturn:
|
) -> NoReturn:
|
||||||
"""
|
"""
|
||||||
@ -625,7 +625,7 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
async def reject_receive(
|
async def reject_receive(
|
||||||
cls,
|
cls,
|
||||||
id: str = "",
|
id: str = "",
|
||||||
prompt: Optional[Union[str, Message, MessageSegment]] = None,
|
prompt: Optional[Union[str, Message, MessageSegment, MessageTemplate]] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> NoReturn:
|
) -> NoReturn:
|
||||||
"""
|
"""
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
DEBUG=true
|
LOG_LEVEL=TRACE
|
||||||
NICKNAME=["test"]
|
NICKNAME=["test"]
|
||||||
CONFIG_FROM_ENV=
|
CONFIG_FROM_ENV=
|
||||||
|
@ -129,3 +129,35 @@ async def test_permission_updater(app: App, load_plugin):
|
|||||||
matcher = test_custom_updater()
|
matcher = test_custom_updater()
|
||||||
new_perm = await matcher.update_permission(bot, event)
|
new_perm = await matcher.update_permission(bot, event)
|
||||||
assert new_perm is default_permission
|
assert new_perm is default_permission
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_run(app: App):
|
||||||
|
from nonebot.matcher import Matcher, matchers
|
||||||
|
|
||||||
|
assert not matchers
|
||||||
|
event = make_fake_event()()
|
||||||
|
|
||||||
|
async def reject():
|
||||||
|
await Matcher.reject()
|
||||||
|
|
||||||
|
test_reject = Matcher.new(handlers=[reject])
|
||||||
|
|
||||||
|
async with app.test_api() as ctx:
|
||||||
|
bot = ctx.create_bot()
|
||||||
|
await test_reject().run(bot, event, {})
|
||||||
|
assert len(matchers[0]) == 1
|
||||||
|
assert len(matchers[0][0].handlers) == 1
|
||||||
|
|
||||||
|
del matchers[0]
|
||||||
|
|
||||||
|
async def pause():
|
||||||
|
await Matcher.pause()
|
||||||
|
|
||||||
|
test_pause = Matcher.new(handlers=[pause])
|
||||||
|
|
||||||
|
async with app.test_api() as ctx:
|
||||||
|
bot = ctx.create_bot()
|
||||||
|
await test_pause().run(bot, event, {})
|
||||||
|
assert len(matchers[0]) == 1
|
||||||
|
assert len(matchers[0][0].handlers) == 0
|
||||||
|
@ -1,18 +1,175 @@
|
|||||||
|
from typing import Tuple, Union
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from nonebug import App
|
from nonebug import App
|
||||||
|
|
||||||
|
from utils import make_fake_event, make_fake_message
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_command(app: App):
|
async def test_rule(app: App):
|
||||||
from nonebot.consts import CMD_KEY, PREFIX_KEY
|
from nonebot.rule import Rule
|
||||||
from nonebot.rule import Rule, CommandRule, command
|
from nonebot.exception import SkippedException
|
||||||
|
|
||||||
test_command = command("help")
|
async def falsy():
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def truthy():
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def skipped() -> bool:
|
||||||
|
raise SkippedException
|
||||||
|
|
||||||
|
event = make_fake_event()()
|
||||||
|
|
||||||
|
async with app.test_api() as ctx:
|
||||||
|
bot = ctx.create_bot()
|
||||||
|
assert await Rule(falsy)(bot, event, {}) == False
|
||||||
|
assert await Rule(truthy)(bot, event, {}) == True
|
||||||
|
assert await Rule(skipped)(bot, event, {}) == False
|
||||||
|
assert await Rule(truthy, falsy)(bot, event, {}) == False
|
||||||
|
assert await Rule(truthy, skipped)(bot, event, {}) == False
|
||||||
|
|
||||||
|
|
||||||
|
@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),
|
||||||
|
("prefix", False, "message", "prefoo", False),
|
||||||
|
("prefix", False, "message", "fooprefix", False),
|
||||||
|
(("prefix", "foo"), False, "message", "fooprefix", True),
|
||||||
|
("prefix", False, "notice", "foo", False),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
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),
|
||||||
|
("suffix", False, "message", "suffoo", False),
|
||||||
|
("suffix", False, "message", "suffixfoo", False),
|
||||||
|
(("suffix", "foo"), False, "message", "suffixfoo", True),
|
||||||
|
("suffix", False, "notice", "foo", False),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"kws,type,text,expected",
|
||||||
|
[
|
||||||
|
(("key",), "message", "_key_", True),
|
||||||
|
(("key", "foo"), "message", "_foo_", True),
|
||||||
|
(("key",), "notice", "foo", False),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_keyword(
|
||||||
|
app: App,
|
||||||
|
kws: Tuple[str, ...],
|
||||||
|
type: str,
|
||||||
|
text: str,
|
||||||
|
expected: bool,
|
||||||
|
):
|
||||||
|
from nonebot.rule import KeywordsRule, keyword
|
||||||
|
|
||||||
|
test_keyword = keyword(*kws)
|
||||||
|
dependent = list(test_keyword.checkers)[0]
|
||||||
|
checker = dependent.call
|
||||||
|
|
||||||
|
assert isinstance(checker, KeywordsRule)
|
||||||
|
assert checker.keywords == kws
|
||||||
|
|
||||||
|
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(
|
||||||
|
"cmds", [(("help",),), (("help", "foo"),), (("help",), ("foo",))]
|
||||||
|
)
|
||||||
|
async def test_command(app: App, cmds: Tuple[Tuple[str, ...]]):
|
||||||
|
from nonebot.rule import CommandRule, command
|
||||||
|
from nonebot.consts import CMD_KEY, PREFIX_KEY
|
||||||
|
|
||||||
|
test_command = command(*cmds)
|
||||||
dependent = list(test_command.checkers)[0]
|
dependent = list(test_command.checkers)[0]
|
||||||
checker = dependent.call
|
checker = dependent.call
|
||||||
|
|
||||||
assert isinstance(checker, CommandRule)
|
assert isinstance(checker, CommandRule)
|
||||||
assert checker.cmds == [("help",)]
|
assert checker.cmds == list(cmds)
|
||||||
|
|
||||||
state = {PREFIX_KEY: {CMD_KEY: ("help",)}}
|
for cmd in cmds:
|
||||||
|
state = {PREFIX_KEY: {CMD_KEY: cmd}}
|
||||||
assert await dependent(state=state)
|
assert await dependent(state=state)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: shell command
|
||||||
|
|
||||||
|
# TODO: regex
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.parametrize("expected", [True, False])
|
||||||
|
async def test_to_me(app: App, expected: bool):
|
||||||
|
from nonebot.rule import ToMeRule, to_me
|
||||||
|
|
||||||
|
test_keyword = to_me()
|
||||||
|
dependent = list(test_keyword.checkers)[0]
|
||||||
|
checker = dependent.call
|
||||||
|
|
||||||
|
assert isinstance(checker, ToMeRule)
|
||||||
|
|
||||||
|
event = make_fake_event(_to_me=expected)()
|
||||||
|
assert await dependent(event=event) == expected
|
||||||
|
@ -37,6 +37,10 @@ const config = {
|
|||||||
showLastUpdateAuthor: true,
|
showLastUpdateAuthor: true,
|
||||||
showLastUpdateTime: true,
|
showLastUpdateTime: true,
|
||||||
},
|
},
|
||||||
|
sitemap: {
|
||||||
|
changefreq: "daily",
|
||||||
|
priority: 0.5,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user