mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 03:36:52 +08:00
✅ add param tests
This commit is contained in:
parent
6968d34fc2
commit
0d24a79840
14
tests/plugins/param/param_arg.py
Normal file
14
tests/plugins/param/param_arg.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from nonebot.adapters import Event, Message
|
||||||
|
from nonebot.params import Arg, ArgStr, ArgEvent
|
||||||
|
|
||||||
|
|
||||||
|
async def arg(key: Message = Arg()) -> Message:
|
||||||
|
return key
|
||||||
|
|
||||||
|
|
||||||
|
async def arg_str(key: str = ArgStr()) -> str:
|
||||||
|
return key
|
||||||
|
|
||||||
|
|
||||||
|
async def arg_event(key: Event = ArgEvent()) -> Event:
|
||||||
|
return key
|
2
tests/plugins/param/param_default.py
Normal file
2
tests/plugins/param/param_default.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
async def default(value: int = 1) -> int:
|
||||||
|
return value
|
6
tests/plugins/param/param_exception.py
Normal file
6
tests/plugins/param/param_exception.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from typing import Union
|
||||||
|
|
||||||
|
|
||||||
|
async def exc(e: Exception, x: Union[ValueError, TypeError]) -> Exception:
|
||||||
|
assert e == x
|
||||||
|
return e
|
@ -200,3 +200,50 @@ async def test_matcher(app: App, load_plugin):
|
|||||||
) as ctx:
|
) as ctx:
|
||||||
ctx.pass_params(matcher=fake_matcher)
|
ctx.pass_params(matcher=fake_matcher)
|
||||||
ctx.should_return(event_next)
|
ctx.should_return(event_next)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_arg(app: App, load_plugin):
|
||||||
|
from nonebot.matcher import Matcher
|
||||||
|
from nonebot.params import ArgParam
|
||||||
|
|
||||||
|
from plugins.param.param_arg import arg, arg_str, arg_event
|
||||||
|
|
||||||
|
matcher = Matcher()
|
||||||
|
message = make_fake_message()("text")
|
||||||
|
event = make_fake_event(_message=message)()
|
||||||
|
matcher.set_arg("key", event)
|
||||||
|
|
||||||
|
async with app.test_dependent(arg, allow_types=[ArgParam]) as ctx:
|
||||||
|
ctx.pass_params(matcher=matcher)
|
||||||
|
ctx.should_return(message)
|
||||||
|
|
||||||
|
async with app.test_dependent(arg_str, allow_types=[ArgParam]) as ctx:
|
||||||
|
ctx.pass_params(matcher=matcher)
|
||||||
|
ctx.should_return(str(message))
|
||||||
|
|
||||||
|
async with app.test_dependent(arg_event, allow_types=[ArgParam]) as ctx:
|
||||||
|
ctx.pass_params(matcher=matcher)
|
||||||
|
ctx.should_return(event)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_exception(app: App, load_plugin):
|
||||||
|
from nonebot.params import ExceptionParam
|
||||||
|
|
||||||
|
from plugins.param.param_exception import exc
|
||||||
|
|
||||||
|
exception = ValueError("test")
|
||||||
|
async with app.test_dependent(exc, allow_types=[ExceptionParam]) as ctx:
|
||||||
|
ctx.pass_params(exception=exception)
|
||||||
|
ctx.should_return(exception)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_default(app: App, load_plugin):
|
||||||
|
from nonebot.params import DefaultParam
|
||||||
|
|
||||||
|
from plugins.param.param_default import default
|
||||||
|
|
||||||
|
async with app.test_dependent(default, allow_types=[DefaultParam]) as ctx:
|
||||||
|
ctx.should_return(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user