Debian Codegraph @

tests/test_cells.py

raw
1 from __future__ import annotations
2
3 import string
4
5 import pytest
6
7 from rich import cells
8 from rich.cells import (
9 CellSpan,
10 _is_single_cell_widths,
11 cell_len,
12 chop_cells,
13 get_character_cell_size,
14 split_graphemes,
15 split_text,
16 )
17
18
19 @pytest.mark.parametrize(
20 "character,size",
21 [
22 ("\0", 0),
23 ("\u200d", 0),
24 ("a", 1),
25 ("💩", 2),
26 (chr(917999 + 1), 0),
27 ],
28 )
29 def test_get_character_cell_size(character: str, size: int) -> None:
30 """Test single character cell size."""
31 assert get_character_cell_size(character) == size
32
33
34 def test_cell_len_long_string():
35 # Long strings don't use cached cell length implementation
36 assert cells.cell_len("abc" * 200) == 3 * 200
37 # Boundary case
38 assert cells.cell_len("a" * 512) == 512
39
40
41 def test_cell_len_short_string():
42 # Short strings use cached cell length implementation
43 assert cells.cell_len("abc" * 100) == 3 * 100
44 # Boundary case
45 assert cells.cell_len("a" * 511) == 511
46
47
48 def test_set_cell_size():
49 assert cells.set_cell_size("foo", 0) == ""
50 assert cells.set_cell_size("f", 0) == ""
51 assert cells.set_cell_size("", 0) == ""
52 assert cells.set_cell_size("😽😽", 0) == ""
53 assert cells.set_cell_size("foo", 2) == "fo"
54 assert cells.set_cell_size("foo", 3) == "foo"
55 assert cells.set_cell_size("foo", 4) == "foo "
56 assert cells.set_cell_size("😽😽", 4) == "😽😽"
57 assert cells.set_cell_size("😽😽", 3) == "😽 "
58 assert cells.set_cell_size("😽😽", 2) == "😽"
59 assert cells.set_cell_size("😽😽", 1) == " "
60 assert cells.set_cell_size("😽😽", 5) == "😽😽 "
61
62
63 def test_set_cell_size_infinite():
64 for size in range(38):
65 assert (
66 cells.cell_len(
67 cells.set_cell_size(
68 "เป็นเกมที่ต้องมีความอดทนมากที่สุดตั้งเเต่เคยเล่นมา", size
69 )
70 )
71 == size
72 )
73
74
75 FM = "👩\u200d🔧"
76
77
78 @pytest.mark.parametrize(
79 "text,offset,left,right",
80 [
81 # Edge cases
82 ("", -1, "", ""),
83 ("x", -1, "", "x"),
84 ("x", 1, "x", ""),
85 ("x", 2, "x", ""),
86 ("", 0, "", ""),
87 ("", 1, "", ""),
88 ("a", 0, "", "a"),
89 ("a", 1, "a", ""),
90 # Check simple double width character
91 ("💩", 0, "", "💩"),
92 ("💩", 1, " ", " "), # Split in the middle of a double wide results in spaces
93 ("💩", 2, "💩", ""),
94 ("💩x", 1, " ", " x"),
95 ("💩x", 2, "💩", "x"),
96 ("💩x", 3, "💩x", ""),
97 # Check same for multi-codepoint emoji
98 (FM, 0, "", FM),
99 (FM, 1, " ", " "), # Split in the middle of a double wide results in spaces
100 (FM, 2, FM, ""),
101 (FM + "x", 1, " ", " x"),
102 (FM + "x", 2, FM, "x"),
103 (FM + "x", 3, FM + "x", ""),
104 # Edge cases
105 ("xxxxxxxxxxxxxxx💩💩", 10, "xxxxxxxxxx", "xxxxx💩💩"),
106 ("xxxxxxxxxxxxxxx💩💩", 15, "xxxxxxxxxxxxxxx", "💩💩"),
107 ("xxxxxxxxxxxxxxx💩💩", 16, "xxxxxxxxxxxxxxx ", " 💩"),
108 ("💩💩", 3, "💩 ", " "),
109 ("💩💩xxxxxxxxxx", 2, "💩", "💩xxxxxxxxxx"),
110 ("💩💩xxxxxxxxxx", 3, "💩 ", " xxxxxxxxxx"),
111 ("💩💩xxxxxxxxxx", 4, "💩💩", "xxxxxxxxxx"),
112 ],
113 )
114 def test_split_text(text: str, offset: int, left: str, right: str) -> None:
115 """Check that split_text works on grapheme boundaries"""
116 assert split_text(text, offset) == (left, right)
117
118
119 def test_chop_cells():
120 """Simple example of splitting cells into lines of width 3."""
121 text = "abcdefghijk"
122 assert chop_cells(text, 3) == ["abc", "def", "ghi", "jk"]
123
124
125 def test_chop_cells_double_width_boundary():
126 """The available width lies within a double-width character."""
127 text = "ありがとう"
128 assert chop_cells(text, 3) == ["あ", "り", "が", "と", "う"]
129
130
131 def test_chop_cells_mixed_width():
132 """Mixed single and double-width characters."""
133 text = "あ1り234が5と6う78"
134 assert chop_cells(text, 3) == ["あ1", "り2", "34", "が5", "と6", "う7", "8"]
135
136
137 @pytest.mark.parametrize(
138 "text,expected",
139 [
140 ("", []),
141 ("\x1b", []),
142 ("\x1b\x1b", []),
143 ("\x1b\x1b\x1b", []),
144 ("\x1b\x1b\x1b\x1b", []),
145 ],
146 )
147 def test_chop_cells_zero_width(text: str, expected: list[str]) -> None:
148 """Test zer width characters being chopped."""
149 assert chop_cells(text, 3) == expected
150
151
152 def test_is_single_cell_widths() -> None:
153 # Check _is_single_cell_widths reports correctly
154 for character in string.printable:
155 if ord(character) >= 32:
156 assert _is_single_cell_widths(character)
157
158 BOX = "┌─┬┐│ ││├─┼┤│ ││├─┼┤├─┼┤│ ││└─┴┘"
159
160 for character in BOX:
161 assert _is_single_cell_widths(character)
162
163 for character in "💩😽":
164 assert not _is_single_cell_widths(character)
165
166 for character in "わさび":
167 assert not _is_single_cell_widths(character)
168
169
170 @pytest.mark.parametrize(
171 "text,expected_spans,expected_cell_length",
172 [
173 ("", [], 0),
174 ("a", [(0, 1, 1)], 1),
175 ("ab", [(0, 1, 1), (1, 2, 1)], 2),
176 ("💩", [(0, 1, 2)], 2),
177 ("わさび", [(0, 1, 2), (1, 2, 2), (2, 3, 2)], 6),
178 (
179 "👩\u200d🔧",
180 [(0, 3, 2)],
181 2,
182 ), # 3 code points for female mechanic: female, joiner, spanner
183 ("a👩\u200d🔧", [(0, 1, 1), (1, 4, 2)], 3),
184 ("a👩\u200d🔧b", [(0, 1, 1), (1, 4, 2), (4, 5, 1)], 4),
185 ("⬇", [(0, 1, 1)], 1),
186 ("⬇️", [(0, 2, 2)], 2), # Variation selector, makes it double width
187 ("♻", [(0, 1, 1)], 1),
188 ("♻️", [(0, 2, 2)], 2),
189 ("♻♻️", [(0, 1, 1), (1, 3, 2)], 3),
190 ("\x1b", [(0, 1, 0)], 0), # One escape should have zero width
191 ("\x1b\x1b", [(0, 2, 0)], 0), # Two escapes should have zero width
192 (
193 "\ufe0f",
194 [(0, 1, 0)],
195 0,
196 ), # Variation selector 16, without anything to change should have zero width
197 (
198 "\ufe0f\ufe0f",
199 [(0, 2, 0)],
200 0,
201 ), # 2 X variation selector 16, without anything to change should have zero width
202 (
203 "\u200d",
204 [(0, 1, 0)],
205 0,
206 ), # A zero width joiner with nothing prior should have zero width
207 (
208 "\u200d\u200d",
209 [(0, 2, 0)],
210 0,
211 ), # Two ZWJs should have zero width
212 (
213 "\x1b\ufe0f",
214 [(0, 2, 0)],
215 0,
216 ), # VS16 after escape (zero-width, doesn't set last_measured_character) should have zero width
217 (
218 "\u200d\ufe0f",
219 [(0, 2, 0)],
220 0,
221 ), # VS16 after ZWJ (zero-width, doesn't set last_measured_character) should have zero width
222 ],
223 )
224 def test_split_graphemes(
225 text: str, expected_spans: list[CellSpan], expected_cell_length: int
226 ):
227 spans, cell_length = split_graphemes(text)
228 print(spans)
229 assert cell_len(text) == expected_cell_length
230 assert spans == expected_spans
231 assert cell_length == expected_cell_length
232
233
234 def test_nerd_font():
235 """Regression test for https://github.com/Textualize/rich/issues/3943"""
236 # Not allocated by unicode, but used by nerd fonts
237 assert cell_len("\U000f024d") == 1
238
239
240 def test_zwj():
241 """Test special case of zero width joiners"""
242 assert cell_len("") == 0
243 assert cell_len("\u200d") == 0
244 assert cell_len("1\u200d") == 1
245 # This sequence should really produce 2, but it aligns with with wcwidth
246 # What gets written to the terminal is anybody's guess, I've seen multiple variations
247 assert cell_len("1\u200d2") == 1
248
249
250 def test_non_printable():
251 """Non printable characters should report a width of 0."""
252 for ordinal in range(31):
253 character = chr(ordinal)
254 assert cell_len(character) == 0