1 # -*- coding: utf-8 -*-
5 # Author: David Goodger <goodger@python.org>
6 # Copyright: This module has been placed in the public domain.
9 Test module for utils/__init__.py.
14 from DocutilsTestSupport
import utils
, nodes
16 from io
import StringIO
17 except ImportError: # io is new in Python 2.6
18 from StringIO
import StringIO
21 class ReporterTests(unittest
.TestCase
):
24 reporter
= utils
.Reporter('test data', 2, 4, stream
, 1)
28 self
.stream
.truncate()
30 def test_level0(self
):
31 sw
= self
.reporter
.system_message(0, 'debug output')
32 self
.assertEquals(sw
.pformat(), """\
33 <system_message level="0" source="test data" type="DEBUG">
37 self
.assertEquals(self
.stream
.getvalue(),
38 'test data:: (DEBUG/0) debug output\n')
40 def test_level1(self
):
41 sw
= self
.reporter
.system_message(1, 'a little reminder')
42 self
.assertEquals(sw
.pformat(), """\
43 <system_message level="1" source="test data" type="INFO">
47 self
.assertEquals(self
.stream
.getvalue(), '')
49 def test_level2(self
):
50 sw
= self
.reporter
.system_message(2, 'a warning')
51 self
.assertEquals(sw
.pformat(), """\
52 <system_message level="2" source="test data" type="WARNING">
56 self
.assertEquals(self
.stream
.getvalue(),
57 'test data:: (WARNING/2) a warning\n')
59 def test_level3(self
):
60 sw
= self
.reporter
.system_message(3, 'an error')
61 self
.assertEquals(sw
.pformat(), """\
62 <system_message level="3" source="test data" type="ERROR">
66 self
.assertEquals(self
.stream
.getvalue(),
67 'test data:: (ERROR/3) an error\n')
69 def test_level4(self
):
70 self
.assertRaises(utils
.SystemMessage
, self
.reporter
.system_message
, 4,
71 'a severe error, raises an exception')
72 self
.assertEquals(self
.stream
.getvalue(), 'test data:: (SEVERE/4) '
73 'a severe error, raises an exception\n')
76 def test_unicode_message(self
):
77 sw
= self
.reporter
.system_message(0, u
'mesidʒ')
78 self
.assertEquals(sw
.pformat(), u
"""\
79 <system_message level="0" source="test data" type="DEBUG">
84 def test_unicode_message_from_exception(self
):
85 """Workaround for Python < 2.6 bug:
86 unicode(<exception instance>) uses __str__
87 and hence fails with unicode message"""
89 raise Exception(u
'mesidʒ')
90 except Exception, err
:
91 sw
= self
.reporter
.system_message(0, err
)
92 self
.assertEquals(sw
.pformat(), u
"""\
93 <system_message level="0" source="test data" type="DEBUG">
98 class QuietReporterTests(unittest
.TestCase
):
101 reporter
= utils
.Reporter('test data', 5, 5, stream
, 0)
105 self
.stream
.truncate()
107 def test_debug(self
):
108 sw
= self
.reporter
.debug('a debug message')
109 # None because debug is disabled.
110 self
.assertEquals(sw
, None)
111 self
.assertEquals(self
.stream
.getvalue(), '')
114 sw
= self
.reporter
.info('an informational message')
115 self
.assertEquals(sw
.pformat(), """\
116 <system_message level="1" source="test data" type="INFO">
118 an informational message
120 self
.assertEquals(self
.stream
.getvalue(), '')
122 def test_warning(self
):
123 sw
= self
.reporter
.warning('a warning')
124 self
.assertEquals(sw
.pformat(), """\
125 <system_message level="2" source="test data" type="WARNING">
129 self
.assertEquals(self
.stream
.getvalue(), '')
131 def test_error(self
):
132 sw
= self
.reporter
.error('an error')
133 self
.assertEquals(sw
.pformat(), """\
134 <system_message level="3" source="test data" type="ERROR">
138 self
.assertEquals(self
.stream
.getvalue(), '')
140 def test_severe(self
):
141 sw
= self
.reporter
.severe('a severe error')
142 self
.assertEquals(sw
.pformat(), """\
143 <system_message level="4" source="test data" type="SEVERE">
147 self
.assertEquals(self
.stream
.getvalue(), '')
150 class NameValueTests(unittest
.TestCase
):
152 def test_extract_name_value(self
):
153 self
.assertRaises(utils
.NameValueError
, utils
.extract_name_value
,
155 self
.assertRaises(utils
.NameValueError
, utils
.extract_name_value
,
157 self
.assertRaises(utils
.NameValueError
, utils
.extract_name_value
,
159 self
.assertRaises(utils
.NameValueError
, utils
.extract_name_value
,
161 self
.assertRaises(utils
.NameValueError
, utils
.extract_name_value
,
163 self
.assertRaises(utils
.NameValueError
, utils
.extract_name_value
,
165 self
.assertRaises(utils
.NameValueError
, utils
.extract_name_value
,
166 'hello="something"else')
167 output
= utils
.extract_name_value(
168 """att1=val1 att2=val2 att3="value number '3'" att4=val4""")
169 self
.assertEquals(output
, [('att1', 'val1'), ('att2', 'val2'),
170 ('att3', "value number '3'"),
174 class ExtensionOptionTests(unittest
.TestCase
):
176 optionspec
= {'a': int, 'bbb': float, 'cdef': (lambda x
: x
),
177 'empty': (lambda x
: x
)}
179 def test_assemble_option_dict(self
):
180 input = utils
.extract_name_value('a=1 bbb=2.0 cdef=hol%s' % chr(224))
182 utils
.assemble_option_dict(input, self
.optionspec
),
183 {'a': 1, 'bbb': 2.0, 'cdef': ('hol%s' % chr(224))})
184 input = utils
.extract_name_value('a=1 b=2.0 c=hol%s' % chr(224))
185 self
.assertRaises(KeyError, utils
.assemble_option_dict
,
186 input, self
.optionspec
)
187 input = utils
.extract_name_value('a=1 bbb=two cdef=hol%s' % chr(224))
188 self
.assertRaises(ValueError, utils
.assemble_option_dict
,
189 input, self
.optionspec
)
191 def test_extract_extension_options(self
):
192 field_list
= nodes
.field_list()
193 field_list
+= nodes
.field(
194 '', nodes
.field_name('', 'a'),
195 nodes
.field_body('', nodes
.paragraph('', '1')))
196 field_list
+= nodes
.field(
197 '', nodes
.field_name('', 'bbb'),
198 nodes
.field_body('', nodes
.paragraph('', '2.0')))
199 field_list
+= nodes
.field(
200 '', nodes
.field_name('', 'cdef'),
201 nodes
.field_body('', nodes
.paragraph('', u
'hol\u00e0')))
202 field_list
+= nodes
.field(
203 '', nodes
.field_name('', 'empty'), nodes
.field_body())
205 utils
.extract_extension_options(field_list
, self
.optionspec
),
207 'cdef': u
'hol\u00e0',
209 self
.assertRaises(KeyError, utils
.extract_extension_options
,
211 field_list
+= nodes
.field(
212 '', nodes
.field_name('', 'cdef'),
213 nodes
.field_body('', nodes
.paragraph('', 'one'),
214 nodes
.paragraph('', 'two')))
215 self
.assertRaises(utils
.BadOptionDataError
,
216 utils
.extract_extension_options
,
217 field_list
, self
.optionspec
)
218 field_list
[-1] = nodes
.field(
219 '', nodes
.field_name('', 'cdef bad'),
220 nodes
.field_body('', nodes
.paragraph('', 'no arguments')))
221 self
.assertRaises(utils
.BadOptionError
,
222 utils
.extract_extension_options
,
223 field_list
, self
.optionspec
)
224 field_list
[-1] = nodes
.field(
225 '', nodes
.field_name('', 'cdef'),
226 nodes
.field_body('', nodes
.paragraph('', 'duplicate')))
227 self
.assertRaises(utils
.DuplicateOptionError
,
228 utils
.extract_extension_options
,
229 field_list
, self
.optionspec
)
230 field_list
[-2] = nodes
.field(
231 '', nodes
.field_name('', 'unkown'),
232 nodes
.field_body('', nodes
.paragraph('', 'unknown')))
233 self
.assertRaises(KeyError, utils
.extract_extension_options
,
234 field_list
, self
.optionspec
)
237 class HelperFunctionsTests(unittest
.TestCase
):
239 def test_normalize_language_tag(self
):
240 self
.assertEquals(utils
.normalize_language_tag('de'), ['de'])
241 self
.assertEquals(utils
.normalize_language_tag('de-AT'),
243 self
.assertEquals(utils
.normalize_language_tag('de-AT-1901'),
244 ['de_at_1901', 'de_at', 'de_1901', 'de'])
245 self
.assertEquals(utils
.normalize_language_tag('de-AT-1901-frak'),
246 ['de_at_1901_frak', 'de_at_1901', 'de_at_frak',
247 'de_1901_frak', 'de_at', 'de_1901', 'de_frak', 'de'])
249 def test_column_width(self
):
250 self
.assertEquals(utils
.column_width(u
'de'), 2)
251 self
.assertEquals(utils
.column_width(u
'dâ'), 2) # pre-composed
252 self
.assertEquals(utils
.column_width(u
'dâ'), 2) # combining
255 if __name__
== '__main__':