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