File size: 3,952 Bytes
2c3c408 | 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 | from unittest.mock import create_autospec
from rich.console import Console
from rich.console import ConsoleOptions
from rich.text import Text
from tests.utilities.render import render
from textual.renderables.underline_bar import UnderlineBar
MAGENTA = "\x1b[35m"
GREY = "\x1b[38;5;59m"
STOP = "\x1b[0m"
GREEN = "\x1b[32m"
RED = "\x1b[31m"
def test_no_highlight():
bar = UnderlineBar(width=6)
assert render(bar) == f"{GREY}ββββββ{STOP}"
def test_highlight_from_zero():
bar = UnderlineBar(highlight_range=(0, 2.5), width=6)
assert render(bar) == (
f"{MAGENTA}ββ{STOP}{MAGENTA}βΈ{STOP}{GREY}βββ{STOP}"
)
def test_highlight_from_zero_point_five():
bar = UnderlineBar(highlight_range=(0.5, 2), width=6)
assert render(bar) == (
f"{MAGENTA}βΊβ{STOP}{GREY}βΊ{STOP}{GREY}βββ{STOP}"
)
def test_highlight_middle():
bar = UnderlineBar(highlight_range=(2, 4), width=6)
assert render(bar) == (
f"{GREY}β{STOP}"
f"{GREY}βΈ{STOP}"
f"{MAGENTA}ββ{STOP}"
f"{GREY}βΊ{STOP}"
f"{GREY}β{STOP}"
)
def test_highlight_half_start():
bar = UnderlineBar(highlight_range=(2.5, 4), width=6)
assert render(bar) == (
f"{GREY}ββ{STOP}"
f"{MAGENTA}βΊβ{STOP}"
f"{GREY}βΊ{STOP}"
f"{GREY}β{STOP}"
)
def test_highlight_half_end():
bar = UnderlineBar(highlight_range=(2, 4.5), width=6)
assert render(bar) == (
f"{GREY}β{STOP}"
f"{GREY}βΈ{STOP}"
f"{MAGENTA}ββ{STOP}"
f"{MAGENTA}βΈ{STOP}"
f"{GREY}β{STOP}"
)
def test_highlight_half_start_and_half_end():
bar = UnderlineBar(highlight_range=(2.5, 4.5), width=6)
assert render(bar) == (
f"{GREY}ββ{STOP}"
f"{MAGENTA}βΊβ{STOP}"
f"{MAGENTA}βΈ{STOP}"
f"{GREY}β{STOP}"
)
def test_highlight_to_near_end():
bar = UnderlineBar(highlight_range=(3, 5.5), width=6)
assert render(bar) == (
f"{GREY}ββ{STOP}"
f"{GREY}βΈ{STOP}"
f"{MAGENTA}ββ{STOP}"
f"{MAGENTA}βΈ{STOP}"
)
def test_highlight_to_end():
bar = UnderlineBar(highlight_range=(3, 6), width=6)
assert render(bar) == (
f"{GREY}ββ{STOP}{GREY}βΈ{STOP}{MAGENTA}βββ{STOP}"
)
def test_highlight_out_of_bounds_start():
bar = UnderlineBar(highlight_range=(-2, 3), width=6)
assert render(bar) == (
f"{MAGENTA}βββ{STOP}{GREY}βΊ{STOP}{GREY}ββ{STOP}"
)
def test_highlight_out_of_bounds_end():
bar = UnderlineBar(highlight_range=(3, 9), width=6)
assert render(bar) == (
f"{GREY}ββ{STOP}{GREY}βΈ{STOP}{MAGENTA}βββ{STOP}"
)
def test_highlight_full_range_out_of_bounds_end():
bar = UnderlineBar(highlight_range=(9, 10), width=6)
assert render(bar) == f"{GREY}ββββββ{STOP}"
def test_highlight_full_range_out_of_bounds_start():
bar = UnderlineBar(highlight_range=(-5, -2), width=6)
assert render(bar) == f"{GREY}ββββββ{STOP}"
def test_custom_styles():
bar = UnderlineBar(highlight_range=(2, 4), highlight_style="red", background_style="green", width=6)
assert render(bar) == (
f"{GREEN}β{STOP}"
f"{GREEN}βΈ{STOP}"
f"{RED}ββ{STOP}"
f"{GREEN}βΊ{STOP}"
f"{GREEN}β{STOP}"
)
def test_clickable_ranges():
bar = UnderlineBar(highlight_range=(0, 1), width=6, clickable_ranges={"foo": (0, 2), "bar": (4, 5)})
console = create_autospec(Console)
options = create_autospec(ConsoleOptions)
text: Text = list(bar.__rich_console__(console, options))[0]
start, end, style = text.spans[-2]
assert (start, end) == (0, 2)
assert style.meta == {'@click': "range_clicked('foo')"}
start, end, style = text.spans[-1]
assert (start, end) == (4, 5)
assert style.meta == {'@click': "range_clicked('bar')"}
|