1"""
2Test lldb data formatter subsystem.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class PrintObjectArrayTestCase(TestBase):
14
15    @skipUnlessDarwin
16    def test_print_array(self):
17        """Test that expr -O -Z works"""
18        self.build()
19        self.printarray_data_formatter_commands()
20
21    @skipUnlessDarwin
22    def test_print_array_no_const(self):
23        """Test that expr -O -Z works"""
24        disable_constant_classes = {
25            'CC':
26            'xcrun clang',  # FIXME: Remove when flags are available upstream.
27            'CFLAGS_EXTRAS':
28            '-fno-constant-nsnumber-literals ' +
29            '-fno-constant-nsarray-literals ' +
30            '-fno-constant-nsdictionary-literals'
31        }
32        self.build(dictionary=disable_constant_classes)
33        self.printarray_data_formatter_commands()
34
35    def setUp(self):
36        # Call super's setUp().
37        TestBase.setUp(self)
38        # Find the line number to break at.
39        self.line = line_number('main.mm', 'break here')
40
41    def printarray_data_formatter_commands(self):
42        """Test that expr -O -Z works"""
43        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
44
45        lldbutil.run_break_set_by_file_and_line(
46            self, "main.mm", self.line, num_expected_locations=1, loc_exact=True)
47
48        self.runCmd("run", RUN_SUCCEEDED)
49
50        # The stop reason of the thread should be breakpoint.
51        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
52                    substrs=['stopped',
53                             'stop reason = breakpoint'])
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        self.expect(
76            'poarray 3 objects',
77            substrs=[
78                '3735928559',
79                '4276993775',
80                '3203398366',
81                'Hello',
82                'World',
83                'Two =',
84                '1 ='])
85        self.expect(
86            'expr --element-count 3 --object-description --description-verbosity=full -- objects',
87            substrs=[
88                '[0] =',
89                '3735928559',
90                '4276993775',
91                '3203398366',
92                '[1] =',
93                'Hello',
94                'World',
95                '[2] =',
96                'Two =',
97                '1 ='])
98        self.expect(
99            'parray 3 objects',
100            substrs=[
101                '[0] = 0x',
102                '[1] = 0x',
103                '[2] = 0x'])
104        self.expect(
105            'expr --element-count 3 -d run -- objects',
106            substrs=[
107                '3 elements',
108                '2 elements',
109                '2 key/value pairs'])
110        self.expect(
111            'expr --element-count 3 -d run --ptr-depth=1 -- objects',
112            substrs=[
113                '3 elements',
114                '3735928559',
115                '4276993775',
116                '3203398366',
117                '2 elements',
118                '"Hello"',
119                '"World"',
120                '2 key/value pairs',
121            ])
122