MCPcopy
hub / github.com/Textualize/rich / test_highlight_regex

Function test_highlight_regex

tests/test_text.py:162–227  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

160
161
162def test_highlight_regex():
163 # As a string
164 text = Text("peek-a-boo")
165
166 count = text.highlight_regex(r"NEVER_MATCH", "red")
167 assert count == 0
168 assert len(text._spans) == 0
169
170 # text: peek-a-boo
171 # indx: 0123456789
172 count = text.highlight_regex(r"[a|e|o]+", "red")
173 assert count == 3
174 assert sorted(text._spans) == [
175 Span(1, 3, "red"),
176 Span(5, 6, "red"),
177 Span(8, 10, "red"),
178 ]
179
180 text = Text("Ada Lovelace, Alan Turing")
181
182 count = text.highlight_regex(
183 r"(?P<yellow>[A-Za-z]+)[ ]+(?P<red>[A-Za-z]+)(?P<NEVER_MATCH>NEVER_MATCH)*"
184 )
185
186 # The number of matched name should be 2
187 assert count == 2
188 assert sorted(text._spans) == [
189 Span(0, 3, "yellow"), # Ada
190 Span(4, 12, "red"), # Lovelace
191 Span(14, 18, "yellow"), # Alan
192 Span(19, 25, "red"), # Turing
193 ]
194
195 # As a regular expression object
196 text = Text("peek-a-boo")
197
198 count = text.highlight_regex(re.compile(r"NEVER_MATCH"), "red")
199 assert count == 0
200 assert len(text._spans) == 0
201
202 # text: peek-a-boo
203 # indx: 0123456789
204 count = text.highlight_regex(re.compile(r"[a|e|o]+"), "red")
205 assert count == 3
206 assert sorted(text._spans) == [
207 Span(1, 3, "red"),
208 Span(5, 6, "red"),
209 Span(8, 10, "red"),
210 ]
211
212 text = Text("Ada Lovelace, Alan Turing")
213
214 count = text.highlight_regex(
215 re.compile(
216 r"(?P<yellow>[A-Za-z]+)[ ]+(?P<red>[A-Za-z]+)(?P<NEVER_MATCH>NEVER_MATCH)*"
217 )
218 )
219

Callers

nothing calls this directly

Calls 3

highlight_regexMethod · 0.95
TextClass · 0.90
SpanClass · 0.90

Tested by

no test coverage detected