| 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.generated.incremental_resolver_test; | 5 library analyzer.test.generated.incremental_resolver_test; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
| 9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/src/context/cache.dart'; | 10 import 'package:analyzer/src/context/cache.dart'; |
| (...skipping 3483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3494 // resolve "newCode" from scratch | 3494 // resolve "newCode" from scratch |
| 3495 CompilationUnit fullNewUnit; | 3495 CompilationUnit fullNewUnit; |
| 3496 { | 3496 { |
| 3497 source = addSource(newCode); | 3497 source = addSource(newCode); |
| 3498 _runTasks(); | 3498 _runTasks(); |
| 3499 LibraryElement library = resolve2(source); | 3499 LibraryElement library = resolve2(source); |
| 3500 fullNewUnit = resolveCompilationUnit(source, library); | 3500 fullNewUnit = resolveCompilationUnit(source, library); |
| 3501 } | 3501 } |
| 3502 _checkCacheEntries(cache); | 3502 _checkCacheEntries(cache); |
| 3503 | 3503 |
| 3504 try { | 3504 assertSameResolution(unit, fullNewUnit); |
| 3505 assertSameResolution(unit, fullNewUnit); | |
| 3506 } on IncrementalResolutionMismatch catch (mismatch) { | |
| 3507 fail(mismatch.message); | |
| 3508 } | |
| 3509 // errors | 3505 // errors |
| 3510 List<AnalysisError> newFullErrors = | 3506 List<AnalysisError> newFullErrors = |
| 3511 analysisContext.getErrors(source).errors; | 3507 analysisContext.getErrors(source).errors; |
| 3512 _assertEqualErrors(newErrors, newFullErrors); | 3508 _assertEqualErrors(newErrors, newFullErrors); |
| 3513 // prepare for the next cycle | 3509 // prepare for the next cycle |
| 3514 code = newCode; | 3510 code = newCode; |
| 3515 } | 3511 } |
| 3516 | 3512 |
| 3517 void _resolveUnit(String code) { | 3513 void _resolveUnit(String code) { |
| 3518 this.code = code; | 3514 this.code = code; |
| (...skipping 1323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4842 { | 4838 { |
| 4843 ClassElement typeA = oldUnitElement.getType('A'); | 4839 ClassElement typeA = oldUnitElement.getType('A'); |
| 4844 expect(oldUnitElement.getElementAt(typeA.nameOffset), typeA); | 4840 expect(oldUnitElement.getElementAt(typeA.nameOffset), typeA); |
| 4845 } | 4841 } |
| 4846 { | 4842 { |
| 4847 ClassElement typeB = oldUnitElement.getType('B'); | 4843 ClassElement typeB = oldUnitElement.getType('B'); |
| 4848 expect(oldUnitElement.getElementAt(typeB.nameOffset), typeB); | 4844 expect(oldUnitElement.getElementAt(typeB.nameOffset), typeB); |
| 4849 } | 4845 } |
| 4850 } | 4846 } |
| 4851 | 4847 |
| 4852 void _assertCacheResults({bool expectCachePostConstantsValid: true}) { | 4848 void test_updateFunctionToForLoop() { |
| 4849 _resolveUnit(r''' |
| 4850 class PlayDrag { |
| 4851 final List<num> times = new List<num>(); |
| 4852 |
| 4853 PlayDrag.start() {} |
| 4854 |
| 4855 void update(num pos) { |
| 4856 fo (int i = times.length - 2; i >= 0; i--) {} |
| 4857 } |
| 4858 } |
| 4859 '''); |
| 4860 |
| 4861 _updateAndValidate( |
| 4862 r''' |
| 4863 class PlayDrag { |
| 4864 final List<num> times = new List<num>(); |
| 4865 |
| 4866 PlayDrag.start() {} |
| 4867 |
| 4868 void update(num pos) { |
| 4869 for (int i = times.length - 2; i >= 0; i--) {} |
| 4870 } |
| 4871 } |
| 4872 ''', |
| 4873 expectLibraryUnchanged: false); |
| 4874 } |
| 4875 |
| 4876 void _assertCacheResults( |
| 4877 {bool expectLibraryUnchanged: true, |
| 4878 bool expectCachePostConstantsValid: true}) { |
| 4853 _assertCacheSourceResult(TOKEN_STREAM); | 4879 _assertCacheSourceResult(TOKEN_STREAM); |
| 4854 _assertCacheSourceResult(SCAN_ERRORS); | 4880 _assertCacheSourceResult(SCAN_ERRORS); |
| 4855 _assertCacheSourceResult(PARSED_UNIT); | 4881 _assertCacheSourceResult(PARSED_UNIT); |
| 4856 _assertCacheSourceResult(PARSE_ERRORS); | 4882 _assertCacheSourceResult(PARSE_ERRORS); |
| 4883 if (!expectLibraryUnchanged) { |
| 4884 return; |
| 4885 } |
| 4857 _assertCacheSourceResult(LIBRARY_ELEMENT1); | 4886 _assertCacheSourceResult(LIBRARY_ELEMENT1); |
| 4858 _assertCacheSourceResult(LIBRARY_ELEMENT2); | 4887 _assertCacheSourceResult(LIBRARY_ELEMENT2); |
| 4859 _assertCacheSourceResult(LIBRARY_ELEMENT3); | 4888 _assertCacheSourceResult(LIBRARY_ELEMENT3); |
| 4860 _assertCacheSourceResult(LIBRARY_ELEMENT4); | 4889 _assertCacheSourceResult(LIBRARY_ELEMENT4); |
| 4861 _assertCacheSourceResult(LIBRARY_ELEMENT5); | 4890 _assertCacheSourceResult(LIBRARY_ELEMENT5); |
| 4862 _assertCacheSourceResult(LIBRARY_ELEMENT6); | 4891 _assertCacheSourceResult(LIBRARY_ELEMENT6); |
| 4863 _assertCacheSourceResult(LIBRARY_ELEMENT7); | 4892 _assertCacheSourceResult(LIBRARY_ELEMENT7); |
| 4864 _assertCacheSourceResult(LIBRARY_ELEMENT8); | 4893 _assertCacheSourceResult(LIBRARY_ELEMENT8); |
| 4865 if (expectCachePostConstantsValid) { | 4894 if (expectCachePostConstantsValid) { |
| 4866 _assertCacheSourceResult(LIBRARY_ELEMENT); | 4895 _assertCacheSourceResult(LIBRARY_ELEMENT); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4936 | 4965 |
| 4937 void _runTasks() { | 4966 void _runTasks() { |
| 4938 AnalysisResult result = analysisContext.performAnalysisTask(); | 4967 AnalysisResult result = analysisContext.performAnalysisTask(); |
| 4939 while (result.changeNotices != null) { | 4968 while (result.changeNotices != null) { |
| 4940 result = analysisContext.performAnalysisTask(); | 4969 result = analysisContext.performAnalysisTask(); |
| 4941 } | 4970 } |
| 4942 } | 4971 } |
| 4943 | 4972 |
| 4944 void _updateAndValidate(String newCode, | 4973 void _updateAndValidate(String newCode, |
| 4945 {bool expectedSuccess: true, | 4974 {bool expectedSuccess: true, |
| 4975 bool expectLibraryUnchanged: true, |
| 4946 bool expectCachePostConstantsValid: true, | 4976 bool expectCachePostConstantsValid: true, |
| 4947 bool compareWithFull: true, | 4977 bool compareWithFull: true, |
| 4948 bool runTasksBeforeIncremental: true}) { | 4978 bool runTasksBeforeIncremental: true}) { |
| 4949 // Run any pending tasks tasks. | 4979 // Run any pending tasks tasks. |
| 4950 if (runTasksBeforeIncremental) { | 4980 if (runTasksBeforeIncremental) { |
| 4951 _runTasks(); | 4981 _runTasks(); |
| 4952 } | 4982 } |
| 4953 // Update the source - currently this may cause incremental resolution. | 4983 // Update the source - currently this may cause incremental resolution. |
| 4954 // Then request the updated resolved unit. | 4984 // Then request the updated resolved unit. |
| 4955 _resetWithIncremental(true); | 4985 _resetWithIncremental(true); |
| 4956 analysisContext2.setContents(source, newCode); | 4986 analysisContext2.setContents(source, newCode); |
| 4957 CompilationUnit newUnit = resolveCompilationUnit(source, oldLibrary); | 4987 CompilationUnit newUnit = resolveCompilationUnit(source, oldLibrary); |
| 4958 expect(logger.hasError, isFalse); | 4988 logger.expectNoErrors(); |
| 4959 List<AnalysisError> newErrors = analysisContext.computeErrors(source); | 4989 List<AnalysisError> newErrors = analysisContext.computeErrors(source); |
| 4960 LineInfo newLineInfo = analysisContext.getLineInfo(source); | 4990 LineInfo newLineInfo = analysisContext.getLineInfo(source); |
| 4961 // check for expected failure | 4991 // check for expected failure |
| 4962 if (!expectedSuccess) { | 4992 if (!expectedSuccess) { |
| 4963 expect(newUnit.element, isNot(same(oldUnitElement))); | 4993 expect(newUnit.element, isNot(same(oldUnitElement))); |
| 4964 return; | 4994 return; |
| 4965 } | 4995 } |
| 4966 // The cache must still have enough results to make the incremental | 4996 // The cache must still have enough results to make the incremental |
| 4967 // resolution useful. | 4997 // resolution useful. |
| 4968 _assertCacheResults( | 4998 _assertCacheResults( |
| 4999 expectLibraryUnchanged: expectLibraryUnchanged, |
| 4969 expectCachePostConstantsValid: expectCachePostConstantsValid); | 5000 expectCachePostConstantsValid: expectCachePostConstantsValid); |
| 4970 // The existing CompilationUnit[Element] should be updated. | 5001 // The existing CompilationUnit[Element] should be updated. |
| 4971 expect(newUnit, same(oldUnit)); | 5002 expect(newUnit, same(oldUnit)); |
| 4972 expect(newUnit.element, same(oldUnitElement)); | 5003 expect(newUnit.element, same(oldUnitElement)); |
| 4973 expect(analysisContext.getResolvedCompilationUnit(source, oldLibrary), | 5004 expect(analysisContext.getResolvedCompilationUnit(source, oldLibrary), |
| 4974 same(oldUnit)); | 5005 same(oldUnit)); |
| 4975 // The only expected pending task should return the same resolved | 5006 // The only expected pending task should return the same resolved |
| 4976 // "newUnit", so all clients will get it using the usual way. | 5007 // "newUnit", so all clients will get it using the usual way. |
| 4977 AnalysisResult analysisResult = analysisContext.performAnalysisTask(); | 5008 AnalysisResult analysisResult = analysisContext.performAnalysisTask(); |
| 4978 ChangeNotice notice = analysisResult.changeNotices[0]; | 5009 ChangeNotice notice = analysisResult.changeNotices[0]; |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5248 } | 5279 } |
| 5249 | 5280 |
| 5250 class _Edit { | 5281 class _Edit { |
| 5251 final int offset; | 5282 final int offset; |
| 5252 final int length; | 5283 final int length; |
| 5253 final String replacement; | 5284 final String replacement; |
| 5254 _Edit(this.offset, this.length, this.replacement); | 5285 _Edit(this.offset, this.length, this.replacement); |
| 5255 } | 5286 } |
| 5256 | 5287 |
| 5257 class _TestLogger implements logging.Logger { | 5288 class _TestLogger implements logging.Logger { |
| 5258 bool hasError = false; | 5289 Object lastException; |
| 5290 Object lastStackTrace; |
| 5291 |
| 5292 void expectNoErrors() { |
| 5293 if (lastException != null) { |
| 5294 fail("logged an exception:\n$lastException\n$lastStackTrace\n"); |
| 5295 } |
| 5296 } |
| 5259 | 5297 |
| 5260 @override | 5298 @override |
| 5261 void enter(String name) {} | 5299 void enter(String name) {} |
| 5262 | 5300 |
| 5263 @override | 5301 @override |
| 5264 void exit() {} | 5302 void exit() {} |
| 5265 | 5303 |
| 5266 @override | 5304 @override |
| 5267 void log(Object obj) {} | 5305 void log(Object obj) {} |
| 5268 | 5306 |
| 5269 @override | 5307 @override |
| 5270 void logException(Object exception, [Object stackTrace]) { | 5308 void logException(Object exception, [Object stackTrace]) { |
| 5271 hasError = true; | 5309 lastException = exception; |
| 5310 lastStackTrace = stackTrace; |
| 5272 } | 5311 } |
| 5273 | 5312 |
| 5274 @override | 5313 @override |
| 5275 logging.LoggingTimer startTimer() { | 5314 logging.LoggingTimer startTimer() { |
| 5276 return new logging.LoggingTimer(this); | 5315 return new logging.LoggingTimer(this); |
| 5277 } | 5316 } |
| 5278 } | 5317 } |
| OLD | NEW |