@@ -36,9 +36,28 @@ def test_bytes_int(self):
36
36
In Py3, bytes(int) -> bytes object of size given by the parameter initialized with null
37
37
"""
38
38
self .assertEqual (bytes (5 ), b'\x00 \x00 \x00 \x00 \x00 ' )
39
+ # Test using newint:
40
+ self .assertEqual (bytes (int (5 )), b'\x00 \x00 \x00 \x00 \x00 ' )
39
41
# Negative counts are not allowed in Py3:
40
42
with self .assertRaises (ValueError ):
41
43
bytes (- 1 )
44
+ with self .assertRaises (ValueError ):
45
+ bytes (int (- 1 ))
46
+
47
+ @unittest .skipIf (utils .PY3 , 'test not needed on Py3: all ints are long' )
48
+ def test_bytes_long (self ):
49
+ """
50
+ As above, but explicitly feeding in a long on Py2. Note that
51
+ checks like:
52
+ isinstance(n, int)
53
+ are fragile on Py2, because isinstance(10L, int) is False.
54
+ """
55
+ m = long (5 )
56
+ n = long (- 1 )
57
+ self .assertEqual (bytes (m ), b'\x00 \x00 \x00 \x00 \x00 ' )
58
+ # Negative counts are not allowed in Py3:
59
+ with self .assertRaises (ValueError ):
60
+ bytes (n )
42
61
43
62
def test_bytes_empty (self ):
44
63
"""
@@ -48,6 +67,7 @@ def test_bytes_empty(self):
48
67
49
68
def test_bytes_iterable_of_ints (self ):
50
69
self .assertEqual (bytes ([65 , 66 , 67 ]), b'ABC' )
70
+ self .assertEqual (bytes ([int (120 ), int (121 ), int (122 )]), b'xyz' )
51
71
52
72
def test_bytes_bytes (self ):
53
73
self .assertEqual (bytes (b'ABC' ), b'ABC' )
@@ -111,7 +131,7 @@ def test_bytes_setitem(self):
111
131
def test_bytes_iteration (self ):
112
132
b = bytes (b'ABCD' )
113
133
for item in b :
114
- self .assertTrue (isinstance (item , int ))
134
+ self .assertTrue (utils . is_int (item ))
115
135
self .assertEqual (list (b ), [65 , 66 , 67 , 68 ])
116
136
117
137
def test_bytes_plus_unicode_string (self ):
@@ -144,6 +164,15 @@ def test_bytes_join_bytes(self):
144
164
self .assertEqual (result , b'AB * EFGH * IJKL' )
145
165
self .assertTrue (isinstance (result , bytes ))
146
166
167
+ def test_bytes_join_others (self ):
168
+ b = bytes (b' ' )
169
+ with self .assertRaises (TypeError ):
170
+ b .join ([42 ])
171
+ with self .assertRaises (TypeError ):
172
+ b .join (b'blah' )
173
+ with self .assertRaises (TypeError ):
174
+ b .join (bytes (b'blah' ))
175
+
147
176
def test_bytes_join_unicode_strings (self ):
148
177
b = bytes (b'ABCD' )
149
178
strings = [u'EFGH' , u'IJKL' ]
@@ -241,6 +270,9 @@ def test_encode(self):
241
270
b .encode ('utf-8' )
242
271
243
272
def test_eq (self ):
273
+ """
274
+ Equals: ==
275
+ """
244
276
b = bytes (b'ABCD' )
245
277
self .assertEqual (b , b'ABCD' )
246
278
self .assertTrue (b == b'ABCD' )
@@ -256,6 +288,8 @@ def test_eq(self):
256
288
257
289
self .assertFalse (b == list (b ))
258
290
self .assertFalse (b == str (b ))
291
+ self .assertFalse (b == u'ABC' )
292
+ self .assertFalse (bytes (b'Z' ) == 90 )
259
293
260
294
def test_ne (self ):
261
295
b = bytes (b'ABCD' )
@@ -304,6 +338,55 @@ def test_hash_with_native_types(self):
304
338
# Fails:
305
339
self .assertEqual (len (d ) > 1 )
306
340
341
+ def test_add (self ):
342
+ b = bytes (b'ABC' )
343
+ c = bytes (b'XYZ' )
344
+ d = b + c
345
+ self .assertTrue (isinstance (d , bytes ))
346
+ self .assertEqual (d , b'ABCXYZ' )
347
+ f = b + b'abc'
348
+ self .assertTrue (isinstance (f , bytes ))
349
+ self .assertEqual (f , b'ABCabc' )
350
+ g = b'abc' + b
351
+ self .assertTrue (isinstance (g , bytes ))
352
+ self .assertEqual (g , b'abcABC' )
353
+
354
+ def test_cmp (self ):
355
+ b = bytes (b'ABC' )
356
+ with self .assertRaises (TypeError ):
357
+ b > 3
358
+ with self .assertRaises (TypeError ):
359
+ b > u'XYZ'
360
+ with self .assertRaises (TypeError ):
361
+ b <= 3
362
+ with self .assertRaises (TypeError ):
363
+ b >= int (3 )
364
+
365
+ def test_mul (self ):
366
+ b = bytes (b'ABC' )
367
+ c = b * 4
368
+ self .assertTrue (isinstance (c , bytes ))
369
+ self .assertEqual (c , b'ABCABCABCABC' )
370
+ d = b * int (4 )
371
+ self .assertTrue (isinstance (d , bytes ))
372
+ self .assertEqual (d , b'ABCABCABCABC' )
373
+ if utils .PY2 :
374
+ e = b * long (4 )
375
+ self .assertTrue (isinstance (e , bytes ))
376
+ self .assertEqual (e , b'ABCABCABCABC' )
377
+
378
+ def test_rmul (self ):
379
+ b = bytes (b'XYZ' )
380
+ c = 3 * b
381
+ self .assertTrue (isinstance (c , bytes ))
382
+ self .assertEqual (c , b'XYZXYZXYZ' )
383
+ d = b * int (3 )
384
+ self .assertTrue (isinstance (d , bytes ))
385
+ self .assertEqual (d , b'XYZXYZXYZ' )
386
+ if utils .PY2 :
387
+ e = long (3 ) * b
388
+ self .assertTrue (isinstance (e , bytes ))
389
+ self .assertEqual (e , b'XYZXYZXYZ' )
307
390
308
391
if __name__ == '__main__' :
309
392
unittest .main ()
0 commit comments