Merge branch 'vertical-layout' into horizontal-layout

This commit is contained in:
Darren Burns
2022-02-04 11:48:48 +00:00
committed by GitHub
20 changed files with 1258 additions and 307 deletions

View File

View File

@@ -0,0 +1,126 @@
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}"
)

24
tests/utilities/render.py Normal file
View File

@@ -0,0 +1,24 @@
import io
import re
from rich.console import Console, RenderableType
re_link_ids = re.compile(r"id=[\d\.\-]*?;.*?\x1b")
def replace_link_ids(render: str) -> str:
"""Link IDs have a random ID and system path which is a problem for
reproducible tests.
"""
return re_link_ids.sub("id=0;foo\x1b", render)
def render(renderable: RenderableType, no_wrap: bool = False) -> str:
console = Console(
width=100, file=io.StringIO(), color_system="truecolor", legacy_windows=False
)
console.print(renderable, no_wrap=no_wrap)
output = replace_link_ids(console.file.getvalue())
return output