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