/hyde/tests/util.py

http://github.com/hyde/hyde · Python · 42 lines · 32 code · 6 blank · 4 comment · 6 complexity · 2c97273b5013ea974c1f6e7ce24c5dff MD5 · raw file

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