Skip to content

Commit 77d322d

Browse files
committed
add ext_serializable() subclass test to unit tests
1 parent c9c5c75 commit 77d322d

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

test_umsgpack.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,40 @@ class IncompleteClass:
699699
umsgpack._ext_classes_to_code = {}
700700
umsgpack._ext_code_to_classes = {}
701701

702+
def test_ext_serializable_subclass(self):
703+
@umsgpack.ext_serializable(0x10)
704+
class Rectangle:
705+
def __init__(self, length, width):
706+
self.length = length
707+
self.width = width
708+
709+
def __eq__(self, other):
710+
return self.length == other.length and self.width == other.width
711+
712+
def packb(self):
713+
return umsgpack.packb([self.length, self.width])
714+
715+
@classmethod
716+
def unpackb(cls, data):
717+
return cls(*umsgpack.unpackb(data))
718+
719+
class Square(Rectangle):
720+
def __init__(self, width):
721+
Rectangle.__init__(self, width, width)
722+
723+
# Test pack (packs base class)
724+
packed = umsgpack.packb(Square(5))
725+
self.assertEqual(packed, b"\xc7\x03\x10\x92\x05\x05")
726+
727+
# Test unpack (unpacks base class)
728+
unpacked = umsgpack.unpackb(packed)
729+
self.assertEqual(unpacked, Rectangle(5, 5))
730+
731+
# Unregister Ext serializable classes to prevent interference with
732+
# subsequent tests
733+
umsgpack._ext_classes_to_code = {}
734+
umsgpack._ext_code_to_classes = {}
735+
702736
def test_streaming_writer(self):
703737
# Try first composite test vector
704738
(_, obj, data) = composite_test_vectors[0]

0 commit comments

Comments
 (0)