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