xref: /llvm-project/lldb/test/API/functionalities/data-formatter/nsarraysynth/TestNSArraySynthetic.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
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 NSArraySyntheticTestCase(TestBase):
13    def setUp(self):
14        # Call super's setUp().
15        TestBase.setUp(self)
16        # Find the line number to break at.
17        self.line = line_number("main.m", "// Set break point at this line.")
18
19    @skipUnlessDarwin
20    def test_rdar11086338_with_run_command(self):
21        """Test that NSArray reports its synthetic children properly."""
22        self.build()
23        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
24
25        lldbutil.run_break_set_by_file_and_line(
26            self, "main.m", self.line, num_expected_locations=1, loc_exact=True
27        )
28
29        self.runCmd("run", RUN_SUCCEEDED)
30
31        # The stop reason of the thread should be breakpoint.
32        self.expect(
33            "thread list",
34            STOPPED_DUE_TO_BREAKPOINT,
35            substrs=["stopped", "stop reason = breakpoint"],
36        )
37
38        # This is the function to remove the custom formats in order to have a
39        # clean slate for the next test case.
40        def cleanup():
41            self.runCmd("type format clear", check=False)
42            self.runCmd("type summary clear", check=False)
43            self.runCmd("type synth clear", check=False)
44
45        # Execute the cleanup function during test case tear down.
46        self.addTearDownHook(cleanup)
47
48        # Now check that we are displaying Cocoa classes correctly
49        self.expect("frame variable arr", substrs=['@"6 elements"'])
50        self.expect("frame variable other_arr", substrs=['@"4 elements"'])
51        self.expect("frame variable empty_arr", substrs=['@"0 elements"'])
52        self.expect(
53            "frame variable arr --ptr-depth 1",
54            substrs=[
55                '@"6 elements"',
56                "[0] = 0x",
57                "[1] = 0x",
58                "[2] = 0x",
59                "[3] = 0x",
60                "[4] = 0x",
61                "[5] = 0x",
62            ],
63        )
64        self.expect(
65            "frame variable other_arr --ptr-depth 1",
66            substrs=['@"4 elements"', "[0] = 0x", "[1] = 0x", "[2] = 0x", "[3] = 0x"],
67        )
68        self.expect("frame variable empty_arr --ptr-depth 1", substrs=['@"0 elements"'])
69        self.expect(
70            "frame variable arr --ptr-depth 1",
71            substrs=[
72                '@"6 elements"',
73                '@"hello"',
74                '@"world"',
75                '@"this"',
76                '@"is"',
77                '@"me"',
78                '@"http://www.apple.com',
79            ],
80        )
81        self.expect(
82            "frame variable other_arr --ptr-depth 1",
83            substrs=['@"4 elements"', "(int)5", '@"a string"', '@"6 elements"'],
84        )
85        self.expect(
86            "frame variable other_arr --ptr-depth 2",
87            substrs=[
88                '@"4 elements"',
89                '@"6 elements" {',
90                '@"hello"',
91                '@"world"',
92                '@"this"',
93                '@"is"',
94                '@"me"',
95                '@"http://www.apple.com',
96            ],
97        )
98
99        self.assertTrue(
100            self.frame().FindVariable("arr").MightHaveChildren(),
101            "arr says it does not have children!",
102        )
103        self.assertTrue(
104            self.frame().FindVariable("other_arr").MightHaveChildren(),
105            "arr says it does not have children!",
106        )
107        self.assertFalse(
108            self.frame().FindVariable("empty_arr").MightHaveChildren(),
109            "arr says it does have children!",
110        )
111