Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Oct 4, 2024
1 parent 4f40703 commit babf74a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
6 changes: 3 additions & 3 deletions rich/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment
)
if out_by == -1 and cell_size(text[pos]) == 2:
return (
_Segment(before[:pos] + " ", style, control),
_Segment(text[:pos] + " ", style, control),
_Segment(" " + text[pos + 1 :], style, control),
)
if out_by == +1 and cell_size(text[pos]) == 2:
if out_by == +1 and cell_size(text[pos - 1]) == 2:
return (
_Segment(before[: pos - 1] + " ", style, control),
_Segment(text[: pos - 1] + " ", style, control),
_Segment(" " + text[pos:], style, control),
)
if cell_pos < cut:
Expand Down
30 changes: 19 additions & 11 deletions tests/test_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,27 @@ def test_split_cells_emoji(text, split, result):
assert Segment(text).split_cells(split) == result


def test_split_cells_mixed() -> None:
"""Check that split cells splits on cell positions."""
# Caused https://github.com/Textualize/textual/issues/4996 in Textual
tests = [
@pytest.mark.parametrize(
"segment",
[
Segment("早乙女リリエル (CV: 徳井青)"),
Segment("メイド・イン・きゅんクチュアリ☆"),
Segment("メイド・イン・きゅんクチュアリ☆ "),
Segment("TVアニメ「メルクストーリア -無気力少年と瓶の中の少女-」 主題歌CD"),
]
for test in tests:
for position in range(1, test.cell_length):
left, right = Segment.split_cells(test, position)
assert cell_len(left.text) == position
assert cell_len(right.text) == test.cell_length - position
Segment("南無阿弥JKうらめしや?! "),
Segment("メルク (CV: 水瀬いのり) "),
],
)
def test_split_cells_mixed(segment: Segment) -> None:
"""Check that split cells splits on cell positions."""
# Caused https://github.com/Textualize/textual/issues/4996 in Textual

for position in range(0, segment.cell_length + 1):
left, right = Segment.split_cells(segment, position)
assert all(
cell_len(c) > 0 for c in segment.text
) # Sanity check there aren't any sneaky control codes
assert cell_len(left.text) == position
assert cell_len(right.text) == segment.cell_length - position


def test_split_cells_doubles() -> None:
Expand Down

0 comments on commit babf74a

Please sign in to comment.