blob: 9e62f2859fe390ce82703a32ac53cd78c29ddac9 [file] [log] [blame]
Benjamin Peterson3a7305e2008-05-22 23:09:26 +00001import os
Éric Araujo9a528302011-07-29 17:34:35 +02002import sys
Georg Brandl8632cc22008-05-18 16:32:48 +00003import difflib
Éric Araujo9a528302011-07-29 17:34:35 +02004import __builtin__
Georg Brandl8632cc22008-05-18 16:32:48 +00005import re
6import pydoc
Antoine Pitrouf41ffed2013-05-19 15:44:54 +02007import contextlib
Georg Brandlb7e419e2008-05-20 08:10:03 +00008import inspect
Ezio Melottibdfa2e62011-04-28 07:59:33 +03009import keyword
Antoine Pitrouf41ffed2013-05-19 15:44:54 +020010import pkgutil
Georg Brandl8632cc22008-05-18 16:32:48 +000011import unittest
Brian Curtinaeb2e822010-03-31 03:10:21 +000012import xml.etree
Georg Brandl8632cc22008-05-18 16:32:48 +000013import test.test_support
Raymond Hettinger9aa5a342011-03-25 16:00:13 -070014from collections import namedtuple
Ned Deily1a96f8d2011-10-06 14:17:34 -070015from test.script_helper import assert_python_ok
Antoine Pitrou161df942009-10-30 21:41:22 +000016from test.test_support import (
Antoine Pitrouf41ffed2013-05-19 15:44:54 +020017 TESTFN, rmtree, reap_children, captured_stdout, captured_stderr)
Benjamin Petersonf5c38da2008-05-18 20:48:07 +000018
Georg Brandl8632cc22008-05-18 16:32:48 +000019from test import pydoc_mod
20
Serhiy Storchaka72121c62013-01-27 19:45:49 +020021if test.test_support.HAVE_DOCSTRINGS:
22 expected_data_docstrings = (
23 'dictionary for instance variables (if defined)',
24 'list of weak references to the object (if defined)',
25 )
26else:
27 expected_data_docstrings = ('', '')
28
Georg Brandl8632cc22008-05-18 16:32:48 +000029expected_text_pattern = \
30"""
31NAME
32 test.pydoc_mod - This is a test module for test_pydoc
33
34FILE
35 %s
Benjamin Peterson3a7305e2008-05-22 23:09:26 +000036%s
Georg Brandl8632cc22008-05-18 16:32:48 +000037CLASSES
38 __builtin__.object
39 B
40 A
41\x20\x20\x20\x20
42 class A
43 | Hello and goodbye
44 |\x20\x20
45 | Methods defined here:
46 |\x20\x20
47 | __init__()
48 | Wow, I have no function!
49\x20\x20\x20\x20
50 class B(__builtin__.object)
51 | Data descriptors defined here:
52 |\x20\x20
Serhiy Storchaka72121c62013-01-27 19:45:49 +020053 | __dict__%s
Georg Brandl8632cc22008-05-18 16:32:48 +000054 |\x20\x20
Serhiy Storchaka72121c62013-01-27 19:45:49 +020055 | __weakref__%s
Georg Brandl8632cc22008-05-18 16:32:48 +000056 |\x20\x20
57 | ----------------------------------------------------------------------
58 | Data and other attributes defined here:
59 |\x20\x20
60 | NO_MEANING = 'eggs'
61
62FUNCTIONS
63 doc_func()
64 This function solves all of the world's problems:
65 hunger
66 lack of Python
67 war
68\x20\x20\x20\x20
69 nodoc_func()
70
71DATA
72 __author__ = 'Benjamin Peterson'
73 __credits__ = 'Nobody'
Georg Brandl8632cc22008-05-18 16:32:48 +000074 __version__ = '1.2.3.4'
75
76VERSION
77 1.2.3.4
78
79AUTHOR
80 Benjamin Peterson
81
82CREDITS
83 Nobody
84""".strip()
85
Serhiy Storchaka72121c62013-01-27 19:45:49 +020086expected_text_data_docstrings = tuple('\n | ' + s if s else ''
87 for s in expected_data_docstrings)
88
Georg Brandl8632cc22008-05-18 16:32:48 +000089expected_html_pattern = \
90"""
91<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
92<tr bgcolor="#7799ee">
93<td valign=bottom>&nbsp;<br>
94<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="test.html"><font color="#ffffff">test</font></a>.pydoc_mod</strong></big></big> (version 1.2.3.4)</font></td
95><td align=right valign=bottom
Benjamin Peterson3a7305e2008-05-22 23:09:26 +000096><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
Georg Brandl8632cc22008-05-18 16:32:48 +000097 <p><tt>This&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;test_pydoc</tt></p>
98<p>
99<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
100<tr bgcolor="#ee77aa">
101<td colspan=3 valign=bottom>&nbsp;<br>
102<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
103\x20\x20\x20\x20
104<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
105<td width="100%%"><dl>
106<dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a>
107</font></dt><dd>
108<dl>
109<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
110</font></dt></dl>
111</dd>
112<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
113</font></dt></dl>
114 <p>
115<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
116<tr bgcolor="#ffc8d8">
117<td colspan=3 valign=bottom>&nbsp;<br>
118<font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a></font></td></tr>
119\x20\x20\x20\x20
120<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
121<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
122<tr><td>&nbsp;</td>
123<td width="100%%">Methods defined here:<br>
124<dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow,&nbsp;I&nbsp;have&nbsp;no&nbsp;function!</tt></dd></dl>
125
126</td></tr></table> <p>
127<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
128<tr bgcolor="#ffc8d8">
129<td colspan=3 valign=bottom>&nbsp;<br>
130<font color="#000000" face="helvetica, arial"><a name="B">class <strong>B</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
131\x20\x20\x20\x20
132<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
133<td width="100%%">Data descriptors defined here:<br>
134<dl><dt><strong>__dict__</strong></dt>
Serhiy Storchaka72121c62013-01-27 19:45:49 +0200135<dd><tt>%s</tt></dd>
Georg Brandl8632cc22008-05-18 16:32:48 +0000136</dl>
137<dl><dt><strong>__weakref__</strong></dt>
Serhiy Storchaka72121c62013-01-27 19:45:49 +0200138<dd><tt>%s</tt></dd>
Georg Brandl8632cc22008-05-18 16:32:48 +0000139</dl>
140<hr>
141Data and other attributes defined here:<br>
142<dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
143
144</td></tr></table></td></tr></table><p>
145<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
146<tr bgcolor="#eeaa77">
147<td colspan=3 valign=bottom>&nbsp;<br>
148<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
149\x20\x20\x20\x20
150<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
151<td width="100%%"><dl><dt><a name="-doc_func"><strong>doc_func</strong></a>()</dt><dd><tt>This&nbsp;function&nbsp;solves&nbsp;all&nbsp;of&nbsp;the&nbsp;world's&nbsp;problems:<br>
152hunger<br>
153lack&nbsp;of&nbsp;Python<br>
154war</tt></dd></dl>
155 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
156</td></tr></table><p>
157<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
158<tr bgcolor="#55aa55">
159<td colspan=3 valign=bottom>&nbsp;<br>
160<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
161\x20\x20\x20\x20
162<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
163<td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br>
164<strong>__credits__</strong> = 'Nobody'<br>
Georg Brandl8632cc22008-05-18 16:32:48 +0000165<strong>__version__</strong> = '1.2.3.4'</td></tr></table><p>
166<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
167<tr bgcolor="#7799ee">
168<td colspan=3 valign=bottom>&nbsp;<br>
169<font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr>
170\x20\x20\x20\x20
171<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
172<td width="100%%">Benjamin&nbsp;Peterson</td></tr></table><p>
173<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
174<tr bgcolor="#7799ee">
175<td colspan=3 valign=bottom>&nbsp;<br>
176<font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr>
177\x20\x20\x20\x20
178<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
179<td width="100%%">Nobody</td></tr></table>
180""".strip()
181
Serhiy Storchaka72121c62013-01-27 19:45:49 +0200182expected_html_data_docstrings = tuple(s.replace(' ', '&nbsp;')
183 for s in expected_data_docstrings)
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000184
Georg Brandl8632cc22008-05-18 16:32:48 +0000185# output pattern for missing module
186missing_pattern = "no Python documentation found for '%s'"
187
R. David Murrayef087da2009-06-23 18:02:46 +0000188# output pattern for module with bad imports
189badimport_pattern = "problem in %s - <type 'exceptions.ImportError'>: No module named %s"
190
Ned Deily1a96f8d2011-10-06 14:17:34 -0700191def run_pydoc(module_name, *args, **env):
Georg Brandl8632cc22008-05-18 16:32:48 +0000192 """
193 Runs pydoc on the specified module. Returns the stripped
194 output of pydoc.
195 """
Ned Deily1a96f8d2011-10-06 14:17:34 -0700196 args = args + (module_name,)
197 # do not write bytecode files to avoid caching errors
198 rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env)
199 return out.strip()
Georg Brandl8632cc22008-05-18 16:32:48 +0000200
201def get_pydoc_html(module):
202 "Returns pydoc generated output as html"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000203 doc = pydoc.HTMLDoc()
204 output = doc.docmodule(module)
205 loc = doc.getdocloc(pydoc_mod) or ""
206 if loc:
207 loc = "<br><a href=\"" + loc + "\">Module Docs</a>"
208 return output.strip(), loc
Georg Brandl8632cc22008-05-18 16:32:48 +0000209
210def get_pydoc_text(module):
211 "Returns pydoc generated output as text"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000212 doc = pydoc.TextDoc()
213 loc = doc.getdocloc(pydoc_mod) or ""
214 if loc:
215 loc = "\nMODULE DOCS\n " + loc + "\n"
216
217 output = doc.docmodule(module)
Georg Brandl8632cc22008-05-18 16:32:48 +0000218
219 # cleanup the extra text formatting that pydoc preforms
220 patt = re.compile('\b.')
221 output = patt.sub('', output)
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000222 return output.strip(), loc
Georg Brandl8632cc22008-05-18 16:32:48 +0000223
224def print_diffs(text1, text2):
225 "Prints unified diffs for two texts"
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000226 lines1 = text1.splitlines(True)
227 lines2 = text2.splitlines(True)
228 diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
229 tofile='got')
230 print '\n' + ''.join(diffs)
Georg Brandl8632cc22008-05-18 16:32:48 +0000231
Georg Brandl8632cc22008-05-18 16:32:48 +0000232
Antoine Pitrouf41ffed2013-05-19 15:44:54 +0200233class PydocBaseTest(unittest.TestCase):
234
235 def _restricted_walk_packages(self, walk_packages, path=None):
236 """
237 A version of pkgutil.walk_packages() that will restrict itself to
238 a given path.
239 """
240 default_path = path or [os.path.dirname(__file__)]
241 def wrapper(path=None, prefix='', onerror=None):
242 return walk_packages(path or default_path, prefix, onerror)
243 return wrapper
244
245 @contextlib.contextmanager
246 def restrict_walk_packages(self, path=None):
247 walk_packages = pkgutil.walk_packages
248 pkgutil.walk_packages = self._restricted_walk_packages(walk_packages,
249 path)
250 try:
251 yield
252 finally:
253 pkgutil.walk_packages = walk_packages
254
255
256class PydocDocTest(unittest.TestCase):
Georg Brandl8632cc22008-05-18 16:32:48 +0000257
R. David Murrayf28fd242010-02-23 00:24:49 +0000258 @unittest.skipIf(sys.flags.optimize >= 2,
259 "Docstrings are omitted with -O2 and above")
Georg Brandl8632cc22008-05-18 16:32:48 +0000260 def test_html_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000261 result, doc_loc = get_pydoc_html(pydoc_mod)
Georg Brandlb7e419e2008-05-20 08:10:03 +0000262 mod_file = inspect.getabsfile(pydoc_mod)
Amaury Forgeot d'Arc8e8de4a2008-06-10 21:37:15 +0000263 if sys.platform == 'win32':
264 import nturl2path
265 mod_url = nturl2path.pathname2url(mod_file)
266 else:
267 mod_url = mod_file
Serhiy Storchaka72121c62013-01-27 19:45:49 +0200268 expected_html = expected_html_pattern % (
269 (mod_url, mod_file, doc_loc) +
270 expected_html_data_docstrings)
Georg Brandl8632cc22008-05-18 16:32:48 +0000271 if result != expected_html:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000272 print_diffs(expected_html, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000273 self.fail("outputs are not equal, see diff above")
274
R. David Murrayf28fd242010-02-23 00:24:49 +0000275 @unittest.skipIf(sys.flags.optimize >= 2,
276 "Docstrings are omitted with -O2 and above")
Georg Brandl8632cc22008-05-18 16:32:48 +0000277 def test_text_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000278 result, doc_loc = get_pydoc_text(pydoc_mod)
Serhiy Storchaka72121c62013-01-27 19:45:49 +0200279 expected_text = expected_text_pattern % (
280 (inspect.getabsfile(pydoc_mod), doc_loc) +
281 expected_text_data_docstrings)
Georg Brandl8632cc22008-05-18 16:32:48 +0000282 if result != expected_text:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000283 print_diffs(expected_text, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000284 self.fail("outputs are not equal, see diff above")
285
Brian Curtinaeb2e822010-03-31 03:10:21 +0000286 def test_issue8225(self):
287 # Test issue8225 to ensure no doc link appears for xml.etree
288 result, doc_loc = get_pydoc_text(xml.etree)
289 self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link")
290
R David Murrayc313b1d2012-04-23 13:27:11 -0400291 def test_non_str_name(self):
292 # issue14638
293 # Treat illegal (non-str) name like no name
294 class A:
295 __name__ = 42
296 class B:
297 pass
298 adoc = pydoc.render_doc(A())
299 bdoc = pydoc.render_doc(B())
300 self.assertEqual(adoc.replace("A", "B"), bdoc)
301
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000302 def test_not_here(self):
303 missing_module = "test.i_am_not_here"
304 result = run_pydoc(missing_module)
305 expected = missing_pattern % missing_module
306 self.assertEqual(expected, result,
307 "documentation for missing module found")
308
R. David Murrayd67ea7d2009-05-27 20:07:21 +0000309 def test_input_strip(self):
310 missing_module = " test.i_am_not_here "
311 result = run_pydoc(missing_module)
312 expected = missing_pattern % missing_module.strip()
313 self.assertEqual(expected, result,
314 "white space was not stripped from module name "
315 "or other error output mismatch")
316
Ezio Melottie511fc72010-02-16 23:26:09 +0000317 def test_stripid(self):
318 # test with strings, other implementations might have different repr()
319 stripid = pydoc.stripid
320 # strip the id
321 self.assertEqual(stripid('<function stripid at 0x88dcee4>'),
322 '<function stripid>')
323 self.assertEqual(stripid('<function stripid at 0x01F65390>'),
324 '<function stripid>')
325 # nothing to strip, return the same text
326 self.assertEqual(stripid('42'), '42')
327 self.assertEqual(stripid("<type 'exceptions.Exception'>"),
328 "<type 'exceptions.Exception'>")
329
Georg Brandl8632cc22008-05-18 16:32:48 +0000330
Antoine Pitrouf41ffed2013-05-19 15:44:54 +0200331class PydocImportTest(PydocBaseTest):
Ned Deily1a96f8d2011-10-06 14:17:34 -0700332
333 def setUp(self):
334 self.test_dir = os.mkdir(TESTFN)
335 self.addCleanup(rmtree, TESTFN)
336
337 def test_badimport(self):
338 # This tests the fix for issue 5230, where if pydoc found the module
339 # but the module had an internal import error pydoc would report no doc
340 # found.
341 modname = 'testmod_xyzzy'
342 testpairs = (
343 ('i_am_not_here', 'i_am_not_here'),
344 ('test.i_am_not_here_either', 'i_am_not_here_either'),
345 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
346 ('i_am_not_here.{}'.format(modname),
347 'i_am_not_here.{}'.format(modname)),
348 ('test.{}'.format(modname), modname),
349 )
350
351 sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py"
352 for importstring, expectedinmsg in testpairs:
353 with open(sourcefn, 'w') as f:
354 f.write("import {}\n".format(importstring))
355 result = run_pydoc(modname, PYTHONPATH=TESTFN)
356 expected = badimport_pattern % (modname, expectedinmsg)
357 self.assertEqual(expected, result)
358
359 def test_apropos_with_bad_package(self):
360 # Issue 7425 - pydoc -k failed when bad package on path
361 pkgdir = os.path.join(TESTFN, "syntaxerr")
362 os.mkdir(pkgdir)
363 badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py"
364 with open(badsyntax, 'w') as f:
365 f.write("invalid python syntax = $1\n")
Antoine Pitrouf41ffed2013-05-19 15:44:54 +0200366 with self.restrict_walk_packages(path=[TESTFN]):
367 with captured_stdout() as out:
368 with captured_stderr() as err:
369 pydoc.apropos('xyzzy')
370 # No result, no error
371 self.assertEqual(out.getvalue(), '')
372 self.assertEqual(err.getvalue(), '')
373 # The package name is still matched
374 with captured_stdout() as out:
375 with captured_stderr() as err:
376 pydoc.apropos('syntaxerr')
377 self.assertEqual(out.getvalue().strip(), 'syntaxerr')
378 self.assertEqual(err.getvalue(), '')
Ned Deily1a96f8d2011-10-06 14:17:34 -0700379
380 def test_apropos_with_unreadable_dir(self):
381 # Issue 7367 - pydoc -k failed when unreadable dir on path
382 self.unreadable_dir = os.path.join(TESTFN, "unreadable")
383 os.mkdir(self.unreadable_dir, 0)
384 self.addCleanup(os.rmdir, self.unreadable_dir)
385 # Note, on Windows the directory appears to be still
386 # readable so this is not really testing the issue there
Antoine Pitrouf41ffed2013-05-19 15:44:54 +0200387 with self.restrict_walk_packages(path=[TESTFN]):
388 with captured_stdout() as out:
389 with captured_stderr() as err:
390 pydoc.apropos('SOMEKEY')
391 # No result, no error
392 self.assertEqual(out.getvalue(), '')
393 self.assertEqual(err.getvalue(), '')
Ned Deily1a96f8d2011-10-06 14:17:34 -0700394
395
Georg Brandl8632cc22008-05-18 16:32:48 +0000396class TestDescriptions(unittest.TestCase):
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000397
Georg Brandl8632cc22008-05-18 16:32:48 +0000398 def test_module(self):
399 # Check that pydocfodder module can be described
400 from test import pydocfodder
401 doc = pydoc.render_doc(pydocfodder)
Ezio Melottiaa980582010-01-23 23:04:36 +0000402 self.assertIn("pydocfodder", doc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000403
404 def test_classic_class(self):
405 class C: "Classic class"
406 c = C()
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000407 self.assertEqual(pydoc.describe(C), 'class C')
408 self.assertEqual(pydoc.describe(c), 'instance of C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000409 expected = 'instance of C in module %s' % __name__
Ezio Melottiaa980582010-01-23 23:04:36 +0000410 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000411
412 def test_class(self):
413 class C(object): "New-style class"
414 c = C()
415
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000416 self.assertEqual(pydoc.describe(C), 'class C')
417 self.assertEqual(pydoc.describe(c), 'C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000418 expected = 'C in module %s object' % __name__
Ezio Melottiaa980582010-01-23 23:04:36 +0000419 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000420
Raymond Hettinger9aa5a342011-03-25 16:00:13 -0700421 def test_namedtuple_public_underscore(self):
422 NT = namedtuple('NT', ['abc', 'def'], rename=True)
423 with captured_stdout() as help_io:
424 help(NT)
425 helptext = help_io.getvalue()
426 self.assertIn('_1', helptext)
427 self.assertIn('_replace', helptext)
428 self.assertIn('_asdict', helptext)
429
Georg Brandl8632cc22008-05-18 16:32:48 +0000430
Ezio Melottibdfa2e62011-04-28 07:59:33 +0300431class TestHelper(unittest.TestCase):
432 def test_keywords(self):
433 self.assertEqual(sorted(pydoc.Helper.keywords),
434 sorted(keyword.kwlist))
435
Éric Araujo9a528302011-07-29 17:34:35 +0200436 def test_builtin(self):
437 for name in ('str', 'str.translate', '__builtin__.str',
438 '__builtin__.str.translate'):
439 # test low-level function
440 self.assertIsNotNone(pydoc.locate(name))
441 # test high-level function
442 try:
443 pydoc.render_doc(name)
444 except ImportError:
445 self.fail('finding the doc of {!r} failed'.format(o))
446
447 for name in ('not__builtin__', 'strrr', 'strr.translate',
448 'str.trrrranslate', '__builtin__.strrr',
449 '__builtin__.str.trrranslate'):
450 self.assertIsNone(pydoc.locate(name))
451 self.assertRaises(ImportError, pydoc.render_doc, name)
452
Ezio Melottibdfa2e62011-04-28 07:59:33 +0300453
Georg Brandl8632cc22008-05-18 16:32:48 +0000454def test_main():
Ned Deily1a96f8d2011-10-06 14:17:34 -0700455 try:
Antoine Pitrouf41ffed2013-05-19 15:44:54 +0200456 test.test_support.run_unittest(PydocDocTest,
Ned Deily1a96f8d2011-10-06 14:17:34 -0700457 PydocImportTest,
458 TestDescriptions,
459 TestHelper)
460 finally:
461 reap_children()
Georg Brandl8632cc22008-05-18 16:32:48 +0000462
463if __name__ == "__main__":
464 test_main()