xref: /llvm-project/lldb/test/API/functionalities/data-formatter/poarray/TestPrintObjectArray.py (revision 44fc987ed174e32544a577387ab0df6886495e82)
1"""
2Test lldb data formatter subsystem.
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class PrintObjectArrayTestCase(TestBase):
13    @skipUnlessDarwin
14    def test_print_array(self):
15        """Test that expr -O -Z works"""
16        self.build()
17        self.printarray_data_formatter_commands()
18
19    @skipUnlessDarwin
20    def test_print_array_no_const(self):
21        """Test that expr -O -Z works"""
22        disable_constant_classes = {
23            "USE_SYSTEM_STDLIB": "1",  # See above.
24            "CFLAGS_EXTRAS": "-fno-constant-nsnumber-literals "
25            + "-fno-constant-nsarray-literals "
26            + "-fno-constant-nsdictionary-literals",
27        }
28        # FIXME: Remove compiler when flags are available upstream.
29        self.build(dictionary=disable_constant_classes, compiler="xcrun clang")
30        self.printarray_data_formatter_commands()
31
32    def setUp(self):
33        # Call super's setUp().
34        TestBase.setUp(self)
35        # Find the line number to break at.
36        self.line = line_number("main.mm", "break here")
37
38    def printarray_data_formatter_commands(self):
39        """Test that expr -O -Z works"""
40        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
41
42        lldbutil.run_break_set_by_file_and_line(
43            self, "main.mm", self.line, num_expected_locations=1, loc_exact=True
44        )
45
46        self.runCmd("run", RUN_SUCCEEDED)
47
48        # The stop reason of the thread should be breakpoint.
49        self.expect(
50            "thread list",
51            STOPPED_DUE_TO_BREAKPOINT,
52            substrs=["stopped", "stop reason = breakpoint"],
53        )
54
55        # This is the function to remove the custom formats in order to have a
56        # clean slate for the next test case.
57        def cleanup():
58            self.runCmd("type format clear", check=False)
59            self.runCmd("type summary clear", check=False)
60            self.runCmd("type synth clear", check=False)
61
62        # Execute the cleanup function during test case tear down.
63        self.addTearDownHook(cleanup)
64
65        self.expect(
66            "expr --element-count 3 --object-description -- objects",
67            substrs=[
68                "3735928559",
69                "4276993775",
70                "3203398366",
71                "Hello",
72                "World",
73                "Two =",
74                "1 =",
75            ],
76        )
77        self.expect(
78            "poarray 3 objects",
79            substrs=[
80                "3735928559",
81                "4276993775",
82                "3203398366",
83                "Hello",
84                "World",
85                "Two =",
86                "1 =",
87            ],
88        )
89        self.expect(
90            "expr --element-count 3 --object-description --description-verbosity=full -- objects",
91            substrs=[
92                "[0] =",
93                "3735928559",
94                "4276993775",
95                "3203398366",
96                "[1] =",
97                "Hello",
98                "World",
99                "[2] =",
100                "Two =",
101                "1 =",
102            ],
103        )
104        self.expect("parray 3 objects", substrs=["[0] = 0x", "[1] = 0x", "[2] = 0x"])
105        self.expect(
106            "expr --element-count 3 -d run -- objects",
107            substrs=["3 elements", "2 elements", "2 key/value pairs"],
108        )
109        self.expect(
110            "expr --element-count 3 -d run --ptr-depth=1 -- objects",
111            substrs=[
112                "3 elements",
113                "3735928559",
114                "4276993775",
115                "3203398366",
116                "2 elements",
117                '"Hello"',
118                '"World"',
119                "2 key/value pairs",
120            ],
121        )
122