From 9323ad7e1cc4836c463df5a24a7388b2652cb596 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Tue, 12 Nov 2024 15:30:05 +0000 Subject: [PATCH] test for content --- tests/test_content.py | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/test_content.py diff --git a/tests/test_content.py b/tests/test_content.py new file mode 100644 index 0000000000..bc2b96775b --- /dev/null +++ b/tests/test_content.py @@ -0,0 +1,52 @@ +from rich.text import Text + +from textual.content import Content, Span + + +def test_constructor(): + content = Content("Hello, World") + assert content + assert len(content) == 12 + assert content.cell_length == 12 + assert content.plain == "Hello, World" + repr(content) + assert content.align == "left" + assert content.no_wrap is False + assert content.ellipsis is False + + +def test_bool(): + assert bool(Content("foo")) is True + assert bool(Content("")) is False + + +def test_from_rich_text(): + text = Text.from_markup("[red]Hello[/red] [blue]World[/blue]") + content = Content.from_rich_text(text) + assert len(content) == 11 + assert content.plain == "Hello World" + assert [Span(start=0, end=5, style="red"), Span(start=6, end=11, style="blue")] + + +def test_styled(): + text = Content.styled("Hello", "red") + assert text.plain == "Hello" + assert len(text) == 5 + assert text.cell_length == 5 + assert text._spans == [Span(0, 5, "red")] + + +def test_getitem(): + content = Content("Hello, world").stylize("blue", 0, 5) + assert content[0].plain == "H" + assert content[0]._spans == [Span(0, 1, "blue")] + assert content[-1].plain == "d" + assert content[-1]._spans == [] + assert content[:2].plain == "He" + assert content[:2]._spans == [Span(0, 2, "blue")] + + +def test_cell_length(): + assert Content("").cell_length == 0 + assert Content("foo").cell_length == 3 + assert Content("💩").cell_length == 2