PageRenderTime 35ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/utils/html.py

https://code.google.com/p/mango-py/
Python | 123 lines | 102 code | 9 blank | 12 comment | 12 complexity | dee8c15e5d4deaad117ee32061e6f521 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import unittest
  2. from django.utils import html
  3. class TestUtilsHtml(unittest.TestCase):
  4. def check_output(self, function, value, output=None):
  5. """
  6. Check that function(value) equals output. If output is None,
  7. check that function(value) equals value.
  8. """
  9. if output is None:
  10. output = value
  11. self.assertEqual(function(value), output)
  12. def test_escape(self):
  13. f = html.escape
  14. items = (
  15. ('&','&'),
  16. ('<', '&lt;'),
  17. ('>', '&gt;'),
  18. ('"', '&quot;'),
  19. ("'", '&#39;'),
  20. )
  21. # Substitution patterns for testing the above items.
  22. patterns = ("%s", "asdf%sfdsa", "%s1", "1%sb")
  23. for value, output in items:
  24. for pattern in patterns:
  25. self.check_output(f, pattern % value, pattern % output)
  26. # Check repeated values.
  27. self.check_output(f, value * 2, output * 2)
  28. # Verify it doesn't double replace &.
  29. self.check_output(f, '<&', '&lt;&amp;')
  30. def test_linebreaks(self):
  31. f = html.linebreaks
  32. items = (
  33. ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
  34. ("para1\nsub1\rsub2\n\npara2", "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"),
  35. ("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br />sub1</p>\n\n<p>para4</p>"),
  36. ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
  37. )
  38. for value, output in items:
  39. self.check_output(f, value, output)
  40. def test_strip_tags(self):
  41. f = html.strip_tags
  42. items = (
  43. ('<adf>a', 'a'),
  44. ('</adf>a', 'a'),
  45. ('<asdf><asdf>e', 'e'),
  46. ('<f', '<f'),
  47. ('</fe', '</fe'),
  48. ('<x>b<y>', 'b'),
  49. )
  50. for value, output in items:
  51. self.check_output(f, value, output)
  52. def test_strip_spaces_between_tags(self):
  53. f = html.strip_spaces_between_tags
  54. # Strings that should come out untouched.
  55. items = (' <adf>', '<adf> ', ' </adf> ', ' <f> x</f>')
  56. for value in items:
  57. self.check_output(f, value)
  58. # Strings that have spaces to strip.
  59. items = (
  60. ('<d> </d>', '<d></d>'),
  61. ('<p>hello </p>\n<p> world</p>', '<p>hello </p><p> world</p>'),
  62. ('\n<p>\t</p>\n<p> </p>\n', '\n<p></p><p></p>\n'),
  63. )
  64. for value, output in items:
  65. self.check_output(f, value, output)
  66. def test_strip_entities(self):
  67. f = html.strip_entities
  68. # Strings that should come out untouched.
  69. values = ("&", "&a", "&a", "a&#a")
  70. for value in values:
  71. self.check_output(f, value)
  72. # Valid entities that should be stripped from the patterns.
  73. entities = ("&#1;", "&#12;", "&a;", "&fdasdfasdfasdf;")
  74. patterns = (
  75. ("asdf %(entity)s ", "asdf "),
  76. ("%(entity)s%(entity)s", ""),
  77. ("&%(entity)s%(entity)s", "&"),
  78. ("%(entity)s3", "3"),
  79. )
  80. for entity in entities:
  81. for in_pattern, output in patterns:
  82. self.check_output(f, in_pattern % {'entity': entity}, output)
  83. def test_fix_ampersands(self):
  84. f = html.fix_ampersands
  85. # Strings without ampersands or with ampersands already encoded.
  86. values = ("a&#1;", "b", "&a;", "&amp; &x; ", "asdf")
  87. patterns = (
  88. ("%s", "%s"),
  89. ("&%s", "&amp;%s"),
  90. ("&%s&", "&amp;%s&amp;"),
  91. )
  92. for value in values:
  93. for in_pattern, out_pattern in patterns:
  94. self.check_output(f, in_pattern % value, out_pattern % value)
  95. # Strings with ampersands that need encoding.
  96. items = (
  97. ("&#;", "&amp;#;"),
  98. ("&#875 ;", "&amp;#875 ;"),
  99. ("&#4abc;", "&amp;#4abc;"),
  100. )
  101. for value, output in items:
  102. self.check_output(f, value, output)
  103. def test_escapejs(self):
  104. f = html.escapejs
  105. items = (
  106. (u'"double quotes" and \'single quotes\'', u'\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027'),
  107. (ur'\ : backslashes, too', u'\\u005C : backslashes, too'),
  108. (u'and lots of whitespace: \r\n\t\v\f\b', u'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008'),
  109. (ur'<script>and this</script>', u'\\u003Cscript\\u003Eand this\\u003C/script\\u003E'),
  110. (u'paragraph separator:\u2029and line separator:\u2028', u'paragraph separator:\\u2029and line separator:\\u2028'),
  111. )
  112. for value, output in items:
  113. self.check_output(f, value, output)