mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-20 04:13:16 +00:00
Use py.test instead of nosetests.
This commit is contained in:
parent
d57e369258
commit
593c832ab0
10 changed files with 77 additions and 134 deletions
|
@ -1,9 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from nose import main
|
||||
from nose.tools import *
|
||||
|
||||
from pytest import raises
|
||||
from msgpack import packb, unpackb
|
||||
|
||||
def _decode_complex(obj):
|
||||
|
@ -19,27 +17,27 @@ def _encode_complex(obj):
|
|||
def test_encode_hook():
|
||||
packed = packb([3, 1+2j], default=_encode_complex)
|
||||
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():
|
||||
packed = packb([3, {b'__complex__': True, b'real': 1, b'imag': 2}])
|
||||
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():
|
||||
packed = packb([3, {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)
|
||||
eq_(unpacked[1], prod_sum)
|
||||
assert unpacked[1] == prod_sum
|
||||
|
||||
@raises(ValueError)
|
||||
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():
|
||||
packed = packb([3, 1+2j], default=lambda o: o)
|
||||
unpacked = unpackb(packed, use_list=1)
|
||||
with raises(ValueError):
|
||||
packed = packb([3, 1+2j], default=lambda o: o)
|
||||
unpacked = unpackb(packed, use_list=1)
|
||||
|
||||
def _arr_to_str(arr):
|
||||
return ''.join(str(c) for c in arr)
|
||||
|
@ -47,7 +45,7 @@ def _arr_to_str(arr):
|
|||
def test_array_hook():
|
||||
packed = packb([1,2,3])
|
||||
unpacked = unpackb(packed, list_hook=_arr_to_str, use_list=1)
|
||||
eq_(unpacked, '123')
|
||||
assert unpacked == '123'
|
||||
|
||||
|
||||
class DecodeError(Exception):
|
||||
|
@ -57,18 +55,13 @@ def bad_complex_decoder(o):
|
|||
raise DecodeError("Ooops!")
|
||||
|
||||
|
||||
@raises(DecodeError)
|
||||
def test_an_exception_in_objecthook1():
|
||||
packed = packb({1: {'__complex__': True, 'real': 1, 'imag': 2}})
|
||||
unpackb(packed, object_hook=bad_complex_decoder)
|
||||
with raises(DecodeError):
|
||||
packed = packb({1: {'__complex__': True, 'real': 1, 'imag': 2}})
|
||||
unpackb(packed, object_hook=bad_complex_decoder)
|
||||
|
||||
|
||||
@raises(DecodeError)
|
||||
def test_an_exception_in_objecthook2():
|
||||
packed = packb({1: [{'__complex__': True, 'real': 1, 'imag': 2}]})
|
||||
unpackb(packed, list_hook=bad_complex_decoder, use_list=1)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
with raises(DecodeError):
|
||||
packed = packb({1: [{'__complex__': True, 'real': 1, 'imag': 2}]})
|
||||
unpackb(packed, list_hook=bad_complex_decoder, use_list=1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue