Martin v. Löwis | a729daf | 2002-08-04 17:28:33 +0000 | [diff] [blame] | 1 | # -*- coding: iso-8859-1 -*- |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 2 | """ Test script for the Unicode implementation. |
| 3 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 4 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 5 | |
| 6 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 7 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 8 | """#" |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 9 | import codecs |
| 10 | import struct |
| 11 | import sys |
| 12 | import unittest |
| 13 | import warnings |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 14 | from test import support, string_tests |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 15 | |
Neal Norwitz | 430f68b | 2005-11-24 22:00:56 +0000 | [diff] [blame] | 16 | # Error handling (bad decoder return) |
| 17 | def search_function(encoding): |
| 18 | def decode1(input, errors="strict"): |
| 19 | return 42 # not a tuple |
| 20 | def encode1(input, errors="strict"): |
| 21 | return 42 # not a tuple |
| 22 | def encode2(input, errors="strict"): |
| 23 | return (42, 42) # no unicode |
| 24 | def decode2(input, errors="strict"): |
| 25 | return (42, 42) # no unicode |
| 26 | if encoding=="test.unicode1": |
| 27 | return (encode1, decode1, None, None) |
| 28 | elif encoding=="test.unicode2": |
| 29 | return (encode2, decode2, None, None) |
| 30 | else: |
| 31 | return None |
| 32 | codecs.register(search_function) |
| 33 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 34 | class UnicodeTest( |
| 35 | string_tests.CommonTest, |
Walter Dörwald | 57d88e5 | 2004-08-26 16:53:04 +0000 | [diff] [blame] | 36 | string_tests.MixinStrUnicodeUserStringTest, |
| 37 | string_tests.MixinStrUnicodeTest, |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 38 | ): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 39 | type2test = str |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 41 | def setUp(self): |
| 42 | self.warning_filters = warnings.filters[:] |
| 43 | |
| 44 | def tearDown(self): |
| 45 | warnings.filters = self.warning_filters |
| 46 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 47 | def checkequalnofix(self, result, object, methodname, *args): |
| 48 | method = getattr(object, methodname) |
| 49 | realresult = method(*args) |
| 50 | self.assertEqual(realresult, result) |
| 51 | self.assert_(type(realresult) is type(result)) |
| 52 | |
| 53 | # if the original is returned make sure that |
| 54 | # this doesn't happen with subclasses |
| 55 | if realresult is object: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 56 | class usub(str): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 57 | def __repr__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 58 | return 'usub(%r)' % str.__repr__(self) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 59 | object = usub(object) |
| 60 | method = getattr(object, methodname) |
| 61 | realresult = method(*args) |
| 62 | self.assertEqual(realresult, result) |
| 63 | self.assert_(object is not realresult) |
Guido van Rossum | e4874ae | 2001-09-21 15:36:41 +0000 | [diff] [blame] | 64 | |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 65 | def test_literals(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 66 | self.assertEqual('\xff', '\u00ff') |
| 67 | self.assertEqual('\uffff', '\U0000ffff') |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 68 | self.assertRaises(SyntaxError, eval, '\'\\Ufffffffe\'') |
| 69 | self.assertRaises(SyntaxError, eval, '\'\\Uffffffff\'') |
| 70 | self.assertRaises(SyntaxError, eval, '\'\\U%08x\'' % 0x110000) |
Benjamin Peterson | cd76c27 | 2008-04-05 15:09:30 +0000 | [diff] [blame] | 71 | # raw strings should not have unicode escapes |
| 72 | self.assertNotEquals(r"\u0020", " ") |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 73 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 74 | def test_repr(self): |
| 75 | if not sys.platform.startswith('java'): |
| 76 | # Test basic sanity of repr() |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 77 | self.assertEqual(repr('abc'), "'abc'") |
| 78 | self.assertEqual(repr('ab\\c'), "'ab\\\\c'") |
| 79 | self.assertEqual(repr('ab\\'), "'ab\\\\'") |
| 80 | self.assertEqual(repr('\\c'), "'\\\\c'") |
| 81 | self.assertEqual(repr('\\'), "'\\\\'") |
| 82 | self.assertEqual(repr('\n'), "'\\n'") |
| 83 | self.assertEqual(repr('\r'), "'\\r'") |
| 84 | self.assertEqual(repr('\t'), "'\\t'") |
| 85 | self.assertEqual(repr('\b'), "'\\x08'") |
| 86 | self.assertEqual(repr("'\""), """'\\'"'""") |
| 87 | self.assertEqual(repr("'\""), """'\\'"'""") |
| 88 | self.assertEqual(repr("'"), '''"'"''') |
| 89 | self.assertEqual(repr('"'), """'"'""") |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 90 | latin1repr = ( |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 91 | "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r" |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 92 | "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a" |
| 93 | "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI" |
| 94 | "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f" |
| 95 | "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d" |
| 96 | "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b" |
| 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\\xb6\\xb7" |
| 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\\xd2\\xd3" |
| 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\\xee\\xef" |
| 103 | "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd" |
| 104 | "\\xfe\\xff'") |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 105 | testrepr = repr(''.join(map(chr, range(256)))) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 106 | self.assertEqual(testrepr, latin1repr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 107 | # Test repr works on wide unicode escapes without overflow. |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 108 | self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096), |
| 109 | repr("\U00010000" * 39 + "\uffff" * 4096)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 111 | def test_iterators(self): |
| 112 | # Make sure unicode objects have an __iter__ method |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 113 | it = "\u1111\u2222\u3333".__iter__() |
| 114 | self.assertEqual(next(it), "\u1111") |
| 115 | self.assertEqual(next(it), "\u2222") |
| 116 | self.assertEqual(next(it), "\u3333") |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 117 | self.assertRaises(StopIteration, next, it) |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 118 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 119 | def test_count(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 120 | string_tests.CommonTest.test_count(self) |
| 121 | # check mixed argument types |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 122 | self.checkequalnofix(3, 'aaa', 'count', 'a') |
| 123 | self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 124 | self.checkequalnofix(3, 'aaa', 'count', 'a') |
| 125 | self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 126 | self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 127 | self.checkequalnofix(1, 'aaa', 'count', 'a', -1) |
| 128 | self.checkequalnofix(3, 'aaa', 'count', 'a', -10) |
| 129 | self.checkequalnofix(2, 'aaa', 'count', 'a', 0, -1) |
| 130 | self.checkequalnofix(0, 'aaa', 'count', 'a', 0, -10) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 131 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 132 | def test_find(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 133 | self.checkequalnofix(0, 'abcdefghiabc', 'find', 'abc') |
| 134 | self.checkequalnofix(9, 'abcdefghiabc', 'find', 'abc', 1) |
| 135 | self.checkequalnofix(-1, 'abcdefghiabc', 'find', 'def', 4) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 137 | self.assertRaises(TypeError, 'hello'.find) |
| 138 | self.assertRaises(TypeError, 'hello'.find, 42) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 139 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 140 | def test_rfind(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 141 | string_tests.CommonTest.test_rfind(self) |
| 142 | # check mixed argument types |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 143 | self.checkequalnofix(9, 'abcdefghiabc', 'rfind', 'abc') |
| 144 | self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') |
| 145 | self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') |
Guido van Rossum | 8b26454 | 2000-12-19 02:22:31 +0000 | [diff] [blame] | 146 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 147 | def test_index(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 148 | string_tests.CommonTest.test_index(self) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 149 | self.checkequalnofix(0, 'abcdefghiabc', 'index', '') |
| 150 | self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def') |
| 151 | self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc') |
| 152 | self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1) |
| 153 | self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib') |
| 154 | self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1) |
| 155 | self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', 8) |
| 156 | self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', -1) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 157 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 158 | def test_rindex(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 159 | string_tests.CommonTest.test_rindex(self) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 160 | self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '') |
| 161 | self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def') |
| 162 | self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc') |
| 163 | self.checkequalnofix(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 164 | |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 165 | self.assertRaises(ValueError, 'abcdefghiabc'.rindex, 'hib') |
| 166 | self.assertRaises(ValueError, 'defghiabc'.rindex, 'def', 1) |
| 167 | self.assertRaises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1) |
| 168 | self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8) |
| 169 | self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 170 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 171 | def test_maketrans_translate(self): |
| 172 | # these work with plain translate() |
| 173 | self.checkequalnofix('bbbc', 'abababc', 'translate', |
| 174 | {ord('a'): None}) |
| 175 | self.checkequalnofix('iiic', 'abababc', 'translate', |
| 176 | {ord('a'): None, ord('b'): ord('i')}) |
| 177 | self.checkequalnofix('iiix', 'abababc', 'translate', |
| 178 | {ord('a'): None, ord('b'): ord('i'), ord('c'): 'x'}) |
| 179 | self.checkequalnofix('c', 'abababc', 'translate', |
| 180 | {ord('a'): None, ord('b'): ''}) |
| 181 | self.checkequalnofix('xyyx', 'xzx', 'translate', |
| 182 | {ord('z'): 'yy'}) |
| 183 | # this needs maketrans() |
| 184 | self.checkequalnofix('abababc', 'abababc', 'translate', |
| 185 | {'b': '<i>'}) |
| 186 | tbl = self.type2test.maketrans({'a': None, 'b': '<i>'}) |
| 187 | self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', tbl) |
| 188 | # test alternative way of calling maketrans() |
| 189 | tbl = self.type2test.maketrans('abc', 'xyz', 'd') |
| 190 | self.checkequalnofix('xyzzy', 'abdcdcbdddd', 'translate', tbl) |
| 191 | |
| 192 | self.assertRaises(TypeError, self.type2test.maketrans) |
| 193 | self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg') |
| 194 | self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def') |
| 195 | self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 2) |
| 196 | self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def', 2) |
| 197 | self.assertRaises(ValueError, self.type2test.maketrans, {'xy': 2}) |
| 198 | self.assertRaises(TypeError, self.type2test.maketrans, {(1,): 2}) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 200 | self.assertRaises(TypeError, 'hello'.translate) |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 201 | self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 202 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 203 | def test_split(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 204 | string_tests.CommonTest.test_split(self) |
Andrew M. Kuchling | eddd68d | 2002-03-29 16:21:44 +0000 | [diff] [blame] | 205 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 206 | # Mixed arguments |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 207 | self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') |
| 208 | self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') |
| 209 | self.checkequalnofix(['endcase ', ''], 'endcase test', 'split', 'test') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 210 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 211 | def test_join(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 212 | string_tests.MixinStrUnicodeUserStringTest.test_join(self) |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame] | 214 | class MyWrapper: |
| 215 | def __init__(self, sval): self.sval = sval |
| 216 | def __str__(self): return self.sval |
| 217 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 218 | # mixed arguments |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 219 | self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 220 | self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd')) |
| 221 | self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz')) |
| 222 | self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 223 | self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 224 | self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd')) |
| 225 | self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz')) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 226 | self.checkraises(TypeError, ' ', 'join', ['1', '2', MyWrapper('foo')]) |
| 227 | self.checkraises(TypeError, ' ', 'join', ['1', '2', '3', bytes()]) |
| 228 | self.checkraises(TypeError, ' ', 'join', [1, 2, 3]) |
| 229 | self.checkraises(TypeError, ' ', 'join', ['1', '2', 3]) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 230 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 231 | def test_replace(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 232 | string_tests.CommonTest.test_replace(self) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 233 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 234 | # method call forwarded from str implementation because of unicode argument |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 235 | self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1) |
| 236 | self.assertRaises(TypeError, 'replace'.replace, "r", 42) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 238 | def test_bytes_comparison(self): |
| 239 | warnings.simplefilter('ignore', BytesWarning) |
| 240 | self.assertEqual('abc' == b'abc', False) |
| 241 | self.assertEqual('abc' != b'abc', True) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 242 | self.assertEqual('abc' == bytearray(b'abc'), False) |
| 243 | self.assertEqual('abc' != bytearray(b'abc'), True) |
Brett Cannon | 4043001 | 2007-10-22 20:24:51 +0000 | [diff] [blame] | 244 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 245 | def test_comparison(self): |
| 246 | # Comparisons: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 247 | self.assertEqual('abc', 'abc') |
| 248 | self.assertEqual('abc', 'abc') |
| 249 | self.assertEqual('abc', 'abc') |
| 250 | self.assert_('abcd' > 'abc') |
| 251 | self.assert_('abcd' > 'abc') |
| 252 | self.assert_('abcd' > 'abc') |
| 253 | self.assert_('abc' < 'abcd') |
| 254 | self.assert_('abc' < 'abcd') |
| 255 | self.assert_('abc' < 'abcd') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 256 | |
| 257 | if 0: |
| 258 | # Move these tests to a Unicode collation module test... |
| 259 | # Testing UTF-16 code point order comparisons... |
| 260 | |
| 261 | # No surrogates, no fixup required. |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 262 | self.assert_('\u0061' < '\u20ac') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 263 | # Non surrogate below surrogate value, no fixup required |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 264 | self.assert_('\u0061' < '\ud800\udc02') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 265 | |
| 266 | # Non surrogate above surrogate value, fixup required |
| 267 | def test_lecmp(s, s2): |
| 268 | self.assert_(s < s2) |
| 269 | |
| 270 | def test_fixup(s): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 271 | s2 = '\ud800\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 272 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 273 | s2 = '\ud900\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 274 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 275 | s2 = '\uda00\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 276 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 277 | s2 = '\udb00\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 278 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 279 | s2 = '\ud800\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 280 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 281 | s2 = '\ud900\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 282 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 283 | s2 = '\uda00\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 284 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 285 | s2 = '\udb00\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 286 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 287 | s2 = '\ud800\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 288 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 289 | s2 = '\ud900\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 290 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 291 | s2 = '\uda00\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 292 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 293 | s2 = '\udb00\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 294 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 295 | s2 = '\ud800\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 296 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 297 | s2 = '\ud900\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 298 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 299 | s2 = '\uda00\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 300 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 301 | s2 = '\udb00\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 302 | test_lecmp(s, s2) |
| 303 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 304 | test_fixup('\ue000') |
| 305 | test_fixup('\uff61') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 306 | |
| 307 | # Surrogates on both sides, no fixup required |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 308 | self.assert_('\ud800\udc02' < '\ud84d\udc56') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 309 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 310 | def test_islower(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 311 | string_tests.MixinStrUnicodeUserStringTest.test_islower(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 312 | self.checkequalnofix(False, '\u1FFc', 'islower') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 313 | |
| 314 | def test_isupper(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 315 | string_tests.MixinStrUnicodeUserStringTest.test_isupper(self) |
| 316 | if not sys.platform.startswith('java'): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 317 | self.checkequalnofix(False, '\u1FFc', 'isupper') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 318 | |
| 319 | def test_istitle(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 320 | string_tests.MixinStrUnicodeUserStringTest.test_title(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 321 | self.checkequalnofix(True, '\u1FFc', 'istitle') |
| 322 | self.checkequalnofix(True, 'Greek \u1FFcitlecases ...', 'istitle') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 323 | |
| 324 | def test_isspace(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 325 | string_tests.MixinStrUnicodeUserStringTest.test_isspace(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 326 | self.checkequalnofix(True, '\u2000', 'isspace') |
| 327 | self.checkequalnofix(True, '\u200a', 'isspace') |
| 328 | self.checkequalnofix(False, '\u2014', 'isspace') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 329 | |
| 330 | def test_isalpha(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 331 | string_tests.MixinStrUnicodeUserStringTest.test_isalpha(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 332 | self.checkequalnofix(True, '\u1FFc', 'isalpha') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 333 | |
| 334 | def test_isdecimal(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 335 | self.checkequalnofix(False, '', 'isdecimal') |
| 336 | self.checkequalnofix(False, 'a', 'isdecimal') |
| 337 | self.checkequalnofix(True, '0', 'isdecimal') |
| 338 | self.checkequalnofix(False, '\u2460', 'isdecimal') # CIRCLED DIGIT ONE |
| 339 | self.checkequalnofix(False, '\xbc', 'isdecimal') # VULGAR FRACTION ONE QUARTER |
| 340 | self.checkequalnofix(True, '\u0660', 'isdecimal') # ARABIC-INDIC DIGIT ZERO |
| 341 | self.checkequalnofix(True, '0123456789', 'isdecimal') |
| 342 | self.checkequalnofix(False, '0123456789a', 'isdecimal') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 343 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 344 | self.checkraises(TypeError, 'abc', 'isdecimal', 42) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 345 | |
| 346 | def test_isdigit(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 347 | string_tests.MixinStrUnicodeUserStringTest.test_isdigit(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 348 | self.checkequalnofix(True, '\u2460', 'isdigit') |
| 349 | self.checkequalnofix(False, '\xbc', 'isdigit') |
| 350 | self.checkequalnofix(True, '\u0660', 'isdigit') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 351 | |
| 352 | def test_isnumeric(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 353 | self.checkequalnofix(False, '', 'isnumeric') |
| 354 | self.checkequalnofix(False, 'a', 'isnumeric') |
| 355 | self.checkequalnofix(True, '0', 'isnumeric') |
| 356 | self.checkequalnofix(True, '\u2460', 'isnumeric') |
| 357 | self.checkequalnofix(True, '\xbc', 'isnumeric') |
| 358 | self.checkequalnofix(True, '\u0660', 'isnumeric') |
| 359 | self.checkequalnofix(True, '0123456789', 'isnumeric') |
| 360 | self.checkequalnofix(False, '0123456789a', 'isnumeric') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 361 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 362 | self.assertRaises(TypeError, "abc".isnumeric, 42) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 363 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 364 | def test_isidentifier(self): |
| 365 | self.assertTrue("a".isidentifier()) |
| 366 | self.assertTrue("Z".isidentifier()) |
| 367 | self.assertTrue("_".isidentifier()) |
| 368 | self.assertTrue("b0".isidentifier()) |
| 369 | self.assertTrue("bc".isidentifier()) |
| 370 | self.assertTrue("b_".isidentifier()) |
| 371 | self.assertTrue("µ".isidentifier()) |
| 372 | |
| 373 | self.assertFalse(" ".isidentifier()) |
| 374 | self.assertFalse("[".isidentifier()) |
| 375 | self.assertFalse("©".isidentifier()) |
| 376 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 377 | def test_contains(self): |
| 378 | # Testing Unicode contains method |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 379 | self.assert_('a' in 'abdb') |
| 380 | self.assert_('a' in 'bdab') |
| 381 | self.assert_('a' in 'bdaba') |
| 382 | self.assert_('a' in 'bdba') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 383 | self.assert_('a' not in 'bdb') |
| 384 | self.assert_('a' in 'bdba') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 385 | self.assert_('a' in ('a',1,None)) |
| 386 | self.assert_('a' in (1,None,'a')) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 387 | self.assert_('a' in ('a',1,None)) |
| 388 | self.assert_('a' in (1,None,'a')) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 389 | self.assert_('a' not in ('x',1,'y')) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 390 | self.assert_('a' not in ('x',1,None)) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 391 | self.assert_('abcd' not in 'abcxxxx') |
| 392 | self.assert_('ab' in 'abcd') |
| 393 | self.assert_('ab' in 'abc') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 394 | self.assert_('ab' in (1,None,'ab')) |
| 395 | self.assert_('' in 'abc') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 396 | self.assert_('' in '') |
| 397 | self.assert_('' in 'abc') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 398 | self.assert_('\0' not in 'abc') |
| 399 | self.assert_('\0' in '\0abc') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 400 | self.assert_('\0' in 'abc\0') |
| 401 | self.assert_('a' in '\0abc') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 402 | self.assert_('asdf' in 'asdf') |
| 403 | self.assert_('asdf' not in 'asd') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 404 | self.assert_('asdf' not in '') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 406 | self.assertRaises(TypeError, "abc".__contains__) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 407 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 408 | def test_format(self): |
| 409 | self.assertEqual(''.format(), '') |
| 410 | self.assertEqual('a'.format(), 'a') |
| 411 | self.assertEqual('ab'.format(), 'ab') |
| 412 | self.assertEqual('a{{'.format(), 'a{') |
| 413 | self.assertEqual('a}}'.format(), 'a}') |
| 414 | self.assertEqual('{{b'.format(), '{b') |
| 415 | self.assertEqual('}}b'.format(), '}b') |
| 416 | self.assertEqual('a{{b'.format(), 'a{b') |
| 417 | |
| 418 | # examples from the PEP: |
| 419 | import datetime |
| 420 | self.assertEqual("My name is {0}".format('Fred'), "My name is Fred") |
| 421 | self.assertEqual("My name is {0[name]}".format(dict(name='Fred')), |
| 422 | "My name is Fred") |
| 423 | self.assertEqual("My name is {0} :-{{}}".format('Fred'), |
| 424 | "My name is Fred :-{}") |
| 425 | |
| 426 | d = datetime.date(2007, 8, 18) |
| 427 | self.assertEqual("The year is {0.year}".format(d), |
| 428 | "The year is 2007") |
| 429 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 430 | # classes we'll use for testing |
| 431 | class C: |
| 432 | def __init__(self, x=100): |
| 433 | self._x = x |
| 434 | def __format__(self, spec): |
| 435 | return spec |
| 436 | |
| 437 | class D: |
| 438 | def __init__(self, x): |
| 439 | self.x = x |
| 440 | def __format__(self, spec): |
| 441 | return str(self.x) |
| 442 | |
| 443 | # class with __str__, but no __format__ |
| 444 | class E: |
| 445 | def __init__(self, x): |
| 446 | self.x = x |
| 447 | def __str__(self): |
| 448 | return 'E(' + self.x + ')' |
| 449 | |
| 450 | # class with __repr__, but no __format__ or __str__ |
| 451 | class F: |
| 452 | def __init__(self, x): |
| 453 | self.x = x |
| 454 | def __repr__(self): |
| 455 | return 'F(' + self.x + ')' |
| 456 | |
| 457 | # class with __format__ that forwards to string, for some format_spec's |
| 458 | class G: |
| 459 | def __init__(self, x): |
| 460 | self.x = x |
| 461 | def __str__(self): |
| 462 | return "string is " + self.x |
| 463 | def __format__(self, format_spec): |
| 464 | if format_spec == 'd': |
| 465 | return 'G(' + self.x + ')' |
| 466 | return object.__format__(self, format_spec) |
| 467 | |
| 468 | # class that returns a bad type from __format__ |
| 469 | class H: |
| 470 | def __format__(self, format_spec): |
| 471 | return 1.0 |
| 472 | |
Eric Smith | 739e2ad | 2007-08-27 19:07:22 +0000 | [diff] [blame] | 473 | class I(datetime.date): |
| 474 | def __format__(self, format_spec): |
| 475 | return self.strftime(format_spec) |
| 476 | |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 477 | class J(int): |
| 478 | def __format__(self, format_spec): |
| 479 | return int.__format__(self * 2, format_spec) |
| 480 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 481 | |
| 482 | self.assertEqual(''.format(), '') |
| 483 | self.assertEqual('abc'.format(), 'abc') |
| 484 | self.assertEqual('{0}'.format('abc'), 'abc') |
| 485 | self.assertEqual('{0:}'.format('abc'), 'abc') |
| 486 | # self.assertEqual('{ 0 }'.format('abc'), 'abc') |
| 487 | self.assertEqual('X{0}'.format('abc'), 'Xabc') |
| 488 | self.assertEqual('{0}X'.format('abc'), 'abcX') |
| 489 | self.assertEqual('X{0}Y'.format('abc'), 'XabcY') |
| 490 | self.assertEqual('{1}'.format(1, 'abc'), 'abc') |
| 491 | self.assertEqual('X{1}'.format(1, 'abc'), 'Xabc') |
| 492 | self.assertEqual('{1}X'.format(1, 'abc'), 'abcX') |
| 493 | self.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY') |
| 494 | self.assertEqual('{0}'.format(-15), '-15') |
| 495 | self.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc') |
| 496 | self.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc') |
| 497 | self.assertEqual('{{'.format(), '{') |
| 498 | self.assertEqual('}}'.format(), '}') |
| 499 | self.assertEqual('{{}}'.format(), '{}') |
| 500 | self.assertEqual('{{x}}'.format(), '{x}') |
| 501 | self.assertEqual('{{{0}}}'.format(123), '{123}') |
| 502 | self.assertEqual('{{{{0}}}}'.format(), '{{0}}') |
| 503 | self.assertEqual('}}{{'.format(), '}{') |
| 504 | self.assertEqual('}}x{{'.format(), '}x{') |
| 505 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 506 | # weird field names |
| 507 | self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz') |
| 508 | self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz') |
Eric Smith | 4cb4e4e | 2007-09-03 08:40:29 +0000 | [diff] [blame] | 509 | self.assertEqual("{0[ ]}".format({' ':3}), '3') |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 510 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 511 | self.assertEqual('{foo._x}'.format(foo=C(20)), '20') |
| 512 | self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010') |
| 513 | self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc') |
| 514 | self.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc') |
| 515 | self.assertEqual('{0[1]}'.format(['abc', 'def']), 'def') |
| 516 | self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def') |
| 517 | self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def') |
| 518 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 519 | # strings |
| 520 | self.assertEqual('{0:.3s}'.format('abc'), 'abc') |
| 521 | self.assertEqual('{0:.3s}'.format('ab'), 'ab') |
| 522 | self.assertEqual('{0:.3s}'.format('abcdef'), 'abc') |
| 523 | self.assertEqual('{0:.0s}'.format('abcdef'), '') |
| 524 | self.assertEqual('{0:3.3s}'.format('abc'), 'abc') |
| 525 | self.assertEqual('{0:2.3s}'.format('abc'), 'abc') |
| 526 | self.assertEqual('{0:2.2s}'.format('abc'), 'ab') |
| 527 | self.assertEqual('{0:3.2s}'.format('abc'), 'ab ') |
| 528 | self.assertEqual('{0:x<0s}'.format('result'), 'result') |
| 529 | self.assertEqual('{0:x<5s}'.format('result'), 'result') |
| 530 | self.assertEqual('{0:x<6s}'.format('result'), 'result') |
| 531 | self.assertEqual('{0:x<7s}'.format('result'), 'resultx') |
| 532 | self.assertEqual('{0:x<8s}'.format('result'), 'resultxx') |
| 533 | self.assertEqual('{0: <7s}'.format('result'), 'result ') |
| 534 | self.assertEqual('{0:<7s}'.format('result'), 'result ') |
| 535 | self.assertEqual('{0:>7s}'.format('result'), ' result') |
| 536 | self.assertEqual('{0:>8s}'.format('result'), ' result') |
| 537 | self.assertEqual('{0:^8s}'.format('result'), ' result ') |
| 538 | self.assertEqual('{0:^9s}'.format('result'), ' result ') |
| 539 | self.assertEqual('{0:^10s}'.format('result'), ' result ') |
| 540 | self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999) |
| 541 | self.assertEqual('{0:10000}'.format(''), ' ' * 10000) |
| 542 | self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000) |
| 543 | |
| 544 | # format specifiers for user defined type |
| 545 | self.assertEqual('{0:abc}'.format(C()), 'abc') |
| 546 | |
| 547 | # !r and !s coersions |
| 548 | self.assertEqual('{0!s}'.format('Hello'), 'Hello') |
| 549 | self.assertEqual('{0!s:}'.format('Hello'), 'Hello') |
| 550 | self.assertEqual('{0!s:15}'.format('Hello'), 'Hello ') |
| 551 | self.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ') |
| 552 | self.assertEqual('{0!r}'.format('Hello'), "'Hello'") |
| 553 | self.assertEqual('{0!r:}'.format('Hello'), "'Hello'") |
| 554 | self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)') |
| 555 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 556 | # test fallback to object.__format__ |
| 557 | self.assertEqual('{0}'.format({}), '{}') |
| 558 | self.assertEqual('{0}'.format([]), '[]') |
| 559 | self.assertEqual('{0}'.format([1]), '[1]') |
| 560 | self.assertEqual('{0}'.format(E('data')), 'E(data)') |
| 561 | self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ') |
| 562 | self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ') |
| 563 | self.assertEqual('{0:d}'.format(G('data')), 'G(data)') |
| 564 | self.assertEqual('{0:>15s}'.format(G('data')), ' string is data') |
| 565 | self.assertEqual('{0!s}'.format(G('data')), 'string is data') |
| 566 | |
Eric Smith | 739e2ad | 2007-08-27 19:07:22 +0000 | [diff] [blame] | 567 | self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007, |
| 568 | month=8, |
| 569 | day=27)), |
| 570 | "date: 2007-08-27") |
| 571 | |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 572 | # test deriving from a builtin type and overriding __format__ |
| 573 | self.assertEqual("{0}".format(J(10)), "20") |
| 574 | |
| 575 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 576 | # string format specifiers |
| 577 | self.assertEqual('{0:}'.format('a'), 'a') |
| 578 | |
| 579 | # computed format specifiers |
| 580 | self.assertEqual("{0:.{1}}".format('hello world', 5), 'hello') |
| 581 | self.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello') |
| 582 | self.assertEqual("{0:.{precision}s}".format('hello world', precision=5), 'hello') |
| 583 | self.assertEqual("{0:{width}.{precision}s}".format('hello world', width=10, precision=5), 'hello ') |
| 584 | self.assertEqual("{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), 'hello ') |
| 585 | |
| 586 | # test various errors |
| 587 | self.assertRaises(ValueError, '{'.format) |
| 588 | self.assertRaises(ValueError, '}'.format) |
| 589 | self.assertRaises(ValueError, 'a{'.format) |
| 590 | self.assertRaises(ValueError, 'a}'.format) |
| 591 | self.assertRaises(ValueError, '{a'.format) |
| 592 | self.assertRaises(ValueError, '}a'.format) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 593 | self.assertRaises(IndexError, '{0}'.format) |
| 594 | self.assertRaises(IndexError, '{1}'.format, 'abc') |
| 595 | self.assertRaises(KeyError, '{x}'.format) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 596 | self.assertRaises(ValueError, "}{".format) |
| 597 | self.assertRaises(ValueError, "{".format) |
| 598 | self.assertRaises(ValueError, "}".format) |
| 599 | self.assertRaises(ValueError, "abc{0:{}".format) |
| 600 | self.assertRaises(ValueError, "{0".format) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 601 | self.assertRaises(IndexError, "{0.}".format) |
| 602 | self.assertRaises(ValueError, "{0.}".format, 0) |
| 603 | self.assertRaises(IndexError, "{0[}".format) |
Eric Smith | 4cb4e4e | 2007-09-03 08:40:29 +0000 | [diff] [blame] | 604 | self.assertRaises(ValueError, "{0[}".format, []) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 605 | self.assertRaises(KeyError, "{0]}".format) |
| 606 | self.assertRaises(ValueError, "{0.[]}".format, 0) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 607 | self.assertRaises(ValueError, "{0..foo}".format, 0) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 608 | self.assertRaises(ValueError, "{0[0}".format, 0) |
| 609 | self.assertRaises(ValueError, "{0[0:foo}".format, 0) |
| 610 | self.assertRaises(KeyError, "{c]}".format) |
| 611 | self.assertRaises(ValueError, "{{ {{{0}}".format, 0) |
| 612 | self.assertRaises(ValueError, "{0}}".format, 0) |
| 613 | self.assertRaises(KeyError, "{foo}".format, bar=3) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 614 | self.assertRaises(ValueError, "{0!x}".format, 3) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 615 | self.assertRaises(ValueError, "{0!}".format, 0) |
| 616 | self.assertRaises(ValueError, "{0!rs}".format, 0) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 617 | self.assertRaises(ValueError, "{!}".format) |
| 618 | self.assertRaises(ValueError, "{:}".format) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 619 | self.assertRaises(ValueError, "{:s}".format) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 620 | self.assertRaises(ValueError, "{}".format) |
| 621 | |
| 622 | # can't have a replacement on the field name portion |
| 623 | self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4) |
| 624 | |
| 625 | # exceed maximum recursion depth |
| 626 | self.assertRaises(ValueError, "{0:{1:{2}}}".format, 'abc', 's', '') |
| 627 | self.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format, |
| 628 | 0, 1, 2, 3, 4, 5, 6, 7) |
| 629 | |
| 630 | # string format spec errors |
| 631 | self.assertRaises(ValueError, "{0:-s}".format, '') |
| 632 | self.assertRaises(ValueError, format, "", "-") |
| 633 | self.assertRaises(ValueError, "{0:=s}".format, '') |
| 634 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 635 | def test_formatting(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 636 | string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 637 | # Testing Unicode formatting strings... |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 638 | self.assertEqual("%s, %s" % ("abc", "abc"), 'abc, abc') |
| 639 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, 2, 3), 'abc, abc, 1, 2.000000, 3.00') |
| 640 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, -2, 3), 'abc, abc, 1, -2.000000, 3.00') |
| 641 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.5), 'abc, abc, -1, -2.000000, 3.50') |
| 642 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.57), 'abc, abc, -1, -2.000000, 3.57') |
| 643 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 1003.57), 'abc, abc, -1, -2.000000, 1003.57') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 644 | if not sys.platform.startswith('java'): |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 645 | self.assertEqual("%r, %r" % (b"abc", "abc"), "b'abc', 'abc'") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 646 | self.assertEqual("%(x)s, %(y)s" % {'x':"abc", 'y':"def"}, 'abc, def') |
| 647 | self.assertEqual("%(x)s, %(\xfc)s" % {'x':"abc", '\xfc':"def"}, 'abc, def') |
Walter Dörwald | 56fbcb5 | 2003-03-31 18:18:41 +0000 | [diff] [blame] | 648 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 649 | self.assertEqual('%c' % 0x1234, '\u1234') |
| 650 | self.assertRaises(OverflowError, "%c".__mod__, (sys.maxunicode+1,)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 651 | |
| 652 | # formatting jobs delegated from the string implementation: |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 653 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 654 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
| 655 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
| 656 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
| 657 | self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...') |
| 658 | self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...') |
| 659 | self.assertEqual('...%s...%s...%s...%s...' % (1,2,3,"abc"), '...1...2...3...abc...') |
| 660 | self.assertEqual('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,"abc"), '...%...%s...1...2...3...abc...') |
| 661 | self.assertEqual('...%s...' % "abc", '...abc...') |
| 662 | self.assertEqual('%*s' % (5,'abc',), ' abc') |
| 663 | self.assertEqual('%*s' % (-5,'abc',), 'abc ') |
| 664 | self.assertEqual('%*.*s' % (5,2,'abc',), ' ab') |
| 665 | self.assertEqual('%*.*s' % (5,3,'abc',), ' abc') |
| 666 | self.assertEqual('%i %*.*s' % (10, 5,3,'abc',), '10 abc') |
| 667 | self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, 'abc',), '103 abc') |
| 668 | self.assertEqual('%c' % 'a', 'a') |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 669 | class Wrapper: |
| 670 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 671 | return '\u1234' |
| 672 | self.assertEqual('%s' % Wrapper(), '\u1234') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 673 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 674 | @support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR') |
Georg Brandl | da6b107 | 2006-01-20 17:48:54 +0000 | [diff] [blame] | 675 | def test_format_float(self): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 676 | # should not format with a comma, but always with C locale |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 677 | self.assertEqual('1.0', '%.1f' % 1.0) |
Georg Brandl | da6b107 | 2006-01-20 17:48:54 +0000 | [diff] [blame] | 678 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 679 | def test_constructor(self): |
| 680 | # unicode(obj) tests (this maps to PyObject_Unicode() at C level) |
| 681 | |
| 682 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 683 | str('unicode remains unicode'), |
| 684 | 'unicode remains unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 685 | ) |
| 686 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 687 | class UnicodeSubclass(str): |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 688 | pass |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 689 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 690 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 691 | str(UnicodeSubclass('unicode subclass becomes unicode')), |
| 692 | 'unicode subclass becomes unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 693 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 694 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 695 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 696 | str('strings are converted to unicode'), |
| 697 | 'strings are converted to unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 698 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 699 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 700 | class StringCompat: |
| 701 | def __init__(self, x): |
| 702 | self.x = x |
| 703 | def __str__(self): |
| 704 | return self.x |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 705 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 706 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 707 | str(StringCompat('__str__ compatible objects are recognized')), |
| 708 | '__str__ compatible objects are recognized' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 709 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 710 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 711 | # unicode(obj) is compatible to str(): |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 712 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 713 | o = StringCompat('unicode(obj) is compatible to str()') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 714 | self.assertEqual(str(o), 'unicode(obj) is compatible to str()') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 715 | self.assertEqual(str(o), 'unicode(obj) is compatible to str()') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 716 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 717 | for obj in (123, 123.45, 123): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 718 | self.assertEqual(str(obj), str(str(obj))) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 719 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 720 | # unicode(obj, encoding, error) tests (this maps to |
| 721 | # PyUnicode_FromEncodedObject() at C level) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 722 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 723 | if not sys.platform.startswith('java'): |
| 724 | self.assertRaises( |
| 725 | TypeError, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 726 | str, |
| 727 | 'decoding unicode is not supported', |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 728 | 'utf-8', |
| 729 | 'strict' |
| 730 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 731 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 732 | self.assertEqual( |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 733 | str(b'strings are decoded to unicode', 'utf-8', 'strict'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 734 | 'strings are decoded to unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 735 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 736 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 737 | if not sys.platform.startswith('java'): |
| 738 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 739 | str( |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 740 | memoryview(b'character buffers are decoded to unicode'), |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 741 | 'utf-8', |
| 742 | 'strict' |
| 743 | ), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 744 | 'character buffers are decoded to unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 745 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 746 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 747 | self.assertRaises(TypeError, str, 42, 42, 42) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 748 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 749 | def test_codecs_utf7(self): |
| 750 | utfTests = [ |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 751 | ('A\u2262\u0391.', b'A+ImIDkQ.'), # RFC2152 example |
| 752 | ('Hi Mom -\u263a-!', b'Hi Mom -+Jjo--!'), # RFC2152 example |
| 753 | ('\u65E5\u672C\u8A9E', b'+ZeVnLIqe-'), # RFC2152 example |
| 754 | ('Item 3 is \u00a31.', b'Item 3 is +AKM-1.'), # RFC2152 example |
| 755 | ('+', b'+-'), |
| 756 | ('+-', b'+--'), |
| 757 | ('+?', b'+-?'), |
| 758 | ('\?', b'+AFw?'), |
| 759 | ('+?', b'+-?'), |
| 760 | (r'\\?', b'+AFwAXA?'), |
| 761 | (r'\\\?', b'+AFwAXABc?'), |
| 762 | (r'++--', b'+-+---') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 763 | ] |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 764 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 765 | for (x, y) in utfTests: |
| 766 | self.assertEqual(x.encode('utf-7'), y) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 767 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 768 | # surrogates not supported |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 769 | self.assertRaises(UnicodeError, str, b'+3ADYAA-', 'utf-7') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 770 | |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 771 | self.assertEqual(str(b'+3ADYAA-', 'utf-7', 'replace'), '\ufffd') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 772 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 773 | def test_codecs_utf8(self): |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 774 | self.assertEqual(''.encode('utf-8'), b'') |
| 775 | self.assertEqual('\u20ac'.encode('utf-8'), b'\xe2\x82\xac') |
| 776 | self.assertEqual('\ud800\udc02'.encode('utf-8'), b'\xf0\x90\x80\x82') |
| 777 | self.assertEqual('\ud84d\udc56'.encode('utf-8'), b'\xf0\xa3\x91\x96') |
| 778 | self.assertEqual('\ud800'.encode('utf-8'), b'\xed\xa0\x80') |
| 779 | self.assertEqual('\udc00'.encode('utf-8'), b'\xed\xb0\x80') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 780 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 781 | ('\ud800\udc02'*1000).encode('utf-8'), |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 782 | b'\xf0\x90\x80\x82'*1000 |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 783 | ) |
| 784 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 785 | '\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f' |
| 786 | '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00' |
| 787 | '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c' |
| 788 | '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067' |
| 789 | '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das' |
| 790 | ' Nunstuck git und'.encode('utf-8'), |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 791 | b'\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81' |
| 792 | b'\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3' |
| 793 | b'\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe' |
| 794 | b'\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83' |
| 795 | b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8' |
| 796 | b'\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81' |
| 797 | b'\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81' |
| 798 | b'\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3' |
| 799 | b'\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf' |
| 800 | b'\xe3\x80\x8cWenn ist das Nunstuck git und' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 801 | ) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 802 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 803 | # UTF-8 specific decoding tests |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 804 | self.assertEqual(str(b'\xf0\xa3\x91\x96', 'utf-8'), '\U00023456' ) |
| 805 | self.assertEqual(str(b'\xf0\x90\x80\x82', 'utf-8'), '\U00010002' ) |
| 806 | self.assertEqual(str(b'\xe2\x82\xac', 'utf-8'), '\u20ac' ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 807 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 808 | # Other possible utf-8 test cases: |
| 809 | # * strict decoding testing for all of the |
| 810 | # UTF8_ERROR cases in PyUnicode_DecodeUTF8 |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 811 | |
Martin v. Löwis | 0d8e16c | 2003-08-05 06:19:47 +0000 | [diff] [blame] | 812 | def test_codecs_idna(self): |
| 813 | # Test whether trailing dot is preserved |
Walter Dörwald | 1324c6f | 2007-05-11 19:57:05 +0000 | [diff] [blame] | 814 | self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.") |
Martin v. Löwis | 0d8e16c | 2003-08-05 06:19:47 +0000 | [diff] [blame] | 815 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 816 | def test_codecs_errors(self): |
| 817 | # Error handling (encoding) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 818 | self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii') |
| 819 | self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict') |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 820 | self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x") |
| 821 | self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x") |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 822 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 823 | # Error handling (decoding) |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 824 | self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii') |
| 825 | self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii', 'strict') |
| 826 | self.assertEqual(str(b'Andr\202 x', 'ascii', 'ignore'), "Andr x") |
| 827 | self.assertEqual(str(b'Andr\202 x', 'ascii', 'replace'), 'Andr\uFFFD x') |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 828 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 829 | # Error handling (unknown character names) |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 830 | self.assertEqual(b"\\N{foo}xx".decode("unicode-escape", "ignore"), "xx") |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 831 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 832 | # Error handling (truncated escape sequence) |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 833 | self.assertRaises(UnicodeError, b"\\".decode, "unicode-escape") |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 834 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 835 | self.assertRaises(TypeError, b"hello".decode, "test.unicode1") |
| 836 | self.assertRaises(TypeError, str, b"hello", "test.unicode2") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 837 | self.assertRaises(TypeError, "hello".encode, "test.unicode1") |
| 838 | self.assertRaises(TypeError, "hello".encode, "test.unicode2") |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 839 | # executes PyUnicode_Encode() |
| 840 | import imp |
| 841 | self.assertRaises( |
| 842 | ImportError, |
| 843 | imp.find_module, |
| 844 | "non-existing module", |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 845 | ["non-existing dir"] |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 846 | ) |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 847 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 848 | # Error handling (wrong arguments) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 849 | self.assertRaises(TypeError, "hello".encode, 42, 42, 42) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 850 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 851 | # Error handling (PyUnicode_EncodeDecimal()) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 852 | self.assertRaises(UnicodeError, int, "\u0200") |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 853 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 854 | def test_codecs(self): |
| 855 | # Encoding |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 856 | self.assertEqual('hello'.encode('ascii'), b'hello') |
| 857 | self.assertEqual('hello'.encode('utf-7'), b'hello') |
| 858 | self.assertEqual('hello'.encode('utf-8'), b'hello') |
| 859 | self.assertEqual('hello'.encode('utf8'), b'hello') |
| 860 | self.assertEqual('hello'.encode('utf-16-le'), b'h\000e\000l\000l\000o\000') |
| 861 | self.assertEqual('hello'.encode('utf-16-be'), b'\000h\000e\000l\000l\000o') |
| 862 | self.assertEqual('hello'.encode('latin-1'), b'hello') |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 863 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 864 | # Roundtrip safety for BMP (just the first 1024 chars) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 865 | for c in range(1024): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 866 | u = chr(c) |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 867 | for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le', |
| 868 | 'utf-16-be', 'raw_unicode_escape', |
| 869 | 'unicode_escape', 'unicode_internal'): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 870 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 871 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 872 | # Roundtrip safety for BMP (just the first 256 chars) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 873 | for c in range(256): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 874 | u = chr(c) |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 875 | for encoding in ('latin-1',): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 876 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 877 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 878 | # Roundtrip safety for BMP (just the first 128 chars) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 879 | for c in range(128): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 880 | u = chr(c) |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 881 | for encoding in ('ascii',): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 882 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 883 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 884 | # Roundtrip safety for non-BMP (just a few chars) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 885 | u = '\U00010001\U00020002\U00030003\U00040004\U00050005' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 886 | for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be', |
| 887 | #'raw_unicode_escape', |
| 888 | 'unicode_escape', 'unicode_internal'): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 889 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 890 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 891 | # UTF-8 must be roundtrip safe for all UCS-2 code points |
| 892 | # This excludes surrogates: in the full range, there would be |
| 893 | # a surrogate pair (\udbff\udc00), which gets converted back |
| 894 | # to a non-BMP character (\U0010fc00) |
Walter Dörwald | 1324c6f | 2007-05-11 19:57:05 +0000 | [diff] [blame] | 895 | u = ''.join(map(chr, list(range(0,0xd800)) + |
| 896 | list(range(0xe000,0x10000)))) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 897 | for encoding in ('utf-8',): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 898 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 899 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 900 | def test_codecs_charmap(self): |
| 901 | # 0-127 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 902 | s = bytes(range(128)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 903 | for encoding in ( |
| 904 | 'cp037', 'cp1026', |
| 905 | 'cp437', 'cp500', 'cp737', 'cp775', 'cp850', |
| 906 | 'cp852', 'cp855', 'cp860', 'cp861', 'cp862', |
| 907 | 'cp863', 'cp865', 'cp866', |
| 908 | 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', |
| 909 | 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6', |
| 910 | 'iso8859_7', 'iso8859_9', 'koi8_r', 'latin_1', |
| 911 | 'mac_cyrillic', 'mac_latin2', |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 912 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 913 | 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', |
| 914 | 'cp1256', 'cp1257', 'cp1258', |
| 915 | 'cp856', 'cp857', 'cp864', 'cp869', 'cp874', |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 916 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 917 | 'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish', |
| 918 | 'cp1006', 'iso8859_8', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 919 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 920 | ### These have undefined mappings: |
| 921 | #'cp424', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 922 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 923 | ### These fail the round-trip: |
| 924 | #'cp875' |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 925 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 926 | ): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 927 | self.assertEqual(str(s, encoding).encode(encoding), s) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 928 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 929 | # 128-255 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 930 | s = bytes(range(128, 256)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 931 | for encoding in ( |
| 932 | 'cp037', 'cp1026', |
| 933 | 'cp437', 'cp500', 'cp737', 'cp775', 'cp850', |
| 934 | 'cp852', 'cp855', 'cp860', 'cp861', 'cp862', |
| 935 | 'cp863', 'cp865', 'cp866', |
| 936 | 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', |
| 937 | 'iso8859_2', 'iso8859_4', 'iso8859_5', |
| 938 | 'iso8859_9', 'koi8_r', 'latin_1', |
| 939 | 'mac_cyrillic', 'mac_latin2', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 940 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 941 | ### These have undefined mappings: |
| 942 | #'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', |
| 943 | #'cp1256', 'cp1257', 'cp1258', |
| 944 | #'cp424', 'cp856', 'cp857', 'cp864', 'cp869', 'cp874', |
| 945 | #'iso8859_3', 'iso8859_6', 'iso8859_7', |
| 946 | #'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 947 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 948 | ### These fail the round-trip: |
| 949 | #'cp1006', 'cp875', 'iso8859_8', |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 950 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 951 | ): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 952 | self.assertEqual(str(s, encoding).encode(encoding), s) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 953 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 954 | def test_concatenation(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 955 | self.assertEqual(("abc" "def"), "abcdef") |
| 956 | self.assertEqual(("abc" "def"), "abcdef") |
| 957 | self.assertEqual(("abc" "def"), "abcdef") |
| 958 | self.assertEqual(("abc" "def" "ghi"), "abcdefghi") |
| 959 | self.assertEqual(("abc" "def" "ghi"), "abcdefghi") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 960 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 961 | def test_printing(self): |
| 962 | class BitBucket: |
| 963 | def write(self, text): |
| 964 | pass |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 965 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 966 | out = BitBucket() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 967 | print('abc', file=out) |
| 968 | print('abc', 'def', file=out) |
| 969 | print('abc', 'def', file=out) |
| 970 | print('abc', 'def', file=out) |
| 971 | print('abc\n', file=out) |
| 972 | print('abc\n', end=' ', file=out) |
| 973 | print('abc\n', end=' ', file=out) |
| 974 | print('def\n', file=out) |
| 975 | print('def\n', file=out) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 976 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 977 | def test_ucs4(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 978 | x = '\U00100000' |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 979 | y = x.encode("raw-unicode-escape").decode("raw-unicode-escape") |
| 980 | self.assertEqual(x, y) |
| 981 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 982 | # FIXME |
| 983 | #y = r'\U00100000' |
| 984 | #x = y.encode("raw-unicode-escape").decode("raw-unicode-escape") |
| 985 | #self.assertEqual(x, y) |
| 986 | #y = r'\U00010000' |
| 987 | #x = y.encode("raw-unicode-escape").decode("raw-unicode-escape") |
| 988 | #self.assertEqual(x, y) |
| 989 | |
| 990 | #try: |
| 991 | # '\U11111111'.decode("raw-unicode-escape") |
| 992 | #except UnicodeDecodeError as e: |
| 993 | # self.assertEqual(e.start, 0) |
| 994 | # self.assertEqual(e.end, 10) |
| 995 | #else: |
| 996 | # self.fail("Should have raised UnicodeDecodeError") |
| 997 | |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 998 | def test_conversion(self): |
| 999 | # Make sure __unicode__() works properly |
| 1000 | class Foo0: |
| 1001 | def __str__(self): |
| 1002 | return "foo" |
| 1003 | |
| 1004 | class Foo1: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1005 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1006 | return "foo" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1007 | |
| 1008 | class Foo2(object): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1009 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1010 | return "foo" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1011 | |
| 1012 | class Foo3(object): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1013 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1014 | return "foo" |
| 1015 | |
| 1016 | class Foo4(str): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1017 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1018 | return "foo" |
| 1019 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1020 | class Foo5(str): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1021 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1022 | return "foo" |
| 1023 | |
| 1024 | class Foo6(str): |
| 1025 | def __str__(self): |
| 1026 | return "foos" |
| 1027 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1028 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1029 | return "foou" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1030 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1031 | class Foo7(str): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1032 | def __str__(self): |
| 1033 | return "foos" |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1034 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1035 | return "foou" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1036 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1037 | class Foo8(str): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1038 | def __new__(cls, content=""): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1039 | return str.__new__(cls, 2*content) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1040 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1041 | return self |
| 1042 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1043 | class Foo9(str): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1044 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1045 | return "not unicode" |
| 1046 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1047 | self.assertEqual(str(Foo0()), "foo") |
| 1048 | self.assertEqual(str(Foo1()), "foo") |
| 1049 | self.assertEqual(str(Foo2()), "foo") |
| 1050 | self.assertEqual(str(Foo3()), "foo") |
| 1051 | self.assertEqual(str(Foo4("bar")), "foo") |
| 1052 | self.assertEqual(str(Foo5("bar")), "foo") |
| 1053 | self.assertEqual(str(Foo6("bar")), "foou") |
| 1054 | self.assertEqual(str(Foo7("bar")), "foou") |
| 1055 | self.assertEqual(str(Foo8("foo")), "foofoo") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1056 | self.assertEqual(str(Foo9("foo")), "not unicode") |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1057 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1058 | def test_unicode_repr(self): |
| 1059 | class s1: |
| 1060 | def __repr__(self): |
| 1061 | return '\\n' |
| 1062 | |
| 1063 | class s2: |
| 1064 | def __repr__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1065 | return '\\n' |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1066 | |
| 1067 | self.assertEqual(repr(s1()), '\\n') |
| 1068 | self.assertEqual(repr(s2()), '\\n') |
| 1069 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1070 | def test_expandtabs_overflows_gracefully(self): |
| 1071 | # This test only affects 32-bit platforms because expandtabs can only take |
| 1072 | # an int as the max value, not a 64-bit C long. If expandtabs is changed |
| 1073 | # to take a 64-bit long, this test should apply to all platforms. |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 1074 | if sys.maxsize > (1 << 32) or struct.calcsize('P') != 4: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1075 | return |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 1076 | self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1077 | |
| 1078 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1079 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1080 | support.run_unittest(__name__) |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 1081 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1082 | if __name__ == "__main__": |
| 1083 | test_main() |