def test_parse_with_nested_if_blocks(self): source = """ #if CONFIG_SOMETHING #if CONFIG_SOMETHING_ELSE #define FOO #endif #else #define BAR #endif """ actual = self.parser.parse(source) expected = pre_ast.CompositeBlock([ pre_ast.If(conditional_blocks=[ pre_ast.ConditionalBlock( conditional_expression='CONFIG_SOMETHING', content=[ pre_ast.If(conditional_blocks=[ pre_ast.ConditionalBlock( conditional_expression="CONFIG_SOMETHING_ELSE", content=[ pre_ast.DefineObjectLike(name="FOO", replacement="") ], ) ]) ]), pre_ast.ConditionalBlock( conditional_expression='1', content=[ pre_ast.DefineObjectLike(name="BAR", replacement="") ], ), ]), ]) self.assertASTEqual(actual, expected)
def test_parse_with_top_level_ifdef_and_else_blocks(self): source = """ int a; #ifdef CONFIG_SOMETHING struct s { int x; } y; struct s t, u; #else int z; #endif int b; """ actual = self.parser.parse(source) expected = pre_ast.CompositeBlock([ pre_ast.TextBlock('int a;'), pre_ast.If(conditional_blocks=[ pre_ast.ConditionalBlock( conditional_expression='defined(CONFIG_SOMETHING)', content=[ pre_ast.TextBlock( '\n'.join(( 'struct s {', ' int x;', '} y;', 'struct s t, u;', )), ), ]), pre_ast.ConditionalBlock(conditional_expression="1", content=[pre_ast.TextBlock('int z;')]) ]), pre_ast.TextBlock('int b;') ]) self.assertASTEqual(actual, expected)
def test_parse_with_empty_if_elif_and_else_blocks(self): source = """ #if CONFIG_SOMETHING #elif defined(CONFIG_SOMETHING_ELSE) #else #endif """ actual = self.parser.parse(source) expected = pre_ast.CompositeBlock([ pre_ast.If(conditional_blocks=[ pre_ast.ConditionalBlock( conditional_expression='CONFIG_SOMETHING', content=[], ), pre_ast.ConditionalBlock( conditional_expression='defined(CONFIG_SOMETHING_ELSE)', content=[], ), pre_ast.ConditionalBlock( conditional_expression='1', content=[], ), ]), ]) self.assertASTEqual(actual, expected)
def _add_conditional_block(self, expression): conditional_block = pre_ast.ConditionalBlock( conditional_expression=expression, content=[]) if_block = pre_ast.If([conditional_block]) self.current_node.content.append(if_block) self.push_node(if_block) self.push_node(conditional_block)
def test_parse_with_empty_ifndef_block(self): source = """ #ifndef CONFIG_SOMETHING #endif """ actual = self.parser.parse(source) expected = pre_ast.CompositeBlock([ pre_ast.If([ pre_ast.ConditionalBlock( conditional_expression="!defined(CONFIG_SOMETHING)", content=[]), ]) ]) self.assertASTEqual(actual, expected)
def test_collect_includes_with_if(self): mock_conditiona_block_1 = mock.MagicMock() mock_conditiona_block_2 = mock.MagicMock() mock_conditiona_block_3 = mock.MagicMock() node = pre_ast.If(conditional_blocks=[ mock_conditiona_block_1, mock_conditiona_block_2, mock_conditiona_block_3, ], ) mock_conditiona_block_1.accept.return_value = [33, 42] mock_conditiona_block_2.accept.return_value = [] mock_conditiona_block_3.accept.return_value = ['foo', 'bar'] actual = self.include_collector.collect_includes(node) expected = [33, 42, 'foo', 'bar'] self.assertEqual(actual, expected)
def test_resolve_with_if(self): mock_conditional_block_1 = mock.MagicMock() mock_conditional_block_2 = mock.MagicMock() mock_conditional_block_3 = mock.MagicMock() node = pre_ast.If(conditional_blocks=[ mock_conditional_block_1, mock_conditional_block_2, mock_conditional_block_3, ], ) self.include_linking_visitor.resolve(node, self.files) for mock_node in ( mock_conditional_block_1, mock_conditional_block_2, mock_conditional_block_3, ): mock_node.accept.assert_called_once_with( self.include_linking_visitor, self.files, )
def test_parse_with_empty_ifndef_header_guard(self): source = """ #ifndef _SOMETHING_H #define _SOMETHING_H #endif """ actual = self.parser.parse(source) expected = pre_ast.CompositeBlock([ pre_ast.If([ pre_ast.ConditionalBlock( conditional_expression="!defined(_SOMETHING_H)", content=[ pre_ast.DefineObjectLike( name='_SOMETHING_H', replacement="", ), ], ), ]), ]) self.assertASTEqual(actual, expected)