OLD | NEW |
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 analyzer.test.src.task.strong_mode_test; | 5 library analyzer.test.src.task.strong_mode_test; |
6 | 6 |
| 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/dart/element/type.dart'; |
7 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/ast.dart'; |
8 import 'package:analyzer/src/generated/element.dart'; | |
9 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
10 import 'package:analyzer/src/task/strong_mode.dart'; | 11 import 'package:analyzer/src/task/strong_mode.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(); |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 PropertyAccessorElement getterB = classB.getGetter(fieldName); | 332 PropertyAccessorElement getterB = classB.getGetter(fieldName); |
332 expect(fieldB.type.isDynamic, isTrue); | 333 expect(fieldB.type.isDynamic, isTrue); |
333 expect(getterB.returnType.isDynamic, isTrue); | 334 expect(getterB.returnType.isDynamic, isTrue); |
334 | 335 |
335 inferrer.inferCompilationUnit(unit); | 336 inferrer.inferCompilationUnit(unit); |
336 | 337 |
337 expect(fieldB.type, fieldA.type); | 338 expect(fieldB.type, fieldA.type); |
338 expect(getterB.returnType, getterA.returnType); | 339 expect(getterB.returnType, getterA.returnType); |
339 } | 340 } |
340 | 341 |
| 342 void test_inferCompilationUnit_fieldFormal() { |
| 343 InstanceMemberInferrer inferrer = createInferrer; |
| 344 String fieldName = 'f'; |
| 345 CompilationUnitElement unit = resolve(''' |
| 346 class A { |
| 347 final $fieldName = 0; |
| 348 A([this.$fieldName = 'hello']); |
| 349 } |
| 350 '''); |
| 351 ClassElement classA = unit.getType('A'); |
| 352 FieldElement fieldA = classA.getField(fieldName); |
| 353 FieldFormalParameterElement paramA = |
| 354 classA.unnamedConstructor.parameters[0]; |
| 355 expect(fieldA.type.isDynamic, isTrue); |
| 356 expect(paramA.type.isDynamic, isTrue); |
| 357 |
| 358 inferrer.inferCompilationUnit(unit); |
| 359 |
| 360 DartType intType = inferrer.typeProvider.intType; |
| 361 expect(fieldA.type, intType); |
| 362 expect(paramA.type, intType); |
| 363 } |
| 364 |
341 void test_inferCompilationUnit_getter_multiple_different() { | 365 void test_inferCompilationUnit_getter_multiple_different() { |
342 InstanceMemberInferrer inferrer = createInferrer; | 366 InstanceMemberInferrer inferrer = createInferrer; |
343 String getterName = 'g'; | 367 String getterName = 'g'; |
344 CompilationUnitElement unit = resolve(''' | 368 CompilationUnitElement unit = resolve(''' |
345 class A { | 369 class A { |
346 int get $getterName => 0; | 370 int get $getterName => 0; |
347 } | 371 } |
348 class B { | 372 class B { |
349 double get $getterName => 0.0; | 373 double get $getterName => 0.0; |
350 } | 374 } |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 expect(getterB.returnType.isDynamic, isTrue); | 515 expect(getterB.returnType.isDynamic, isTrue); |
492 | 516 |
493 inferrer.inferCompilationUnit(unit); | 517 inferrer.inferCompilationUnit(unit); |
494 | 518 |
495 // Expected behavior is that the getter is inferred: getters and setters | 519 // Expected behavior is that the getter is inferred: getters and setters |
496 // are treated as independent methods. | 520 // are treated as independent methods. |
497 expect(fieldB.type, fieldA.type); | 521 expect(fieldB.type, fieldA.type); |
498 expect(getterB.returnType, getterA.returnType); | 522 expect(getterB.returnType, getterA.returnType); |
499 } | 523 } |
500 | 524 |
501 void test_inferCompilationUnit_setter_single() { | |
502 InstanceMemberInferrer inferrer = createInferrer; | |
503 String setterName = 'g'; | |
504 CompilationUnitElement unit = resolve(''' | |
505 class A { | |
506 set $setterName(int x) {} | |
507 } | |
508 class B extends A { | |
509 set $setterName(x) {} | |
510 } | |
511 '''); | |
512 ClassElement classA = unit.getType('A'); | |
513 FieldElement fieldA = classA.getField(setterName); | |
514 PropertyAccessorElement setterA = classA.getSetter(setterName); | |
515 ClassElement classB = unit.getType('B'); | |
516 FieldElement fieldB = classB.getField(setterName); | |
517 PropertyAccessorElement setterB = classB.getSetter(setterName); | |
518 expect(fieldB.type.isDynamic, isTrue); | |
519 expect(setterB.parameters[0].type.isDynamic, isTrue); | |
520 | |
521 inferrer.inferCompilationUnit(unit); | |
522 | |
523 expect(fieldB.type, fieldA.type); | |
524 expect(setterB.parameters[0].type, setterA.parameters[0].type); | |
525 } | |
526 | |
527 void test_inferCompilationUnit_setter_single_generic() { | |
528 InstanceMemberInferrer inferrer = createInferrer; | |
529 String setterName = 'g'; | |
530 CompilationUnitElement unit = resolve(''' | |
531 class A<E> { | |
532 set $setterName(E x) {} | |
533 } | |
534 class B<E> extends A<E> { | |
535 set $setterName(x) {} | |
536 } | |
537 '''); | |
538 ClassElement classB = unit.getType('B'); | |
539 DartType typeBE = classB.typeParameters[0].type; | |
540 FieldElement fieldB = classB.getField(setterName); | |
541 PropertyAccessorElement setterB = classB.getSetter(setterName); | |
542 expect(fieldB.type.isDynamic, isTrue); | |
543 expect(setterB.parameters[0].type.isDynamic, isTrue); | |
544 | |
545 inferrer.inferCompilationUnit(unit); | |
546 | |
547 expect(fieldB.type, typeBE); | |
548 expect(setterB.parameters[0].type, typeBE); | |
549 } | |
550 | |
551 void test_inferCompilationUnit_setter_single_inconsistentAccessors() { | |
552 InstanceMemberInferrer inferrer = createInferrer; | |
553 String getterName = 'g'; | |
554 CompilationUnitElement unit = resolve(''' | |
555 class A { | |
556 int get $getterName => 0; | |
557 set $getterName(String value) {} | |
558 } | |
559 class B extends A { | |
560 set $getterName(x) {} | |
561 } | |
562 '''); | |
563 ClassElement classA = unit.getType('A'); | |
564 PropertyAccessorElement setterA = classA.getSetter(getterName); | |
565 ClassElement classB = unit.getType('B'); | |
566 FieldElement fieldB = classB.getField(getterName); | |
567 PropertyAccessorElement setterB = classB.getSetter(getterName); | |
568 expect(fieldB.type.isDynamic, isTrue); | |
569 expect(setterB.parameters[0].type.isDynamic, isTrue); | |
570 | |
571 inferrer.inferCompilationUnit(unit); | |
572 | |
573 // Expected behavior is that the getter is inferred: getters and setters | |
574 // are treated as independent methods. | |
575 expect(setterB.parameters[0].type, setterA.parameters[0].type); | |
576 | |
577 // Note that B's synthetic field type will be String. This matches what | |
578 // resolver would do if we explicitly typed the parameter as 'String' | |
579 expect(fieldB.type, setterB.parameters[0].type); | |
580 } | |
581 | |
582 void test_inferCompilationUnit_invalid_inheritanceCycle() { | 525 void test_inferCompilationUnit_invalid_inheritanceCycle() { |
583 InstanceMemberInferrer inferrer = createInferrer; | 526 InstanceMemberInferrer inferrer = createInferrer; |
584 CompilationUnitElement unit = resolve(''' | 527 CompilationUnitElement unit = resolve(''' |
585 class A extends C {} | 528 class A extends C {} |
586 class B extends A {} | 529 class B extends A {} |
587 class C extends B {} | 530 class C extends B {} |
588 '''); | 531 '''); |
589 inferrer.inferCompilationUnit(unit); | 532 inferrer.inferCompilationUnit(unit); |
590 } | 533 } |
591 | 534 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 ClassElement classC = unit.getType('C'); | 625 ClassElement classC = unit.getType('C'); |
683 MethodElement methodC = classC.getMethod(methodName); | 626 MethodElement methodC = classC.getMethod(methodName); |
684 ParameterElement parameterC = methodC.parameters[0]; | 627 ParameterElement parameterC = methodC.parameters[0]; |
685 expect(parameterC.type.isDynamic, isTrue); | 628 expect(parameterC.type.isDynamic, isTrue); |
686 | 629 |
687 inferrer.inferCompilationUnit(unit); | 630 inferrer.inferCompilationUnit(unit); |
688 | 631 |
689 expect(parameterC.type.isDynamic, isTrue); | 632 expect(parameterC.type.isDynamic, isTrue); |
690 } | 633 } |
691 | 634 |
692 void test_inferCompilationUnit_method_parameter_multiple_optionalAndRequired()
{ | 635 void |
| 636 test_inferCompilationUnit_method_parameter_multiple_optionalAndRequired()
{ |
693 InstanceMemberInferrer inferrer = createInferrer; | 637 InstanceMemberInferrer inferrer = createInferrer; |
694 String methodName = 'm'; | 638 String methodName = 'm'; |
695 CompilationUnitElement unit = resolve(''' | 639 CompilationUnitElement unit = resolve(''' |
696 class A { | 640 class A { |
697 $methodName(int p) => 0; | 641 $methodName(int p) => 0; |
698 } | 642 } |
699 class B { | 643 class B { |
700 $methodName([int p]) => 0; | 644 $methodName([int p]) => 0; |
701 } | 645 } |
702 class C implements A, B { | 646 class C implements A, B { |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
948 expect(methodB.returnType.isDynamic, isTrue); | 892 expect(methodB.returnType.isDynamic, isTrue); |
949 expect(methodB.type.typeArguments, [typeBE]); | 893 expect(methodB.type.typeArguments, [typeBE]); |
950 | 894 |
951 inferrer.inferCompilationUnit(unit); | 895 inferrer.inferCompilationUnit(unit); |
952 | 896 |
953 expect(methodB.returnType, classB.typeParameters[0].type); | 897 expect(methodB.returnType, classB.typeParameters[0].type); |
954 expect(methodB.type.typeArguments, [typeBE], | 898 expect(methodB.type.typeArguments, [typeBE], |
955 reason: 'function type should still have type arguments'); | 899 reason: 'function type should still have type arguments'); |
956 } | 900 } |
957 | 901 |
958 void test_inferCompilationUnit_fieldFormal() { | 902 void test_inferCompilationUnit_setter_single() { |
959 InstanceMemberInferrer inferrer = createInferrer; | 903 InstanceMemberInferrer inferrer = createInferrer; |
960 String fieldName = 'f'; | 904 String setterName = 'g'; |
961 CompilationUnitElement unit = resolve(''' | 905 CompilationUnitElement unit = resolve(''' |
962 class A { | 906 class A { |
963 final $fieldName = 0; | 907 set $setterName(int x) {} |
964 A([this.$fieldName = 'hello']); | 908 } |
| 909 class B extends A { |
| 910 set $setterName(x) {} |
965 } | 911 } |
966 '''); | 912 '''); |
967 ClassElement classA = unit.getType('A'); | 913 ClassElement classA = unit.getType('A'); |
968 FieldElement fieldA = classA.getField(fieldName); | 914 FieldElement fieldA = classA.getField(setterName); |
969 FieldFormalParameterElement paramA = | 915 PropertyAccessorElement setterA = classA.getSetter(setterName); |
970 classA.unnamedConstructor.parameters[0]; | 916 ClassElement classB = unit.getType('B'); |
971 expect(fieldA.type.isDynamic, isTrue); | 917 FieldElement fieldB = classB.getField(setterName); |
972 expect(paramA.type.isDynamic, isTrue); | 918 PropertyAccessorElement setterB = classB.getSetter(setterName); |
| 919 expect(fieldB.type.isDynamic, isTrue); |
| 920 expect(setterB.parameters[0].type.isDynamic, isTrue); |
973 | 921 |
974 inferrer.inferCompilationUnit(unit); | 922 inferrer.inferCompilationUnit(unit); |
975 | 923 |
976 DartType intType = inferrer.typeProvider.intType; | 924 expect(fieldB.type, fieldA.type); |
977 expect(fieldA.type, intType); | 925 expect(setterB.parameters[0].type, setterA.parameters[0].type); |
978 expect(paramA.type, intType); | 926 } |
| 927 |
| 928 void test_inferCompilationUnit_setter_single_generic() { |
| 929 InstanceMemberInferrer inferrer = createInferrer; |
| 930 String setterName = 'g'; |
| 931 CompilationUnitElement unit = resolve(''' |
| 932 class A<E> { |
| 933 set $setterName(E x) {} |
| 934 } |
| 935 class B<E> extends A<E> { |
| 936 set $setterName(x) {} |
| 937 } |
| 938 '''); |
| 939 ClassElement classB = unit.getType('B'); |
| 940 DartType typeBE = classB.typeParameters[0].type; |
| 941 FieldElement fieldB = classB.getField(setterName); |
| 942 PropertyAccessorElement setterB = classB.getSetter(setterName); |
| 943 expect(fieldB.type.isDynamic, isTrue); |
| 944 expect(setterB.parameters[0].type.isDynamic, isTrue); |
| 945 |
| 946 inferrer.inferCompilationUnit(unit); |
| 947 |
| 948 expect(fieldB.type, typeBE); |
| 949 expect(setterB.parameters[0].type, typeBE); |
| 950 } |
| 951 |
| 952 void test_inferCompilationUnit_setter_single_inconsistentAccessors() { |
| 953 InstanceMemberInferrer inferrer = createInferrer; |
| 954 String getterName = 'g'; |
| 955 CompilationUnitElement unit = resolve(''' |
| 956 class A { |
| 957 int get $getterName => 0; |
| 958 set $getterName(String value) {} |
| 959 } |
| 960 class B extends A { |
| 961 set $getterName(x) {} |
| 962 } |
| 963 '''); |
| 964 ClassElement classA = unit.getType('A'); |
| 965 PropertyAccessorElement setterA = classA.getSetter(getterName); |
| 966 ClassElement classB = unit.getType('B'); |
| 967 FieldElement fieldB = classB.getField(getterName); |
| 968 PropertyAccessorElement setterB = classB.getSetter(getterName); |
| 969 expect(fieldB.type.isDynamic, isTrue); |
| 970 expect(setterB.parameters[0].type.isDynamic, isTrue); |
| 971 |
| 972 inferrer.inferCompilationUnit(unit); |
| 973 |
| 974 // Expected behavior is that the getter is inferred: getters and setters |
| 975 // are treated as independent methods. |
| 976 expect(setterB.parameters[0].type, setterA.parameters[0].type); |
| 977 |
| 978 // Note that B's synthetic field type will be String. This matches what |
| 979 // resolver would do if we explicitly typed the parameter as 'String' |
| 980 expect(fieldB.type, setterB.parameters[0].type); |
979 } | 981 } |
980 } | 982 } |
981 | 983 |
982 @reflectiveTest | 984 @reflectiveTest |
983 class VariableGathererTest extends AbstractContextTest { | 985 class VariableGathererTest extends AbstractContextTest { |
984 void test_creation_withFilter() { | 986 void test_creation_withFilter() { |
985 VariableFilter filter = (variable) => true; | 987 VariableFilter filter = (variable) => true; |
986 VariableGatherer gatherer = new VariableGatherer(filter); | 988 VariableGatherer gatherer = new VariableGatherer(filter); |
987 expect(gatherer, isNotNull); | 989 expect(gatherer, isNotNull); |
988 expect(gatherer.filter, filter); | 990 expect(gatherer.filter, filter); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1040 } | 1042 } |
1041 } | 1043 } |
1042 } | 1044 } |
1043 '''); | 1045 '''); |
1044 CompilationUnit unit = context.resolveCompilationUnit2(source, source); | 1046 CompilationUnit unit = context.resolveCompilationUnit2(source, source); |
1045 VariableGatherer gatherer = new VariableGatherer(filter); | 1047 VariableGatherer gatherer = new VariableGatherer(filter); |
1046 unit.accept(gatherer); | 1048 unit.accept(gatherer); |
1047 return gatherer.results; | 1049 return gatherer.results; |
1048 } | 1050 } |
1049 } | 1051 } |
OLD | NEW |