def test_optional_dynamic_field(): class Dynamic(prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [('a_len', prophy.u32), ('a', prophy.array(prophy.u8, bound='a_len'))] with pytest.raises(Exception) as e: prophy.optional(Dynamic) assert "optional dynamic fields not implemented" == str(e.value)
def test_optional_array(): with pytest.raises(Exception) as e: prophy.optional(prophy.array(prophy.u8, size=3)) assert "optional array not implemented" == str(e.value) with pytest.raises(Exception) as e: class S(prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a_len", prophy.optional(prophy.u32)), ("a", prophy.array(prophy.u32, bound="a_len"))] assert "array S.a must not be bound to optional field" == str(e.value) with pytest.raises(Exception) as e: prophy.array(prophy.optional(prophy.u32)) assert "array of optional type not allowed" == str(e.value)
class DifferentStruct( prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [ ("x", prophy.u8), ("y", prophy.array(prophy.u32, size=1)), ("o", prophy.optional(prophy.u32)), ]
class SameStruct( prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [ ("x", prophy.u8), ("y", prophy.u32), ("o", prophy.optional(prophy.u32)), ]
def test_optional_attributes(): assert 1 == prophy.optional(prophy.i8)._SIZE assert False == prophy.optional(prophy.i8)._DYNAMIC assert False == prophy.optional(prophy.i8)._UNLIMITED assert 0 == prophy.optional(prophy.i8)._DEFAULT assert True == prophy.optional(prophy.i8)._OPTIONAL assert 1 == prophy.optional(prophy.i8)._ALIGNMENT assert None == prophy.optional(prophy.i8)._BOUND assert None == prophy.optional(prophy.i8)._PARTIAL_ALIGNMENT
def test_optional_array(): with pytest.raises(Exception) as e: prophy.optional(prophy.array(prophy.u8, size=3)) assert "optional array not implemented" == e.value.message with pytest.raises(Exception) as e: class S(prophy.struct_packed): __metaclass__ = prophy.struct_generator _descriptor = [("a_len", prophy.optional(prophy.u32)), ("a", prophy.array(prophy.u32, bound="a_len"))] assert "array must not be bound to optional field" == e.value.message with pytest.raises(Exception) as e: prophy.array(prophy.optional(prophy.u32)) assert "array of optional type not allowed" == e.value.message
def test_optional_bytes(): with pytest.raises(Exception) as e: prophy.optional(prophy.bytes(size=3)) assert "optional bytes not implemented" == str(e.value)
class U(prophy.with_metaclass(prophy.union_generator, prophy.union)): _descriptor = [("a", prophy.u32, 0), ("b", prophy.optional(prophy.u32), 1), ("c", prophy.u32, 2)]
class O(prophy.struct_packed): __metaclass__ = prophy.struct_generator _descriptor = [("a", prophy.optional(prophy.u32)), ("b", prophy.optional(S)), ("c", prophy.optional(U))]
class O( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a", prophy.optional(prophy.u32)), ("b", prophy.optional(S)), ("c", prophy.optional(U))]
class K( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a", prophy.optional(U))]
class X(prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [('x', prophy.optional(prophy.u8)), ('y', prophy.optional(prophy.u64))]
class B( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [('a', prophy.optional(A))]
class U(prophy.union): __metaclass__ = prophy.union_generator _descriptor = [("a", prophy.u32, 0), ("b", prophy.optional(prophy.u32), 1), ("c", prophy.u32, 2)]
class X(prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [("value", prophy.optional(_type))]
def test_optional_attributes(): assert prophy.optional(prophy.i8)._SIZE == 1 assert prophy.optional(prophy.i8)._DYNAMIC is False assert prophy.optional(prophy.i8)._UNLIMITED is False assert prophy.optional(prophy.i8)._DEFAULT == 0 assert prophy.optional(prophy.i8)._OPTIONAL assert prophy.optional(prophy.i8)._ALIGNMENT == 1 assert prophy.optional(prophy.i8)._BOUND is None assert prophy.optional(prophy.i8)._PARTIAL_ALIGNMENT is None assert prophy.optional(prophy.i8)._OPTIONAL_SIZE == 5 assert prophy.optional(prophy.i8)._OPTIONAL_ALIGNMENT == 4 assert prophy.optional(prophy.i64)._OPTIONAL_SIZE == 16 assert prophy.optional(prophy.i64)._OPTIONAL_ALIGNMENT == 8
class S( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a_len", prophy.optional(prophy.u32)), ("a", prophy.array(prophy.u32, bound="a_len"))]
class B(prophy.struct_packed): __metaclass__ = prophy.struct_generator _descriptor = [('a', prophy.optional(A))]