Use py.test instead of nosetests.

This commit is contained in:
INADA Naoki 2012-12-29 11:24:25 +09:00
parent d57e369258
commit 593c832ab0
10 changed files with 77 additions and 134 deletions

View file

@ -1,16 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
from nose import main
from nose.tools import *
from msgpack import packb, unpackb from msgpack import packb, unpackb
def test_unpack_buffer(): def test_unpack_buffer():
from array import array from array import array
buf = array('b') buf = array('b')
buf.fromstring(packb(('foo', 'bar'))) buf.fromstring(packb(('foo', 'bar')))
obj = unpackb(buf, use_list=1) obj = unpackb(buf, use_list=1)
assert_equal([b'foo', b'bar'], obj) assert [b'foo', b'bar'] == obj
if __name__ == '__main__':
main()

View file

@ -1,15 +1,14 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
from nose import main
from nose.tools import *
from msgpack import packb, unpackb from msgpack import packb, unpackb
def check(length, obj): def check(length, obj):
v = packb(obj) v = packb(obj)
assert_equal(len(v), length, "%r length should be %r but get %r" % (obj, length, len(v))) assert len(v) == length, \
assert_equal(unpackb(v, use_list=0), obj) "%r length should be %r but get %r" % (obj, length, len(v))
assert unpackb(v, use_list=0) == obj
def test_1(): def test_1():
for o in [None, True, False, 0, 1, (1 << 6), (1 << 7) - 1, -1, for o in [None, True, False, 0, 1, (1 << 6), (1 << 7) - 1, -1,
@ -70,8 +69,8 @@ def test_array32():
def match(obj, buf): def match(obj, buf):
assert_equal(packb(obj), buf) assert packb(obj) == buf
assert_equal(unpackb(buf, use_list=0), obj) assert unpackb(buf, use_list=0) == obj
def test_match(): def test_match():
cases = [ cases = [
@ -99,7 +98,5 @@ def test_match():
match(v, p) match(v, p)
def test_unicode(): def test_unicode():
assert_equal(b'foobar', unpackb(packb('foobar'), use_list=1)) assert unpackb(packb('foobar'), use_list=1) == b'foobar'
if __name__ == '__main__':
main()

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
from nose.tools import * from pytest import raises
from msgpack import packb, unpackb from msgpack import packb, unpackb
import datetime import datetime
@ -12,28 +12,20 @@ class DummyException(Exception):
def test_raise_on_find_unsupported_value(): def test_raise_on_find_unsupported_value():
assert_raises(TypeError, packb, datetime.datetime.now()) with raises(TypeError):
packb(datetime.datetime.now())
def test_raise_from_object_hook(): def test_raise_from_object_hook():
def hook(obj): def hook(obj):
raise DummyException raise DummyException
assert_raises(DummyException, unpackb, packb({}), object_hook=hook) raises(DummyException, unpackb, packb({}), object_hook=hook)
assert_raises(DummyException, unpackb, packb({'fizz': 'buzz'}), raises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_hook=hook)
object_hook=hook) raises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_pairs_hook=hook)
assert_raises(DummyException, unpackb, packb({'fizz': 'buzz'}), raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_hook=hook)
object_pairs_hook=hook) raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_pairs_hook=hook)
assert_raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}),
object_hook=hook)
assert_raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}),
object_pairs_hook=hook)
@raises(ValueError)
def test_invalidvalue(): def test_invalidvalue():
unpackb(b'\xd9\x97#DL_') with raises(ValueError):
unpackb(b'\xd9\x97#DL_')
if __name__ == '__main__':
from nose import main
main()

View file

@ -1,12 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
from nose import main
from nose.tools import *
from msgpack import unpackb from msgpack import unpackb
def check(src, should, use_list=0): def check(src, should, use_list=0):
assert_equal(unpackb(src, use_list=use_list), should) assert unpackb(src, use_list=use_list) == should
def testSimpleValue(): def testSimpleValue():
check(b"\x93\xc0\xc2\xc3", check(b"\x93\xc0\xc2\xc3",
@ -70,6 +68,3 @@ def testMap():
b"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2", b"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2",
({}, {None: False}, {True: False, None: False}, {}, ({}, {None: False}, {True: False, None: False}, {},
{None: False}, {True: False, None: False})) {None: False}, {True: False, None: False}))
if __name__ == '__main__':
main()

View file

@ -1,9 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
from nose import main from pytest import raises
from nose.tools import *
from msgpack import packb, unpackb from msgpack import packb, unpackb
def _decode_complex(obj): def _decode_complex(obj):
@ -19,27 +17,27 @@ def _encode_complex(obj):
def test_encode_hook(): def test_encode_hook():
packed = packb([3, 1+2j], default=_encode_complex) packed = packb([3, 1+2j], default=_encode_complex)
unpacked = unpackb(packed, use_list=1) unpacked = unpackb(packed, use_list=1)
eq_(unpacked[1], {b'__complex__': True, b'real': 1, b'imag': 2}) assert unpacked[1] == {b'__complex__': True, b'real': 1, b'imag': 2}
def test_decode_hook(): def test_decode_hook():
packed = packb([3, {b'__complex__': True, b'real': 1, b'imag': 2}]) packed = packb([3, {b'__complex__': True, b'real': 1, b'imag': 2}])
unpacked = unpackb(packed, object_hook=_decode_complex, use_list=1) unpacked = unpackb(packed, object_hook=_decode_complex, use_list=1)
eq_(unpacked[1], 1+2j) assert unpacked[1] == 1+2j
def test_decode_pairs_hook(): def test_decode_pairs_hook():
packed = packb([3, {1: 2, 3: 4}]) packed = packb([3, {1: 2, 3: 4}])
prod_sum = 1 * 2 + 3 * 4 prod_sum = 1 * 2 + 3 * 4
unpacked = unpackb(packed, object_pairs_hook=lambda l: sum(k * v for k, v in l), use_list=1) unpacked = unpackb(packed, object_pairs_hook=lambda l: sum(k * v for k, v in l), use_list=1)
eq_(unpacked[1], prod_sum) assert unpacked[1] == prod_sum
@raises(ValueError)
def test_only_one_obj_hook(): def test_only_one_obj_hook():
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x) with raises(ValueError):
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x)
@raises(ValueError)
def test_bad_hook(): def test_bad_hook():
packed = packb([3, 1+2j], default=lambda o: o) with raises(ValueError):
unpacked = unpackb(packed, use_list=1) packed = packb([3, 1+2j], default=lambda o: o)
unpacked = unpackb(packed, use_list=1)
def _arr_to_str(arr): def _arr_to_str(arr):
return ''.join(str(c) for c in arr) return ''.join(str(c) for c in arr)
@ -47,7 +45,7 @@ def _arr_to_str(arr):
def test_array_hook(): def test_array_hook():
packed = packb([1,2,3]) packed = packb([1,2,3])
unpacked = unpackb(packed, list_hook=_arr_to_str, use_list=1) unpacked = unpackb(packed, list_hook=_arr_to_str, use_list=1)
eq_(unpacked, '123') assert unpacked == '123'
class DecodeError(Exception): class DecodeError(Exception):
@ -57,18 +55,13 @@ def bad_complex_decoder(o):
raise DecodeError("Ooops!") raise DecodeError("Ooops!")
@raises(DecodeError)
def test_an_exception_in_objecthook1(): def test_an_exception_in_objecthook1():
packed = packb({1: {'__complex__': True, 'real': 1, 'imag': 2}}) with raises(DecodeError):
unpackb(packed, object_hook=bad_complex_decoder) packed = packb({1: {'__complex__': True, 'real': 1, 'imag': 2}})
unpackb(packed, object_hook=bad_complex_decoder)
@raises(DecodeError)
def test_an_exception_in_objecthook2(): def test_an_exception_in_objecthook2():
packed = packb({1: [{'__complex__': True, 'real': 1, 'imag': 2}]}) with raises(DecodeError):
unpackb(packed, list_hook=bad_complex_decoder, use_list=1) packed = packb({1: [{'__complex__': True, 'real': 1, 'imag': 2}]})
unpackb(packed, list_hook=bad_complex_decoder, use_list=1)
if __name__ == '__main__':
main()

View file

@ -3,9 +3,7 @@
import six import six
import struct import struct
from nose import main from pytest import raises, xfail
from nose.tools import *
from nose.plugins.skip import SkipTest
from msgpack import packb, unpackb, Unpacker, Packer from msgpack import packb, unpackb, Unpacker, Packer
@ -13,7 +11,7 @@ from io import BytesIO
def check(data, use_list=False): def check(data, use_list=False):
re = unpackb(packb(data), use_list=use_list) re = unpackb(packb(data), use_list=use_list)
assert_equal(re, data) assert re == data
def testPack(): def testPack():
test_data = [ test_data = [
@ -35,11 +33,11 @@ def testPackUnicode():
] ]
for td in test_data: for td in test_data:
re = unpackb(packb(td, encoding='utf-8'), use_list=1, encoding='utf-8') re = unpackb(packb(td, encoding='utf-8'), use_list=1, encoding='utf-8')
assert_equal(re, td) assert re == td
packer = Packer(encoding='utf-8') packer = Packer(encoding='utf-8')
data = packer.pack(td) data = packer.pack(td)
re = Unpacker(BytesIO(data), encoding='utf-8', use_list=1).unpack() re = Unpacker(BytesIO(data), encoding='utf-8', use_list=1).unpack()
assert_equal(re, td) assert re == td
def testPackUTF32(): def testPackUTF32():
try: try:
@ -51,9 +49,9 @@ def testPackUTF32():
] ]
for td in test_data: for td in test_data:
re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32') re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
assert_equal(re, td) assert re == td
except LookupError: except LookupError as e:
raise SkipTest xfail(e)
def testPackBytes(): def testPackBytes():
test_data = [ test_data = [
@ -64,31 +62,31 @@ def testPackBytes():
def testIgnoreUnicodeErrors(): def testIgnoreUnicodeErrors():
re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1) re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1)
assert_equal(re, "abcdef") assert re == "abcdef"
@raises(UnicodeDecodeError)
def testStrictUnicodeUnpack(): def testStrictUnicodeUnpack():
unpackb(packb(b'abc\xeddef'), encoding='utf-8', use_list=1) with raises(UnicodeDecodeError):
unpackb(packb(b'abc\xeddef'), encoding='utf-8', use_list=1)
@raises(UnicodeEncodeError)
def testStrictUnicodePack(): def testStrictUnicodePack():
packb(six.u("abc\xeddef"), encoding='ascii', unicode_errors='strict') with raises(UnicodeEncodeError):
packb(six.u("abc\xeddef"), encoding='ascii', unicode_errors='strict')
def testIgnoreErrorsPack(): def testIgnoreErrorsPack():
re = unpackb(packb(six.u("abcФФФdef"), encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1) re = unpackb(packb(six.u("abcФФФdef"), encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1)
assert_equal(re, six.u("abcdef")) assert re == six.u("abcdef")
@raises(TypeError)
def testNoEncoding(): def testNoEncoding():
packb(six.u("abc"), encoding=None) with raises(TypeError):
packb(six.u("abc"), encoding=None)
def testDecodeBinary(): def testDecodeBinary():
re = unpackb(packb("abc"), encoding=None, use_list=1) re = unpackb(packb("abc"), encoding=None, use_list=1)
assert_equal(re, b"abc") assert re == b"abc"
def testPackFloat(): def testPackFloat():
assert_equal(packb(1.0, use_single_float=True), b'\xca' + struct.pack('>f', 1.0)) assert packb(1.0, use_single_float=True) == b'\xca' + struct.pack('>f', 1.0)
assert_equal(packb(1.0, use_single_float=False), b'\xcb' + struct.pack('>d', 1.0)) assert packb(1.0, use_single_float=False) == b'\xcb' + struct.pack('>d', 1.0)
def testArraySize(sizes=[0, 5, 50, 1000]): def testArraySize(sizes=[0, 5, 50, 1000]):
bio = six.BytesIO() bio = six.BytesIO()
@ -151,10 +149,10 @@ class odict(dict):
def test_odict(): def test_odict():
seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)] seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
od = odict(seq) od = odict(seq)
assert_equal(unpackb(packb(od), use_list=1), dict(seq)) assert unpackb(packb(od), use_list=1) == dict(seq)
def pair_hook(seq): def pair_hook(seq):
return seq return seq
assert_equal(unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1), seq) assert unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
def test_pairlist(): def test_pairlist():
@ -163,8 +161,3 @@ def test_pairlist():
packed = packer.pack_map_pairs(pairlist) packed = packer.pack_map_pairs(pairlist)
unpacked = unpackb(packed, object_pairs_hook=list) unpacked = unpackb(packed, object_pairs_hook=list)
assert pairlist == unpacked assert pairlist == unpacked
if __name__ == '__main__':
main()

View file

@ -2,9 +2,6 @@
# coding: utf-8 # coding: utf-8
import six import six
from nose import main
from nose.tools import *
import io import io
import msgpack import msgpack
@ -38,13 +35,8 @@ def test_exceeding_unpacker_read_size():
read_count = 0 read_count = 0
for idx, o in enumerate(unpacker): for idx, o in enumerate(unpacker):
assert_equal(type(o), bytes) assert type(o) == bytes
assert_equal(o, gen_binary_data(idx)) assert o == gen_binary_data(idx)
read_count += 1 read_count += 1
assert_equal(read_count, NUMBER_OF_STRINGS) assert read_count == NUMBER_OF_STRINGS
if __name__ == '__main__':
main()
#test_exceeding_unpacker_read_size()

View file

@ -4,7 +4,8 @@
import six import six
from msgpack import Unpacker, BufferFull from msgpack import Unpacker, BufferFull
from msgpack.exceptions import OutOfData from msgpack.exceptions import OutOfData
import nose from pytest import raises
def test_foobar(): def test_foobar():
unpacker = Unpacker(read_size=3, use_list=1) unpacker = Unpacker(read_size=3, use_list=1)
@ -15,11 +16,8 @@ def test_foobar():
assert unpacker.unpack() == ord(b'b') assert unpacker.unpack() == ord(b'b')
assert unpacker.unpack() == ord(b'a') assert unpacker.unpack() == ord(b'a')
assert unpacker.unpack() == ord(b'r') assert unpacker.unpack() == ord(b'r')
try: with raises(OutOfData):
o = unpacker.unpack() unpacker.unpack()
assert 0, "should raise exception"
except OutOfData:
assert 1, "ok"
unpacker.feed(b'foo') unpacker.feed(b'foo')
unpacker.feed(b'bar') unpacker.feed(b'bar')
@ -39,17 +37,16 @@ def test_foobar_skip():
unpacker.skip() unpacker.skip()
assert unpacker.unpack() == ord(b'a') assert unpacker.unpack() == ord(b'a')
unpacker.skip() unpacker.skip()
try: with raises(OutOfData):
o = unpacker.unpack() unpacker.unpack()
assert 0, "should raise exception"
except OutOfData:
assert 1, "ok"
def test_maxbuffersize(): def test_maxbuffersize():
nose.tools.assert_raises(ValueError, Unpacker, read_size=5, max_buffer_size=3) with raises(ValueError):
Unpacker(read_size=5, max_buffer_size=3)
unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1) unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1)
unpacker.feed(b'fo') unpacker.feed(b'fo')
nose.tools.assert_raises(BufferFull, unpacker.feed, b'ob') with raises(BufferFull):
unpacker.feed(b'ob')
unpacker.feed(b'o') unpacker.feed(b'o')
assert ord('f') == next(unpacker) assert ord('f') == next(unpacker)
unpacker.feed(b'b') unpacker.feed(b'b')
@ -73,5 +70,3 @@ def test_readbytes():
assert unpacker.unpack() == ord(b'a') assert unpacker.unpack() == ord(b'a')
assert unpacker.unpack() == ord(b'r') assert unpacker.unpack() == ord(b'r')
if __name__ == '__main__':
nose.main()

View file

@ -1,8 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
from nose import main
from nose.tools import *
from msgpack import packb, unpackb from msgpack import packb, unpackb
from collections import namedtuple from collections import namedtuple
@ -18,10 +16,6 @@ class MyTuple(tuple):
MyNamedTuple = namedtuple('MyNamedTuple', 'x y') MyNamedTuple = namedtuple('MyNamedTuple', 'x y')
def test_types(): def test_types():
assert_equal(packb(dict()), packb(MyDict())) assert packb(MyDict()) == packb(dict())
assert_equal(packb(list()), packb(MyList())) assert packb(MyList()) == packb(list())
assert_equal(packb(MyNamedTuple(1,2)), packb((1,2))) assert packb(MyNamedTuple(1, 2)) == packb((1, 2))
if __name__ == '__main__':
main()

View file

@ -1,7 +1,5 @@
"""Tests for cases where the user seeks to obtain packed msgpack objects""" """Tests for cases where the user seeks to obtain packed msgpack objects"""
from nose import main
from nose.tools import *
import six import six
from msgpack import Unpacker, packb from msgpack import Unpacker, packb
@ -10,14 +8,14 @@ def test_write_bytes():
unpacker = Unpacker() unpacker = Unpacker()
unpacker.feed(b'abc') unpacker.feed(b'abc')
f = six.BytesIO() f = six.BytesIO()
assert_equal(unpacker.unpack(f.write), ord('a')) assert unpacker.unpack(f.write) == ord('a')
assert_equal(f.getvalue(), b'a') assert f.getvalue() == b'a'
f = six.BytesIO() f = six.BytesIO()
assert unpacker.skip(f.write) is None assert unpacker.skip(f.write) is None
assert_equal(f.getvalue(), b'b') assert f.getvalue() == b'b'
f = six.BytesIO() f = six.BytesIO()
assert unpacker.skip() is None assert unpacker.skip() is None
assert_equal(f.getvalue(), b'') assert f.getvalue() == b''
def test_write_bytes_multi_buffer(): def test_write_bytes_multi_buffer():
@ -27,8 +25,5 @@ def test_write_bytes_multi_buffer():
f = six.BytesIO() f = six.BytesIO()
unpacked = unpacker.unpack(f.write) unpacked = unpacker.unpack(f.write)
assert_equal(unpacked, long_val) assert unpacked == long_val
assert_equal(f.getvalue(), expected) assert f.getvalue() == expected
if __name__ == '__main__':
main()