Skip to content

Commit 794b6c4

Browse files
committed
Use of Text.highlight_regex now tests against receiving a str or a compiled regular expression
1 parent c1d3fca commit 794b6c4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tests/test_text.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
from io import StringIO
24
from typing import List
35

@@ -159,6 +161,7 @@ def test_stylize_negative_index():
159161

160162

161163
def test_highlight_regex():
164+
# As a string
162165
text = Text("peek-a-boo")
163166

164167
count = text.highlight_regex(r"NEVER_MATCH", "red")
@@ -176,6 +179,7 @@ def test_highlight_regex():
176179
]
177180

178181
text = Text("Ada Lovelace, Alan Turing")
182+
179183
count = text.highlight_regex(
180184
r"(?P<yellow>[A-Za-z]+)[ ]+(?P<red>[A-Za-z]+)(?P<NEVER_MATCH>NEVER_MATCH)*"
181185
)
@@ -189,16 +193,51 @@ def test_highlight_regex():
189193
Span(19, 25, "red"), # Turing
190194
]
191195

196+
# As a regular expression object
197+
text = Text("peek-a-boo")
198+
199+
count = text.highlight_regex(re.compile(r"NEVER_MATCH"), "red")
200+
assert count == 0
201+
assert len(text._spans) == 0
202+
203+
# text: peek-a-boo
204+
# indx: 0123456789
205+
count = text.highlight_regex(re.compile(r"[a|e|o]+"), "red")
206+
assert count == 3
207+
assert sorted(text._spans) == [
208+
Span(1, 3, "red"),
209+
Span(5, 6, "red"),
210+
Span(8, 10, "red"),
211+
]
212+
213+
text = Text("Ada Lovelace, Alan Turing")
214+
215+
count = text.highlight_regex(
216+
re.compile(r"(?P<yellow>[A-Za-z]+)[ ]+(?P<red>[A-Za-z]+)(?P<NEVER_MATCH>NEVER_MATCH)*")
217+
)
218+
219+
# The number of matched name should be 2
220+
assert count == 2
221+
assert sorted(text._spans) == [
222+
Span(0, 3, "yellow"), # Ada
223+
Span(4, 12, "red"), # Lovelace
224+
Span(14, 18, "yellow"), # Alan
225+
Span(19, 25, "red"), # Turing
226+
]
227+
228+
192229

193230
def test_highlight_regex_callable():
194231
text = Text("Vulnerability CVE-2018-6543 detected")
195232
re_cve = r"CVE-\d{4}-\d+"
233+
compiled_re_cve = re.compile(r"CVE-\d{4}-\d+")
196234

197235
def get_style(text: str) -> Style:
198236
return Style.parse(
199237
f"bold yellow link https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword={text}"
200238
)
201239

240+
# string
202241
count = text.highlight_regex(re_cve, get_style)
203242
assert count == 1
204243
assert len(text._spans) == 1
@@ -209,6 +248,20 @@ def get_style(text: str) -> Style:
209248
== "https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=CVE-2018-6543"
210249
)
211250

251+
# Clear the tracked _spans for the regular expression object's use
252+
text._spans.clear()
253+
254+
# regular expression object
255+
count = text.highlight_regex(compiled_re_cve, get_style)
256+
assert count == 1
257+
assert len(text._spans) == 1
258+
assert text._spans[0].start == 14
259+
assert text._spans[0].end == 27
260+
assert (
261+
text._spans[0].style.link
262+
== "https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=CVE-2018-6543"
263+
)
264+
212265

213266
def test_highlight_words():
214267
text = Text("Do NOT! touch anything!")

0 commit comments

Comments
 (0)