Debian Codegraph @

tests/test_builders/test_build_text.py

raw
1 """Test the build process with Text builder with the test root."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 import pytest
8 from docutils.utils import column_width
9
10 from sphinx.testing.util import SphinxTestApp
11 from sphinx.writers.text import MAXWIDTH, Cell, Table
12
13 if TYPE_CHECKING:
14 from typing import Any
15
16 from sphinx.testing.util import SphinxTestApp
17
18
19 def with_text_app(*args: Any, **kw: Any) -> pytest.MarkDecorator:
20 return pytest.mark.sphinx(*args, buildername='text', testroot='build-text', **kw)
21
22
23 @with_text_app()
24 def test_maxwitdh_with_prefix(app: SphinxTestApp) -> None:
25 app.build()
26 result = (app.outdir / 'maxwidth.txt').read_text(encoding='utf8')
27
28 lines = result.splitlines()
29 line_widths = [column_width(line) for line in lines]
30 assert max(line_widths) < MAXWIDTH
31 assert lines[0].startswith('See also:')
32 assert lines[1].startswith('')
33 assert lines[2].startswith(' ham')
34 assert lines[3].startswith(' ham')
35 assert lines[4] == ''
36 assert lines[5].startswith('* ham')
37 assert lines[6].startswith(' ham')
38 assert lines[7] == ''
39 assert lines[8].startswith('* ham')
40 assert lines[9].startswith(' ham')
41 assert lines[10] == ''
42 assert lines[11].startswith('spam egg')
43
44
45 @with_text_app()
46 def test_lineblock(app: SphinxTestApp) -> None:
47 # regression test for https://github.com/sphinx-doc/sphinx/issues/1109:
48 # need empty line after line block
49 app.build()
50 result = (app.outdir / 'lineblock.txt').read_text(encoding='utf8')
51 expect = '* one\n\n line-block 1\n line-block 2\n\nfollowed paragraph.\n'
52 assert result == expect
53
54
55 @with_text_app()
56 def test_nonascii_title_line(app: SphinxTestApp) -> None:
57 app.build(force_all=True)
58 result = (app.outdir / 'nonascii_title.txt').read_text(encoding='utf8')
59 expect_underline = '*********'
60 result_underline = result.splitlines()[1].strip()
61 assert result_underline == expect_underline
62
63
64 @with_text_app()
65 def test_nonascii_table(app: SphinxTestApp) -> None:
66 app.build()
67 result = (app.outdir / 'nonascii_table.txt').read_text(encoding='utf8')
68 lines = [line.strip() for line in result.splitlines() if line.strip()]
69 line_widths = [column_width(line) for line in lines]
70 assert len(set(line_widths)) == 1 # same widths
71
72
73 @with_text_app()
74 def test_nonascii_maxwidth(app: SphinxTestApp) -> None:
75 app.build()
76 result = (app.outdir / 'nonascii_maxwidth.txt').read_text(encoding='utf8')
77 lines = [line.strip() for line in result.splitlines() if line.strip()]
78 line_widths = [column_width(line) for line in lines]
79 assert max(line_widths) < MAXWIDTH
80
81
82 def test_table_builder() -> None:
83 table = Table([6, 6])
84 table.add_cell(Cell('foo'))
85 table.add_cell(Cell('bar'))
86 table_str = str(table).split('\n')
87 assert table_str[0] == '+--------+--------+'
88 assert table_str[1] == '| foo | bar |'
89 assert table_str[2] == '+--------+--------+'
90 assert repr(table).count('<Cell ') == 2
91
92
93 def test_table_separator() -> None:
94 table = Table([6, 6])
95 table.add_cell(Cell('foo'))
96 table.add_cell(Cell('bar'))
97 table.set_separator()
98 table.add_row()
99 table.add_cell(Cell('FOO'))
100 table.add_cell(Cell('BAR'))
101 table_str = str(table).split('\n')
102 assert table_str[0] == '+--------+--------+'
103 assert table_str[1] == '| foo | bar |'
104 assert table_str[2] == '|========|========|'
105 assert table_str[3] == '| FOO | BAR |'
106 assert table_str[4] == '+--------+--------+'
107 assert repr(table).count('<Cell ') == 4
108
109
110 def test_table_cell() -> None:
111 cell = Cell('Foo bar baz')
112 cell.wrap(3)
113 assert 'Cell' in repr(cell)
114 assert cell.wrapped == ['Foo', 'bar', 'baz']
115
116
117 @with_text_app()
118 def test_table_with_empty_cell(app: SphinxTestApp) -> None:
119 app.build()
120 result = (app.outdir / 'table.txt').read_text(encoding='utf8')
121 lines = [line.strip() for line in result.splitlines() if line.strip()]
122 assert lines[0] == '+-------+-------+'
123 assert lines[1] == '| XXX | XXX |'
124 assert lines[2] == '+-------+-------+'
125 assert lines[3] == '| | XXX |'
126 assert lines[4] == '+-------+-------+'
127 assert lines[5] == '| XXX | |'
128 assert lines[6] == '+-------+-------+'
129
130
131 @with_text_app()
132 def test_table_with_rowspan(app: SphinxTestApp) -> None:
133 app.build()
134 result = (app.outdir / 'table_rowspan.txt').read_text(encoding='utf8')
135 lines = [line.strip() for line in result.splitlines() if line.strip()]
136 assert lines[0] == '+-------+-------+'
137 assert lines[1] == '| XXXXXXXXX |'
138 assert lines[2] == '+-------+-------+'
139 assert lines[3] == '| | XXX |'
140 assert lines[4] == '+-------+-------+'
141 assert lines[5] == '| XXX | |'
142 assert lines[6] == '+-------+-------+'
143
144
145 @with_text_app()
146 def test_table_with_colspan(app: SphinxTestApp) -> None:
147 app.build()
148 result = (app.outdir / 'table_colspan.txt').read_text(encoding='utf8')
149 lines = [line.strip() for line in result.splitlines() if line.strip()]
150 assert lines[0] == '+-------+-------+'
151 assert lines[1] == '| XXX | XXX |'
152 assert lines[2] == '+-------+-------+'
153 assert lines[3] == '| | XXX |'
154 assert lines[4] == '+-------+ |'
155 assert lines[5] == '| XXX | |'
156 assert lines[6] == '+-------+-------+'
157
158
159 @with_text_app()
160 def test_table_with_colspan_left(app: SphinxTestApp) -> None:
161 app.build()
162 result = (app.outdir / 'table_colspan_left.txt').read_text(encoding='utf8')
163 lines = [line.strip() for line in result.splitlines() if line.strip()]
164 assert lines[0] == '+-------+-------+'
165 assert lines[1] == '| XXX | XXX |'
166 assert lines[2] == '+-------+-------+'
167 assert lines[3] == '| XXX | XXX |'
168 assert lines[4] == '| +-------+'
169 assert lines[5] == '| | |'
170 assert lines[6] == '+-------+-------+'
171
172
173 @with_text_app()
174 def test_table_with_colspan_and_rowspan(app: SphinxTestApp) -> None:
175 app.build()
176 result = (app.outdir / 'table_colspan_and_rowspan.txt').read_text(encoding='utf8')
177 lines = [line.strip() for line in result.splitlines() if line.strip()]
178 assert result
179 assert lines[0] == '+-------+-------+-------+'
180 assert lines[1] == '| AAA | BBB |'
181 assert lines[2] == '+-------+-------+ |'
182 assert lines[3] == '| DDD | XXX | |'
183 assert lines[4] == '| +-------+-------+'
184 assert lines[5] == '| | CCC |'
185 assert lines[6] == '+-------+-------+-------+'
186
187
188 @with_text_app()
189 def test_list_items_in_admonition(app: SphinxTestApp) -> None:
190 app.build()
191 result = (app.outdir / 'listitems.txt').read_text(encoding='utf8')
192 lines = [line.rstrip() for line in result.splitlines()]
193 assert lines[0] == 'See also:'
194 assert lines[1] == ''
195 assert lines[2] == ' * item 1'
196 assert lines[3] == ''
197 assert lines[4] == ' * item 2'
198
199
200 @with_text_app()
201 def test_secnums(app: SphinxTestApp) -> None:
202 app.build(force_all=True)
203 index = (app.outdir / 'index.txt').read_text(encoding='utf8')
204 lines = index.splitlines()
205 assert lines[0] == '* 1. Section A'
206 assert lines[1] == ''
207 assert lines[2] == '* 2. Section B'
208 assert lines[3] == ''
209 assert lines[4] == ' * 2.1. Sub Ba'
210 assert lines[5] == ''
211 assert lines[6] == ' * 2.2. Sub Bb'
212 doc2 = (app.outdir / 'doc2.txt').read_text(encoding='utf8')
213 expect = (
214 '2. Section B\n'
215 '************\n'
216 '\n'
217 '\n'
218 '2.1. Sub Ba\n'
219 '===========\n'
220 '\n'
221 '\n'
222 '2.2. Sub Bb\n'
223 '===========\n'
224 )
225 assert doc2 == expect
226
227 app.config.text_secnumber_suffix = ' '
228 app.build(force_all=True)
229 index = (app.outdir / 'index.txt').read_text(encoding='utf8')
230 lines = index.splitlines()
231 assert lines[0] == '* 1 Section A'
232 assert lines[1] == ''
233 assert lines[2] == '* 2 Section B'
234 assert lines[3] == ''
235 assert lines[4] == ' * 2.1 Sub Ba'
236 assert lines[5] == ''
237 assert lines[6] == ' * 2.2 Sub Bb'
238 doc2 = (app.outdir / 'doc2.txt').read_text(encoding='utf8')
239 expect = (
240 '2 Section B\n'
241 '***********\n'
242 '\n'
243 '\n'
244 '2.1 Sub Ba\n'
245 '==========\n'
246 '\n'
247 '\n'
248 '2.2 Sub Bb\n'
249 '==========\n'
250 )
251 assert doc2 == expect
252
253 app.config.text_add_secnumbers = False
254 app.build(force_all=True)
255 index = (app.outdir / 'index.txt').read_text(encoding='utf8')
256 lines = index.splitlines()
257 assert lines[0] == '* Section A'
258 assert lines[1] == ''
259 assert lines[2] == '* Section B'
260 assert lines[3] == ''
261 assert lines[4] == ' * Sub Ba'
262 assert lines[5] == ''
263 assert lines[6] == ' * Sub Bb'
264 doc2 = (app.outdir / 'doc2.txt').read_text(encoding='utf8')
265 expect = 'Section B\n*********\n\n\nSub Ba\n======\n\n\nSub Bb\n======\n'
266 assert doc2 == expect