Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: pkg/analyzer/test/src/task/strong_mode_test.dart

Issue 1306313002: Implement instance member inference (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address more comments and fix bug Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 library test.src.task.strong_mode_test; 5 library test.src.task.strong_mode_test;
6 6
7 import 'package:analyzer/src/generated/ast.dart'; 7 import 'package:analyzer/src/generated/ast.dart';
8 import 'package:analyzer/src/generated/element.dart';
8 import 'package:analyzer/src/generated/source.dart'; 9 import 'package:analyzer/src/generated/source.dart';
9 import 'package:analyzer/src/task/strong_mode.dart'; 10 import 'package:analyzer/src/task/strong_mode.dart';
10 import 'package:analyzer/task/dart.dart'; 11 import 'package:analyzer/task/dart.dart';
11 import 'package:unittest/unittest.dart'; 12 import 'package:unittest/unittest.dart';
12 13
13 import '../../reflective_tests.dart'; 14 import '../../reflective_tests.dart';
14 import '../../utils.dart'; 15 import '../../utils.dart';
15 import '../context/abstract_context.dart'; 16 import '../context/abstract_context.dart';
16 17
17 main() { 18 main() {
18 initializeTestEnvironment(); 19 initializeTestEnvironment();
19 runReflectiveTests(InferrenceFinderTest); 20 runReflectiveTests(InferrenceFinderTest);
21 runReflectiveTests(InstanceMemberInferrerTest);
20 } 22 }
21 23
22 @reflectiveTest 24 @reflectiveTest
23 class InferrenceFinderTest extends AbstractContextTest { 25 class InferrenceFinderTest extends AbstractContextTest {
24 void test_creation() { 26 void test_creation() {
25 InferrenceFinder finder = new InferrenceFinder(); 27 InferrenceFinder finder = new InferrenceFinder();
26 expect(finder, isNotNull); 28 expect(finder, isNotNull);
27 expect(finder.classes, isEmpty); 29 expect(finder.classes, isEmpty);
28 expect(finder.staticVariables, isEmpty); 30 expect(finder.staticVariables, isEmpty);
29 } 31 }
(...skipping 20 matching lines...) Expand all
50 typedef int F(int x); 52 typedef int F(int x);
51 '''); 53 ''');
52 computeResult(source, PARSED_UNIT); 54 computeResult(source, PARSED_UNIT);
53 CompilationUnit unit = outputs[PARSED_UNIT]; 55 CompilationUnit unit = outputs[PARSED_UNIT];
54 InferrenceFinder finder = new InferrenceFinder(); 56 InferrenceFinder finder = new InferrenceFinder();
55 unit.accept(finder); 57 unit.accept(finder);
56 expect(finder.classes, hasLength(3)); 58 expect(finder.classes, hasLength(3));
57 expect(finder.staticVariables, hasLength(6)); 59 expect(finder.staticVariables, hasLength(6));
58 } 60 }
59 } 61 }
62
63 @reflectiveTest
64 class InstanceMemberInferrerTest extends AbstractContextTest {
65 InstanceMemberInferrer get createInferrer =>
66 new InstanceMemberInferrer(context.typeProvider);
67
68 /**
69 * Add a source with the given [content] and return the result of resolving
70 * the source.
71 */
72 CompilationUnitElement resolve(String content) {
73 Source source = addSource('/test.dart', content);
74 return context.resolveCompilationUnit2(source, source).element;
75 }
76
77 void test_creation() {
78 InstanceMemberInferrer inferrer = createInferrer;
79 expect(inferrer, isNotNull);
80 expect(inferrer.typeSystem, isNotNull);
81 }
82
83 void test_inferCompilationUnit_field_multiple_different() {
84 InstanceMemberInferrer inferrer = createInferrer;
85 String fieldName = 'f';
86 CompilationUnitElement unit = resolve('''
87 class A {
88 int $fieldName;
89 }
90 class B {
91 double $fieldName;
92 }
93 class C implements A, B {
94 var $fieldName;
95 }
96 ''');
97 ClassElement classC = unit.getType('C');
98 FieldElement fieldC = classC.getField(fieldName);
99 PropertyAccessorElement getterC = classC.getGetter(fieldName);
100 expect(fieldC.type.isDynamic, isTrue);
101 expect(getterC.returnType.isDynamic, isTrue);
102
103 inferrer.inferCompilationUnit(unit);
104
105 expect(fieldC.type.isDynamic, isTrue);
106 expect(getterC.returnType.isDynamic, isTrue);
107 }
108
109 void test_inferCompilationUnit_field_multiple_dynamic() {
110 InstanceMemberInferrer inferrer = createInferrer;
111 String fieldName = 'f';
112 CompilationUnitElement unit = resolve('''
113 class A {
114 int $fieldName;
115 }
116 class B {
117 var $fieldName;
118 }
119 class C implements A, B {
120 var $fieldName;
121 }
122 ''');
123 ClassElement classC = unit.getType('C');
124 FieldElement fieldC = classC.getField(fieldName);
125 PropertyAccessorElement getterC = classC.getGetter(fieldName);
126 expect(fieldC.type.isDynamic, isTrue);
127 expect(getterC.returnType.isDynamic, isTrue);
128
129 inferrer.inferCompilationUnit(unit);
130
131 expect(fieldC.type.isDynamic, isTrue);
132 expect(getterC.returnType.isDynamic, isTrue);
133 }
134
135 void test_inferCompilationUnit_field_multiple_same() {
136 InstanceMemberInferrer inferrer = createInferrer;
137 String fieldName = 'f';
138 CompilationUnitElement unit = resolve('''
139 class A {
140 int $fieldName;
141 }
142 class B {
143 int $fieldName;
144 }
145 class C implements A, B {
146 var $fieldName;
147 }
148 ''');
149 ClassElement classA = unit.getType('A');
150 FieldElement fieldA = classA.getField(fieldName);
151 DartType expectedType = fieldA.type;
152 ClassElement classC = unit.getType('C');
153 FieldElement fieldC = classC.getField(fieldName);
154 PropertyAccessorElement getterC = classC.getGetter(fieldName);
155 expect(fieldC.type.isDynamic, isTrue);
156 expect(getterC.returnType.isDynamic, isTrue);
157
158 inferrer.inferCompilationUnit(unit);
159
160 expect(fieldC.type, expectedType);
161 expect(getterC.returnType, expectedType);
162 }
163
164 void test_inferCompilationUnit_field_noOverride() {
165 InstanceMemberInferrer inferrer = createInferrer;
166 String fieldName = 'f';
167 CompilationUnitElement unit = resolve('''
168 class A {
169 final $fieldName = 0;
170 }
171 ''');
172 ClassElement classA = unit.getType('A');
173 FieldElement fieldA = classA.getField(fieldName);
174 PropertyAccessorElement getterA = classA.getGetter(fieldName);
175 expect(fieldA.type.isDynamic, isTrue);
176 expect(getterA.returnType.isDynamic, isTrue);
177
178 inferrer.inferCompilationUnit(unit);
179
180 DartType intType = inferrer.typeProvider.intType;
181 expect(fieldA.type, intType);
182 expect(getterA.returnType, intType);
183 }
184
185 void test_inferCompilationUnit_field_noOverride_bottom() {
186 InstanceMemberInferrer inferrer = createInferrer;
187 String fieldName = 'f';
188 CompilationUnitElement unit = resolve('''
189 class A {
190 var $fieldName = null;
191 }
192 ''');
193 ClassElement classA = unit.getType('A');
194 FieldElement fieldA = classA.getField(fieldName);
195 PropertyAccessorElement getterA = classA.getGetter(fieldName);
196 expect(fieldA.type.isDynamic, isTrue);
197 expect(getterA.returnType.isDynamic, isTrue);
198
199 inferrer.inferCompilationUnit(unit);
200
201 expect(fieldA.type.isDynamic, isTrue);
202 expect(getterA.returnType.isDynamic, isTrue);
203 }
204
205 void test_inferCompilationUnit_field_single_explicitlyDynamic() {
206 InstanceMemberInferrer inferrer = createInferrer;
207 String fieldName = 'f';
208 CompilationUnitElement unit = resolve('''
209 class A {
210 dynamic $fieldName;
211 }
212 class B extends A {
213 var $fieldName = 0;
214 }
215 ''');
216 ClassElement classA = unit.getType('A');
217 FieldElement fieldA = classA.getField(fieldName);
218 PropertyAccessorElement getterA = classA.getGetter(fieldName);
219 ClassElement classB = unit.getType('B');
220 FieldElement fieldB = classB.getField(fieldName);
221 PropertyAccessorElement getterB = classB.getGetter(fieldName);
222 expect(fieldB.type.isDynamic, isTrue);
223 expect(getterB.returnType.isDynamic, isTrue);
224
225 inferrer.inferCompilationUnit(unit);
226
227 expect(fieldB.type, fieldA.type);
228 expect(getterB.returnType, getterA.returnType);
229 }
230
231 void test_inferCompilationUnit_field_single_final() {
232 InstanceMemberInferrer inferrer = createInferrer;
233 String fieldName = 'f';
234 CompilationUnitElement unit = resolve('''
235 class A {
236 final int $fieldName;
237 }
238 class B extends A {
239 final $fieldName;
240 }
241 ''');
242 ClassElement classA = unit.getType('A');
243 FieldElement fieldA = classA.getField(fieldName);
244 PropertyAccessorElement getterA = classA.getGetter(fieldName);
245 ClassElement classB = unit.getType('B');
246 FieldElement fieldB = classB.getField(fieldName);
247 PropertyAccessorElement getterB = classB.getGetter(fieldName);
248 expect(fieldB.type.isDynamic, isTrue);
249 expect(getterB.returnType.isDynamic, isTrue);
250
251 inferrer.inferCompilationUnit(unit);
252
253 expect(fieldB.type, fieldA.type);
254 expect(getterB.returnType, getterA.returnType);
255 }
256
257 void test_inferCompilationUnit_field_single_inconsistentAccessors() {
258 InstanceMemberInferrer inferrer = createInferrer;
259 String fieldName = 'f';
260 CompilationUnitElement unit = resolve('''
261 class A {
262 int get $fieldName => 0;
263 set $fieldName(String value) {}
264 }
265 class B extends A {
266 var $fieldName;
267 }
268 ''');
269 ClassElement classB = unit.getType('B');
270 FieldElement fieldB = classB.getField(fieldName);
271 PropertyAccessorElement getterB = classB.getGetter(fieldName);
272 expect(fieldB.type.isDynamic, isTrue);
273 expect(getterB.returnType.isDynamic, isTrue);
274
275 inferrer.inferCompilationUnit(unit);
276
277 expect(fieldB.type.isDynamic, isTrue);
278 expect(getterB.returnType.isDynamic, isTrue);
279 }
280
281 void test_inferCompilationUnit_field_single_noModifiers() {
282 InstanceMemberInferrer inferrer = createInferrer;
283 String fieldName = 'f';
284 CompilationUnitElement unit = resolve('''
285 class A {
286 int $fieldName;
287 }
288 class B extends A {
289 var $fieldName;
290 }
291 ''');
292 ClassElement classA = unit.getType('A');
293 FieldElement fieldA = classA.getField(fieldName);
294 PropertyAccessorElement getterA = classA.getGetter(fieldName);
295 ClassElement classB = unit.getType('B');
296 FieldElement fieldB = classB.getField(fieldName);
297 PropertyAccessorElement getterB = classB.getGetter(fieldName);
298 expect(fieldB.type.isDynamic, isTrue);
299 expect(getterB.returnType.isDynamic, isTrue);
300
301 inferrer.inferCompilationUnit(unit);
302
303 expect(fieldB.type, fieldA.type);
304 expect(getterB.returnType, getterA.returnType);
305 }
306
307 void test_inferCompilationUnit_getter_multiple_different() {
308 InstanceMemberInferrer inferrer = createInferrer;
309 String getterName = 'g';
310 CompilationUnitElement unit = resolve('''
311 class A {
312 int get $getterName => 0;
313 }
314 class B {
315 double get $getterName => 0.0;
316 }
317 class C implements A, B {
318 get $getterName => 0;
319 }
320 ''');
321 ClassElement classC = unit.getType('C');
322 FieldElement fieldC = classC.getField(getterName);
323 PropertyAccessorElement getterC = classC.getGetter(getterName);
324 expect(fieldC.type.isDynamic, isTrue);
325 expect(getterC.returnType.isDynamic, isTrue);
326
327 inferrer.inferCompilationUnit(unit);
328
329 expect(fieldC.type.isDynamic, isTrue);
330 expect(getterC.returnType.isDynamic, isTrue);
331 }
332
333 void test_inferCompilationUnit_getter_multiple_dynamic() {
334 InstanceMemberInferrer inferrer = createInferrer;
335 String getterName = 'g';
336 CompilationUnitElement unit = resolve('''
337 class A {
338 int get $getterName => 0;
339 }
340 class B {
341 get $getterName => 0;
342 }
343 class C implements A, B {
344 get $getterName => 0;
345 }
346 ''');
347 ClassElement classC = unit.getType('C');
348 FieldElement fieldC = classC.getField(getterName);
349 PropertyAccessorElement getterC = classC.getGetter(getterName);
350 expect(fieldC.type.isDynamic, isTrue);
351 expect(getterC.returnType.isDynamic, isTrue);
352
353 inferrer.inferCompilationUnit(unit);
354
355 expect(fieldC.type.isDynamic, isTrue);
356 expect(getterC.returnType.isDynamic, isTrue);
357 }
358
359 void test_inferCompilationUnit_getter_multiple_same() {
360 InstanceMemberInferrer inferrer = createInferrer;
361 String getterName = 'g';
362 CompilationUnitElement unit = resolve('''
363 class A {
364 String get $getterName => '';
365 }
366 class B {
367 String get $getterName => '';
368 }
369 class C implements A, B {
370 get $getterName => '';
371 }
372 ''');
373 ClassElement classA = unit.getType('A');
374 PropertyAccessorElement getterA = classA.getGetter(getterName);
375 DartType expectedType = getterA.returnType;
376 ClassElement classC = unit.getType('C');
377 FieldElement fieldC = classC.getField(getterName);
378 PropertyAccessorElement getterC = classC.getGetter(getterName);
379 expect(fieldC.type.isDynamic, isTrue);
380 expect(getterC.returnType.isDynamic, isTrue);
381
382 inferrer.inferCompilationUnit(unit);
383
384 expect(fieldC.type, expectedType);
385 expect(getterC.returnType, expectedType);
386 }
387
388 void test_inferCompilationUnit_getter_single() {
389 InstanceMemberInferrer inferrer = createInferrer;
390 String getterName = 'g';
391 CompilationUnitElement unit = resolve('''
392 class A {
393 int get $getterName => 0;
394 }
395 class B extends A {
396 get $getterName => 0;
397 }
398 ''');
399 ClassElement classA = unit.getType('A');
400 FieldElement fieldA = classA.getField(getterName);
401 PropertyAccessorElement getterA = classA.getGetter(getterName);
402 ClassElement classB = unit.getType('B');
403 FieldElement fieldB = classB.getField(getterName);
404 PropertyAccessorElement getterB = classB.getGetter(getterName);
405 expect(fieldB.type.isDynamic, isTrue);
406 expect(getterB.returnType.isDynamic, isTrue);
407
408 inferrer.inferCompilationUnit(unit);
409
410 expect(fieldB.type, fieldA.type);
411 expect(getterB.returnType, getterA.returnType);
412 }
413
414 void test_inferCompilationUnit_getter_single_inconsistentAccessors() {
415 InstanceMemberInferrer inferrer = createInferrer;
416 String getterName = 'g';
417 CompilationUnitElement unit = resolve('''
418 class A {
419 int get $getterName => 0;
420 set $getterName(String value) {}
421 }
422 class B extends A {
423 var get $getterName => 1;
424 }
425 ''');
426 ClassElement classB = unit.getType('B');
427 FieldElement fieldB = classB.getField(getterName);
428 PropertyAccessorElement getterB = classB.getGetter(getterName);
429 expect(fieldB.type.isDynamic, isTrue);
430 expect(getterB.returnType.isDynamic, isTrue);
431
432 inferrer.inferCompilationUnit(unit);
433
434 expect(fieldB.type.isDynamic, isTrue);
435 expect(getterB.returnType.isDynamic, isTrue);
436 }
437
438 void test_inferCompilationUnit_invalid_inheritanceCycle() {
439 InstanceMemberInferrer inferrer = createInferrer;
440 CompilationUnitElement unit = resolve('''
441 class A extends C {}
442 class B extends A {}
443 class C extends B {}
444 ''');
445 inferrer.inferCompilationUnit(unit);
446 }
447
448 void test_inferCompilationUnit_method_multiple_different() {
449 InstanceMemberInferrer inferrer = createInferrer;
450 String methodName = 'm';
451 CompilationUnitElement unit = resolve('''
452 class A {
453 int $methodName() => 0;
454 }
455 class B {
456 double $methodName() => 0.0;
457 }
458 class C implements A, B {
459 $methodName() => 0;
460 }
461 ''');
462 ClassElement classC = unit.getType('C');
463 MethodElement methodC = classC.getMethod(methodName);
464 expect(methodC.returnType.isDynamic, isTrue);
465
466 inferrer.inferCompilationUnit(unit);
467
468 expect(methodC.returnType.isDynamic, isTrue);
469 }
470
471 void test_inferCompilationUnit_method_multiple_dynamic() {
472 InstanceMemberInferrer inferrer = createInferrer;
473 String methodName = 'm';
474 CompilationUnitElement unit = resolve('''
475 class A {
476 int $methodName() => 0;
477 }
478 class B {
479 $methodName() => 0;
480 }
481 class C implements A, B {
482 $methodName() => 0;
483 }
484 ''');
485 ClassElement classC = unit.getType('C');
486 MethodElement methodC = classC.getMethod(methodName);
487 expect(methodC.returnType.isDynamic, isTrue);
488
489 inferrer.inferCompilationUnit(unit);
490
491 expect(methodC.returnType.isDynamic, isTrue);
492 }
493
494 void test_inferCompilationUnit_method_multiple_same_nonVoid() {
495 InstanceMemberInferrer inferrer = createInferrer;
496 String methodName = 'm';
497 CompilationUnitElement unit = resolve('''
498 class A {
499 int $methodName() => 0;
500 }
501 class B {
502 int $methodName() => 0.0;
503 }
504 class C implements A, B {
505 $methodName() => 0;
506 }
507 ''');
508 ClassElement classA = unit.getType('A');
509 MethodElement methodA = classA.getMethod(methodName);
510 DartType expectedType = methodA.returnType;
511 ClassElement classC = unit.getType('C');
512 MethodElement methodC = classC.getMethod(methodName);
513 expect(methodC.returnType.isDynamic, isTrue);
514
515 inferrer.inferCompilationUnit(unit);
516
517 expect(methodC.returnType, expectedType);
518 }
519
520 void test_inferCompilationUnit_method_multiple_same_void() {
521 InstanceMemberInferrer inferrer = createInferrer;
522 String methodName = 'm';
523 CompilationUnitElement unit = resolve('''
524 class A {
525 void $methodName() {};
526 }
527 class B {
528 void $methodName() {};
529 }
530 class C implements A, B {
531 $methodName() {};
532 }
533 ''');
534 ClassElement classA = unit.getType('A');
535 MethodElement methodA = classA.getMethod(methodName);
536 DartType expectedType = methodA.returnType;
537 ClassElement classC = unit.getType('C');
538 MethodElement methodC = classC.getMethod(methodName);
539 expect(methodC.returnType.isDynamic, isTrue);
540
541 inferrer.inferCompilationUnit(unit);
542
543 expect(methodC.returnType, expectedType);
544 }
545
546 void test_inferCompilationUnit_method_multiple_void() {
547 InstanceMemberInferrer inferrer = createInferrer;
548 String methodName = 'm';
549 CompilationUnitElement unit = resolve('''
550 class A {
551 int $methodName() => 0;
552 }
553 class B {
554 void $methodName() => 0;
555 }
556 class C implements A, B {
557 $methodName() => 0;
558 }
559 ''');
560 ClassElement classC = unit.getType('C');
561 MethodElement methodC = classC.getMethod(methodName);
562 expect(methodC.returnType.isDynamic, isTrue);
563
564 inferrer.inferCompilationUnit(unit);
565
566 expect(methodC.returnType.isDynamic, isTrue);
567 }
568
569 void test_inferCompilationUnit_method_single() {
570 InstanceMemberInferrer inferrer = createInferrer;
571 String methodName = 'm';
572 CompilationUnitElement unit = resolve('''
573 class A {
574 int $methodName() => 0;
575 }
576 class B extends A {
577 $methodName() => 0;
578 }
579 ''');
580 ClassElement classA = unit.getType('A');
581 MethodElement methodA = classA.getMethod(methodName);
582 ClassElement classB = unit.getType('B');
583 MethodElement methodB = classB.getMethod(methodName);
584 expect(methodB.returnType.isDynamic, isTrue);
585
586 inferrer.inferCompilationUnit(unit);
587
588 expect(methodB.returnType, methodA.returnType);
589 }
590 }
OLDNEW
« pkg/analyzer/lib/src/task/strong_mode.dart ('K') | « pkg/analyzer/lib/src/task/strong_mode.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698