HTML5: Place code-block line numbers in pseudo-elements.
[docutils.git] / docutils / test / test_settings.py
blob37698f7d62a29cdd6c1e36000fc5b405c39928a8
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # $Id$
5 # Author: David Goodger <goodger@python.org>
6 # Copyright: This module has been placed in the public domain.
8 """
9 Tests of runtime settings.
10 """
11 from __future__ import print_function
13 import sys
14 import os
15 import difflib
16 import pprint
17 import warnings
18 import unittest
19 import DocutilsTestSupport # must be imported before docutils
20 from docutils import frontend, utils
21 from docutils.writers import html4css1, pep_html
22 from docutils.parsers import rst
25 warnings.filterwarnings(action='ignore',
26 category=frontend.ConfigDeprecationWarning)
29 def fixpath(path):
30 return os.path.abspath(os.path.join(*(path.split('/'))))
33 class ConfigFileTests(unittest.TestCase):
35 config_files = {'old': fixpath('data/config_old.txt'),
36 'one': fixpath('data/config_1.txt'),
37 'two': fixpath('data/config_2.txt'),
38 'list': fixpath('data/config_list.txt'),
39 'list2': fixpath('data/config_list_2.txt'),
40 'error': fixpath('data/config_error_handler.txt')}
42 settings = {
43 'old': {u'datestamp': u'%Y-%m-%d %H:%M UTC',
44 u'generator': True,
45 u'no_random': True,
46 u'python_home': u'http://www.python.org',
47 u'source_link': True,
48 'stylesheet': None,
49 u'stylesheet_path': [u'stylesheets/pep.css'],
50 'template': fixpath(u'data/pep-html-template')},
51 'one': {u'datestamp': u'%Y-%m-%d %H:%M UTC',
52 u'generator': True,
53 u'no_random': True,
54 u'python_home': u'http://www.python.org',
55 u'raw_enabled': False,
56 'record_dependencies': utils.DependencyList(),
57 u'source_link': True,
58 'stylesheet': None,
59 u'stylesheet_path': [u'stylesheets/pep.css'],
60 u'tab_width': 8,
61 u'template': fixpath(u'data/pep-html-template'),
62 u'trim_footnote_reference_space': True,
64 'two': {u'footnote_references': u'superscript',
65 u'generator': False,
66 'record_dependencies': utils.DependencyList(),
67 u'stylesheet': None,
68 u'stylesheet_path': [u'test.css'],
69 'trim_footnote_reference_space': None},
70 'list': {u'expose_internals': [u'a', u'b', u'c', u'd', u'e'],
71 u'strip_classes': [u'spam', u'pan', u'fun', u'parrot'],
72 u'strip_elements_with_classes': [u'sugar', u'flour', u'milk',
73 u'safran']},
74 'list2': {u'expose_internals': [u'a', u'b', u'c', u'd', u'e', u'f'],
75 u'strip_classes': [u'spam', u'pan', u'fun', u'parrot',
76 u'ham', u'eggs'],
77 u'strip_elements_with_classes': [u'sugar', u'flour',
78 u'milk', u'safran',
79 u'eggs', u'salt']},
80 'error': {u'error_encoding': u'ascii',
81 u'error_encoding_error_handler': u'strict'},
84 compare = difflib.Differ().compare
85 """Comparison method shared by all tests."""
87 def setUp(self):
88 self.option_parser = frontend.OptionParser(
89 components=(pep_html.Writer, rst.Parser), read_config_files=None)
91 def files_settings(self, *names):
92 settings = frontend.Values()
93 for name in names:
94 settings.update(self.option_parser.get_config_file_settings(
95 self.config_files[name]), self.option_parser)
96 return settings.__dict__
98 def expected_settings(self, *names):
99 expected = {}
100 for name in names:
101 expected.update(self.settings[name])
102 return expected
104 def compare_output(self, result, expected):
105 """`result` and `expected` should both be dicts."""
106 self.assertTrue('record_dependencies' in result)
107 if 'record_dependencies' not in expected:
108 # Delete it if we don't want to test it.
109 del result['record_dependencies']
110 result = pprint.pformat(result) + '\n'
111 expected = pprint.pformat(expected) + '\n'
112 try:
113 self.assertEqual(result, expected)
114 except AssertionError:
115 print('\n%s\n' % (self,), file=sys.stderr)
116 print('-: expected\n+: result', file=sys.stderr)
117 print(''.join(self.compare(expected.splitlines(1),
118 result.splitlines(1))), file=sys.stderr)
119 raise
121 def test_nofiles(self):
122 self.compare_output(self.files_settings(),
123 self.expected_settings())
125 def test_old(self):
126 self.compare_output(self.files_settings('old'),
127 self.expected_settings('old'))
129 def test_one(self):
130 self.compare_output(self.files_settings('one'),
131 self.expected_settings('one'))
133 def test_multiple(self):
134 self.compare_output(self.files_settings('one', 'two'),
135 self.expected_settings('one', 'two'))
137 def test_old_and_new(self):
138 self.compare_output(self.files_settings('old', 'two'),
139 self.expected_settings('old', 'two'))
141 def test_list(self):
142 self.compare_output(self.files_settings('list'),
143 self.expected_settings('list'))
145 def test_list2(self):
146 self.compare_output(self.files_settings('list', 'list2'),
147 self.expected_settings('list2'))
149 def test_error_handler(self):
150 self.compare_output(self.files_settings('error'),
151 self.expected_settings('error'))
154 class ConfigEnvVarFileTests(ConfigFileTests):
157 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
158 environment variable and the standard Docutils config file mechanism.
161 def setUp(self):
162 ConfigFileTests.setUp(self)
163 self.orig_environ = os.environ
164 os.environ = os.environ.copy()
166 def files_settings(self, *names):
167 files = [self.config_files[name] for name in names]
168 os.environ['DOCUTILSCONFIG'] = os.pathsep.join(files)
169 settings = self.option_parser.get_standard_config_settings()
170 return settings.__dict__
172 def tearDown(self):
173 os.environ = self.orig_environ
176 class HelperFunctionsTests(unittest.TestCase):
178 pathdict = {'foo': 'hallo', 'ham': u'h\xE4m', 'spam': u'spam'}
179 keys = ['foo', 'ham']
181 def setUp(self):
182 self.option_parser = frontend.OptionParser(
183 components=(rst.Parser,), read_config_files=None)
185 def test_make_paths_absolute(self):
186 pathdict = self.pathdict.copy()
187 frontend.make_paths_absolute(pathdict, self.keys, base_path='base')
188 self.assertEqual(pathdict['foo'], os.path.abspath('base/hallo'))
189 self.assertEqual(pathdict['ham'], os.path.abspath(u'base/h\xE4m'))
190 # not touched, because key not in keys:
191 self.assertEqual(pathdict['spam'], u'spam')
193 def test_make_paths_absolute_cwd(self):
194 # With base_path None, the cwd is used as base path.
195 # Settings values may-be `unicode` instances, therefore
196 # os.getcwdu() is used and the converted path is a unicode instance:
197 pathdict = self.pathdict.copy()
198 frontend.make_paths_absolute(pathdict, self.keys)
199 self.assertEqual(pathdict['foo'], os.path.abspath(u'hallo'))
200 self.assertEqual(pathdict['ham'], os.path.abspath(u'h\xE4m'))
201 # not touched, because key not in keys:
202 self.assertEqual(pathdict['spam'], u'spam')
204 boolean_settings = (
205 (True, True ),
206 ('1', True ),
207 (u'on', True ),
208 ('yes', True ),
209 (u'true', True ),
210 (u'0', False ),
211 ('off', False ),
212 (u'no', False ),
213 ('false', False ),
215 def test_validate_boolean(self):
216 for t in self.boolean_settings:
217 self.assertEqual(
218 frontend.validate_boolean(None, t[0], self.option_parser),
219 t[1])
221 def test_validate_ternary(self):
222 tests = (
223 ('500V', '500V'),
224 (u'parrot', u'parrot'),
226 for t in self.boolean_settings + tests:
227 self.assertEqual(
228 frontend.validate_ternary(None, t[0], self.option_parser),
229 t[1])
231 def test_validate_colon_separated_string_list(self):
232 tests = (
233 (u'a', ['a',] ),
234 ('a', ['a',] ),
235 (u'a:b', ['a', 'b'] ),
236 ('a:b', ['a', 'b'] ),
237 ([u'a',], ['a',] ),
238 ([u'a', u'b:c'], ['a', 'b', 'c'] ),
240 for t in tests:
241 self.assertEqual(
242 frontend.validate_colon_separated_string_list(None, t[0], None),
243 t[1])
245 def test_validate_comma_separated_list(self):
246 tests = (
247 (u'a', ['a',] ),
248 ('a', ['a',] ),
249 (u'a,b', ['a', 'b'] ),
250 ('a,b', ['a', 'b'] ),
251 ([u'a',], ['a',] ),
252 ([u'a', u'b,c'], ['a', 'b', 'c'] ),
253 (['a', 'b,c'], ['a', 'b', 'c'] ),
255 for t in tests:
256 self.assertEqual(
257 frontend.validate_comma_separated_list(None, t[0], None),
258 t[1])
260 def test_validate_url_trailing_slash(self):
261 tests = (
262 ('', './' ),
263 (None, './' ),
264 (u'http://example.org', u'http://example.org/' ),
265 ('http://example.org/', 'http://example.org/' ),
267 for t in tests:
268 self.assertEqual(
269 frontend.validate_url_trailing_slash(None, t[0], None),
270 t[1])
272 def test_validate_smartquotes_locales(self):
273 tests = (
274 ('en:ssvv', [('en', 'ssvv')]),
275 (u'sd:«»°°', [(u'sd', u'«»°°')]),
276 ([('sd', u'«»°°'), u'ds:°°«»'], [('sd', u'«»°°'),
277 ('ds', u'°°«»')]),
278 (u'frs:« : »:((:))', [(u'frs', [u'« ', u' »',
279 u'((', u'))'])]),
281 for t in tests:
282 self.assertEqual(
283 frontend.validate_smartquotes_locales(None, t[0], None),
284 t[1])
288 if __name__ == '__main__':
289 unittest.main()