Debian Codegraph @

tests/test_parser.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import random
4 import string
5 from itertools import zip_longest
6
7 import pytest
8
9 from pytermgui import (
10 StyledText,
11 pretty,
12 tim,
13 tokenize_ansi,
14 tokens_to_markup,
15 tokenize_markup,
16 )
17 from pytermgui.colors import Color, str_to_color
18 from pytermgui.markup import StyledText, Token
19 from pytermgui.markup import tokens as tkns
20 from pytermgui.markup.parsing import parse_tokens
21 from pytermgui.markup.style_maps import CLEARERS, STYLES
22
23
24 def random_plain() -> tkns.PlainToken:
25 value = "".join(random.choices(string.ascii_letters, k=5))
26 value = (
27 value.replace("\n", " ")
28 .replace("\r", " ")
29 .replace("\x0b", " ")
30 .replace("\x0c", " ")
31 )
32
33 return tkns.PlainToken(value[: random.randint(1, len(value) - 1)])
34
35
36 def random_color() -> tkns.ColorToken:
37 background = random.randint(0, 1)
38
39 def _random_std() -> Color:
40 value = str(random.randint(0, 15))
41
42 if background:
43 value = "@" + value
44
45 return value, str_to_color(value)
46
47 def _random_256() -> Color:
48 value = str(random.randint(0, 255))
49
50 if background:
51 value = "@" + value
52
53 return value, str_to_color(value)
54
55 def _random_rgb() -> Color:
56 value = ";".join(map(str, (random.randint(0, 256) for _ in range(3))))
57
58 if background:
59 value = "@" + value
60
61 return value, str_to_color(value)
62
63 generators = [_random_std, _random_256, _random_rgb]
64
65 generate = random.choice(generators)
66 return tkns.ColorToken(*generate())
67
68
69 def random_style() -> tkns.StyleToken:
70 return tkns.StyleToken(random.choice(list(STYLES.keys())))
71
72
73 def random_clear() -> tkns.ClearToken:
74 return tkns.ClearToken(random.choice(list(CLEARERS.keys())))
75
76
77 def random_cursor() -> tkns.CursorToken:
78 pos = tuple(map(str, (random.randint(0, 24), random.randint(0, 80))))
79 return tkns.CursorToken(f"{pos[0]};{pos[1]}", pos[0], pos[1])
80
81
82 def random_hlink() -> tkns.HLinkToken:
83 return tkns.HLinkToken("".join(random.choices(string.ascii_letters, k=20)))
84
85
86 class TestParser:
87 def test_length(self):
88 output = StyledText.first_of(tim.parse("[141 @61 bold]One"))
89 print(output)
90
91 assert len(output) == len(output.plain) == len("One")
92
93 def test_styledtext_index(self):
94 text = StyledText.first_of(tim.parse("[141 @61 bold]Test"))
95 assert text[1] == "\x1b[38;5;141m\x1b[48;5;61m\x1b[1me", ascii(text[1])
96
97 def test_sequence_redundancy(self):
98 output = tim.parse(
99 "[123 245 bold /bold italic]Test", append_reset=False, optimize=True
100 )
101 assert output == "\x1b[38;5;245m\x1b[3mTest", repr(output)
102
103 def test_get_markup(self):
104 base = "[141 @61 bold]Hello[/]"
105 ansi = tim.parse("[141 @61 bold]Hello")
106 markup = tim.get_markup(ansi)
107 assert base == markup
108
109 def test_mutiline_markup(self):
110 base = "[141 @61 bold]Hello[/]\nWhat a beautifull day to look a this [~https://example.com]website[/~] !\nOh and this [~https://example.com]website also[/~]\nHave a nice day !"
111 opti_base = "[141 @61 bold]Hello[/]\nWhat a beautifull day to look a this [~https://example.com]website[/~] !\nOh and this [~https://example.com]website also[/~]\nHave a nice day ![/]"
112
113 tokens = tokenize_markup(base)
114 ansi = parse_tokens(list(tokens))
115 tokens2 = tokenize_ansi(ansi)
116 markup = tokens_to_markup(list(tokens2))
117 assert opti_base == markup
118
119 ansi = tim.parse(base)
120 markup = tim.get_markup(ansi)
121 assert opti_base == markup
122
123 def test_parse(self):
124 assert (
125 tim.parse("[141 @61 bold !upper]Hello")
126 == "\x1b[38;5;141m\x1b[48;5;61m\x1b[1mHELLO\x1b[0m"
127 ), repr(tim.parse("[141 @61 bold !upper]Hello"))
128
129
130 class TestFunctionality:
131 def test_alias(self):
132 tim.alias("test", "141")
133 assert tim.parse("[test]Test") == tim.parse("[141]Test")
134
135 def test_define(self):
136 tim.define("!upper", lambda item: item.upper())
137 assert tim.parse("[!upper]test", append_reset=False) == "TEST"
138 assert tim.parse("[!upper 141]test") == tim.parse("[141]TEST")
139
140 def test_pprint_works(self):
141 for obj in [{1, 2, 3}, "test", set, list(range(10)), {"abc": "efg"}]:
142 pretty.pprint(obj)
143
144 def test_displayhook_works(self):
145 pretty.install()
146
147
148 def test_random_tokens() -> None:
149 def _get_next(previous: tkns.Token | None) -> tkns.Token:
150 generators = [
151 random_plain,
152 random_color,
153 random_style,
154 random_clear,
155 ]
156
157 if previous is not None and (previous.is_plain() or previous.is_hyperlink()):
158 generators.remove(random_plain)
159
160 return random.choice(generators)()
161
162 tokens = []
163 previous = None
164 for _ in range(50):
165 previous = _get_next(previous)
166 tokens.append(previous)
167
168 markup = tokens_to_markup(tokens)
169 ansi = tim.parse(markup, append_reset=False)
170
171 reverse = list(tokenize_ansi(ansi))
172
173 for expected, real in zip_longest(tokens, reverse, fillvalue=None):
174 if expected != real:
175 print(expected, real, sep=" != ")
176
177 assert expected == real, f"Expected {expected}, got {real}"
178
179 assert tokens == reverse