File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16,3 +16,46 @@ async def agen():
1616
1717 with pytest .raises (StopAsyncIteration ):
1818 await g .__anext__ ()
19+
20+
21+ async def test_generator_closes_on_exception_in_context () -> None :
22+ closed = False
23+
24+ @enapter .async_ .generator
25+ async def agen ():
26+ nonlocal closed
27+ try :
28+ yield 1
29+ yield 2 # pragma: no cover
30+ finally :
31+ closed = True
32+
33+ with pytest .raises (ValueError , match = "Test error" ):
34+ async with agen () as g :
35+ assert await g .__anext__ () == 1
36+ raise ValueError ("Test error" )
37+
38+ assert closed
39+
40+
41+ async def test_generator_raises_exception () -> None :
42+ @enapter .async_ .generator
43+ async def agen ():
44+ yield 1
45+ raise ValueError ("Generator error" )
46+
47+ async with agen () as g :
48+ assert await g .__anext__ () == 1
49+ with pytest .raises (ValueError , match = "Generator error" ):
50+ await g .__anext__ ()
51+
52+
53+ async def test_generator_with_args () -> None :
54+ @enapter .async_ .generator
55+ async def agen (a : int , b : int = 0 ):
56+ yield a
57+ yield b
58+
59+ async with agen (10 , b = 20 ) as g :
60+ assert await g .__anext__ () == 10
61+ assert await g .__anext__ () == 20
You can’t perform that action at this time.
0 commit comments