Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2)

Side by Side Diff: Lib/test/test_bytes.py

Issue 20552: Use specific asserts in bytes tests
Patch Set: Created 6 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Unit tests for the bytes and bytearray types. 1 """Unit tests for the bytes and bytearray types.
2 2
3 XXX This is a mess. Common tests should be moved to buffer_tests.py, 3 XXX This is a mess. Common tests should be moved to buffer_tests.py,
4 which itself ought to be unified with string_tests.py (and the latter 4 which itself ought to be unified with string_tests.py (and the latter
5 should be modernized). 5 should be modernized).
6 """ 6 """
7 7
8 import os 8 import os
9 import re 9 import re
10 import sys 10 import sys
(...skipping 939 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 b = bytearray(range(256)) 950 b = bytearray(range(256))
951 b[8:] = b 951 b[8:] = b
952 self.assertEqual(b, bytearray(list(range(8)) + list(range(256)))) 952 self.assertEqual(b, bytearray(list(range(8)) + list(range(256))))
953 953
954 def test_iconcat(self): 954 def test_iconcat(self):
955 b = bytearray(b"abc") 955 b = bytearray(b"abc")
956 b1 = b 956 b1 = b
957 b += b"def" 957 b += b"def"
958 self.assertEqual(b, b"abcdef") 958 self.assertEqual(b, b"abcdef")
959 self.assertEqual(b, b1) 959 self.assertEqual(b, b1)
960 self.assertTrue(b is b1) 960 self.assertIs(b, b1)
961 b += b"xyz" 961 b += b"xyz"
962 self.assertEqual(b, b"abcdefxyz") 962 self.assertEqual(b, b"abcdefxyz")
963 try: 963 try:
964 b += "" 964 b += ""
965 except TypeError: 965 except TypeError:
966 pass 966 pass
967 else: 967 else:
968 self.fail("bytes += unicode didn't raise TypeError") 968 self.fail("bytes += unicode didn't raise TypeError")
969 969
970 def test_irepeat(self): 970 def test_irepeat(self):
971 b = bytearray(b"abc") 971 b = bytearray(b"abc")
972 b1 = b 972 b1 = b
973 b *= 3 973 b *= 3
974 self.assertEqual(b, b"abcabcabc") 974 self.assertEqual(b, b"abcabcabc")
975 self.assertEqual(b, b1) 975 self.assertEqual(b, b1)
976 self.assertTrue(b is b1) 976 self.assertIs(b, b1)
977 977
978 def test_irepeat_1char(self): 978 def test_irepeat_1char(self):
979 b = bytearray(b"x") 979 b = bytearray(b"x")
980 b1 = b 980 b1 = b
981 b *= 100 981 b *= 100
982 self.assertEqual(b, b"x"*100) 982 self.assertEqual(b, b"x"*100)
983 self.assertEqual(b, b1) 983 self.assertEqual(b, b1)
984 self.assertTrue(b is b1) 984 self.assertIs(b, b1)
985 985
986 def test_alloc(self): 986 def test_alloc(self):
987 b = bytearray() 987 b = bytearray()
988 alloc = b.__alloc__() 988 alloc = b.__alloc__()
989 self.assertTrue(alloc >= 0) 989 self.assertGreaterEqual(alloc, 0)
990 seq = [alloc] 990 seq = [alloc]
991 for i in range(100): 991 for i in range(100):
992 b += b"x" 992 b += b"x"
993 alloc = b.__alloc__() 993 alloc = b.__alloc__()
994 self.assertTrue(alloc >= len(b)) 994 self.assertGreaterEqual(alloc, len(b))
995 if alloc not in seq: 995 if alloc not in seq:
996 seq.append(alloc) 996 seq.append(alloc)
997 997
998 def test_extend(self): 998 def test_extend(self):
999 orig = b'hello' 999 orig = b'hello'
1000 a = bytearray(orig) 1000 a = bytearray(orig)
1001 a.extend(a) 1001 a.extend(a)
1002 self.assertEqual(a, orig + orig) 1002 self.assertEqual(a, orig + orig)
1003 self.assertEqual(a[5:], orig) 1003 self.assertEqual(a[5:], orig)
1004 a = bytearray(b'') 1004 a = bytearray(b'')
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1075 self.assertEqual(b, b'mississippi') 1075 self.assertEqual(b, b'mississippi')
1076 self.assertRaises(TypeError, lambda: b.insert(0, b'1')) 1076 self.assertRaises(TypeError, lambda: b.insert(0, b'1'))
1077 b = bytearray() 1077 b = bytearray()
1078 b.insert(0, Indexable(ord('A'))) 1078 b.insert(0, Indexable(ord('A')))
1079 self.assertEqual(b, b'A') 1079 self.assertEqual(b, b'A')
1080 1080
1081 def test_copied(self): 1081 def test_copied(self):
1082 # Issue 4348. Make sure that operations that don't mutate the array 1082 # Issue 4348. Make sure that operations that don't mutate the array
1083 # copy the bytes. 1083 # copy the bytes.
1084 b = bytearray(b'abc') 1084 b = bytearray(b'abc')
1085 self.assertFalse(b is b.replace(b'abc', b'cde', 0)) 1085 self.assertIsNot(b, b.replace(b'abc', b'cde', 0))
1086 1086
1087 t = bytearray([i for i in range(256)]) 1087 t = bytearray([i for i in range(256)])
1088 x = bytearray(b'') 1088 x = bytearray(b'')
1089 self.assertFalse(x is x.translate(t)) 1089 self.assertIsNot(x, x.translate(t))
1090 1090
1091 def test_partition_bytearray_doesnt_share_nullstring(self): 1091 def test_partition_bytearray_doesnt_share_nullstring(self):
1092 a, b, c = bytearray(b"x").partition(b"y") 1092 a, b, c = bytearray(b"x").partition(b"y")
1093 self.assertEqual(b, b"") 1093 self.assertEqual(b, b"")
1094 self.assertEqual(c, b"") 1094 self.assertEqual(c, b"")
1095 self.assertTrue(b is not c) 1095 self.assertIsNot(b, c)
1096 b += b"!" 1096 b += b"!"
1097 self.assertEqual(c, b"") 1097 self.assertEqual(c, b"")
1098 a, b, c = bytearray(b"x").partition(b"y") 1098 a, b, c = bytearray(b"x").partition(b"y")
1099 self.assertEqual(b, b"") 1099 self.assertEqual(b, b"")
1100 self.assertEqual(c, b"") 1100 self.assertEqual(c, b"")
1101 # Same for rpartition 1101 # Same for rpartition
1102 b, c, a = bytearray(b"x").rpartition(b"y") 1102 b, c, a = bytearray(b"x").rpartition(b"y")
1103 self.assertEqual(b, b"") 1103 self.assertEqual(b, b"")
1104 self.assertEqual(c, b"") 1104 self.assertEqual(c, b"")
1105 self.assertTrue(b is not c) 1105 self.assertIsNot(b, c)
1106 b += b"!" 1106 b += b"!"
1107 self.assertEqual(c, b"") 1107 self.assertEqual(c, b"")
1108 c, b, a = bytearray(b"x").rpartition(b"y") 1108 c, b, a = bytearray(b"x").rpartition(b"y")
1109 self.assertEqual(b, b"") 1109 self.assertEqual(b, b"")
1110 self.assertEqual(c, b"") 1110 self.assertEqual(c, b"")
1111 1111
1112 def test_resize_forbidden(self): 1112 def test_resize_forbidden(self):
1113 # #4509: can't resize a bytearray when there are buffer exports, even 1113 # #4509: can't resize a bytearray when there are buffer exports, even
1114 # if it wouldn't reallocate the underlying buffer. 1114 # if it wouldn't reallocate the underlying buffer.
1115 # Furthermore, no destructive changes to the buffer may be applied 1115 # Furthermore, no destructive changes to the buffer may be applied
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1242 1242
1243 def test_split_bytearray(self): 1243 def test_split_bytearray(self):
1244 self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b']) 1244 self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b'])
1245 1245
1246 def test_rsplit_bytearray(self): 1246 def test_rsplit_bytearray(self):
1247 self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b']) 1247 self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b'])
1248 1248
1249 def test_return_self(self): 1249 def test_return_self(self):
1250 # bytearray.replace must always return a new bytearray 1250 # bytearray.replace must always return a new bytearray
1251 b = bytearray() 1251 b = bytearray()
1252 self.assertFalse(b.replace(b'', b'') is b) 1252 self.assertIsNot(b.replace(b'', b''), b)
1253 1253
1254 def test_compare(self): 1254 def test_compare(self):
1255 if sys.flags.bytes_warning: 1255 if sys.flags.bytes_warning:
1256 def bytes_warning(): 1256 def bytes_warning():
1257 return test.support.check_warnings(('', BytesWarning)) 1257 return test.support.check_warnings(('', BytesWarning))
1258 with bytes_warning(): 1258 with bytes_warning():
1259 b'' == '' 1259 b'' == ''
1260 with bytes_warning(): 1260 with bytes_warning():
1261 b'' != '' 1261 b'' != ''
1262 with bytes_warning(): 1262 with bytes_warning():
(...skipping 24 matching lines...) Expand all
1287 return bytearray(x) 1287 return bytearray(x)
1288 1288
1289 def test_returns_new_copy(self): 1289 def test_returns_new_copy(self):
1290 val = self.marshal(b'1234') 1290 val = self.marshal(b'1234')
1291 # On immutable types these MAY return a reference to themselves 1291 # On immutable types these MAY return a reference to themselves
1292 # but on mutable types like bytearray they MUST return a new copy. 1292 # but on mutable types like bytearray they MUST return a new copy.
1293 for methname in ('zfill', 'rjust', 'ljust', 'center'): 1293 for methname in ('zfill', 'rjust', 'ljust', 'center'):
1294 method = getattr(val, methname) 1294 method = getattr(val, methname)
1295 newval = method(3) 1295 newval = method(3)
1296 self.assertEqual(val, newval) 1296 self.assertEqual(val, newval)
1297 self.assertTrue(val is not newval, 1297 self.assertIsNot(val, newval,
1298 methname+' returned self on a mutable object') 1298 methname+' returned self on a mutable object')
1299 for expr in ('val.split()[0]', 'val.rsplit()[0]', 1299 for expr in ('val.split()[0]', 'val.rsplit()[0]',
1300 'val.partition(b".")[0]', 'val.rpartition(b".")[2]', 1300 'val.partition(b".")[0]', 'val.rpartition(b".")[2]',
1301 'val.splitlines()[0]', 'val.replace(b"", b"")'): 1301 'val.splitlines()[0]', 'val.replace(b"", b"")'):
1302 newval = eval(expr) 1302 newval = eval(expr)
1303 self.assertEqual(val, newval) 1303 self.assertEqual(val, newval)
1304 self.assertTrue(val is not newval, 1304 self.assertIsNot(val, newval,
1305 expr+' returned val on a mutable object') 1305 expr+' returned val on a mutable object')
1306 sep = self.marshal(b'') 1306 sep = self.marshal(b'')
1307 newval = sep.join([val]) 1307 newval = sep.join([val])
1308 self.assertEqual(val, newval) 1308 self.assertEqual(val, newval)
1309 self.assertIsNot(val, newval) 1309 self.assertIsNot(val, newval)
1310 1310
1311 1311
1312 class FixedStringTest(test.string_tests.BaseTest): 1312 class FixedStringTest(test.string_tests.BaseTest):
1313 1313
1314 def fixtype(self, obj): 1314 def fixtype(self, obj):
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1346 a, b = b"abcd", b"efgh" 1346 a, b = b"abcd", b"efgh"
1347 _a, _b = self.subclass2test(a), self.subclass2test(b) 1347 _a, _b = self.subclass2test(a), self.subclass2test(b)
1348 1348
1349 # test comparison operators with subclass instances 1349 # test comparison operators with subclass instances
1350 self.assertTrue(_a == _a) 1350 self.assertTrue(_a == _a)
1351 self.assertTrue(_a != _b) 1351 self.assertTrue(_a != _b)
1352 self.assertTrue(_a < _b) 1352 self.assertTrue(_a < _b)
1353 self.assertTrue(_a <= _b) 1353 self.assertTrue(_a <= _b)
1354 self.assertTrue(_b >= _a) 1354 self.assertTrue(_b >= _a)
1355 self.assertTrue(_b > _a) 1355 self.assertTrue(_b > _a)
1356 self.assertTrue(_a is not a) 1356 self.assertIsNot(_a, a)
1357 1357
1358 # test concat of subclass instances 1358 # test concat of subclass instances
1359 self.assertEqual(a + b, _a + _b) 1359 self.assertEqual(a + b, _a + _b)
1360 self.assertEqual(a + b, a + _b) 1360 self.assertEqual(a + b, a + _b)
1361 self.assertEqual(a + b, _a + b) 1361 self.assertEqual(a + b, _a + b)
1362 1362
1363 # test repeat 1363 # test repeat
1364 self.assertTrue(a*5 == _a*5) 1364 self.assertTrue(a*5 == _a*5)
1365 1365
1366 def test_join(self): 1366 def test_join(self):
1367 # Make sure join returns a NEW object for single item sequences 1367 # Make sure join returns a NEW object for single item sequences
1368 # involving a subclass. 1368 # involving a subclass.
1369 # Make sure that it is of the appropriate type. 1369 # Make sure that it is of the appropriate type.
1370 s1 = self.subclass2test(b"abcd") 1370 s1 = self.subclass2test(b"abcd")
1371 s2 = self.type2test().join([s1]) 1371 s2 = self.type2test().join([s1])
1372 self.assertTrue(s1 is not s2) 1372 self.assertIsNot(s1, s2)
1373 self.assertTrue(type(s2) is self.type2test, type(s2)) 1373 self.assertIs(type(s2), self.type2test)
1374 1374
1375 # Test reverse, calling join on subclass 1375 # Test reverse, calling join on subclass
1376 s3 = s1.join([b"abcd"]) 1376 s3 = s1.join([b"abcd"])
1377 self.assertTrue(type(s3) is self.type2test) 1377 self.assertIs(type(s3), self.type2test)
1378 1378
1379 def test_pickle(self): 1379 def test_pickle(self):
1380 a = self.subclass2test(b"abcd") 1380 a = self.subclass2test(b"abcd")
1381 a.x = 10 1381 a.x = 10
1382 a.y = self.subclass2test(b"efgh") 1382 a.y = self.subclass2test(b"efgh")
1383 for proto in range(pickle.HIGHEST_PROTOCOL + 1): 1383 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1384 b = pickle.loads(pickle.dumps(a, proto)) 1384 b = pickle.loads(pickle.dumps(a, proto))
1385 self.assertNotEqual(id(a), id(b)) 1385 self.assertNotEqual(id(a), id(b))
1386 self.assertEqual(a, b) 1386 self.assertEqual(a, b)
1387 self.assertEqual(a.x, b.x) 1387 self.assertEqual(a.x, b.x)
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1424 self.assertEqual(x, b"abcd") 1424 self.assertEqual(x, b"abcd")
1425 1425
1426 1426
1427 class BytesSubclassTest(SubclassTest, unittest.TestCase): 1427 class BytesSubclassTest(SubclassTest, unittest.TestCase):
1428 type2test = bytes 1428 type2test = bytes
1429 subclass2test = BytesSubclass 1429 subclass2test = BytesSubclass
1430 1430
1431 1431
1432 if __name__ == "__main__": 1432 if __name__ == "__main__":
1433 unittest.main() 1433 unittest.main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+