summaryrefslogtreecommitdiff
path: root/test/test_read_size.py
diff options
context:
space:
mode:
authorJoel Nothman <joel.nothman@gmail.com>2012-09-25 01:18:33 +1000
committerJoel Nothman <joel.nothman@gmail.com>2012-09-25 01:19:07 +1000
commit0431a766f4e069d74627441aa3facbc7e64e4511 (patch)
treed571b0c381cd579be529402992d7dc2e27e53f23 /test/test_read_size.py
parentd56e2b2c8aa1005fbac3b584cd003ba0cdece2e2 (diff)
downloadmsgpack-python-0431a766f4e069d74627441aa3facbc7e64e4511.tar.gz
read_array/map_header functionality
Diffstat (limited to 'test/test_read_size.py')
-rw-r--r--test/test_read_size.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/test/test_read_size.py b/test/test_read_size.py
new file mode 100644
index 0000000..714f963
--- /dev/null
+++ b/test/test_read_size.py
@@ -0,0 +1,66 @@
+"""Test Unpacker's read_array_header and read_map_header methods"""
+from msgpack import packb, Unpacker
+UnexpectedTypeException = ValueError
+
+def test_read_array_header():
+ unpacker = Unpacker()
+ unpacker.feed(packb(['a', 'b', 'c']))
+ assert unpacker.read_array_header() == 3
+ assert unpacker.unpack() == 'a'
+ assert unpacker.unpack() == 'b'
+ assert unpacker.unpack() == 'c'
+ try:
+ unpacker.unpack()
+ assert 0, 'should raise exception'
+ except StopIteration:
+ assert 1, 'okay'
+
+
+def test_read_map_header():
+ unpacker = Unpacker()
+ unpacker.feed(packb({'a': 'A'}))
+ assert unpacker.read_map_header() == 1
+ assert unpacker.unpack() == 'a'
+ assert unpacker.unpack() == 'A'
+ try:
+ unpacker.unpack()
+ assert 0, 'should raise exception'
+ except StopIteration:
+ assert 1, 'okay'
+
+def test_incorrect_type_array():
+ unpacker = Unpacker()
+ unpacker.feed(packb(1))
+ try:
+ unpacker.read_array_header()
+ assert 0, 'should raise exception'
+ except UnexpectedTypeException:
+ assert 1, 'okay'
+
+def test_incorrect_type_map():
+ unpacker = Unpacker()
+ unpacker.feed(packb(1))
+ try:
+ unpacker.read_map_header()
+ assert 0, 'should raise exception'
+ except UnexpectedTypeException:
+ assert 1, 'okay'
+
+def test_correct_type_nested_array():
+ unpacker = Unpacker()
+ unpacker.feed(packb({'a': ['b', 'c', 'd']}))
+ try:
+ unpacker.read_array_header()
+ assert 0, 'should raise exception'
+ except UnexpectedTypeException:
+ assert 1, 'okay'
+
+def test_incorrect_type_nested_map():
+ unpacker = Unpacker()
+ unpacker.feed(packb([{'a': 'b'}]))
+ try:
+ unpacker.read_map_header()
+ assert 0, 'should raise exception'
+ except UnexpectedTypeException:
+ assert 1, 'okay'
+