xref: /llvm-project/lldb/test/API/lang/objc/modules/TestObjCModules.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""Test that importing modules in Objective-C works as expected."""
2
3
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class ObjCModulesTestCase(TestBase):
11    def setUp(self):
12        # Call super's setUp().
13        TestBase.setUp(self)
14        # Find the line number to break inside main().
15        self.line = line_number("main.m", "// Set breakpoint 0 here.")
16
17    @skipIf(macos_version=["<", "10.12"])
18    def test_expr(self):
19        self.build()
20        exe = self.getBuildArtifact("a.out")
21        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
22
23        # Break inside the foo function which takes a bar_ptr argument.
24        lldbutil.run_break_set_by_file_and_line(
25            self, "main.m", self.line, num_expected_locations=1, loc_exact=True
26        )
27
28        self.runCmd("run", RUN_SUCCEEDED)
29
30        # The stop reason of the thread should be breakpoint.
31        self.expect(
32            "thread list",
33            STOPPED_DUE_TO_BREAKPOINT,
34            substrs=["stopped", "stop reason = breakpoint"],
35        )
36
37        # The breakpoint should have a hit count of 1.
38        lldbutil.check_breakpoint(self, bpno=1, expected_hit_count=1)
39
40        self.expect(
41            "expr @import Darwin; 3",
42            VARIABLES_DISPLAYED_CORRECTLY,
43            substrs=["int", "3"],
44        )
45
46        self.expect("expr getpid()", VARIABLES_DISPLAYED_CORRECTLY, substrs=["pid_t"])
47
48        self.expect(
49            "expr @import Foundation; 4",
50            VARIABLES_DISPLAYED_CORRECTLY,
51            substrs=["int", "4"],
52        )
53
54        # Type lookup should still work and print something reasonable
55        # for types from the module.
56        self.expect("type lookup NSObject", substrs=["instanceMethod"])
57
58        self.expect(
59            "expr string.length",
60            VARIABLES_DISPLAYED_CORRECTLY,
61            substrs=["NSUInteger", "5"],
62        )
63
64        self.expect(
65            "expr array.count",
66            VARIABLES_DISPLAYED_CORRECTLY,
67            substrs=["NSUInteger", "3"],
68        )
69
70        self.expect(
71            'expression *[NSURL URLWithString:@"http://lldb.llvm.org"]',
72            VARIABLES_DISPLAYED_CORRECTLY,
73            substrs=["NSURL", "isa", "_urlString"],
74        )
75
76        self.expect(
77            'expression [NSURL URLWithString:@"http://lldb.llvm.org"].scheme',
78            VARIABLES_DISPLAYED_CORRECTLY,
79            substrs=["http"],
80        )
81        # Test that the NULL macro still works with a loaded module.
82        self.expect_expr("int *i = NULL; i == NULL", result_value="true")
83