| 
									
										
										
										
											2012-09-25 01:18:33 +10:00
										 |  |  | """Test Unpacker's read_array_header and read_map_header methods""" | 
					
						
							| 
									
										
										
										
											2012-12-10 00:31:19 +09:00
										 |  |  | from msgpack import packb, Unpacker, OutOfData | 
					
						
							| 
									
										
										
										
											2012-09-25 01:18:33 +10:00
										 |  |  | UnexpectedTypeException = ValueError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_read_array_header(): | 
					
						
							|  |  |  |     unpacker = Unpacker() | 
					
						
							|  |  |  |     unpacker.feed(packb(['a', 'b', 'c'])) | 
					
						
							|  |  |  |     assert unpacker.read_array_header() == 3 | 
					
						
							| 
									
										
										
										
											2012-10-01 01:34:58 +09:00
										 |  |  |     assert unpacker.unpack() == b'a' | 
					
						
							|  |  |  |     assert unpacker.unpack() == b'b' | 
					
						
							|  |  |  |     assert unpacker.unpack() == b'c' | 
					
						
							| 
									
										
										
										
											2012-09-25 01:18:33 +10:00
										 |  |  |     try: | 
					
						
							|  |  |  |         unpacker.unpack() | 
					
						
							|  |  |  |         assert 0, 'should raise exception' | 
					
						
							| 
									
										
										
										
											2012-12-10 00:31:19 +09:00
										 |  |  |     except OutOfData: | 
					
						
							| 
									
										
										
										
											2012-09-25 01:18:33 +10:00
										 |  |  |         assert 1, 'okay' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_read_map_header(): | 
					
						
							|  |  |  |     unpacker = Unpacker() | 
					
						
							|  |  |  |     unpacker.feed(packb({'a': 'A'})) | 
					
						
							|  |  |  |     assert unpacker.read_map_header() == 1 | 
					
						
							| 
									
										
										
										
											2012-10-01 01:34:58 +09:00
										 |  |  |     assert unpacker.unpack() == B'a' | 
					
						
							|  |  |  |     assert unpacker.unpack() == B'A' | 
					
						
							| 
									
										
										
										
											2012-09-25 01:18:33 +10:00
										 |  |  |     try: | 
					
						
							|  |  |  |         unpacker.unpack() | 
					
						
							|  |  |  |         assert 0, 'should raise exception' | 
					
						
							| 
									
										
										
										
											2012-12-10 00:31:19 +09:00
										 |  |  |     except OutOfData: | 
					
						
							| 
									
										
										
										
											2012-09-25 01:18:33 +10:00
										 |  |  |         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' | 
					
						
							|  |  |  | 
 |