PageRenderTime 235ms CodeModel.GetById 224ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/utils/html.py

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