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