|
| 1 | +"""Test the LaTeX writer""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | +from docutils.utils import column_width |
| 7 | + |
| 8 | +from sphinx.writers.text import TextWrapper |
| 9 | + |
| 10 | +find_break_end = TextWrapper._find_break_end |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + # glyph of column width 0 |
| 15 | + 'glyph', |
| 16 | + ['', '\N{COMBINING TILDE}'], |
| 17 | +) |
| 18 | +def test_text_wrapper_break_phantom_symbol(glyph: str) -> None: |
| 19 | + assert column_width(glyph) == 0 |
| 20 | + glyph_length = len(glyph) |
| 21 | + |
| 22 | + for n in range(1, 5): |
| 23 | + # Since the glyph has length 0 and column width 0, |
| 24 | + # we can always take the entire glpyh. |
| 25 | + assert find_break_end(glyph, n) == glyph_length |
| 26 | + for m in range(1, 5): |
| 27 | + # The multiplied glyph may have non-zero length |
| 28 | + # but its column width will always be 0, so we |
| 29 | + # take the entire glyph again. |
| 30 | + assert find_break_end(m * glyph, n) == m * glyph_length |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize( |
| 34 | + ('text', 'colwidth'), |
| 35 | + [ |
| 36 | + # Glyph of length 1 and column width 1 |
| 37 | + ('X', 1), |
| 38 | + # Glyph of length 1 and column width 2 |
| 39 | + ('\N{CJK UNIFIED IDEOGRAPH-65E5}', 2), |
| 40 | + # Glyph of length 2 and column width 1 |
| 41 | + ('\N{COMBINING TILDE}X', 1), |
| 42 | + # Glyph of length 2 and column width 2 |
| 43 | + ('\N{COMBINING TILDE}\N{CJK UNIFIED IDEOGRAPH-65E5}', 2), |
| 44 | + # Glyph of length 3 and column width 1 |
| 45 | + ('\N{COMBINING TILDE}\N{COMBINING BREVE}X', 1), |
| 46 | + ], |
| 47 | +) |
| 48 | +def test_text_wrapper_break_visible_symbol(text: str, colwidth: int) -> None: |
| 49 | + assert column_width(text) == colwidth |
| 50 | + for n in range(1, 5): |
| 51 | + end = find_break_end(text, n) |
| 52 | + assert column_width(text[:end]) <= n |
| 53 | + for m in range(2, 5): |
| 54 | + m_text = m * text |
| 55 | + end = find_break_end(m_text, n) |
| 56 | + assert column_width(m_text[:end]) <= n |
| 57 | + assert end == m * len(text) or column_width(m_text[: end + 1]) > n |
| 58 | + |
| 59 | + |
| 60 | +def test_text_wrapper_break_stop_after_combining_symbols() -> None: |
| 61 | + tilde = '\N{COMBINING TILDE}' |
| 62 | + multi = '\N{CJK UNIFIED IDEOGRAPH-65E5}' |
| 63 | + |
| 64 | + head = tilde + tilde + '....' |
| 65 | + tail = multi + tilde + tilde |
| 66 | + text = head + tail |
| 67 | + assert find_break_end(head + tail, column_width(head)) == len(head) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + ('text', 'results'), |
| 72 | + [ |
| 73 | + ('Hello', {1: list('Hello'), 2: ['He', 'll', 'o']}), |
| 74 | + ( |
| 75 | + 'Hello a\N{CJK UNIFIED IDEOGRAPH-65E5}ab!', |
| 76 | + { |
| 77 | + 1: list('Helloa\N{CJK UNIFIED IDEOGRAPH-65E5}ab!'), |
| 78 | + 2: ['He', 'll', 'o', 'a', '\N{CJK UNIFIED IDEOGRAPH-65E5}', 'ab', '!'], |
| 79 | + 3: ['Hel', 'lo', 'a\N{CJK UNIFIED IDEOGRAPH-65E5}', 'ab!'], |
| 80 | + }, |
| 81 | + ), |
| 82 | + ( |
| 83 | + 'ab c\N{COMBINING TILDE}def', |
| 84 | + { |
| 85 | + 1: ['a', 'b', 'c\N{COMBINING TILDE}', 'd', 'e', 'f'], |
| 86 | + 2: ['ab', 'c\N{COMBINING TILDE}d', 'ef'], |
| 87 | + 3: ['ab ', 'c\N{COMBINING TILDE}de', 'f'], |
| 88 | + }, |
| 89 | + ), |
| 90 | + ( |
| 91 | + 'abc\N{COMBINING TILDE}\N{CJK UNIFIED IDEOGRAPH-65E5}def', |
| 92 | + { |
| 93 | + 1: [ |
| 94 | + 'a', |
| 95 | + 'b', |
| 96 | + 'c\N{COMBINING TILDE}', |
| 97 | + '\N{CJK UNIFIED IDEOGRAPH-65E5}', |
| 98 | + 'd', |
| 99 | + 'e', |
| 100 | + 'f', |
| 101 | + ], |
| 102 | + 2: [ |
| 103 | + 'ab', |
| 104 | + 'c\N{COMBINING TILDE}', |
| 105 | + '\N{CJK UNIFIED IDEOGRAPH-65E5}', |
| 106 | + 'de', |
| 107 | + 'f', |
| 108 | + ], |
| 109 | + 3: ['abc\N{COMBINING TILDE}', '\N{CJK UNIFIED IDEOGRAPH-65E5}', 'def'], |
| 110 | + }, |
| 111 | + ), |
| 112 | + ( |
| 113 | + 'abc\N{COMBINING TILDE}\N{COMBINING BREVE}def', |
| 114 | + { |
| 115 | + 1: ['a', 'b', 'c\N{COMBINING TILDE}\N{COMBINING BREVE}', 'd', 'e', 'f'], |
| 116 | + 2: ['ab', 'c\N{COMBINING TILDE}\N{COMBINING BREVE}d', 'ef'], |
| 117 | + 3: ['abc\N{COMBINING TILDE}\N{COMBINING BREVE}', 'def'], |
| 118 | + }, |
| 119 | + ), |
| 120 | + ], |
| 121 | +) |
| 122 | +def test_text_wrapper(text: str, results: dict[int, list[str]]) -> None: |
| 123 | + for width, expected in results.items(): |
| 124 | + w = TextWrapper(width=width, drop_whitespace=True) |
| 125 | + assert w.wrap(text) == expected |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.parametrize( |
| 129 | + ('text', 'results'), |
| 130 | + [ |
| 131 | + ('Hello', {1: list('Hello'), 2: ['He', 'll', 'o']}), |
| 132 | + ( |
| 133 | + 'Hello a\N{CJK UNIFIED IDEOGRAPH-65E5}ab!', |
| 134 | + { |
| 135 | + 1: list('Hello a\N{CJK UNIFIED IDEOGRAPH-65E5}ab!'), |
| 136 | + 2: ['He', 'll', 'o ', 'a', '\N{CJK UNIFIED IDEOGRAPH-65E5}', 'ab', '!'], |
| 137 | + 3: ['Hel', 'lo ', 'a\N{CJK UNIFIED IDEOGRAPH-65E5}', 'ab!'], |
| 138 | + }, |
| 139 | + ), |
| 140 | + ( |
| 141 | + 'ab c\N{COMBINING TILDE}def', |
| 142 | + { |
| 143 | + 1: ['a', 'b', ' ', 'c\N{COMBINING TILDE}', 'd', 'e', 'f'], |
| 144 | + 2: ['ab', ' c\N{COMBINING TILDE}', 'de', 'f'], |
| 145 | + 3: ['ab ', 'c\N{COMBINING TILDE}de', 'f'], |
| 146 | + }, |
| 147 | + ), |
| 148 | + ( |
| 149 | + 'abc\N{COMBINING TILDE}\N{CJK UNIFIED IDEOGRAPH-65E5}def', |
| 150 | + { |
| 151 | + 1: [ |
| 152 | + 'a', |
| 153 | + 'b', |
| 154 | + 'c\N{COMBINING TILDE}', |
| 155 | + '\N{CJK UNIFIED IDEOGRAPH-65E5}', |
| 156 | + 'd', |
| 157 | + 'e', |
| 158 | + 'f', |
| 159 | + ], |
| 160 | + 2: [ |
| 161 | + 'ab', |
| 162 | + 'c\N{COMBINING TILDE}', |
| 163 | + '\N{CJK UNIFIED IDEOGRAPH-65E5}', |
| 164 | + 'de', |
| 165 | + 'f', |
| 166 | + ], |
| 167 | + 3: ['abc\N{COMBINING TILDE}', '\N{CJK UNIFIED IDEOGRAPH-65E5}', 'def'], |
| 168 | + }, |
| 169 | + ), |
| 170 | + ( |
| 171 | + 'abc\N{COMBINING TILDE}\N{COMBINING BREVE}def', |
| 172 | + { |
| 173 | + 1: ['a', 'b', 'c\N{COMBINING TILDE}\N{COMBINING BREVE}', 'd', 'e', 'f'], |
| 174 | + 2: ['ab', 'c\N{COMBINING TILDE}\N{COMBINING BREVE}d', 'ef'], |
| 175 | + 3: ['abc\N{COMBINING TILDE}\N{COMBINING BREVE}', 'def'], |
| 176 | + }, |
| 177 | + ), |
| 178 | + ], |
| 179 | +) |
| 180 | +def test_text_wrapper_drop_ws(text: str, results: dict[int, list[str]]) -> None: |
| 181 | + for width, expected in results.items(): |
| 182 | + w = TextWrapper(width=width, drop_whitespace=False) |
| 183 | + assert w.wrap(text) == expected |
0 commit comments