|
41 | 41 | else: |
42 | 42 | from pathlib import Path |
43 | 43 |
|
| 44 | + no_index_entry = sphinx.version_info >= (8, 2) |
| 45 | + no_index = 'noindex' if sphinx.version_info < (7, 2) else 'no-index' |
| 46 | + |
44 | 47 |
|
45 | 48 | # Configuration file content for testing. |
46 | 49 | CONF_PY = """\ |
@@ -97,6 +100,12 @@ def not_a_trait(self): |
97 | 100 | """ |
98 | 101 |
|
99 | 102 |
|
| 103 | +class MySubClass(MyTestClass): |
| 104 | + |
| 105 | + #: A new attribute. |
| 106 | + foo = Bool(True) |
| 107 | + |
| 108 | + |
100 | 109 | @requires_sphinx |
101 | 110 | class TestTraitDocumenter(unittest.TestCase): |
102 | 111 | """ Tests for the trait documenter. """ |
@@ -128,7 +137,6 @@ def test_get_definition_tokens(self): |
128 | 137 | self.assertEqual(src.rstrip(), string) |
129 | 138 |
|
130 | 139 | def test_add_line(self): |
131 | | - |
132 | 140 | mocked_directive = mock.MagicMock() |
133 | 141 |
|
134 | 142 | documenter = TraitDocumenter(mocked_directive, "test", " ") |
@@ -209,6 +217,79 @@ def test_can_document_member(self): |
209 | 217 | ) |
210 | 218 | ) |
211 | 219 |
|
| 220 | + def test_class(self): |
| 221 | + # given |
| 222 | + documenter = TraitDocumenter(mock.Mock(), 'test') |
| 223 | + documenter.parent = MyTestClass |
| 224 | + documenter.object_name = 'bar' |
| 225 | + documenter.modname = 'traits.util.tests.test_trait_documenter' |
| 226 | + documenter.get_sourcename = mock.Mock(return_value='<autodoc>') |
| 227 | + documenter.objpath = ['MyTestClass', 'bar'] |
| 228 | + documenter.add_line = mock.Mock() |
| 229 | + |
| 230 | + # when |
| 231 | + documenter.add_directive_header('') |
| 232 | + |
| 233 | + # then |
| 234 | + self.assertEqual(documenter.directive.warn.call_args_list, []) |
| 235 | + expected = [ |
| 236 | + ('.. py:attribute:: MyTestClass.bar', '<autodoc>'), |
| 237 | + (f' :{no_index}:', '<autodoc>'), |
| 238 | + (' :module: traits.util.tests.test_trait_documenter', '<autodoc>'), # noqa |
| 239 | + (' :annotation: = Int(42, desc=""" First line …', '<autodoc>')] # noqa |
| 240 | + if no_index_entry: |
| 241 | + expected.insert(2, (' :no-index-entry:', '<autodoc>')) |
| 242 | + calls = documenter.add_line.call_args_list |
| 243 | + for index, line in enumerate(expected): |
| 244 | + self.assertEqual(calls[index][0], line) |
| 245 | + |
| 246 | + def test_subclass(self): |
| 247 | + # given |
| 248 | + documenter = TraitDocumenter(mock.Mock(), 'test') |
| 249 | + documenter.object_name = 'bar' |
| 250 | + documenter.objpath = ['MySubClass', 'bar'] |
| 251 | + documenter.parent = MySubClass |
| 252 | + documenter.modname = 'traits.util.tests.test_trait_documenter' |
| 253 | + documenter.get_sourcename = mock.Mock(return_value='<autodoc>') |
| 254 | + documenter.add_line = mock.Mock() |
| 255 | + |
| 256 | + # when |
| 257 | + documenter.add_directive_header('') |
| 258 | + |
| 259 | + # then |
| 260 | + self.assertEqual(documenter.directive.warn.call_args_list, []) |
| 261 | + expected = [ |
| 262 | + ('.. py:attribute:: MySubClass.bar', '<autodoc>'), |
| 263 | + (f' :{no_index}:', '<autodoc>'), |
| 264 | + (' :module: traits.util.tests.test_trait_documenter', '<autodoc>'), # noqa |
| 265 | + (' :annotation: = Int(42, desc=""" First line …', '<autodoc>')] # noqa |
| 266 | + if no_index_entry: |
| 267 | + expected.insert(2, (' :no-index-entry:', '<autodoc>')) |
| 268 | + calls = documenter.add_line.call_args_list |
| 269 | + for index, line in enumerate(expected): |
| 270 | + self.assertEqual(calls[index][0], line) |
| 271 | + |
| 272 | + # given |
| 273 | + documenter.object_name = 'foo' |
| 274 | + documenter.objpath = ['MySubClass', 'foo'] |
| 275 | + documenter.add_line = mock.Mock() |
| 276 | + |
| 277 | + # when |
| 278 | + documenter.add_directive_header('') |
| 279 | + |
| 280 | + # then |
| 281 | + self.assertEqual(documenter.directive.warn.call_args_list, []) |
| 282 | + expected = [ |
| 283 | + ('.. py:attribute:: MySubClass.foo', '<autodoc>'), |
| 284 | + (f' :{no_index}:', '<autodoc>'), |
| 285 | + (' :module: traits.util.tests.test_trait_documenter', '<autodoc>'), # noqa |
| 286 | + (' :annotation: = Bool(True)', '<autodoc>')] # noqa |
| 287 | + if no_index_entry: |
| 288 | + expected.insert(2, (' :no-index-entry:', '<autodoc>')) |
| 289 | + calls = documenter.add_line.call_args_list |
| 290 | + for index, line in enumerate(expected): |
| 291 | + self.assertEqual(calls[index][0], line) |
| 292 | + |
212 | 293 | @contextlib.contextmanager |
213 | 294 | def create_directive(self): |
214 | 295 | """ |
|
0 commit comments