Skip to content

Commit

Permalink
🐛 fix reject target error
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Dec 4, 2024
1 parent 227729c commit 27cca30
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
18 changes: 14 additions & 4 deletions nonebot/internal/matcher/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,13 @@ async def reject_arg(
请参考对应 adapter 的 bot 对象 api
"""
matcher = current_matcher.get()
matcher.set_target(ARG_KEY.format(key=key))
await cls.reject(prompt, **kwargs)
arg_key = ARG_KEY.format(key=key)
matcher.set_target(arg_key)

if prompt is not None:
result = await cls.send(prompt, **kwargs)
matcher.state[REJECT_PROMPT_RESULT_KEY.format(key=arg_key)] = result

Check warning on line 664 in nonebot/internal/matcher/matcher.py

View check run for this annotation

Codecov / codecov/patch

nonebot/internal/matcher/matcher.py#L663-L664

Added lines #L663 - L664 were not covered by tests
raise RejectedException

@classmethod
async def reject_receive(
Expand All @@ -676,8 +681,13 @@ async def reject_receive(
请参考对应 adapter 的 bot 对象 api
"""
matcher = current_matcher.get()
matcher.set_target(RECEIVE_KEY.format(id=id))
await cls.reject(prompt, **kwargs)
receive_key = RECEIVE_KEY.format(id=id)
matcher.set_target(receive_key)

if prompt is not None:
result = await cls.send(prompt, **kwargs)
matcher.state[REJECT_PROMPT_RESULT_KEY.format(key=receive_key)] = result

Check warning on line 689 in nonebot/internal/matcher/matcher.py

View check run for this annotation

Codecov / codecov/patch

nonebot/internal/matcher/matcher.py#L688-L689

Added lines #L688 - L689 were not covered by tests
raise RejectedException

@classmethod
def skip(cls) -> NoReturn:
Expand Down
35 changes: 27 additions & 8 deletions tests/test_param.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import suppress
import re

from exceptiongroup import BaseExceptionGroup
Expand All @@ -13,18 +14,16 @@
ENDSWITH_KEY,
FULLMATCH_KEY,
KEYWORD_KEY,
PAUSE_PROMPT_RESULT_KEY,
PREFIX_KEY,
RAW_CMD_KEY,
RECEIVE_KEY,
REGEX_MATCHED,
REJECT_PROMPT_RESULT_KEY,
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,
Expand Down Expand Up @@ -544,17 +543,28 @@ async def test_matcher(app: App):
ctx.pass_params(matcher=fake_matcher)
ctx.should_return(event_next)

fake_matcher.state[
REJECT_PROMPT_RESULT_KEY.format(key=RECEIVE_KEY.format(id="test"))
] = True
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)

fake_matcher.state[PAUSE_PROMPT_RESULT_KEY] = True
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):
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]
Expand All @@ -578,9 +588,9 @@ async def test_arg(app: App):
)

matcher = Matcher()
event = make_fake_event()()
message = FakeMessage("text")
matcher.set_arg("key", message)
matcher.state[REJECT_PROMPT_RESULT_KEY.format(key=ARG_KEY.format(key="key"))] = True

async with app.test_dependent(arg, allow_types=[ArgParam]) as ctx:
ctx.pass_params(matcher=matcher)
Expand Down Expand Up @@ -608,6 +618,15 @@ 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=True, 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:
Expand Down

0 comments on commit 27cca30

Please sign in to comment.