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

Side by Side Diff: pkg/analyzer/test/src/dart/analysis/driver_test.dart

Issue 3010463002: Make creating LibraryContext async, so all its transitive clients. (Closed)
Patch Set: tweak Created 3 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) 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 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; 8 import 'package:analyzer/dart/ast/standard_resolution_map.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 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 '''); 546 ''');
547 547
548 driver.addFile(lib); 548 driver.addFile(lib);
549 549
550 AnalysisResult libResult = await driver.getResult(lib); 550 AnalysisResult libResult = await driver.getResult(lib);
551 List<AnalysisError> errors = libResult.errors; 551 List<AnalysisError> errors = libResult.errors;
552 expect(errors, hasLength(1)); 552 expect(errors, hasLength(1));
553 expect(errors[0].errorCode, CompileTimeErrorCode.PART_OF_NON_PART); 553 expect(errors[0].errorCode, CompileTimeErrorCode.PART_OF_NON_PART);
554 } 554 }
555 555
556 test_asyncChangesDuringAnalysis_getErrors() async {
557 var path = _p('/test/lib/test.dart');
558 provider.newFile(path, 'class A {}');
559 driver.addFile(path);
560
561 // Compute and cache errors.
562 await driver.getErrors(path);
563 await scheduler.waitForIdle();
564
565 // Simulate a change that happens during reading the cached errors.
566 bool asyncWorkExecuted = false;
567 driver.test.workToWaitAfterComputingResult = (path) {
568 provider.updateFile(path, 'class B');
569 driver.changeFile(path);
570 asyncWorkExecuted = true;
571 };
572
573 ErrorsResult result = await driver.getErrors(testFile);
574 expect(asyncWorkExecuted, isTrue);
575 expect(result.errors, isNotEmpty);
576 }
577
578 test_asyncChangesDuringAnalysis_getResult() async {
579 var path = _p('/test/lib/test.dart');
580 provider.newFile(path, 'class A {}');
581 driver.addFile(path);
582
583 // Schedule the result to be computed.
584 Future<AnalysisResult> future1 = driver.getResult(testFile);
585
586 // Simulate a change that happens during computing the result.
587 // We also request a new result, which must include the change.
588 Future<AnalysisResult> future2;
589 bool asyncWorkExecuted = false;
590 driver.test.workToWaitAfterComputingResult = (path) {
591 provider.updateFile(path, 'class B {}');
592 driver.changeFile(path);
593 future2 = driver.getResult(testFile);
594 asyncWorkExecuted = true;
595 };
596
597 // Both futures complete, with the same result.
598 // The result must be with the new changes.
599 //
600 // It would not be wrong to have "class A {}" in result1, and "class B {}"
601 // in result2, but we test here the actual implementation behaviour.
602 AnalysisResult result1 = await future1;
603 AnalysisResult result2 = await future2;
604 expect(asyncWorkExecuted, isTrue);
605 expect(result2, same(result1));
606 expect(result1.path, testFile);
607 expect(result1.unit, isNotNull);
608 expect((result1.unit.declarations[0] as ClassDeclaration).name.name, 'B');
609 }
610
611 test_asyncChangesDuringAnalysis_resultsStream() async {
612 var path = _p('/test/lib/test.dart');
613 provider.newFile(path, 'class A {}');
614 driver.addFile(path);
615
616 // Simulate a change that happens during computing the result.
617 bool asyncWorkExecuted = false;
618 driver.test.workToWaitAfterComputingResult = (p) {
619 if (p == path && !asyncWorkExecuted) {
620 provider.updateFile(path, 'class B');
621 driver.changeFile(path);
622 asyncWorkExecuted = true;
623 }
624 };
625
626 await scheduler.waitForIdle();
627 expect(asyncWorkExecuted, isTrue);
628
629 // The last result must have an error.
630 expect(allResults.last.errors, isNotEmpty);
631 }
632
633 test_asyncChangesDuringAnalysis_resultsStream_priority() async {
634 var path = _p('/test/lib/test.dart');
635 provider.newFile(path, 'class A {}');
636 driver.addFile(path);
637 driver.priorityFiles = [path];
638
639 // Simulate a change that happens during computing the result.
640 bool asyncWorkExecuted = false;
641 driver.test.workToWaitAfterComputingResult = (p) {
642 if (p == path && !asyncWorkExecuted) {
643 provider.updateFile(path, 'class B {}');
644 driver.changeFile(path);
645 asyncWorkExecuted = true;
646 }
647 };
648
649 await scheduler.waitForIdle();
650 expect(asyncWorkExecuted, isTrue);
651
652 // The last unit must have "class B {}".
653 var lastUnit = allResults.last.unit;
654 expect((lastUnit.declarations[0] as ClassDeclaration).name.name, 'B');
655 }
656
556 test_cachedPriorityResults() async { 657 test_cachedPriorityResults() async {
557 var a = _p('/test/bin/a.dart'); 658 var a = _p('/test/bin/a.dart');
558 provider.newFile(a, 'var a = 1;'); 659 provider.newFile(a, 'var a = 1;');
559 660
560 driver.priorityFiles = [a]; 661 driver.priorityFiles = [a];
561 662
562 AnalysisResult result1 = await driver.getResult(a); 663 AnalysisResult result1 = await driver.getResult(a);
563 expect(driver.test.priorityResults, containsPair(a, result1)); 664 expect(driver.test.priorityResults, containsPair(a, result1));
564 665
565 await scheduler.waitForIdle(); 666 await scheduler.waitForIdle();
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 provider.newFile(a, r''' 1071 provider.newFile(a, r'''
971 class A {} 1072 class A {}
972 '''); 1073 ''');
973 provider.newFile(b, r''' 1074 provider.newFile(b, r'''
974 import 'a.dart'; 1075 import 'a.dart';
975 var a = new A(); 1076 var a = new A();
976 '''); 1077 ''');
977 1078
978 // Prepare the store with a.dart and everything it needs. 1079 // Prepare the store with a.dart and everything it needs.
979 SummaryDataStore summaryStore = 1080 SummaryDataStore summaryStore =
980 createAnalysisDriver().test.getSummaryStore(a); 1081 await createAnalysisDriver().test.getSummaryStore(a);
981 1082
982 // There are at least a.dart and dart:core libraries. 1083 // There are at least a.dart and dart:core libraries.
983 String aUri = provider.pathContext.toUri(a).toString(); 1084 String aUri = provider.pathContext.toUri(a).toString();
984 expect(summaryStore.unlinkedMap.keys, contains(aUri)); 1085 expect(summaryStore.unlinkedMap.keys, contains(aUri));
985 expect(summaryStore.linkedMap.keys, contains(aUri)); 1086 expect(summaryStore.linkedMap.keys, contains(aUri));
986 expect(summaryStore.unlinkedMap.keys, contains('dart:core')); 1087 expect(summaryStore.unlinkedMap.keys, contains('dart:core'));
987 expect(summaryStore.linkedMap.keys, contains('dart:core')); 1088 expect(summaryStore.linkedMap.keys, contains('dart:core'));
988 1089
989 // Remove a.dart from the file system. 1090 // Remove a.dart from the file system.
990 provider.deleteFile(a); 1091 provider.deleteFile(a);
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
1150 expect(fooId, isNonNegative); 1251 expect(fooId, isNonNegative);
1151 } 1252 }
1152 1253
1153 test_getLibraryByUri_external_resynthesize() async { 1254 test_getLibraryByUri_external_resynthesize() async {
1154 provider.newFile(testFile, r''' 1255 provider.newFile(testFile, r'''
1155 class Test {} 1256 class Test {}
1156 '''); 1257 ''');
1157 1258
1158 // Prepare the store with package:test/test.dart URI. 1259 // Prepare the store with package:test/test.dart URI.
1159 SummaryDataStore summaryStore = 1260 SummaryDataStore summaryStore =
1160 createAnalysisDriver().test.getSummaryStore(testFile); 1261 await createAnalysisDriver().test.getSummaryStore(testFile);
1161 1262
1162 // package:test/test.dart is in the store. 1263 // package:test/test.dart is in the store.
1163 String uri = 'package:test/test.dart'; 1264 String uri = 'package:test/test.dart';
1164 expect(summaryStore.unlinkedMap.keys, contains(uri)); 1265 expect(summaryStore.unlinkedMap.keys, contains(uri));
1165 expect(summaryStore.linkedMap.keys, contains(uri)); 1266 expect(summaryStore.linkedMap.keys, contains(uri));
1166 1267
1167 // Remove the file from the file system. 1268 // Remove the file from the file system.
1168 provider.deleteFile(testFile); 1269 provider.deleteFile(testFile);
1169 1270
1170 // We can resynthesize the library from the store without reading the file. 1271 // We can resynthesize the library from the store without reading the file.
1171 AnalysisDriver driver = 1272 AnalysisDriver driver =
1172 createAnalysisDriver(externalSummaries: summaryStore); 1273 createAnalysisDriver(externalSummaries: summaryStore);
1173 expect(driver.test.numOfCreatedLibraryContexts, 0); 1274 expect(driver.test.numOfCreatedLibraryContexts, 0);
1174 LibraryElement library = await driver.getLibraryByUri(uri); 1275 LibraryElement library = await driver.getLibraryByUri(uri);
1175 expect(library.getType('Test'), isNotNull); 1276 expect(library.getType('Test'), isNotNull);
1176 } 1277 }
1177 1278
1178 test_getLibraryByUri_sdk_analyze() async { 1279 test_getLibraryByUri_sdk_analyze() async {
1179 LibraryElement coreLibrary = await driver.getLibraryByUri('dart:core'); 1280 LibraryElement coreLibrary = await driver.getLibraryByUri('dart:core');
1180 expect(coreLibrary, isNotNull); 1281 expect(coreLibrary, isNotNull);
1181 expect(coreLibrary.getType('Object'), isNotNull); 1282 expect(coreLibrary.getType('Object'), isNotNull);
1182 expect(coreLibrary.getType('int'), isNotNull); 1283 expect(coreLibrary.getType('int'), isNotNull);
1183 } 1284 }
1184 1285
1185 test_getLibraryByUri_sdk_resynthesize() async { 1286 test_getLibraryByUri_sdk_resynthesize() async {
1186 SummaryDataStore sdkStore; 1287 SummaryDataStore sdkStore;
1187 { 1288 {
1188 String corePath = sdk.mapDartUri('dart:core').fullName; 1289 String corePath = sdk.mapDartUri('dart:core').fullName;
1189 sdkStore = createAnalysisDriver().test.getSummaryStore(corePath); 1290 sdkStore = await createAnalysisDriver().test.getSummaryStore(corePath);
1190 } 1291 }
1191 1292
1192 // There are dart:core and dart:async in the store. 1293 // There are dart:core and dart:async in the store.
1193 expect(sdkStore.unlinkedMap.keys, contains('dart:core')); 1294 expect(sdkStore.unlinkedMap.keys, contains('dart:core'));
1194 expect(sdkStore.unlinkedMap.keys, contains('dart:async')); 1295 expect(sdkStore.unlinkedMap.keys, contains('dart:async'));
1195 expect(sdkStore.linkedMap.keys, contains('dart:core')); 1296 expect(sdkStore.linkedMap.keys, contains('dart:core'));
1196 expect(sdkStore.linkedMap.keys, contains('dart:async')); 1297 expect(sdkStore.linkedMap.keys, contains('dart:async'));
1197 1298
1198 // We don't create new library context (so, don't parse, summarize and 1299 // We don't create new library context (so, don't parse, summarize and
1199 // link) for dart:core. The library is resynthesized from the provided 1300 // link) for dart:core. The library is resynthesized from the provided
(...skipping 1543 matching lines...) Expand 10 before | Expand all | Expand 10 after
2743 var path = _p('/test.dart'); 2844 var path = _p('/test.dart');
2744 expect(() { 2845 expect(() {
2745 driver.removeFile(path); 2846 driver.removeFile(path);
2746 }, throwsStateError); 2847 }, throwsStateError);
2747 } 2848 }
2748 2849
2749 String _p(String path) => provider.convertPath(path); 2850 String _p(String path) => provider.convertPath(path);
2750 } 2851 }
2751 2852
2752 class _SourceMock extends Mock implements Source {} 2853 class _SourceMock extends Mock implements Source {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698