dosage/tests/test_util.py

58 lines
1.8 KiB
Python
Raw Permalink Normal View History

# SPDX-License-Identifier: MIT
2016-10-28 22:21:41 +00:00
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
2014-01-05 15:50:57 +00:00
# Copyright (C) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2020 Tobias Gruetzmacher
import pytest
import re
from dosagelib.util import normaliseURL, tagre, get_system_uid
2012-06-20 19:58:13 +00:00
class TestURL(object):
2012-06-20 19:58:13 +00:00
"""
Tests for URL utility functions.
"""
2012-06-20 19:58:13 +00:00
def test_normalisation(self):
# Test URL normalisation.
assert (normaliseURL('http://example.com//bar/baz&baz') ==
u'http://example.com/bar/baz&baz')
2012-06-20 19:58:13 +00:00
class TestRegex(object):
2012-06-20 19:58:13 +00:00
ValuePrefix = '/bla/'
2020-04-18 11:03:02 +00:00
@pytest.mark.parametrize(('tag', 'value', 'domatch'), [
2017-05-14 22:54:02 +00:00
('<img src="%s">', ValuePrefix + 'foo', True),
2012-06-20 19:58:13 +00:00
('< img src = "%s" >', ValuePrefix, True),
2017-05-14 22:54:02 +00:00
('<img class="prev" src="%s">', ValuePrefix + '...', True),
2012-06-20 19:58:13 +00:00
('<img origsrc="%s">', ValuePrefix, False),
('<Img src="%s">', ValuePrefix, True),
('<img SrC="%s">', ValuePrefix, True),
('<img src="%s">', ValuePrefix[:-1], False),
2012-10-11 19:32:42 +00:00
('<img class="prev" src="%s" a="b">', ValuePrefix, True),
])
def test_regex(self, tag, value, domatch):
matcher = re.compile(tagre("img", "src", '(%s[^"]*)' %
self.ValuePrefix))
self.match_tag(matcher, tag, value, domatch)
2012-06-20 19:58:13 +00:00
def match_tag(self, matcher, tag, value, domatch=True):
2012-10-11 15:02:40 +00:00
text = tag % value
match = matcher.search(text)
2012-06-20 19:58:13 +00:00
if domatch:
assert match, "%s should match %s" % (matcher.pattern, text)
assert match.group(1) == value
2012-06-20 19:58:13 +00:00
else:
assert not match, "%s should not match %s" % (matcher.pattern,
text)
2013-04-08 19:20:01 +00:00
class TestUid(object):
2013-04-08 19:20:01 +00:00
"""
Tests for unique system IDs.
"""
def test_system_uid(self):
assert get_system_uid()