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.context.context_test; | 5 library analyzer.test.src.context.context_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
(...skipping 1145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1156 } | 1156 } |
1157 return false; | 1157 return false; |
1158 }); | 1158 }); |
1159 // Update the file, but don't notify the context. | 1159 // Update the file, but don't notify the context. |
1160 resourceProvider.updateFile(path, newCode); | 1160 resourceProvider.updateFile(path, newCode); |
1161 // Driver must detect that the file was changed and recover. | 1161 // Driver must detect that the file was changed and recover. |
1162 CompilationUnit unit = context.resolveCompilationUnit2(source, source); | 1162 CompilationUnit unit = context.resolveCompilationUnit2(source, source); |
1163 expect(unit, isNotNull); | 1163 expect(unit, isNotNull); |
1164 } | 1164 } |
1165 | 1165 |
| 1166 void test_flushSingleResolvedUnit_instanceField() { |
| 1167 _checkFlushSingleResolvedUnit('class C { var x = 0; }', |
| 1168 (CompilationUnitElement unitElement, String reason) { |
| 1169 expect(unitElement.types, hasLength(1), reason: reason); |
| 1170 ClassElement cls = unitElement.types[0]; |
| 1171 expect(cls.fields, hasLength(1), reason: reason); |
| 1172 expect(cls.fields[0].type.toString(), 'int', reason: reason); |
| 1173 expect(cls.accessors, hasLength(2), reason: reason); |
| 1174 expect(cls.accessors[0].isGetter, isTrue, reason: reason); |
| 1175 expect(cls.accessors[0].returnType.toString(), 'int', reason: reason); |
| 1176 expect(cls.accessors[1].isSetter, isTrue, reason: reason); |
| 1177 expect(cls.accessors[1].returnType.toString(), 'void', reason: reason); |
| 1178 expect(cls.accessors[1].parameters, hasLength(1), reason: reason); |
| 1179 expect(cls.accessors[1].parameters[0].type.toString(), 'int', |
| 1180 reason: reason); |
| 1181 }); |
| 1182 } |
| 1183 |
| 1184 void test_flushSingleResolvedUnit_instanceGetter() { |
| 1185 _checkFlushSingleResolvedUnit( |
| 1186 ''' |
| 1187 abstract class B { |
| 1188 int get x; |
| 1189 } |
| 1190 class C extends B { |
| 1191 get x => null; |
| 1192 } |
| 1193 ''', (CompilationUnitElement unitElement, String reason) { |
| 1194 expect(unitElement.types, hasLength(2), reason: reason); |
| 1195 ClassElement cls = unitElement.types[1]; |
| 1196 expect(cls.name, 'C', reason: reason); |
| 1197 expect(cls.accessors, hasLength(1), reason: reason); |
| 1198 expect(cls.accessors[0].returnType.toString(), 'int', reason: reason); |
| 1199 expect(cls.fields, hasLength(1), reason: reason); |
| 1200 expect(cls.fields[0].type.toString(), 'int', reason: reason); |
| 1201 }); |
| 1202 } |
| 1203 |
| 1204 void test_flushSingleResolvedUnit_instanceMethod() { |
| 1205 _checkFlushSingleResolvedUnit( |
| 1206 ''' |
| 1207 abstract class B { |
| 1208 int f(String s); |
| 1209 } |
| 1210 class C extends B { |
| 1211 f(s) => null; |
| 1212 } |
| 1213 ''', (CompilationUnitElement unitElement, String reason) { |
| 1214 expect(unitElement.types, hasLength(2), reason: reason); |
| 1215 ClassElement cls = unitElement.types[1]; |
| 1216 expect(cls.name, 'C', reason: reason); |
| 1217 expect(cls.methods, hasLength(1), reason: reason); |
| 1218 expect(cls.methods[0].returnType.toString(), 'int', reason: reason); |
| 1219 expect(cls.methods[0].parameters, hasLength(1), reason: reason); |
| 1220 expect(cls.methods[0].parameters[0].type.toString(), 'String', |
| 1221 reason: reason); |
| 1222 }); |
| 1223 } |
| 1224 |
| 1225 void test_flushSingleResolvedUnit_instanceSetter() { |
| 1226 _checkFlushSingleResolvedUnit( |
| 1227 ''' |
| 1228 abstract class B { |
| 1229 set x(int value); |
| 1230 } |
| 1231 class C extends B { |
| 1232 set x(value) {} |
| 1233 } |
| 1234 ''', (CompilationUnitElement unitElement, String reason) { |
| 1235 expect(unitElement.types, hasLength(2), reason: reason); |
| 1236 ClassElement cls = unitElement.types[1]; |
| 1237 expect(cls.name, 'C', reason: reason); |
| 1238 expect(cls.accessors, hasLength(1), reason: reason); |
| 1239 expect(cls.accessors[0].returnType.toString(), 'void', reason: reason); |
| 1240 expect(cls.accessors[0].parameters, hasLength(1), reason: reason); |
| 1241 expect(cls.accessors[0].parameters[0].type.toString(), 'int', |
| 1242 reason: reason); |
| 1243 expect(cls.fields, hasLength(1), reason: reason); |
| 1244 expect(cls.fields[0].type.toString(), 'int', reason: reason); |
| 1245 }); |
| 1246 } |
| 1247 |
| 1248 void test_flushSingleResolvedUnit_staticField() { |
| 1249 _checkFlushSingleResolvedUnit('class C { static var x = 0; }', |
| 1250 (CompilationUnitElement unitElement, String reason) { |
| 1251 expect(unitElement.types, hasLength(1), reason: reason); |
| 1252 ClassElement cls = unitElement.types[0]; |
| 1253 expect(cls.fields, hasLength(1), reason: reason); |
| 1254 expect(cls.fields[0].type.toString(), 'int', reason: reason); |
| 1255 expect(cls.accessors, hasLength(2), reason: reason); |
| 1256 expect(cls.accessors[0].isGetter, isTrue, reason: reason); |
| 1257 expect(cls.accessors[0].returnType.toString(), 'int', reason: reason); |
| 1258 expect(cls.accessors[1].isSetter, isTrue, reason: reason); |
| 1259 expect(cls.accessors[1].returnType.toString(), 'void', reason: reason); |
| 1260 expect(cls.accessors[1].parameters, hasLength(1), reason: reason); |
| 1261 expect(cls.accessors[1].parameters[0].type.toString(), 'int', |
| 1262 reason: reason); |
| 1263 }); |
| 1264 } |
| 1265 |
| 1266 void test_flushSingleResolvedUnit_topLevelVariable() { |
| 1267 _checkFlushSingleResolvedUnit('var x = 0;', |
| 1268 (CompilationUnitElement unitElement, String reason) { |
| 1269 expect(unitElement.topLevelVariables, hasLength(1), reason: reason); |
| 1270 expect(unitElement.topLevelVariables[0].type.toString(), 'int', |
| 1271 reason: reason); |
| 1272 expect(unitElement.accessors, hasLength(2), reason: reason); |
| 1273 expect(unitElement.accessors[0].isGetter, isTrue, reason: reason); |
| 1274 expect(unitElement.accessors[0].returnType.toString(), 'int', |
| 1275 reason: reason); |
| 1276 expect(unitElement.accessors[1].isSetter, isTrue, reason: reason); |
| 1277 expect(unitElement.accessors[1].returnType.toString(), 'void', |
| 1278 reason: reason); |
| 1279 expect(unitElement.accessors[1].parameters, hasLength(1), reason: reason); |
| 1280 expect(unitElement.accessors[1].parameters[0].type.toString(), 'int', |
| 1281 reason: reason); |
| 1282 }); |
| 1283 } |
| 1284 |
1166 void test_getAnalysisOptions() { | 1285 void test_getAnalysisOptions() { |
1167 expect(context.analysisOptions, isNotNull); | 1286 expect(context.analysisOptions, isNotNull); |
1168 } | 1287 } |
1169 | 1288 |
1170 void test_getContents_fromSource() { | 1289 void test_getContents_fromSource() { |
1171 String content = "library lib;"; | 1290 String content = "library lib;"; |
1172 TimestampedData<String> contents = | 1291 TimestampedData<String> contents = |
1173 context.getContents(new TestSource('/test.dart', content)); | 1292 context.getContents(new TestSource('/test.dart', content)); |
1174 expect(contents.data.toString(), content); | 1293 expect(contents.data.toString(), content); |
1175 } | 1294 } |
(...skipping 1346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2522 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}"); | 2641 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}"); |
2523 addSource("/libB.dart", "library libB; import 'libA.dart'; class B{}"); | 2642 addSource("/libB.dart", "library libB; import 'libA.dart'; class B{}"); |
2524 CompilationUnit compilationUnit = | 2643 CompilationUnit compilationUnit = |
2525 context.resolveCompilationUnit2(sourceA, sourceA); | 2644 context.resolveCompilationUnit2(sourceA, sourceA); |
2526 expect(compilationUnit, isNotNull); | 2645 expect(compilationUnit, isNotNull); |
2527 LibraryElement library = compilationUnit.element.library; | 2646 LibraryElement library = compilationUnit.element.library; |
2528 List<LibraryElement> importedLibraries = library.importedLibraries; | 2647 List<LibraryElement> importedLibraries = library.importedLibraries; |
2529 assertNamedElements(importedLibraries, ["dart.core", "libB"]); | 2648 assertNamedElements(importedLibraries, ["dart.core", "libB"]); |
2530 } | 2649 } |
2531 | 2650 |
| 2651 void test_resolveCompilationUnit_library() { |
| 2652 Source source = addSource("/lib.dart", "library lib;"); |
| 2653 LibraryElement library = context.computeLibraryElement(source); |
| 2654 CompilationUnit compilationUnit = |
| 2655 context.resolveCompilationUnit(source, library); |
| 2656 expect(compilationUnit, isNotNull); |
| 2657 expect(compilationUnit.element, isNotNull); |
| 2658 } |
| 2659 |
2532 // void test_resolveCompilationUnit_sourceChangeDuringResolution() { | 2660 // void test_resolveCompilationUnit_sourceChangeDuringResolution() { |
2533 // _context = new _AnalysisContext_sourceChangeDuringResolution(); | 2661 // _context = new _AnalysisContext_sourceChangeDuringResolution(); |
2534 // AnalysisContextFactory.initContextWithCore(_context); | 2662 // AnalysisContextFactory.initContextWithCore(_context); |
2535 // _sourceFactory = _context.sourceFactory; | 2663 // _sourceFactory = _context.sourceFactory; |
2536 // Source source = _addSource("/lib.dart", "library lib;"); | 2664 // Source source = _addSource("/lib.dart", "library lib;"); |
2537 // CompilationUnit compilationUnit = | 2665 // CompilationUnit compilationUnit = |
2538 // _context.resolveCompilationUnit2(source, source); | 2666 // _context.resolveCompilationUnit2(source, source); |
2539 // expect(compilationUnit, isNotNull); | 2667 // expect(compilationUnit, isNotNull); |
2540 // expect(_context.getLineInfo(source), isNotNull); | 2668 // expect(_context.getLineInfo(source), isNotNull); |
2541 // } | 2669 // } |
2542 | 2670 |
2543 void test_resolveCompilationUnit_library() { | |
2544 Source source = addSource("/lib.dart", "library lib;"); | |
2545 LibraryElement library = context.computeLibraryElement(source); | |
2546 CompilationUnit compilationUnit = | |
2547 context.resolveCompilationUnit(source, library); | |
2548 expect(compilationUnit, isNotNull); | |
2549 expect(compilationUnit.element, isNotNull); | |
2550 } | |
2551 | |
2552 void test_resolveCompilationUnit_source() { | 2671 void test_resolveCompilationUnit_source() { |
2553 Source source = addSource("/lib.dart", "library lib;"); | 2672 Source source = addSource("/lib.dart", "library lib;"); |
2554 CompilationUnit compilationUnit = | 2673 CompilationUnit compilationUnit = |
2555 context.resolveCompilationUnit2(source, source); | 2674 context.resolveCompilationUnit2(source, source); |
2556 expect(compilationUnit, isNotNull); | 2675 expect(compilationUnit, isNotNull); |
2557 } | 2676 } |
2558 | 2677 |
2559 void test_setAnalysisOptions() { | 2678 void test_setAnalysisOptions() { |
2560 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 2679 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
2561 options.dart2jsHint = false; | 2680 options.dart2jsHint = false; |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2822 } | 2941 } |
2823 } | 2942 } |
2824 | 2943 |
2825 void _changeSource(TestSource source, String contents) { | 2944 void _changeSource(TestSource source, String contents) { |
2826 source.setContents(contents); | 2945 source.setContents(contents); |
2827 ChangeSet changeSet = new ChangeSet(); | 2946 ChangeSet changeSet = new ChangeSet(); |
2828 changeSet.changedSource(source); | 2947 changeSet.changedSource(source); |
2829 context.applyChanges(changeSet); | 2948 context.applyChanges(changeSet); |
2830 } | 2949 } |
2831 | 2950 |
| 2951 void _checkFlushSingleResolvedUnit(String code, |
| 2952 void validate(CompilationUnitElement unitElement, String reason)) { |
| 2953 prepareAnalysisContext(new AnalysisOptionsImpl()..strongMode = true); |
| 2954 String path = resourceProvider.convertPath('/test.dart'); |
| 2955 Source source = resourceProvider.newFile(path, code).createSource(); |
| 2956 context.applyChanges(new ChangeSet()..addedSource(source)); |
| 2957 CompilationUnitElement unitElement = |
| 2958 context.resolveCompilationUnit2(source, source).element; |
| 2959 validate(unitElement, 'initial state'); |
| 2960 for (ResultDescriptor<CompilationUnit> descriptor |
| 2961 in RESOLVED_UNIT_RESULTS) { |
| 2962 context.analysisCache.flush( |
| 2963 (target, result) => target.source == source && result == descriptor); |
| 2964 context.computeResult( |
| 2965 new LibrarySpecificUnit(source, source), descriptor); |
| 2966 validate(unitElement, 'after flushing $descriptor'); |
| 2967 } |
| 2968 } |
| 2969 |
2832 /** | 2970 /** |
2833 * Search the given compilation unit for a class with the given name. Return t
he class with the | 2971 * Search the given compilation unit for a class with the given name. Return t
he class with the |
2834 * given name, or `null` if the class cannot be found. | 2972 * given name, or `null` if the class cannot be found. |
2835 * | 2973 * |
2836 * @param unit the compilation unit being searched | 2974 * @param unit the compilation unit being searched |
2837 * @param className the name of the class being searched for | 2975 * @param className the name of the class being searched for |
2838 * @return the class with the given name | 2976 * @return the class with the given name |
2839 */ | 2977 */ |
2840 ClassElement _findClass(CompilationUnitElement unit, String className) { | 2978 ClassElement _findClass(CompilationUnitElement unit, String className) { |
2841 for (ClassElement classElement in unit.types) { | 2979 for (ClassElement classElement in unit.types) { |
(...skipping 2451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5293 * Initialize the visitor. | 5431 * Initialize the visitor. |
5294 */ | 5432 */ |
5295 _ElementGatherer(); | 5433 _ElementGatherer(); |
5296 | 5434 |
5297 @override | 5435 @override |
5298 void visitElement(Element element) { | 5436 void visitElement(Element element) { |
5299 elements[element] = element; | 5437 elements[element] = element; |
5300 super.visitElement(element); | 5438 super.visitElement(element); |
5301 } | 5439 } |
5302 } | 5440 } |
OLD | NEW |