| 1 | # -*- coding: iso-8859-1 -*-
|
|---|
| 2 | """ Test script for the Unicode implementation.
|
|---|
| 3 |
|
|---|
| 4 | Written by Marc-Andre Lemburg ([email protected]).
|
|---|
| 5 |
|
|---|
| 6 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
|
|---|
| 7 |
|
|---|
| 8 | """#"
|
|---|
| 9 | import unittest, sys, string, codecs, new
|
|---|
| 10 | from test import test_support, string_tests
|
|---|
| 11 |
|
|---|
| 12 | # Error handling (bad decoder return)
|
|---|
| 13 | def search_function(encoding):
|
|---|
| 14 | def decode1(input, errors="strict"):
|
|---|
| 15 | return 42 # not a tuple
|
|---|
| 16 | def encode1(input, errors="strict"):
|
|---|
| 17 | return 42 # not a tuple
|
|---|
| 18 | def encode2(input, errors="strict"):
|
|---|
| 19 | return (42, 42) # no unicode
|
|---|
| 20 | def decode2(input, errors="strict"):
|
|---|
| 21 | return (42, 42) # no unicode
|
|---|
| 22 | if encoding=="test.unicode1":
|
|---|
| 23 | return (encode1, decode1, None, None)
|
|---|
| 24 | elif encoding=="test.unicode2":
|
|---|
| 25 | return (encode2, decode2, None, None)
|
|---|
| 26 | else:
|
|---|
| 27 | return None
|
|---|
| 28 | codecs.register(search_function)
|
|---|
| 29 |
|
|---|
| 30 | class UnicodeTest(
|
|---|
| 31 | string_tests.CommonTest,
|
|---|
| 32 | string_tests.MixinStrUnicodeUserStringTest,
|
|---|
| 33 | string_tests.MixinStrUnicodeTest,
|
|---|
| 34 | ):
|
|---|
| 35 | type2test = unicode
|
|---|
| 36 |
|
|---|
| 37 | def checkequalnofix(self, result, object, methodname, *args):
|
|---|
| 38 | method = getattr(object, methodname)
|
|---|
| 39 | realresult = method(*args)
|
|---|
| 40 | self.assertEqual(realresult, result)
|
|---|
| 41 | self.assert_(type(realresult) is type(result))
|
|---|
| 42 |
|
|---|
| 43 | # if the original is returned make sure that
|
|---|
| 44 | # this doesn't happen with subclasses
|
|---|
| 45 | if realresult is object:
|
|---|
| 46 | class usub(unicode):
|
|---|
| 47 | def __repr__(self):
|
|---|
| 48 | return 'usub(%r)' % unicode.__repr__(self)
|
|---|
| 49 | object = usub(object)
|
|---|
| 50 | method = getattr(object, methodname)
|
|---|
| 51 | realresult = method(*args)
|
|---|
| 52 | self.assertEqual(realresult, result)
|
|---|
| 53 | self.assert_(object is not realresult)
|
|---|
| 54 |
|
|---|
| 55 | def test_literals(self):
|
|---|
| 56 | self.assertEqual(u'\xff', u'\u00ff')
|
|---|
| 57 | self.assertEqual(u'\uffff', u'\U0000ffff')
|
|---|
| 58 | self.assertRaises(UnicodeError, eval, 'u\'\\Ufffffffe\'')
|
|---|
| 59 | self.assertRaises(UnicodeError, eval, 'u\'\\Uffffffff\'')
|
|---|
| 60 | self.assertRaises(UnicodeError, eval, 'u\'\\U%08x\'' % 0x110000)
|
|---|
| 61 |
|
|---|
| 62 | def test_repr(self):
|
|---|
| 63 | if not sys.platform.startswith('java'):
|
|---|
| 64 | # Test basic sanity of repr()
|
|---|
| 65 | self.assertEqual(repr(u'abc'), "u'abc'")
|
|---|
| 66 | self.assertEqual(repr(u'ab\\c'), "u'ab\\\\c'")
|
|---|
| 67 | self.assertEqual(repr(u'ab\\'), "u'ab\\\\'")
|
|---|
| 68 | self.assertEqual(repr(u'\\c'), "u'\\\\c'")
|
|---|
| 69 | self.assertEqual(repr(u'\\'), "u'\\\\'")
|
|---|
| 70 | self.assertEqual(repr(u'\n'), "u'\\n'")
|
|---|
| 71 | self.assertEqual(repr(u'\r'), "u'\\r'")
|
|---|
| 72 | self.assertEqual(repr(u'\t'), "u'\\t'")
|
|---|
| 73 | self.assertEqual(repr(u'\b'), "u'\\x08'")
|
|---|
| 74 | self.assertEqual(repr(u"'\""), """u'\\'"'""")
|
|---|
|
|---|