def test_with_chained_typedef_references_ended_by_unresolvable_type(self): some_type = c_ast.CSimpleType(33, 42) some_other_type = c_ast.CSimpleType(42, 33) type_1_t = c_ast.CTypedef( name='type_1_t', type_definition=c_ast.CTypeReference('some_type'), ) type_2_t = c_ast.CTypedef( name='type_2_t', type_definition=c_ast.CTypeReference('type_1_t'), ) type_3_t = c_ast.CTypedef( name='type_3_t', type_definition=c_ast.CTypeReference('type_2_t'), ) self.types.update({ 'some_type': some_type, 'some_other_type': some_other_type, 'type_1_t': type_1_t, 'type_2_t': type_2_t, 'type_3_t': type_3_t, }) node = c_ast.CTypeReference('type_3_t') actual = self.typedef_resolving_visitor.resolve(node, self.types) expected = c_ast.CTypeReference('some_type') self.assertEqual(actual, expected)
def test_resolve_with_typedef_with_resolvable_type(self): other_type_reference = c_ast.CTypeReference('some_other_name') node = c_ast.CTypedef( name='some_name', type_definition=other_type_reference, ) actual = self.typedef_resolving_visitor.resolve(node, self.types) self.assertEqual(actual, other_type_reference)
def test_resolve_with_typedef_with_unresolvable_type(self): other_type_reference = c_ast.CSimpleType(33, 42) node = c_ast.CTypedef( name='some_name', type_definition=other_type_reference, ) actual = self.typedef_resolving_visitor.resolve(node, self.types) self.assertIsNone(actual)
def test_collect_types_with_typedef(self): typedef = c_ast.CTypedef( name='type_name_1', type_definition=c_ast.CTypeDefinition( type_name='type_name_2', type_definition='type_definition_2', ), attributes='attributes', ) actual = self.type_collector.collect_types(typedef) expected = { 'type_name_1': typedef, 'type_name_2': 'type_definition_2', } self.assertEqual(actual, expected)
def test_collect_types_with_type_name_collision(self): program = c_ast.CProgram([ c_ast.CTypeDefinition('type_name_1', 'type_definition_1'), c_ast.CTypeDefinition('type_name_2', 'type_definition_2'), c_ast.CTypedef( name='type_name_3', type_definition=c_ast.CTypeDefinition( type_name='type_name_2', type_definition='type_definition_3', ) ), ]) with self.assertRaises( type_collecting_visitor.TwoDefinitionsOfTheSameTypeException, ): self.type_collector.collect_types(program)
def test_collect_types(self): typedef = c_ast.CTypedef( name='type_name_3', type_definition=c_ast.CTypeDefinition( type_name='type_name_4', type_definition='type_definition_3', ) ) program = c_ast.CProgram([ c_ast.CTypeDefinition('type_name_1', 'type_definition_1'), c_ast.CTypeDefinition('type_name_2', 'type_definition_2'), typedef, ]) actual = self.type_collector.collect_types(program) expected = { 'type_name_1': 'type_definition_1', 'type_name_2': 'type_definition_2', 'type_name_3': typedef, 'type_name_4': 'type_definition_3', } self.assertEqual(actual, expected)