def test_parse_func_as_paramter_to_type_for_json(self):
        func_type = 'int(int a)'
        ret = PrimitiveType('int')
        param = Param('a', ret.type_hash)

        self.assertEqual(parse_func_as_param_to_type_for_json(func_type, {}),
                         FunctionType(ret, [param]))
    def test_convert_functions_to_json_type(self):
        function = {
            'f':
            FuncInfo('int f(char c);', 'f', 'file', 'int',
                     [Param('c', 'char')])
        }
        convert_func_types_to_type_for_json(function, {})
        p = PrimitiveType('int')
        c = PrimitiveType('char')
        expected = {
            'f':
            FuncInfo('int f(char c);', 'f', 'file', p.type_hash,
                     [Param('c', c.type_hash)])
        }

        self.assertEqual(function, expected)
    def test_typedef_to_int_type_for_json(self):
        types = {}
        typedef = Param('tdef', 'int')

        parse_typedef_to_type_for_json(typedef, types)
        t = PrimitiveType('int')
        tdef = TypedefedType('tdef', t)
        expected = {t.type_hash: t, tdef.type_hash: tdef}

        self.assertEqual(types, expected)
    def test_parse_ptr_to_vararg_function_to_type_for_json(self):
        types = {}
        fptr = 'void (*)(int p1, ...)'

        fptr_type = parse_type_to_type_for_json(fptr, types)
        types[fptr_type.type_hash] = fptr_type

        ret_type = PrimitiveType('void')
        p1 = PrimitiveType('int')
        func_type = FunctionType(ret_type, [Param('p1', p1.type_hash)], True)
        ptr = PointerType(func_type)
        expected = {
            ret_type.type_hash: ret_type,
            p1.type_hash: p1,
            func_type.type_hash: func_type,
            ptr.type_hash: ptr
        }

        self.assertEqual(types, expected)
    def test_convert_typedef_to_json_type(self):
        tdef = [Param('x', 'int')]
        types = {}

        convert_typedefs_to_type_for_json(tdef, types)
        x = PrimitiveType('int')

        self.assertEqual(types, {
            x.type_hash: x,
            TypedefedType('x', x).type_hash: TypedefedType('x', x)
        })
    def test_parse_union_to_type_for_json(self):
        types = {}
        union = 'union xy { int a;};'

        s_info = parse_union(union, 'file')
        parse_union_to_type_for_json(s_info, types)
        st = UnionType('xy', [])
        pt = PrimitiveType('int')
        expected = {st.type_hash: st, pt.type_hash: pt}

        self.assertEqual(types, expected)
    def test_parse_struct_to_type_for_json(self):
        types = {}
        struct = 'typedef struct xy { int a;};'

        s_info = parse_struct(struct, 'file')
        parse_struct_to_type_for_json(s_info, types)
        st = StructType('xy', [])
        pt = PrimitiveType('int')
        expected = {st.type_hash: st, pt.type_hash: pt}

        self.assertEqual(types, expected)
    def test_parse_nested_union_without_name_to_type_for_json(self):
        union = 'union s1{ union { char c; }s2; };'
        types = {}

        union = parse_struct(union, 'file')
        parse_union_to_type_for_json(union, types)
        prim = PrimitiveType('char')
        s1 = UnionType('s1', [])
        s2 = UnionType('_LOCAL_s1_s2', [])
        expected = {prim.type_hash: prim, s1.type_hash: s1, s2.type_hash: s2}

        self.assertEqual(types, expected)
    def test_parse_nested_structs_without_name_to_type_for_json(self):
        struct = 'struct s1{ struct { char c; }s2; };'
        types = {}

        struct = parse_struct(struct, 'file')
        parse_struct_to_type_for_json(struct, types)
        prim = PrimitiveType('char')
        s1 = StructType('s1', [])
        s2 = StructType('_LOCAL_s1_s2', [])
        expected = {prim.type_hash: prim, s1.type_hash: s1, s2.type_hash: s2}

        self.assertEqual(types, expected)
    def test_call_conv_is_set_only_when_one_of_CALL_CONVENTIONS(self):
        types = {}
        fptr = 'void (__MYCALL)(int p1)'

        fptr_type = parse_type_to_type_for_json(fptr, types)
        types[fptr_type.type_hash] = fptr_type

        ret_type = VoidType()
        p1 = PrimitiveType('int')
        func_type = FunctionType(ret_type, [Param('p1', p1.type_hash)], False)
        expected = {
            ret_type.type_hash: ret_type,
            p1.type_hash: p1,
            func_type.type_hash: func_type
        }
        self.assertEqual(types, expected)
    def test_parse_function_with_call_conv_to_type_for_json(self):
        types = {}
        fptr = 'void (__cdecl)(int p1)'

        fptr_type = parse_type_to_type_for_json(fptr, types)
        types[fptr_type.type_hash] = fptr_type

        ret_type = VoidType()
        p1 = PrimitiveType('int')
        func_type = FunctionType(ret_type, [Param('p1', p1.type_hash)], False,
                                 'cdecl')
        expected = {
            ret_type.type_hash: ret_type,
            p1.type_hash: p1,
            func_type.type_hash: func_type
        }
        self.assertEqual(types, expected)
    def test_parse_ptr_to_function_no_params_to_type_for_json(self):
        types = {}
        fptr = 'void * (*)()'

        fptr_type = parse_type_to_type_for_json(fptr, types)
        types[fptr_type.type_hash] = fptr_type
        ret = PrimitiveType('void')
        ret_type = PointerType(ret)
        func_type = FunctionType(ret_type, [])
        ptr = PointerType(func_type)
        expected = {
            ret_type.type_hash: ret_type,
            ret.type_hash: ret,
            func_type.type_hash: func_type,
            ptr.type_hash: ptr
        }

        self.assertEqual(types, expected)
    def test_parse_nested_union_without_name_to_type_for_json_use_typedefed_name(
            self):
        union = 'typedef union { union { char c; }s2; } Tunion;'
        types = {}

        union = parse_union(union, 'file')
        parse_union_to_type_for_json(union, types)
        prim = PrimitiveType('char')
        s1 = UnionType('_TYPEDEF_Tunion', [])
        s2 = UnionType('_LOCAL_Tunion_s2', [])
        tdef = TypedefedType('Tunion', s1)
        expected = {
            prim.type_hash: prim,
            s1.type_hash: s1,
            s2.type_hash: s2,
            tdef.type_hash: tdef
        }

        self.assertEqual(types, expected)
    def test_union_with_func_ptr_to_type_for_json(self):
        types = {}
        union = 'union s{int *(* ptr)();};'

        s_info = parse_union(union, 'file')
        parse_union_to_type_for_json(s_info, types)
        tint = PrimitiveType('int')
        ptrint = PointerType(tint)
        func = FunctionType(ptrint, [])
        func_ptr = PointerType(func)
        st = UnionType('s', [func_ptr])
        expected = {
            tint.type_hash: tint,
            ptrint.type_hash: ptrint,
            func.type_hash: func,
            func_ptr.type_hash: func_ptr,
            st.type_hash: st
        }

        self.assertEqual(types, expected)
    def test_parse_ptr_to_function_with_ptr_to_func_as_param(self):
        types = {}
        fptr = 'void (*)(void (*)())'

        fptr_type = parse_type_to_type_for_json(fptr, types)
        types[fptr_type.type_hash] = fptr_type

        ret_type = PrimitiveType('void')
        param = FunctionType(ret_type, [])
        param_ptr = PointerType(param)
        func_type = FunctionType(ret_type, [Param('', param_ptr.type_hash)])
        ptr = PointerType(func_type)
        expected = {
            ret_type.type_hash: ret_type,
            param.type_hash: param,
            param_ptr.type_hash: param_ptr,
            func_type.type_hash: func_type,
            ptr.type_hash: ptr
        }

        self.assertEqual(types, expected)
 def test_floating_point_type_correct_string_repr(self):
     self.assertEqual(
         PrimitiveType('float').__repr__(),
         "PrimitiveType('floating_point_type' 'float')")
 def test_parse_array_multi_dimensional(self):
     self.assertEqual(parse_type_to_type_for_json('int [10][10][12]', {}),
                      ArrayType(PrimitiveType('int'), [10, 10, 12]))
 def test_type_text_returns_type_as_string(self):
     self.assertEqual(PrimitiveType('int').type_text, 'integral_type')
 def test_parse_array_dimension_set_by_arithmetic_expr(self):
     self.assertEqual(
         parse_type_to_type_for_json('int [10][5 + sizeof(int) - 4 * 3]',
                                     {}),
         ArrayType(PrimitiveType('int'), [10, '5 + sizeof(int) - 4 * 3']))
 def test_ptr_to_const_ptr_to_void_is_(self):
     self.assertEqual(
         parse_type_to_type_for_json('void * const *', {}),
         PointerType(
             QualifierType('const', PointerType(PrimitiveType('void')))))
 def test_repr_json_returns_correct_dict_for_int(self):
     self.assertEqual(
         PrimitiveType('int').repr_json(), {
             'type': 'integral_type',
             'name': 'int'
         })
 def test_primitive_type_correct_string_repr(self):
     self.assertEqual(
         PrimitiveType('int').__repr__(),
         "PrimitiveType('integral_type' 'int')")
    def test_constant_type_correct_string_repr(self):
        prim = PrimitiveType('int')

        self.assertEqual(
            QualifierType('const', prim).__repr__(),
            "QualifierType('qualifier' 'const' '{}')".format(prim.type_hash))
    def test_function_type_correct_string_repr(self):
        ptr = PointerType(PrimitiveType('int'))

        self.assertEqual(
            FunctionType(ptr).__repr__(),
            "FunctionType('{}', [])".format(ptr.type_hash))
 def test_parse_const_array_one_dimension(self):
     self.assertEqual(
         parse_type_to_type_for_json('const int [10]', {}),
         ArrayType(QualifierType('const', PrimitiveType('int')), [10]))
 def test_more_modifiers_in_one_type_parsed_correctly(self):
     self.assertEqual(
         parse_type_to_type_for_json('volatile char * const', {}),
         QualifierType(
             'const',
             PointerType(QualifierType('volatile', PrimitiveType('char')))))
    def test_pointer_type_correct_string_repr(self):
        prim = PrimitiveType('int')

        self.assertEqual(
            PointerType(prim).__repr__(),
            "PointerType('{}')".format(prim.type_hash))
 def test_parse_array_one_dimension_not_specified(self):
     self.assertEqual(parse_type_to_type_for_json('int [][10]', {}),
                      ArrayType(PrimitiveType('int'), ['', 10]))
 def test_array_type_correct_string_repr(self):
     prim = PrimitiveType('int')
     self.assertEqual(
         ArrayType(prim).__repr__(),
         "ArrayType('{}', [])".format(prim.type_hash))
 def test_char_const_ptr_is_pointer_type_to_const_type_of_primitive_type(
         self):
     self.assertEqual(
         parse_type_to_type_for_json('char const *', {}),
         PointerType(QualifierType('const', PrimitiveType('char'))))