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 NSDictionarySyntheticTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line number to break at.
21        self.line = line_number('main.m', '// Set break point at this line.')
22
23    @skipUnlessDarwin
24    def test_rdar11988289_with_run_command(self):
25        """Test that NSDictionary reports its synthetic children properly."""
26        self.build()
27        self.run_tests()
28
29    @skipUnlessDarwin
30    def test_rdar11988289_with_run_command_no_const(self):
31        """Test that NSDictionary reports its synthetic children properly."""
32        disable_constant_classes = {
33            'CC':
34            'xcrun clang',  # FIXME: Remove when flags are available upstream.
35            'CFLAGS_EXTRAS':
36            '-fno-constant-nsnumber-literals ' +
37            '-fno-constant-nsarray-literals ' +
38            '-fno-constant-nsdictionary-literals'
39        }
40        self.build(dictionary=disable_constant_classes)
41        self.run_tests()
42
43    def run_tests(self):
44        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
45
46        lldbutil.run_break_set_by_file_and_line(
47            self, "main.m", self.line, num_expected_locations=1, loc_exact=True)
48
49        self.runCmd("run", RUN_SUCCEEDED)
50
51        # The stop reason of the thread should be breakpoint.
52        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
53                    substrs=['stopped',
54                             'stop reason = breakpoint'])
55
56        # This is the function to remove the custom formats in order to have a
57        # clean slate for the next test case.
58        def cleanup():
59            self.runCmd('type format clear', check=False)
60            self.runCmd('type summary clear', check=False)
61            self.runCmd('type synth clear', check=False)
62
63        # Execute the cleanup function during test case tear down.
64        self.addTearDownHook(cleanup)
65
66        # Now check that we are displaying Cocoa classes correctly
67        self.expect('frame variable dictionary',
68                    substrs=['3 key/value pairs'])
69        self.expect('frame variable mutabledict',
70                    substrs=['4 key/value pairs'])
71        self.expect(
72            'frame variable dictionary --ptr-depth 1',
73            substrs=[
74                '3 key/value pairs',
75                '[0] = ',
76                'key = 0x',
77                'value = 0x',
78                '[1] = ',
79                '[2] = '])
80        self.expect(
81            'frame variable mutabledict --ptr-depth 1',
82            substrs=[
83                '4 key/value pairs',
84                '[0] = ',
85                'key = 0x',
86                'value = 0x',
87                '[1] = ',
88                '[2] = ',
89                '[3] = '])
90        self.expect(
91            'frame variable dictionary --ptr-depth 1 --dynamic-type no-run-target',
92            substrs=[
93                '3 key/value pairs',
94                '@"bar"',
95                '@"2 elements"',
96                '@"baz"',
97                '2 key/value pairs'])
98        self.expect(
99            'frame variable mutabledict --ptr-depth 1 --dynamic-type no-run-target',
100            substrs=[
101                '4 key/value pairs',
102                '(int)23',
103                '@"123"',
104                '@"http://www.apple.com"',
105                '@"sourceofstuff"',
106                '3 key/value pairs'])
107        self.expect(
108            'frame variable mutabledict --ptr-depth 2 --dynamic-type no-run-target',
109            substrs=[
110                '4 key/value pairs',
111                '(int)23',
112                '@"123"',
113                '@"http://www.apple.com"',
114                '@"sourceofstuff"',
115                '3 key/value pairs',
116                '@"bar"',
117                '@"2 elements"'])
118        self.expect(
119            'frame variable mutabledict --ptr-depth 3 --dynamic-type no-run-target',
120            substrs=[
121                '4 key/value pairs',
122                '(int)23',
123                '@"123"',
124                '@"http://www.apple.com"',
125                '@"sourceofstuff"',
126                '3 key/value pairs',
127                '@"bar"',
128                '@"2 elements"',
129                '(int)1',
130                '@"two"'])
131
132        self.assertTrue(
133            self.frame().FindVariable("dictionary").MightHaveChildren(),
134            "dictionary says it does not have children!")
135        self.assertTrue(
136            self.frame().FindVariable("mutabledict").MightHaveChildren(),
137            "mutable says it does not have children!")
138