Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 1 | import sys |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 2 | import os |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 3 | import difflib |
| 4 | import subprocess |
| 5 | import re |
| 6 | import pydoc |
Georg Brandl | b7e419e | 2008-05-20 08:10:03 +0000 | [diff] [blame] | 7 | import inspect |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 8 | import unittest |
| 9 | import test.test_support |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 10 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 11 | from test import pydoc_mod |
| 12 | |
| 13 | expected_text_pattern = \ |
| 14 | """ |
| 15 | NAME |
| 16 | test.pydoc_mod - This is a test module for test_pydoc |
| 17 | |
| 18 | FILE |
| 19 | %s |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 20 | %s |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 21 | CLASSES |
| 22 | __builtin__.object |
| 23 | B |
| 24 | A |
| 25 | \x20\x20\x20\x20 |
| 26 | class A |
| 27 | | Hello and goodbye |
| 28 | |\x20\x20 |
| 29 | | Methods defined here: |
| 30 | |\x20\x20 |
| 31 | | __init__() |
| 32 | | Wow, I have no function! |
| 33 | \x20\x20\x20\x20 |
| 34 | class B(__builtin__.object) |
| 35 | | Data descriptors defined here: |
| 36 | |\x20\x20 |
| 37 | | __dict__ |
| 38 | | dictionary for instance variables (if defined) |
| 39 | |\x20\x20 |
| 40 | | __weakref__ |
| 41 | | list of weak references to the object (if defined) |
| 42 | |\x20\x20 |
| 43 | | ---------------------------------------------------------------------- |
| 44 | | Data and other attributes defined here: |
| 45 | |\x20\x20 |
| 46 | | NO_MEANING = 'eggs' |
| 47 | |
| 48 | FUNCTIONS |
| 49 | doc_func() |
| 50 | This function solves all of the world's problems: |
| 51 | hunger |
| 52 | lack of Python |
| 53 | war |
| 54 | \x20\x20\x20\x20 |
| 55 | nodoc_func() |
| 56 | |
| 57 | DATA |
| 58 | __author__ = 'Benjamin Peterson' |
| 59 | __credits__ = 'Nobody' |
| 60 | __package__ = None |
| 61 | __version__ = '1.2.3.4' |
| 62 | |
| 63 | VERSION |
| 64 | 1.2.3.4 |
| 65 | |
| 66 | AUTHOR |
| 67 | Benjamin Peterson |
| 68 | |
| 69 | CREDITS |
| 70 | Nobody |
| 71 | """.strip() |
| 72 | |
| 73 | expected_html_pattern = \ |
| 74 | """ |
| 75 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 76 | <tr bgcolor="#7799ee"> |
| 77 | <td valign=bottom> <br> |
| 78 | <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 |
| 79 | ><td align=right valign=bottom |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 80 | ><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] | 81 | <p><tt>This is a test module for test_pydoc</tt></p> |
| 82 | <p> |
| 83 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 84 | <tr bgcolor="#ee77aa"> |
| 85 | <td colspan=3 valign=bottom> <br> |
| 86 | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| 87 | \x20\x20\x20\x20 |
| 88 | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| 89 | <td width="100%%"><dl> |
| 90 | <dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a> |
| 91 | </font></dt><dd> |
| 92 | <dl> |
| 93 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a> |
| 94 | </font></dt></dl> |
| 95 | </dd> |
| 96 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a> |
| 97 | </font></dt></dl> |
| 98 | <p> |
| 99 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 100 | <tr bgcolor="#ffc8d8"> |
| 101 | <td colspan=3 valign=bottom> <br> |
| 102 | <font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a></font></td></tr> |
| 103 | \x20\x20\x20\x20 |
| 104 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 105 | <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr> |
| 106 | <tr><td> </td> |
| 107 | <td width="100%%">Methods defined here:<br> |
| 108 | <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl> |
| 109 | |
| 110 | </td></tr></table> <p> |
| 111 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 112 | <tr bgcolor="#ffc8d8"> |
| 113 | <td colspan=3 valign=bottom> <br> |
| 114 | <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> |
| 115 | \x20\x20\x20\x20 |
| 116 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 117 | <td width="100%%">Data descriptors defined here:<br> |
| 118 | <dl><dt><strong>__dict__</strong></dt> |
| 119 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 120 | </dl> |
| 121 | <dl><dt><strong>__weakref__</strong></dt> |
| 122 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 123 | </dl> |
| 124 | <hr> |
| 125 | Data and other attributes defined here:<br> |
| 126 | <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl> |
| 127 | |
| 128 | </td></tr></table></td></tr></table><p> |
| 129 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 130 | <tr bgcolor="#eeaa77"> |
| 131 | <td colspan=3 valign=bottom> <br> |
| 132 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 133 | \x20\x20\x20\x20 |
| 134 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 135 | <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> |
| 136 | hunger<br> |
| 137 | lack of Python<br> |
| 138 | war</tt></dd></dl> |
| 139 | <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl> |
| 140 | </td></tr></table><p> |
| 141 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 142 | <tr bgcolor="#55aa55"> |
| 143 | <td colspan=3 valign=bottom> <br> |
| 144 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 145 | \x20\x20\x20\x20 |
| 146 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
| 147 | <td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br> |
| 148 | <strong>__credits__</strong> = 'Nobody'<br> |
| 149 | <strong>__package__</strong> = None<br> |
| 150 | <strong>__version__</strong> = '1.2.3.4'</td></tr></table><p> |
| 151 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 152 | <tr bgcolor="#7799ee"> |
| 153 | <td colspan=3 valign=bottom> <br> |
| 154 | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| 155 | \x20\x20\x20\x20 |
| 156 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 157 | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
| 158 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 159 | <tr bgcolor="#7799ee"> |
| 160 | <td colspan=3 valign=bottom> <br> |
| 161 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 162 | \x20\x20\x20\x20 |
| 163 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 164 | <td width="100%%">Nobody</td></tr></table> |
| 165 | """.strip() |
| 166 | |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 167 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 168 | # output pattern for missing module |
| 169 | missing_pattern = "no Python documentation found for '%s'" |
| 170 | |
| 171 | def run_pydoc(module_name, *args): |
| 172 | """ |
| 173 | Runs pydoc on the specified module. Returns the stripped |
| 174 | output of pydoc. |
| 175 | """ |
| 176 | cmd = [sys.executable, pydoc.__file__, " ".join(args), module_name] |
| 177 | output = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout.read() |
| 178 | return output.strip() |
| 179 | |
| 180 | def get_pydoc_html(module): |
| 181 | "Returns pydoc generated output as html" |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 182 | doc = pydoc.HTMLDoc() |
| 183 | output = doc.docmodule(module) |
| 184 | loc = doc.getdocloc(pydoc_mod) or "" |
| 185 | if loc: |
| 186 | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
| 187 | return output.strip(), loc |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 188 | |
| 189 | def get_pydoc_text(module): |
| 190 | "Returns pydoc generated output as text" |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 191 | doc = pydoc.TextDoc() |
| 192 | loc = doc.getdocloc(pydoc_mod) or "" |
| 193 | if loc: |
| 194 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 195 | |
| 196 | output = doc.docmodule(module) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 197 | |
| 198 | # cleanup the extra text formatting that pydoc preforms |
| 199 | patt = re.compile('\b.') |
| 200 | output = patt.sub('', output) |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 201 | return output.strip(), loc |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 202 | |
| 203 | def print_diffs(text1, text2): |
| 204 | "Prints unified diffs for two texts" |
Georg Brandl | fb3de1f | 2008-05-20 08:07:36 +0000 | [diff] [blame] | 205 | lines1 = text1.splitlines(True) |
| 206 | lines2 = text2.splitlines(True) |
| 207 | diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected', |
| 208 | tofile='got') |
| 209 | print '\n' + ''.join(diffs) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 210 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 211 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 212 | class PyDocDocTest(unittest.TestCase): |
| 213 | |
| 214 | def test_html_doc(self): |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 215 | result, doc_loc = get_pydoc_html(pydoc_mod) |
Georg Brandl | b7e419e | 2008-05-20 08:10:03 +0000 | [diff] [blame] | 216 | mod_file = inspect.getabsfile(pydoc_mod) |
Amaury Forgeot d'Arc | 8e8de4a | 2008-06-10 21:37:15 +0000 | [diff] [blame^] | 217 | if sys.platform == 'win32': |
| 218 | import nturl2path |
| 219 | mod_url = nturl2path.pathname2url(mod_file) |
| 220 | else: |
| 221 | mod_url = mod_file |
| 222 | expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 223 | if result != expected_html: |
Georg Brandl | fb3de1f | 2008-05-20 08:07:36 +0000 | [diff] [blame] | 224 | print_diffs(expected_html, result) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 225 | self.fail("outputs are not equal, see diff above") |
| 226 | |
| 227 | def test_text_doc(self): |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 228 | result, doc_loc = get_pydoc_text(pydoc_mod) |
| 229 | expected_text = expected_text_pattern % \ |
| 230 | (inspect.getabsfile(pydoc_mod), doc_loc) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 231 | if result != expected_text: |
Georg Brandl | fb3de1f | 2008-05-20 08:07:36 +0000 | [diff] [blame] | 232 | print_diffs(expected_text, result) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 233 | self.fail("outputs are not equal, see diff above") |
| 234 | |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 235 | def test_not_here(self): |
| 236 | missing_module = "test.i_am_not_here" |
| 237 | result = run_pydoc(missing_module) |
| 238 | expected = missing_pattern % missing_module |
| 239 | self.assertEqual(expected, result, |
| 240 | "documentation for missing module found") |
| 241 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 242 | |
| 243 | class TestDescriptions(unittest.TestCase): |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 244 | |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 245 | def test_module(self): |
| 246 | # Check that pydocfodder module can be described |
| 247 | from test import pydocfodder |
| 248 | doc = pydoc.render_doc(pydocfodder) |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 249 | self.assert_("pydocfodder" in doc) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 250 | |
| 251 | def test_classic_class(self): |
| 252 | class C: "Classic class" |
| 253 | c = C() |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 254 | self.assertEqual(pydoc.describe(C), 'class C') |
| 255 | self.assertEqual(pydoc.describe(c), 'instance of C') |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 256 | expected = 'instance of C in module %s' % __name__ |
| 257 | self.assert_(expected in pydoc.render_doc(c)) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 258 | |
| 259 | def test_class(self): |
| 260 | class C(object): "New-style class" |
| 261 | c = C() |
| 262 | |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 263 | self.assertEqual(pydoc.describe(C), 'class C') |
| 264 | self.assertEqual(pydoc.describe(c), 'C') |
Benjamin Peterson | 3a7305e | 2008-05-22 23:09:26 +0000 | [diff] [blame] | 265 | expected = 'C in module %s object' % __name__ |
| 266 | self.assert_(expected in pydoc.render_doc(c)) |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 267 | |
| 268 | |
| 269 | def test_main(): |
Benjamin Peterson | f5c38da | 2008-05-18 20:48:07 +0000 | [diff] [blame] | 270 | test.test_support.run_unittest(PyDocDocTest, |
Georg Brandl | 8632cc2 | 2008-05-18 16:32:48 +0000 | [diff] [blame] | 271 | TestDescriptions) |
| 272 | |
| 273 | if __name__ == "__main__": |
| 274 | test_main() |