Ejemplo n.º 1
0
class TestEscaping(unittest.TestCase):

    def setUp(self):
        self.x = Xunit()

    def test_all(self):
        eq_(self.x._quoteattr(
            '''<baz src="http://foo?f=1&b=2" quote="inix hubris 'maximus'?" />'''),
            ('"&lt;baz src=&quot;http://foo?f=1&amp;b=2&quot; '
                'quote=&quot;inix hubris \'maximus\'?&quot; /&gt;"'))

    def test_unicode_is_utf8_by_default(self):
        if not UNICODE_STRINGS:
            eq_(self.x._quoteattr(u'Ivan Krsti\u0107'),
                '"Ivan Krsti\xc4\x87"')

    def test_unicode_custom_utf16_madness(self):
        self.x.encoding = 'utf-16'
        utf16 = self.x._quoteattr(u'Ivan Krsti\u0107')[1:-1]

        if UNICODE_STRINGS:
            # If all internal strings are unicode, then _quoteattr shouldn't
            # have changed anything.
            eq_(utf16, u'Ivan Krsti\u0107')
        else:
            # to avoid big/little endian bytes, assert that we can put it back:
            eq_(utf16.decode('utf16'), u'Ivan Krsti\u0107')

    def test_control_characters(self):
        # quoting of \n, \r varies in diff. python versions
        n = saxutils.quoteattr('\n')[1:-1]
        r = saxutils.quoteattr('\r')[1:-1]
        eq_(self.x._quoteattr('foo\n\b\f\r'), '"foo%s??%s"' % (n, r))
        eq_(escape_cdata('foo\n\b\f\r'), 'foo\n??\r')