Example #1
0
 def test_collect_types_with_if(self):
   content1 = [
       c_ast.CTypeDefinition('type_name_1', 'type_definition_1'),
   ]
   content2 = [
       c_ast.CTypeDefinition('type_name_2', 'type_definition_2'),
       c_ast.CTypeDefinition('type_name_3', 'type_definition_3'),
   ]
   content3 = [
       c_ast.CTypeDefinition('type_name_4', 'type_definition_4'),
   ]
   else_content = [
       c_ast.CTypeDefinition('type_name_5', 'type_definition_5'),
   ]
   if_ = pre_ast.If(
       conditional_blocks=[
           pre_ast.ConditionalBlock('expression1', content1),
           pre_ast.ConditionalBlock('expression2', content2),
           pre_ast.ConditionalBlock('expression3', content3),
       ],
       else_content=else_content,
   )
   self.expression_evaluator.evaluate.side_effect = (
       c_ast.CNumber(0),
       c_ast.CNumber(1),
   )
   actual = self.type_collector.visit_if(if_)
   expected = {
       'type_name_2': 'type_definition_2',
       'type_name_3': 'type_definition_3',
   }
   self.assertEqual(actual, expected)
Example #2
0
 def test_collect_types_with_program(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.CTypeDefinition('type_name_3', 'type_definition_3'),
   ])
   actual = self.type_collector.collect_types(program)
   expected = {
       'type_name_1': 'type_definition_1',
       'type_name_2': 'type_definition_2',
       'type_name_3': 'type_definition_3',
   }
   self.assertEqual(actual, expected)
Example #3
0
 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_with_chained_typedef_references_ended_by_type_definition(self):
     some_type = c_ast.CTypeDefinition(
         type_name='some_type',
         type_definition='some_definition',
     )
     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)
     self.assertEqual(actual, some_type)
Example #5
0
def _insert_type_definition_into_fields(
    identifier,
    type_definition,
    *fields
):
  for field in fields:
    field.insert_type_definition(type_definition)
  return c_ast.CTypeDefinition(identifier, type_definition, list(fields))
Example #6
0
 def test_collect_types_with_type_definition(self):
   type_definition = c_ast.CTypeDefinition(
       type_name='type_name_1',
       type_definition='type_definition_1',
       following_fields='following_fields',
   )
   actual = self.type_collector.collect_types(type_definition)
   expected = {
       'type_name_1': 'type_definition_1',
   }
   self.assertEqual(actual, expected)
Example #7
0
def _insert_type_definition_into_typedef_instance(
    identifier,
    definition,
    typedef_instance,
):
  if identifier:
    definition = c_ast.CTypeDefinition(
        type_name=identifier,
        type_definition=definition,
    )
  typedef_instance.insert_type_definition(definition)
  return typedef_instance
Example #8
0
 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)
 def test_get_description_with_type_reference_resolving_to_type_definition(
     self, ):
     type_definition = c_ast.CTypeReference('some_type')
     self.typedef_resolver.resolve.return_value = c_ast.CTypeDefinition(
         type_name='some_other_name',
         type_definition='some_definition',
     )
     actual = self.type_description_visitor.get_description(
         type_definition,
         self.types,
     )
     expected = ['some_other_name']
     self.assertEqual(actual, expected)
Example #10
0
 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)