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