1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
import pytest
from rich.segment import Segment
from rich.style import Style
from textual._segment_tools import NoCellPositionForIndex
from textual.color import Color
from textual.filter import Monochrome
from textual.strip import Strip
def test_cell_length() -> None:
strip = Strip([Segment("foo"), Segment("π©"), Segment("bar")])
assert strip._cell_length is None
assert strip.cell_length == 8
assert strip._cell_length == 8
def test_repr() -> None:
strip = Strip([Segment("foo")])
assert repr(strip) == "Strip([Segment('foo')], 3)"
def test_join() -> None:
strip1 = Strip([Segment("foo")])
strip2 = Strip([Segment("bar")])
strip = Strip.join([strip1, strip2])
assert len(strip) == 2
assert strip.cell_length == 6
assert list(strip) == [Segment("foo"), Segment("bar")]
def test_bool() -> None:
assert not Strip([])
assert Strip([Segment("foo")])
def test_iter() -> None:
assert list(Strip([])) == []
assert list(Strip([Segment("foo")])) == [Segment("foo")]
assert list(Strip([Segment("foo"), Segment("bar")])) == [
Segment("foo"),
Segment("bar"),
]
def test_len():
assert len(Strip([])) == 0
assert len(Strip([Segment("foo")])) == 1
assert len(Strip([Segment("foo"), Segment("bar")])) == 2
def test_reversed():
assert list(reversed(Strip([]))) == []
assert list(reversed(Strip([Segment("foo")]))) == [Segment("foo")]
assert list(reversed(Strip([Segment("foo"), Segment("bar")]))) == [
Segment("bar"),
Segment("foo"),
]
def test_eq():
assert Strip([]) == Strip([])
assert Strip([Segment("foo")]) == Strip([Segment("foo")])
assert Strip([Segment("foo")]) != Strip([Segment("bar")])
def test_adjust_cell_length():
assert Strip([]).adjust_cell_length(3) == Strip([Segment(" ")])
assert Strip([Segment("f")]).adjust_cell_length(3) == Strip(
[Segment("f"), Segment(" ")]
)
assert Strip([Segment("π©")]).adjust_cell_length(3) == Strip(
[Segment("π©"), Segment(" ")]
)
assert Strip([Segment("π©π©")]).adjust_cell_length(3) == Strip([Segment("π© ")])
assert Strip([Segment("π©π©")]).adjust_cell_length(4) == Strip([Segment("π©π©")])
assert Strip([Segment("π©"), Segment("π©π©")]).adjust_cell_length(2) == Strip(
[Segment("π©")]
)
assert Strip([Segment("π©"), Segment("π©π©")]).adjust_cell_length(4) == Strip(
[Segment("π©"), Segment("π©")]
)
def test_extend_cell_length():
strip = Strip([Segment("foo"), Segment("bar")])
assert strip.extend_cell_length(3).text == "foobar"
assert strip.extend_cell_length(6).text == "foobar"
assert strip.extend_cell_length(7).text == "foobar "
assert strip.extend_cell_length(9).text == "foobar "
def test_simplify():
assert Strip([Segment("foo"), Segment("bar")]).simplify() == Strip(
[Segment("foobar")]
)
def test_apply_filter():
strip = Strip([Segment("foo", Style.parse("red"))])
expected = Strip([Segment("foo", Style.parse("#1b1b1b"))])
assert strip.apply_filter(Monochrome(), Color(0, 0, 0)) == expected
def test_style_links():
link_style = Style.on(click="clicked")
strip = Strip(
[
Segment("foo"),
Segment("bar", link_style),
Segment("baz"),
]
)
hover_style = Style(underline=True)
new_strip = strip.style_links(link_style._link_id, hover_style)
expected = Strip(
[
Segment("foo"),
Segment("bar", link_style + hover_style),
Segment("baz"),
]
)
assert new_strip == expected
def test_crop():
assert Strip([Segment("foo")]).crop(0, 3) == Strip([Segment("foo")])
assert Strip([Segment("foo")]).crop(0, 2) == Strip([Segment("fo")])
assert Strip([Segment("foo")]).crop(0, 1) == Strip([Segment("f")])
assert Strip([Segment("foo")]).crop(1, 3) == Strip([Segment("oo")])
assert Strip([Segment("foo")]).crop(1, 2) == Strip([Segment("o")])
assert Strip([Segment("foo")]).crop(1, 1) == Strip([])
assert Strip([Segment("fooπ©"), Segment("bπ©ar"), Segment("baπ©z")]).crop(
1, 6
) == Strip([Segment("ooπ©"), Segment("b")])
@pytest.mark.parametrize(
"text,crop,output",
[
["foo", (0, 5), [Segment("foo")]],
["foo", (2, 5), [Segment("o")]],
["foo", (3, 5), []],
["foo", (4, 6), []],
],
)
def test_crop_out_of_bounds(text, crop, output):
assert Strip([Segment(text)]).crop(*crop) == Strip(output)
def test_divide():
assert Strip([Segment("foo")]).divide([1, 2]) == [
Strip([Segment("f")]),
Strip([Segment("o")]),
]
@pytest.mark.parametrize(
"index,cell_position",
[
(0, 0),
(1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 6),
(6, 8),
(7, 10),
(8, 11),
(9, 12),
(10, 13),
(11, 14),
],
)
def test_index_to_cell_position(index, cell_position):
strip = Strip([Segment("ab"), Segment("cdζ₯ζ¬θͺef"), Segment("gh")])
assert cell_position == strip.index_to_cell_position(index)
def test_index_cell_position_no_segments():
strip = Strip([])
with pytest.raises(NoCellPositionForIndex):
strip.index_to_cell_position(2)
def test_index_cell_position_index_too_large():
strip = Strip([Segment("abcdef"), Segment("ghi")])
with pytest.raises(NoCellPositionForIndex):
strip.index_to_cell_position(100)
def test_text():
assert Strip([]).text == ""
assert Strip([Segment("foo")]).text == "foo"
assert Strip([Segment("foo"), Segment("bar")]).text == "foobar"
|