Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 1 | import os |
Éric Araujo | 9a52830 | 2011-07-29 17:34:35 +0200 | [diff] [blame] | 2 | import sys |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 3 | import difflib |
Éric Araujo | 9a52830 | 2011-07-29 17:34:35 +0200 | [diff] [blame] | 4 | import __builtin__ |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 5 | import re |
| 6 | import pydoc |
Georg Brandl | b7e419e | 2008-05-20 08:10:03 +0000 | [diff] [blame] | 7 | import inspect |
Ezio Melotti | bdfa2e6 | 2011-04-28 07:59:33 +0300 | [diff] [blame] | 8 | import keyword |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 9 | import unittest |
Brian Curtin | aeb2e82 | 2010-03-31 03:10:21 +0000 | [diff] [blame] | 10 | import xml.etree |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 11 | import test.test_support |
Raymond Hettinger | 9aa5a34 | 2011-03-25 16:00:13 -0700 | [diff] [blame] | 12 | from collections import namedtuple |
Ned Deily | 1a96f8d | 2011-10-06 14:17:34 -0700 | [diff] [blame] | 13 | from test.script_helper import assert_python_ok |
Antoine Pitrou | 161df94 | 2009-10-30 21:41:22 +0000 | [diff] [blame] | 14 | from test.test_support import ( |
Ned Deily | 1a96f8d | 2011-10-06 14:17:34 -0700 | [diff] [blame] | 15 | TESTFN, rmtree, reap_children, captured_stdout) |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 16 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 17 | from test import pydoc_mod |
| 18 | |
| 19 | expected_text_pattern = \ |
| 20 | """ |
| 21 | NAME |
| 22 | test.pydoc_mod - This is a test module for test_pydoc |
| 23 | |
| 24 | FILE |
| 25 | %s |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 26 | %s |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 27 | CLASSES |
| 28 | __builtin__.object |
| 29 | B |
| 30 | A |
| 31 | \x20\x20\x20\x20 |
| 32 | class A |
| 33 | | Hello and goodbye |
| 34 | |\x20\x20 |
| 35 | | Methods defined here: |
| 36 | |\x20\x20 |
| 37 | | __init__() |
| 38 | | Wow, I have no function! |
| 39 | \x20\x20\x20\x20 |
| 40 | class B(__builtin__.object) |
| 41 | | Data descriptors defined here: |
| 42 | |\x20\x20 |
| 43 | | __dict__ |
| 44 | | dictionary for instance variables (if defined) |
| 45 | |\x20\x20 |
| 46 | | __weakref__ |
| 47 | | list of weak references to the object (if defined) |
| 48 | |\x20\x20 |
| 49 | | ---------------------------------------------------------------------- |
| 50 | | Data and other attributes defined here: |
| 51 | |\x20\x20 |
| 52 | | NO_MEANING = 'eggs' |
| 53 | |
| 54 | FUNCTIONS |
| 55 | doc_func() |
| 56 | This function solves all of the world's problems: |
| 57 | hunger |
| 58 | lack of Python |
| 59 | war |
| 60 | \x20\x20\x20\x20 |
| 61 | nodoc_func() |
| 62 | |
| 63 | DATA |
| 64 | __author__ = 'Benjamin Peterson' |
| 65 | __credits__ = 'Nobody' |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 66 | __version__ = '1.2.3.4' |
| 67 | |
| 68 | VERSION |
| 69 | 1.2.3.4 |
| 70 | |
| 71 | AUTHOR |
| 72 | Benjamin Peterson |
| 73 | |
| 74 | CREDITS |
| 75 | Nobody |
| 76 | """.strip() |
| 77 | |
| 78 | expected_html_pattern = \ |
| 79 | """ |
| 80 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 81 | <tr bgcolor="#7799ee"> |
| 82 | <td valign=bottom> <br> |
| 83 | <font color="#ffffff" face="helvetica, arial"> <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 |
| 84 | ><td align=right valign=bottom |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 85 | ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table> |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 86 | <p><tt>This is a test module for test_pydoc</tt></p> |
| 87 | <p> |
| 88 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 89 | <tr bgcolor="#ee77aa"> |
| 90 | <td colspan=3 valign=bottom> <br> |
| 91 | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| 92 | \x20\x20\x20\x20 |
| 93 | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| 94 | <td width="100%%"><dl> |
| 95 | <dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a> |
| 96 | </font></dt><dd> |
| 97 | <dl> |
| 98 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a> |
| 99 | </font></dt></dl> |
| 100 | </dd> |
| 101 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a> |
| 102 | </font></dt></dl> |
| 103 | <p> |
| 104 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 105 | <tr bgcolor="#ffc8d8"> |
| 106 | <td colspan=3 valign=bottom> <br> |
| 107 | <font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a></font></td></tr> |
| 108 | \x20\x20\x20\x20 |
| 109 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 110 | <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr> |
| 111 | <tr><td> </td> |
| 112 | <td width="100%%">Methods defined here:<br> |
| 113 | <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl> |
| 114 | |
| 115 | </td></tr></table> <p> |
| 116 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 117 | <tr bgcolor="#ffc8d8"> |
| 118 | <td colspan=3 valign=bottom> <br> |
| 119 | <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> |
| 120 | \x20\x20\x20\x20 |
| 121 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 122 | <td width="100%%">Data descriptors defined here:<br> |
| 123 | <dl><dt><strong>__dict__</strong></dt> |
| 124 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 125 | </dl> |
| 126 | <dl><dt><strong>__weakref__</strong></dt> |
| 127 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 128 | </dl> |
| 129 | <hr> |
| 130 | Data and other attributes defined here:<br> |
| 131 | <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl> |
| 132 | |
| 133 | </td></tr></table></td></tr></table><p> |
| 134 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 135 | <tr bgcolor="#eeaa77"> |
| 136 | <td colspan=3 valign=bottom> <br> |
| 137 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 138 | \x20\x20\x20\x20 |
| 139 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 140 | <td width="100%%"><dl><dt><a name="-doc_func"><strong>doc_func</strong></a>()</dt><dd><tt>This function solves all of the world's problems:<br> |
| 141 | hunger<br> |
| 142 | lack of Python<br> |
| 143 | war</tt></dd></dl> |
| 144 | <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl> |
| 145 | </td></tr></table><p> |
| 146 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 147 | <tr bgcolor="#55aa55"> |
| 148 | <td colspan=3 valign=bottom> <br> |
| 149 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 150 | \x20\x20\x20\x20 |
| 151 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
| 152 | <td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br> |
| 153 | <strong>__credits__</strong> = 'Nobody'<br> |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 154 | <strong>__version__</strong> = '1.2.3.4'</td></tr></table><p> |
| 155 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 156 | <tr bgcolor="#7799ee"> |
| 157 | <td colspan=3 valign=bottom> <br> |
| 158 | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| 159 | \x20\x20\x20\x20 |
| 160 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 161 | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
| 162 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 163 | <tr bgcolor="#7799ee"> |
| 164 | <td colspan=3 valign=bottom> <br> |
| 165 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 166 | \x20\x20\x20\x20 |
| 167 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 168 | <td width="100%%">Nobody</td></tr></table> |
| 169 | """.strip() |
| 170 | |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 171 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 172 | # output pattern for missing module |
| 173 | missing_pattern = "no Python documentation found for '%s'" |
| 174 | |
R. David Murray | ef087da | 2009-06-23 18:02:46 +0000 | [diff] [blame] | 175 | # output pattern for module with bad imports |
| 176 | badimport_pattern = "problem in %s - <type 'exceptions.ImportError'>: No module named %s" |
| 177 | |
Ned Deily | 1a96f8d | 2011-10-06 14:17:34 -0700 | [diff] [blame] | 178 | def run_pydoc(module_name, *args, **env): |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 179 | """ |
| 180 | Runs pydoc on the specified module. Returns the stripped |
| 181 | output of pydoc. |
| 182 | """ |
Ned Deily | 1a96f8d | 2011-10-06 14:17:34 -0700 | [diff] [blame] | 183 | args = args + (module_name,) |
| 184 | # do not write bytecode files to avoid caching errors |
| 185 | rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env) |
| 186 | return out.strip() |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 187 | |
| 188 | def get_pydoc_html(module): |
| 189 | "Returns pydoc generated output as html" |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 190 | doc = pydoc.HTMLDoc() |
| 191 | output = doc.docmodule(module) |
| 192 | loc = doc.getdocloc(pydoc_mod) or "" |
| 193 | if loc: |
| 194 | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
| 195 | return output.strip(), loc |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 196 | |
| 197 | def get_pydoc_text(module): |
| 198 | "Returns pydoc generated output as text" |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 199 | doc = pydoc.TextDoc() |
| 200 | loc = doc.getdocloc(pydoc_mod) or "" |
| 201 | if loc: |
| 202 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 203 | |
| 204 | output = doc.docmodule(module) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 205 | |
| 206 | # cleanup the extra text formatting that pydoc preforms |
| 207 | patt = re.compile('\b.') |
| 208 | output = patt.sub('', output) |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 209 | return output.strip(), loc |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 210 | |
| 211 | def print_diffs(text1, text2): |
| 212 | "Prints unified diffs for two texts" |
Georg Brandl | fb3de1f | 2008-05-20 08:07:36 +0000 | [diff] [blame] | 213 | lines1 = text1.splitlines(True) |
| 214 | lines2 = text2.splitlines(True) |
| 215 | diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected', |
| 216 | tofile='got') |
| 217 | print '\n' + ''.join(diffs) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 218 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 219 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 220 | class PyDocDocTest(unittest.TestCase): |
| 221 | |
R. David Murray | f28fd24 | 2010-02-23 00:24:49 +0000 | [diff] [blame] | 222 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 223 | "Docstrings are omitted with -O2 and above") |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 224 | def test_html_doc(self): |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 225 | result, doc_loc = get_pydoc_html(pydoc_mod) |
Georg Brandl | b7e419e | 2008-05-20 08:10:03 +0000 | [diff] [blame] | 226 | mod_file = inspect.getabsfile(pydoc_mod) |
Amaury Forgeot d'Arc | 8e8de4a | 2008-06-10 21:37:15 +0000 | [diff] [blame] | 227 | if sys.platform == 'win32': |
| 228 | import nturl2path |
| 229 | mod_url = nturl2path.pathname2url(mod_file) |
| 230 | else: |
| 231 | mod_url = mod_file |
| 232 | expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 233 | if result != expected_html: |
Georg Brandl | fb3de1f | 2008-05-20 08:07:36 +0000 | [diff] [blame] | 234 | print_diffs(expected_html, result) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 235 | self.fail("outputs are not equal, see diff above") |
| 236 | |
R. David Murray | f28fd24 | 2010-02-23 00:24:49 +0000 | [diff] [blame] | 237 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 238 | "Docstrings are omitted with -O2 and above") |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 239 | def test_text_doc(self): |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 240 | result, doc_loc = get_pydoc_text(pydoc_mod) |
| 241 | expected_text = expected_text_pattern % \ |
| 242 | (inspect.getabsfile(pydoc_mod), doc_loc) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 243 | if result != expected_text: |
Georg Brandl | fb3de1f | 2008-05-20 08:07:36 +0000 | [diff] [blame] | 244 | print_diffs(expected_text, result) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 245 | self.fail("outputs are not equal, see diff above") |
| 246 | |
Brian Curtin | aeb2e82 | 2010-03-31 03:10:21 +0000 | [diff] [blame] | 247 | def test_issue8225(self): |
| 248 | # Test issue8225 to ensure no doc link appears for xml.etree |
| 249 | result, doc_loc = get_pydoc_text(xml.etree) |
| 250 | self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link") |
| 251 | |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 252 | def test_not_here(self): |
| 253 | missing_module = "test.i_am_not_here" |
| 254 | result = run_pydoc(missing_module) |
| 255 | expected = missing_pattern % missing_module |
| 256 | self.assertEqual(expected, result, |
| 257 | "documentation for missing module found") |
| 258 | |
R. David Murray | d67ea7d | 2009-05-27 20:07:21 +0000 | [diff] [blame] | 259 | def test_input_strip(self): |
| 260 | missing_module = " test.i_am_not_here " |
| 261 | result = run_pydoc(missing_module) |
| 262 | expected = missing_pattern % missing_module.strip() |
| 263 | self.assertEqual(expected, result, |
| 264 | "white space was not stripped from module name " |
| 265 | "or other error output mismatch") |
| 266 | |
Ezio Melotti | e511fc7 | 2010-02-16 23:26:09 +0000 | [diff] [blame] | 267 | def test_stripid(self): |
| 268 | # test with strings, other implementations might have different repr() |
| 269 | stripid = pydoc.stripid |
| 270 | # strip the id |
| 271 | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
| 272 | '<function stripid>') |
| 273 | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
| 274 | '<function stripid>') |
| 275 | # nothing to strip, return the same text |
| 276 | self.assertEqual(stripid('42'), '42') |
| 277 | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
| 278 | "<type 'exceptions.Exception'>") |
| 279 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 280 | |
Ned Deily | 1a96f8d | 2011-10-06 14:17:34 -0700 | [diff] [blame] | 281 | class PydocImportTest(unittest.TestCase): |
| 282 | |
| 283 | def setUp(self): |
| 284 | self.test_dir = os.mkdir(TESTFN) |
| 285 | self.addCleanup(rmtree, TESTFN) |
| 286 | |
| 287 | def test_badimport(self): |
| 288 | # This tests the fix for issue 5230, where if pydoc found the module |
| 289 | # but the module had an internal import error pydoc would report no doc |
| 290 | # found. |
| 291 | modname = 'testmod_xyzzy' |
| 292 | testpairs = ( |
| 293 | ('i_am_not_here', 'i_am_not_here'), |
| 294 | ('test.i_am_not_here_either', 'i_am_not_here_either'), |
| 295 | ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'), |
| 296 | ('i_am_not_here.{}'.format(modname), |
| 297 | 'i_am_not_here.{}'.format(modname)), |
| 298 | ('test.{}'.format(modname), modname), |
| 299 | ) |
| 300 | |
| 301 | sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py" |
| 302 | for importstring, expectedinmsg in testpairs: |
| 303 | with open(sourcefn, 'w') as f: |
| 304 | f.write("import {}\n".format(importstring)) |
| 305 | result = run_pydoc(modname, PYTHONPATH=TESTFN) |
| 306 | expected = badimport_pattern % (modname, expectedinmsg) |
| 307 | self.assertEqual(expected, result) |
| 308 | |
| 309 | def test_apropos_with_bad_package(self): |
| 310 | # Issue 7425 - pydoc -k failed when bad package on path |
| 311 | pkgdir = os.path.join(TESTFN, "syntaxerr") |
| 312 | os.mkdir(pkgdir) |
| 313 | badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py" |
| 314 | with open(badsyntax, 'w') as f: |
| 315 | f.write("invalid python syntax = $1\n") |
| 316 | result = run_pydoc('nothing', '-k', PYTHONPATH=TESTFN) |
| 317 | self.assertEqual('', result) |
| 318 | |
| 319 | def test_apropos_with_unreadable_dir(self): |
| 320 | # Issue 7367 - pydoc -k failed when unreadable dir on path |
| 321 | self.unreadable_dir = os.path.join(TESTFN, "unreadable") |
| 322 | os.mkdir(self.unreadable_dir, 0) |
| 323 | self.addCleanup(os.rmdir, self.unreadable_dir) |
| 324 | # Note, on Windows the directory appears to be still |
| 325 | # readable so this is not really testing the issue there |
| 326 | result = run_pydoc('nothing', '-k', PYTHONPATH=TESTFN) |
| 327 | self.assertEqual('', result) |
| 328 | |
| 329 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 330 | class TestDescriptions(unittest.TestCase): |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 331 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 332 | def test_module(self): |
| 333 | # Check that pydocfodder module can be described |
| 334 | from test import pydocfodder |
| 335 | doc = pydoc.render_doc(pydocfodder) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 336 | self.assertIn("pydocfodder", doc) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 337 | |
| 338 | def test_classic_class(self): |
| 339 | class C: "Classic class" |
| 340 | c = C() |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 341 | self.assertEqual(pydoc.describe(C), 'class C') |
| 342 | self.assertEqual(pydoc.describe(c), 'instance of C') |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 343 | expected = 'instance of C in module %s' % __name__ |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 344 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 345 | |
| 346 | def test_class(self): |
| 347 | class C(object): "New-style class" |
| 348 | c = C() |
| 349 | |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 350 | self.assertEqual(pydoc.describe(C), 'class C') |
| 351 | self.assertEqual(pydoc.describe(c), 'C') |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 352 | expected = 'C in module %s object' % __name__ |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 353 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 354 | |
Raymond Hettinger | 9aa5a34 | 2011-03-25 16:00:13 -0700 | [diff] [blame] | 355 | def test_namedtuple_public_underscore(self): |
| 356 | NT = namedtuple('NT', ['abc', 'def'], rename=True) |
| 357 | with captured_stdout() as help_io: |
| 358 | help(NT) |
| 359 | helptext = help_io.getvalue() |
| 360 | self.assertIn('_1', helptext) |
| 361 | self.assertIn('_replace', helptext) |
| 362 | self.assertIn('_asdict', helptext) |
| 363 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 364 | |
Ezio Melotti | bdfa2e6 | 2011-04-28 07:59:33 +0300 | [diff] [blame] | 365 | class TestHelper(unittest.TestCase): |
| 366 | def test_keywords(self): |
| 367 | self.assertEqual(sorted(pydoc.Helper.keywords), |
| 368 | sorted(keyword.kwlist)) |
| 369 | |
Éric Araujo | 9a52830 | 2011-07-29 17:34:35 +0200 | [diff] [blame] | 370 | def test_builtin(self): |
| 371 | for name in ('str', 'str.translate', '__builtin__.str', |
| 372 | '__builtin__.str.translate'): |
| 373 | # test low-level function |
| 374 | self.assertIsNotNone(pydoc.locate(name)) |
| 375 | # test high-level function |
| 376 | try: |
| 377 | pydoc.render_doc(name) |
| 378 | except ImportError: |
| 379 | self.fail('finding the doc of {!r} failed'.format(o)) |
| 380 | |
| 381 | for name in ('not__builtin__', 'strrr', 'strr.translate', |
| 382 | 'str.trrrranslate', '__builtin__.strrr', |
| 383 | '__builtin__.str.trrranslate'): |
| 384 | self.assertIsNone(pydoc.locate(name)) |
| 385 | self.assertRaises(ImportError, pydoc.render_doc, name) |
| 386 | |
Ezio Melotti | bdfa2e6 | 2011-04-28 07:59:33 +0300 | [diff] [blame] | 387 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 388 | def test_main(): |
Ned Deily | 1a96f8d | 2011-10-06 14:17:34 -0700 | [diff] [blame] | 389 | try: |
| 390 | test.test_support.run_unittest(PyDocDocTest, |
| 391 | PydocImportTest, |
| 392 | TestDescriptions, |
| 393 | TestHelper) |
| 394 | finally: |
| 395 | reap_children() |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 396 | |
| 397 | if __name__ == "__main__": |
| 398 | test_main() |