def test_get_height(self):
     prog_tree = GeneratorAstTree.prog_tree()
     basic_if_tree = GeneratorAstTree.basic_if()
     h_prog = prog_tree.get_height()
     h_if = basic_if_tree.get_height()
     self.assertEqual(h_prog, 4)
     self.assertEqual(h_if, 3)
 def test_childrenAreCstOrVar(self):
     if_tree = GeneratorAstTree.basic_if()
     body_part = if_tree.children[1]
     self.assertEqual(
         AstToCfgConverter.check_children_are_cst_or_var(if_tree), False)
     self.assertEqual(
         AstToCfgConverter.check_children_are_cst_or_var(body_part), True)
    def test_treat_if_node(self):
        # basic if tree
        basic_if_tree = GeneratorAstTree.basic_if()
        parser_for_basic = AstToCfgConverter(basic_if_tree)
        result_for_basic = parser_for_basic.treat_if_node(basic_if_tree)
        expected_basic = {
            1: ['if', [[('==', ["x", 1])]], [2, 3]],
            2: ['assign', {
                'x': '1'
            }, [4]],
            3: ['assign', {
                'x': 'x+1'
            }, [4]]
        }
        self.assertEqual(result_for_basic, expected_basic)

        # if with sequence inside
        if_tree_with_seq = GeneratorAstTree.if_nested_seq()
        parser = AstToCfgConverter(if_tree_with_seq)
        result = parser.treat_if_node(if_tree_with_seq)
        expected = {
            1: ['if', [[('==', ["x", 1])]], [2, 3]],
            2: ['assign', {
                'x': '1'
            }, [5]],
            3: ['assign', {
                'x': '32'
            }, [4]],
            4: ['assign', {
                'x': 'x*4'
            }, [5]]
        }
        self.assertEqual(result, expected)

        # if with nested while (right)
        if_with_while_right = GeneratorAstTree.if_with_while_right_part()
        parser_for_if_while_right = AstToCfgConverter(if_with_while_right)
        result_for_if_while_right = parser_for_if_while_right.treat_if_node(
            if_with_while_right)

        expected_for_if_while = {
            1: ['if', [[('<', ['x', 5])]], [2, 3]],
            2: ['assign', {
                'x': '1'
            }, [5]],
            3: ['while', [[('<', ['x', 5])]], [4, 5]],
            4: ['assign', {
                'x': 'x+1'
            }, [3]]
        }

        self.assertEqual(result_for_if_while_right, expected_for_if_while)

        # if with nested while (left)
        if_with_while_left = GeneratorAstTree.if_with_while_left_part()
        parser_for_if_while_left = AstToCfgConverter(if_with_while_left)
        result_for_if_while_left = parser_for_if_while_left.treat_if_node(
            if_with_while_left)

        expected_for_if_while_left = {
            1: ['if', [[('<', ['x', 5])]], [2, 4]],
            2: ['while', [[('<', ['x', 5])]], [3, 5]],
            3: ['assign', {
                'x': 'x+1'
            }, [2]],
            4: ['assign', {
                'x': '1'
            }, [5]]
        }
        self.assertEqual(result_for_if_while_left, expected_for_if_while_left)

        # if with two nested while (left and right)
        if_with_two_while = GeneratorAstTree.if_with_two_while()
        parser_for_if_two_while = AstToCfgConverter(if_with_two_while)
        result_for_if_two_while = parser_for_if_two_while.treat_if_node(
            if_with_two_while)

        expected_for_if_two_while = {
            1: ['if', [[('<', ['x', 5])]], [2, 4]],
            2: ['while', [[('<', ['x', 5])]], [3, 6]],
            3: ['assign', {
                'x': 'x+1'
            }, [2]],
            4: ['while', [[('<', ['x', 5])]], [5, 6]],
            5: ['assign', {
                'x': 'x+1'
            }, [4]],
        }

        self.assertEqual(result_for_if_two_while, expected_for_if_two_while)

        # if with nested if
        if_with_if = GeneratorAstTree.if_with_if()
        parser_for_if_if = AstToCfgConverter(if_with_if)
        result_for_if_if = parser_for_if_if.treat_if_node(if_with_if)
        expected_for_if_within_if = {
            1: ['if', [[('<', ['x', 5])]], [2, 3]],
            2: ['assign', {
                'x': '1'
            }, [6]],
            3: ['if', [[('==', ['x', 1])]], [4, 5]],
            4: ['assign', {
                'x': '1'
            }, [6]],
            5: ['assign', {
                'x': 'x+1'
            }, [6]]
        }
        self.assertEqual(result_for_if_if, expected_for_if_within_if)