mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-28 00:31:14 +00:00
✅ add test cases
This commit is contained in:
@ -1,24 +0,0 @@
|
||||
from nonebot import on_message
|
||||
from nonebot.adapters import Event
|
||||
from nonebot.params import Depends
|
||||
|
||||
test_depends = on_message()
|
||||
|
||||
runned = []
|
||||
|
||||
|
||||
def dependency(event: Event):
|
||||
# test cache
|
||||
runned.append(event)
|
||||
return event
|
||||
|
||||
|
||||
@test_depends.handle()
|
||||
async def depends(x: Event = Depends(dependency)):
|
||||
# test dependency
|
||||
return x
|
||||
|
||||
|
||||
@test_depends.handle()
|
||||
async def depends_cache(y: Event = Depends(dependency, use_cache=True)):
|
||||
return y
|
7
tests/plugins/param/__init__.py
Normal file
7
tests/plugins/param/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
from pathlib import Path
|
||||
|
||||
from nonebot import load_plugins
|
||||
|
||||
_sub_plugins = set()
|
||||
|
||||
_sub_plugins |= load_plugins(str(Path(__file__).parent))
|
5
tests/plugins/param/param_bot.py
Normal file
5
tests/plugins/param/param_bot.py
Normal file
@ -0,0 +1,5 @@
|
||||
from nonebot.adapters import Bot
|
||||
|
||||
|
||||
async def get_bot(b: Bot):
|
||||
return b
|
30
tests/plugins/param/param_depend.py
Normal file
30
tests/plugins/param/param_depend.py
Normal file
@ -0,0 +1,30 @@
|
||||
from nonebot import on_message
|
||||
from nonebot.adapters import Event
|
||||
from nonebot.params import Depends
|
||||
|
||||
test_depends = on_message()
|
||||
|
||||
runned = []
|
||||
|
||||
|
||||
def dependency():
|
||||
runned.append(1)
|
||||
return 1
|
||||
|
||||
|
||||
def parameterless():
|
||||
assert len(runned) == 0
|
||||
runned.append(1)
|
||||
|
||||
|
||||
# test parameterless
|
||||
@test_depends.handle(parameterless=[Depends(parameterless)])
|
||||
async def depends(x: int = Depends(dependency)):
|
||||
# test dependency
|
||||
return x
|
||||
|
||||
|
||||
@test_depends.handle()
|
||||
async def depends_cache(y: int = Depends(dependency, use_cache=True)):
|
||||
# test cache
|
||||
return y
|
22
tests/plugins/param/param_event.py
Normal file
22
tests/plugins/param/param_event.py
Normal file
@ -0,0 +1,22 @@
|
||||
from nonebot.adapters import Event, Message
|
||||
from nonebot.params import EventToMe, EventType, EventMessage, EventPlainText
|
||||
|
||||
|
||||
async def event(e: Event) -> Event:
|
||||
return e
|
||||
|
||||
|
||||
async def event_type(t: str = EventType()) -> str:
|
||||
return t
|
||||
|
||||
|
||||
async def event_message(msg: Message = EventMessage()) -> Message:
|
||||
return msg
|
||||
|
||||
|
||||
async def event_plain_text(text: str = EventPlainText()) -> str:
|
||||
return text
|
||||
|
||||
|
||||
async def event_to_me(to_me: bool = EventToMe()) -> bool:
|
||||
return to_me
|
15
tests/plugins/param/param_matcher.py
Normal file
15
tests/plugins/param/param_matcher.py
Normal file
@ -0,0 +1,15 @@
|
||||
from nonebot.adapters import Event
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.params import Received, LastReceived
|
||||
|
||||
|
||||
async def matcher(m: Matcher) -> Matcher:
|
||||
return m
|
||||
|
||||
|
||||
async def receive(e: Event = Received("test")) -> Event:
|
||||
return e
|
||||
|
||||
|
||||
async def last_receive(e: Event = LastReceived()) -> Event:
|
||||
return e
|
55
tests/plugins/param/param_state.py
Normal file
55
tests/plugins/param/param_state.py
Normal file
@ -0,0 +1,55 @@
|
||||
from typing import List, Tuple
|
||||
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters import Message
|
||||
from nonebot.params import (
|
||||
State,
|
||||
Command,
|
||||
RegexDict,
|
||||
CommandArg,
|
||||
RawCommand,
|
||||
RegexGroup,
|
||||
RegexMatched,
|
||||
ShellCommandArgs,
|
||||
ShellCommandArgv,
|
||||
)
|
||||
|
||||
|
||||
async def state(x: T_State = State()) -> T_State:
|
||||
return x
|
||||
|
||||
|
||||
async def command(cmd: Tuple[str, ...] = Command()) -> Tuple[str, ...]:
|
||||
return cmd
|
||||
|
||||
|
||||
async def raw_command(raw_cmd: str = RawCommand()) -> str:
|
||||
return raw_cmd
|
||||
|
||||
|
||||
async def command_arg(cmd_arg: Message = CommandArg()) -> Message:
|
||||
return cmd_arg
|
||||
|
||||
|
||||
async def shell_command_args(
|
||||
shell_command_args: dict = ShellCommandArgs(),
|
||||
) -> dict:
|
||||
return shell_command_args
|
||||
|
||||
|
||||
async def shell_command_argv(
|
||||
shell_command_argv: List[str] = ShellCommandArgv(),
|
||||
) -> List[str]:
|
||||
return shell_command_argv
|
||||
|
||||
|
||||
async def regex_dict(regex_dict: dict = RegexDict()) -> dict:
|
||||
return regex_dict
|
||||
|
||||
|
||||
async def regex_group(regex_group: Tuple = RegexGroup()) -> Tuple:
|
||||
return regex_group
|
||||
|
||||
|
||||
async def regex_matched(regex_matched: str = RegexMatched()) -> str:
|
||||
return regex_matched
|
Reference in New Issue
Block a user