Add URL regex pattern

This commit is contained in:
Veronica Berglyd Olsen
2024-10-25 17:49:53 +02:00
parent 439a04c603
commit 37268b873a
3 changed files with 53 additions and 2 deletions
+46 -2
View File
@@ -40,6 +40,48 @@ def allMatches(regEx: re.Pattern, text: str) -> list[list[str]]:
return result
@pytest.mark.core
def testTextPatterns_Urls():
"""Test the URL regex."""
regEx = REGEX_PATTERNS.url
valid = [
"http://example.com",
"http://example.com/",
"http://example.com/path+to+page",
"http://example.com/path-to-page",
"http://example.com/path_to_page",
"http://example.com/path~to~page",
"http://example.com/path/to/page",
"http://example.com/path/to/page.html",
"http://example.com/path/to/page.html#title",
"http://example.com/path/to/page.html#title%20here",
"http://example.com/path/to/page.html#title%20here",
"http://example.com/path/to/page?foo=bar&bar=baz",
"http://example.com/path/to/page.html?foo=bar&bar=baz",
"http://example.com/path/to/page.html#title?foo=bar&bar=baz",
"http://user:password@example.com/",
"http://www.example.com/",
"http://www.www.example.com/",
"http://www.www.www.example.com/",
"https://example.com",
"https://www.example.com/",
]
invalid = [
"hppt://example.com/",
"sftp://example.com/",
"http:/example.com/",
"http://www example com/",
"http://www\texample\tcom/",
]
for test in valid:
assert allMatches(regEx, f"Text {test} more text") == [[(test, 5, 5 + len(test))]]
for test in invalid:
assert allMatches(regEx, f"Text {test} more text") == []
@pytest.mark.core
def testTextPatterns_Words():
"""Test the word split regex."""
@@ -198,8 +240,10 @@ def testTextPatterns_ShortcodesPlain():
assert allMatches(regEx, "one [x]two[/x] three") == []
# Line Break Substitution
# =======================
@pytest.mark.core
def testTextPatterns_LineBreakReplace():
"""Test replacing forced line breaks."""
regEx = REGEX_PATTERNS.lineBreak
assert regEx.sub("\n", "one[br]two") == "one\ntwo"