| 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 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 changeSet2.changedRange(source, 'library test;', 0, 0, 13); | 313 changeSet2.changedRange(source, 'library test;', 0, 0, 13); |
| 314 context.applyChanges(changeSet2); | 314 context.applyChanges(changeSet2); |
| 315 return pumpEventQueue().then((_) { | 315 return pumpEventQueue().then((_) { |
| 316 listener.assertEvent(wereSourcesAdded: true); | 316 listener.assertEvent(wereSourcesAdded: true); |
| 317 listener.assertEvent(wereSourcesAdded: true, changedSources: [source]); | 317 listener.assertEvent(wereSourcesAdded: true, changedSources: [source]); |
| 318 listener.assertNoMoreEvents(); | 318 listener.assertNoMoreEvents(); |
| 319 }); | 319 }); |
| 320 } | 320 } |
| 321 | 321 |
| 322 void test_applyChanges_changedSource_updateModificationTime() { | 322 void test_applyChanges_changedSource_updateModificationTime() { |
| 323 String path = '/test.dart'; | 323 String path = resourceProvider.convertPath('/test.dart'); |
| 324 File file = resourceProvider.newFile(path, 'var V = 1;'); | 324 File file = resourceProvider.newFile(path, 'var V = 1;'); |
| 325 Source source = file.createSource(); | 325 Source source = file.createSource(); |
| 326 context.applyChanges(new ChangeSet()..addedSource(source)); | 326 context.applyChanges(new ChangeSet()..addedSource(source)); |
| 327 // Analyze all. | 327 // Analyze all. |
| 328 _analyzeAll_assertFinished(); | 328 _analyzeAll_assertFinished(); |
| 329 expect(context.analysisCache.getState(source, RESOLVED_UNIT), | 329 expect(context.analysisCache.getState(source, RESOLVED_UNIT), |
| 330 CacheState.INVALID); | 330 CacheState.INVALID); |
| 331 // Update the file and notify the context about the change. | 331 // Update the file and notify the context about the change. |
| 332 resourceProvider.updateFile(path, 'var V = 2;'); | 332 resourceProvider.updateFile(path, 'var V = 2;'); |
| 333 context.applyChanges(new ChangeSet()..changedSource(source)); | 333 context.applyChanges(new ChangeSet()..changedSource(source)); |
| (...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1110 void test_exists_true() { | 1110 void test_exists_true() { |
| 1111 expect(context.exists(new _AnalysisContextImplTest_Source_exists_true()), | 1111 expect(context.exists(new _AnalysisContextImplTest_Source_exists_true()), |
| 1112 isTrue); | 1112 isTrue); |
| 1113 } | 1113 } |
| 1114 | 1114 |
| 1115 void test_flushResolvedUnit_updateFile_dontNotify() { | 1115 void test_flushResolvedUnit_updateFile_dontNotify() { |
| 1116 String oldCode = ''; | 1116 String oldCode = ''; |
| 1117 String newCode = r''' | 1117 String newCode = r''' |
| 1118 import 'dart:async'; | 1118 import 'dart:async'; |
| 1119 '''; | 1119 '''; |
| 1120 String path = '/test.dart'; | 1120 String path = resourceProvider.convertPath('/test.dart'); |
| 1121 Source source = resourceProvider.newFile(path, oldCode).createSource(); | 1121 Source source = resourceProvider.newFile(path, oldCode).createSource(); |
| 1122 context.applyChanges(new ChangeSet()..addedSource(source)); | 1122 context.applyChanges(new ChangeSet()..addedSource(source)); |
| 1123 context.resolveCompilationUnit2(source, source); | 1123 context.resolveCompilationUnit2(source, source); |
| 1124 // Flush all results units. | 1124 // Flush all results units. |
| 1125 context.analysisCache.flush((target, result) { | 1125 context.analysisCache.flush((target, result) { |
| 1126 if (target.source == source) { | 1126 if (target.source == source) { |
| 1127 return RESOLVED_UNIT_RESULTS.contains(result); | 1127 return RESOLVED_UNIT_RESULTS.contains(result); |
| 1128 } | 1128 } |
| 1129 return false; | 1129 return false; |
| 1130 }); | 1130 }); |
| 1131 // Update the file, but don't notify the context. | 1131 // Update the file, but don't notify the context. |
| 1132 resourceProvider.updateFile(path, newCode); | 1132 resourceProvider.updateFile(path, newCode); |
| 1133 // Driver must detect that the file was changed and recover. | 1133 // Driver must detect that the file was changed and recover. |
| 1134 CompilationUnit unit = context.resolveCompilationUnit2(source, source); | 1134 CompilationUnit unit = context.resolveCompilationUnit2(source, source); |
| 1135 expect(unit, isNotNull); | 1135 expect(unit, isNotNull); |
| 1136 } | 1136 } |
| 1137 | 1137 |
| 1138 void test_flushResolvedUnit_updateFile_dontNotify2() { | 1138 void test_flushResolvedUnit_updateFile_dontNotify2() { |
| 1139 String oldCode = r''' | 1139 String oldCode = r''' |
| 1140 main() {} | 1140 main() {} |
| 1141 '''; | 1141 '''; |
| 1142 String newCode = r''' | 1142 String newCode = r''' |
| 1143 import 'dart:async'; | 1143 import 'dart:async'; |
| 1144 main() {} | 1144 main() {} |
| 1145 '''; | 1145 '''; |
| 1146 String path = '/test.dart'; | 1146 String path = resourceProvider.convertPath('/test.dart'); |
| 1147 Source source = resourceProvider.newFile(path, oldCode).createSource(); | 1147 Source source = resourceProvider.newFile(path, oldCode).createSource(); |
| 1148 context.applyChanges(new ChangeSet()..addedSource(source)); | 1148 context.applyChanges(new ChangeSet()..addedSource(source)); |
| 1149 context.resolveCompilationUnit2(source, source); | 1149 context.resolveCompilationUnit2(source, source); |
| 1150 // Flush all results units. | 1150 // Flush all results units. |
| 1151 context.analysisCache.flush((target, result) { | 1151 context.analysisCache.flush((target, result) { |
| 1152 if (target.source == source) { | 1152 if (target.source == source) { |
| 1153 if (target.source == source) { | 1153 if (target.source == source) { |
| 1154 return RESOLVED_UNIT_RESULTS.contains(result); | 1154 return RESOLVED_UNIT_RESULTS.contains(result); |
| 1155 } | 1155 } |
| 1156 } | 1156 } |
| (...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1745 Source source = file.createSource(); | 1745 Source source = file.createSource(); |
| 1746 context.applyChanges(new ChangeSet()..addedSource(source)); | 1746 context.applyChanges(new ChangeSet()..addedSource(source)); |
| 1747 _analyzeAll_assertFinished(); | 1747 _analyzeAll_assertFinished(); |
| 1748 expect(context.getResolvedCompilationUnit2(source, source), isNotNull); | 1748 expect(context.getResolvedCompilationUnit2(source, source), isNotNull); |
| 1749 // Test for the race condition. | 1749 // Test for the race condition. |
| 1750 // 1. Update the file. | 1750 // 1. Update the file. |
| 1751 // 2. Update the content cache. | 1751 // 2. Update the content cache. |
| 1752 // 3. Notify the context, and because this is the first time when we | 1752 // 3. Notify the context, and because this is the first time when we |
| 1753 // update the content cache, we don't know "originalContents". | 1753 // update the content cache, we don't know "originalContents". |
| 1754 // The source must be invalidated, because it has different contents now. | 1754 // The source must be invalidated, because it has different contents now. |
| 1755 resourceProvider.updateFile('/test.dart', newCode); | 1755 resourceProvider.updateFile( |
| 1756 resourceProvider.convertPath('/test.dart'), newCode); |
| 1756 contentCache.setContents(source, newCode); | 1757 contentCache.setContents(source, newCode); |
| 1757 context.handleContentsChanged(source, null, newCode, true); | 1758 context.handleContentsChanged(source, null, newCode, true); |
| 1758 expect(context.getResolvedCompilationUnit2(source, source), isNull); | 1759 expect(context.getResolvedCompilationUnit2(source, source), isNull); |
| 1759 } | 1760 } |
| 1760 | 1761 |
| 1761 Future test_implicitAnalysisEvents_added() async { | 1762 Future test_implicitAnalysisEvents_added() async { |
| 1762 AnalyzedSourcesListener listener = new AnalyzedSourcesListener(); | 1763 AnalyzedSourcesListener listener = new AnalyzedSourcesListener(); |
| 1763 context.implicitAnalysisEvents.listen(listener.onData); | 1764 context.implicitAnalysisEvents.listen(listener.onData); |
| 1764 // | 1765 // |
| 1765 // Create a file that references an file that is not explicitly being | 1766 // Create a file that references an file that is not explicitly being |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1894 Source source = addSource("/lib.dart", "library lib;"); | 1895 Source source = addSource("/lib.dart", "library lib;"); |
| 1895 CompilationUnit compilationUnit = context.parseCompilationUnit(source); | 1896 CompilationUnit compilationUnit = context.parseCompilationUnit(source); |
| 1896 expect(compilationUnit, isNotNull); | 1897 expect(compilationUnit, isNotNull); |
| 1897 AnalysisErrorInfo errorInfo = context.getErrors(source); | 1898 AnalysisErrorInfo errorInfo = context.getErrors(source); |
| 1898 expect(errorInfo, isNotNull); | 1899 expect(errorInfo, isNotNull); |
| 1899 expect(errorInfo.errors, hasLength(0)); | 1900 expect(errorInfo.errors, hasLength(0)); |
| 1900 } | 1901 } |
| 1901 | 1902 |
| 1902 void test_parseCompilationUnit_nonExistentSource() { | 1903 void test_parseCompilationUnit_nonExistentSource() { |
| 1903 Source source = newSource('/test.dart'); | 1904 Source source = newSource('/test.dart'); |
| 1904 resourceProvider.deleteFile('/test.dart'); | 1905 resourceProvider.deleteFile(resourceProvider.convertPath('/test.dart')); |
| 1905 try { | 1906 try { |
| 1906 context.parseCompilationUnit(source); | 1907 context.parseCompilationUnit(source); |
| 1907 fail("Expected AnalysisException because file does not exist"); | 1908 fail("Expected AnalysisException because file does not exist"); |
| 1908 } on AnalysisException { | 1909 } on AnalysisException { |
| 1909 // Expected result | 1910 // Expected result |
| 1910 } | 1911 } |
| 1911 } | 1912 } |
| 1912 | 1913 |
| 1913 void test_parseHtmlDocument() { | 1914 void test_parseHtmlDocument() { |
| 1914 Source source = addSource("/lib.html", "<!DOCTYPE html><html></html>"); | 1915 Source source = addSource("/lib.html", "<!DOCTYPE html><html></html>"); |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2352 expect(event.wasComputed, isTrue); | 2353 expect(event.wasComputed, isTrue); |
| 2353 expect(event.wasInvalidated, isFalse); | 2354 expect(event.wasInvalidated, isFalse); |
| 2354 LibrarySpecificUnit target = event.target; | 2355 LibrarySpecificUnit target = event.target; |
| 2355 Source librarySource = target.library; | 2356 Source librarySource = target.library; |
| 2356 resolvedUnitUris.add(librarySource.uri.toString()); | 2357 resolvedUnitUris.add(librarySource.uri.toString()); |
| 2357 }); | 2358 }); |
| 2358 // analyze | 2359 // analyze |
| 2359 addSource('/test.dart', 'main() {}'); | 2360 addSource('/test.dart', 'main() {}'); |
| 2360 _analyzeAll_assertFinished(); | 2361 _analyzeAll_assertFinished(); |
| 2361 // verify | 2362 // verify |
| 2362 expect(libraryElementUris, contains('file:///test.dart')); | 2363 String testUri = resourceProvider |
| 2363 expect(parsedUnitUris, contains('file:///test.dart')); | 2364 .getFile(resourceProvider.convertPath('/test.dart')) |
| 2364 expect(resolvedUnitUris, contains('file:///test.dart')); | 2365 .toUri() |
| 2366 .toString(); |
| 2367 expect(libraryElementUris, contains(testUri)); |
| 2368 expect(parsedUnitUris, contains(testUri)); |
| 2369 expect(resolvedUnitUris, contains(testUri)); |
| 2365 } | 2370 } |
| 2366 | 2371 |
| 2367 void test_performAnalysisTask_switchPackageVersion() { | 2372 void test_performAnalysisTask_switchPackageVersion() { |
| 2368 // version 1 | 2373 // version 1 |
| 2369 resourceProvider.newFile( | 2374 resourceProvider.newFile( |
| 2370 '/pkgs/crypto-1/lib/crypto.dart', | 2375 '/pkgs/crypto-1/lib/crypto.dart', |
| 2371 r''' | 2376 r''' |
| 2372 library crypto; | 2377 library crypto; |
| 2373 part 'src/hash_utils.dart'; | 2378 part 'src/hash_utils.dart'; |
| 2374 '''); | 2379 '''); |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2715 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); | 2720 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); |
| 2716 var fileA = resourceProvider.newFile('/a.dart', ""); | 2721 var fileA = resourceProvider.newFile('/a.dart', ""); |
| 2717 var fileB = resourceProvider.newFile('/b.dart', "import 'a.dart';"); | 2722 var fileB = resourceProvider.newFile('/b.dart', "import 'a.dart';"); |
| 2718 Source sourceA = fileA.createSource(); | 2723 Source sourceA = fileA.createSource(); |
| 2719 Source sourceB = fileB.createSource(); | 2724 Source sourceB = fileB.createSource(); |
| 2720 context.applyChanges( | 2725 context.applyChanges( |
| 2721 new ChangeSet()..addedSource(sourceA)..addedSource(sourceB)); | 2726 new ChangeSet()..addedSource(sourceA)..addedSource(sourceB)); |
| 2722 // analyze everything | 2727 // analyze everything |
| 2723 _analyzeAll_assertFinished(); | 2728 _analyzeAll_assertFinished(); |
| 2724 // delete a.dart | 2729 // delete a.dart |
| 2725 resourceProvider.deleteFile('/a.dart'); | 2730 resourceProvider.deleteFile(resourceProvider.convertPath('/a.dart')); |
| 2726 // analysis should eventually stop | 2731 // analysis should eventually stop |
| 2727 _analyzeAll_assertFinished(); | 2732 _analyzeAll_assertFinished(); |
| 2728 } | 2733 } |
| 2729 | 2734 |
| 2730 void xtest_performAnalysisTask_stress() { | 2735 void xtest_performAnalysisTask_stress() { |
| 2731 int maxCacheSize = 4; | 2736 int maxCacheSize = 4; |
| 2732 AnalysisOptionsImpl options = | 2737 AnalysisOptionsImpl options = |
| 2733 new AnalysisOptionsImpl.from(context.analysisOptions); | 2738 new AnalysisOptionsImpl.from(context.analysisOptions); |
| 2734 context.analysisOptions = options; | 2739 context.analysisOptions = options; |
| 2735 int sourceCount = maxCacheSize + 2; | 2740 int sourceCount = maxCacheSize + 2; |
| (...skipping 931 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3667 '/b.dart', | 3672 '/b.dart', |
| 3668 r''' | 3673 r''' |
| 3669 import 'a.dart'; | 3674 import 'a.dart'; |
| 3670 main() { | 3675 main() { |
| 3671 new A(); | 3676 new A(); |
| 3672 new B(); | 3677 new B(); |
| 3673 } | 3678 } |
| 3674 '''); | 3679 '''); |
| 3675 _performPendingAnalysisTasks(); | 3680 _performPendingAnalysisTasks(); |
| 3676 resourceProvider.updateFile( | 3681 resourceProvider.updateFile( |
| 3677 '/a.dart', | 3682 resourceProvider.convertPath('/a.dart'), |
| 3678 r''' | 3683 r''' |
| 3679 class A2 {} | 3684 class A2 {} |
| 3680 class B {} | 3685 class B {} |
| 3681 '''); | 3686 '''); |
| 3682 // Update a.dart: remove A, add A2. | 3687 // Update a.dart: remove A, add A2. |
| 3683 // b.dart is invalid, because it references A. | 3688 // b.dart is invalid, because it references A. |
| 3684 var changeSet = new ChangeSet()..changedSource(a); | 3689 var changeSet = new ChangeSet()..changedSource(a); |
| 3685 context.applyChanges(changeSet); | 3690 context.applyChanges(changeSet); |
| 3686 _assertValidForChangedLibrary(a); | 3691 _assertValidForChangedLibrary(a); |
| 3687 _assertInvalid(a, LIBRARY_ERRORS_READY); | 3692 _assertInvalid(a, LIBRARY_ERRORS_READY); |
| (...skipping 1579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5267 * Initialize the visitor. | 5272 * Initialize the visitor. |
| 5268 */ | 5273 */ |
| 5269 _ElementGatherer(); | 5274 _ElementGatherer(); |
| 5270 | 5275 |
| 5271 @override | 5276 @override |
| 5272 void visitElement(Element element) { | 5277 void visitElement(Element element) { |
| 5273 elements[element] = element; | 5278 elements[element] = element; |
| 5274 super.visitElement(element); | 5279 super.visitElement(element); |
| 5275 } | 5280 } |
| 5276 } | 5281 } |
| OLD | NEW |