dosage/tests/test_util.py

64 lines
2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
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-2018 Tobias Gruetzmacher
import pytest
import re
2013-04-08 19:20:01 +00:00
from dosagelib.util import normaliseURL, unescape, 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_unescape(self):
# Test HTML replacement.
assert unescape(u'foo&bar') == u'foo&bar'
assert unescape(u'foo bar') == u'foo\xa0bar'
assert unescape(u'"foo"') == u'"foo"'
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/'
@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()