Add tests for between() and cut_lines() and fix them.

Also fix a bug in the application interface.
This commit is contained in:
Georg Brandl 2008-08-04 20:16:18 +00:00
parent 00b30f6f65
commit 03b12e5d85
3 changed files with 48 additions and 3 deletions

View File

@ -159,7 +159,7 @@ class Sphinx(object):
return listener_id return listener_id
def disconnect(self, listener_id): def disconnect(self, listener_id):
for event in self._listeners: for event in self._listeners.itervalues():
event.pop(listener_id, None) event.pop(listener_id, None)
def emit(self, event, *args): def emit(self, event, *args):

View File

@ -111,7 +111,13 @@ def cut_lines(pre, post=0, what=None):
return return
del lines[:pre] del lines[:pre]
if post: if post:
# remove one trailing blank line.
if lines and not lines[-1]:
lines.pop(-1)
del lines[-post:] del lines[-post:]
# make sure there is a blank line at the end
if lines and lines[-1]:
lines.append('')
return process return process
def between(marker, what=None, keepempty=False): def between(marker, what=None, keepempty=False):
@ -141,6 +147,9 @@ def between(marker, what=None, keepempty=False):
deleted += 1 deleted += 1
if not lines and not keepempty: if not lines and not keepempty:
lines[:] = orig_lines lines[:] = orig_lines
# make sure there is a blank line at the end
if lines and lines[-1]:
lines.append('')
return process return process
@ -340,10 +349,14 @@ class RstGenerator(object):
if what == 'class': if what == 'class':
# for classes, the relevant signature is the __init__ method's # for classes, the relevant signature is the __init__ method's
obj = getattr(obj, '__init__', None) obj = getattr(obj, '__init__', None)
# classes without __init__ method? # classes without __init__ method, default __init__ or
# __init__ written in C?
if obj is None or obj is object.__init__ or not \ if obj is None or obj is object.__init__ or not \
(inspect.ismethod(obj) or inspect.isfunction(obj)): (inspect.ismethod(obj) or inspect.isfunction(obj)):
getargs = False getargs = False
elif inspect.isbuiltin(obj) or inspect.ismethoddescriptor(obj):
# can never get arguments of a C function or method
getargs = False
if getargs: if getargs:
argspec = inspect.getargspec(obj) argspec = inspect.getargspec(obj)
if what in ('class', 'method') and argspec[0] and \ if what in ('class', 'method') and argspec[0] and \

View File

@ -14,7 +14,7 @@ from util import *
from docutils.statemachine import ViewList from docutils.statemachine import ViewList
from sphinx.ext.autodoc import RstGenerator from sphinx.ext.autodoc import RstGenerator, cut_lines, between
def setup_module(): def setup_module():
@ -222,6 +222,30 @@ def test_get_doc():
assert getdocl('class', 'bar', D) == ['Init docstring', '', '42'] assert getdocl('class', 'bar', D) == ['Init docstring', '', '42']
def test_docstring_processing_functions():
lid = app.connect('autodoc-process-docstring', cut_lines(1, 1, ['function']))
def f():
"""
first line
second line
third line
"""
assert list(gen.get_doc('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 list(gen.get_doc('function', 'f', f)) == ['second line', '']
app.disconnect(lid)
def test_generate(): def test_generate():
def assert_warns(warn_str, *args): def assert_warns(warn_str, *args):
gen.generate(*args) gen.generate(*args)
@ -246,6 +270,7 @@ def test_generate():
gen.generate(*args) gen.generate(*args)
assert len(gen.warnings) == 0, gen.warnings assert len(gen.warnings) == 0, gen.warnings
assert item in gen.result assert item in gen.result
print '\n'.join(gen.result)
del gen.result[:] del gen.result[:]
# no module found? # no module found?
@ -303,6 +328,10 @@ def test_generate():
assert_result_contains(' :noindex:', 'module', 'test_autodoc', [], None) assert_result_contains(' :noindex:', 'module', 'test_autodoc', [], None)
assert_result_contains(' :noindex:', 'class', 'Base', [], 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)
# --- generate fodder ------------ # --- generate fodder ------------
@ -329,3 +358,6 @@ class Class(Base):
@property @property
def prop(self): def prop(self):
"""Property.""" """Property."""
class CustomDict(dict):
"""Docstring."""