Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1 | import os |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2 | import sys |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 3 | import difflib |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 4 | import inspect |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 5 | import pydoc |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 6 | import keyword |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 7 | import re |
| 8 | import string |
| 9 | import subprocess |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 10 | import test.support |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 11 | import time |
| 12 | import unittest |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 13 | import xml.etree |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 14 | import textwrap |
| 15 | from io import StringIO |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 16 | from collections import namedtuple |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 17 | from contextlib import contextmanager |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame^] | 18 | from test.support import ( |
| 19 | TESTFN, forget, rmtree, EnvironmentVarGuard, |
| 20 | reap_children, reap_threads, captured_output, captured_stdout, unlink |
| 21 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 22 | |
| 23 | from test import pydoc_mod |
| 24 | |
Victor Stinner | 62a68f2 | 2011-05-20 02:29:13 +0200 | [diff] [blame] | 25 | try: |
| 26 | import threading |
| 27 | except ImportError: |
| 28 | threading = None |
| 29 | |
Florent Xicluna | 085a656 | 2010-03-06 14:04:16 +0000 | [diff] [blame] | 30 | # Just in case sys.modules["test"] has the optional attribute __loader__. |
| 31 | if hasattr(pydoc_mod, "__loader__"): |
| 32 | del pydoc_mod.__loader__ |
| 33 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 34 | expected_text_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 35 | NAME |
| 36 | test.pydoc_mod - This is a test module for test_pydoc |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 37 | %s |
| 38 | CLASSES |
| 39 | builtins.object |
| 40 | A |
| 41 | B |
| 42 | \x20\x20\x20\x20 |
| 43 | class A(builtins.object) |
| 44 | | Hello and goodbye |
| 45 | |\x20\x20 |
| 46 | | Methods defined here: |
| 47 | |\x20\x20 |
| 48 | | __init__() |
| 49 | | Wow, I have no function! |
| 50 | |\x20\x20 |
| 51 | | ---------------------------------------------------------------------- |
| 52 | | Data descriptors defined here: |
| 53 | |\x20\x20 |
| 54 | | __dict__ |
| 55 | | dictionary for instance variables (if defined) |
| 56 | |\x20\x20 |
| 57 | | __weakref__ |
| 58 | | list of weak references to the object (if defined) |
| 59 | \x20\x20\x20\x20 |
| 60 | class B(builtins.object) |
| 61 | | Data descriptors defined here: |
| 62 | |\x20\x20 |
| 63 | | __dict__ |
| 64 | | dictionary for instance variables (if defined) |
| 65 | |\x20\x20 |
| 66 | | __weakref__ |
| 67 | | list of weak references to the object (if defined) |
| 68 | |\x20\x20 |
| 69 | | ---------------------------------------------------------------------- |
| 70 | | Data and other attributes defined here: |
| 71 | |\x20\x20 |
| 72 | | NO_MEANING = 'eggs' |
| 73 | |
| 74 | FUNCTIONS |
| 75 | doc_func() |
| 76 | This function solves all of the world's problems: |
| 77 | hunger |
| 78 | lack of Python |
| 79 | war |
| 80 | \x20\x20\x20\x20 |
| 81 | nodoc_func() |
| 82 | |
| 83 | DATA |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 84 | __xyz__ = 'X, Y and Z' |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 85 | |
| 86 | VERSION |
| 87 | 1.2.3.4 |
| 88 | |
| 89 | AUTHOR |
| 90 | Benjamin Peterson |
| 91 | |
| 92 | CREDITS |
| 93 | Nobody |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 94 | |
| 95 | FILE |
| 96 | %s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 97 | """.strip() |
| 98 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 99 | expected_html_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 100 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 101 | <tr bgcolor="#7799ee"> |
| 102 | <td valign=bottom> <br> |
| 103 | <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 |
| 104 | ><td align=right valign=bottom |
| 105 | ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table> |
| 106 | <p><tt>This is a test module for test_pydoc</tt></p> |
| 107 | <p> |
| 108 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 109 | <tr bgcolor="#ee77aa"> |
| 110 | <td colspan=3 valign=bottom> <br> |
| 111 | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| 112 | \x20\x20\x20\x20 |
| 113 | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| 114 | <td width="100%%"><dl> |
| 115 | <dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a> |
| 116 | </font></dt><dd> |
| 117 | <dl> |
| 118 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a> |
| 119 | </font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a> |
| 120 | </font></dt></dl> |
| 121 | </dd> |
| 122 | </dl> |
| 123 | <p> |
| 124 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 125 | <tr bgcolor="#ffc8d8"> |
| 126 | <td colspan=3 valign=bottom> <br> |
| 127 | <font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
| 128 | \x20\x20\x20\x20 |
| 129 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 130 | <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr> |
| 131 | <tr><td> </td> |
| 132 | <td width="100%%">Methods defined here:<br> |
| 133 | <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl> |
| 134 | |
| 135 | <hr> |
| 136 | Data descriptors defined here:<br> |
| 137 | <dl><dt><strong>__dict__</strong></dt> |
| 138 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 139 | </dl> |
| 140 | <dl><dt><strong>__weakref__</strong></dt> |
| 141 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 142 | </dl> |
| 143 | </td></tr></table> <p> |
| 144 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 145 | <tr bgcolor="#ffc8d8"> |
| 146 | <td colspan=3 valign=bottom> <br> |
| 147 | <font color="#000000" face="helvetica, arial"><a name="B">class <strong>B</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
| 148 | \x20\x20\x20\x20 |
| 149 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 150 | <td width="100%%">Data descriptors defined here:<br> |
| 151 | <dl><dt><strong>__dict__</strong></dt> |
| 152 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 153 | </dl> |
| 154 | <dl><dt><strong>__weakref__</strong></dt> |
| 155 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 156 | </dl> |
| 157 | <hr> |
| 158 | Data and other attributes defined here:<br> |
| 159 | <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl> |
| 160 | |
| 161 | </td></tr></table></td></tr></table><p> |
| 162 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 163 | <tr bgcolor="#eeaa77"> |
| 164 | <td colspan=3 valign=bottom> <br> |
| 165 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 166 | \x20\x20\x20\x20 |
| 167 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 168 | <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> |
| 169 | hunger<br> |
| 170 | lack of Python<br> |
| 171 | war</tt></dd></dl> |
| 172 | <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl> |
| 173 | </td></tr></table><p> |
| 174 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 175 | <tr bgcolor="#55aa55"> |
| 176 | <td colspan=3 valign=bottom> <br> |
| 177 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 178 | \x20\x20\x20\x20 |
| 179 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 180 | <td width="100%%"><strong>__xyz__</strong> = 'X, Y and Z'</td></tr></table><p> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 181 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 182 | <tr bgcolor="#7799ee"> |
| 183 | <td colspan=3 valign=bottom> <br> |
| 184 | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| 185 | \x20\x20\x20\x20 |
| 186 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 187 | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
| 188 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 189 | <tr bgcolor="#7799ee"> |
| 190 | <td colspan=3 valign=bottom> <br> |
| 191 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 192 | \x20\x20\x20\x20 |
| 193 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 194 | <td width="100%%">Nobody</td></tr></table> |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 195 | """.strip() # ' <- emacs turd |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 196 | |
| 197 | |
| 198 | # output pattern for missing module |
| 199 | missing_pattern = "no Python documentation found for '%s'" |
| 200 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 201 | # output pattern for module with bad imports |
| 202 | badimport_pattern = "problem in %s - ImportError: No module named %s" |
| 203 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 204 | def run_pydoc(module_name, *args): |
| 205 | """ |
| 206 | Runs pydoc on the specified module. Returns the stripped |
| 207 | output of pydoc. |
| 208 | """ |
| 209 | cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name] |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame^] | 210 | output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 211 | return output.strip() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 212 | |
| 213 | def get_pydoc_html(module): |
| 214 | "Returns pydoc generated output as html" |
| 215 | doc = pydoc.HTMLDoc() |
| 216 | output = doc.docmodule(module) |
| 217 | loc = doc.getdocloc(pydoc_mod) or "" |
| 218 | if loc: |
| 219 | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
| 220 | return output.strip(), loc |
| 221 | |
| 222 | def get_pydoc_text(module): |
| 223 | "Returns pydoc generated output as text" |
| 224 | doc = pydoc.TextDoc() |
| 225 | loc = doc.getdocloc(pydoc_mod) or "" |
| 226 | if loc: |
| 227 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 228 | |
| 229 | output = doc.docmodule(module) |
| 230 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 231 | # clean up the extra text formatting that pydoc performs |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 232 | patt = re.compile('\b.') |
| 233 | output = patt.sub('', output) |
| 234 | return output.strip(), loc |
| 235 | |
| 236 | def print_diffs(text1, text2): |
| 237 | "Prints unified diffs for two texts" |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 238 | # XXX now obsolete, use unittest built-in support |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 239 | lines1 = text1.splitlines(True) |
| 240 | lines2 = text2.splitlines(True) |
| 241 | diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected', |
| 242 | tofile='got') |
| 243 | print('\n' + ''.join(diffs)) |
| 244 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 245 | def get_html_title(text): |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 246 | # Bit of hack, but good enough for test purposes |
| 247 | header, _, _ = text.partition("</head>") |
| 248 | _, _, title = header.partition("<title>") |
| 249 | title, _, _ = title.partition("</title>") |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 250 | return title |
| 251 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 252 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 253 | class PydocDocTest(unittest.TestCase): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 254 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 255 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 256 | "Docstrings are omitted with -O2 and above") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 257 | def test_html_doc(self): |
| 258 | result, doc_loc = get_pydoc_html(pydoc_mod) |
| 259 | mod_file = inspect.getabsfile(pydoc_mod) |
Benjamin Peterson | c5e9464 | 2008-06-14 23:04:46 +0000 | [diff] [blame] | 260 | if sys.platform == 'win32': |
| 261 | import nturl2path |
| 262 | mod_url = nturl2path.pathname2url(mod_file) |
| 263 | else: |
| 264 | mod_url = mod_file |
| 265 | expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 266 | if result != expected_html: |
| 267 | print_diffs(expected_html, result) |
| 268 | self.fail("outputs are not equal, see diff above") |
| 269 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 270 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 271 | "Docstrings are omitted with -O2 and above") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 272 | def test_text_doc(self): |
| 273 | result, doc_loc = get_pydoc_text(pydoc_mod) |
| 274 | expected_text = expected_text_pattern % \ |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 275 | (doc_loc, inspect.getabsfile(pydoc_mod)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 276 | if result != expected_text: |
| 277 | print_diffs(expected_text, result) |
| 278 | self.fail("outputs are not equal, see diff above") |
| 279 | |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 280 | def test_issue8225(self): |
| 281 | # Test issue8225 to ensure no doc link appears for xml.etree |
| 282 | result, doc_loc = get_pydoc_text(xml.etree) |
| 283 | self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link") |
| 284 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 285 | def test_not_here(self): |
| 286 | missing_module = "test.i_am_not_here" |
| 287 | result = str(run_pydoc(missing_module), 'ascii') |
| 288 | expected = missing_pattern % missing_module |
| 289 | self.assertEqual(expected, result, |
| 290 | "documentation for missing module found") |
| 291 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 292 | def test_badimport(self): |
| 293 | # This tests the fix for issue 5230, where if pydoc found the module |
| 294 | # but the module had an internal import error pydoc would report no doc |
| 295 | # found. |
| 296 | modname = 'testmod_xyzzy' |
| 297 | testpairs = ( |
| 298 | ('i_am_not_here', 'i_am_not_here'), |
| 299 | ('test.i_am_not_here_either', 'i_am_not_here_either'), |
| 300 | ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'), |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 301 | ('i_am_not_here.{}'.format(modname), |
| 302 | 'i_am_not_here.{}'.format(modname)), |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 303 | ('test.{}'.format(modname), modname), |
| 304 | ) |
| 305 | |
| 306 | @contextmanager |
| 307 | def newdirinpath(dir): |
| 308 | os.mkdir(dir) |
| 309 | sys.path.insert(0, dir) |
| 310 | yield |
| 311 | sys.path.pop(0) |
| 312 | rmtree(dir) |
| 313 | |
| 314 | with newdirinpath(TESTFN), EnvironmentVarGuard() as env: |
| 315 | env['PYTHONPATH'] = TESTFN |
| 316 | fullmodname = os.path.join(TESTFN, modname) |
| 317 | sourcefn = fullmodname + os.extsep + "py" |
| 318 | for importstring, expectedinmsg in testpairs: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 319 | with open(sourcefn, 'w') as f: |
| 320 | f.write("import {}\n".format(importstring)) |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 321 | try: |
| 322 | result = run_pydoc(modname).decode("ascii") |
| 323 | finally: |
| 324 | forget(modname) |
| 325 | expected = badimport_pattern % (modname, expectedinmsg) |
| 326 | self.assertEqual(expected, result) |
| 327 | |
R. David Murray | 1f1b9d3 | 2009-05-27 20:56:59 +0000 | [diff] [blame] | 328 | def test_input_strip(self): |
| 329 | missing_module = " test.i_am_not_here " |
| 330 | result = str(run_pydoc(missing_module), 'ascii') |
| 331 | expected = missing_pattern % missing_module.strip() |
| 332 | self.assertEqual(expected, result) |
| 333 | |
Ezio Melotti | 412c95a | 2010-02-16 23:31:04 +0000 | [diff] [blame] | 334 | def test_stripid(self): |
| 335 | # test with strings, other implementations might have different repr() |
| 336 | stripid = pydoc.stripid |
| 337 | # strip the id |
| 338 | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
| 339 | '<function stripid>') |
| 340 | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
| 341 | '<function stripid>') |
| 342 | # nothing to strip, return the same text |
| 343 | self.assertEqual(stripid('42'), '42') |
| 344 | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
| 345 | "<type 'exceptions.Exception'>") |
| 346 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 347 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 348 | 'Docstrings are omitted with -O2 and above') |
| 349 | def test_help_output_redirect(self): |
| 350 | # issue 940286, if output is set in Helper, then all output from |
| 351 | # Helper.help should be redirected |
| 352 | old_pattern = expected_text_pattern |
| 353 | getpager_old = pydoc.getpager |
| 354 | getpager_new = lambda: (lambda x: x) |
| 355 | self.maxDiff = None |
| 356 | |
| 357 | buf = StringIO() |
| 358 | helper = pydoc.Helper(output=buf) |
| 359 | unused, doc_loc = get_pydoc_text(pydoc_mod) |
| 360 | module = "test.pydoc_mod" |
| 361 | help_header = """ |
| 362 | Help on module test.pydoc_mod in test: |
| 363 | |
| 364 | """.lstrip() |
| 365 | help_header = textwrap.dedent(help_header) |
| 366 | expected_help_pattern = help_header + expected_text_pattern |
| 367 | |
| 368 | pydoc.getpager = getpager_new |
| 369 | try: |
| 370 | with captured_output('stdout') as output, \ |
| 371 | captured_output('stderr') as err: |
| 372 | helper.help(module) |
| 373 | result = buf.getvalue().strip() |
| 374 | expected_text = expected_help_pattern % \ |
| 375 | (doc_loc, inspect.getabsfile(pydoc_mod)) |
| 376 | self.assertEqual('', output.getvalue()) |
| 377 | self.assertEqual('', err.getvalue()) |
| 378 | self.assertEqual(expected_text, result) |
| 379 | finally: |
| 380 | pydoc.getpager = getpager_old |
| 381 | |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 382 | def test_namedtuple_public_underscore(self): |
| 383 | NT = namedtuple('NT', ['abc', 'def'], rename=True) |
| 384 | with captured_stdout() as help_io: |
| 385 | help(NT) |
| 386 | helptext = help_io.getvalue() |
| 387 | self.assertIn('_1', helptext) |
| 388 | self.assertIn('_replace', helptext) |
| 389 | self.assertIn('_asdict', helptext) |
| 390 | |
Victor Stinner | e6c910e | 2011-06-30 15:55:43 +0200 | [diff] [blame] | 391 | def test_synopsis(self): |
| 392 | self.addCleanup(unlink, TESTFN) |
| 393 | for encoding in ('ISO-8859-1', 'UTF-8'): |
| 394 | with open(TESTFN, 'w', encoding=encoding) as script: |
| 395 | if encoding != 'UTF-8': |
| 396 | print('#coding: {}'.format(encoding), file=script) |
| 397 | print('"""line 1: h\xe9', file=script) |
| 398 | print('line 2: hi"""', file=script) |
| 399 | synopsis = pydoc.synopsis(TESTFN, {}) |
| 400 | self.assertEqual(synopsis, 'line 1: h\xe9') |
| 401 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 402 | |
| 403 | class TestDescriptions(unittest.TestCase): |
| 404 | |
| 405 | def test_module(self): |
| 406 | # Check that pydocfodder module can be described |
| 407 | from test import pydocfodder |
| 408 | doc = pydoc.render_doc(pydocfodder) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 409 | self.assertIn("pydocfodder", doc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 410 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 411 | def test_class(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 412 | class C: "New-style class" |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 413 | c = C() |
| 414 | |
| 415 | self.assertEqual(pydoc.describe(C), 'class C') |
| 416 | self.assertEqual(pydoc.describe(c), 'C') |
| 417 | expected = 'C in module %s object' % __name__ |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 418 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 419 | |
| 420 | |
Victor Stinner | 62a68f2 | 2011-05-20 02:29:13 +0200 | [diff] [blame] | 421 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 422 | class PydocServerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 423 | """Tests for pydoc._start_server""" |
| 424 | |
| 425 | def test_server(self): |
| 426 | |
| 427 | # Minimal test that starts the server, then stops it. |
| 428 | def my_url_handler(url, content_type): |
| 429 | text = 'the URL sent was: (%s, %s)' % (url, content_type) |
| 430 | return text |
| 431 | |
| 432 | serverthread = pydoc._start_server(my_url_handler, port=0) |
| 433 | starttime = time.time() |
| 434 | timeout = 1 #seconds |
| 435 | |
| 436 | while serverthread.serving: |
| 437 | time.sleep(.01) |
| 438 | if serverthread.serving and time.time() - starttime > timeout: |
| 439 | serverthread.stop() |
| 440 | break |
| 441 | |
| 442 | self.assertEqual(serverthread.error, None) |
| 443 | |
| 444 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 445 | class PydocUrlHandlerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 446 | """Tests for pydoc._url_handler""" |
| 447 | |
| 448 | def test_content_type_err(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 449 | f = pydoc._url_handler |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 450 | self.assertRaises(TypeError, f, 'A', '') |
| 451 | self.assertRaises(TypeError, f, 'B', 'foobar') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 452 | |
| 453 | def test_url_requests(self): |
| 454 | # Test for the correct title in the html pages returned. |
| 455 | # This tests the different parts of the URL handler without |
| 456 | # getting too picky about the exact html. |
| 457 | requests = [ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 458 | ("", "Pydoc: Index of Modules"), |
| 459 | ("get?key=", "Pydoc: Index of Modules"), |
| 460 | ("index", "Pydoc: Index of Modules"), |
| 461 | ("topics", "Pydoc: Topics"), |
| 462 | ("keywords", "Pydoc: Keywords"), |
| 463 | ("pydoc", "Pydoc: module pydoc"), |
| 464 | ("get?key=pydoc", "Pydoc: module pydoc"), |
| 465 | ("search?key=pydoc", "Pydoc: Search Results"), |
| 466 | ("topic?key=def", "Pydoc: KEYWORD def"), |
| 467 | ("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"), |
| 468 | ("foobar", "Pydoc: Error - foobar"), |
| 469 | ("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"), |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 470 | ] |
| 471 | |
| 472 | for url, title in requests: |
| 473 | text = pydoc._url_handler(url, "text/html") |
| 474 | result = get_html_title(text) |
| 475 | self.assertEqual(result, title) |
| 476 | |
| 477 | path = string.__file__ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 478 | title = "Pydoc: getfile " + path |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 479 | url = "getfile?key=" + path |
| 480 | text = pydoc._url_handler(url, "text/html") |
| 481 | result = get_html_title(text) |
| 482 | self.assertEqual(result, title) |
| 483 | |
| 484 | |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 485 | class TestHelper(unittest.TestCase): |
| 486 | def test_keywords(self): |
| 487 | self.assertEqual(sorted(pydoc.Helper.keywords), |
| 488 | sorted(keyword.kwlist)) |
| 489 | |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame^] | 490 | @reap_threads |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 491 | def test_main(): |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame^] | 492 | try: |
| 493 | test.support.run_unittest(PydocDocTest, |
| 494 | TestDescriptions, |
| 495 | PydocServerTest, |
| 496 | PydocUrlHandlerTest, |
| 497 | TestHelper, |
| 498 | ) |
| 499 | finally: |
| 500 | reap_children() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 501 | |
| 502 | if __name__ == "__main__": |
| 503 | test_main() |