68 lines
2 KiB
Python
68 lines
2 KiB
Python
from librefi.utils import regex_search_string
|
|
|
|
HTML_STRING = """
|
|
<form method="POST" action="/?your=mother">
|
|
<input type="hidden" value="tesco" />
|
|
<input type="hidden" name="twoja" value="stara" />
|
|
</form>
|
|
"""
|
|
|
|
PATTERN_1 = r'<form [^>]+action="([^"]+)'
|
|
EXPECTED_RESULT_1 = "/?your=mother"
|
|
|
|
PATTERN_2 = r'<input [^>]*value="([^"]+)"'
|
|
EXPECTED_RESULT_2 = ["tesco", "stara"]
|
|
|
|
|
|
def test_regex_search_single_regex():
|
|
result = regex_search_string(PATTERN_1, HTML_STRING)
|
|
assert result is not None
|
|
assert result == EXPECTED_RESULT_1
|
|
|
|
|
|
def test_regex_search_multiple_regexes():
|
|
result = regex_search_string(
|
|
[r'dupsko', PATTERN_1, r'byle jakie'], HTML_STRING)
|
|
assert result is not None
|
|
assert result == EXPECTED_RESULT_1
|
|
|
|
|
|
def test_regex_search_invalid():
|
|
result = regex_search_string([r'dupsko', r'czorne'], HTML_STRING)
|
|
assert result is None
|
|
assert result != EXPECTED_RESULT_1
|
|
|
|
|
|
def test_regex_search_default():
|
|
default = "sth"
|
|
result = regex_search_string(
|
|
[r'dupsko', r'czorne'], HTML_STRING, default=default)
|
|
assert result is not None
|
|
assert result == default
|
|
|
|
|
|
def test_regex_search_multiple_results():
|
|
results = regex_search_string(PATTERN_2, HTML_STRING, multiple=True)
|
|
assert results is not None
|
|
assert isinstance(results, list)
|
|
assert len(results) == len(EXPECTED_RESULT_2)
|
|
for i in range(len(results)):
|
|
assert isinstance(results[i], str)
|
|
assert results[i] == EXPECTED_RESULT_2[i]
|
|
|
|
|
|
def test_regex_search_whole_single_match():
|
|
result = regex_search_string(PATTERN_1, HTML_STRING, whole_match=True)
|
|
assert result is not None
|
|
assert result.group(1) == EXPECTED_RESULT_1
|
|
|
|
|
|
def test_regex_search_multiple_whole_matches():
|
|
results = regex_search_string(
|
|
PATTERN_2, HTML_STRING, multiple=True, whole_match=True)
|
|
assert results is not None
|
|
assert isinstance(results, list)
|
|
assert len(results) == len(EXPECTED_RESULT_2)
|
|
for i in range(len(results)):
|
|
assert results[i].group(1) == EXPECTED_RESULT_2[i]
|