add test cases

This commit is contained in:
yanyongyu
2021-12-20 00:28:02 +08:00
parent ca045b2f73
commit c2c3d5ef4b
17 changed files with 432 additions and 55 deletions

View File

@ -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

View 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))

View File

@ -0,0 +1,5 @@
from nonebot.adapters import Bot
async def get_bot(b: Bot):
return b

View 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

View 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

View 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

View 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

View File

@ -79,8 +79,11 @@ async def test_get(monkeypatch: pytest.MonkeyPatch, nonebug_clear):
async def test_load_plugin(load_plugin: Set["Plugin"]):
import nonebot
assert nonebot.get_loaded_plugins() == load_plugin
plugin = nonebot.get_plugin("depends")
loaded_plugins = set(
plugin for plugin in nonebot.get_loaded_plugins() if not plugin.parent_plugin
)
assert loaded_plugins == load_plugin
plugin = nonebot.get_plugin("param_depend")
assert plugin
assert plugin.module_name == "plugins.depends"
assert "plugins.depends" in sys.modules
assert plugin.module_name == "plugins.param.param_depend"
assert "plugins.param.param_depend" in sys.modules

View File

@ -1,23 +1,19 @@
import pytest
from nonebug import App
from utils import load_plugin, make_fake_event
from utils import load_plugin, make_fake_event, make_fake_message
@pytest.mark.asyncio
async def test_depends(app: App, load_plugin):
from nonebot.params import EventParam, DependParam
async def test_depend(app: App, load_plugin):
from nonebot.params import DependParam
from plugins.depends import runned, depends, test_depends
from plugins.param.param_depend import runned, depends, test_depends
async with app.test_dependent(
depends, allow_types=[EventParam, DependParam]
) as ctx:
event = make_fake_event()()
ctx.pass_params(event=event)
ctx.should_return(event)
async with app.test_dependent(depends, allow_types=[DependParam]) as ctx:
ctx.should_return(1)
assert len(runned) == 1 and runned[0] == event
assert len(runned) == 1 and runned[0] == 1
runned.clear()
@ -26,4 +22,181 @@ async def test_depends(app: App, load_plugin):
event_next = make_fake_event()()
ctx.receive_event(bot, event_next)
assert len(runned) == 1 and runned[0] == event_next
assert len(runned) == 2 and runned[0] == runned[1] == 1
@pytest.mark.asyncio
async def test_bot(app: App, load_plugin):
from nonebot.params import BotParam
from plugins.param.param_bot import get_bot
async with app.test_dependent(get_bot, allow_types=[BotParam]) as ctx:
bot = ctx.create_bot()
ctx.pass_params(bot=bot)
ctx.should_return(bot)
@pytest.mark.asyncio
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())
@pytest.mark.asyncio
async def test_state(app: App, load_plugin):
from nonebot.params import StateParam, DependParam
from nonebot.consts import (
CMD_KEY,
PREFIX_KEY,
REGEX_DICT,
SHELL_ARGS,
SHELL_ARGV,
CMD_ARG_KEY,
RAW_CMD_KEY,
REGEX_GROUP,
REGEX_MATCHED,
)
from plugins.param.param_state import (
state,
command,
regex_dict,
command_arg,
raw_command,
regex_group,
regex_matched,
shell_command_args,
shell_command_argv,
)
fake_message = make_fake_message()("text")
fake_state = {
PREFIX_KEY: {CMD_KEY: ("cmd",), RAW_CMD_KEY: "/cmd", CMD_ARG_KEY: fake_message},
SHELL_ARGV: ["-h"],
SHELL_ARGS: {"help": True},
REGEX_MATCHED: "[cq:test,arg=value]",
REGEX_GROUP: ("test", "arg=value"),
REGEX_DICT: {"type": "test", "arg": "value"},
}
async with app.test_dependent(state, allow_types=[StateParam]) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state)
async with app.test_dependent(
command, allow_types=[StateParam, DependParam]
) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state[PREFIX_KEY][CMD_KEY])
async with app.test_dependent(
raw_command, allow_types=[StateParam, DependParam]
) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state[PREFIX_KEY][RAW_CMD_KEY])
async with app.test_dependent(
command_arg, allow_types=[StateParam, DependParam]
) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state[PREFIX_KEY][CMD_ARG_KEY])
async with app.test_dependent(
shell_command_argv, allow_types=[StateParam, DependParam]
) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state[SHELL_ARGV])
async with app.test_dependent(
shell_command_args, allow_types=[StateParam, DependParam]
) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state[SHELL_ARGS])
async with app.test_dependent(
regex_matched, allow_types=[StateParam, DependParam]
) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state[REGEX_MATCHED])
async with app.test_dependent(
regex_group, allow_types=[StateParam, DependParam]
) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state[REGEX_GROUP])
async with app.test_dependent(
regex_dict, allow_types=[StateParam, DependParam]
) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state[REGEX_DICT])
@pytest.mark.asyncio
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(None, 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)

View File

@ -9,6 +9,36 @@ if TYPE_CHECKING:
from nonebot.adapters import Event, Message
def make_fake_message() -> Type["Message"]:
from nonebot.adapters import Message, MessageSegment
class FakeMessageSegment(MessageSegment):
@classmethod
def get_message_class(cls):
return FakeMessage
def __str__(self) -> str:
return self.data["text"]
@classmethod
def text(cls, text: str):
return cls("text", {"text": text})
def is_text(self) -> bool:
return True
class FakeMessage(Message):
@classmethod
def get_segment_class(cls):
return FakeMessageSegment
@staticmethod
def _construct(msg: str):
yield FakeMessageSegment.text(msg)
return FakeMessage
def make_fake_event(
_type: str = "message",
_name: str = "test",