| 1 |
import pytest |
| 2 |
|
| 3 |
from rich.console import Console |
| 4 |
from rich.errors import MarkupError |
| 5 |
from rich.markup import RE_TAGS, Tag, _parse, escape, render |
| 6 |
from rich.text import Span, Text |
| 7 |
|
| 8 |
|
| 9 |
def test_re_no_match(): |
| 10 |
assert RE_TAGS.match("[True]") == None |
| 11 |
assert RE_TAGS.match("[False]") == None |
| 12 |
assert RE_TAGS.match("[None]") == None |
| 13 |
assert RE_TAGS.match("[1]") == None |
| 14 |
assert RE_TAGS.match("[2]") == None |
| 15 |
assert RE_TAGS.match("[]") == None |
| 16 |
|
| 17 |
|
| 18 |
def test_re_match(): |
| 19 |
assert RE_TAGS.match("[true]") |
| 20 |
assert RE_TAGS.match("[false]") |
| 21 |
assert RE_TAGS.match("[none]") |
| 22 |
assert RE_TAGS.match("[color(1)]") |
| 23 |
assert RE_TAGS.match("[#ff00ff]") |
| 24 |
assert RE_TAGS.match("[/]") |
| 25 |
assert RE_TAGS.match("[@]") |
| 26 |
assert RE_TAGS.match("[@foo]") |
| 27 |
assert RE_TAGS.match("[@foo=bar]") |
| 28 |
|
| 29 |
|
| 30 |
def test_escape(): |
| 31 |
# Potential tags |
| 32 |
assert escape("foo[bar]") == r"foo\[bar]" |
| 33 |
assert escape(r"foo\[bar]") == r"foo\\\[bar]" |
| 34 |
|
| 35 |
# Not tags (escape not required) |
| 36 |
assert escape("[5]") == "[5]" |
| 37 |
assert escape("\\[5]") == "\\[5]" |
| 38 |
|
| 39 |
# Test @ escape |
| 40 |
assert escape("[@foo]") == "\\[@foo]" |
| 41 |
assert escape("[@]") == "\\[@]" |
| 42 |
|
| 43 |
# https://github.com/Textualize/rich/issues/2187 |
| 44 |
assert escape("[nil, [nil]]") == r"[nil, \[nil]]" |
| 45 |
|
| 46 |
|
| 47 |
def test_escape_backslash_end(): |
| 48 |
# https://github.com/Textualize/rich/issues/2987 |
| 49 |
value = "C:\\" |
| 50 |
assert escape(value) == "C:\\\\" |
| 51 |
|
| 52 |
escaped_tags = f"[red]{escape(value)}[/red]" |
| 53 |
assert escaped_tags == "[red]C:\\\\[/red]" |
| 54 |
escaped_text = Text.from_markup(escaped_tags) |
| 55 |
assert escaped_text.plain == "C:\\" |
| 56 |
assert escaped_text.spans == [Span(0, 3, "red")] |
| 57 |
|
| 58 |
|
| 59 |
def test_render_escape(): |
| 60 |
console = Console(width=80, color_system=None) |
| 61 |
console.begin_capture() |
| 62 |
console.print( |
| 63 |
escape(r"[red]"), escape(r"\[red]"), escape(r"\\[red]"), escape(r"\\\[red]") |
| 64 |
) |
| 65 |
result = console.end_capture() |
| 66 |
expected = r"[red] \[red] \\[red] \\\[red]" + "\n" |
| 67 |
assert result == expected |
| 68 |
|
| 69 |
|
| 70 |
def test_parse(): |
| 71 |
result = list(_parse(r"[foo]hello[/foo][bar]world[/]\[escaped]")) |
| 72 |
expected = [ |
| 73 |
(0, None, Tag(name="foo", parameters=None)), |
| 74 |
(10, "hello", None), |
| 75 |
(10, None, Tag(name="/foo", parameters=None)), |
| 76 |
(16, None, Tag(name="bar", parameters=None)), |
| 77 |
(26, "world", None), |
| 78 |
(26, None, Tag(name="/", parameters=None)), |
| 79 |
(29, "[escaped]", None), |
| 80 |
] |
| 81 |
print(repr(result)) |
| 82 |
assert result == expected |
| 83 |
|
| 84 |
|
| 85 |
def test_parse_link(): |
| 86 |
result = list(_parse("[link=foo]bar[/link]")) |
| 87 |
expected = [ |
| 88 |
(0, None, Tag(name="link", parameters="foo")), |
| 89 |
(13, "bar", None), |
| 90 |
(13, None, Tag(name="/link", parameters=None)), |
| 91 |
] |
| 92 |
assert result == expected |
| 93 |
|
| 94 |
|
| 95 |
def test_render(): |
| 96 |
result = render("[bold]FOO[/bold]") |
| 97 |
assert str(result) == "FOO" |
| 98 |
assert result.spans == [Span(0, 3, "bold")] |
| 99 |
|
| 100 |
|
| 101 |
def test_render_not_tags(): |
| 102 |
result = render('[[1], [1,2,3,4], ["hello"], [None], [False], [True]] []') |
| 103 |
assert str(result) == '[[1], [1,2,3,4], ["hello"], [None], [False], [True]] []' |
| 104 |
assert result.spans == [] |
| 105 |
|
| 106 |
|
| 107 |
def test_render_link(): |
| 108 |
result = render("[link=foo]FOO[/link]") |
| 109 |
assert str(result) == "FOO" |
| 110 |
assert result.spans == [Span(0, 3, "link foo")] |
| 111 |
|
| 112 |
|
| 113 |
def test_render_combine(): |
| 114 |
result = render("[green]X[blue]Y[/blue]Z[/green]") |
| 115 |
assert str(result) == "XYZ" |
| 116 |
assert result.spans == [ |
| 117 |
Span(0, 3, "green"), |
| 118 |
Span(1, 2, "blue"), |
| 119 |
] |
| 120 |
|
| 121 |
|
| 122 |
def test_render_overlap(): |
| 123 |
result = render("[green]X[bold]Y[/green]Z[/bold]") |
| 124 |
assert str(result) == "XYZ" |
| 125 |
assert result.spans == [ |
| 126 |
Span(0, 2, "green"), |
| 127 |
Span(1, 3, "bold"), |
| 128 |
] |
| 129 |
|
| 130 |
|
| 131 |
def test_adjoint(): |
| 132 |
result = render("[red][blue]B[/blue]R[/red]") |
| 133 |
print(repr(result)) |
| 134 |
assert result.spans == [Span(0, 2, "red"), Span(0, 1, "blue")] |
| 135 |
|
| 136 |
|
| 137 |
def test_render_close(): |
| 138 |
result = render("[bold]X[/]Y") |
| 139 |
assert str(result) == "XY" |
| 140 |
assert result.spans == [Span(0, 1, "bold")] |
| 141 |
|
| 142 |
|
| 143 |
def test_render_close_ambiguous(): |
| 144 |
result = render("[green]X[bold]Y[/]Z[/]") |
| 145 |
assert str(result) == "XYZ" |
| 146 |
assert result.spans == [Span(0, 3, "green"), Span(1, 2, "bold")] |
| 147 |
|
| 148 |
|
| 149 |
def test_markup_error(): |
| 150 |
with pytest.raises(MarkupError): |
| 151 |
assert render("foo[/]") |
| 152 |
with pytest.raises(MarkupError): |
| 153 |
assert render("foo[/bar]") |
| 154 |
with pytest.raises(MarkupError): |
| 155 |
assert render("[foo]hello[/bar]") |
| 156 |
|
| 157 |
|
| 158 |
def test_markup_escape(): |
| 159 |
result = str(render("[dim white][url=[/]")) |
| 160 |
assert result == "[url=" |
| 161 |
|
| 162 |
|
| 163 |
def test_escape_escape(): |
| 164 |
# Escaped escapes (i.e. double backslash)should be treated as literal |
| 165 |
result = render(r"\\[bold]FOO") |
| 166 |
assert str(result) == r"\FOO" |
| 167 |
|
| 168 |
# Single backslash makes the tag literal |
| 169 |
result = render(r"\[bold]FOO") |
| 170 |
assert str(result) == "[bold]FOO" |
| 171 |
|
| 172 |
# Double backslash produces a backslash |
| 173 |
result = render(r"\\[bold]some text[/]") |
| 174 |
assert str(result) == r"\some text" |
| 175 |
|
| 176 |
# Triple backslash parsed as literal backslash plus escaped tag |
| 177 |
result = render(r"\\\[bold]some text\[/]") |
| 178 |
assert str(result) == r"\[bold]some text[/]" |
| 179 |
|
| 180 |
# Backslash escaping only happens when preceding a tag |
| 181 |
result = render(r"\\") |
| 182 |
assert str(result) == r"\\" |
| 183 |
|
| 184 |
result = render(r"\\\\") |
| 185 |
assert str(result) == r"\\\\" |
| 186 |
|
| 187 |
|
| 188 |
def test_events(): |
| 189 |
result = render("[@click]Hello[/@click] [@click='view.toggle', 'left']World[/]") |
| 190 |
assert str(result) == "Hello World" |
| 191 |
|
| 192 |
|
| 193 |
def test_events_broken(): |
| 194 |
with pytest.raises(MarkupError): |
| 195 |
render("[@click=sdfwer(sfs)]foo[/]") |
| 196 |
|
| 197 |
with pytest.raises(MarkupError): |
| 198 |
render("[@click='view.toggle]foo[/]") |
| 199 |
|
| 200 |
|
| 201 |
def test_render_meta(): |
| 202 |
console = Console() |
| 203 |
text = render("foo[@click=close]bar[/]baz") |
| 204 |
assert text.get_style_at_offset(console, 3).meta == {"@click": ("close", ())} |
| 205 |
|
| 206 |
text = render("foo[@click=close()]bar[/]baz") |
| 207 |
assert text.get_style_at_offset(console, 3).meta == {"@click": ("close", ())} |
| 208 |
|
| 209 |
text = render("foo[@click=close('dialog')]bar[/]baz") |
| 210 |
assert text.get_style_at_offset(console, 3).meta == { |
| 211 |
"@click": ("close", ("dialog",)) |
| 212 |
} |
| 213 |
text = render("foo[@click=close('dialog', 3)]bar[/]baz") |
| 214 |
assert text.get_style_at_offset(console, 3).meta == { |
| 215 |
"@click": ("close", ("dialog", 3)) |
| 216 |
} |
| 217 |
|
| 218 |
text = render("foo[@click=(1, 2, 3)]bar[/]baz") |
| 219 |
assert text.get_style_at_offset(console, 3).meta == {"@click": (1, 2, 3)} |