blob: 6c10ebf182b903d013e69c1523326d143278bc52 [file] [log] [blame]
Georg Brandl8632cc22008-05-18 16:32:48 +00001import sys
2import difflib
3import subprocess
4import re
5import pydoc
6import unittest
7import test.test_support
Benjamin Petersonf5c38da2008-05-18 20:48:07 +00008
Georg Brandl8632cc22008-05-18 16:32:48 +00009from test import pydoc_mod
10
11expected_text_pattern = \
12"""
13NAME
14 test.pydoc_mod - This is a test module for test_pydoc
15
16FILE
17 %s
18
19CLASSES
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
46FUNCTIONS
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
55DATA
56 __author__ = 'Benjamin Peterson'
57 __credits__ = 'Nobody'
58 __package__ = None
59 __version__ = '1.2.3.4'
60
61VERSION
62 1.2.3.4
63
64AUTHOR
65 Benjamin Peterson
66
67CREDITS
68 Nobody
69""".strip()
70
71expected_html_pattern = \
72"""
73<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
74<tr bgcolor="#7799ee">
75<td valign=bottom>&nbsp;<br>
76<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
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&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</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>&nbsp;<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>&nbsp;&nbsp;&nbsp;</tt></td>
103<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
104<tr><td>&nbsp;</td>
105<td width="100%%">Methods defined here:<br>
106<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>
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>&nbsp;<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>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
115<td width="100%%">Data descriptors defined here:<br>
116<dl><dt><strong>__dict__</strong></dt>
117<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
118</dl>
119<dl><dt><strong>__weakref__</strong></dt>
120<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
121</dl>
122<hr>
123Data 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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
133<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>
134hunger<br>
135lack&nbsp;of&nbsp;Python<br>
136war</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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
155<td width="100%%">Benjamin&nbsp;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>&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
162<td width="100%%">Nobody</td></tr></table>
163""".strip()
164
165# output pattern for missing module
166missing_pattern = "no Python documentation found for '%s'"
167
168def 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
177def get_pydoc_html(module):
178 "Returns pydoc generated output as html"
179 output = pydoc.HTMLDoc().docmodule(module)
180 return output.strip()
181
182def 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
191def print_diffs(text1, text2):
192 "Prints unified diffs for two texts"
193 lines1 = text1.splitlines()
194 lines2 = text2.splitlines()
195 diffs = difflib.unified_diff(lines1, lines2, n=0)
196 diffs = list(diffs) # its a generator
197 print '\n'.join(diffs)
198
199def 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 Brandl8632cc22008-05-18 16:32:48 +0000205class 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:
212 print_diffs(result, expected_html)
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:
219 print_diffs(result, expected_text)
220 self.fail("outputs are not equal, see diff above")
221
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000222 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 Brandl8632cc22008-05-18 16:32:48 +0000229
230class TestDescriptions(unittest.TestCase):
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000231
Georg Brandl8632cc22008-05-18 16:32:48 +0000232 def test_module(self):
233 # Check that pydocfodder module can be described
234 from test import pydocfodder
235 doc = pydoc.render_doc(pydocfodder)
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000236 self.assert_("pydocfodder" in doc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000237
238 def test_classic_class(self):
239 class C: "Classic class"
240 c = C()
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000241 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 Brandl8632cc22008-05-18 16:32:48 +0000244 in pydoc.render_doc(c))
245
246 def test_class(self):
247 class C(object): "New-style class"
248 c = C()
249
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000250 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 Brandl8632cc22008-05-18 16:32:48 +0000253 in pydoc.render_doc(c))
254
255
256def test_main():
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000257 test.test_support.run_unittest(PyDocDocTest,
Georg Brandl8632cc22008-05-18 16:32:48 +0000258 TestDescriptions)
259
260if __name__ == "__main__":
261 test_main()