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