def test_dynamic_array(): """Test that dynamic array types enforce matching lengths""" ns = MessageNamespace([ 'msgtype,test1,1', 'msgdata,test1,count,u16,', 'msgdata,test1,arr1,byte,count', 'msgdata,test1,arr2,u32,count' ]) # This one is fine. m = Message(ns.get_msgtype('test1'), arr1='01020304', arr2='[1,2,3,4]') buf = io.BytesIO() m.write(buf) assert buf.getvalue() == bytes( [0, 1] + [0, 4] + [1, 2, 3, 4] + [0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4]) # These ones are not with pytest.raises(ValueError, match='Inconsistent length.*count'): m = Message(ns.get_msgtype('test1'), arr1='01020304', arr2='[1,2,3]') with pytest.raises(ValueError, match='Inconsistent length.*count'): m = Message(ns.get_msgtype('test1'), arr1='01020304', arr2='[1,2,3,4,5]')
def test_message_constructor(): ns = MessageNamespace([ 'msgtype,test1,1', 'msgdata,test1,tlvs,test_tlvstream,', 'tlvtype,test_tlvstream,tlv1,1', 'tlvdata,test_tlvstream,tlv1,field1,byte,4', 'tlvdata,test_tlvstream,tlv1,field2,u32,', 'tlvtype,test_tlvstream,tlv2,255', 'tlvdata,test_tlvstream,tlv2,field3,byte,...' ]) m = Message(ns.get_msgtype('test1'), tlvs='{tlv1={field1=01020304,field2=5}' ',tlv2={field3=01020304},4=010203}') buf = io.BytesIO() m.write(buf) assert buf.getvalue() == bytes([0, 1] + [1, 8, 1, 2, 3, 4, 0, 0, 0, 5] + [4, 3, 1, 2, 3] + [253, 0, 255, 4, 1, 2, 3, 4])