xref: /llvm-project/lldb/test/API/lang/objc/objc-class-method/TestObjCClassMethod.py (revision 193259cbcec77add8e189c4dedeefb15fef50d5e)
1"""Test calling functions in class methods."""
2
3import lldb
4from lldbsuite.test.decorators import *
5from lldbsuite.test.lldbtest import *
6from lldbsuite.test import lldbutil
7
8
9class TestObjCClassMethod(TestBase):
10
11    def setUp(self):
12        # Call super's setUp().
13        TestBase.setUp(self)
14        # Find the line numbers to break inside main().
15        self.main_source = "class.m"
16        self.break_line = line_number(
17            self.main_source, '// Set breakpoint here.')
18
19    @add_test_categories(['pyapi'])
20    def test_with_python_api(self):
21        """Test calling functions in class methods."""
22        self.build()
23        exe = self.getBuildArtifact("a.out")
24
25        target = self.dbg.CreateTarget(exe)
26        self.assertTrue(target, VALID_TARGET)
27
28        bpt = target.BreakpointCreateByLocation(
29            self.main_source, self.break_line)
30        self.assertTrue(bpt, VALID_BREAKPOINT)
31
32        # Now launch the process, and do not stop at entry point.
33        process = target.LaunchSimple(
34            None, None, self.get_process_working_directory())
35
36        self.assertTrue(process, PROCESS_IS_VALID)
37
38        # The stop reason of the thread should be breakpoint.
39        thread_list = lldbutil.get_threads_stopped_at_breakpoint(process, bpt)
40
41        # Make sure we stopped at the first breakpoint.
42        self.assertTrue(
43            len(thread_list) != 0,
44            "No thread stopped at our breakpoint.")
45        self.assertEqual(len(thread_list), 1,
46                        "More than one thread stopped at our breakpoint.")
47
48        # Now make sure we can call a function in the class method we've
49        # stopped in.
50        frame = thread_list[0].GetFrameAtIndex(0)
51        self.assertTrue(frame, "Got a valid frame 0 frame.")
52
53        cmd_value = frame.EvaluateExpression(
54            "(int)[Foo doSomethingWithString:@\"Hello\"]")
55        if self.TraceOn():
56            if cmd_value.IsValid():
57                print("cmd_value is valid")
58                print("cmd_value has the value %d" % cmd_value.GetValueAsUnsigned())
59        self.assertTrue(cmd_value.IsValid())
60        self.assertEqual(cmd_value.GetValueAsUnsigned(), 5)
61