Exemple #1
0
    def test_init(self):
        assert Struct().members == []
        assert Struct().pack == None
        assert Struct(*self.TEST_MEMBERS).members == self.TEST_MEMBERS
        assert Struct(pack=2).pack == 2

        assert Union(*self.TEST_MEMBERS).members == self.TEST_MEMBERS
    def test_struct(self):

        path = os.path.join(self.h_dir, 'structs.h')
        self.parser.load_file(path)
        self.parser.process_all()

        structs = self.parser.defs['structs']
        types = self.parser.defs['types']
        variables = self.parser.defs['variables']

        # Test creating a structure using only base types.
        assert ('struct_name' in structs and 'struct struct_name' in types)
        assert structs['struct_name'] == \
               Struct(('x', Type('int'), 1),
                      ('y', Type('type_type_int'), None, 2),
                      ('str', Type('char', [10]), None))
        assert ('struct_inst' in variables and variables['struct_inst']
                == (None, Type('struct struct_name')))

        # Test creating a structure using only base types.
        assert ('struct_arr' in structs and 'struct struct_arr' in types)
        assert structs['struct_arr'] == \
               Struct(('str', Type('char', [10], [20]), None))
        assert ('struct_inst' in variables and variables['struct_inst']
                == (None, Type('struct struct_name')))

        # Test creating a pointer type from a structure.
        assert ('struct_name_ptr' in types and types['struct_name_ptr']
                == Type('struct struct_name', '*'))

        assert ('struct_name2_ptr' in types and types['struct_name2_ptr']
                == Type('struct anon_struct0', '*'))

        # Test declaring a recursive structure.
        assert ('recursive_struct' in structs
                and 'struct recursive_struct' in types)
        assert structs['recursive_struct'] == \
               Struct(('next', Type('struct recursive_struct', '*'), None))

        # Test declaring near and far pointers.
        assert 'tagWNDCLASSEXA' in structs
        assert ('NPWNDCLASSEXA' in types and (types['NPWNDCLASSEXA'] == Type(
            'struct tagWNDCLASSEXA', '*', type_quals=(('near', ), ()))))

        # Test altering the packing of a structure.
        assert ('struct_name_p' in structs and 'struct struct_name_p' in types)
        assert structs['struct_name_p'] == \
               Struct(('x', Type('int'), None),
                      ('y', Type('type_type_int'), None),
                      ('str', Type('char', [10]), "brace }  \0"),
                      pack=16)
Exemple #3
0
    def test_list_equality(self):
        assert Struct(*self.TEST_MEMBERS, pack=2) == {
            'members': [ ('a', Type('int'), None),
                         ('b', Type('char', '*'), None)],
            'pack': 2 }
        assert issubclass(Struct, dict)

        assert Union(*self.TEST_MEMBERS)['members'] == self.TEST_MEMBERS
Exemple #4
0
    def test_unions(self):

        path = os.path.join(self.h_dir, 'unions.h')
        self.parser.load_file(path)
        self.parser.process_all()

        unions = self.parser.defs['unions']
        structs = self.parser.defs['structs']
        types = self.parser.defs['types']
        variables = self.parser.defs['variables']

        # Test declaring an union.
        assert 'union_name' in unions and 'union union_name' in types
        assert unions['union_name'] == \
               Union(('x', Type('int'), 1),
                     ('y', Type('int'), None),
                     pack=None)
        assert ('union_name_ptr' in types and
                types['union_name_ptr'] == Type('union union_name', '*'))

        # Test defining an unnamed union
        assert ('no_name_union_inst' in variables and
                variables['no_name_union_inst'] == (None,
                                                    Type('union anon_union0')))

        # Test defining a structure using an unnamed union internally.
        assert ('tagRID_DEVICE_INFO' in structs and
                structs['tagRID_DEVICE_INFO'] == \
                Struct(('cbSize', Type('DWORD'), None),
                       ('dwType', Type('DWORD'), None),
                       (None, Type('union anon_union1'), None)))

        assert ('RID_DEVICE_INFO' in types and
                types['RID_DEVICE_INFO'] == Type('struct tagRID_DEVICE_INFO'))
        assert ('PRID_DEVICE_INFO' in types and
                types['PRID_DEVICE_INFO'] ==
                    Type('struct tagRID_DEVICE_INFO', '*')
                )
        assert ('LPRID_DEVICE_INFO' in types and
                ( types['LPRID_DEVICE_INFO'] ==
                  Type('struct tagRID_DEVICE_INFO', '*')
                  )
                )
Exemple #5
0
 def test_repr(self):
     assert repr(Struct()) == 'Struct()'
     assert repr(Struct(*self.TEST_MEMBERS, pack=2)) == \
            ( 'Struct(' + repr(self.TEST_MEMBERS[0]) + ', ' +
              repr(self.TEST_MEMBERS[1]) + ', pack=2)' )
     assert repr(Union()) == 'Union()'