9 import test
.test_support
11 from test
import pydoc_mod
13 expected_text_pattern
= \
16 test.pydoc_mod - This is a test module for test_pydoc
29 | Methods defined here:
32 | Wow, I have no function!
34 class B(__builtin__.object)
35 | Data descriptors defined here:
38 | dictionary for instance variables (if defined)
41 | list of weak references to the object (if defined)
43 | ----------------------------------------------------------------------
44 | Data and other attributes defined here:
50 This function solves all of the world's problems:
58 __author__ = 'Benjamin Peterson'
59 __credits__ = 'Nobody'
60 __version__ = '1.2.3.4'
72 expected_html_pattern
= \
74 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
75 <tr bgcolor="#7799ee">
76 <td valign=bottom> <br>
77 <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
78 ><td align=right valign=bottom
79 ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
80 <p><tt>This is a test module for test_pydoc</tt></p>
82 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
83 <tr bgcolor="#ee77aa">
84 <td colspan=3 valign=bottom> <br>
85 <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
87 <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td>
88 <td width="100%%"><dl>
89 <dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a>
92 <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
95 <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
98 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
99 <tr bgcolor="#ffc8d8">
100 <td colspan=3 valign=bottom> <br>
101 <font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a></font></td></tr>
103 <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
104 <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr>
106 <td width="100%%">Methods defined here:<br>
107 <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl>
109 </td></tr></table> <p>
110 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
111 <tr bgcolor="#ffc8d8">
112 <td colspan=3 valign=bottom> <br>
113 <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 <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td>
116 <td width="100%%">Data descriptors defined here:<br>
117 <dl><dt><strong>__dict__</strong></dt>
118 <dd><tt>dictionary for instance variables (if defined)</tt></dd>
120 <dl><dt><strong>__weakref__</strong></dt>
121 <dd><tt>list of weak references to the object (if defined)</tt></dd>
124 Data and other attributes defined here:<br>
125 <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
127 </td></tr></table></td></tr></table><p>
128 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
129 <tr bgcolor="#eeaa77">
130 <td colspan=3 valign=bottom> <br>
131 <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
133 <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td>
134 <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>
136 lack of Python<br>
138 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
139 </td></tr></table><p>
140 <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
141 <tr bgcolor="#55aa55">
142 <td colspan=3 valign=bottom> <br>
143 <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
145 <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td>
146 <td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br>
147 <strong>__credits__</strong> = 'Nobody'<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>
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>
161 <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td>
162 <td width="100%%">Nobody</td></tr></table>
166 # output pattern for missing module
167 missing_pattern
= "no Python documentation found for '%s'"
169 def run_pydoc(module_name
, *args
):
171 Runs pydoc on the specified module. Returns the stripped
174 cmd
= [sys
.executable
, pydoc
.__file
__, " ".join(args
), module_name
]
175 output
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
).stdout
.read()
176 return output
.strip()
178 def get_pydoc_html(module
):
179 "Returns pydoc generated output as html"
180 doc
= pydoc
.HTMLDoc()
181 output
= doc
.docmodule(module
)
182 loc
= doc
.getdocloc(pydoc_mod
) or ""
184 loc
= "<br><a href=\"" + loc
+ "\">Module Docs</a>"
185 return output
.strip(), loc
187 def get_pydoc_text(module
):
188 "Returns pydoc generated output as text"
189 doc
= pydoc
.TextDoc()
190 loc
= doc
.getdocloc(pydoc_mod
) or ""
192 loc
= "\nMODULE DOCS\n " + loc
+ "\n"
194 output
= doc
.docmodule(module
)
196 # cleanup the extra text formatting that pydoc preforms
197 patt
= re
.compile('\b.')
198 output
= patt
.sub('', output
)
199 return output
.strip(), loc
201 def print_diffs(text1
, text2
):
202 "Prints unified diffs for two texts"
203 lines1
= text1
.splitlines(True)
204 lines2
= text2
.splitlines(True)
205 diffs
= difflib
.unified_diff(lines1
, lines2
, n
=0, fromfile
='expected',
207 print '\n' + ''.join(diffs
)
210 class PyDocDocTest(unittest
.TestCase
):
212 def test_html_doc(self
):
213 result
, doc_loc
= get_pydoc_html(pydoc_mod
)
214 mod_file
= inspect
.getabsfile(pydoc_mod
)
215 if sys
.platform
== 'win32':
217 mod_url
= nturl2path
.pathname2url(mod_file
)
220 expected_html
= expected_html_pattern
% (mod_url
, mod_file
, doc_loc
)
221 if result
!= expected_html
:
222 print_diffs(expected_html
, result
)
223 self
.fail("outputs are not equal, see diff above")
225 def test_text_doc(self
):
226 result
, doc_loc
= get_pydoc_text(pydoc_mod
)
227 expected_text
= expected_text_pattern
% \
228 (inspect
.getabsfile(pydoc_mod
), doc_loc
)
229 if result
!= expected_text
:
230 print_diffs(expected_text
, result
)
231 self
.fail("outputs are not equal, see diff above")
233 def test_not_here(self
):
234 missing_module
= "test.i_am_not_here"
235 result
= run_pydoc(missing_module
)
236 expected
= missing_pattern
% missing_module
237 self
.assertEqual(expected
, result
,
238 "documentation for missing module found")
241 class TestDescriptions(unittest
.TestCase
):
243 def test_module(self
):
244 # Check that pydocfodder module can be described
245 from test
import pydocfodder
246 doc
= pydoc
.render_doc(pydocfodder
)
247 self
.assert_("pydocfodder" in doc
)
249 def test_classic_class(self
):
250 class C
: "Classic class"
252 self
.assertEqual(pydoc
.describe(C
), 'class C')
253 self
.assertEqual(pydoc
.describe(c
), 'instance of C')
254 expected
= 'instance of C in module %s' % __name__
255 self
.assert_(expected
in pydoc
.render_doc(c
))
257 def test_class(self
):
258 class C(object): "New-style class"
261 self
.assertEqual(pydoc
.describe(C
), 'class C')
262 self
.assertEqual(pydoc
.describe(c
), 'C')
263 expected
= 'C in module %s object' % __name__
264 self
.assert_(expected
in pydoc
.render_doc(c
))
268 test
.test_support
.run_unittest(PyDocDocTest
,
271 if __name__
== "__main__":