Debian Codegraph @

test/test_writers/test_latex2e_misc.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # $Id: test_latex2e_misc.py 10003 2024-12-18 22:03:08Z milde $
3 # Author: Günter Milde
4 # Maintainer: [email protected]
5 # :Copyright: 2020 Günter Milde,
6 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
7 #
8 # Copying and distribution of this file, with or without modification,
9 # are permitted in any medium without royalty provided the copyright
10 # notice and this notice are preserved.
11 # This file is offered as-is, without any warranty.
12 #
13 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
14
15 """
16 Miscellaneous LaTeX writer tests.
17 """
18
19 from pathlib import Path
20 import sys
21 import unittest
22
23
24 if __name__ == '__main__':
25 # prepend the "docutils root" to the Python library path
26 # so we import the local `docutils` package.
27 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
28
29 from docutils import core
30 from docutils.writers import latex2e
31
32 # TEST_ROOT is ./test/ from the docutils root
33 TEST_ROOT = Path(__file__).parents[1]
34 DATA_ROOT = TEST_ROOT / 'data'
35
36 sample_toc = """\
37 .. contents:: TOC
38
39 foo
40 ---
41
42 bar
43 ---
44
45 """
46
47 sample_multiterm = f"""\
48 .. include:: {DATA_ROOT}/multiple-term-definition.xml
49 :parser: xml
50 """
51 expected_multiterm = """
52 \\begin{description}
53 \\item[{New in Docutils 0.22}] \n\
54 A definition list item may contain several
55 terms with optional classifier(s).
56
57 However, there is currently no corresponding
58 reStructuredText syntax.
59
60 \\item[{term 2a}] \n\
61 \\item[{term 2b}] \n\
62 definition 2
63
64 \\item[{term 3a}] (\\textbf{classifier 3a})
65 (\\textbf{classifier 3aa})
66 \\item[{term 3b}] (\\textbf{classifier 3b})
67 definition 3
68 \\end{description}
69 """
70
71
72 class PublishTestCase(unittest.TestCase):
73 maxDiff = None
74
75 settings = {'_disable_config': True,
76 # avoid latex writer future warnings:
77 'use_latex_citations': False,
78 'legacy_column_widths': False,
79 }
80
81 def test_publish_from_doctree(self):
82 """Ignore the Docutils-generated ToC when ``use_latex_toc`` is True.
83
84 (This did happen when publishing from a doctree.)
85 """
86 settings = self.settings.copy()
87 settings['output_encoding'] = 'unicode'
88 settings['warning_stream'] = '' # don't warn for missing ToC details
89 doctree = core.publish_doctree(sample_toc,
90 settings_overrides=settings)
91 result = core.publish_from_doctree(doctree,
92 writer=latex2e.Writer(),
93 settings_overrides=settings)
94 self.assertNotIn(r'\item \hyperref[foo]{foo}', result)
95 self.assertIn(r'\tableofcontents', result)
96
97
98 class WarningsTestCase(unittest.TestCase):
99
100 def test_future_warnings(self):
101 """Warn about changing defaults."""
102 # Warn only if not set (uncommenting should make test fail):
103 settings = {'_disable_config': True,
104 # 'use_latex_citations': False,
105 # 'legacy_column_widths': True,
106 'output_encoding': 'unicode',
107 }
108 with self.assertWarnsRegex(FutureWarning,
109 '"legacy_column_widths" will change'):
110 core.publish_string('warnings test', writer=latex2e.Writer(),
111 settings_overrides=settings)
112 with self.assertWarnsRegex(FutureWarning,
113 '"use_latex_citations" will change'):
114 core.publish_string('warnings test', writer=latex2e.Writer(),
115 settings_overrides=settings)
116
117 def test_deprecation_warnings(self):
118 with self.assertWarnsRegex(DeprecationWarning, 'will be removed'):
119 latex2e.SortableDict(deprecated=True)
120
121
122 if __name__ == '__main__':
123 unittest.main()