1"""
2Test more expression command sequences with objective-c.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11@unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
12class FoundationTestCase2(TestBase):
13
14    mydir = os.path.join("lang", "objc", "foundation")
15
16    @dsym_test
17    def test_more_expr_commands_with_dsym(self):
18        """More expression commands for objective-c."""
19        self.buildDsym()
20        self.more_expr_objc()
21
22    @dwarf_test
23    def test_more_expr_commands_with_dwarf(self):
24        """More expression commands for objective-c."""
25        self.buildDwarf()
26        self.more_expr_objc()
27
28    @dsym_test
29    def test_NSArray_expr_commands_with_dsym(self):
30        """Test expression commands for NSArray."""
31        self.buildDsym()
32        self.NSArray_expr()
33
34    @dwarf_test
35    def test_NSArray_expr_commands_with_dwarf(self):
36        """Test expression commands for NSArray."""
37        self.buildDwarf()
38        self.NSArray_expr()
39
40    @dsym_test
41    def test_NSString_expr_commands_with_dsym(self):
42        """Test expression commands for NSString."""
43        self.buildDsym()
44        self.NSString_expr()
45
46    @dwarf_test
47    def test_NSString_expr_commands_with_dwarf(self):
48        """Test expression commands for NSString."""
49        self.buildDwarf()
50        self.NSString_expr()
51
52    @dsym_test
53    def test_MyString_dump_with_dsym(self):
54        """Test dump of a known Objective-C object by dereferencing it."""
55        self.buildDsym()
56        self.MyString_dump()
57
58    @dwarf_test
59    def test_MyString_dump_with_dwarf(self):
60        """Test dump of a known Objective-C object by dereferencing it."""
61        self.buildDwarf()
62        self.MyString_dump()
63
64    @expectedFailurei386
65    @dsym_test
66    def test_NSError_po_with_dsym(self):
67        """Test that po of the result of an unknown method doesn't require a cast."""
68        self.buildDsym()
69        self.NSError_po()
70
71    @expectedFailurei386
72    @dwarf_test
73    def test_NSError_po_with_dwarf(self):
74        """Test that po of the result of an unknown method doesn't require a cast."""
75        self.buildDsym()
76        self.NSError_po()
77
78    @dsym_test
79    def test_NSError_p_with_dsym(self):
80        """Test that p of the result of an unknown method does require a cast."""
81        self.buildDsym()
82        self.NSError_p()
83
84    @dwarf_test
85    def test_NSError_p_with_dwarf(self):
86        """Test that p of the result of an unknown method does require a cast."""
87        self.buildDsym()
88        self.NSError_p()
89
90    def setUp(self):
91        # Call super's setUp().
92        TestBase.setUp(self)
93        # Find the line numbers to break at.
94        self.lines = []
95        self.lines.append(line_number('main.m', '// Break here for selector: tests'))
96        self.lines.append(line_number('main.m', '// Break here for NSArray tests'))
97        self.lines.append(line_number('main.m', '// Break here for NSString tests'))
98        self.lines.append(line_number('main.m', '// Break here for description test'))
99        self.lines.append(line_number('main.m', '// Set break point at this line'))
100
101    def more_expr_objc(self):
102        """More expression commands for objective-c."""
103        exe = os.path.join(os.getcwd(), "a.out")
104        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
105
106        # Create a bunch of breakpoints.
107        for line in self.lines:
108            lldbutil.run_break_set_by_file_and_line (self, "main.m", line, num_expected_locations=1, loc_exact=True)
109
110        self.runCmd("run", RUN_SUCCEEDED)
111
112        # Test_Selector:
113        self.runCmd("thread backtrace")
114        self.expect("expression (char *)sel_getName(sel)",
115            substrs = ["(char *)",
116                       "length"])
117
118        self.runCmd("process continue")
119
120        # Test_NSArray:
121        self.runCmd("thread backtrace")
122        self.runCmd("process continue")
123
124        # Test_NSString:
125        self.runCmd("thread backtrace")
126        self.runCmd("process continue")
127
128        # Test_MyString:
129        self.runCmd("thread backtrace")
130        self.expect("expression (char *)sel_getName(_cmd)",
131            substrs = ["(char *)",
132                       "description"])
133
134        self.runCmd("process continue")
135
136    @unittest2.expectedFailure(8741897)
137    def NSArray_expr(self):
138        """Test expression commands for NSArray."""
139        exe = os.path.join(os.getcwd(), "a.out")
140        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
141
142        # Break inside Test_NSArray:
143        line = self.lines[1]
144        lldbutil.run_break_set_by_file_and_line (self, "main.m", line, num_expected_locations=1, loc_exact=True)
145
146        self.runCmd("run", RUN_SUCCEEDED)
147
148        # Test_NSArray:
149        self.runCmd("thread backtrace")
150        self.expect("expression (int)[nil_mutable_array count]",
151            patterns = ["\(int\) \$.* = 0"])
152        self.expect("expression (int)[array1 count]",
153            patterns = ["\(int\) \$.* = 3"])
154        self.expect("expression (int)[array2 count]",
155            patterns = ["\(int\) \$.* = 3"])
156        self.expect("expression (int)array1.count",
157            patterns = ["\(int\) \$.* = 3"])
158        self.expect("expression (int)array2.count",
159            patterns = ["\(int\) \$.* = 3"])
160        self.runCmd("process continue")
161
162    @unittest2.expectedFailure(8741897)
163    def NSString_expr(self):
164        """Test expression commands for NSString."""
165        exe = os.path.join(os.getcwd(), "a.out")
166        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
167
168        # Break inside Test_NSString:
169        line = self.lines[2]
170        lldbutil.run_break_set_by_file_and_line (self, "main.m", line, num_expected_locations=1, loc_exact=True)
171
172        self.runCmd("run", RUN_SUCCEEDED)
173
174        # Test_NSString:
175        self.runCmd("thread backtrace")
176        self.expect("expression (int)[str length]",
177            patterns = ["\(int\) \$.* ="])
178        self.expect("expression (int)[str_id length]",
179            patterns = ["\(int\) \$.* ="])
180        self.expect("expression [str description]",
181            patterns = ["\(id\) \$.* = 0x"])
182        self.expect("expression [str_id description]",
183            patterns = ["\(id\) \$.* = 0x"])
184        self.expect("expression str.description")
185        self.expect("expression str_id.description")
186        self.expect('expression str = @"new"')
187        self.expect('expression str = [NSString stringWithFormat: @"%cew", \'N\']')
188        self.runCmd("process continue")
189
190    def MyString_dump(self):
191        """Test dump of a known Objective-C object by dereferencing it."""
192        exe = os.path.join(os.getcwd(), "a.out")
193        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
194
195        line = self.lines[4]
196
197        lldbutil.run_break_set_by_file_and_line (self, "main.m", line, num_expected_locations=1, loc_exact=True)
198
199        self.runCmd("run", RUN_SUCCEEDED)
200
201        self.expect("expression --show-types -- *my",
202            patterns = ["\(MyString\) \$.* = ", "\(MyBase\)", "\(NSObject\)", "\(Class\)"])
203        self.runCmd("process continue")
204
205    def NSError_po(self):
206        """Test that po of the result of an unknown method doesn't require a cast."""
207        exe = os.path.join(os.getcwd(), "a.out")
208        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
209
210        line = self.lines[4]
211
212        lldbutil.run_break_set_by_file_and_line (self, "main.m", line, num_expected_locations=1, loc_exact=True)
213
214        self.runCmd("run", RUN_SUCCEEDED)
215
216        self.expect("po [NSError errorWithDomain:@\"Hello\" code:35 userInfo:nil]",
217            substrs = ["Error Domain=Hello", "Code=35", "be completed."])
218        self.runCmd("process continue")
219
220    def NSError_p(self):
221        """Test that p of the result of an unknown method does require a cast."""
222        exe = os.path.join(os.getcwd(), "a.out")
223        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
224
225        line = self.lines[4]
226
227        lldbutil.run_break_set_by_file_and_line (self, "main.m", line, num_expected_locations=1, loc_exact=True)
228
229        self.runCmd("run", RUN_SUCCEEDED)
230
231        self.expect("p [NSError thisMethodIsntImplemented:0]",
232                    error = True,
233                    patterns = ["no known method", "cast the message send to the method's return type"])
234        self.runCmd("process continue")
235
236if __name__ == '__main__':
237    import atexit
238    lldb.SBDebugger.Initialize()
239    atexit.register(lambda: lldb.SBDebugger.Terminate())
240    unittest2.main()
241