rich/tests/test_cells.py

82 lines
2.7 KiB
Python
Raw Normal View History

2024-10-22 15:13:14 +00:00
import string
2020-04-26 14:48:04 +00:00
from rich import cells
2024-10-22 15:13:14 +00:00
from rich.cells import _is_single_cell_widths, chop_cells
2020-04-26 14:48:04 +00:00
2022-09-07 14:26:05 +00:00
def test_cell_len_long_string():
# Long strings don't use cached cell length implementation
assert cells.cell_len("abc" * 200) == 3 * 200
2023-01-29 13:05:25 +00:00
# Boundary case
assert cells.cell_len("a" * 512) == 512
def test_cell_len_short_string():
# Short strings use cached cell length implementation
assert cells.cell_len("abc" * 100) == 3 * 100
# Boundary case
assert cells.cell_len("a" * 511) == 511
2022-09-07 14:26:05 +00:00
2020-04-26 14:48:04 +00:00
def test_set_cell_size():
2021-11-13 09:07:42 +00:00
assert cells.set_cell_size("foo", 0) == ""
assert cells.set_cell_size("f", 0) == ""
assert cells.set_cell_size("", 0) == ""
assert cells.set_cell_size("😽😽", 0) == ""
2020-04-26 14:48:04 +00:00
assert cells.set_cell_size("foo", 2) == "fo"
assert cells.set_cell_size("foo", 3) == "foo"
assert cells.set_cell_size("foo", 4) == "foo "
assert cells.set_cell_size("😽😽", 4) == "😽😽"
assert cells.set_cell_size("😽😽", 3) == "😽 "
assert cells.set_cell_size("😽😽", 2) == "😽"
assert cells.set_cell_size("😽😽", 1) == " "
2021-11-27 19:41:29 +00:00
assert cells.set_cell_size("😽😽", 5) == "😽😽 "
2021-11-13 09:04:12 +00:00
def test_set_cell_size_infinite():
for size in range(38):
assert (
cells.cell_len(
cells.set_cell_size(
"เป็นเกมที่ต้องมีความอดทนมากที่สุดตั้งเเต่เคยเล่นมา", size
)
)
== size
)
def test_chop_cells():
"""Simple example of splitting cells into lines of width 3."""
text = "abcdefghijk"
assert chop_cells(text, 3) == ["abc", "def", "ghi", "jk"]
def test_chop_cells_double_width_boundary():
"""The available width lies within a double-width character."""
text = "ありがとう"
assert chop_cells(text, 3) == ["", "", "", "", ""]
def test_chop_cells_mixed_width():
"""Mixed single and double-width characters."""
text = "あ1り234が5と6う78"
assert chop_cells(text, 3) == ["あ1", "り2", "34", "が5", "と6", "う7", "8"]
2024-10-22 15:13:14 +00:00
def test_is_single_cell_widths() -> None:
# Check _is_single_cell_widths reports correctly
for character in string.printable:
if ord(character) >= 32:
assert _is_single_cell_widths(character)
BOX = "┌─┬┐│ ││├─┼┤│ ││├─┼┤├─┼┤│ ││└─┴┘"
for character in BOX:
assert _is_single_cell_widths(character)
2024-10-22 15:29:46 +00:00
for character in "💩😽":
2024-10-22 15:13:14 +00:00
assert not _is_single_cell_widths(character)
for character in "わさび":
assert not _is_single_cell_widths(character)