diff --git a/tests/plugins/param/param_depend.py b/tests/plugins/param/param_depend.py index fc13c415..5e4c438b 100644 --- a/tests/plugins/param/param_depend.py +++ b/tests/plugins/param/param_depend.py @@ -1,5 +1,6 @@ +from dataclasses import dataclass + from nonebot import on_message -from nonebot.adapters import Event from nonebot.params import Depends test_depends = on_message() @@ -17,6 +18,20 @@ def parameterless(): runned.append(1) +def gen_sync(): + yield 1 + + +async def gen_async(): + yield 2 + + +@dataclass +class ClassDependency: + x: int = Depends(gen_sync) + y: int = Depends(gen_async) + + # test parameterless @test_depends.handle(parameterless=[Depends(parameterless)]) async def depends(x: int = Depends(dependency)): @@ -28,3 +43,7 @@ async def depends(x: int = Depends(dependency)): async def depends_cache(y: int = Depends(dependency, use_cache=True)): # test cache return y + + +async def class_depend(c: ClassDependency = Depends()): + return c diff --git a/tests/test_param.py b/tests/test_param.py index 55725385..c338091e 100644 --- a/tests/test_param.py +++ b/tests/test_param.py @@ -8,7 +8,13 @@ from utils import load_plugin, make_fake_event, make_fake_message async def test_depend(app: App, load_plugin): from nonebot.params import DependParam - from plugins.param.param_depend import runned, depends, test_depends + from plugins.param.param_depend import ( + ClassDependency, + runned, + depends, + class_depend, + test_depends, + ) async with app.test_dependent(depends, allow_types=[DependParam]) as ctx: ctx.should_return(1) @@ -24,6 +30,9 @@ async def test_depend(app: App, load_plugin): assert len(runned) == 2 and runned[0] == runned[1] == 1 + async with app.test_dependent(class_depend, allow_types=[DependParam]) as ctx: + ctx.should_return(ClassDependency(x=1, y=2)) + @pytest.mark.asyncio async def test_bot(app: App, load_plugin):