diff options
author | Georg Brandl <georg@python.org> | 2007-11-27 23:48:05 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-11-27 23:48:05 (GMT) |
commit | ceee0773d262bfe876e40da927b03279ed9f8419 (patch) | |
tree | e0a354553f537cab6479daa9845baabbb3bc4131 /Lib/test/test_unicode.py | |
parent | 45f9af34b334b483678225a943578d2e1ea540b1 (diff) | |
download | cpython-ceee0773d262bfe876e40da927b03279ed9f8419.zip cpython-ceee0773d262bfe876e40da927b03279ed9f8419.tar.gz cpython-ceee0773d262bfe876e40da927b03279ed9f8419.tar.bz2 |
#1496: revert str.translate() to the old version, and add
str.maketrans() to make a table in a more comfortable way.
Diffstat (limited to 'Lib/test/test_unicode.py')
-rw-r--r-- | Lib/test/test_unicode.py | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 7475535..fe4eb85 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -166,18 +166,37 @@ class UnicodeTest( self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8) self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1) - def test_translate(self): - self.checkequalnofix('bbbc', 'abababc', 'translate', {ord('a'):None}) - self.checkequalnofix('iiic', 'abababc', 'translate', {ord('a'):None, ord('b'):ord('i')}) - self.checkequalnofix('iiix', 'abababc', 'translate', {ord('a'):None, ord('b'):ord('i'), ord('c'):'x'}) - self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', {'a':None, 'b':'<i>'}) - self.checkequalnofix('c', 'abababc', 'translate', {ord('a'):None, ord('b'):''}) - self.checkequalnofix('xyyx', 'xzx', 'translate', {ord('z'):'yy'}) + def test_maketrans_translate(self): + # these work with plain translate() + self.checkequalnofix('bbbc', 'abababc', 'translate', + {ord('a'): None}) + self.checkequalnofix('iiic', 'abababc', 'translate', + {ord('a'): None, ord('b'): ord('i')}) + self.checkequalnofix('iiix', 'abababc', 'translate', + {ord('a'): None, ord('b'): ord('i'), ord('c'): 'x'}) + self.checkequalnofix('c', 'abababc', 'translate', + {ord('a'): None, ord('b'): ''}) + self.checkequalnofix('xyyx', 'xzx', 'translate', + {ord('z'): 'yy'}) + # this needs maketrans() + self.checkequalnofix('abababc', 'abababc', 'translate', + {'b': '<i>'}) + tbl = self.type2test.maketrans({'a': None, 'b': '<i>'}) + self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', tbl) + # test alternative way of calling maketrans() + tbl = self.type2test.maketrans('abc', 'xyz', 'd') + self.checkequalnofix('xyzzy', 'abdcdcbdddd', 'translate', tbl) + + self.assertRaises(TypeError, self.type2test.maketrans) + self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg') + self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def') + self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 2) + self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def', 2) + self.assertRaises(ValueError, self.type2test.maketrans, {'xy': 2}) + self.assertRaises(TypeError, self.type2test.maketrans, {(1,): 2}) self.assertRaises(TypeError, 'hello'.translate) self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz') - self.assertRaises(ValueError, 'abababc'.translate, {'xy':2}) - self.assertRaises(TypeError, 'abababc'.translate, {(1,):2}) def test_split(self): string_tests.CommonTest.test_split(self) |