OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'package:analyzer/dart/ast/ast.dart'; | 5 import 'package:analyzer/dart/ast/ast.dart'; |
6 import 'package:analyzer/dart/ast/token.dart'; | 6 import 'package:analyzer/dart/ast/token.dart'; |
7 import 'package:analyzer/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
8 import 'package:analyzer/file_system/memory_file_system.dart'; | 8 import 'package:analyzer/file_system/memory_file_system.dart'; |
9 import 'package:analyzer/src/dart/sdk/patch.dart'; | 9 import 'package:analyzer/src/dart/sdk/patch.dart'; |
10 import 'package:analyzer/src/dart/sdk/sdk.dart'; | 10 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 SdkPatcher patcher = new SdkPatcher(); | 28 SdkPatcher patcher = new SdkPatcher(); |
29 RecordingErrorListener listener = new RecordingErrorListener(); | 29 RecordingErrorListener listener = new RecordingErrorListener(); |
30 | 30 |
31 void setUp() { | 31 void setUp() { |
32 sdkFolder = provider.getFolder(_p('/sdk')); | 32 sdkFolder = provider.getFolder(_p('/sdk')); |
33 } | 33 } |
34 | 34 |
35 test_class_constructor_append_fail_notPrivate_named() { | 35 test_class_constructor_append_fail_notPrivate_named() { |
36 expect(() { | 36 expect(() { |
37 _doTopLevelPatching( | 37 _doTopLevelPatching(r''' |
38 r''' | |
39 class C {} | 38 class C {} |
40 ''', | 39 ''', r''' |
41 r''' | |
42 @patch | 40 @patch |
43 class C { | 41 class C { |
44 C.named() {} | 42 C.named() {} |
45 } | 43 } |
46 '''); | 44 '''); |
47 }, throwsArgumentError); | 45 }, throwsArgumentError); |
48 } | 46 } |
49 | 47 |
50 test_class_constructor_append_fail_notPrivate_unnamed() { | 48 test_class_constructor_append_fail_notPrivate_unnamed() { |
51 expect(() { | 49 expect(() { |
52 _doTopLevelPatching( | 50 _doTopLevelPatching(r''' |
53 r''' | |
54 class C {} | 51 class C {} |
55 ''', | 52 ''', r''' |
56 r''' | |
57 @patch | 53 @patch |
58 class C { | 54 class C { |
59 C() {} | 55 C() {} |
60 } | 56 } |
61 '''); | 57 '''); |
62 }, throwsArgumentError); | 58 }, throwsArgumentError); |
63 } | 59 } |
64 | 60 |
65 test_class_constructor_append_named() { | 61 test_class_constructor_append_named() { |
66 CompilationUnit unit = _doTopLevelPatching( | 62 CompilationUnit unit = _doTopLevelPatching(r''' |
67 r''' | |
68 class C { | 63 class C { |
69 } | 64 } |
70 ''', | 65 ''', r''' |
71 r''' | |
72 @patch | 66 @patch |
73 class C { | 67 class C { |
74 C._named() {} | 68 C._named() {} |
75 } | 69 } |
76 '''); | 70 '''); |
77 _assertUnitCode(unit, 'class C {C._named() {}}'); | 71 _assertUnitCode(unit, 'class C {C._named() {}}'); |
78 ClassDeclaration clazz = unit.declarations[0]; | 72 ClassDeclaration clazz = unit.declarations[0]; |
79 ConstructorDeclaration constructor = clazz.members[0]; | 73 ConstructorDeclaration constructor = clazz.members[0]; |
80 _assertPrevNextToken(clazz.leftBracket, constructor.beginToken); | 74 _assertPrevNextToken(clazz.leftBracket, constructor.beginToken); |
81 _assertPrevNextToken(constructor.endToken, clazz.rightBracket); | 75 _assertPrevNextToken(constructor.endToken, clazz.rightBracket); |
82 } | 76 } |
83 | 77 |
84 test_class_constructor_append_unnamed() { | 78 test_class_constructor_append_unnamed() { |
85 CompilationUnit unit = _doTopLevelPatching( | 79 CompilationUnit unit = _doTopLevelPatching(r''' |
86 r''' | |
87 class _C { | 80 class _C { |
88 } | 81 } |
89 ''', | 82 ''', r''' |
90 r''' | |
91 @patch | 83 @patch |
92 class _C { | 84 class _C { |
93 _C() {} | 85 _C() {} |
94 } | 86 } |
95 '''); | 87 '''); |
96 _assertUnitCode(unit, 'class _C {_C() {}}'); | 88 _assertUnitCode(unit, 'class _C {_C() {}}'); |
97 ClassDeclaration clazz = unit.declarations[0]; | 89 ClassDeclaration clazz = unit.declarations[0]; |
98 ConstructorDeclaration constructor = clazz.members[0]; | 90 ConstructorDeclaration constructor = clazz.members[0]; |
99 _assertPrevNextToken(clazz.leftBracket, constructor.beginToken); | 91 _assertPrevNextToken(clazz.leftBracket, constructor.beginToken); |
100 _assertPrevNextToken(constructor.endToken, clazz.rightBracket); | 92 _assertPrevNextToken(constructor.endToken, clazz.rightBracket); |
101 } | 93 } |
102 | 94 |
103 test_class_constructor_patch() { | 95 test_class_constructor_patch() { |
104 CompilationUnit unit = _doTopLevelPatching( | 96 CompilationUnit unit = _doTopLevelPatching(r''' |
105 r''' | |
106 class C { | 97 class C { |
107 external C.named(); | 98 external C.named(); |
108 } | 99 } |
109 ''', | 100 ''', r''' |
110 r''' | |
111 @patch | 101 @patch |
112 class C { | 102 class C { |
113 @patch | 103 @patch |
114 C.named() { | 104 C.named() { |
115 print(42); | 105 print(42); |
116 } | 106 } |
117 } | 107 } |
118 '''); | 108 '''); |
119 _assertUnitCode(unit, 'class C {C.named() {print(42);}}'); | 109 _assertUnitCode(unit, 'class C {C.named() {print(42);}}'); |
120 ClassDeclaration clazz = unit.declarations[0]; | 110 ClassDeclaration clazz = unit.declarations[0]; |
121 ConstructorDeclaration constructor = clazz.members[0]; | 111 ConstructorDeclaration constructor = clazz.members[0]; |
122 expect(constructor.externalKeyword, isNull); | 112 expect(constructor.externalKeyword, isNull); |
123 _assertPrevNextToken( | 113 _assertPrevNextToken( |
124 constructor.parameters.endToken, constructor.body.beginToken); | 114 constructor.parameters.endToken, constructor.body.beginToken); |
125 _assertPrevNextToken(constructor.endToken, clazz.rightBracket); | 115 _assertPrevNextToken(constructor.endToken, clazz.rightBracket); |
126 } | 116 } |
127 | 117 |
128 test_class_constructor_patch_fail_baseFactory_patchGenerative() { | 118 test_class_constructor_patch_fail_baseFactory_patchGenerative() { |
129 expect(() { | 119 expect(() { |
130 _doTopLevelPatching( | 120 _doTopLevelPatching(r''' |
131 r''' | |
132 class C { | 121 class C { |
133 external factory C.named(); | 122 external factory C.named(); |
134 } | 123 } |
135 ''', | 124 ''', r''' |
136 r''' | |
137 @patch | 125 @patch |
138 class C { | 126 class C { |
139 @patch | 127 @patch |
140 C.named() {} | 128 C.named() {} |
141 } | 129 } |
142 '''); | 130 '''); |
143 }, throwsArgumentError); | 131 }, throwsArgumentError); |
144 } | 132 } |
145 | 133 |
146 test_class_constructor_patch_fail_baseGenerative_patchFactory() { | 134 test_class_constructor_patch_fail_baseGenerative_patchFactory() { |
147 expect(() { | 135 expect(() { |
148 _doTopLevelPatching( | 136 _doTopLevelPatching(r''' |
149 r''' | |
150 class C { | 137 class C { |
151 external C.named(); | 138 external C.named(); |
152 } | 139 } |
153 ''', | 140 ''', r''' |
154 r''' | |
155 @patch | 141 @patch |
156 class C { | 142 class C { |
157 @patch | 143 @patch |
158 factory C.named() {} | 144 factory C.named() {} |
159 } | 145 } |
160 '''); | 146 '''); |
161 }, throwsArgumentError); | 147 }, throwsArgumentError); |
162 } | 148 } |
163 | 149 |
164 test_class_constructor_patch_fail_fieldFormalParam_inBase() { | 150 test_class_constructor_patch_fail_fieldFormalParam_inBase() { |
165 expect(() { | 151 expect(() { |
166 _doTopLevelPatching( | 152 _doTopLevelPatching(r''' |
167 r''' | |
168 class C { | 153 class C { |
169 int f; | 154 int f; |
170 external C.named(this.f); | 155 external C.named(this.f); |
171 } | 156 } |
172 ''', | 157 ''', r''' |
173 r''' | |
174 @patch | 158 @patch |
175 class C { | 159 class C { |
176 @patch | 160 @patch |
177 C.named() : f = 2 {} | 161 C.named() : f = 2 {} |
178 } | 162 } |
179 '''); | 163 '''); |
180 }, throwsArgumentError); | 164 }, throwsArgumentError); |
181 } | 165 } |
182 | 166 |
183 test_class_constructor_patch_fail_fieldFormalParam_inPatch() { | 167 test_class_constructor_patch_fail_fieldFormalParam_inPatch() { |
184 expect(() { | 168 expect(() { |
185 _doTopLevelPatching( | 169 _doTopLevelPatching(r''' |
186 r''' | |
187 class C { | 170 class C { |
188 int f; | 171 int f; |
189 external C.named(int f); | 172 external C.named(int f); |
190 } | 173 } |
191 ''', | 174 ''', r''' |
192 r''' | |
193 @patch | 175 @patch |
194 class C { | 176 class C { |
195 @patch | 177 @patch |
196 C.named(this.f) {} | 178 C.named(this.f) {} |
197 } | 179 } |
198 '''); | 180 '''); |
199 }, throwsArgumentError); | 181 }, throwsArgumentError); |
200 } | 182 } |
201 | 183 |
202 test_class_constructor_patch_fail_fieldFormalParam_inPatchAndBase() { | 184 test_class_constructor_patch_fail_fieldFormalParam_inPatchAndBase() { |
203 expect(() { | 185 expect(() { |
204 _doTopLevelPatching( | 186 _doTopLevelPatching(r''' |
205 r''' | |
206 class C { | 187 class C { |
207 int f; | 188 int f; |
208 external C.named(this.f); | 189 external C.named(this.f); |
209 } | 190 } |
210 ''', | 191 ''', r''' |
211 r''' | |
212 @patch | 192 @patch |
213 class C { | 193 class C { |
214 @patch | 194 @patch |
215 C.named(this.f) {} | 195 C.named(this.f) {} |
216 } | 196 } |
217 '''); | 197 '''); |
218 }, throwsArgumentError); | 198 }, throwsArgumentError); |
219 } | 199 } |
220 | 200 |
221 test_class_constructor_patch_fail_hasInitializers() { | 201 test_class_constructor_patch_fail_hasInitializers() { |
222 expect(() { | 202 expect(() { |
223 _doTopLevelPatching( | 203 _doTopLevelPatching(r''' |
224 r''' | |
225 class C { | 204 class C { |
226 int f; | 205 int f; |
227 external C.named() : f = 1; | 206 external C.named() : f = 1; |
228 } | 207 } |
229 ''', | 208 ''', r''' |
230 r''' | |
231 @patch | 209 @patch |
232 class C { | 210 class C { |
233 @patch | 211 @patch |
234 C.named() : f = 2 {} | 212 C.named() : f = 2 {} |
235 } | 213 } |
236 '''); | 214 '''); |
237 }, throwsArgumentError); | 215 }, throwsArgumentError); |
238 } | 216 } |
239 | 217 |
240 test_class_constructor_patch_fail_noExternalKeyword() { | 218 test_class_constructor_patch_fail_noExternalKeyword() { |
241 expect(() { | 219 expect(() { |
242 _doTopLevelPatching( | 220 _doTopLevelPatching(r''' |
243 r''' | |
244 class C { | 221 class C { |
245 C.named(); | 222 C.named(); |
246 } | 223 } |
247 ''', | 224 ''', r''' |
248 r''' | |
249 @patch | 225 @patch |
250 class C { | 226 class C { |
251 @patch | 227 @patch |
252 C.named() {} | 228 C.named() {} |
253 } | 229 } |
254 '''); | 230 '''); |
255 }, throwsArgumentError); | 231 }, throwsArgumentError); |
256 } | 232 } |
257 | 233 |
258 test_class_constructor_patch_fail_signatureChange() { | 234 test_class_constructor_patch_fail_signatureChange() { |
259 expect(() { | 235 expect(() { |
260 _doTopLevelPatching( | 236 _doTopLevelPatching(r''' |
261 r''' | |
262 class C { | 237 class C { |
263 external C.named(int x); | 238 external C.named(int x); |
264 } | 239 } |
265 ''', | 240 ''', r''' |
266 r''' | |
267 @patch | 241 @patch |
268 class C { | 242 class C { |
269 @patch | 243 @patch |
270 C.named(double x) {} | 244 C.named(double x) {} |
271 } | 245 } |
272 '''); | 246 '''); |
273 }, throwsArgumentError); | 247 }, throwsArgumentError); |
274 } | 248 } |
275 | 249 |
276 test_class_constructor_patch_fail_signatureChange_nameOnly() { | 250 test_class_constructor_patch_fail_signatureChange_nameOnly() { |
277 expect(() { | 251 expect(() { |
278 _doTopLevelPatching( | 252 _doTopLevelPatching(r''' |
279 r''' | |
280 class C { | 253 class C { |
281 external C.named(int x); | 254 external C.named(int x); |
282 } | 255 } |
283 ''', | 256 ''', r''' |
284 r''' | |
285 @patch | 257 @patch |
286 class C { | 258 class C { |
287 @patch | 259 @patch |
288 C.named(int y) {} | 260 C.named(int y) {} |
289 } | 261 } |
290 '''); | 262 '''); |
291 }, throwsArgumentError); | 263 }, throwsArgumentError); |
292 } | 264 } |
293 | 265 |
294 test_class_constructor_patch_initializers() { | 266 test_class_constructor_patch_initializers() { |
295 CompilationUnit unit = _doTopLevelPatching( | 267 CompilationUnit unit = _doTopLevelPatching(r''' |
296 r''' | |
297 class C { | 268 class C { |
298 int f; | 269 int f; |
299 external C.named(); | 270 external C.named(); |
300 } | 271 } |
301 ''', | 272 ''', r''' |
302 r''' | |
303 @patch | 273 @patch |
304 class C { | 274 class C { |
305 @patch | 275 @patch |
306 C.named() : f = 2 { | 276 C.named() : f = 2 { |
307 print(42); | 277 print(42); |
308 } | 278 } |
309 } | 279 } |
310 '''); | 280 '''); |
311 _assertUnitCode(unit, 'class C {int f; C.named() : f = 2 {print(42);}}'); | 281 _assertUnitCode(unit, 'class C {int f; C.named() : f = 2 {print(42);}}'); |
312 ClassDeclaration clazz = unit.declarations[0]; | 282 ClassDeclaration clazz = unit.declarations[0]; |
313 ConstructorDeclaration constructor = clazz.members[1]; | 283 ConstructorDeclaration constructor = clazz.members[1]; |
314 expect(constructor.externalKeyword, isNull); | 284 expect(constructor.externalKeyword, isNull); |
315 _assertPrevNextToken(constructor.parameters.endToken, | 285 _assertPrevNextToken(constructor.parameters.endToken, |
316 constructor.initializers.beginToken.previous); | 286 constructor.initializers.beginToken.previous); |
317 _assertPrevNextToken(constructor.endToken, clazz.rightBracket); | 287 _assertPrevNextToken(constructor.endToken, clazz.rightBracket); |
318 } | 288 } |
319 | 289 |
320 test_class_field_append() { | 290 test_class_field_append() { |
321 CompilationUnit unit = _doTopLevelPatching( | 291 CompilationUnit unit = _doTopLevelPatching(r''' |
322 r''' | |
323 class C { | 292 class C { |
324 void a() {} | 293 void a() {} |
325 } | 294 } |
326 ''', | 295 ''', r''' |
327 r''' | |
328 @patch | 296 @patch |
329 class C { | 297 class C { |
330 int _b = 42; | 298 int _b = 42; |
331 } | 299 } |
332 '''); | 300 '''); |
333 _assertUnitCode(unit, 'class C {void a() {} int _b = 42;}'); | 301 _assertUnitCode(unit, 'class C {void a() {} int _b = 42;}'); |
334 ClassDeclaration clazz = unit.declarations[0]; | 302 ClassDeclaration clazz = unit.declarations[0]; |
335 MethodDeclaration a = clazz.members[0]; | 303 MethodDeclaration a = clazz.members[0]; |
336 FieldDeclaration b = clazz.members[1]; | 304 FieldDeclaration b = clazz.members[1]; |
337 _assertPrevNextToken(a.endToken, b.beginToken); | 305 _assertPrevNextToken(a.endToken, b.beginToken); |
338 _assertPrevNextToken(b.endToken, clazz.rightBracket); | 306 _assertPrevNextToken(b.endToken, clazz.rightBracket); |
339 } | 307 } |
340 | 308 |
341 test_class_field_append_fail_moreThanOne() { | 309 test_class_field_append_fail_moreThanOne() { |
342 expect(() { | 310 expect(() { |
343 _doTopLevelPatching( | 311 _doTopLevelPatching(r''' |
344 r''' | |
345 class A {} | 312 class A {} |
346 ''', | 313 ''', r''' |
347 r''' | |
348 @patch | 314 @patch |
349 class A { | 315 class A { |
350 @patch | 316 @patch |
351 int _f1, _f2; | 317 int _f1, _f2; |
352 } | 318 } |
353 '''); | 319 '''); |
354 }, throwsArgumentError); | 320 }, throwsArgumentError); |
355 } | 321 } |
356 | 322 |
357 test_class_field_append_fail_notPrivate() { | 323 test_class_field_append_fail_notPrivate() { |
358 expect(() { | 324 expect(() { |
359 _doTopLevelPatching( | 325 _doTopLevelPatching(r''' |
360 r''' | |
361 class A {} | 326 class A {} |
362 ''', | 327 ''', r''' |
363 r''' | |
364 @patch | 328 @patch |
365 class A { | 329 class A { |
366 @patch | 330 @patch |
367 int b; | 331 int b; |
368 } | 332 } |
369 '''); | 333 '''); |
370 }, throwsArgumentError); | 334 }, throwsArgumentError); |
371 } | 335 } |
372 | 336 |
373 test_class_field_append_publicInPrivateClass() { | 337 test_class_field_append_publicInPrivateClass() { |
374 CompilationUnit unit = _doTopLevelPatching( | 338 CompilationUnit unit = _doTopLevelPatching(r''' |
375 r''' | |
376 class _C { | 339 class _C { |
377 void a() {} | 340 void a() {} |
378 } | 341 } |
379 ''', | 342 ''', r''' |
380 r''' | |
381 @patch | 343 @patch |
382 class _C { | 344 class _C { |
383 int b = 42; | 345 int b = 42; |
384 } | 346 } |
385 '''); | 347 '''); |
386 _assertUnitCode(unit, 'class _C {void a() {} int b = 42;}'); | 348 _assertUnitCode(unit, 'class _C {void a() {} int b = 42;}'); |
387 ClassDeclaration clazz = unit.declarations[0]; | 349 ClassDeclaration clazz = unit.declarations[0]; |
388 MethodDeclaration a = clazz.members[0]; | 350 MethodDeclaration a = clazz.members[0]; |
389 FieldDeclaration b = clazz.members[1]; | 351 FieldDeclaration b = clazz.members[1]; |
390 _assertPrevNextToken(a.endToken, b.beginToken); | 352 _assertPrevNextToken(a.endToken, b.beginToken); |
391 _assertPrevNextToken(b.endToken, clazz.rightBracket); | 353 _assertPrevNextToken(b.endToken, clazz.rightBracket); |
392 } | 354 } |
393 | 355 |
394 test_class_field_patch_fail() { | 356 test_class_field_patch_fail() { |
395 expect(() { | 357 expect(() { |
396 _doTopLevelPatching( | 358 _doTopLevelPatching(r''' |
397 r''' | |
398 class A {} | 359 class A {} |
399 ''', | 360 ''', r''' |
400 r''' | |
401 @patch | 361 @patch |
402 class A { | 362 class A { |
403 @patch | 363 @patch |
404 int _f; | 364 int _f; |
405 } | 365 } |
406 '''); | 366 '''); |
407 }, throwsArgumentError); | 367 }, throwsArgumentError); |
408 } | 368 } |
409 | 369 |
410 test_class_getter_append() { | 370 test_class_getter_append() { |
411 CompilationUnit unit = _doTopLevelPatching( | 371 CompilationUnit unit = _doTopLevelPatching(r''' |
412 r''' | |
413 class C { | 372 class C { |
414 void a() {} | 373 void a() {} |
415 } | 374 } |
416 ''', | 375 ''', r''' |
417 r''' | |
418 @patch | 376 @patch |
419 class C { | 377 class C { |
420 int get _b => 2; | 378 int get _b => 2; |
421 } | 379 } |
422 '''); | 380 '''); |
423 _assertUnitCode(unit, 'class C {void a() {} int get _b => 2;}'); | 381 _assertUnitCode(unit, 'class C {void a() {} int get _b => 2;}'); |
424 } | 382 } |
425 | 383 |
426 test_class_method_append() { | 384 test_class_method_append() { |
427 CompilationUnit unit = _doTopLevelPatching( | 385 CompilationUnit unit = _doTopLevelPatching(r''' |
428 r''' | |
429 class C { | 386 class C { |
430 void a() {} | 387 void a() {} |
431 } | 388 } |
432 ''', | 389 ''', r''' |
433 r''' | |
434 @patch | 390 @patch |
435 class C { | 391 class C { |
436 void _b() {} | 392 void _b() {} |
437 void _c() {} | 393 void _c() {} |
438 } | 394 } |
439 '''); | 395 '''); |
440 _assertUnitCode(unit, 'class C {void a() {} void _b() {} void _c() {}}'); | 396 _assertUnitCode(unit, 'class C {void a() {} void _b() {} void _c() {}}'); |
441 ClassDeclaration clazz = unit.declarations[0]; | 397 ClassDeclaration clazz = unit.declarations[0]; |
442 MethodDeclaration a = clazz.members[0]; | 398 MethodDeclaration a = clazz.members[0]; |
443 MethodDeclaration b = clazz.members[1]; | 399 MethodDeclaration b = clazz.members[1]; |
444 MethodDeclaration c = clazz.members[2]; | 400 MethodDeclaration c = clazz.members[2]; |
445 _assertPrevNextToken(a.endToken, b.beginToken); | 401 _assertPrevNextToken(a.endToken, b.beginToken); |
446 _assertPrevNextToken(b.endToken, c.beginToken); | 402 _assertPrevNextToken(b.endToken, c.beginToken); |
447 _assertPrevNextToken(c.endToken, clazz.rightBracket); | 403 _assertPrevNextToken(c.endToken, clazz.rightBracket); |
448 } | 404 } |
449 | 405 |
450 test_class_method_fail_notPrivate() { | 406 test_class_method_fail_notPrivate() { |
451 expect(() { | 407 expect(() { |
452 _doTopLevelPatching( | 408 _doTopLevelPatching(r''' |
453 r''' | |
454 class A {} | 409 class A {} |
455 ''', | 410 ''', r''' |
456 r''' | |
457 @patch | 411 @patch |
458 class A { | 412 class A { |
459 void m() {} | 413 void m() {} |
460 } | 414 } |
461 '''); | 415 '''); |
462 }, throwsArgumentError); | 416 }, throwsArgumentError); |
463 } | 417 } |
464 | 418 |
465 test_class_method_patch() { | 419 test_class_method_patch() { |
466 CompilationUnit unit = _doTopLevelPatching( | 420 CompilationUnit unit = _doTopLevelPatching(r''' |
467 r''' | |
468 class C { | 421 class C { |
469 external int m(); | 422 external int m(); |
470 } | 423 } |
471 ''', | 424 ''', r''' |
472 r''' | |
473 @patch | 425 @patch |
474 class C { | 426 class C { |
475 @patch | 427 @patch |
476 int m() => 42; | 428 int m() => 42; |
477 } | 429 } |
478 '''); | 430 '''); |
479 _assertUnitCode(unit, 'class C {int m() => 42;}'); | 431 _assertUnitCode(unit, 'class C {int m() => 42;}'); |
480 ClassDeclaration clazz = unit.declarations[0]; | 432 ClassDeclaration clazz = unit.declarations[0]; |
481 MethodDeclaration m = clazz.members[0]; | 433 MethodDeclaration m = clazz.members[0]; |
482 expect(m.externalKeyword, isNull); | 434 expect(m.externalKeyword, isNull); |
483 _assertPrevNextToken(m.parameters.rightParenthesis, m.body.beginToken); | 435 _assertPrevNextToken(m.parameters.rightParenthesis, m.body.beginToken); |
484 _assertPrevNextToken(m.body.endToken, clazz.rightBracket); | 436 _assertPrevNextToken(m.body.endToken, clazz.rightBracket); |
485 } | 437 } |
486 | 438 |
487 test_class_method_patch_fail_noExternalKeyword() { | 439 test_class_method_patch_fail_noExternalKeyword() { |
488 expect(() { | 440 expect(() { |
489 _doTopLevelPatching( | 441 _doTopLevelPatching(r''' |
490 r''' | |
491 class C { | 442 class C { |
492 int m(); | 443 int m(); |
493 } | 444 } |
494 ''', | 445 ''', r''' |
495 r''' | |
496 @patch | 446 @patch |
497 class C { | 447 class C { |
498 @patch | 448 @patch |
499 int m() => 42; | 449 int m() => 42; |
500 } | 450 } |
501 '''); | 451 '''); |
502 }, throwsArgumentError); | 452 }, throwsArgumentError); |
503 } | 453 } |
504 | 454 |
505 test_class_method_patch_fail_signatureChange() { | 455 test_class_method_patch_fail_signatureChange() { |
506 expect(() { | 456 expect(() { |
507 _doTopLevelPatching( | 457 _doTopLevelPatching(r''' |
508 r''' | |
509 class C { | 458 class C { |
510 external void f(int x); | 459 external void f(int x); |
511 } | 460 } |
512 ''', | 461 ''', r''' |
513 r''' | |
514 @patch | 462 @patch |
515 class C { | 463 class C { |
516 @patch | 464 @patch |
517 void f(double x) {} | 465 void f(double x) {} |
518 } | 466 } |
519 '''); | 467 '''); |
520 }, throwsArgumentError); | 468 }, throwsArgumentError); |
521 } | 469 } |
522 | 470 |
523 test_class_method_patch_fail_signatureChange_extraArgument() { | 471 test_class_method_patch_fail_signatureChange_extraArgument() { |
524 expect(() { | 472 expect(() { |
525 _doTopLevelPatching( | 473 _doTopLevelPatching(r''' |
526 r''' | |
527 class C { | 474 class C { |
528 external void f(); | 475 external void f(); |
529 } | 476 } |
530 ''', | 477 ''', r''' |
531 r''' | |
532 @patch | 478 @patch |
533 class C { | 479 class C { |
534 @patch | 480 @patch |
535 void f(int x) {} | 481 void f(int x) {} |
536 } | 482 } |
537 '''); | 483 '''); |
538 }, throwsArgumentError); | 484 }, throwsArgumentError); |
539 } | 485 } |
540 | 486 |
541 test_class_method_patch_fail_signatureChange_extraTypeTokens() { | 487 test_class_method_patch_fail_signatureChange_extraTypeTokens() { |
542 expect(() { | 488 expect(() { |
543 _doTopLevelPatching( | 489 _doTopLevelPatching(r''' |
544 r''' | |
545 class C { | 490 class C { |
546 external List f(); | 491 external List f(); |
547 } | 492 } |
548 ''', | 493 ''', r''' |
549 r''' | |
550 @patch | 494 @patch |
551 class C { | 495 class C { |
552 @patch | 496 @patch |
553 List<int> f() => null; | 497 List<int> f() => null; |
554 } | 498 } |
555 '''); | 499 '''); |
556 }, throwsArgumentError); | 500 }, throwsArgumentError); |
557 } | 501 } |
558 | 502 |
559 test_class_method_patch_fail_signatureChange_functionTypedParam_paramType() { | 503 test_class_method_patch_fail_signatureChange_functionTypedParam_paramType() { |
560 expect(() { | 504 expect(() { |
561 _doTopLevelPatching( | 505 _doTopLevelPatching(r''' |
562 r''' | |
563 class C { | 506 class C { |
564 external void f(void x(int y)); | 507 external void f(void x(int y)); |
565 } | 508 } |
566 ''', | 509 ''', r''' |
567 r''' | |
568 @patch | 510 @patch |
569 class C { | 511 class C { |
570 @patch | 512 @patch |
571 void f(void x(double y)) {} | 513 void f(void x(double y)) {} |
572 } | 514 } |
573 '''); | 515 '''); |
574 }, throwsArgumentError); | 516 }, throwsArgumentError); |
575 } | 517 } |
576 | 518 |
577 test_class_method_patch_fail_signatureChange_functionTypedParam_returnType() { | 519 test_class_method_patch_fail_signatureChange_functionTypedParam_returnType() { |
578 expect(() { | 520 expect(() { |
579 _doTopLevelPatching( | 521 _doTopLevelPatching(r''' |
580 r''' | |
581 class C { | 522 class C { |
582 external void f(int x()); | 523 external void f(int x()); |
583 } | 524 } |
584 ''', | 525 ''', r''' |
585 r''' | |
586 @patch | 526 @patch |
587 class C { | 527 class C { |
588 @patch | 528 @patch |
589 void f(double x()) {} | 529 void f(double x()) {} |
590 } | 530 } |
591 '''); | 531 '''); |
592 }, throwsArgumentError); | 532 }, throwsArgumentError); |
593 } | 533 } |
594 | 534 |
595 test_class_method_patch_fail_signatureChange_makeReturnTypeExplicit() { | 535 test_class_method_patch_fail_signatureChange_makeReturnTypeExplicit() { |
596 expect(() { | 536 expect(() { |
597 _doTopLevelPatching( | 537 _doTopLevelPatching(r''' |
598 r''' | |
599 class C { | 538 class C { |
600 external f(); | 539 external f(); |
601 } | 540 } |
602 ''', | 541 ''', r''' |
603 r''' | |
604 @patch | 542 @patch |
605 class C { | 543 class C { |
606 @patch | 544 @patch |
607 int f() => 0; | 545 int f() => 0; |
608 } | 546 } |
609 '''); | 547 '''); |
610 }, throwsArgumentError); | 548 }, throwsArgumentError); |
611 } | 549 } |
612 | 550 |
613 test_class_method_patch_fail_signatureChange_missingArgument() { | 551 test_class_method_patch_fail_signatureChange_missingArgument() { |
614 expect(() { | 552 expect(() { |
615 _doTopLevelPatching( | 553 _doTopLevelPatching(r''' |
616 r''' | |
617 class C { | 554 class C { |
618 external void f(int x); | 555 external void f(int x); |
619 } | 556 } |
620 ''', | 557 ''', r''' |
621 r''' | |
622 @patch | 558 @patch |
623 class C { | 559 class C { |
624 @patch | 560 @patch |
625 void f() {} | 561 void f() {} |
626 } | 562 } |
627 '''); | 563 '''); |
628 }, throwsArgumentError); | 564 }, throwsArgumentError); |
629 } | 565 } |
630 | 566 |
631 test_class_method_patch_fail_signatureChange_missingTypeTokens() { | 567 test_class_method_patch_fail_signatureChange_missingTypeTokens() { |
632 expect(() { | 568 expect(() { |
633 _doTopLevelPatching( | 569 _doTopLevelPatching(r''' |
634 r''' | |
635 class C { | 570 class C { |
636 external List<int> f(); | 571 external List<int> f(); |
637 } | 572 } |
638 ''', | 573 ''', r''' |
639 r''' | |
640 @patch | 574 @patch |
641 class C { | 575 class C { |
642 @patch | 576 @patch |
643 List f() => null; | 577 List f() => null; |
644 } | 578 } |
645 '''); | 579 '''); |
646 }, throwsArgumentError); | 580 }, throwsArgumentError); |
647 } | 581 } |
648 | 582 |
649 test_class_method_patch_fail_signatureChange_nameOnly() { | 583 test_class_method_patch_fail_signatureChange_nameOnly() { |
650 expect(() { | 584 expect(() { |
651 _doTopLevelPatching( | 585 _doTopLevelPatching(r''' |
652 r''' | |
653 class C { | 586 class C { |
654 external void f(int x); | 587 external void f(int x); |
655 } | 588 } |
656 ''', | 589 ''', r''' |
657 r''' | |
658 @patch | 590 @patch |
659 class C { | 591 class C { |
660 @patch | 592 @patch |
661 void f(int y) {} | 593 void f(int y) {} |
662 } | 594 } |
663 '''); | 595 '''); |
664 }, throwsArgumentError); | 596 }, throwsArgumentError); |
665 } | 597 } |
666 | 598 |
667 test_class_method_patch_fail_signatureChange_returnTypeOnly() { | 599 test_class_method_patch_fail_signatureChange_returnTypeOnly() { |
668 expect(() { | 600 expect(() { |
669 _doTopLevelPatching( | 601 _doTopLevelPatching(r''' |
670 r''' | |
671 class C { | 602 class C { |
672 external void f(int x); | 603 external void f(int x); |
673 } | 604 } |
674 ''', | 605 ''', r''' |
675 r''' | |
676 @patch | 606 @patch |
677 class C { | 607 class C { |
678 @patch | 608 @patch |
679 int f(int x) {} | 609 int f(int x) {} |
680 } | 610 } |
681 '''); | 611 '''); |
682 }, throwsArgumentError); | 612 }, throwsArgumentError); |
683 } | 613 } |
684 | 614 |
685 test_class_method_patch_success_defaultFormalParameter() { | 615 test_class_method_patch_success_defaultFormalParameter() { |
686 CompilationUnit unit = _doTopLevelPatching( | 616 CompilationUnit unit = _doTopLevelPatching(r''' |
687 r''' | |
688 class C { | 617 class C { |
689 external void f(int x = 0); | 618 external void f(int x = 0); |
690 } | 619 } |
691 ''', | 620 ''', r''' |
692 r''' | |
693 @patch | 621 @patch |
694 class C { | 622 class C { |
695 @patch | 623 @patch |
696 void f(int x) {} | 624 void f(int x) {} |
697 } | 625 } |
698 '''); | 626 '''); |
699 ClassDeclaration cls = unit.declarations[0]; | 627 ClassDeclaration cls = unit.declarations[0]; |
700 MethodDeclaration method = cls.members[0]; | 628 MethodDeclaration method = cls.members[0]; |
701 FormalParameter parameter = method.parameters.parameters[0]; | 629 FormalParameter parameter = method.parameters.parameters[0]; |
702 expect(parameter, new isInstanceOf<DefaultFormalParameter>()); | 630 expect(parameter, new isInstanceOf<DefaultFormalParameter>()); |
703 } | 631 } |
704 | 632 |
705 test_class_method_patch_success_implicitReturnType() { | 633 test_class_method_patch_success_implicitReturnType() { |
706 _doTopLevelPatching( | 634 _doTopLevelPatching(r''' |
707 r''' | |
708 class C { | 635 class C { |
709 external f(); | 636 external f(); |
710 } | 637 } |
711 ''', | 638 ''', r''' |
712 r''' | |
713 @patch | 639 @patch |
714 class C { | 640 class C { |
715 @patch | 641 @patch |
716 f() => null; | 642 f() => null; |
717 } | 643 } |
718 '''); | 644 '''); |
719 } | 645 } |
720 | 646 |
721 test_class_method_patch_success_multiTokenReturnType() { | 647 test_class_method_patch_success_multiTokenReturnType() { |
722 _doTopLevelPatching( | 648 _doTopLevelPatching(r''' |
723 r''' | |
724 class C { | 649 class C { |
725 external List<int> f(); | 650 external List<int> f(); |
726 } | 651 } |
727 ''', | 652 ''', r''' |
728 r''' | |
729 @patch | 653 @patch |
730 class C { | 654 class C { |
731 @patch | 655 @patch |
732 List<int> f() => null; | 656 List<int> f() => null; |
733 } | 657 } |
734 '''); | 658 '''); |
735 } | 659 } |
736 | 660 |
737 test_class_method_patch_success_signatureChange_functionTypedParam_matching()
{ | 661 test_class_method_patch_success_signatureChange_functionTypedParam_matching()
{ |
738 _doTopLevelPatching( | 662 _doTopLevelPatching(r''' |
739 r''' | |
740 class C { | 663 class C { |
741 external void f(void x(int y)); | 664 external void f(void x(int y)); |
742 } | 665 } |
743 ''', | 666 ''', r''' |
744 r''' | |
745 @patch | 667 @patch |
746 class C { | 668 class C { |
747 @patch | 669 @patch |
748 void f(void x(int y)) {} | 670 void f(void x(int y)) {} |
749 } | 671 } |
750 '''); | 672 '''); |
751 } | 673 } |
752 | 674 |
753 test_class_setter_append() { | 675 test_class_setter_append() { |
754 CompilationUnit unit = _doTopLevelPatching( | 676 CompilationUnit unit = _doTopLevelPatching(r''' |
755 r''' | |
756 class C { | 677 class C { |
757 void a() {} | 678 void a() {} |
758 } | 679 } |
759 ''', | 680 ''', r''' |
760 r''' | |
761 @patch | 681 @patch |
762 class C { | 682 class C { |
763 void set _b(_) {} | 683 void set _b(_) {} |
764 } | 684 } |
765 '''); | 685 '''); |
766 _assertUnitCode(unit, 'class C {void a() {} void set _b(_) {}}'); | 686 _assertUnitCode(unit, 'class C {void a() {} void set _b(_) {}}'); |
767 } | 687 } |
768 | 688 |
769 test_directive_fail_export() { | 689 test_directive_fail_export() { |
770 expect(() { | 690 expect(() { |
771 _doTopLevelPatching( | 691 _doTopLevelPatching(r''' |
772 r''' | |
773 import 'a.dart'; | 692 import 'a.dart'; |
774 ''', | 693 ''', r''' |
775 r''' | |
776 export 'c.dart'; | 694 export 'c.dart'; |
777 '''); | 695 '''); |
778 }, throwsArgumentError); | 696 }, throwsArgumentError); |
779 } | 697 } |
780 | 698 |
781 test_directive_import() { | 699 test_directive_import() { |
782 CompilationUnit unit = _doTopLevelPatching( | 700 CompilationUnit unit = _doTopLevelPatching(r''' |
783 r''' | |
784 import 'a.dart'; | 701 import 'a.dart'; |
785 part 'b.dart'; | 702 part 'b.dart'; |
786 int bar() => 0; | 703 int bar() => 0; |
787 ''', | 704 ''', r''' |
788 r''' | |
789 import 'c.dart'; | 705 import 'c.dart'; |
790 '''); | 706 '''); |
791 _assertUnitCode(unit, | 707 _assertUnitCode(unit, |
792 "import 'a.dart'; part 'b.dart'; import 'c.dart'; int bar() => 0;"); | 708 "import 'a.dart'; part 'b.dart'; import 'c.dart'; int bar() => 0;"); |
793 } | 709 } |
794 | 710 |
795 test_fail_patchFileDoesNotExist() { | 711 test_fail_patchFileDoesNotExist() { |
796 expect(() { | 712 expect(() { |
797 _setSdkLibraries(r''' | 713 _setSdkLibraries(r''' |
798 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { | 714 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { |
(...skipping 13 matching lines...) Expand all Loading... |
812 | 728 |
813 test_internal_allowNewPublicNames() { | 729 test_internal_allowNewPublicNames() { |
814 _setSdkLibraries(r''' | 730 _setSdkLibraries(r''' |
815 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { | 731 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { |
816 '_internal' : const LibraryInfo( | 732 '_internal' : const LibraryInfo( |
817 'internal/internal.dart'), | 733 'internal/internal.dart'), |
818 };'''); | 734 };'''); |
819 var patchPaths = { | 735 var patchPaths = { |
820 'dart:_internal': [_p('/sdk/lib/internal/internal_patch.dart')] | 736 'dart:_internal': [_p('/sdk/lib/internal/internal_patch.dart')] |
821 }; | 737 }; |
822 File file = provider.newFile( | 738 File file = provider.newFile(_p('/sdk/lib/internal/internal.dart'), r''' |
823 _p('/sdk/lib/internal/internal.dart'), | |
824 r''' | |
825 library dart._internal; | 739 library dart._internal; |
826 class A {} | 740 class A {} |
827 class B { | 741 class B { |
828 B(); | 742 B(); |
829 } | 743 } |
830 '''); | 744 '''); |
831 provider.newFile( | 745 provider.newFile(_p('/sdk/lib/internal/internal_patch.dart'), r''' |
832 _p('/sdk/lib/internal/internal_patch.dart'), | |
833 r''' | |
834 @patch | 746 @patch |
835 class B { | 747 class B { |
836 int newField; | 748 int newField; |
837 B.newConstructor(); | 749 B.newConstructor(); |
838 int newMethod() => 1; | 750 int newMethod() => 1; |
839 } | 751 } |
840 class NewClass {} | 752 class NewClass {} |
841 int newFunction() => 2; | 753 int newFunction() => 2; |
842 '''); | 754 '''); |
843 | 755 |
(...skipping 24 matching lines...) Expand all Loading... |
868 'test' : const LibraryInfo( | 780 'test' : const LibraryInfo( |
869 'test/test.dart', | 781 'test/test.dart', |
870 patches: {VM_PLATFORM: ['test/test_patch.dart']}), | 782 patches: {VM_PLATFORM: ['test/test_patch.dart']}), |
871 };'''); | 783 };'''); |
872 var patchPaths = { | 784 var patchPaths = { |
873 'dart:test': [_p('/sdk/lib/test/test_patch.dart')] | 785 'dart:test': [_p('/sdk/lib/test/test_patch.dart')] |
874 }; | 786 }; |
875 File fileLib = provider.newFile(_p('/sdk/lib/test/test.dart'), baseLibCode); | 787 File fileLib = provider.newFile(_p('/sdk/lib/test/test.dart'), baseLibCode); |
876 File filePart = | 788 File filePart = |
877 provider.newFile(_p('/sdk/lib/test/test_part.dart'), basePartCode); | 789 provider.newFile(_p('/sdk/lib/test/test_part.dart'), basePartCode); |
878 provider.newFile( | 790 provider.newFile(_p('/sdk/lib/test/test_patch.dart'), r''' |
879 _p('/sdk/lib/test/test_patch.dart'), | |
880 r''' | |
881 import 'foo.dart'; | 791 import 'foo.dart'; |
882 | 792 |
883 @patch | 793 @patch |
884 class A { | 794 class A { |
885 int _a() => 1; | 795 int _a() => 1; |
886 } | 796 } |
887 | 797 |
888 @patch | 798 @patch |
889 class B { | 799 class B { |
890 int _b() => 1; | 800 int _b() => 1; |
(...skipping 18 matching lines...) Expand all Loading... |
909 { | 819 { |
910 Uri uri = Uri.parse('dart:test/test_part.dart'); | 820 Uri uri = Uri.parse('dart:test/test_part.dart'); |
911 Source source = filePart.createSource(uri); | 821 Source source = filePart.createSource(uri); |
912 CompilationUnit unit = SdkPatcher.parse(source, true, listener); | 822 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
913 patcher.patch(provider, true, patchPaths, listener, source, unit); | 823 patcher.patch(provider, true, patchPaths, listener, source, unit); |
914 _assertUnitCode(unit, "part of test; class B {int _b() => 1;}"); | 824 _assertUnitCode(unit, "part of test; class B {int _b() => 1;}"); |
915 } | 825 } |
916 } | 826 } |
917 | 827 |
918 test_topLevel_class_append() { | 828 test_topLevel_class_append() { |
919 CompilationUnit unit = _doTopLevelPatching( | 829 CompilationUnit unit = _doTopLevelPatching(r''' |
920 r''' | |
921 class A {} | 830 class A {} |
922 ''', | 831 ''', r''' |
923 r''' | |
924 class _B { | 832 class _B { |
925 void mmm() {} | 833 void mmm() {} |
926 } | 834 } |
927 '''); | 835 '''); |
928 _assertUnitCode(unit, 'class A {} class _B {void mmm() {}}'); | 836 _assertUnitCode(unit, 'class A {} class _B {void mmm() {}}'); |
929 ClassDeclaration a = unit.declarations[0]; | 837 ClassDeclaration a = unit.declarations[0]; |
930 ClassDeclaration b = unit.declarations[1]; | 838 ClassDeclaration b = unit.declarations[1]; |
931 _assertPrevNextToken(a.endToken, b.beginToken); | 839 _assertPrevNextToken(a.endToken, b.beginToken); |
932 } | 840 } |
933 | 841 |
934 test_topLevel_class_fail_mixinApplication() { | 842 test_topLevel_class_fail_mixinApplication() { |
935 expect(() { | 843 expect(() { |
936 _doTopLevelPatching( | 844 _doTopLevelPatching(r''' |
937 r''' | |
938 class A {} | 845 class A {} |
939 ''', | 846 ''', r''' |
940 r''' | |
941 class _B {} | 847 class _B {} |
942 class _C = Object with _B; | 848 class _C = Object with _B; |
943 '''); | 849 '''); |
944 }, throwsArgumentError); | 850 }, throwsArgumentError); |
945 } | 851 } |
946 | 852 |
947 test_topLevel_class_fail_notPrivate() { | 853 test_topLevel_class_fail_notPrivate() { |
948 expect(() { | 854 expect(() { |
949 _doTopLevelPatching( | 855 _doTopLevelPatching(r''' |
950 r''' | |
951 class A {} | 856 class A {} |
952 ''', | 857 ''', r''' |
953 r''' | |
954 class B {} | 858 class B {} |
955 '''); | 859 '''); |
956 }, throwsArgumentError); | 860 }, throwsArgumentError); |
957 } | 861 } |
958 | 862 |
959 test_topLevel_function_append() { | 863 test_topLevel_function_append() { |
960 CompilationUnit unit = _doTopLevelPatching( | 864 CompilationUnit unit = _doTopLevelPatching(r''' |
961 r''' | |
962 int foo() => 0; | 865 int foo() => 0; |
963 ''', | 866 ''', r''' |
964 r''' | |
965 int _bar1() => 1; | 867 int _bar1() => 1; |
966 int _bar2() => 2; | 868 int _bar2() => 2; |
967 '''); | 869 '''); |
968 _assertUnitCode( | 870 _assertUnitCode( |
969 unit, 'int foo() => 0; int _bar1() => 1; int _bar2() => 2;'); | 871 unit, 'int foo() => 0; int _bar1() => 1; int _bar2() => 2;'); |
970 | 872 |
971 FunctionDeclaration foo = unit.declarations[0]; | 873 FunctionDeclaration foo = unit.declarations[0]; |
972 FunctionDeclaration bar1 = unit.declarations[1]; | 874 FunctionDeclaration bar1 = unit.declarations[1]; |
973 FunctionDeclaration bar2 = unit.declarations[2]; | 875 FunctionDeclaration bar2 = unit.declarations[2]; |
974 | 876 |
975 _assertPrevNextToken(foo.endToken, bar1.beginToken); | 877 _assertPrevNextToken(foo.endToken, bar1.beginToken); |
976 _assertPrevNextToken(bar1.endToken, bar2.beginToken); | 878 _assertPrevNextToken(bar1.endToken, bar2.beginToken); |
977 } | 879 } |
978 | 880 |
979 test_topLevel_function_fail_noExternalKeyword() { | 881 test_topLevel_function_fail_noExternalKeyword() { |
980 expect(() { | 882 expect(() { |
981 _doTopLevelPatching( | 883 _doTopLevelPatching(r''' |
982 r''' | |
983 int foo(); | 884 int foo(); |
984 ''', | 885 ''', r''' |
985 r''' | |
986 @patch | 886 @patch |
987 int foo() => 1; | 887 int foo() => 1; |
988 '''); | 888 '''); |
989 }, throwsArgumentError); | 889 }, throwsArgumentError); |
990 } | 890 } |
991 | 891 |
992 test_topLevel_function_fail_notPrivate() { | 892 test_topLevel_function_fail_notPrivate() { |
993 expect(() { | 893 expect(() { |
994 _doTopLevelPatching( | 894 _doTopLevelPatching(r''' |
995 r''' | |
996 int foo() => 1; | 895 int foo() => 1; |
997 ''', | 896 ''', r''' |
998 r''' | |
999 int bar() => 2; | 897 int bar() => 2; |
1000 '''); | 898 '''); |
1001 }, throwsArgumentError); | 899 }, throwsArgumentError); |
1002 } | 900 } |
1003 | 901 |
1004 test_topLevel_functionTypeAlias_append() { | 902 test_topLevel_functionTypeAlias_append() { |
1005 CompilationUnit unit = _doTopLevelPatching( | 903 CompilationUnit unit = _doTopLevelPatching(r''' |
1006 r''' | |
1007 int foo() => 0; | 904 int foo() => 0; |
1008 ''', | 905 ''', r''' |
1009 r''' | |
1010 typedef int _bar1(); | 906 typedef int _bar1(); |
1011 typedef int _bar2(); | 907 typedef int _bar2(); |
1012 '''); | 908 '''); |
1013 _assertUnitCode( | 909 _assertUnitCode( |
1014 unit, 'int foo() => 0; typedef int _bar1(); typedef int _bar2();'); | 910 unit, 'int foo() => 0; typedef int _bar1(); typedef int _bar2();'); |
1015 | 911 |
1016 FunctionDeclaration foo = unit.declarations[0]; | 912 FunctionDeclaration foo = unit.declarations[0]; |
1017 FunctionTypeAlias bar1 = unit.declarations[1]; | 913 FunctionTypeAlias bar1 = unit.declarations[1]; |
1018 FunctionTypeAlias bar2 = unit.declarations[2]; | 914 FunctionTypeAlias bar2 = unit.declarations[2]; |
1019 | 915 |
1020 _assertPrevNextToken(foo.endToken, bar1.beginToken); | 916 _assertPrevNextToken(foo.endToken, bar1.beginToken); |
1021 _assertPrevNextToken(bar1.endToken, bar2.beginToken); | 917 _assertPrevNextToken(bar1.endToken, bar2.beginToken); |
1022 expect(unit.endToken.type, TokenType.EOF); | 918 expect(unit.endToken.type, TokenType.EOF); |
1023 expect(bar2.endToken.next, same(unit.endToken)); | 919 expect(bar2.endToken.next, same(unit.endToken)); |
1024 } | 920 } |
1025 | 921 |
1026 test_topLevel_functionTypeAlias_fail_hasAnnotation() { | 922 test_topLevel_functionTypeAlias_fail_hasAnnotation() { |
1027 expect(() { | 923 expect(() { |
1028 _doTopLevelPatching( | 924 _doTopLevelPatching(r''' |
1029 r''' | |
1030 int foo() => 0; | 925 int foo() => 0; |
1031 ''', | 926 ''', r''' |
1032 r''' | |
1033 @patch | 927 @patch |
1034 typedef int _bar(); | 928 typedef int _bar(); |
1035 '''); | 929 '''); |
1036 }, throwsArgumentError); | 930 }, throwsArgumentError); |
1037 } | 931 } |
1038 | 932 |
1039 test_topLevel_functionTypeAlias_fail_notPrivate() { | 933 test_topLevel_functionTypeAlias_fail_notPrivate() { |
1040 expect(() { | 934 expect(() { |
1041 _doTopLevelPatching( | 935 _doTopLevelPatching(r''' |
1042 r''' | |
1043 int foo() => 0; | 936 int foo() => 0; |
1044 ''', | 937 ''', r''' |
1045 r''' | |
1046 typedef int bar(); | 938 typedef int bar(); |
1047 '''); | 939 '''); |
1048 }, throwsArgumentError); | 940 }, throwsArgumentError); |
1049 } | 941 } |
1050 | 942 |
1051 test_topLevel_patch_function() { | 943 test_topLevel_patch_function() { |
1052 CompilationUnit unit = _doTopLevelPatching( | 944 CompilationUnit unit = _doTopLevelPatching(r''' |
1053 r''' | |
1054 external int foo(); | 945 external int foo(); |
1055 int bar() => 2; | 946 int bar() => 2; |
1056 ''', | 947 ''', r''' |
1057 r''' | |
1058 @patch | 948 @patch |
1059 int foo() => 1; | 949 int foo() => 1; |
1060 '''); | 950 '''); |
1061 _assertUnitCode(unit, 'int foo() => 1; int bar() => 2;'); | 951 _assertUnitCode(unit, 'int foo() => 1; int bar() => 2;'); |
1062 | 952 |
1063 // Prepare functions. | 953 // Prepare functions. |
1064 FunctionDeclaration foo = unit.declarations[0]; | 954 FunctionDeclaration foo = unit.declarations[0]; |
1065 FunctionDeclaration bar = unit.declarations[1]; | 955 FunctionDeclaration bar = unit.declarations[1]; |
1066 | 956 |
1067 // The "external" token is removed from the stream. | 957 // The "external" token is removed from the stream. |
1068 { | 958 { |
1069 expect(foo.externalKeyword, isNull); | 959 expect(foo.externalKeyword, isNull); |
1070 Token token = foo.beginToken; | 960 Token token = foo.beginToken; |
1071 expect(token.lexeme, 'int'); | 961 expect(token.lexeme, 'int'); |
1072 expect(token.previous.type, TokenType.EOF); | 962 expect(token.previous.type, TokenType.EOF); |
1073 } | 963 } |
1074 | 964 |
1075 // The body tokens are included into the patched token stream. | 965 // The body tokens are included into the patched token stream. |
1076 { | 966 { |
1077 FunctionExpression fooExpr = foo.functionExpression; | 967 FunctionExpression fooExpr = foo.functionExpression; |
1078 FunctionBody fooBody = fooExpr.body; | 968 FunctionBody fooBody = fooExpr.body; |
1079 expect(fooBody.beginToken.previous, same(fooExpr.parameters.endToken)); | 969 expect(fooBody.beginToken.previous, same(fooExpr.parameters.endToken)); |
1080 expect(fooBody.endToken.next, same(bar.beginToken)); | 970 expect(fooBody.endToken.next, same(bar.beginToken)); |
1081 } | 971 } |
1082 } | 972 } |
1083 | 973 |
1084 test_topLevel_patch_function_blockBody() { | 974 test_topLevel_patch_function_blockBody() { |
1085 CompilationUnit unit = _doTopLevelPatching( | 975 CompilationUnit unit = _doTopLevelPatching(r''' |
1086 r''' | |
1087 external int foo(); | 976 external int foo(); |
1088 ''', | 977 ''', r''' |
1089 r''' | |
1090 @patch | 978 @patch |
1091 int foo() {int v = 1; return v + 2;} | 979 int foo() {int v = 1; return v + 2;} |
1092 '''); | 980 '''); |
1093 _assertUnitCode(unit, 'int foo() {int v = 1; return v + 2;}'); | 981 _assertUnitCode(unit, 'int foo() {int v = 1; return v + 2;}'); |
1094 } | 982 } |
1095 | 983 |
1096 test_topLevel_patch_function_fail_signatureChange() { | 984 test_topLevel_patch_function_fail_signatureChange() { |
1097 expect(() { | 985 expect(() { |
1098 _doTopLevelPatching( | 986 _doTopLevelPatching(r''' |
1099 r''' | |
1100 external void f(int x); | 987 external void f(int x); |
1101 ''', | 988 ''', r''' |
1102 r''' | |
1103 @patch | 989 @patch |
1104 void f(double x) {} | 990 void f(double x) {} |
1105 '''); | 991 '''); |
1106 }, throwsArgumentError); | 992 }, throwsArgumentError); |
1107 } | 993 } |
1108 | 994 |
1109 test_topLevel_patch_function_fail_signatureChange_nameOnly() { | 995 test_topLevel_patch_function_fail_signatureChange_nameOnly() { |
1110 expect(() { | 996 expect(() { |
1111 _doTopLevelPatching( | 997 _doTopLevelPatching(r''' |
1112 r''' | |
1113 external void f(int x); | 998 external void f(int x); |
1114 ''', | 999 ''', r''' |
1115 r''' | |
1116 @patch | 1000 @patch |
1117 void f(int y) {} | 1001 void f(int y) {} |
1118 '''); | 1002 '''); |
1119 }, throwsArgumentError); | 1003 }, throwsArgumentError); |
1120 } | 1004 } |
1121 | 1005 |
1122 test_topLevel_patch_function_fail_signatureChange_returnTypeOnly() { | 1006 test_topLevel_patch_function_fail_signatureChange_returnTypeOnly() { |
1123 expect(() { | 1007 expect(() { |
1124 _doTopLevelPatching( | 1008 _doTopLevelPatching(r''' |
1125 r''' | |
1126 external void f(int x); | 1009 external void f(int x); |
1127 ''', | 1010 ''', r''' |
1128 r''' | |
1129 @patch | 1011 @patch |
1130 int f(int x) {} | 1012 int f(int x) {} |
1131 '''); | 1013 '''); |
1132 }, throwsArgumentError); | 1014 }, throwsArgumentError); |
1133 } | 1015 } |
1134 | 1016 |
1135 test_topLevel_patch_getter() { | 1017 test_topLevel_patch_getter() { |
1136 CompilationUnit unit = _doTopLevelPatching( | 1018 CompilationUnit unit = _doTopLevelPatching(r''' |
1137 r''' | |
1138 external int get foo; | 1019 external int get foo; |
1139 int bar() => 2; | 1020 int bar() => 2; |
1140 ''', | 1021 ''', r''' |
1141 r''' | |
1142 @patch | 1022 @patch |
1143 int get foo => 1; | 1023 int get foo => 1; |
1144 '''); | 1024 '''); |
1145 _assertUnitCode(unit, 'int get foo => 1; int bar() => 2;'); | 1025 _assertUnitCode(unit, 'int get foo => 1; int bar() => 2;'); |
1146 } | 1026 } |
1147 | 1027 |
1148 test_topLevel_patch_setter() { | 1028 test_topLevel_patch_setter() { |
1149 CompilationUnit unit = _doTopLevelPatching( | 1029 CompilationUnit unit = _doTopLevelPatching(r''' |
1150 r''' | |
1151 external void set foo(int val); | 1030 external void set foo(int val); |
1152 int bar() => 2; | 1031 int bar() => 2; |
1153 ''', | 1032 ''', r''' |
1154 r''' | |
1155 @patch | 1033 @patch |
1156 void set foo(int val) {} | 1034 void set foo(int val) {} |
1157 '''); | 1035 '''); |
1158 _assertUnitCode(unit, 'void set foo(int val) {} int bar() => 2;'); | 1036 _assertUnitCode(unit, 'void set foo(int val) {} int bar() => 2;'); |
1159 } | 1037 } |
1160 | 1038 |
1161 test_topLevel_topLevelVariable_append() { | 1039 test_topLevel_topLevelVariable_append() { |
1162 CompilationUnit unit = _doTopLevelPatching( | 1040 CompilationUnit unit = _doTopLevelPatching(r''' |
1163 r''' | |
1164 int foo() => 0; | 1041 int foo() => 0; |
1165 ''', | 1042 ''', r''' |
1166 r''' | |
1167 int _bar; | 1043 int _bar; |
1168 '''); | 1044 '''); |
1169 _assertUnitCode(unit, 'int foo() => 0; int _bar;'); | 1045 _assertUnitCode(unit, 'int foo() => 0; int _bar;'); |
1170 FunctionDeclaration a = unit.declarations[0]; | 1046 FunctionDeclaration a = unit.declarations[0]; |
1171 TopLevelVariableDeclaration b = unit.declarations[1]; | 1047 TopLevelVariableDeclaration b = unit.declarations[1]; |
1172 _assertPrevNextToken(a.endToken, b.beginToken); | 1048 _assertPrevNextToken(a.endToken, b.beginToken); |
1173 } | 1049 } |
1174 | 1050 |
1175 void _assertUnitCode(CompilationUnit unit, String expectedCode) { | 1051 void _assertUnitCode(CompilationUnit unit, String expectedCode) { |
1176 expect(unit.toSource(), expectedCode); | 1052 expect(unit.toSource(), expectedCode); |
(...skipping 29 matching lines...) Expand all Loading... |
1206 void _setSdkLibraries(String code) { | 1082 void _setSdkLibraries(String code) { |
1207 provider.newFile( | 1083 provider.newFile( |
1208 _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code); | 1084 _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code); |
1209 } | 1085 } |
1210 | 1086 |
1211 static void _assertPrevNextToken(Token prev, Token next) { | 1087 static void _assertPrevNextToken(Token prev, Token next) { |
1212 expect(prev.next, same(next)); | 1088 expect(prev.next, same(next)); |
1213 expect(next.previous, same(prev)); | 1089 expect(next.previous, same(prev)); |
1214 } | 1090 } |
1215 } | 1091 } |
OLD | NEW |