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