def test_cross_reference_array_size_from_includes(): nodes = [ model.Include('x', [ model.Include('y', [ model.Constant('NUM_HEX', '0xf'), model.Constant('NUM_DEC', '3'), ]), model.Enum('E', [ model.EnumMember('E1', 'NUM_HEX'), model.EnumMember('E3', 'NUM_DEC'), ]), ]), model.Struct('X', [ model.StructMember('x', 'u32', size='NUM_DEC'), model.StructMember('y', 'u32', size='E1'), model.StructMember('z', 'u32', size='UNKNOWN'), model.StructMember('a', 'u32', size='E3'), ]) ] constants = model.cross_reference(nodes) assert nodes[1].members[0].numeric_size == 3 assert nodes[1].members[1].numeric_size == 15 assert nodes[1].members[2].numeric_size is None assert nodes[1].members[3].numeric_size == 3 assert constants == { 'E1': 15, 'E3': 3, 'NUM_DEC': 3, 'NUM_HEX': 15, }
def test_cross_reference_array_size_from_includes(): nodes = [ model.Include('x', [ model.Include('y', [ model.Constant('NUM', '3'), ]), model.Enum('E', [ model.EnumMember('E1', '1'), model.EnumMember('E3', 'NUM') ]), ]), model.Struct('X', [ model.StructMember('x', 'u32', size = 'NUM'), model.StructMember('y', 'u32', size = 'E1'), model.StructMember('z', 'u32', size = 'UNKNOWN'), model.StructMember('a', 'u32', size = 'E3') ]) ] model.cross_reference(nodes) assert nodes[1].members[0].numeric_size == 3 assert nodes[1].members[1].numeric_size == 1 assert nodes[1].members[2].numeric_size == None assert nodes[1].members[3].numeric_size == 3
def test_evaluate_sizes_array_with_named_size(): nodes = process([ model.Constant('NUM', '3'), model.Enum('E', [ model.EnumMember('E1', '1'), model.EnumMember('E3', 'NUM') ]), model.Struct('X', [ model.StructMember('x', 'u32', size = 'NUM'), model.StructMember('y', 'u32', size = 'E1'), model.StructMember('z', 'u32', size = 'E3') ]), model.Struct('Y', [ model.StructMember('x', 'u32', size = 'UNKNOWN'), model.StructMember('y', 'u32') ]) ]) assert list(map(get_size_alignment_padding, get_members_and_node(nodes[2]))) == [ (12, 4, 0), (4, 4, 0), (12, 4, 0), (28, 4) ] assert list(map(get_size_alignment_padding, get_members_and_node(nodes[3]))) == [ (None, None, None), (4, 4, None), (None, None) ]
def test_multiple_enums(): hpp = """\ typedef enum Enum { Enum_One = 1, Enum_Two = 2, Enum_Three = 3 } Enum; struct X { Enum a; Enum b; Enum c; }; """ assert parse(hpp) == [ model.Enum("Enum", [ model.EnumMember("Enum_One", "1"), model.EnumMember("Enum_Two", "2"), model.EnumMember("Enum_Three", "3") ]), model.Struct("X", [ model.StructMember("a", "Enum"), model.StructMember("b", "Enum"), model.StructMember("c", "Enum") ]) ]
def test_namespaced_enum(): hpp = """\ namespace m { namespace n { enum Enum { Enum_One = 1, Enum_Two = 2, Enum_Three = 3 }; } } struct X { m::n::Enum a; }; """ assert parse(hpp) == [ model.Enum("m__n__Enum", [ model.EnumMember("Enum_One", "1"), model.EnumMember("Enum_Two", "2"), model.EnumMember("Enum_Three", "3") ]), model.Struct("X", [model.StructMember("a", "m__n__Enum")]) ]
def test_definitions_enums(): nodes = [ model.Enum("EEnum", [ model.EnumMember("EEnum_A", "0"), model.EnumMember("EEnum_B", "1"), model.EnumMember("EEnum_C", "2") ]) ] assert generate_definitions(nodes) == """\
def test_enum_str_and_repr(): enum = model.Enum('TheEnum', [ model.EnumMember('E1', 1), model.EnumMember('E2', '2'), model.EnumMember('E3', '3'), ]) assert str(enum) == """\ enum TheEnum { E1 = 1; E2 = '2'; E3 = '3'; }; """ assert_repr_reproduces(enum)
def test_definitions_newlines(): nodes = [ model.Typedef("a", "b"), model.Typedef("c", "d"), model.Enum("E1", [model.EnumMember("E1_A", "0")]), model.Enum("E2", [model.EnumMember("E2_A", "0")]), model.Constant("CONST_A", "0"), model.Typedef("e", "f"), model.Constant("CONST_B", "0"), model.Constant("CONST_C", "0"), model.Struct("A", [model.StructMember("a", "u32")]), model.Struct("B", [model.StructMember("b", "u32")]) ] assert generate_definitions(nodes) == """\
def test_swap_enum_in_struct(): nodes = process([ model.Enum("E1", [model.EnumMember("E1_A", "0")]), model.Struct("X", [(model.StructMember("x", "E1"))]) ]) assert generate_swap(nodes) == """\
def test_include_no_error_with_enum(): content = """\ #include "test.prophy" typedef X Y; const Z = X1; """ parse(content, lambda path: [model.Enum('X', [model.EnumMember('X1', '1')])])
def make_enum(xml_elem): def check_for_duplicates(enum_obj): values = set() for m in enum_obj.members: if m.value in values: raise ValueError( "Duplicate Enum value in '{}', value '{}'.".format( enum_obj.name, m.value)) values.add(m.value) if len(xml_elem): members = [] for member in xml_elem: value = member.get('value') try: int_value = int(value, 0) if int_value < 0: value = "0x{:X}".format(0x100000000 + int_value) except ValueError: pass members.append( model.EnumMember(member.get("name"), expand_operators(value), docstring=get_docstr(member))) enum = model.Enum(xml_elem.get("name"), members, docstring=get_docstr(xml_elem)) check_for_duplicates(enum) return enum
def test_enums_parsing(): xml = """\ <x> <enum name="EEnum"> <enum-member name="EEnum_A" value="0"/> <enum-member name="EEnum_B" value="1"/> <enum-member name="EEnum_C" value="-1"/> </enum> </x> """ assert parse(xml) == [ model.Enum("EEnum", [ model.EnumMember("EEnum_A", "0"), model.EnumMember("EEnum_B", "1"), model.EnumMember("EEnum_C", "0xFFFFFFFF"), ]) ]
def get_enum_member(cursor): name = cursor.spelling.decode() value = cursor.enum_value if value < 0: value = "0x%X" % (0x100000000 + value) else: value = str(value) return model.EnumMember(name, value)
def test_rename_field_not_composite(): nodes = [model.Enum("MyEnum", [model.EnumMember("val", "123")])] patches = {'MyEnum': [patch.Action('rename', ['field3', 'field69'])]} with pytest.raises(Exception) as e: patch.patch(nodes, patches) assert 'Can rename fields only in composites: MyEnum' in str(e.value)
def test_swap_enum_in_greedy_array(): nodes = process([ model.Enum("E1", [model.EnumMember("E1_A", "0")]), model.Struct("EnumGreedyArray", [(model.StructMember("x", "E1", unlimited=True))]) ]) assert generate_swap(nodes) == """\
def test_swap_enum_in_union(): nodes = process([ model.Enum("E1", [model.EnumMember("E1_A", "0")]), model.Union("EnumUnion", [ (model.UnionMember("x", "E1", 1)), ]) ]) assert generate_swap(nodes) == """\
def test_enums_rendering(): nodes = [ model.Enum("EEnum", [(model.EnumMember("EEnum_A", "0")), (model.EnumMember("EEnum_B", "1")), (model.EnumMember("EEnum_C", "2"))]) ] ref = """\ class EEnum(prophy.with_metaclass(prophy.enum_generator, prophy.enum)): _enumerators = [('EEnum_A', 0), ('EEnum_B', 1), ('EEnum_C', 2)] EEnum_A = 0 EEnum_B = 1 EEnum_C = 2 """ assert ref == serialize(nodes)
def test_enum_with_negative_one_values(): hpp = """\ enum Enum { Enum_MinusOne = -1, Enum_MinusTwo = -2, Enum_MinusThree = -3 }; struct X { Enum a; }; """ assert parse(hpp) == [ model.Enum("Enum", [ model.EnumMember("Enum_MinusOne", "0xFFFFFFFF"), model.EnumMember("Enum_MinusTwo", "0xFFFFFFFE"), model.EnumMember("Enum_MinusThree", "0xFFFFFFFD") ]), model.Struct("X", [model.StructMember("a", "Enum")]) ]
def test_generate_swap_declarations(): nodes = process([ model.Struct("A", [ (model.StructMember("x", "u32")), ]), model.Union("B", [ (model.UnionMember("x", "u32", 1)), ]), model.Enum("C", [model.EnumMember("E1_A", "0")]) ]) assert generate_swap_declarations(nodes) == """\
def test_enum(): hpp = """\ enum Enum { Enum_One = 1, Enum_Two = 2, Enum_Three = 3 }; struct X { Enum a; }; """ assert parse(hpp) == [ model.Enum("Enum", [ model.EnumMember("Enum_One", "1"), model.EnumMember("Enum_Two", "2"), model.EnumMember("Enum_Three", "3") ]), model.Struct("X", [model.StructMember("a", "Enum")]) ]
def test_c_enum(extension): hpp = """\ typedef enum { Enum_One = 1, Enum_Two = 2, Enum_Three = 3 } Enum; struct X { Enum a; }; """ assert parse(hpp, extension) == [ model.Enum("Enum", [ model.EnumMember("Enum_One", "1"), model.EnumMember("Enum_Two", "2"), model.EnumMember("Enum_Three", "3") ]), model.Struct("X", [model.StructMember("a", "Enum")]) ]
def test_enums_parsing(): content = """\ enum enum_t { enum_t_1 = 1, enum_t_2 = 2, enum_t_3 = enum_t_2 }; enum enum2_t { enum2_t_1 = enum_t_3 }; """ assert parse(content) == [ model.Enum('enum_t', [ model.EnumMember('enum_t_1', '1'), model.EnumMember('enum_t_2', '2'), model.EnumMember('enum_t_3', 'enum_t_2') ]), model.Enum('enum2_t', [model.EnumMember('enum2_t_1', 'enum_t_3')]) ]
def test_swap_enum_in_arrays(): nodes = process([ model.Enum("E1", [model.EnumMember("E1_A", "0")]), model.Struct( "EnumArrays", [(model.StructMember("a", "E1", size=2)), (model.StructMember("num_of_b", "u32")), (model.StructMember("b", "E1", size=2, bound="num_of_b")), (model.StructMember("num_of_c", "u32")), (model.StructMember("c", "E1", bound="num_of_c"))]) ]) assert generate_swap(nodes) == """\
def test_evaluate_sizes_enum(): nodes = process([ model.Enum('E', [ model.EnumMember('E1', '1') ]), model.Struct('X', [ model.StructMember('x', 'E'), model.StructMember('y', 'i8'), ]) ]) assert list(map(get_size_alignment_padding, get_members_and_node(nodes[1]))) == [ (4, 4, 0), (1, 1, 3), (8, 4) ]
def test_rename_node(): nodes = [ model.Struct("OldStruct", [model.StructMember("field", "u32")]), model.Enum("OldEnum", [model.EnumMember("val", "123")]) ] patches = { 'OldStruct': [patch.Action('rename', ['NewStruct'])], 'OldEnum': [patch.Action('rename', ['NewEnum'])] } patch.patch(nodes, patches) assert nodes[0].name == 'NewStruct' assert nodes[1].name == 'NewEnum'
def test_operator_expansion_in_enum_and_constant(): xml = """\ <x> <constant name="Constant" value="bitMaskOr(value_one, value_two)"/> <enum name="Enum"> <enum-member name="Enum_A" value="shiftLeft(Constant, 16)"/> </enum> </x> """ nodes = parse(xml) assert nodes == [ ("Constant", "((value_one) | (value_two))"), (model.Enum("Enum", [ model.EnumMember("Enum_A", "((Constant) << (16))") ]) )]
def test_structs_with_limited_array_parsing(): content = """\ enum sizes { size = 10 }; struct test { u32 x<5>; u32 y<size>; }; """ assert parse(content) == [ model.Enum('sizes', [model.EnumMember('size', '10')]), model.Struct('test', [ model.StructMember('num_of_x', 'u32'), model.StructMember('x', 'u32', bound='num_of_x', size='5'), model.StructMember('num_of_y', 'u32'), model.StructMember('y', 'u32', bound='num_of_y', size='size') ]) ]
def test_python_translator_1(): ih = [] th = [] for x in range(20, 200, 60): ih.append( model.Include("test_include_" + str(x), [model.Constant("n_%s" % x, x, "doc")])) th.append( model.Typedef("td_elem_name_" + str(x), "td_elem_val_" + str(x))) th.append( model.Typedef("td_elem_name_" + str(x), "i_td_elem_val_" + str(x))) th.append( model.Typedef("td_elem_name_" + str(x), "u_td_elem_val_" + str(x))) enum = [] for x in range(1, 100, 30): enum.append((model.EnumMember("elem_" + str(x), "val_" + str(x)))) name = "MAC_L2CallConfigResp" members = [model.StructMember('messageResult', 'SMessageResult')] msg_h = model.Struct(name, members) nodes = [] nodes += ih nodes += [ model.Constant("C_A", "5"), model.Constant("C_B", "5"), model.Constant("C_C", "C_B + C_A") ] nodes += th nodes += [model.Enum("test", enum)] nodes += [msg_h] python_translator = _PythonTranslator() output = python_translator(nodes, "") ref = """\ # -*- encoding: utf-8 -*- # This file has been generated by prophyc. import sys import prophy if sys.version_info < (3,): from test_include_20 import n_20 else: from .test_include_20 import n_20 if sys.version_info < (3,): from test_include_80 import n_80 else: from .test_include_80 import n_80 if sys.version_info < (3,): from test_include_140 import n_140 else: from .test_include_140 import n_140 C_A = 5 C_B = 5 C_C = C_B + C_A td_elem_name_20 = td_elem_val_20 td_elem_name_20 = i_td_elem_val_20 td_elem_name_20 = u_td_elem_val_20 td_elem_name_80 = td_elem_val_80 td_elem_name_80 = i_td_elem_val_80 td_elem_name_80 = u_td_elem_val_80 td_elem_name_140 = td_elem_val_140 td_elem_name_140 = i_td_elem_val_140 td_elem_name_140 = u_td_elem_val_140 class test(prophy.with_metaclass(prophy.enum_generator, prophy.enum)): _enumerators = [ ('elem_1', val_1), ('elem_31', val_31), ('elem_61', val_61), ('elem_91', val_91), ] elem_1 = val_1 elem_31 = val_31 elem_61 = val_61 elem_91 = val_91 class MAC_L2CallConfigResp(prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [ ('messageResult', SMessageResult), ] """ assert output == ref
def make_enum_member(elem): value = elem.get('value') value = value if value != "-1" else "0xFFFFFFFF" return model.EnumMember(elem.get("name"), expand_operators(value))
def larger_model(lorem_with_breaks): return [ model.Typedef('a', 'i16'), model.Typedef('c', 'a'), model.Include('some_defs', [ model.Struct('IncludedStruct', [ model.StructMember( 'member1', 'r32', docstring='doc for member1'), model.StructMember( 'member2', 'u64', docstring='docstring for member1') ]), model.Typedef('c', 'a'), ]), model.Include('cplx', [ model.Struct('cint16_t', [ model.StructMember('re', 'i16', docstring='real'), model.StructMember('im', 'i16', docstring='imaginary') ]), model.Struct('cint32_t', [ model.StructMember('re', 'i32', docstring='real'), model.StructMember('im', 'i32', docstring='imaginary') ]), ]), model.Union('the_union', [ model.UnionMember('a', 'IncludedStruct', 0), model.UnionMember( 'field_with_a_long_name', 'cint16_t', 1, docstring="Shorter"), model.UnionMember('field_with_a_longer_name', 'cint32_t', 2, docstring="Longer description"), model.UnionMember('other', 'i32', 4090, docstring='This one has larger discriminator'), ], "spec for that union"), model.Enum( 'E1', [ model.EnumMember('E1_A', '0', 'enum1 constant value A'), model.EnumMember('E1_B_has_a_long_name', '1', 'enum1 constant va3lue B'), model.EnumMember('E1_C_desc', '2', lorem_with_breaks[:150]), ], "Enumerator is a model type that is not supposed to be serialized. Its definition represents yet another " "syntax variation for typing a constant. Of course elements of it's type are serializable " "(as int32)"), model.Enum('E2', [ model.EnumMember('E2_A', '0', "Short\nmultiline\ndoc"), ]), model.Constant('CONST_A', '6'), model.Constant('CONST_B', '0'), model.Struct('StructMemberKinds', [ model.StructMember('member_without_docstring', 'i16'), model.StructMember('ext_size', 'i16', docstring='arbitrary sizer for dynamic arrays'), model.StructMember('optional_element', 'cint16_t', optional=True, docstring='optional array'), model.StructMember('fixed_array', 'cint16_t', size=3, docstring='Array with static size.'), model.StructMember('samples', 'cint16_t', bound='ext_size', docstring='dynamic (ext.sized) array'), model.StructMember('limited_array', 'r64', size=4, bound='ext_size', docstring='Has statically ' 'evaluable maximum size.'), model.StructMember('greedy', 'cint16_t', greedy=True, docstring='Represents array of arbitrary ' 'number of elements. Buffer size ' 'must be multiply of element size.'), ], lorem_with_breaks[:400]), ]