xref: /llvm-project/lldb/test/API/lang/objc/objc-static-method/TestObjCStaticMethod.py (revision 80fcecb13c388ff087a27a4b0e7ca3dd8c98eaa4)
199451b44SJordan Rupprecht"""Test calling functions in static methods."""
299451b44SJordan Rupprecht
399451b44SJordan Rupprecht
499451b44SJordan Rupprechtimport lldb
599451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
699451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
799451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
899451b44SJordan Rupprecht
999451b44SJordan Rupprecht
1099451b44SJordan Rupprechtclass TestObjCStaticMethod(TestBase):
1199451b44SJordan Rupprecht    def setUp(self):
1299451b44SJordan Rupprecht        # Call super's setUp().
1399451b44SJordan Rupprecht        TestBase.setUp(self)
1499451b44SJordan Rupprecht        # Find the line numbers to break inside main().
1599451b44SJordan Rupprecht        self.main_source = "static.m"
162238dcc3SJonas Devlieghere        self.break_line = line_number(self.main_source, "// Set breakpoint here.")
1799451b44SJordan Rupprecht
182238dcc3SJonas Devlieghere    @add_test_categories(["pyapi"])
1999451b44SJordan Rupprecht    # <rdar://problem/9745789> "expression" can't call functions in class methods
2099451b44SJordan Rupprecht    def test_with_python_api(self):
2199451b44SJordan Rupprecht        """Test calling functions in static methods."""
2299451b44SJordan Rupprecht        self.build()
2399451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
2499451b44SJordan Rupprecht
2599451b44SJordan Rupprecht        target = self.dbg.CreateTarget(exe)
2699451b44SJordan Rupprecht        self.assertTrue(target, VALID_TARGET)
2799451b44SJordan Rupprecht
282238dcc3SJonas Devlieghere        bpt = target.BreakpointCreateByLocation(self.main_source, self.break_line)
2999451b44SJordan Rupprecht        self.assertTrue(bpt, VALID_BREAKPOINT)
3099451b44SJordan Rupprecht
3199451b44SJordan Rupprecht        # Now launch the process, and do not stop at entry point.
322238dcc3SJonas Devlieghere        process = target.LaunchSimple(None, None, self.get_process_working_directory())
3399451b44SJordan Rupprecht
3499451b44SJordan Rupprecht        self.assertTrue(process, PROCESS_IS_VALID)
3599451b44SJordan Rupprecht
3699451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
3799451b44SJordan Rupprecht        thread_list = lldbutil.get_threads_stopped_at_breakpoint(process, bpt)
3899451b44SJordan Rupprecht
3999451b44SJordan Rupprecht        # Make sure we stopped at the first breakpoint.
402238dcc3SJonas Devlieghere        self.assertNotEqual(len(thread_list), 0, "No thread stopped at our breakpoint.")
41*80fcecb1SJonas Devlieghere        self.assertEqual(
422238dcc3SJonas Devlieghere            len(thread_list), 1, "More than one thread stopped at our breakpoint."
432238dcc3SJonas Devlieghere        )
4499451b44SJordan Rupprecht
4599451b44SJordan Rupprecht        # Now make sure we can call a function in the static method we've
4699451b44SJordan Rupprecht        # stopped in.
4799451b44SJordan Rupprecht        frame = thread_list[0].GetFrameAtIndex(0)
4899451b44SJordan Rupprecht        self.assertTrue(frame, "Got a valid frame 0 frame.")
4999451b44SJordan Rupprecht
5099451b44SJordan Rupprecht        cmd_value = frame.EvaluateExpression("(char *) sel_getName (_cmd)")
5199451b44SJordan Rupprecht        self.assertTrue(cmd_value.IsValid())
5299451b44SJordan Rupprecht        sel_name = cmd_value.GetSummary()
530ed758b2SDave Lee        self.assertEqual(
542238dcc3SJonas Devlieghere            sel_name,
552238dcc3SJonas Devlieghere            '"doSomethingWithString:"',
562238dcc3SJonas Devlieghere            "Got the right value for the selector as string.",
572238dcc3SJonas Devlieghere        )
5899451b44SJordan Rupprecht
592238dcc3SJonas Devlieghere        cmd_value = frame.EvaluateExpression("[self doSomethingElseWithString:string]")
6099451b44SJordan Rupprecht        self.assertTrue(cmd_value.IsValid())
6199451b44SJordan Rupprecht        string_length = cmd_value.GetValueAsUnsigned()
620ed758b2SDave Lee        self.assertEqual(
632238dcc3SJonas Devlieghere            string_length,
642238dcc3SJonas Devlieghere            27,
652238dcc3SJonas Devlieghere            "Got the right value from another class method on the same class.",
662238dcc3SJonas Devlieghere        )
67