-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathasync_doctest.py
50 lines (34 loc) · 1.21 KB
/
async_doctest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from textwrap import indent
from typing import Any
from sphinx.application import Sphinx
from sphinx.ext.doctest import DocTestBuilder, setup as doctest_setup
from doctest import DocTest, DocTestRunner
test_template = """
import asyncio as __asyncio
async def __run():
{test}
globals().update(locals())
loop = __asyncio.get_event_loop()
loop.run_until_complete(__run())
"""
class TestRunnerWrapper:
def __init__(self, runner: DocTestRunner):
self._runner = runner
def __getattr__(self, name: str) -> Any:
return getattr(self._runner, name)
def run(self, test: DocTest, *args: Any, **kwargs: Any) -> Any:
for ex in test.examples:
ex.source = test_template.format(test=indent(ex.source, " ").strip())
return self._runner.run(test, *args, **kwargs)
class AsyncDoctestBuilder(DocTestBuilder):
@property
def test_runner(self) -> DocTestRunner:
return self._test_runner
@test_runner.setter
def test_runner(self, value: DocTestRunner) -> None:
self._test_runner = TestRunnerWrapper(value)
return None
def setup(app: Sphinx) -> None:
doctest_setup(app)
app.add_builder(AsyncDoctestBuilder, override=True)
return None