| OLD | NEW |
| 1 # -*- coding: iso-8859-1 -*- | 1 # -*- coding: iso-8859-1 -*- |
| 2 """ Test script for the Unicode implementation. | 2 """ Test script for the Unicode implementation. |
| 3 | 3 |
| 4 Written by Marc-Andre Lemburg (mal@lemburg.com). | 4 Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 5 | 5 |
| 6 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. | 6 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 7 | 7 |
| 8 """#" | 8 """#" |
| 9 import codecs | 9 import codecs |
| 10 import struct | 10 import struct |
| 11 import sys | 11 import sys |
| 12 import unittest | 12 import unittest |
| 13 import warnings | 13 import warnings |
| 14 from test import test_support, string_tests | 14 from test import test_support, string_tests |
| 15 | 15 |
| 16 # Error handling (bad decoder return) | 16 # Error handling (bad decoder return) |
| 17 def search_function(encoding): | 17 def search_function(encoding): |
| 18 def decode1(input, errors="strict"): | 18 def decode1(input, errors="strict"): |
| 19 return 42 # not a tuple | 19 return 42 # not a tuple |
| 20 def encode1(input, errors="strict"): | 20 def encode1(input, errors="strict"): |
| 21 return 42 # not a tuple | 21 return 42 # not a tuple |
| 22 def encode2(input, errors="strict"): | 22 def encode2(input, errors="strict"): |
| 23 return (42, 42) # no unicode | 23 return (42, 42) # no unicode |
| 24 def decode2(input, errors="strict"): | 24 def decode2(input, errors="strict"): |
| 25 return (42, 42) # no unicode | 25 return (42, 42) # no unicode |
| 26 if encoding=="test.unicode1": | 26 if encoding=="test.unicode1": |
| 27 return (encode1, decode1, None, None) | 27 return (encode1, decode1, None, None) |
| 28 elif encoding=="test.unicode2": | 28 elif encoding=="test.unicode2": |
| 29 return (encode2, decode2, None, None) | 29 return (encode2, decode2, None, None) |
| 30 else: | 30 else: |
| 31 return None | 31 return None |
| 32 codecs.register(search_function) | 32 codecs.register(search_function) |
| 33 | 33 |
| 34 class UnicodeTest( | 34 class UnicodeTest( |
| 35 string_tests.CommonTest, | 35 string_tests.CommonTest, |
| 36 string_tests.MixinStrUnicodeUserStringTest, | 36 string_tests.MixinStrUnicodeUserStringTest, |
| 37 string_tests.MixinStrUnicodeTest, | 37 string_tests.MixinStrUnicodeTest, |
| 38 ): | 38 ): |
| 39 type2test = str | 39 type2test = str |
| 40 | 40 |
| 41 def setUp(self): | 41 def setUp(self): |
| 42 self.warning_filters = warnings.filters[:] | 42 self.warning_filters = warnings.filters[:] |
| 43 | 43 |
| 44 def tearDown(self): | 44 def tearDown(self): |
| 45 warnings.filters = self.warning_filters | 45 warnings.filters = self.warning_filters |
| 46 | 46 |
| 47 def checkequalnofix(self, result, object, methodname, *args): | 47 def checkequalnofix(self, result, object, methodname, *args): |
| 48 method = getattr(object, methodname) | 48 method = getattr(object, methodname) |
| 49 realresult = method(*args) | 49 realresult = method(*args) |
| 50 self.assertEqual(realresult, result) | 50 self.assertEqual(realresult, result) |
| 51 self.assert_(type(realresult) is type(result)) | 51 self.assert_(type(realresult) is type(result)) |
| 52 | 52 |
| 53 # if the original is returned make sure that | 53 # if the original is returned make sure that |
| 54 # this doesn't happen with subclasses | 54 # this doesn't happen with subclasses |
| 55 if realresult is object: | 55 if realresult is object: |
| 56 class usub(str): | 56 class usub(str): |
| 57 def __repr__(self): | 57 def __repr__(self): |
| 58 return 'usub(%r)' % str.__repr__(self) | 58 return 'usub(%r)' % str.__repr__(self) |
| 59 object = usub(object) | 59 object = usub(object) |
| 60 method = getattr(object, methodname) | 60 method = getattr(object, methodname) |
| 61 realresult = method(*args) | 61 realresult = method(*args) |
| 62 self.assertEqual(realresult, result) | 62 self.assertEqual(realresult, result) |
| 63 self.assert_(object is not realresult) | 63 self.assert_(object is not realresult) |
| 64 | 64 |
| 65 def test_literals(self): | 65 def test_literals(self): |
| 66 self.assertEqual('\xff', '\u00ff') | 66 self.assertEqual('\xff', '\u00ff') |
| 67 self.assertEqual('\uffff', '\U0000ffff') | 67 self.assertEqual('\uffff', '\U0000ffff') |
| 68 self.assertRaises(SyntaxError, eval, '\'\\Ufffffffe\'') | 68 self.assertRaises(SyntaxError, eval, '\'\\Ufffffffe\'') |
| 69 self.assertRaises(SyntaxError, eval, '\'\\Uffffffff\'') | 69 self.assertRaises(SyntaxError, eval, '\'\\Uffffffff\'') |
| 70 self.assertRaises(SyntaxError, eval, '\'\\U%08x\'' % 0x110000) | 70 self.assertRaises(SyntaxError, eval, '\'\\U%08x\'' % 0x110000) |
| 71 # raw strings should not have unicode escapes | 71 # raw strings should not have unicode escapes |
| 72 self.assertNotEquals(r"\u0020", " ") | 72 self.assertNotEquals(r"\u0020", " ") |
| 73 | 73 |
| 74 def test_repr(self): | 74 def test_repr(self): |
| 75 if not sys.platform.startswith('java'): | 75 if not sys.platform.startswith('java'): |
| 76 # Test basic sanity of repr() | 76 # Test basic sanity of repr() |
| 77 self.assertEqual(repr('abc'), "'abc'") | 77 self.assertEqual(repr('abc'), "'abc'") |
| 78 self.assertEqual(repr('ab\\c'), "'ab\\\\c'") | 78 self.assertEqual(repr('ab\\c'), "'ab\\\\c'") |
| 79 self.assertEqual(repr('ab\\'), "'ab\\\\'") | 79 self.assertEqual(repr('ab\\'), "'ab\\\\'") |
| 80 self.assertEqual(repr('\\c'), "'\\\\c'") | 80 self.assertEqual(repr('\\c'), "'\\\\c'") |
| 81 self.assertEqual(repr('\\'), "'\\\\'") | 81 self.assertEqual(repr('\\'), "'\\\\'") |
| 82 self.assertEqual(repr('\n'), "'\\n'") | 82 self.assertEqual(repr('\n'), "'\\n'") |
| 83 self.assertEqual(repr('\r'), "'\\r'") | 83 self.assertEqual(repr('\r'), "'\\r'") |
| 84 self.assertEqual(repr('\t'), "'\\t'") | 84 self.assertEqual(repr('\t'), "'\\t'") |
| 85 self.assertEqual(repr('\b'), "'\\x08'") | 85 self.assertEqual(repr('\b'), "'\\x08'") |
| 86 self.assertEqual(repr("'\""), """'\\'"'""") | 86 self.assertEqual(repr("'\""), """'\\'"'""") |
| 87 self.assertEqual(repr("'\""), """'\\'"'""") | 87 self.assertEqual(repr("'\""), """'\\'"'""") |
| 88 self.assertEqual(repr("'"), '''"'"''') | 88 self.assertEqual(repr("'"), '''"'"''') |
| 89 self.assertEqual(repr('"'), """'"'""") | 89 self.assertEqual(repr('"'), """'"'""") |
| 90 latin1repr = ( | 90 latin1repr = ( |
| 91 "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\
\r" | 91 "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\
\r" |
| 92 "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x
1a" | 92 "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x
1a" |
| 93 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@AB
CDEFGHI" | 93 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@AB
CDEFGHI" |
| 94 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f" | 94 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f" |
| 95 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x
8c\\x8d" | 95 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x
8c\\x8d" |
| 96 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x
9a\\x9b" | 96 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x
9a\\x9b" |
| 97 "\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\x
a8\\xa9" | 97 "\\x9c\\x9d\\x9e\\x9f\\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9" |
| 98 "\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\x
b6\\xb7" | 98 "\xaa\xab\xac\\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7" |
| 99 "\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\x
c4\\xc5" | 99 "\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5" |
| 100 "\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\x
d2\\xd3" | 100 "\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3" |
| 101 "\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\x
e0\\xe1" | 101 "\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1" |
| 102 "\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\x
ee\\xef" | 102 "\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" |
| 103 "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\x
fc\\xfd" | 103 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd" |
| 104 "\\xfe\\xff'") | 104 "\xfe\xff'") |
| 105 testrepr = repr(''.join(map(chr, range(256)))) | 105 testrepr = repr(''.join(map(chr, range(256)))) |
| 106 self.assertEqual(testrepr, latin1repr) | 106 self.assertEqual(testrepr, latin1repr) |
| 107 # Test repr works on wide unicode escapes without overflow. | 107 # Test repr works on wide unicode escapes without overflow. |
| 108 self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096), | 108 self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096), |
| 109 repr("\U00010000" * 39 + "\uffff" * 4096)) | 109 repr("\U00010000" * 39 + "\uffff" * 4096)) |
| 110 | 110 |
| 111 def test_iterators(self): | 111 def test_iterators(self): |
| 112 # Make sure unicode objects have an __iter__ method | 112 # Make sure unicode objects have an __iter__ method |
| 113 it = "\u1111\u2222\u3333".__iter__() | 113 it = "\u1111\u2222\u3333".__iter__() |
| 114 self.assertEqual(next(it), "\u1111") | 114 self.assertEqual(next(it), "\u1111") |
| 115 self.assertEqual(next(it), "\u2222") | 115 self.assertEqual(next(it), "\u2222") |
| 116 self.assertEqual(next(it), "\u3333") | 116 self.assertEqual(next(it), "\u3333") |
| 117 self.assertRaises(StopIteration, next, it) | 117 self.assertRaises(StopIteration, next, it) |
| 118 | 118 |
| 119 def test_count(self): | 119 def test_count(self): |
| 120 string_tests.CommonTest.test_count(self) | 120 string_tests.CommonTest.test_count(self) |
| 121 # check mixed argument types | 121 # check mixed argument types |
| 122 self.checkequalnofix(3, 'aaa', 'count', 'a') | 122 self.checkequalnofix(3, 'aaa', 'count', 'a') |
| 123 self.checkequalnofix(0, 'aaa', 'count', 'b') | 123 self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 124 self.checkequalnofix(3, 'aaa', 'count', 'a') | 124 self.checkequalnofix(3, 'aaa', 'count', 'a') |
| 125 self.checkequalnofix(0, 'aaa', 'count', 'b') | 125 self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 126 self.checkequalnofix(0, 'aaa', 'count', 'b') | 126 self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 127 self.checkequalnofix(1, 'aaa', 'count', 'a', -1) | 127 self.checkequalnofix(1, 'aaa', 'count', 'a', -1) |
| 128 self.checkequalnofix(3, 'aaa', 'count', 'a', -10) | 128 self.checkequalnofix(3, 'aaa', 'count', 'a', -10) |
| 129 self.checkequalnofix(2, 'aaa', 'count', 'a', 0, -1) | 129 self.checkequalnofix(2, 'aaa', 'count', 'a', 0, -1) |
| 130 self.checkequalnofix(0, 'aaa', 'count', 'a', 0, -10) | 130 self.checkequalnofix(0, 'aaa', 'count', 'a', 0, -10) |
| 131 | 131 |
| 132 def test_find(self): | 132 def test_find(self): |
| 133 self.checkequalnofix(0, 'abcdefghiabc', 'find', 'abc') | 133 self.checkequalnofix(0, 'abcdefghiabc', 'find', 'abc') |
| 134 self.checkequalnofix(9, 'abcdefghiabc', 'find', 'abc', 1) | 134 self.checkequalnofix(9, 'abcdefghiabc', 'find', 'abc', 1) |
| 135 self.checkequalnofix(-1, 'abcdefghiabc', 'find', 'def', 4) | 135 self.checkequalnofix(-1, 'abcdefghiabc', 'find', 'def', 4) |
| 136 | 136 |
| 137 self.assertRaises(TypeError, 'hello'.find) | 137 self.assertRaises(TypeError, 'hello'.find) |
| 138 self.assertRaises(TypeError, 'hello'.find, 42) | 138 self.assertRaises(TypeError, 'hello'.find, 42) |
| 139 | 139 |
| 140 def test_rfind(self): | 140 def test_rfind(self): |
| 141 string_tests.CommonTest.test_rfind(self) | 141 string_tests.CommonTest.test_rfind(self) |
| 142 # check mixed argument types | 142 # check mixed argument types |
| 143 self.checkequalnofix(9, 'abcdefghiabc', 'rfind', 'abc') | 143 self.checkequalnofix(9, 'abcdefghiabc', 'rfind', 'abc') |
| 144 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') | 144 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') |
| 145 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') | 145 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') |
| 146 | 146 |
| 147 def test_index(self): | 147 def test_index(self): |
| 148 string_tests.CommonTest.test_index(self) | 148 string_tests.CommonTest.test_index(self) |
| 149 self.checkequalnofix(0, 'abcdefghiabc', 'index', '') | 149 self.checkequalnofix(0, 'abcdefghiabc', 'index', '') |
| 150 self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def') | 150 self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def') |
| 151 self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc') | 151 self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc') |
| 152 self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1) | 152 self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1) |
| 153 self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib') | 153 self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib') |
| 154 self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1) | 154 self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1) |
| (...skipping 879 matching lines...) Show 10 above Show 10 below |
| 1034 def __str__(self): | 1034 def __str__(self): |
| 1035 return "foou" | 1035 return "foou" |
| 1036 | 1036 |
| 1037 class Foo8(str): | 1037 class Foo8(str): |
| 1038 def __new__(cls, content=""): | 1038 def __new__(cls, content=""): |
| 1039 return str.__new__(cls, 2*content) | 1039 return str.__new__(cls, 2*content) |
| 1040 def __str__(self): | 1040 def __str__(self): |
| 1041 return self | 1041 return self |
| 1042 | 1042 |
| 1043 class Foo9(str): | 1043 class Foo9(str): |
| 1044 def __str__(self): | 1044 def __str__(self): |
| 1045 return "not unicode" | 1045 return "not unicode" |
| 1046 | 1046 |
| 1047 self.assertEqual(str(Foo0()), "foo") | 1047 self.assertEqual(str(Foo0()), "foo") |
| 1048 self.assertEqual(str(Foo1()), "foo") | 1048 self.assertEqual(str(Foo1()), "foo") |
| 1049 self.assertEqual(str(Foo2()), "foo") | 1049 self.assertEqual(str(Foo2()), "foo") |
| 1050 self.assertEqual(str(Foo3()), "foo") | 1050 self.assertEqual(str(Foo3()), "foo") |
| 1051 self.assertEqual(str(Foo4("bar")), "foo") | 1051 self.assertEqual(str(Foo4("bar")), "foo") |
| 1052 self.assertEqual(str(Foo5("bar")), "foo") | 1052 self.assertEqual(str(Foo5("bar")), "foo") |
| 1053 self.assertEqual(str(Foo6("bar")), "foou") | 1053 self.assertEqual(str(Foo6("bar")), "foou") |
| 1054 self.assertEqual(str(Foo7("bar")), "foou") | 1054 self.assertEqual(str(Foo7("bar")), "foou") |
| 1055 self.assertEqual(str(Foo8("foo")), "foofoo") | 1055 self.assertEqual(str(Foo8("foo")), "foofoo") |
| 1056 self.assertEqual(str(Foo9("foo")), "not unicode") | 1056 self.assertEqual(str(Foo9("foo")), "not unicode") |
| 1057 | 1057 |
| 1058 def test_unicode_repr(self): | 1058 def test_unicode_repr(self): |
| 1059 class s1: | 1059 class s1: |
| 1060 def __repr__(self): | 1060 def __repr__(self): |
| 1061 return '\\n' | 1061 return '\\n' |
| 1062 | 1062 |
| 1063 class s2: | 1063 class s2: |
| 1064 def __repr__(self): | 1064 def __repr__(self): |
| 1065 return '\\n' | 1065 return '\\n' |
| 1066 | 1066 |
| 1067 self.assertEqual(repr(s1()), '\\n') | 1067 self.assertEqual(repr(s1()), '\\n') |
| 1068 self.assertEqual(repr(s2()), '\\n') | 1068 self.assertEqual(repr(s2()), '\\n') |
| 1069 | 1069 |
| 1070 def test_expandtabs_overflows_gracefully(self): | 1070 def test_expandtabs_overflows_gracefully(self): |
| 1071 # This test only affects 32-bit platforms because expandtabs can only ta
ke | 1071 # This test only affects 32-bit platforms because expandtabs can only ta
ke |
| 1072 # an int as the max value, not a 64-bit C long. If expandtabs is change
d | 1072 # an int as the max value, not a 64-bit C long. If expandtabs is change
d |
| 1073 # to take a 64-bit long, this test should apply to all platforms. | 1073 # to take a 64-bit long, this test should apply to all platforms. |
| 1074 if sys.maxsize > (1 << 32) or struct.calcsize('P') != 4: | 1074 if sys.maxsize > (1 << 32) or struct.calcsize('P') != 4: |
| 1075 return | 1075 return |
| 1076 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize) | 1076 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize) |
| 1077 | 1077 |
| 1078 | 1078 |
| 1079 def test_main(): | 1079 def test_main(): |
| 1080 test_support.run_unittest(__name__) | 1080 test_support.run_unittest(__name__) |
| 1081 | 1081 |
| 1082 if __name__ == "__main__": | 1082 if __name__ == "__main__": |
| 1083 test_main() | 1083 test_main() |
| OLD | NEW |