| 1 |
"""Test the autodoc extension. This tests mainly for private-members option.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from sphinx.ext.autodoc._shared import _AutodocConfig |
| 8 |
|
| 9 |
from tests.test_ext_autodoc.autodoc_util import do_autodoc |
| 10 |
|
| 11 |
pytestmark = pytest.mark.usefixtures('inject_autodoc_root_into_sys_path') |
| 12 |
|
| 13 |
|
| 14 |
def test_private_field() -> None: |
| 15 |
config = _AutodocConfig(autoclass_content='class') |
| 16 |
options = {'members': None} |
| 17 |
actual = do_autodoc('module', 'target.private', config=config, options=options) |
| 18 |
assert actual == [ |
| 19 |
'', |
| 20 |
'.. py:module:: target.private', |
| 21 |
'', |
| 22 |
'', |
| 23 |
'.. py:data:: _PUBLIC_CONSTANT', |
| 24 |
' :module: target.private', |
| 25 |
' :value: None', |
| 26 |
'', |
| 27 |
' :meta public:', |
| 28 |
'', |
| 29 |
'', |
| 30 |
'.. py:function:: _public_function(name)', |
| 31 |
' :module: target.private', |
| 32 |
'', |
| 33 |
' public_function is a docstring().', |
| 34 |
'', |
| 35 |
' :meta public:', |
| 36 |
'', |
| 37 |
] |
| 38 |
|
| 39 |
|
| 40 |
def test_private_field_and_private_members() -> None: |
| 41 |
config = _AutodocConfig(autoclass_content='class') |
| 42 |
options = { |
| 43 |
'members': None, |
| 44 |
'private-members': None, |
| 45 |
} |
| 46 |
actual = do_autodoc('module', 'target.private', config=config, options=options) |
| 47 |
assert actual == [ |
| 48 |
'', |
| 49 |
'.. py:module:: target.private', |
| 50 |
'', |
| 51 |
'', |
| 52 |
'.. py:data:: PRIVATE_CONSTANT', |
| 53 |
' :module: target.private', |
| 54 |
' :value: None', |
| 55 |
'', |
| 56 |
' :meta private:', |
| 57 |
'', |
| 58 |
'', |
| 59 |
'.. py:data:: _PUBLIC_CONSTANT', |
| 60 |
' :module: target.private', |
| 61 |
' :value: None', |
| 62 |
'', |
| 63 |
' :meta public:', |
| 64 |
'', |
| 65 |
'', |
| 66 |
'.. py:function:: _public_function(name)', |
| 67 |
' :module: target.private', |
| 68 |
'', |
| 69 |
' public_function is a docstring().', |
| 70 |
'', |
| 71 |
' :meta public:', |
| 72 |
'', |
| 73 |
'', |
| 74 |
'.. py:function:: private_function(name)', |
| 75 |
' :module: target.private', |
| 76 |
'', |
| 77 |
' private_function is a docstring().', |
| 78 |
'', |
| 79 |
' :meta private:', |
| 80 |
'', |
| 81 |
] |
| 82 |
|
| 83 |
|
| 84 |
def test_private_members() -> None: |
| 85 |
config = _AutodocConfig(autoclass_content='class') |
| 86 |
options = { |
| 87 |
'members': None, |
| 88 |
'private-members': '_PUBLIC_CONSTANT,_public_function', |
| 89 |
} |
| 90 |
actual = do_autodoc('module', 'target.private', config=config, options=options) |
| 91 |
assert actual == [ |
| 92 |
'', |
| 93 |
'.. py:module:: target.private', |
| 94 |
'', |
| 95 |
'', |
| 96 |
'.. py:data:: _PUBLIC_CONSTANT', |
| 97 |
' :module: target.private', |
| 98 |
' :value: None', |
| 99 |
'', |
| 100 |
' :meta public:', |
| 101 |
'', |
| 102 |
'', |
| 103 |
'.. py:function:: _public_function(name)', |
| 104 |
' :module: target.private', |
| 105 |
'', |
| 106 |
' public_function is a docstring().', |
| 107 |
'', |
| 108 |
' :meta public:', |
| 109 |
'', |
| 110 |
] |
| 111 |
|
| 112 |
|
| 113 |
def test_private_attributes() -> None: |
| 114 |
config = _AutodocConfig(autoclass_content='class') |
| 115 |
options = {'members': None} |
| 116 |
actual = do_autodoc('class', 'target.private.Foo', config=config, options=options) |
| 117 |
assert actual == [ |
| 118 |
'', |
| 119 |
'.. py:class:: Foo()', |
| 120 |
' :module: target.private', |
| 121 |
'', |
| 122 |
'', |
| 123 |
' .. py:attribute:: Foo._public_attribute', |
| 124 |
' :module: target.private', |
| 125 |
' :value: 47', |
| 126 |
'', |
| 127 |
' A public class attribute whose name starts with an underscore.', |
| 128 |
'', |
| 129 |
' :meta public:', |
| 130 |
'', |
| 131 |
] |
| 132 |
|
| 133 |
|
| 134 |
def test_private_attributes_and_private_members() -> None: |
| 135 |
config = _AutodocConfig(autoclass_content='class') |
| 136 |
options = { |
| 137 |
'members': None, |
| 138 |
'private-members': None, |
| 139 |
} |
| 140 |
actual = do_autodoc('class', 'target.private.Foo', config=config, options=options) |
| 141 |
assert actual == [ |
| 142 |
'', |
| 143 |
'.. py:class:: Foo()', |
| 144 |
' :module: target.private', |
| 145 |
'', |
| 146 |
'', |
| 147 |
' .. py:attribute:: Foo._public_attribute', |
| 148 |
' :module: target.private', |
| 149 |
' :value: 47', |
| 150 |
'', |
| 151 |
' A public class attribute whose name starts with an underscore.', |
| 152 |
'', |
| 153 |
' :meta public:', |
| 154 |
'', |
| 155 |
'', |
| 156 |
' .. py:attribute:: Foo.private_attribute', |
| 157 |
' :module: target.private', |
| 158 |
' :value: 11', |
| 159 |
'', |
| 160 |
' A private class attribute whose name does not start with an underscore.', |
| 161 |
'', |
| 162 |
' :meta private:', |
| 163 |
'', |
| 164 |
] |