Esempio n. 1
0
def test_node_eq():
    a = model.Enum("name", [], "docstr 1")
    b = model.Enum("name", [], "docstr 2")
    c = model.Struct("name", [], "docstr 3")

    assert a == b
    assert a != c
Esempio n. 2
0
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) == """\
Esempio n. 3
0
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")])
    ]
Esempio n. 4
0
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
Esempio n. 5
0
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,
    }
Esempio n. 6
0
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) == """\
Esempio n. 7
0
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
Esempio n. 8
0
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')])])
Esempio n. 9
0
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)
    ]
Esempio n. 10
0
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")
        ])
    ]
Esempio n. 11
0
def test_model_sort_enums():
    nodes = [model.Typedef("B", "A"),
             model.Typedef("C", "A"),
             model.Enum("A", [])]

    model.topological_sort(nodes)

    assert ["A", "B", "C"] == [node.name for node in nodes]
Esempio n. 12
0
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) == """\
Esempio n. 13
0
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)
Esempio n. 14
0
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) == """\
Esempio n. 15
0
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) == """\
Esempio n. 16
0
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) == """\
Esempio n. 17
0
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')])
    ]
Esempio n. 18
0
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) == """\
Esempio n. 19
0
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)
Esempio n. 20
0
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)
    ]
Esempio n. 21
0
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'
Esempio n. 22
0
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"),
        ])
    ]
Esempio n. 23
0
def test_enums_rendering():
    nodes = [
        model.Enum("EEnum", [("EEnum_A", "0"), ("EEnum_B", "1"),
                             ("EEnum_C", "2")])
    ]

    ref = """\
class EEnum(prophy.enum):
    __metaclass__ = prophy.enum_generator
    _enumerators  = [('EEnum_A', 0),
                     ('EEnum_B', 1),
                     ('EEnum_C', 2)]

EEnum_A = 0
EEnum_B = 1
EEnum_C = 2
"""
    assert ref == serialize(nodes)
Esempio n. 24
0
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))")
        ])
    )]
Esempio n. 25
0
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 serialize(nodes) == ref
Esempio n. 26
0
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")])
    ]
Esempio n. 27
0
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")])
    ]
Esempio n. 28
0
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")])
    ]
Esempio n. 29
0
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')
        ])
    ]
Esempio n. 30
0
 def add_enum(self, cursor):
     members = list(map(get_enum_member, cursor.get_children()))
     node = model.Enum(alphanumeric_name(cursor), members)
     self._add_node(node)