11 import test
.test_support
12 from contextlib
import contextmanager
13 from test
.test_support
import (
14 TESTFN
, forget
, rmtree
, EnvironmentVarGuard
, reap_children
)
16 from test
import pydoc_mod
18 expected_text_pattern
= \
21 test.pydoc_mod - This is a test module for test_pydoc
34 | Methods defined here:
37 | Wow, I have no function!
39 class B(__builtin__.object)
40 | Data descriptors defined here:
43 | dictionary for instance variables (if defined)
46 | list of weak references to the object (if defined)
48 | ----------------------------------------------------------------------
49 | Data and other attributes defined here:
55 This function solves all of the world's problems:
63 __author__ = 'Benjamin Peterson'
64 __credits__ = 'Nobody'
65 __version__ = '1.2.3.4'
77 expected_html_pattern
= \
79 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
80 <tr bgcolor="#7799ee">
81 <td valign=bottom> <br>
82 <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
83 ><td align=right valign=bottom
84 ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
85 <p><tt>This is a test module for test_pydoc</tt></p>
87 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
88 <tr bgcolor="#ee77aa">
89 <td colspan=3 valign=bottom> <br>
90 <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
92 <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td>
93 <td width="100%%"><dl>
94 <dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a>
97 <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
100 <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
103 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
104 <tr bgcolor="#ffc8d8">
105 <td colspan=3 valign=bottom> <br>
106 <font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a></font></td></tr>
108 <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
109 <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr>
111 <td width="100%%">Methods defined here:<br>
112 <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl>
114 </td></tr></table> <p>
115 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
116 <tr bgcolor="#ffc8d8">
117 <td colspan=3 valign=bottom> <br>
118 <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>
120 <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td>
121 <td width="100%%">Data descriptors defined here:<br>
122 <dl><dt><strong>__dict__</strong></dt>
123 <dd><tt>dictionary for instance variables (if defined)</tt></dd>
125 <dl><dt><strong>__weakref__</strong></dt>
126 <dd><tt>list of weak references to the object (if defined)</tt></dd>
129 Data and other attributes defined here:<br>
130 <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
132 </td></tr></table></td></tr></table><p>
133 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
134 <tr bgcolor="#eeaa77">
135 <td colspan=3 valign=bottom> <br>
136 <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
138 <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td>
139 <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>
141 lack of Python<br>
143 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
144 </td></tr></table><p>
145 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
146 <tr bgcolor="#55aa55">
147 <td colspan=3 valign=bottom> <br>
148 <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
150 <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td>
151 <td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br>
152 <strong>__credits__</strong> = 'Nobody'<br>
153 <strong>__version__</strong> = '1.2.3.4'</td></tr></table><p>
154 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
155 <tr bgcolor="#7799ee">
156 <td colspan=3 valign=bottom> <br>
157 <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr>
159 <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td>
160 <td width="100%%">Benjamin Peterson</td></tr></table><p>
161 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
162 <tr bgcolor="#7799ee">
163 <td colspan=3 valign=bottom> <br>
164 <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr>
166 <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td>
167 <td width="100%%">Nobody</td></tr></table>
171 # output pattern for missing module
172 missing_pattern
= "no Python documentation found for '%s'"
174 # output pattern for module with bad imports
175 badimport_pattern
= "problem in %s - <type 'exceptions.ImportError'>: No module named %s"
177 def run_pydoc(module_name
, *args
):
179 Runs pydoc on the specified module. Returns the stripped
182 cmd
= [sys
.executable
, pydoc
.__file
__, " ".join(args
), module_name
]
184 output
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
).communicate()[0]
185 return output
.strip()
189 def get_pydoc_html(module
):
190 "Returns pydoc generated output as html"
191 doc
= pydoc
.HTMLDoc()
192 output
= doc
.docmodule(module
)
193 loc
= doc
.getdocloc(pydoc_mod
) or ""
195 loc
= "<br><a href=\"" + loc
+ "\">Module Docs</a>"
196 return output
.strip(), loc
198 def get_pydoc_text(module
):
199 "Returns pydoc generated output as text"
200 doc
= pydoc
.TextDoc()
201 loc
= doc
.getdocloc(pydoc_mod
) or ""
203 loc
= "\nMODULE DOCS\n " + loc
+ "\n"
205 output
= doc
.docmodule(module
)
207 # cleanup the extra text formatting that pydoc preforms
208 patt
= re
.compile('\b.')
209 output
= patt
.sub('', output
)
210 return output
.strip(), loc
212 def print_diffs(text1
, text2
):
213 "Prints unified diffs for two texts"
214 lines1
= text1
.splitlines(True)
215 lines2
= text2
.splitlines(True)
216 diffs
= difflib
.unified_diff(lines1
, lines2
, n
=0, fromfile
='expected',
218 print '\n' + ''.join(diffs
)
221 class PyDocDocTest(unittest
.TestCase
):
223 @unittest.skipIf(sys
.flags
.optimize
>= 2,
224 "Docstrings are omitted with -O2 and above")
225 def test_html_doc(self
):
226 result
, doc_loc
= get_pydoc_html(pydoc_mod
)
227 mod_file
= inspect
.getabsfile(pydoc_mod
)
228 if sys
.platform
== 'win32':
230 mod_url
= nturl2path
.pathname2url(mod_file
)
233 expected_html
= expected_html_pattern
% (mod_url
, mod_file
, doc_loc
)
234 if result
!= expected_html
:
235 print_diffs(expected_html
, result
)
236 self
.fail("outputs are not equal, see diff above")
238 @unittest.skipIf(sys
.flags
.optimize
>= 2,
239 "Docstrings are omitted with -O2 and above")
240 def test_text_doc(self
):
241 result
, doc_loc
= get_pydoc_text(pydoc_mod
)
242 expected_text
= expected_text_pattern
% \
243 (inspect
.getabsfile(pydoc_mod
), doc_loc
)
244 if result
!= expected_text
:
245 print_diffs(expected_text
, result
)
246 self
.fail("outputs are not equal, see diff above")
248 def test_issue8225(self
):
249 # Test issue8225 to ensure no doc link appears for xml.etree
250 result
, doc_loc
= get_pydoc_text(xml
.etree
)
251 self
.assertEqual(doc_loc
, "", "MODULE DOCS incorrectly includes a link")
253 def test_not_here(self
):
254 missing_module
= "test.i_am_not_here"
255 result
= run_pydoc(missing_module
)
256 expected
= missing_pattern
% missing_module
257 self
.assertEqual(expected
, result
,
258 "documentation for missing module found")
260 def test_badimport(self
):
261 # This tests the fix for issue 5230, where if pydoc found the module
262 # but the module had an internal import error pydoc would report no doc
264 modname
= 'testmod_xyzzy'
266 ('i_am_not_here', 'i_am_not_here'),
267 ('test.i_am_not_here_either', 'i_am_not_here_either'),
268 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
269 ('i_am_not_here.{}'.format(modname
), 'i_am_not_here.{}'.format(modname
)),
270 ('test.{}'.format(modname
), modname
),
274 def newdirinpath(dir):
276 sys
.path
.insert(0, dir)
281 with
newdirinpath(TESTFN
), EnvironmentVarGuard() as env
:
282 env
['PYTHONPATH'] = TESTFN
283 fullmodname
= os
.path
.join(TESTFN
, modname
)
284 sourcefn
= fullmodname
+ os
.extsep
+ "py"
285 for importstring
, expectedinmsg
in testpairs
:
286 f
= open(sourcefn
, 'w')
287 f
.write("import {}\n".format(importstring
))
290 result
= run_pydoc(modname
)
293 expected
= badimport_pattern
% (modname
, expectedinmsg
)
294 self
.assertEqual(expected
, result
)
296 def test_input_strip(self
):
297 missing_module
= " test.i_am_not_here "
298 result
= run_pydoc(missing_module
)
299 expected
= missing_pattern
% missing_module
.strip()
300 self
.assertEqual(expected
, result
,
301 "white space was not stripped from module name "
302 "or other error output mismatch")
304 def test_stripid(self
):
305 # test with strings, other implementations might have different repr()
306 stripid
= pydoc
.stripid
308 self
.assertEqual(stripid('<function stripid at 0x88dcee4>'),
309 '<function stripid>')
310 self
.assertEqual(stripid('<function stripid at 0x01F65390>'),
311 '<function stripid>')
312 # nothing to strip, return the same text
313 self
.assertEqual(stripid('42'), '42')
314 self
.assertEqual(stripid("<type 'exceptions.Exception'>"),
315 "<type 'exceptions.Exception'>")
318 class TestDescriptions(unittest
.TestCase
):
320 def test_module(self
):
321 # Check that pydocfodder module can be described
322 from test
import pydocfodder
323 doc
= pydoc
.render_doc(pydocfodder
)
324 self
.assertIn("pydocfodder", doc
)
326 def test_classic_class(self
):
327 class C
: "Classic class"
329 self
.assertEqual(pydoc
.describe(C
), 'class C')
330 self
.assertEqual(pydoc
.describe(c
), 'instance of C')
331 expected
= 'instance of C in module %s' % __name__
332 self
.assertIn(expected
, pydoc
.render_doc(c
))
334 def test_class(self
):
335 class C(object): "New-style class"
338 self
.assertEqual(pydoc
.describe(C
), 'class C')
339 self
.assertEqual(pydoc
.describe(c
), 'C')
340 expected
= 'C in module %s object' % __name__
341 self
.assertIn(expected
, pydoc
.render_doc(c
))
345 test
.test_support
.run_unittest(PyDocDocTest
,
348 if __name__
== "__main__":