dosage/tests/test_util.py

53 lines
1.8 KiB
Python
Raw Normal View History

# -*- coding: iso-8859-1 -*-
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
2013-02-15 17:32:36 +00:00
# Copyright (C) 2012-2013 Bastian Kleineidam
2012-06-20 19:58:13 +00:00
import re
from unittest import TestCase
2012-12-12 16:41:29 +00:00
from dosagelib.util import normaliseURL, unescape, tagre
2012-06-20 19:58:13 +00:00
class URLTest(TestCase):
"""
Tests for URL utility functions.
"""
def test_unescape(self):
# Test HTML replacement.
2012-12-12 16:41:29 +00:00
self.assertEqual(unescape('foo&bar'), 'foo&bar')
2013-02-13 19:02:57 +00:00
self.assertEqual(unescape('foo bar'), u'foo\xa0bar')
self.assertEqual(unescape('"foo"'), '"foo"')
2012-06-20 19:58:13 +00:00
def test_normalisation(self):
# Test URL normalisation.
self.assertEqual(normaliseURL('http://example.com//bar/baz&baz'),
2013-02-13 19:02:57 +00:00
u'http://example.com/bar/baz%26baz')
2012-06-20 19:58:13 +00:00
class RegexTest(TestCase):
ValuePrefix = '/bla/'
TagTests = (
('<img src="%s">', ValuePrefix+'foo', True),
('< img src = "%s" >', ValuePrefix, True),
('<img class="prev" src="%s">', ValuePrefix+'...', True),
('<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),
2012-06-20 19:58:13 +00:00
)
def test_regex(self):
2012-10-11 19:32:42 +00:00
matcher = re.compile(tagre("img", "src", '(%s[^"]*)' % self.ValuePrefix))
2012-06-20 19:58:13 +00:00
for tag, value, domatch in self.TagTests:
self.match_tag(matcher, tag, value, domatch)
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:
2012-10-11 15:02:40 +00:00
self.assertTrue(match, "%s should match %s" % (matcher.pattern, text))
2012-06-20 19:58:13 +00:00
self.assertEqual(match.group(1), value)
else:
2012-10-11 15:02:40 +00:00
self.assertFalse(match, "%s should not match %s" % (matcher.pattern, text))