Skip to content

Commit 0b38e86

Browse files
committed
unify tests for py2 and py3
1 parent 76f3466 commit 0b38e86

14 files changed

Lines changed: 85 additions & 462 deletions

test/test_buffer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
def test_unpack_buffer():
99
from array import array
10-
buf = array('c')
10+
buf = array('b')
1111
buf.fromstring(packb(('foo', 'bar')))
1212
obj = unpackb(buf)
13-
assert_equal(('foo', 'bar'), obj)
13+
assert_equal((b'foo', b'bar'), obj)
1414

1515
if __name__ == '__main__':
1616
main()

test/test_case.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_9():
4040

4141

4242
def check_raw(overhead, num):
43-
check(num + overhead, " " * num)
43+
check(num + overhead, b" " * num)
4444

4545
def test_fixraw():
4646
check_raw(1, 0)
@@ -75,31 +75,31 @@ def match(obj, buf):
7575

7676
def test_match():
7777
cases = [
78-
(None, '\xc0'),
79-
(False, '\xc2'),
80-
(True, '\xc3'),
81-
(0, '\x00'),
82-
(127, '\x7f'),
83-
(128, '\xcc\x80'),
84-
(256, '\xcd\x01\x00'),
85-
(-1, '\xff'),
86-
(-33, '\xd0\xdf'),
87-
(-129, '\xd1\xff\x7f'),
88-
({1:1}, '\x81\x01\x01'),
89-
(1.0, "\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"),
90-
((), '\x90'),
91-
(tuple(range(15)),"\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"),
92-
(tuple(range(16)),"\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"),
93-
({}, '\x80'),
94-
(dict([(x,x) for x in range(15)]), '\x8f\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e'),
95-
(dict([(x,x) for x in range(16)]), '\xde\x00\x10\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e\x0f\x0f'),
78+
(None, b'\xc0'),
79+
(False, b'\xc2'),
80+
(True, b'\xc3'),
81+
(0, b'\x00'),
82+
(127, b'\x7f'),
83+
(128, b'\xcc\x80'),
84+
(256, b'\xcd\x01\x00'),
85+
(-1, b'\xff'),
86+
(-33, b'\xd0\xdf'),
87+
(-129, b'\xd1\xff\x7f'),
88+
({1:1}, b'\x81\x01\x01'),
89+
(1.0, b"\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"),
90+
((), b'\x90'),
91+
(tuple(range(15)),b"\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"),
92+
(tuple(range(16)),b"\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"),
93+
({}, b'\x80'),
94+
(dict([(x,x) for x in range(15)]), b'\x8f\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e'),
95+
(dict([(x,x) for x in range(16)]), b'\xde\x00\x10\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e\x0f\x0f'),
9696
]
9797

9898
for v, p in cases:
9999
match(v, p)
100100

101101
def test_unicode():
102-
assert_equal('foobar', unpacks(packs(u'foobar')))
102+
assert_equal(b'foobar', unpacks(packs('foobar')))
103103

104104
if __name__ == '__main__':
105105
main()

test/test_format.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,65 @@ def check(src, should):
99
assert_equal(unpacks(src), should)
1010

1111
def testSimpleValue():
12-
check("\x93\xc0\xc2\xc3",
12+
check(b"\x93\xc0\xc2\xc3",
1313
(None, False, True,))
1414

1515
def testFixnum():
16-
check("\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff",
16+
check(b"\x92\x93\x00\x40\x7f\x93\xe0\xf0\xff",
1717
((0,64,127,), (-32,-16,-1,),)
1818
)
1919

2020
def testFixArray():
21-
check("\x92\x90\x91\x91\xc0",
21+
check(b"\x92\x90\x91\x91\xc0",
2222
((),((None,),),),
2323
)
2424

2525
def testFixRaw():
26-
check("\x94\xa0\xa1a\xa2bc\xa3def",
27-
("", "a", "bc", "def",),
26+
check(b"\x94\xa0\xa1a\xa2bc\xa3def",
27+
(b"", b"a", b"bc", b"def",),
2828
)
2929

3030
def testFixMap():
3131
check(
32-
"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80",
32+
b"\x82\xc2\x81\xc0\xc0\xc3\x81\xc0\x80",
3333
{False: {None: None}, True:{None:{}}},
3434
)
3535

3636
def testUnsignedInt():
3737
check(
38-
"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00"
39-
"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
40-
"\xce\xff\xff\xff\xff",
38+
b"\x99\xcc\x00\xcc\x80\xcc\xff\xcd\x00\x00\xcd\x80\x00"
39+
b"\xcd\xff\xff\xce\x00\x00\x00\x00\xce\x80\x00\x00\x00"
40+
b"\xce\xff\xff\xff\xff",
4141
(0, 128, 255, 0, 32768, 65535, 0, 2147483648, 4294967295,),
4242
)
4343

4444
def testSignedInt():
45-
check("\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00"
46-
"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
47-
"\xd2\xff\xff\xff\xff",
45+
check(b"\x99\xd0\x00\xd0\x80\xd0\xff\xd1\x00\x00\xd1\x80\x00"
46+
b"\xd1\xff\xff\xd2\x00\x00\x00\x00\xd2\x80\x00\x00\x00"
47+
b"\xd2\xff\xff\xff\xff",
4848
(0, -128, -1, 0, -32768, -1, 0, -2147483648, -1,))
4949

5050
def testRaw():
51-
check("\x96\xda\x00\x00\xda\x00\x01a\xda\x00\x02ab\xdb\x00\x00"
52-
"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab",
53-
("", "a", "ab", "", "a", "ab"))
51+
check(b"\x96\xda\x00\x00\xda\x00\x01a\xda\x00\x02ab\xdb\x00\x00"
52+
b"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab",
53+
(b"", b"a", b"ab", b"", b"a", b"ab"))
5454

5555
def testArray():
56-
check("\x96\xdc\x00\x00\xdc\x00\x01\xc0\xdc\x00\x02\xc2\xc3\xdd\x00"
57-
"\x00\x00\x00\xdd\x00\x00\x00\x01\xc0\xdd\x00\x00\x00\x02"
58-
"\xc2\xc3",
56+
check(b"\x96\xdc\x00\x00\xdc\x00\x01\xc0\xdc\x00\x02\xc2\xc3\xdd\x00"
57+
b"\x00\x00\x00\xdd\x00\x00\x00\x01\xc0\xdd\x00\x00\x00\x02"
58+
b"\xc2\xc3",
5959
((), (None,), (False,True), (), (None,), (False,True))
6060
)
6161

6262
def testMap():
6363
check(
64-
"\x96"
65-
"\xde\x00\x00"
66-
"\xde\x00\x01\xc0\xc2"
67-
"\xde\x00\x02\xc0\xc2\xc3\xc2"
68-
"\xdf\x00\x00\x00\x00"
69-
"\xdf\x00\x00\x00\x01\xc0\xc2"
70-
"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2",
64+
b"\x96"
65+
b"\xde\x00\x00"
66+
b"\xde\x00\x01\xc0\xc2"
67+
b"\xde\x00\x02\xc0\xc2\xc3\xc2"
68+
b"\xdf\x00\x00\x00\x00"
69+
b"\xdf\x00\x00\x00\x01\xc0\xc2"
70+
b"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2",
7171
({}, {None: False}, {True: False, None: False}, {},
7272
{None: False}, {True: False, None: False}))
7373

test/test_obj.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
from msgpack import packs, unpacks
88

99
def _decode_complex(obj):
10-
if '__complex__' in obj:
11-
return complex(obj['real'], obj['imag'])
10+
if b'__complex__' in obj:
11+
return complex(obj[b'real'], obj[b'imag'])
1212
return obj
1313

1414
def _encode_complex(obj):
1515
if isinstance(obj, complex):
16-
return {'__complex__': True, 'real': 1, 'imag': 2}
16+
return {b'__complex__': True, b'real': 1, b'imag': 2}
1717
return obj
1818

1919
def test_encode_hook():
2020
packed = packs([3, 1+2j], default=_encode_complex)
2121
unpacked = unpacks(packed)
22-
eq_(unpacked[1], {'__complex__': True, 'real': 1, 'imag': 2})
22+
eq_(unpacked[1], {b'__complex__': True, b'real': 1, b'imag': 2})
2323

2424
def test_decode_hook():
25-
packed = packs([3, {'__complex__': True, 'real': 1, 'imag': 2}])
25+
packed = packs([3, {b'__complex__': True, b'real': 1, b'imag': 2}])
2626
unpacked = unpacks(packed, object_hook=_decode_complex)
2727
eq_(unpacked[1], 1+2j)
2828

@@ -40,7 +40,4 @@ def test_array_hook():
4040
eq_(unpacked, '123')
4141

4242
if __name__ == '__main__':
43-
test_decode_hook()
44-
test_encode_hook()
45-
test_bad_hook()
46-
test_array_hook()
43+
main()

test/test_pack.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from nose.tools import *
66
from nose.plugins.skip import SkipTest
77

8-
from msgpack import packs, unpacks, Packer, Unpacker
8+
from msgpack import packs, unpacks, Unpacker, Packer
99

10-
from StringIO import StringIO
10+
from io import BytesIO
1111

1212
def check(data):
1313
re = unpacks(packs(data))
@@ -18,7 +18,7 @@ def testPack():
1818
0, 1, 127, 128, 255, 256, 65535, 65536,
1919
-1, -32, -33, -128, -129, -32768, -32769,
2020
1.0,
21-
"", "a", "a"*31, "a"*32,
21+
b"", b"a", b"a"*31, b"a"*32,
2222
None, True, False,
2323
(), ((),), ((), None,),
2424
{None: 0},
@@ -29,20 +29,20 @@ def testPack():
2929

3030
def testPackUnicode():
3131
test_data = [
32-
u"", u"abcd", (u"defgh",), u"Русский текст",
32+
"", "abcd", ("defgh",), "Русский текст",
3333
]
3434
for td in test_data:
3535
re = unpacks(packs(td, encoding='utf-8'), encoding='utf-8')
3636
assert_equal(re, td)
3737
packer = Packer(encoding='utf-8')
3838
data = packer.pack(td)
39-
re = Unpacker(StringIO(data), encoding='utf-8').unpack()
39+
re = Unpacker(BytesIO(data), encoding='utf-8').unpack()
4040
assert_equal(re, td)
4141

4242
def testPackUTF32():
4343
try:
4444
test_data = [
45-
u"", u"abcd", (u"defgh",), u"Русский текст",
45+
"", "abcd", ("defgh",), "Русский текст",
4646
]
4747
for td in test_data:
4848
re = unpacks(packs(td, encoding='utf-32'), encoding='utf-32')
@@ -52,37 +52,35 @@ def testPackUTF32():
5252

5353
def testPackBytes():
5454
test_data = [
55-
"", "abcd", ("defgh",),
55+
b"", b"abcd", (b"defgh",),
5656
]
5757
for td in test_data:
5858
check(td)
5959

6060
def testIgnoreUnicodeErrors():
61-
re = unpacks(packs('abc\xeddef'),
62-
encoding='ascii', unicode_errors='ignore')
61+
re = unpacks(packs(b'abc\xeddef'),
62+
encoding='utf-8', unicode_errors='ignore')
6363
assert_equal(re, "abcdef")
6464

6565
@raises(UnicodeDecodeError)
6666
def testStrictUnicodeUnpack():
67-
unpacks(packs('abc\xeddef'), encoding='utf-8')
67+
unpacks(packs(b'abc\xeddef'), encoding='utf-8')
6868

6969
@raises(UnicodeEncodeError)
7070
def testStrictUnicodePack():
71-
packs(u"abc\xeddef", encoding='ascii', unicode_errors='strict')
71+
packs("abc\xeddef", encoding='ascii', unicode_errors='strict')
7272

7373
def testIgnoreErrorsPack():
74-
re = unpacks(
75-
packs(u"abcФФФdef", encoding='ascii', unicode_errors='ignore'),
76-
encoding='utf-8')
77-
assert_equal(re, u"abcdef")
74+
re = unpacks(packs("abcФФФdef", encoding='ascii', unicode_errors='ignore'), encoding='utf-8')
75+
assert_equal(re, "abcdef")
7876

7977
@raises(TypeError)
8078
def testNoEncoding():
81-
packs(u"abc", encoding=None)
79+
packs("abc", encoding=None)
8280

8381
def testDecodeBinary():
84-
re = unpacks(packs(u"abc"), encoding=None)
85-
assert_equal(re, "abc")
82+
re = unpacks(packs("abc"), encoding=None)
83+
assert_equal(re, b"abc")
8684

8785
if __name__ == '__main__':
8886
main()

test/test_sequnpack.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
#!/usr/bin/env python
22
# coding: utf-8
33

4+
5+
46
from msgpack import Unpacker
57

68
def test_foobar():
79
unpacker = Unpacker(read_size=3)
8-
unpacker.feed('foobar')
9-
assert unpacker.unpack() == ord('f')
10-
assert unpacker.unpack() == ord('o')
11-
assert unpacker.unpack() == ord('o')
12-
assert unpacker.unpack() == ord('b')
13-
assert unpacker.unpack() == ord('a')
14-
assert unpacker.unpack() == ord('r')
10+
unpacker.feed(b'foobar')
11+
assert unpacker.unpack() == ord(b'f')
12+
assert unpacker.unpack() == ord(b'o')
13+
assert unpacker.unpack() == ord(b'o')
14+
assert unpacker.unpack() == ord(b'b')
15+
assert unpacker.unpack() == ord(b'a')
16+
assert unpacker.unpack() == ord(b'r')
1517
try:
1618
o = unpacker.unpack()
17-
print "Oops!", o
19+
print(("Oops!", o))
1820
assert 0
1921
except StopIteration:
2022
assert 1
2123
else:
2224
assert 0
23-
unpacker.feed('foo')
24-
unpacker.feed('bar')
25+
unpacker.feed(b'foo')
26+
unpacker.feed(b'bar')
2527

2628
k = 0
27-
for o, e in zip(unpacker, 'foobarbaz'):
28-
assert o == ord(e)
29+
for o, e in zip(unpacker, b'foobarbaz'):
30+
assert o == e
2931
k += 1
30-
assert k == len('foobar')
32+
assert k == len(b'foobar')
3133

3234
if __name__ == '__main__':
3335
test_foobar()

test3/test_buffer.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)