mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 02:50:48 +00:00
✨ Feature: 存储 matcher 发送 prompt 的结果 (#3155)
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
from contextlib import suppress
|
||||
import re
|
||||
|
||||
from exceptiongroup import BaseExceptionGroup
|
||||
@ -5,6 +6,7 @@ from nonebug import App
|
||||
import pytest
|
||||
|
||||
from nonebot.consts import (
|
||||
ARG_KEY,
|
||||
CMD_ARG_KEY,
|
||||
CMD_KEY,
|
||||
CMD_START_KEY,
|
||||
@ -14,13 +16,14 @@ from nonebot.consts import (
|
||||
KEYWORD_KEY,
|
||||
PREFIX_KEY,
|
||||
RAW_CMD_KEY,
|
||||
RECEIVE_KEY,
|
||||
REGEX_MATCHED,
|
||||
SHELL_ARGS,
|
||||
SHELL_ARGV,
|
||||
STARTSWITH_KEY,
|
||||
)
|
||||
from nonebot.dependencies import Dependent
|
||||
from nonebot.exception import TypeMisMatch
|
||||
from nonebot.exception import PausedException, RejectedException, TypeMisMatch
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import (
|
||||
ArgParam,
|
||||
@ -469,8 +472,10 @@ async def test_matcher(app: App):
|
||||
matcher,
|
||||
not_legacy_matcher,
|
||||
not_matcher,
|
||||
pause_prompt_result,
|
||||
postpone_matcher,
|
||||
receive,
|
||||
receive_prompt_result,
|
||||
sub_matcher,
|
||||
union_matcher,
|
||||
)
|
||||
@ -538,12 +543,42 @@ async def test_matcher(app: App):
|
||||
ctx.pass_params(matcher=fake_matcher)
|
||||
ctx.should_return(event_next)
|
||||
|
||||
fake_matcher.set_target(RECEIVE_KEY.format(id="test"), cache=False)
|
||||
|
||||
async with app.test_api() as ctx:
|
||||
bot = ctx.create_bot()
|
||||
ctx.should_call_send(event, "test", result=True, bot=bot)
|
||||
with fake_matcher.ensure_context(bot, event):
|
||||
with suppress(RejectedException):
|
||||
await fake_matcher.reject("test")
|
||||
|
||||
async with app.test_dependent(
|
||||
receive_prompt_result, allow_types=[MatcherParam, DependParam]
|
||||
) as ctx:
|
||||
ctx.pass_params(matcher=fake_matcher)
|
||||
ctx.should_return(True)
|
||||
|
||||
async with app.test_api() as ctx:
|
||||
bot = ctx.create_bot()
|
||||
ctx.should_call_send(event, "test", result=False, bot=bot)
|
||||
with fake_matcher.ensure_context(bot, event):
|
||||
fake_matcher.set_target("test")
|
||||
with suppress(PausedException):
|
||||
await fake_matcher.pause("test")
|
||||
|
||||
async with app.test_dependent(
|
||||
pause_prompt_result, allow_types=[MatcherParam, DependParam]
|
||||
) as ctx:
|
||||
ctx.pass_params(matcher=fake_matcher)
|
||||
ctx.should_return(False)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_arg(app: App):
|
||||
from plugins.param.param_arg import (
|
||||
annotated_arg,
|
||||
annotated_arg_plain_text,
|
||||
annotated_arg_prompt_result,
|
||||
annotated_arg_str,
|
||||
annotated_multi_arg,
|
||||
annotated_prior_arg,
|
||||
@ -553,6 +588,7 @@ async def test_arg(app: App):
|
||||
)
|
||||
|
||||
matcher = Matcher()
|
||||
event = make_fake_event()()
|
||||
message = FakeMessage("text")
|
||||
matcher.set_arg("key", message)
|
||||
|
||||
@ -582,6 +618,21 @@ async def test_arg(app: App):
|
||||
ctx.pass_params(matcher=matcher)
|
||||
ctx.should_return(message.extract_plain_text())
|
||||
|
||||
matcher.set_target(ARG_KEY.format(key="key"), cache=False)
|
||||
|
||||
async with app.test_api() as ctx:
|
||||
bot = ctx.create_bot()
|
||||
ctx.should_call_send(event, "test", result="arg", bot=bot)
|
||||
with matcher.ensure_context(bot, event):
|
||||
with suppress(RejectedException):
|
||||
await matcher.reject("test")
|
||||
|
||||
async with app.test_dependent(
|
||||
annotated_arg_prompt_result, allow_types=[ArgParam]
|
||||
) as ctx:
|
||||
ctx.pass_params(matcher=matcher)
|
||||
ctx.should_return("arg")
|
||||
|
||||
async with app.test_dependent(annotated_multi_arg, allow_types=[ArgParam]) as ctx:
|
||||
ctx.pass_params(matcher=matcher)
|
||||
ctx.should_return(message.extract_plain_text())
|
||||
|
Reference in New Issue
Block a user