1"""Test that lldb can invoke blocks and access variables inside them"""
2
3
4
5import unittest2
6import lldb
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test.decorators import *
9import lldbsuite.test.lldbutil as lldbutil
10
11
12class BlocksTestCase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15    lines = []
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line numbers to break at.
21        self.lines.append(line_number('main.c', '// Set breakpoint 0 here.'))
22        self.lines.append(line_number('main.c', '// Set breakpoint 1 here.'))
23        self.lines.append(line_number('main.c', '// Set breakpoint 2 here.'))
24
25    def launch_common(self):
26        self.build()
27        exe = self.getBuildArtifact("a.out")
28        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
29
30        self.is_started = False
31
32        # Break inside the foo function which takes a bar_ptr argument.
33        for line in self.lines:
34            lldbutil.run_break_set_by_file_and_line(
35                self, "main.c", line, num_expected_locations=1, loc_exact=True)
36
37        self.wait_for_breakpoint()
38
39    @skipUnlessDarwin
40    def test_expr(self):
41        self.launch_common()
42
43        self.expect("expression a + b", VARIABLES_DISPLAYED_CORRECTLY,
44                    substrs=["= 7"])
45
46        self.expect("expression c", VARIABLES_DISPLAYED_CORRECTLY,
47                    substrs=["= 1"])
48
49        self.wait_for_breakpoint()
50
51        # This should display correctly.
52        self.expect("expression (int)neg (-12)", VARIABLES_DISPLAYED_CORRECTLY,
53                    substrs=["= 12"])
54
55        self.wait_for_breakpoint()
56
57        self.expect_expr("h(cg)", result_type="int", result_value="42")
58
59    @skipUnlessDarwin
60    def test_define(self):
61        self.launch_common()
62
63        self.runCmd(
64            "expression int (^$add)(int, int) = ^int(int a, int b) { return a + b; };")
65        self.expect(
66            "expression $add(2,3)",
67            VARIABLES_DISPLAYED_CORRECTLY,
68            substrs=[" = 5"])
69
70        self.runCmd("expression int $a = 3")
71        self.expect(
72            "expression int (^$addA)(int) = ^int(int b) { return $a + b; };",
73            "Proper error is reported on capture",
74            error=True)
75
76    def wait_for_breakpoint(self):
77        if not self.is_started:
78            self.is_started = True
79            self.runCmd("process launch", RUN_SUCCEEDED)
80        else:
81            self.runCmd("process continue", RUN_SUCCEEDED)
82
83        # The stop reason of the thread should be breakpoint.
84        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
85                    substrs=['stopped',
86                             'stop reason = breakpoint'])
87