"""Test the build process with Text builder with the test root.""" from __future__ import annotations from typing import TYPE_CHECKING import pytest from docutils.utils import column_width from sphinx.writers.text import MAXWIDTH, Cell, Table if TYPE_CHECKING: from typing import Any def with_text_app(*args: Any, **kw: Any) -> pytest.MarkDecorator: return pytest.mark.sphinx(*args, buildername='text', testroot='build-text', **kw) @with_text_app() def test_maxwitdh_with_prefix(app): app.build() result = (app.outdir / 'maxwidth.txt').read_text(encoding='utf8') lines = result.splitlines() line_widths = [column_width(line) for line in lines] assert max(line_widths) < MAXWIDTH assert lines[0].startswith('See also:') assert lines[1].startswith('') assert lines[2].startswith(' ham') assert lines[3].startswith(' ham') assert lines[4] == '' assert lines[5].startswith('* ham') assert lines[6].startswith(' ham') assert lines[7] == '' assert lines[8].startswith('* ham') assert lines[9].startswith(' ham') assert lines[10] == '' assert lines[11].startswith('spam egg') @with_text_app() def test_lineblock(app): # regression test for #1109: need empty line after line block app.build() result = (app.outdir / 'lineblock.txt').read_text(encoding='utf8') expect = ( '* one\n' '\n' ' line-block 1\n' ' line-block 2\n' '\n' 'followed paragraph.\n' ) assert result == expect @with_text_app() def test_nonascii_title_line(app): app.build() result = (app.outdir / 'nonascii_title.txt').read_text(encoding='utf8') expect_underline = '*********' result_underline = result.splitlines()[1].strip() assert result_underline == expect_underline @with_text_app() def test_nonascii_table(app): app.build() result = (app.outdir / 'nonascii_table.txt').read_text(encoding='utf8') lines = [line.strip() for line in result.splitlines() if line.strip()] line_widths = [column_width(line) for line in lines] assert len(set(line_widths)) == 1 # same widths @with_text_app() def test_nonascii_maxwidth(app): app.build() result = (app.outdir / 'nonascii_maxwidth.txt').read_text(encoding='utf8') lines = [line.strip() for line in result.splitlines() if line.strip()] line_widths = [column_width(line) for line in lines] assert max(line_widths) < MAXWIDTH def test_table_builder(): table = Table([6, 6]) table.add_cell(Cell('foo')) table.add_cell(Cell('bar')) table_str = str(table).split('\n') assert table_str[0] == '+--------+--------+' assert table_str[1] == '| foo | bar |' assert table_str[2] == '+--------+--------+' assert repr(table).count('