async def test_matcher(app: App, load_plugin): from nonebot.matcher import Matcher from nonebot.params import DependParam, MatcherParam from plugins.param.param_matcher import matcher, receive, last_receive fake_matcher = Matcher() async with app.test_dependent(matcher, allow_types=[MatcherParam]) as ctx: ctx.pass_params(matcher=fake_matcher) ctx.should_return(fake_matcher) event = make_fake_event()() fake_matcher.set_receive("test", event) event_next = make_fake_event()() fake_matcher.set_receive("", event_next) async with app.test_dependent(receive, allow_types=[MatcherParam, DependParam]) as ctx: ctx.pass_params(matcher=fake_matcher) ctx.should_return(event) async with app.test_dependent(last_receive, allow_types=[MatcherParam, DependParam]) as ctx: ctx.pass_params(matcher=fake_matcher) ctx.should_return(event_next)
async def test_matcher_mutex(): from nonebot.plugins.single_session import matcher_mutex, _running_matcher am = asynccontextmanager(matcher_mutex) event = make_fake_event()() event_1 = make_fake_event()() event_2 = make_fake_event(_session_id="test1")() event_3 = make_fake_event(_session_id=None)() async with am(event) as ctx: assert ctx == False assert not _running_matcher async with am(event) as ctx: async with am(event_1) as ctx_1: assert ctx == False assert ctx_1 == True assert not _running_matcher async with am(event) as ctx: async with am(event_2) as ctx_2: assert ctx == False assert ctx_2 == False assert not _running_matcher async with am(event_3) as ctx_3: assert ctx_3 == False assert not _running_matcher
async def test_rule(app: App): from nonebot.rule import Rule from nonebot.exception import SkippedException async def falsy(): return False async def truthy(): return True async def skipped() -> bool: raise SkippedException def _is_eq(a: Rule, b: Rule) -> bool: return {d.call for d in a.checkers} == {d.call for d in b.checkers} assert _is_eq(Rule(truthy) & None, Rule(truthy)) assert _is_eq(Rule(truthy) & falsy, Rule(truthy, falsy)) assert _is_eq(Rule(truthy) & Rule(falsy), Rule(truthy, falsy)) assert _is_eq(None & Rule(truthy), Rule(truthy)) assert _is_eq(truthy & Rule(falsy), Rule(truthy, falsy)) 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
async def test_depend(app: App, load_plugin): from nonebot.params import DependParam from plugins.param.param_depend import ( ClassDependency, runned, depends, class_depend, test_depends, ) async with app.test_dependent(depends, allow_types=[DependParam]) as ctx: ctx.should_return(1) assert len(runned) == 1 and runned[0] == 1 runned.clear() async with app.test_matcher(test_depends) as ctx: bot = ctx.create_bot() event_next = make_fake_event()() ctx.receive_event(bot, event_next) assert len(runned) == 2 and runned[0] == runned[1] == 1 async with app.test_dependent(class_depend, allow_types=[DependParam]) as ctx: ctx.should_return(ClassDependency(x=1, y=2))
async def test_permission(app: App): from nonebot.permission import Permission from nonebot.exception import SkippedException async def falsy(): return False async def truthy(): return True async def skipped() -> bool: raise SkippedException def _is_eq(a: Permission, b: Permission) -> bool: return {d.call for d in a.checkers} == {d.call for d in b.checkers} assert _is_eq(Permission(truthy) | None, Permission(truthy)) assert _is_eq(Permission(truthy) | falsy, Permission(truthy, falsy)) assert _is_eq(Permission(truthy) | Permission(falsy), Permission(truthy, falsy)) assert _is_eq(None | Permission(truthy), Permission(truthy)) assert _is_eq(truthy | Permission(falsy), Permission(truthy, falsy)) event = make_fake_event()() async with app.test_api() as ctx: bot = ctx.create_bot() assert await Permission(falsy)(bot, event) == False assert await Permission(truthy)(bot, event) == True assert await Permission(skipped)(bot, event) == False assert await Permission(truthy, falsy)(bot, event) == True assert await Permission(truthy, skipped)(bot, event) == True
async def test_permission_updater(app: App, load_plugin): from nonebot.permission import User from plugins.matcher.matcher_permission import ( default_permission, test_custom_updater, test_permission_updater, ) event = make_fake_event(_session_id="test")() assert test_permission_updater.permission is default_permission async with app.test_api() as ctx: bot = ctx.create_bot() matcher = test_permission_updater() new_perm = await matcher.update_permission(bot, event) assert len(new_perm.checkers) == 1 checker = list(new_perm.checkers)[0].call assert isinstance(checker, User) assert checker.users == ("test", ) assert checker.perm is default_permission assert test_custom_updater.permission is default_permission async with app.test_api() as ctx: bot = ctx.create_bot() matcher = test_custom_updater() new_perm = await matcher.update_permission(bot, event) assert new_perm is default_permission
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
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
async def test_weather(app: App): from examples.weather import weather from utils import make_fake_event, make_fake_message # 将此处的 make_fake_message() 替换为你要发送的平台消息 Message 类型 Message = make_fake_message() async with app.test_matcher(weather) as ctx: bot = ctx.create_bot() msg = Message("/天气 上海") # 将此处的 make_fake_event() 替换为你要发送的平台事件 Event 类型 event = make_fake_event(_message=msg, _to_me=True)() ctx.receive_event(bot, event) ctx.should_call_send(event, "上海的天气是...", True) ctx.should_finished() async with app.test_matcher(weather) as ctx: bot = ctx.create_bot() msg = Message("/天气 南京") # 将此处的 make_fake_event() 替换为你要发送的平台事件 Event 类型 event = make_fake_event(_message=msg, _to_me=True)() ctx.receive_event(bot, event) ctx.should_call_send(event, Message("你想查询的城市 南京 暂不支持,请重新输入!"), True) ctx.should_rejected() msg = Message("北京") event = make_fake_event(_message=msg)() ctx.receive_event(bot, event) ctx.should_call_send(event, "北京的天气是...", True) ctx.should_finished() async with app.test_matcher(weather) as ctx: bot = ctx.create_bot() msg = Message("/天气") # 将此处的 make_fake_event() 替换为你要发送的平台事件 Event 类型 event = make_fake_event(_message=msg, _to_me=True)() ctx.receive_event(bot, event) ctx.should_call_send(event, "你想查询哪个城市的天气呢?", True) msg = Message("杭州") event = make_fake_event(_message=msg)() ctx.receive_event(bot, event) ctx.should_call_send(event, Message("你想查询的城市 杭州 暂不支持,请重新输入!"), True) ctx.should_rejected() msg = Message("北京") event = make_fake_event(_message=msg)() ctx.receive_event(bot, event) ctx.should_call_send(event, "北京的天气是...", True) ctx.should_finished()
async def test_request( app: App, type: str, expected: bool, ): from nonebot.permission import REQUEST, Request dependent = list(REQUEST.checkers)[0] checker = dependent.call assert isinstance(checker, Request) event = make_fake_event(_type=type)() assert await dependent(event=event) == expected
async def test_notice( app: App, type: str, expected: bool, ): from nonebot.permission import NOTICE, Notice dependent = list(NOTICE.checkers)[0] checker = dependent.call assert isinstance(checker, Notice) event = make_fake_event(_type=type)() assert await dependent(event=event) == expected
async def test_message( app: App, type: str, expected: bool, ): from nonebot.permission import MESSAGE, Message dependent = list(MESSAGE.checkers)[0] checker = dependent.call assert isinstance(checker, Message) event = make_fake_event(_type=type)() assert await dependent(event=event) == expected
async def test_startswith( app: App, type: str, user_id: str, expected: bool, ): from nonebot.permission import SUPERUSER, SuperUser dependent = list(SUPERUSER.checkers)[0] checker = dependent.call assert isinstance(checker, SuperUser) event = make_fake_event(_type=type, _user_id=user_id)() async with app.test_api() as ctx: bot = ctx.create_bot() assert await dependent(bot=bot, event=event) == expected
async def test_type_updater(app: App, load_plugin): from plugins.matcher.matcher_type import test_type_updater, test_custom_updater event = make_fake_event()() assert test_type_updater.type == "test" async with app.test_api() as ctx: bot = ctx.create_bot() matcher = test_type_updater() new_type = await matcher.update_type(bot, event) assert new_type == "message" assert test_custom_updater.type == "test" async with app.test_api() as ctx: bot = ctx.create_bot() matcher = test_custom_updater() new_type = await matcher.update_type(bot, event) assert new_type == "custom"
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
async def test_event(app: App, load_plugin): from nonebot.params import EventParam, DependParam from plugins.param.param_event import ( event, event_type, event_to_me, event_message, event_plain_text, ) fake_message = make_fake_message()("text") fake_event = make_fake_event(_message=fake_message)() async with app.test_dependent(event, allow_types=[EventParam]) as ctx: ctx.pass_params(event=fake_event) ctx.should_return(fake_event) async with app.test_dependent(event_type, allow_types=[EventParam, DependParam]) as ctx: ctx.pass_params(event=fake_event) ctx.should_return(fake_event.get_type()) async with app.test_dependent(event_message, allow_types=[EventParam, DependParam]) as ctx: ctx.pass_params(event=fake_event) ctx.should_return(fake_event.get_message()) async with app.test_dependent(event_plain_text, allow_types=[EventParam, DependParam]) as ctx: ctx.pass_params(event=fake_event) ctx.should_return(fake_event.get_plaintext()) async with app.test_dependent(event_to_me, allow_types=[EventParam, DependParam]) as ctx: ctx.pass_params(event=fake_event) ctx.should_return(fake_event.is_tome())
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
async def test_rule(app: App): from nonebot.rule import Rule from nonebot.exception import SkippedException 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
async def test_matcher(app: App, load_plugin): from plugins.matcher.matcher_process import ( test_got, test_handle, test_preset, test_combine, test_receive, test_overload, ) message = make_fake_message()("text") event = make_fake_event(_message=message)() message_next = make_fake_message()("text_next") event_next = make_fake_event(_message=message_next)() assert len(test_handle.handlers) == 1 async with app.test_matcher(test_handle) as ctx: bot = ctx.create_bot() ctx.receive_event(bot, event) ctx.should_call_send(event, "send", "result", at_sender=True) ctx.should_finished() assert len(test_got.handlers) == 1 async with app.test_matcher(test_got) as ctx: bot = ctx.create_bot() ctx.receive_event(bot, event) ctx.should_call_send(event, "prompt key1", "result1") ctx.receive_event(bot, event) ctx.should_call_send(event, "prompt key2", "result2") ctx.receive_event(bot, event) ctx.should_call_send(event, "reject", "result3", at_sender=True) ctx.should_rejected() ctx.receive_event(bot, event_next) assert len(test_receive.handlers) == 1 async with app.test_matcher(test_receive) as ctx: bot = ctx.create_bot() ctx.receive_event(bot, event) ctx.receive_event(bot, event) ctx.receive_event(bot, event) ctx.should_call_send(event, "pause", "result", at_sender=True) ctx.should_paused() assert len(test_receive.handlers) == 1 async with app.test_matcher(test_combine) as ctx: bot = ctx.create_bot() ctx.receive_event(bot, event) ctx.receive_event(bot, event) ctx.receive_event(bot, event) ctx.should_rejected() ctx.receive_event(bot, event_next) ctx.should_rejected() ctx.receive_event(bot, event_next) ctx.should_rejected() ctx.receive_event(bot, event_next) assert len(test_preset.handlers) == 2 async with app.test_matcher(test_preset) as ctx: bot = ctx.create_bot() ctx.receive_event(bot, event) ctx.receive_event(bot, event) ctx.should_rejected() ctx.receive_event(bot, event_next) assert len(test_overload.handlers) == 2 async with app.test_matcher(test_overload) as ctx: bot = ctx.create_bot() ctx.receive_event(bot, event) ctx.should_finished()