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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
# -*- coding: utf-8 -*-
"""
test_autodoc
~~~~~~~~~~~~
Test the autodoc extension. This tests mainly the RstGenerator; the auto
directives are tested in a test source file translated by test_build.
:copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from util import *
from docutils.statemachine import ViewList
from sphinx.ext.autodoc import RstGenerator, cut_lines, between
def setup_module():
global app, lid, options, gen
app = TestApp()
app.builder.env.app = app
app.connect('autodoc-process-docstring', process_docstring)
app.connect('autodoc-process-signature', process_signature)
app.connect('autodoc-skip-member', skip_member)
options = Struct(
inherited_members = False,
undoc_members = False,
show_inheritance = False,
noindex = False,
synopsis = '',
platform = '',
deprecated = False,
)
gen = TestGenerator(options, app)
def teardown_module():
app.cleanup()
class TestGenerator(RstGenerator):
"""Generator that handles warnings without a reporter."""
def __init__(self, options, app):
self.options = options
self.env = app.builder.env
self.lineno = 42
self.filename_set = set()
self.warnings = []
self.result = ViewList()
def warn(self, msg):
self.warnings.append(msg)
processed_docstrings = []
def process_docstring(app, what, name, obj, options, lines):
processed_docstrings.append((what, name))
if name == 'bar':
lines.extend(['42', ''])
processed_signatures = []
def process_signature(app, what, name, obj, options, args, retann):
processed_signatures.append((what, name))
if name == 'bar':
return '42', None
def skip_member(app, what, name, obj, skip, options):
if name.startswith('_'):
return True
if name == 'skipmeth':
return True
def test_resolve_name():
# for modules
assert gen.resolve_name('module', 'test_autodoc') == \
('test_autodoc', [], None, None)
assert gen.resolve_name('module', 'test.test_autodoc') == \
('test.test_autodoc', [], None, None)
assert gen.resolve_name('module', 'test(arg)') == \
('test', [], None, None)
assert 'ignoring signature arguments' in gen.warnings[0]
del gen.warnings[:]
# for functions/classes
assert gen.resolve_name('function', 'util.raises') == \
('util', ['raises'], None, None)
assert gen.resolve_name('function', 'util.raises(exc) -> None') == \
('util', ['raises'], 'exc', 'None')
gen.env.autodoc_current_module = 'util'
assert gen.resolve_name('function', 'raises') == \
('util', ['raises'], None, None)
gen.env.autodoc_current_module = None
gen.env.currmodule = 'util'
assert gen.resolve_name('function', 'raises') == \
('util', ['raises'], None, None)
assert gen.resolve_name('class', 'TestApp') == \
('util', ['TestApp'], None, None)
# for members
gen.env.currmodule = 'foo'
assert gen.resolve_name('method', 'util.TestApp.cleanup') == \
('util', ['TestApp', 'cleanup'], None, None)
gen.env.currmodule = 'util'
gen.env.currclass = 'Foo'
gen.env.autodoc_current_class = 'TestApp'
assert gen.resolve_name('method', 'cleanup') == \
('util', ['TestApp', 'cleanup'], None, None)
assert gen.resolve_name('method', 'TestApp.cleanup') == \
('util', ['TestApp', 'cleanup'], None, None)
# and clean up
gen.env.currmodule = None
gen.env.currclass = None
gen.env.autodoc_current_class = None
def test_format_signature():
# no signatures for modules
assert gen.format_signature('module', 'test', None, None, None) == ''
# test for functions
def f(a, b, c=1, **d):
pass
assert gen.format_signature('function', 'f', f, None, None) == \
'(a, b, c=1, **d)'
assert gen.format_signature('function', 'f', f, 'a, b, c, d', None) == \
'(a, b, c, d)'
assert gen.format_signature('function', 'f', f, None, 'None') == \
'(a, b, c=1, **d) -> None'
# test for classes
class D:
pass
class E(object):
pass
# no signature for classes without __init__
for C in (D, E):
assert gen.format_signature('class', 'D', C, None, None) == ''
class F:
def __init__(self, a, b=None):
pass
class G(F, object):
pass
for C in (F, G):
assert gen.format_signature('class', 'C', C, None, None) == \
'(a, b=None)'
assert gen.format_signature('class', 'C', D, 'a, b', 'X') == '(a, b) -> X'
# test for methods
class H:
def foo1(self, b, *c):
pass
def foo2(b, *c):
pass
assert gen.format_signature('method', 'H.foo', H.foo1, None, None) == \
'(b, *c)'
assert gen.format_signature('method', 'H.foo', H.foo1, 'a', None) == \
'(a)'
assert gen.format_signature('method', 'H.foo', H.foo2, None, None) == \
'(b, *c)'
# test exception handling
raises(RuntimeError, gen.format_signature,
'function', 'int', int, None, None)
# test processing by event handler
assert gen.format_signature('method', 'bar', H.foo1, None, None) == '42'
def test_get_doc():
def getdocl(*args):
ds = gen.get_doc(*args)
# for testing purposes, concat them and strip the empty line at the end
return sum(ds, [])[:-1]
# objects without docstring
def f():
pass
assert getdocl('function', f) == []
# standard function, diverse docstring styles...
def f():
"""Docstring"""
def g():
"""
Docstring
"""
for func in (f, g):
assert getdocl('function', func) == ['Docstring']
# first line vs. other lines indentation
def f():
"""First line
Other
lines
"""
assert getdocl('function', f) == ['First line', '', 'Other', ' lines']
# charset guessing (this module is encoded in utf-8)
def f():
"""Döcstring"""
assert getdocl('function', f) == [u'Döcstring']
# already-unicode docstrings must be taken literally
def f():
u"""Döcstring"""
assert getdocl('function', f) == [u'Döcstring']
# class docstring: depends on config value which one is taken
class C:
"""Class docstring"""
def __init__(self):
"""Init docstring"""
gen.env.config.autoclass_content = 'class'
assert getdocl('class', C) == ['Class docstring']
gen.env.config.autoclass_content = 'init'
assert getdocl('class', C) == ['Init docstring']
gen.env.config.autoclass_content = 'both'
assert getdocl('class', C) == ['Class docstring', '', 'Init docstring']
class D:
"""Class docstring"""
def __init__(self):
"""Init docstring
Other
lines
"""
# Indentation is normalized for 'both'
assert getdocl('class', D) == ['Class docstring', '', 'Init docstring',
'', 'Other', ' lines']
def test_docstring_processing():
def process(what, name, obj):
return list(gen.process_doc(gen.get_doc(what, obj), what, name, obj))
class E:
def __init__(self):
"""Init docstring"""
# docstring processing by event handler
assert process('class', 'bar', E) == ['Init docstring', '', '42', '']
lid = app.connect('autodoc-process-docstring',
cut_lines(1, 1, ['function']))
def f():
"""
first line
second line
third line
"""
assert process('function', 'f', f) == ['second line', '']
app.disconnect(lid)
lid = app.connect('autodoc-process-docstring', between('---', ['function']))
def f():
"""
first line
---
second line
---
third line
"""
assert process('function', 'f', f) == ['second line', '']
app.disconnect(lid)
def test_generate():
def assert_warns(warn_str, *args):
gen.generate(*args)
assert len(gen.result) == 0, gen.result
assert len(gen.warnings) == 1, gen.warnings
assert warn_str in gen.warnings[0], gen.warnings
del gen.warnings[:]
def assert_works(*args):
gen.generate(*args)
assert gen.result
assert len(gen.warnings) == 0, gen.warnings
del gen.result[:]
def assert_processes(items, *args):
del processed_docstrings[:]
del processed_signatures[:]
assert_works(*args)
assert set(processed_docstrings) | set(processed_signatures) == \
set(items)
def assert_result_contains(item, *args):
gen.generate(*args)
#print '\n'.join(gen.result)
assert len(gen.warnings) == 0, gen.warnings
assert item in gen.result
del gen.result[:]
# no module found?
assert_warns("import for autodocumenting 'foobar'",
'function', 'foobar', None, None)
# importing
assert_warns("import/find module 'test_foobar'",
'module', 'test_foobar', None, None)
# attributes missing
assert_warns("import/find function 'util.foobar'",
'function', 'util.foobar', None, None)
# test auto and given content mixing
gen.env.currmodule = 'test_autodoc'
assert_result_contains(' Function.', 'method', 'Class.meth', [], None)
add_content = ViewList()
add_content.append('Content.', '', 0)
assert_result_contains(' Function.', 'method',
'Class.meth', [], add_content)
assert_result_contains(' Content.', 'method',
'Class.meth', [], add_content)
# test check_module
gen.generate('function', 'raises', None, None, check_module=True)
assert len(gen.result) == 0
# assert that exceptions can be documented
assert_works('exception', 'test_autodoc.CustomEx', ['__all__'], None)
assert_works('exception', 'test_autodoc.CustomEx', [], None)
# test diverse inclusion settings for members
should = [('class', 'test_autodoc.Class')]
assert_processes(should, 'class', 'Class', [], None)
should.extend([('method', 'test_autodoc.Class.meth')])
assert_processes(should, 'class', 'Class', ['meth'], None)
should.extend([('attribute', 'test_autodoc.Class.prop'),
('attribute', 'test_autodoc.Class.attr'),
('attribute', 'test_autodoc.Class.docattr'),
('attribute', 'test_autodoc.Class.udocattr')])
assert_processes(should, 'class', 'Class', ['__all__'], None)
options.undoc_members = True
should.append(('method', 'test_autodoc.Class.undocmeth'))
assert_processes(should, 'class', 'Class', ['__all__'], None)
options.inherited_members = True
should.append(('method', 'test_autodoc.Class.inheritedmeth'))
assert_processes(should, 'class', 'Class', ['__all__'], None)
# test module flags
assert_result_contains('.. module:: test_autodoc',
'module', 'test_autodoc', [], None)
options.synopsis = 'Synopsis'
assert_result_contains(' :synopsis: Synopsis',
'module', 'test_autodoc', [], None)
options.deprecated = True
assert_result_contains(' :deprecated:',
'module', 'test_autodoc', [], None)
options.platform = 'Platform'
assert_result_contains(' :platform: Platform',
'module', 'test_autodoc', [], None)
# test if __all__ is respected for modules
assert_result_contains('.. class:: Class',
'module', 'test_autodoc', ['__all__'], None)
try:
assert_result_contains('.. exception:: CustomEx',
'module', 'test_autodoc', ['__all__'], None)
except AssertionError:
pass
else:
assert False, 'documented CustomEx which is not in __all__'
# test noindex flag
options.noindex = True
assert_result_contains(' :noindex:', 'module', 'test_autodoc', [], None)
assert_result_contains(' :noindex:', 'class', 'Base', [], None)
# okay, now let's get serious about mixing Python and C signature stuff
assert_result_contains('.. class:: CustomDict', 'class', 'CustomDict',
['__all__'], None)
# test inner class handling
assert_processes([('class', 'test_autodoc.Outer'),
('class', 'test_autodoc.Outer.Inner'),
('method', 'test_autodoc.Outer.Inner.meth')],
'class', 'Outer', ['__all__'], None)
# test generation for C modules (which have no source file)
gen.env.currmodule = 'time'
assert_processes([('function', 'time.asctime')],
'function', 'asctime', [], None)
assert_processes([('function', 'time.asctime')],
'function', 'asctime', [], None)
# --- generate fodder ------------
__all__ = ['Class']
class CustomEx(Exception):
"""My custom exception."""
def f(self):
"""Exception method."""
class Base(object):
def inheritedmeth(self):
"""Inherited function."""
class Class(Base):
"""Class to document."""
def meth(self):
"""Function."""
def undocmeth(self):
pass
def skipmeth(self):
"""Method that should be skipped."""
pass
# should not be documented
skipattr = 'foo'
#: should be documented -- süß
attr = 'bar'
@property
def prop(self):
"""Property."""
docattr = 'baz'
"""should likewise be documented -- süß"""
udocattr = 'quux'
u"""should be documented as well - süß"""
class CustomDict(dict):
"""Docstring."""
def function(foo, *args, **kwds):
"""
Return spam.
"""
pass
class Outer(object):
"""Foo"""
class Inner(object):
"""Foo"""
def meth(self):
"""Foo"""
# should be documented as an alias
factory = dict
|