OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.dart.element.element_test; | 5 library analyzer.test.src.dart.element.element_test; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/constant/value.dart'; | 8 import 'package:analyzer/dart/constant/value.dart'; |
9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
10 import 'package:analyzer/dart/element/type.dart'; | 10 import 'package:analyzer/dart/element/type.dart'; |
(...skipping 946 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 ClassElementImpl classB = ElementFactory.classElement("B", classA.type); | 957 ClassElementImpl classB = ElementFactory.classElement("B", classA.type); |
958 classA.supertype = classB.type; | 958 classA.supertype = classB.type; |
959 (library.definingCompilationUnit as CompilationUnitElementImpl).types = | 959 (library.definingCompilationUnit as CompilationUnitElementImpl).types = |
960 <ClassElement>[classA, classB]; | 960 <ClassElement>[classA, classB]; |
961 expect(classA.lookUpSetter("s", library), isNull); | 961 expect(classA.lookUpSetter("s", library), isNull); |
962 } | 962 } |
963 } | 963 } |
964 | 964 |
965 @reflectiveTest | 965 @reflectiveTest |
966 class CompilationUnitElementImplTest extends EngineTestCase { | 966 class CompilationUnitElementImplTest extends EngineTestCase { |
967 void test_getElementAt() { | |
968 AnalysisContextHelper contextHelper = new AnalysisContextHelper(); | |
969 AnalysisContext context = contextHelper.context; | |
970 String code = r''' | |
971 class A { | |
972 int field; | |
973 } | |
974 main() { | |
975 int localVar = 42; | |
976 } | |
977 '''; | |
978 Source libSource = contextHelper.addSource("/my_lib.dart", code); | |
979 // prepare library/unit elements | |
980 LibraryElement libraryElement = context.computeLibraryElement(libSource); | |
981 CompilationUnitElement unitElement = libraryElement.definingCompilationUnit; | |
982 // A | |
983 ClassElement elementA; | |
984 { | |
985 int offset = code.indexOf('A {'); | |
986 elementA = unitElement.getElementAt(offset); | |
987 expect(elementA, isNotNull); | |
988 expect(elementA.enclosingElement, unitElement); | |
989 expect(elementA.name, 'A'); | |
990 } | |
991 // A.field | |
992 { | |
993 int offset = code.indexOf('field;'); | |
994 FieldElement element = unitElement.getElementAt(offset); | |
995 expect(element, isNotNull); | |
996 expect(element.enclosingElement, elementA); | |
997 expect(element.name, 'field'); | |
998 } | |
999 // main | |
1000 FunctionElement mainElement; | |
1001 { | |
1002 int offset = code.indexOf('main() {'); | |
1003 mainElement = unitElement.getElementAt(offset); | |
1004 expect(mainElement, isNotNull); | |
1005 expect(mainElement.enclosingElement, unitElement); | |
1006 expect(mainElement.name, 'main'); | |
1007 } | |
1008 // main.localVar | |
1009 { | |
1010 int offset = code.indexOf('localVar'); | |
1011 LocalVariableElement element = unitElement.getElementAt(offset); | |
1012 expect(element, isNotNull); | |
1013 expect(element.enclosingElement, mainElement); | |
1014 expect(element.name, 'localVar'); | |
1015 } | |
1016 // null | |
1017 expect(unitElement.getElementAt(1000), isNull); | |
1018 } | |
1019 | |
1020 void test_getElementAt_multipleUnitsInLibrary() { | |
1021 AnalysisContextHelper contextHelper = new AnalysisContextHelper(); | |
1022 AnalysisContext context = contextHelper.context; | |
1023 Source libSource = contextHelper.addSource( | |
1024 "/my_lib.dart", | |
1025 r''' | |
1026 library my_lib; | |
1027 part 'unit_a.dart'; | |
1028 part 'unit_b.dart'; | |
1029 '''); | |
1030 Source unitSourceA = | |
1031 contextHelper.addSource("/unit_a.dart", 'part of my_lib;class A {}'); | |
1032 Source unitSourceB = | |
1033 contextHelper.addSource("/unit_b.dart", 'part of my_lib;class B {}'); | |
1034 int offset = 'part of my_lib;class A {}'.indexOf('A {}'); | |
1035 // prepare library/unit elements | |
1036 context.computeLibraryElement(libSource); | |
1037 CompilationUnitElement unitElementA = | |
1038 context.getCompilationUnitElement(unitSourceA, libSource); | |
1039 CompilationUnitElement unitElementB = | |
1040 context.getCompilationUnitElement(unitSourceB, libSource); | |
1041 // A | |
1042 { | |
1043 ClassElement element = unitElementA.getElementAt(offset); | |
1044 expect(element, isNotNull); | |
1045 expect(element.enclosingElement, unitElementA); | |
1046 expect(element.name, 'A'); | |
1047 } | |
1048 // B | |
1049 { | |
1050 ClassElement element = unitElementB.getElementAt(offset); | |
1051 expect(element, isNotNull); | |
1052 expect(element.enclosingElement, unitElementB); | |
1053 expect(element.name, 'B'); | |
1054 } | |
1055 } | |
1056 | |
1057 void test_getEnum_declared() { | 967 void test_getEnum_declared() { |
1058 TestTypeProvider typeProvider = new TestTypeProvider(); | 968 TestTypeProvider typeProvider = new TestTypeProvider(); |
1059 CompilationUnitElementImpl unit = | 969 CompilationUnitElementImpl unit = |
1060 ElementFactory.compilationUnit("/lib.dart"); | 970 ElementFactory.compilationUnit("/lib.dart"); |
1061 String enumName = "E"; | 971 String enumName = "E"; |
1062 ClassElement enumElement = | 972 ClassElement enumElement = |
1063 ElementFactory.enumElement(typeProvider, enumName); | 973 ElementFactory.enumElement(typeProvider, enumName); |
1064 unit.enums = <ClassElement>[enumElement]; | 974 unit.enums = <ClassElement>[enumElement]; |
1065 expect(unit.getEnum(enumName), same(enumElement)); | 975 expect(unit.getEnum(enumName), same(enumElement)); |
1066 } | 976 } |
(...skipping 3440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4507 } | 4417 } |
4508 | 4418 |
4509 class _FunctionTypeImplTest_isSubtypeOf_baseCase_classFunction | 4419 class _FunctionTypeImplTest_isSubtypeOf_baseCase_classFunction |
4510 extends InterfaceTypeImpl { | 4420 extends InterfaceTypeImpl { |
4511 _FunctionTypeImplTest_isSubtypeOf_baseCase_classFunction(ClassElement arg0) | 4421 _FunctionTypeImplTest_isSubtypeOf_baseCase_classFunction(ClassElement arg0) |
4512 : super(arg0); | 4422 : super(arg0); |
4513 | 4423 |
4514 @override | 4424 @override |
4515 bool get isDartCoreFunction => true; | 4425 bool get isDartCoreFunction => true; |
4516 } | 4426 } |
OLD | NEW |