File: test_markup.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (127 lines) | stat: -rw-r--r-- 3,554 bytes parent folder | download | duplicates (2)
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
from __future__ import annotations

import pytest

from textual.content import Content, Span
from textual.markup import MarkupError, to_content


@pytest.mark.parametrize(
    ["markup", "content"],
    [
        ("", Content("")),
        ("foo", Content("foo")),
        ("foo\n", Content("foo\n")),
        ("foo\nbar", Content("foo\nbar")),
        ("[bold]Hello", Content("Hello", [Span(0, 5, "bold")])),
        (
            "[bold rgb(10, 20, 30)]Hello",
            Content("Hello", [Span(0, 5, "bold rgb(10, 20, 30)")]),
        ),
        (
            "[bold red]Hello[/] World",
            Content("Hello World", [Span(0, 5, "bold red")]),
        ),
        (
            "[bold red]Hello",
            Content("Hello", [Span(0, 5, "bold red")]),
        ),
        (
            "[bold red]Hello[/]\nWorld",
            Content("Hello\nWorld", [Span(0, 5, "bold red")]),
        ),
        (
            "[b][on red]What [i]is up[/on red] with you?[/]",
            Content(
                "What is up with you?",
                spans=[
                    Span(0, 10, style="on red"),
                    Span(5, 20, style="i"),
                    Span(0, 20, style="b"),
                ],
            ),
        ),
        (
            "[b]Welcome to Textual[/b]\n\nI must not fear",
            Content(
                "Welcome to Textual\n\nI must not fear",
                spans=[
                    Span(0, 18, style="b"),
                ],
            ),
        ),
        (
            "[$accent]Hello",
            Content(
                "Hello",
                spans=[
                    Span(0, 5, "$accent"),
                ],
            ),
        ),
        (
            "[on red @click=app.bell]Click me",
            Content(
                "Click me",
                spans=[
                    Span(0, 8, "on red @click=app.bell"),
                ],
            ),
        ),
        (
            "[on red @click=app.bell]Click me[/on red @click=]",
            Content(
                "Click me",
                spans=[
                    Span(0, 8, "on red @click=app.bell"),
                ],
            ),
        ),
    ],
)
def test_to_content(markup: str, content: Content):
    markup_content = to_content(markup)
    print(repr(markup_content))
    print(repr(content))
    assert markup_content.is_same(content)


def test_content_parse_fail() -> None:
    with pytest.raises(MarkupError):
        to_content("[rgb(1,2,3,4)]foo")
    with pytest.raises(MarkupError):
        to_content("[foo]foo[/bar]")
    with pytest.raises(MarkupError):
        to_content("foo[/]")


@pytest.mark.parametrize(
    ["markup", "variables", "content"],
    [
        # Simple substitution
        (
            "Hello $name",
            {"name": "Will"},
            Content("Hello Will"),
        ),
        # Wrapped in tags
        (
            "Hello [bold]$name[/bold]",
            {"name": "Will"},
            Content("Hello Will", spans=[Span(6, 10, style="bold")]),
        ),
        # dollar in tags should not trigger substitution.
        (
            "Hello [link='$name']$name[/link]",
            {"name": "Will"},
            Content("Hello Will", spans=[Span(6, 10, style="link='$name'")]),
        ),
    ],
)
def test_template_variables(
    markup: str, variables: dict[str, object], content: Content
) -> None:
    markup_content = Content.from_markup(markup, **variables)
    print(repr(markup_content))
    print(repr(content))
    assert markup_content.is_same(content)