python 2.3 alias unittest.TestCase.assertTrue (possibly reverted on next release)
[docutils/kirr.git] / docutils / test / test_settings.py
blobfa5e8b34bd3d81a068ef1e2967f6f7d439ac3ff8
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 Tests of runtime settings.
9 """
11 import sys
12 import os
13 import difflib
14 import pprint
15 import warnings
16 import unittest
17 import DocutilsTestSupport # must be imported before docutils
18 from docutils import frontend, utils
19 from docutils.writers import html4css1, pep_html
20 from docutils.parsers import rst
23 warnings.filterwarnings(action='ignore',
24 category=frontend.ConfigDeprecationWarning)
26 # python 2.3
27 if not hasattr(unittest.TestCase, "assertTrue"):
28 # HACK? this changes TestCase, fixes the problem for tests executing afterwards.
29 # this tests break if run alone
30 unittest.TestCase.assertTrue = unittest.TestCase.failUnless
32 def fixpath(path):
33 return os.path.abspath(os.path.join(*(path.split('/'))))
36 class ConfigFileTests(unittest.TestCase):
38 config_files = {'old': fixpath('data/config_old.txt'),
39 'one': fixpath('data/config_1.txt'),
40 'two': fixpath('data/config_2.txt'),
41 'list': fixpath('data/config_list.txt'),
42 'list2': fixpath('data/config_list_2.txt'),
43 'error': fixpath('data/config_error_handler.txt')}
45 settings = {
46 'old': {u'datestamp': u'%Y-%m-%d %H:%M UTC',
47 u'generator': 1,
48 u'no_random': 1,
49 u'python_home': u'http://www.python.org',
50 u'source_link': 1,
51 'stylesheet': None,
52 u'stylesheet_path': fixpath(u'data/stylesheets/pep.css'),
53 'template': fixpath(u'data/pep-html-template')},
54 'one': {u'datestamp': u'%Y-%m-%d %H:%M UTC',
55 u'generator': 1,
56 u'no_random': 1,
57 u'python_home': u'http://www.python.org',
58 u'raw_enabled': 0,
59 'record_dependencies': utils.DependencyList(),
60 u'source_link': 1,
61 'stylesheet': None,
62 u'stylesheet_path': fixpath(u'data/stylesheets/pep.css'),
63 u'tab_width': 8,
64 u'template': fixpath(u'data/pep-html-template'),
65 u'trim_footnote_reference_space': 1},
66 'two': {u'footnote_references': u'superscript',
67 u'generator': 0,
68 'record_dependencies': utils.DependencyList(),
69 u'stylesheet': None,
70 u'stylesheet_path': fixpath(u'data/test.css'),
71 'trim_footnote_reference_space': None},
72 'list': {u'expose_internals': [u'a', u'b', u'c', u'd', u'e'],
73 u'strip_classes': [u'spam', u'pan', u'fun', u'parrot'],
74 u'strip_elements_with_classes': [u'sugar', u'flour', u'milk',
75 u'safran']},
76 'list2': {u'expose_internals': [u'a', u'b', u'c', u'd', u'e', u'f'],
77 u'strip_classes': [u'spam', u'pan', u'fun', u'parrot',
78 u'ham', u'eggs'],
79 u'strip_elements_with_classes': [u'sugar', u'flour',
80 u'milk', u'safran',
81 u'eggs', u'salt']},
82 'error': {u'error_encoding': u'ascii',
83 u'error_encoding_error_handler': u'strict'},
86 compare = difflib.Differ().compare
87 """Comparison method shared by all tests."""
89 def setUp(self):
90 self.option_parser = frontend.OptionParser(
91 components=(pep_html.Writer, rst.Parser), read_config_files=None)
93 def files_settings(self, *names):
94 settings = frontend.Values()
95 for name in names:
96 settings.update(self.option_parser.get_config_file_settings(
97 self.config_files[name]), self.option_parser)
98 return settings.__dict__
100 def expected_settings(self, *names):
101 expected = {}
102 for name in names:
103 expected.update(self.settings[name])
104 return expected
106 def compare_output(self, result, expected):
107 """`result` and `expected` should both be dicts."""
108 self.assertTrue('record_dependencies' in result)
109 if 'record_dependencies' not in expected:
110 # Delete it if we don't want to test it.
111 del result['record_dependencies']
112 result = pprint.pformat(result) + '\n'
113 expected = pprint.pformat(expected) + '\n'
114 try:
115 self.assertEqual(result, expected)
116 except AssertionError:
117 print >>sys.stderr, '\n%s\n' % (self,)
118 print >>sys.stderr, '-: expected\n+: result'
119 print >>sys.stderr, ''.join(self.compare(expected.splitlines(1),
120 result.splitlines(1)))
121 raise
123 def test_nofiles(self):
124 self.compare_output(self.files_settings(),
125 self.expected_settings())
127 def test_old(self):
128 self.compare_output(self.files_settings('old'),
129 self.expected_settings('old'))
131 def test_one(self):
132 self.compare_output(self.files_settings('one'),
133 self.expected_settings('one'))
135 def test_multiple(self):
136 self.compare_output(self.files_settings('one', 'two'),
137 self.expected_settings('one', 'two'))
139 def test_old_and_new(self):
140 self.compare_output(self.files_settings('old', 'two'),
141 self.expected_settings('old', 'two'))
143 def test_list(self):
144 self.compare_output(self.files_settings('list'),
145 self.expected_settings('list'))
147 def test_list2(self):
148 self.compare_output(self.files_settings('list', 'list2'),
149 self.expected_settings('list2'))
151 def test_error_handler(self):
152 self.compare_output(self.files_settings('error'),
153 self.expected_settings('error'))
156 class ConfigEnvVarFileTests(ConfigFileTests):
159 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
160 environment variable and the standard Docutils config file mechanism.
163 def setUp(self):
164 ConfigFileTests.setUp(self)
165 self.orig_environ = os.environ
166 os.environ = os.environ.copy()
168 def files_settings(self, *names):
169 files = [self.config_files[name] for name in names]
170 os.environ['DOCUTILSCONFIG'] = os.pathsep.join(files)
171 settings = self.option_parser.get_standard_config_settings()
172 return settings.__dict__
174 def tearDown(self):
175 os.environ = self.orig_environ
178 class HelperFunctionsTests(unittest.TestCase):
180 pathdict = {'foo': 'hallo', 'ham': u'h\xE4m', 'spam': u'spam'}
181 keys = ['foo', 'ham']
183 def test_make_paths_absolute(self):
184 pathdict = self.pathdict.copy()
185 frontend.make_paths_absolute(pathdict, self.keys, base_path='base')
186 self.assertEqual(pathdict['foo'], os.path.abspath('base/hallo'))
187 self.assertEqual(pathdict['ham'], os.path.abspath(u'base/h\xE4m'))
188 # not touched, because key not in keys:
189 self.assertEqual(pathdict['spam'], u'spam')
191 def test_make_paths_absolute_cwd(self):
192 # With base_path None, the cwd is used as base path.
193 # Settings values may-be `unicode` instances, therefore
194 # os.getcwdu() is used and the converted path is a unicode instance:
195 pathdict = self.pathdict.copy()
196 frontend.make_paths_absolute(pathdict, self.keys)
197 self.assertEqual(pathdict['foo'], os.path.abspath(u'hallo'))
198 self.assertEqual(pathdict['ham'], os.path.abspath(u'h\xE4m'))
199 # not touched, because key not in keys:
200 self.assertEqual(pathdict['spam'], u'spam')
202 if __name__ == '__main__':
203 unittest.main()