| 1 |
"""Test the autodoc extension. |
| 2 |
|
| 3 |
This tests mainly the Documenters; the auto directives are tested in a test |
| 4 |
source file translated by test_build. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from tests.test_ext_autodoc.autodoc_util import do_autodoc |
| 12 |
|
| 13 |
pytestmark = pytest.mark.usefixtures('inject_autodoc_root_into_sys_path') |
| 14 |
|
| 15 |
|
| 16 |
def test_classes() -> None: |
| 17 |
actual = do_autodoc('function', 'target.classes.Foo') |
| 18 |
assert actual == [ |
| 19 |
'', |
| 20 |
'.. py:function:: Foo()', |
| 21 |
' :module: target.classes', |
| 22 |
'', |
| 23 |
] |
| 24 |
|
| 25 |
actual = do_autodoc('function', 'target.classes.Bar') |
| 26 |
assert actual == [ |
| 27 |
'', |
| 28 |
'.. py:function:: Bar(x, y)', |
| 29 |
' :module: target.classes', |
| 30 |
'', |
| 31 |
] |
| 32 |
|
| 33 |
actual = do_autodoc('function', 'target.classes.Baz') |
| 34 |
assert actual == [ |
| 35 |
'', |
| 36 |
'.. py:function:: Baz(x, y)', |
| 37 |
' :module: target.classes', |
| 38 |
'', |
| 39 |
] |
| 40 |
|
| 41 |
actual = do_autodoc('function', 'target.classes.Qux') |
| 42 |
assert actual == [ |
| 43 |
'', |
| 44 |
'.. py:function:: Qux(foo, bar)', |
| 45 |
' :module: target.classes', |
| 46 |
'', |
| 47 |
] |
| 48 |
|
| 49 |
|
| 50 |
def test_callable() -> None: |
| 51 |
actual = do_autodoc('function', 'target.callable.function') |
| 52 |
assert actual == [ |
| 53 |
'', |
| 54 |
'.. py:function:: function(arg1, arg2, **kwargs)', |
| 55 |
' :module: target.callable', |
| 56 |
'', |
| 57 |
' A callable object that behaves like a function.', |
| 58 |
'', |
| 59 |
] |
| 60 |
|
| 61 |
|
| 62 |
def test_method() -> None: |
| 63 |
actual = do_autodoc('function', 'target.callable.method') |
| 64 |
assert actual == [ |
| 65 |
'', |
| 66 |
'.. py:function:: method(arg1, arg2)', |
| 67 |
' :module: target.callable', |
| 68 |
'', |
| 69 |
' docstring of Callable.method().', |
| 70 |
'', |
| 71 |
] |
| 72 |
|
| 73 |
|
| 74 |
def test_builtin_function() -> None: |
| 75 |
actual = do_autodoc('function', 'os.umask') |
| 76 |
assert actual == [ |
| 77 |
'', |
| 78 |
'.. py:function:: umask(mask, /)', |
| 79 |
' :module: os', |
| 80 |
'', |
| 81 |
' Set the current numeric umask and return the previous umask.', |
| 82 |
'', |
| 83 |
] |
| 84 |
|
| 85 |
|
| 86 |
def test_methoddescriptor() -> None: |
| 87 |
actual = do_autodoc('function', 'builtins.int.__add__') |
| 88 |
assert actual == [ |
| 89 |
'', |
| 90 |
'.. py:function:: __add__(self, value, /)', |
| 91 |
' :module: builtins.int', |
| 92 |
'', |
| 93 |
' Return self+value.', |
| 94 |
'', |
| 95 |
] |
| 96 |
|
| 97 |
|
| 98 |
def test_decorated() -> None: |
| 99 |
actual = do_autodoc('function', 'target.decorator.foo') |
| 100 |
assert actual == [ |
| 101 |
'', |
| 102 |
'.. py:function:: foo(name=None, age=None)', |
| 103 |
' :module: target.decorator', |
| 104 |
'', |
| 105 |
] |
| 106 |
|
| 107 |
|
| 108 |
def test_singledispatch() -> None: |
| 109 |
actual = do_autodoc('function', 'target.singledispatch.func') |
| 110 |
assert actual == [ |
| 111 |
'', |
| 112 |
'.. py:function:: func(arg, kwarg=None)', |
| 113 |
' func(arg: float, kwarg=None)', |
| 114 |
' func(arg: int, kwarg=None)', |
| 115 |
' func(arg: str, kwarg=None)', |
| 116 |
' func(arg: dict, kwarg=None)', |
| 117 |
' :module: target.singledispatch', |
| 118 |
'', |
| 119 |
' A function for general use.', |
| 120 |
'', |
| 121 |
] |
| 122 |
|
| 123 |
|
| 124 |
def test_cfunction() -> None: |
| 125 |
actual = do_autodoc('function', 'time.asctime') |
| 126 |
assert actual == [ |
| 127 |
'', |
| 128 |
'.. py:function:: asctime([tuple]) -> string', |
| 129 |
' :module: time', |
| 130 |
'', |
| 131 |
" Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.", |
| 132 |
' When the time tuple is not present, current time as returned by localtime()', |
| 133 |
' is used.', |
| 134 |
'', |
| 135 |
] |
| 136 |
|
| 137 |
|
| 138 |
def test_wrapped_function() -> None: |
| 139 |
actual = do_autodoc('function', 'target.wrappedfunction.slow_function') |
| 140 |
assert actual == [ |
| 141 |
'', |
| 142 |
'.. py:function:: slow_function(message, timeout)', |
| 143 |
' :module: target.wrappedfunction', |
| 144 |
'', |
| 145 |
' This function is slow.', |
| 146 |
'', |
| 147 |
] |
| 148 |
|
| 149 |
|
| 150 |
def test_wrapped_function_contextmanager() -> None: |
| 151 |
actual = do_autodoc('function', 'target.wrappedfunction.feeling_good') |
| 152 |
assert actual == [ |
| 153 |
'', |
| 154 |
'.. py:function:: feeling_good(x: int, y: int) -> ~typing.Generator', |
| 155 |
' :module: target.wrappedfunction', |
| 156 |
'', |
| 157 |
" You'll feel better in this context!", |
| 158 |
'', |
| 159 |
] |
| 160 |
|
| 161 |
|
| 162 |
def test_coroutine() -> None: |
| 163 |
actual = do_autodoc('function', 'target.functions.coroutinefunc') |
| 164 |
assert actual == [ |
| 165 |
'', |
| 166 |
'.. py:function:: coroutinefunc()', |
| 167 |
' :module: target.functions', |
| 168 |
' :async:', |
| 169 |
'', |
| 170 |
] |
| 171 |
|
| 172 |
|
| 173 |
def test_synchronized_coroutine() -> None: |
| 174 |
actual = do_autodoc('function', 'target.coroutine.sync_func') |
| 175 |
assert actual == [ |
| 176 |
'', |
| 177 |
'.. py:function:: sync_func()', |
| 178 |
' :module: target.coroutine', |
| 179 |
'', |
| 180 |
] |
| 181 |
|
| 182 |
|
| 183 |
def test_async_generator() -> None: |
| 184 |
actual = do_autodoc('function', 'target.functions.asyncgenerator') |
| 185 |
assert actual == [ |
| 186 |
'', |
| 187 |
'.. py:function:: asyncgenerator()', |
| 188 |
' :module: target.functions', |
| 189 |
' :async:', |
| 190 |
'', |
| 191 |
] |
| 192 |
|
| 193 |
|
| 194 |
def test_slice_function_arg() -> None: |
| 195 |
actual = do_autodoc('function', 'target.functions.slice_arg_func') |
| 196 |
assert actual == [ |
| 197 |
'', |
| 198 |
'.. py:function:: slice_arg_func(arg: float64[:, :])', |
| 199 |
' :module: target.functions', |
| 200 |
'', |
| 201 |
] |