Added enumlist styles (loweralpha, lowerroman, etc).
[docutils.git] / test / test_utils.py
blob9f0eda54b2d4529a7c0cbb00c372f395e46ec175
1 #! /usr/bin/env python
3 # $Id$
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Test module for utils.py.
9 """
11 import unittest
12 import StringIO
13 import sys
14 from DocutilsTestSupport import utils, nodes
17 class ReporterTests(unittest.TestCase):
19 stream = StringIO.StringIO()
20 reporter = utils.Reporter('test data', 2, 4, stream, 1)
22 def setUp(self):
23 self.stream.seek(0)
24 self.stream.truncate()
26 def test_level0(self):
27 sw = self.reporter.system_message(0, 'debug output')
28 self.assertEquals(sw.pformat(), """\
29 <system_message level="0" source="test data" type="DEBUG">
30 <paragraph>
31 debug output
32 """)
33 self.assertEquals(self.stream.getvalue(),
34 'test data:: (DEBUG/0) debug output\n')
36 def test_level1(self):
37 sw = self.reporter.system_message(1, 'a little reminder')
38 self.assertEquals(sw.pformat(), """\
39 <system_message level="1" source="test data" type="INFO">
40 <paragraph>
41 a little reminder
42 """)
43 self.assertEquals(self.stream.getvalue(), '')
45 def test_level2(self):
46 sw = self.reporter.system_message(2, 'a warning')
47 self.assertEquals(sw.pformat(), """\
48 <system_message level="2" source="test data" type="WARNING">
49 <paragraph>
50 a warning
51 """)
52 self.assertEquals(self.stream.getvalue(),
53 'test data:: (WARNING/2) a warning\n')
55 def test_level3(self):
56 sw = self.reporter.system_message(3, 'an error')
57 self.assertEquals(sw.pformat(), """\
58 <system_message level="3" source="test data" type="ERROR">
59 <paragraph>
60 an error
61 """)
62 self.assertEquals(self.stream.getvalue(),
63 'test data:: (ERROR/3) an error\n')
65 def test_level4(self):
66 self.assertRaises(utils.SystemMessage, self.reporter.system_message, 4,
67 'a severe error, raises an exception')
68 self.assertEquals(self.stream.getvalue(), 'test data:: (SEVERE/4) '
69 'a severe error, raises an exception\n')
72 class QuietReporterTests(unittest.TestCase):
74 stream = StringIO.StringIO()
75 reporter = utils.Reporter('test data', 5, 5, stream, 0)
77 def setUp(self):
78 self.stream.seek(0)
79 self.stream.truncate()
81 def test_debug(self):
82 sw = self.reporter.debug('a debug message')
83 # None because debug is disabled.
84 self.assertEquals(sw, None)
85 self.assertEquals(self.stream.getvalue(), '')
87 def test_info(self):
88 sw = self.reporter.info('an informational message')
89 self.assertEquals(sw.pformat(), """\
90 <system_message level="1" source="test data" type="INFO">
91 <paragraph>
92 an informational message
93 """)
94 self.assertEquals(self.stream.getvalue(), '')
96 def test_warning(self):
97 sw = self.reporter.warning('a warning')
98 self.assertEquals(sw.pformat(), """\
99 <system_message level="2" source="test data" type="WARNING">
100 <paragraph>
101 a warning
102 """)
103 self.assertEquals(self.stream.getvalue(), '')
105 def test_error(self):
106 sw = self.reporter.error('an error')
107 self.assertEquals(sw.pformat(), """\
108 <system_message level="3" source="test data" type="ERROR">
109 <paragraph>
110 an error
111 """)
112 self.assertEquals(self.stream.getvalue(), '')
114 def test_severe(self):
115 sw = self.reporter.severe('a severe error')
116 self.assertEquals(sw.pformat(), """\
117 <system_message level="4" source="test data" type="SEVERE">
118 <paragraph>
119 a severe error
120 """)
121 self.assertEquals(self.stream.getvalue(), '')
124 class NameValueTests(unittest.TestCase):
126 def test_extract_name_value(self):
127 self.assertRaises(utils.NameValueError, utils.extract_name_value,
128 'hello')
129 self.assertRaises(utils.NameValueError, utils.extract_name_value,
130 'hello')
131 self.assertRaises(utils.NameValueError, utils.extract_name_value,
132 '=hello')
133 self.assertRaises(utils.NameValueError, utils.extract_name_value,
134 'hello=')
135 self.assertRaises(utils.NameValueError, utils.extract_name_value,
136 'hello="')
137 self.assertRaises(utils.NameValueError, utils.extract_name_value,
138 'hello="something')
139 self.assertRaises(utils.NameValueError, utils.extract_name_value,
140 'hello="something"else')
141 output = utils.extract_name_value(
142 """att1=val1 att2=val2 att3="value number '3'" att4=val4""")
143 self.assertEquals(output, [('att1', 'val1'), ('att2', 'val2'),
144 ('att3', "value number '3'"),
145 ('att4', 'val4')])
148 class ExtensionOptionTests(unittest.TestCase):
150 optionspec = {'a': int, 'bbb': float, 'cdef': (lambda x: x),
151 'empty': (lambda x: x)}
153 def test_assemble_option_dict(self):
154 input = utils.extract_name_value('a=1 bbb=2.0 cdef=hol%s' % chr(224))
155 self.assertEquals(
156 utils.assemble_option_dict(input, self.optionspec),
157 {'a': 1, 'bbb': 2.0, 'cdef': ('hol%s' % chr(224))})
158 input = utils.extract_name_value('a=1 b=2.0 c=hol%s' % chr(224))
159 self.assertRaises(KeyError, utils.assemble_option_dict,
160 input, self.optionspec)
161 input = utils.extract_name_value('a=1 bbb=two cdef=hol%s' % chr(224))
162 self.assertRaises(ValueError, utils.assemble_option_dict,
163 input, self.optionspec)
165 def test_extract_extension_options(self):
166 field_list = nodes.field_list()
167 field_list += nodes.field(
168 '', nodes.field_name('', 'a'),
169 nodes.field_body('', nodes.paragraph('', '1')))
170 field_list += nodes.field(
171 '', nodes.field_name('', 'bbb'),
172 nodes.field_body('', nodes.paragraph('', '2.0')))
173 field_list += nodes.field(
174 '', nodes.field_name('', 'cdef'),
175 nodes.field_body('', nodes.paragraph('',
176 unicode('hol%s' % chr(224), 'iso-8859-1'))))
177 field_list += nodes.field(
178 '', nodes.field_name('', 'empty'), nodes.field_body())
179 self.assertEquals(
180 utils.extract_extension_options(field_list, self.optionspec),
181 {'a': 1, 'bbb': 2.0,
182 'cdef': unicode('hol%s' % chr(224), 'iso-8859-1'),
183 'empty': None})
184 self.assertRaises(KeyError, utils.extract_extension_options,
185 field_list, {})
186 field_list += nodes.field(
187 '', nodes.field_name('', 'cdef'),
188 nodes.field_body('', nodes.paragraph('', 'one'),
189 nodes.paragraph('', 'two')))
190 self.assertRaises(utils.BadOptionDataError,
191 utils.extract_extension_options,
192 field_list, self.optionspec)
193 field_list[-1] = nodes.field(
194 '', nodes.field_name('', 'cdef bad'),
195 nodes.field_body('', nodes.paragraph('', 'no arguments')))
196 self.assertRaises(utils.BadOptionError,
197 utils.extract_extension_options,
198 field_list, self.optionspec)
199 field_list[-1] = nodes.field(
200 '', nodes.field_name('', 'cdef'),
201 nodes.field_body('', nodes.paragraph('', 'duplicate')))
202 self.assertRaises(utils.DuplicateOptionError,
203 utils.extract_extension_options,
204 field_list, self.optionspec)
205 field_list[-2] = nodes.field(
206 '', nodes.field_name('', 'unkown'),
207 nodes.field_body('', nodes.paragraph('', 'unknown')))
208 self.assertRaises(KeyError, utils.extract_extension_options,
209 field_list, self.optionspec)
212 if __name__ == '__main__':
213 unittest.main()