blob: c78e986e2b876bfcfec23e83ec69c4b0b45db106 [file] [log] [blame]
Georg Brandl8632cc22008-05-18 16:32:48 +00001import sys
Benjamin Peterson3a7305e2008-05-22 23:09:26 +00002import os
Georg Brandl8632cc22008-05-18 16:32:48 +00003import difflib
4import subprocess
5import re
6import pydoc
Georg Brandlb7e419e2008-05-20 08:10:03 +00007import inspect
Georg Brandl8632cc22008-05-18 16:32:48 +00008import unittest
9import test.test_support
Benjamin Petersonf5c38da2008-05-18 20:48:07 +000010
Georg Brandl8632cc22008-05-18 16:32:48 +000011from test import pydoc_mod
12
13expected_text_pattern = \
14"""
15NAME
16 test.pydoc_mod - This is a test module for test_pydoc
17
18FILE
19 %s
Benjamin Peterson3a7305e2008-05-22 23:09:26 +000020%s
Georg Brandl8632cc22008-05-18 16:32:48 +000021CLASSES
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
48FUNCTIONS
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
57DATA
58 __author__ = 'Benjamin Peterson'
59 __credits__ = 'Nobody'
60 __package__ = None
61 __version__ = '1.2.3.4'
62
63VERSION
64 1.2.3.4
65
66AUTHOR
67 Benjamin Peterson
68
69CREDITS
70 Nobody
71""".strip()
72
73expected_html_pattern = \
74"""
75<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
76<tr bgcolor="#7799ee">
77<td valign=bottom>&nbsp;<br>
78<font color="#ffffff" face="helvetica, arial">&nbsp;<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 Peterson3a7305e2008-05-22 23:09:26 +000080><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
Georg Brandl8632cc22008-05-18 16:32:48 +000081 <p><tt>This&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</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>&nbsp;<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>&nbsp;&nbsp;&nbsp;</tt></td>
105<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
106<tr><td>&nbsp;</td>
107<td width="100%%">Methods defined here:<br>
108<dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow,&nbsp;I&nbsp;have&nbsp;no&nbsp;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>&nbsp;<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>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
117<td width="100%%">Data descriptors defined here:<br>
118<dl><dt><strong>__dict__</strong></dt>
119<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
120</dl>
121<dl><dt><strong>__weakref__</strong></dt>
122<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
123</dl>
124<hr>
125Data 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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
135<td width="100%%"><dl><dt><a name="-doc_func"><strong>doc_func</strong></a>()</dt><dd><tt>This&nbsp;function&nbsp;solves&nbsp;all&nbsp;of&nbsp;the&nbsp;world's&nbsp;problems:<br>
136hunger<br>
137lack&nbsp;of&nbsp;Python<br>
138war</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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
157<td width="100%%">Benjamin&nbsp;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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
164<td width="100%%">Nobody</td></tr></table>
165""".strip()
166
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000167
Georg Brandl8632cc22008-05-18 16:32:48 +0000168# output pattern for missing module
169missing_pattern = "no Python documentation found for '%s'"
170
171def 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
180def get_pydoc_html(module):
181 "Returns pydoc generated output as html"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000182 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 Brandl8632cc22008-05-18 16:32:48 +0000188
189def get_pydoc_text(module):
190 "Returns pydoc generated output as text"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000191 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 Brandl8632cc22008-05-18 16:32:48 +0000197
198 # cleanup the extra text formatting that pydoc preforms
199 patt = re.compile('\b.')
200 output = patt.sub('', output)
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000201 return output.strip(), loc
Georg Brandl8632cc22008-05-18 16:32:48 +0000202
203def print_diffs(text1, text2):
204 "Prints unified diffs for two texts"
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000205 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 Brandl8632cc22008-05-18 16:32:48 +0000210
Georg Brandl8632cc22008-05-18 16:32:48 +0000211
Georg Brandl8632cc22008-05-18 16:32:48 +0000212class PyDocDocTest(unittest.TestCase):
213
214 def test_html_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000215 result, doc_loc = get_pydoc_html(pydoc_mod)
Georg Brandlb7e419e2008-05-20 08:10:03 +0000216 mod_file = inspect.getabsfile(pydoc_mod)
Amaury Forgeot d'Arc8e8de4a2008-06-10 21:37:15 +0000217 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 Brandl8632cc22008-05-18 16:32:48 +0000223 if result != expected_html:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000224 print_diffs(expected_html, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000225 self.fail("outputs are not equal, see diff above")
226
227 def test_text_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000228 result, doc_loc = get_pydoc_text(pydoc_mod)
229 expected_text = expected_text_pattern % \
230 (inspect.getabsfile(pydoc_mod), doc_loc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000231 if result != expected_text:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000232 print_diffs(expected_text, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000233 self.fail("outputs are not equal, see diff above")
234
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000235 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 Brandl8632cc22008-05-18 16:32:48 +0000242
243class TestDescriptions(unittest.TestCase):
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000244
Georg Brandl8632cc22008-05-18 16:32:48 +0000245 def test_module(self):
246 # Check that pydocfodder module can be described
247 from test import pydocfodder
248 doc = pydoc.render_doc(pydocfodder)
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000249 self.assert_("pydocfodder" in doc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000250
251 def test_classic_class(self):
252 class C: "Classic class"
253 c = C()
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000254 self.assertEqual(pydoc.describe(C), 'class C')
255 self.assertEqual(pydoc.describe(c), 'instance of C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000256 expected = 'instance of C in module %s' % __name__
257 self.assert_(expected in pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000258
259 def test_class(self):
260 class C(object): "New-style class"
261 c = C()
262
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000263 self.assertEqual(pydoc.describe(C), 'class C')
264 self.assertEqual(pydoc.describe(c), 'C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000265 expected = 'C in module %s object' % __name__
266 self.assert_(expected in pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000267
268
269def test_main():
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000270 test.test_support.run_unittest(PyDocDocTest,
Georg Brandl8632cc22008-05-18 16:32:48 +0000271 TestDescriptions)
272
273if __name__ == "__main__":
274 test_main()