PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/tests/util.py

http://github.com/hyde/hyde
Python | 42 lines | 36 code | 2 blank | 4 comment | 3 complexity | 2c97273b5013ea974c1f6e7ce24c5dff MD5 | raw file
 1import re
 2import difflib
 3
 4def strip_spaces_between_tags(value):
 5    """
 6    Stolen from `django.util.html`
 7    Returns the given HTML with spaces between tags removed.
 8    """
 9    return re.sub(r'>\s+<', '><', unicode(value))
10
11def assert_no_diff(expected, out):
12    diff = [l for l in difflib.unified_diff(expected.splitlines(True),
13                                                out.splitlines(True),
14                                                n=3)]
15    assert not diff, ''.join(diff)
16
17
18def assert_html_equals(expected, actual, sanitize=None):
19    expected = strip_spaces_between_tags(expected.strip())
20    actual = strip_spaces_between_tags(actual.strip())
21    if sanitize:
22        expected = sanitize(expected)
23        actual = sanitize(actual)
24    assert expected == actual
25
26def trap_exit_fail(f):
27    def test_wrapper(*args):
28        try:
29            f(*args)
30        except SystemExit:
31            assert False
32    test_wrapper.__name__ = f.__name__
33    return test_wrapper
34
35def trap_exit_pass(f):
36    def test_wrapper(*args):
37        try:
38            f(*args)
39        except SystemExit:
40            pass
41    test_wrapper.__name__ = f.__name__
42    return test_wrapper