8000 Improve tests for newbytes object · thecodingchicken/python-future@fc9c24a · GitHub
[go: up one dir, main page]

Skip to content

Commit fc9c24a

Browse files
committed
Improve tests for newbytes object
1 parent c171199 commit fc9c24a

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

future/tests/test_bytes.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,28 @@ def test_bytes_int(self):
3636
In Py3, bytes(int) -> bytes object of size given by the parameter initialized with null
3737
"""
3838
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')
3941
# Negative counts are not allowed in Py3:
4042
with self.assertRaises(ValueError):
4143
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)
4261

4362
def test_bytes_empty(self):
4463
"""
@@ -48,6 +67,7 @@ def test_bytes_empty(self):
4867

4968
def test_bytes_iterable_of_ints(self):
5069
self.assertEqual(bytes([65, 66, 67]), b'ABC')
70+
self.assertEqual(bytes([int(120), int(121), int(122)]), b'xyz')
5171

5272
def test_bytes_bytes(self):
5373
self.assertEqual(bytes(b'ABC'), b'ABC')
@@ -111,7 +131,7 @@ def test_bytes_setitem(self):
111131
def test_bytes_iteration(self):
112132
b = bytes(b'ABCD')
113133
for item in b:
114-
self.assertTrue(isinstance(item, int))
134+
self.assertTrue(utils.is_int(item))
115135
self.assertEqual(list(b), [65, 66, 67, 68])
116136

117137
def test_bytes_plus_unicode_string(self):
@@ -144,6 +164,15 @@ def test_bytes_join_bytes(self):
144164
self.assertEqual(result, b'AB * EFGH * IJKL')
145165
self.assertTrue(isinstance(result, bytes))
146166

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+
147176
def test_bytes_join_unicode_strings(self):
148177
b = bytes(b'ABCD')
149178
strings = [u'EFGH', u'IJKL']
@@ -241,6 +270,9 @@ def test_encode(self):
241270
b.encode('utf-8')
242271

243272
def test_eq(self):
273+
"""
274+
Equals: ==
275+
"""
244276
b = bytes(b'ABCD')
245277
self.assertEqual(b, b'ABCD')
246278
self.assertTrue(b == b'ABCD')
@@ -256,6 +288,8 @@ def test_eq(self):
256288

257289
self.assertFalse(b == list(b))
258290
self.assertFalse(b == str(b))
291+
self.assertFalse(b == u'ABC')
292+
self.assertFalse(bytes(b'Z') == 90)
259293

260294
def test_ne(self):
261295
b = bytes(b'ABCD')
@@ -304,6 +338,55 @@ def test_hash_with_native_types(self):
304338
# Fails:
305339
self.assertEqual(len(d) > 1)
306340

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')
307390

308391
if __name__ == '__main__':
309392
unittest.main()

0 commit comments

Comments
 (0)
0