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

Side by Side Diff: packages/analyzer/test/src/context/context_test.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 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) 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 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 9
10 import 'package:analyzer/dart/ast/ast.dart';
11 import 'package:analyzer/dart/element/element.dart';
12 import 'package:analyzer/dart/element/type.dart';
13 import 'package:analyzer/dart/element/visitor.dart';
14 import 'package:analyzer/error/error.dart';
15 import 'package:analyzer/exception/exception.dart';
16 import 'package:analyzer/file_system/file_system.dart';
9 import 'package:analyzer/file_system/memory_file_system.dart'; 17 import 'package:analyzer/file_system/memory_file_system.dart';
18 import 'package:analyzer/source/package_map_resolver.dart';
10 import 'package:analyzer/src/cancelable_future.dart'; 19 import 'package:analyzer/src/cancelable_future.dart';
11 import 'package:analyzer/src/context/cache.dart'; 20 import 'package:analyzer/src/context/cache.dart';
12 import 'package:analyzer/src/context/context.dart'; 21 import 'package:analyzer/src/context/context.dart';
13 import 'package:analyzer/src/generated/ast.dart'; 22 import 'package:analyzer/src/dart/element/element.dart';
14 import 'package:analyzer/src/generated/element.dart'; 23 import 'package:analyzer/src/dart/scanner/scanner.dart';
15 import 'package:analyzer/src/generated/engine.dart' 24 import 'package:analyzer/src/generated/engine.dart';
16 show
17 AnalysisContext,
18 AnalysisContextStatistics,
19 AnalysisDelta,
20 AnalysisEngine,
21 AnalysisErrorInfo,
22 AnalysisLevel,
23 AnalysisNotScheduledError,
24 AnalysisOptions,
25 AnalysisOptionsImpl,
26 AnalysisResult,
27 CacheState,
28 ChangeNotice,
29 ChangeSet,
30 IncrementalAnalysisCache,
31 TimestampedData;
32 import 'package:analyzer/src/generated/error.dart';
33 import 'package:analyzer/src/generated/java_engine.dart';
34 import 'package:analyzer/src/generated/resolver.dart'; 25 import 'package:analyzer/src/generated/resolver.dart';
35 import 'package:analyzer/src/generated/scanner.dart';
36 import 'package:analyzer/src/generated/source.dart'; 26 import 'package:analyzer/src/generated/source.dart';
27 import 'package:analyzer/src/generated/utilities_collection.dart';
37 import 'package:analyzer/src/task/dart.dart'; 28 import 'package:analyzer/src/task/dart.dart';
38 import 'package:analyzer/src/task/html.dart'; 29 import 'package:analyzer/src/task/html.dart';
39 import 'package:analyzer/task/dart.dart'; 30 import 'package:analyzer/task/dart.dart';
31 import 'package:analyzer/task/general.dart';
40 import 'package:analyzer/task/model.dart'; 32 import 'package:analyzer/task/model.dart';
41 import 'package:html/dom.dart' show Document; 33 import 'package:html/dom.dart' show Document;
34 import 'package:test_reflective_loader/test_reflective_loader.dart';
42 import 'package:unittest/unittest.dart'; 35 import 'package:unittest/unittest.dart';
43 import 'package:watcher/src/utils.dart'; 36 import 'package:watcher/src/utils.dart';
44 37
45 import '../../generated/engine_test.dart'; 38 import '../../generated/engine_test.dart';
46 import '../../generated/test_support.dart'; 39 import '../../generated/test_support.dart';
47 import '../../reflective_tests.dart';
48 import '../../utils.dart'; 40 import '../../utils.dart';
49 import 'abstract_context.dart'; 41 import 'abstract_context.dart';
50 42
51 main() { 43 main() {
52 initializeTestEnvironment(); 44 initializeTestEnvironment();
53 runReflectiveTests(AnalysisContextImplTest); 45 defineReflectiveTests(AnalysisContextImplTest);
54 runReflectiveTests(LimitedInvalidateTest); 46 defineReflectiveTests(LimitedInvalidateTest);
55 } 47 }
56 48
57 @reflectiveTest 49 @reflectiveTest
58 class AnalysisContextImplTest extends AbstractContextTest { 50 class AnalysisContextImplTest extends AbstractContextTest {
51 void fail_getErrors_html_some() {
52 Source source = addSource(
53 "/test.html",
54 r'''
55 <html><head>
56 <script type='application/dart' src='test.dart'/>
57 </head></html>''');
58 AnalysisErrorInfo errorInfo = context.getErrors(source);
59 expect(errorInfo, isNotNull);
60 List<AnalysisError> errors = errorInfo.errors;
61 expect(errors, hasLength(0));
62 errors = context.computeErrors(source);
63 expect(errors, hasLength(2));
64 }
65
59 Future fail_implicitAnalysisEvents_removed() async { 66 Future fail_implicitAnalysisEvents_removed() async {
60 AnalyzedSourcesListener listener = new AnalyzedSourcesListener(); 67 AnalyzedSourcesListener listener = new AnalyzedSourcesListener();
61 context.implicitAnalysisEvents.listen(listener.onData); 68 context.implicitAnalysisEvents.listen(listener.onData);
62 // 69 //
63 // Create a file that references an file that is not explicitly being 70 // Create a file that references an file that is not explicitly being
64 // analyzed and fully analyze it. Ensure that the listener is told about 71 // analyzed and fully analyze it. Ensure that the listener is told about
65 // the implicitly analyzed file. 72 // the implicitly analyzed file.
66 // 73 //
67 Source sourceA = newSource('/a.dart', "library a; import 'b.dart';"); 74 Source sourceA = newSource('/a.dart', "library a; import 'b.dart';");
68 Source sourceB = newSource('/b.dart', "library b;"); 75 Source sourceB = newSource('/b.dart', "library b;");
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 ChangeSet changeSet = new ChangeSet(); 135 ChangeSet changeSet = new ChangeSet();
129 changeSet.addedSource(source); 136 changeSet.addedSource(source);
130 context.applyChanges(changeSet); 137 context.applyChanges(changeSet);
131 expect(context.sourcesNeedingProcessing, contains(source)); 138 expect(context.sourcesNeedingProcessing, contains(source));
132 return pumpEventQueue().then((_) { 139 return pumpEventQueue().then((_) {
133 listener.assertEvent(wereSourcesAdded: true); 140 listener.assertEvent(wereSourcesAdded: true);
134 listener.assertNoMoreEvents(); 141 listener.assertNoMoreEvents();
135 }); 142 });
136 } 143 }
137 144
145 void test_applyChanges_add_makesExplicit() {
146 Source source = newSource('/test.dart');
147 // get the entry, it's not explicit
148 CacheEntry entry = context.getCacheEntry(source);
149 expect(entry.explicitlyAdded, isFalse);
150 // add the source
151 ChangeSet changeSet = new ChangeSet();
152 changeSet.addedSource(source);
153 context.applyChanges(changeSet);
154 // now the entry is explicit
155 expect(entry.explicitlyAdded, isTrue);
156 }
157
158 void test_applyChanges_addNewImport_invalidateLibraryCycle() {
159 context.analysisOptions =
160 new AnalysisOptionsImpl.from(context.analysisOptions)
161 ..strongMode = true;
162 Source embedder = addSource(
163 '/a.dart',
164 r'''
165 library a;
166 import 'b.dart';
167 //import 'c.dart';
168 ''');
169 addSource(
170 '/b.dart',
171 r'''
172 library b;
173 import 'a.dart';
174 ''');
175 addSource(
176 '/c.dart',
177 r'''
178 library c;
179 import 'b.dart';
180 ''');
181 _performPendingAnalysisTasks();
182 // Add a new import into a.dart, this should invalidate its library cycle.
183 // If it doesn't, we will get a task cycle exception.
184 context.setContents(
185 embedder,
186 r'''
187 library a;
188 import 'b.dart';
189 import 'c.dart';
190 ''');
191 _performPendingAnalysisTasks();
192 expect(context.getCacheEntry(embedder).exception, isNull);
193 }
194
138 Future test_applyChanges_change() { 195 Future test_applyChanges_change() {
139 SourcesChangedListener listener = new SourcesChangedListener(); 196 SourcesChangedListener listener = new SourcesChangedListener();
140 context.onSourcesChanged.listen(listener.onData); 197 context.onSourcesChanged.listen(listener.onData);
141 expect(context.sourcesNeedingProcessing, isEmpty); 198 expect(context.sourcesNeedingProcessing, isEmpty);
142 Source source = newSource('/test.dart'); 199 Source source = newSource('/test.dart');
143 ChangeSet changeSet1 = new ChangeSet(); 200 ChangeSet changeSet1 = new ChangeSet();
144 changeSet1.addedSource(source); 201 changeSet1.addedSource(source);
145 context.applyChanges(changeSet1); 202 context.applyChanges(changeSet1);
146 expect(context.sourcesNeedingProcessing, contains(source)); 203 expect(context.sourcesNeedingProcessing, contains(source));
147 Source source2 = newSource('/test2.dart'); 204 Source source2 = newSource('/test2.dart');
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 context.resolveCompilationUnit2(librarySource, librarySource); 276 context.resolveCompilationUnit2(librarySource, librarySource);
220 expect(libraryUnit, isNotNull); 277 expect(libraryUnit, isNotNull);
221 CompilationUnit partUnit = 278 CompilationUnit partUnit =
222 context.resolveCompilationUnit2(partSource, librarySource); 279 context.resolveCompilationUnit2(partSource, librarySource);
223 expect(partUnit, isNotNull); 280 expect(partUnit, isNotNull);
224 TopLevelVariableDeclaration declaration = 281 TopLevelVariableDeclaration declaration =
225 libraryUnit.declarations[0] as TopLevelVariableDeclaration; 282 libraryUnit.declarations[0] as TopLevelVariableDeclaration;
226 Element declarationElement = declaration.variables.variables[0].element; 283 Element declarationElement = declaration.variables.variables[0].element;
227 TopLevelVariableDeclaration use = 284 TopLevelVariableDeclaration use =
228 partUnit.declarations[0] as TopLevelVariableDeclaration; 285 partUnit.declarations[0] as TopLevelVariableDeclaration;
229 Element useElement = (use.variables.variables[0].initializer 286 Element useElement =
230 as SimpleIdentifier).staticElement; 287 (use.variables.variables[0].initializer as SimpleIdentifier)
288 .staticElement;
231 expect((useElement as PropertyAccessorElement).variable, 289 expect((useElement as PropertyAccessorElement).variable,
232 same(declarationElement)); 290 same(declarationElement));
233 return pumpEventQueue().then((_) { 291 return pumpEventQueue().then((_) {
234 listener.assertEvent(wereSourcesAdded: true); 292 listener.assertEvent(wereSourcesAdded: true);
235 listener.assertEvent(wereSourcesAdded: true); 293 listener.assertEvent(wereSourcesAdded: true);
236 listener.assertEvent(changedSources: [librarySource]); 294 listener.assertEvent(changedSources: [librarySource]);
237 listener.assertEvent(changedSources: [partSource]); 295 listener.assertEvent(changedSources: [partSource]);
238 listener.assertNoMoreEvents(); 296 listener.assertNoMoreEvents();
239 }); 297 });
240 } 298 }
(...skipping 12 matching lines...) Expand all
253 changeSet2.addedSource(source2); 311 changeSet2.addedSource(source2);
254 changeSet2.changedRange(source, 'library test;', 0, 0, 13); 312 changeSet2.changedRange(source, 'library test;', 0, 0, 13);
255 context.applyChanges(changeSet2); 313 context.applyChanges(changeSet2);
256 return pumpEventQueue().then((_) { 314 return pumpEventQueue().then((_) {
257 listener.assertEvent(wereSourcesAdded: true); 315 listener.assertEvent(wereSourcesAdded: true);
258 listener.assertEvent(wereSourcesAdded: true, changedSources: [source]); 316 listener.assertEvent(wereSourcesAdded: true, changedSources: [source]);
259 listener.assertNoMoreEvents(); 317 listener.assertNoMoreEvents();
260 }); 318 });
261 } 319 }
262 320
321 void test_applyChanges_changedSource_updateModificationTime() {
322 String path = '/test.dart';
323 File file = resourceProvider.newFile(path, 'var V = 1;');
324 Source source = file.createSource();
325 context.applyChanges(new ChangeSet()..addedSource(source));
326 // Analyze all.
327 _analyzeAll_assertFinished();
328 expect(context.analysisCache.getState(source, RESOLVED_UNIT),
329 CacheState.INVALID);
330 // Update the file and notify the context about the change.
331 resourceProvider.updateFile(path, 'var V = 2;');
332 context.applyChanges(new ChangeSet()..changedSource(source));
333 // The analysis results are invalidated.
334 // We have seen the new contents, so 'modificationTime' is also updated.
335 expect(context.analysisCache.getState(source, RESOLVED_UNIT),
336 CacheState.INVALID);
337 expect(
338 context.getCacheEntry(source).modificationTime, file.modificationStamp);
339 }
340
263 void test_applyChanges_empty() { 341 void test_applyChanges_empty() {
264 context.applyChanges(new ChangeSet()); 342 context.applyChanges(new ChangeSet());
265 expect(context.performAnalysisTask().changeNotices, isNull); 343 expect(context.performAnalysisTask().changeNotices, isNull);
266 } 344 }
267 345
346 void test_applyChanges_incremental_resetDriver() {
347 context.analysisOptions = new AnalysisOptionsImpl()..incremental = true;
348 Source source = addSource(
349 "/test.dart",
350 r'''
351 main() {
352 print(42);
353 }
354 ''');
355 _performPendingAnalysisTasks();
356 expect(context.getErrors(source).errors, hasLength(0));
357 // Update the source to have a parse error.
358 // This is an incremental change, but we always invalidate DART_ERRORS.
359 context.setContents(
360 source,
361 r'''
362 main() {
363 print(42)
364 }
365 ''');
366 AnalysisCache cache = context.analysisCache;
367 expect(cache.getValue(source, PARSE_ERRORS), hasLength(1));
368 expect(cache.getState(source, DART_ERRORS), CacheState.INVALID);
369 // Perform enough analysis to prepare inputs (is not actually tested) for
370 // the DART_ERRORS computing task, but don't compute it yet.
371 context.performAnalysisTask();
372 context.performAnalysisTask();
373 expect(cache.getState(source, DART_ERRORS), CacheState.INVALID);
374 // Update the source so that PARSE_ERRORS is empty.
375 context.setContents(
376 source,
377 r'''
378 main() {
379 print(42);
380 }
381 ''');
382 expect(cache.getValue(source, PARSE_ERRORS), hasLength(0));
383 // After full analysis DART_ERRORS should also be empty.
384 _performPendingAnalysisTasks();
385 expect(cache.getValue(source, DART_ERRORS), hasLength(0));
386 expect(context.getErrors(source).errors, hasLength(0));
387 }
388
268 void test_applyChanges_overriddenSource() { 389 void test_applyChanges_overriddenSource() {
269 // Note: addSource adds the source to the contentCache. 390 String content = "library test;";
270 Source source = addSource("/test.dart", "library test;"); 391 Source source = addSource("/test.dart", content);
392 context.setContents(source, content);
271 context.computeErrors(source); 393 context.computeErrors(source);
272 while (!context.sourcesNeedingProcessing.isEmpty) { 394 while (!context.sourcesNeedingProcessing.isEmpty) {
273 context.performAnalysisTask(); 395 context.performAnalysisTask();
274 } 396 }
275 // Adding the source as a changedSource should have no effect since 397 // Adding the source as a changedSource should have no effect since
276 // it is already overridden in the content cache. 398 // it is already overridden in the content cache.
277 ChangeSet changeSet = new ChangeSet(); 399 ChangeSet changeSet = new ChangeSet();
278 changeSet.changedSource(source); 400 changeSet.changedSource(source);
279 context.applyChanges(changeSet); 401 context.applyChanges(changeSet);
280 expect(context.sourcesNeedingProcessing, hasLength(0)); 402 expect(context.sourcesNeedingProcessing, hasLength(0));
281 } 403 }
282 404
405 void test_applyChanges_recompute_exportNamespace() {
406 Source libSource = addSource(
407 "/lib.dart",
408 r'''
409 class A {}
410 ''');
411 Source exporterSource = addSource(
412 "/exporter.dart",
413 r'''
414 export 'lib.dart';
415 ''');
416 _performPendingAnalysisTasks();
417 // initially: A
418 {
419 LibraryElement libraryElement =
420 context.getResult(exporterSource, LIBRARY_ELEMENT1);
421 expect(libraryElement.exportNamespace.definedNames.keys,
422 unorderedEquals(['A']));
423 }
424 // after update: B
425 context.setContents(
426 libSource,
427 r'''
428 class B {}''');
429 _performPendingAnalysisTasks();
430 {
431 LibraryElement libraryElement =
432 context.getResult(exporterSource, LIBRARY_ELEMENT1);
433 expect(libraryElement.exportNamespace.definedNames.keys,
434 unorderedEquals(['B']));
435 }
436 }
437
283 Future test_applyChanges_remove() { 438 Future test_applyChanges_remove() {
284 SourcesChangedListener listener = new SourcesChangedListener(); 439 SourcesChangedListener listener = new SourcesChangedListener();
285 context.onSourcesChanged.listen(listener.onData); 440 context.onSourcesChanged.listen(listener.onData);
286 String libAContents = r''' 441 String libAContents = r'''
287 library libA; 442 library libA;
288 import 'libB.dart';'''; 443 import 'libB.dart';''';
289 Source libA = addSource("/libA.dart", libAContents); 444 Source libA = addSource("/libA.dart", libAContents);
290 String libBContents = "library libB;"; 445 String libBContents = "library libB;";
291 Source libB = addSource("/libB.dart", libBContents); 446 Source libB = addSource("/libB.dart", libBContents);
292 LibraryElement libAElement = context.computeLibraryElement(libA); 447 LibraryElement libAElement = context.computeLibraryElement(libA);
293 expect(libAElement, isNotNull); 448 expect(libAElement, isNotNull);
294 List<LibraryElement> importedLibraries = libAElement.importedLibraries; 449 List<LibraryElement> importedLibraries = libAElement.importedLibraries;
295 expect(importedLibraries, hasLength(2)); 450 expect(importedLibraries, hasLength(2));
296 context.computeErrors(libA); 451 context.computeErrors(libA);
297 context.computeErrors(libB); 452 context.computeErrors(libB);
298 expect(context.sourcesNeedingProcessing, hasLength(0));
299 context.setContents(libB, null); 453 context.setContents(libB, null);
300 _removeSource(libB); 454 _removeSource(libB);
301 List<Source> sources = context.sourcesNeedingProcessing; 455 List<Source> sources = context.sourcesNeedingProcessing;
302 expect(sources, hasLength(1)); 456 expect(sources, hasLength(1));
303 expect(sources[0], same(libA)); 457 expect(sources[0], same(libA));
304 libAElement = context.computeLibraryElement(libA); 458 libAElement = context.computeLibraryElement(libA);
305 importedLibraries = libAElement.importedLibraries; 459 importedLibraries = libAElement.importedLibraries;
306 expect(importedLibraries, hasLength(1)); 460 expect(importedLibraries, hasLength(2));
307 return pumpEventQueue().then((_) { 461 return pumpEventQueue().then((_) {
308 listener.assertEvent(wereSourcesAdded: true); 462 listener.assertEvent(wereSourcesAdded: true);
309 listener.assertEvent(wereSourcesAdded: true); 463 listener.assertEvent(wereSourcesAdded: true);
310 listener.assertEvent(wereSourcesRemovedOrDeleted: true); 464 listener.assertEvent(wereSourcesRemovedOrDeleted: true);
311 listener.assertNoMoreEvents(); 465 listener.assertNoMoreEvents();
312 }); 466 });
313 } 467 }
314 468
315 /** 469 /**
316 * IDEA uses the following scenario: 470 * IDEA uses the following scenario:
(...skipping 24 matching lines...) Expand all
341 source, 495 source,
342 r''' 496 r'''
343 main() { 497 main() {
344 print(12); 498 print(12);
345 } 499 }
346 '''); 500 ''');
347 _analyzeAll_assertFinished(); 501 _analyzeAll_assertFinished();
348 expect(context.getResolvedCompilationUnit2(source, source), unit); 502 expect(context.getResolvedCompilationUnit2(source, source), unit);
349 // remove overlay 503 // remove overlay
350 context.setContents(source, null); 504 context.setContents(source, null);
351 context.validateCacheConsistency();
352 _analyzeAll_assertFinished(); 505 _analyzeAll_assertFinished();
353 expect(context.getResolvedCompilationUnit2(source, source), unit); 506 expect(context.getResolvedCompilationUnit2(source, source), unit);
354 } 507 }
355 508
356 Future test_applyChanges_removeContainer() { 509 Future test_applyChanges_removeContainer() {
357 SourcesChangedListener listener = new SourcesChangedListener(); 510 SourcesChangedListener listener = new SourcesChangedListener();
358 context.onSourcesChanged.listen(listener.onData); 511 context.onSourcesChanged.listen(listener.onData);
359 String libAContents = r''' 512 String libAContents = r'''
360 library libA; 513 library libA;
361 import 'libB.dart';'''; 514 import 'libB.dart';''';
362 Source libA = addSource("/libA.dart", libAContents); 515 Source libA = addSource("/libA.dart", libAContents);
363 String libBContents = "library libB;"; 516 String libBContents = "library libB;";
364 Source libB = addSource("/libB.dart", libBContents); 517 Source libB = addSource("/libB.dart", libBContents);
365 context.computeLibraryElement(libA); 518 context.computeLibraryElement(libA);
366 context.computeErrors(libA); 519 context.computeErrors(libA);
367 context.computeErrors(libB); 520 context.computeErrors(libB);
368 expect(context.sourcesNeedingProcessing, hasLength(0));
369 ChangeSet changeSet = new ChangeSet(); 521 ChangeSet changeSet = new ChangeSet();
370 SourceContainer removedContainer = 522 SourceContainer removedContainer =
371 new _AnalysisContextImplTest_test_applyChanges_removeContainer(libB); 523 new _AnalysisContextImplTest_test_applyChanges_removeContainer(libB);
372 changeSet.removedContainer(removedContainer); 524 changeSet.removedContainer(removedContainer);
373 context.applyChanges(changeSet); 525 context.applyChanges(changeSet);
374 List<Source> sources = context.sourcesNeedingProcessing; 526 List<Source> sources = context.sourcesNeedingProcessing;
375 expect(sources, hasLength(1)); 527 expect(sources, hasLength(1));
376 expect(sources[0], same(libA)); 528 expect(sources[0], same(libA));
377 return pumpEventQueue().then((_) { 529 return pumpEventQueue().then((_) {
378 listener.assertEvent(wereSourcesAdded: true); 530 listener.assertEvent(wereSourcesAdded: true);
379 listener.assertEvent(wereSourcesAdded: true); 531 listener.assertEvent(wereSourcesAdded: true);
380 listener.assertEvent(wereSourcesRemovedOrDeleted: true); 532 listener.assertEvent(wereSourcesRemovedOrDeleted: true);
381 listener.assertNoMoreEvents(); 533 listener.assertNoMoreEvents();
382 }); 534 });
383 } 535 }
384 536
385 void test_computeDocumentationComment_block() { 537 void test_applyChanges_removeUsedLibrary_addAgain() {
538 String codeA = r'''
539 import 'b.dart';
540 B b = null;
541 ''';
542 String codeB = r'''
543 class B {}
544 ''';
545 Source a = addSource('/a.dart', codeA);
546 Source b = addSource('/b.dart', codeB);
547 CacheState getErrorsState(Source source) =>
548 context.analysisCache.getState(source, LIBRARY_ERRORS_READY);
549 _performPendingAnalysisTasks();
550 expect(context.getErrors(a).errors, hasLength(0));
551 // Remove b.dart - errors in a.dart are invalidated and recomputed.
552 // Now a.dart has an error.
553 _removeSource(b);
554 expect(getErrorsState(a), CacheState.INVALID);
555 _performPendingAnalysisTasks();
556 expect(getErrorsState(a), CacheState.VALID);
557 expect(context.getErrors(a).errors, hasLength(isPositive));
558 // Add b.dart - errors in a.dart are invalidated and recomputed.
559 // The reason is that a.dart adds dependencies on (not existing) b.dart
560 // results in cache.
561 // Now a.dart does not have errors.
562 addSource('/b.dart', codeB);
563 expect(getErrorsState(a), CacheState.INVALID);
564 _performPendingAnalysisTasks();
565 expect(getErrorsState(a), CacheState.VALID);
566 expect(context.getErrors(a).errors, hasLength(0));
567 }
568
569 void test_cacheConsistencyValidator_computed_deleted() {
570 CacheConsistencyValidator validator = context.cacheConsistencyValidator;
571 var stat = PerformanceStatistics.cacheConsistencyValidationStatistics;
572 stat.reset();
573 // Add sources.
574 MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
575 String path1 = '/test1.dart';
576 String path2 = '/test2.dart';
577 Source source1 = resourceProvider.newFile(path1, '// 1-1').createSource();
578 Source source2 = resourceProvider.newFile(path2, '// 2-1').createSource();
579 context.applyChanges(
580 new ChangeSet()..addedSource(source1)..addedSource(source2));
581 // Same modification times.
582 expect(
583 validator.sourceModificationTimesComputed([source1, source2],
584 [source1.modificationStamp, source2.modificationStamp]),
585 isFalse);
586 expect(stat.numOfChanged, 0);
587 expect(stat.numOfRemoved, 0);
588 // Add overlay
589 context.setContents(source1, '// 1-2');
590 expect(
591 validator.sourceModificationTimesComputed(
592 [source1, source2], [-1, source2.modificationStamp]),
593 isFalse);
594 context.setContents(source1, null);
595 expect(stat.numOfChanged, 0);
596 expect(stat.numOfRemoved, 0);
597 // Different modification times.
598 expect(
599 validator.sourceModificationTimesComputed(
600 [source1, source2], [-1, source2.modificationStamp]),
601 isTrue);
602 expect(stat.numOfChanged, 0);
603 expect(stat.numOfRemoved, 1);
604 }
605
606 void test_cacheConsistencyValidator_computed_modified() {
607 CacheConsistencyValidator validator = context.cacheConsistencyValidator;
608 var stat = PerformanceStatistics.cacheConsistencyValidationStatistics;
609 stat.reset();
610 // Add sources.
611 MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
612 String path1 = '/test1.dart';
613 String path2 = '/test2.dart';
614 Source source1 = resourceProvider.newFile(path1, '// 1-1').createSource();
615 Source source2 = resourceProvider.newFile(path2, '// 2-1').createSource();
616 context.applyChanges(
617 new ChangeSet()..addedSource(source1)..addedSource(source2));
618 // Same modification times.
619 expect(
620 validator.sourceModificationTimesComputed([source1, source2],
621 [source1.modificationStamp, source2.modificationStamp]),
622 isFalse);
623 expect(stat.numOfChanged, 0);
624 expect(stat.numOfRemoved, 0);
625 // Add overlay
626 context.setContents(source1, '// 1-2');
627 expect(
628 validator.sourceModificationTimesComputed([source1, source2],
629 [source1.modificationStamp + 1, source2.modificationStamp]),
630 isFalse);
631 context.setContents(source1, null);
632 expect(stat.numOfChanged, 0);
633 expect(stat.numOfRemoved, 0);
634 // Different modification times.
635 expect(
636 validator.sourceModificationTimesComputed([source1, source2],
637 [source1.modificationStamp + 1, source2.modificationStamp]),
638 isTrue);
639 expect(stat.numOfChanged, 1);
640 expect(stat.numOfRemoved, 0);
641 }
642
643 void test_cacheConsistencyValidator_getSources() {
644 CacheConsistencyValidator validator = context.cacheConsistencyValidator;
645 // Add sources.
646 MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
647 String path1 = '/test1.dart';
648 String path2 = '/test2.dart';
649 Source source1 = resourceProvider.newFile(path1, '// 1-1').createSource();
650 Source source2 = resourceProvider.newFile(path2, '// 2-1').createSource();
651 context.applyChanges(
652 new ChangeSet()..addedSource(source1)..addedSource(source2));
653 // Verify.
654 expect(validator.getSourcesToComputeModificationTimes(),
655 unorderedEquals([source1, source2]));
656 }
657
658 void test_computeDocumentationComment_class_block() {
386 String comment = "/** Comment */"; 659 String comment = "/** Comment */";
387 Source source = addSource( 660 Source source = addSource(
388 "/test.dart", 661 "/test.dart",
389 """ 662 """
390 $comment 663 $comment
391 class A {}"""); 664 class A {}""");
392 LibraryElement libraryElement = context.computeLibraryElement(source); 665 LibraryElement libraryElement = context.computeLibraryElement(source);
393 expect(libraryElement, isNotNull); 666 expect(libraryElement, isNotNull);
394 ClassElement classElement = libraryElement.definingCompilationUnit.types[0]; 667 ClassElement classElement = libraryElement.definingCompilationUnit.types[0];
395 expect(libraryElement, isNotNull); 668 expect(libraryElement, isNotNull);
396 expect(context.computeDocumentationComment(classElement), comment); 669 expect(context.computeDocumentationComment(classElement), comment);
397 } 670 }
398 671
399 void test_computeDocumentationComment_none() { 672 void test_computeDocumentationComment_class_none() {
400 Source source = addSource("/test.dart", "class A {}"); 673 Source source = addSource("/test.dart", "class A {}");
401 LibraryElement libraryElement = context.computeLibraryElement(source); 674 LibraryElement libraryElement = context.computeLibraryElement(source);
402 expect(libraryElement, isNotNull); 675 expect(libraryElement, isNotNull);
403 ClassElement classElement = libraryElement.definingCompilationUnit.types[0]; 676 ClassElement classElement = libraryElement.definingCompilationUnit.types[0];
404 expect(libraryElement, isNotNull); 677 expect(libraryElement, isNotNull);
405 expect(context.computeDocumentationComment(classElement), isNull); 678 expect(context.computeDocumentationComment(classElement), isNull);
406 } 679 }
407 680
408 void test_computeDocumentationComment_null() { 681 void test_computeDocumentationComment_class_singleLine_multiple_EOL_n() {
409 expect(context.computeDocumentationComment(null), isNull);
410 }
411
412 void test_computeDocumentationComment_singleLine_multiple_EOL_n() {
413 String comment = "/// line 1\n/// line 2\n/// line 3\n"; 682 String comment = "/// line 1\n/// line 2\n/// line 3\n";
414 Source source = addSource("/test.dart", "${comment}class A {}"); 683 Source source = addSource("/test.dart", "${comment}class A {}");
415 LibraryElement libraryElement = context.computeLibraryElement(source); 684 LibraryElement libraryElement = context.computeLibraryElement(source);
416 expect(libraryElement, isNotNull); 685 expect(libraryElement, isNotNull);
417 ClassElement classElement = libraryElement.definingCompilationUnit.types[0]; 686 ClassElement classElement = libraryElement.definingCompilationUnit.types[0];
418 expect(libraryElement, isNotNull); 687 expect(libraryElement, isNotNull);
419 String actual = context.computeDocumentationComment(classElement); 688 String actual = context.computeDocumentationComment(classElement);
420 expect(actual, "/// line 1\n/// line 2\n/// line 3"); 689 expect(actual, "/// line 1\n/// line 2\n/// line 3");
421 } 690 }
422 691
423 void test_computeDocumentationComment_singleLine_multiple_EOL_rn() { 692 void test_computeDocumentationComment_class_singleLine_multiple_EOL_rn() {
424 String comment = "/// line 1\r\n/// line 2\r\n/// line 3\r\n"; 693 String comment = "/// line 1\r\n/// line 2\r\n/// line 3\r\n";
425 Source source = addSource("/test.dart", "${comment}class A {}"); 694 Source source = addSource("/test.dart", "${comment}class A {}");
426 LibraryElement libraryElement = context.computeLibraryElement(source); 695 LibraryElement libraryElement = context.computeLibraryElement(source);
427 expect(libraryElement, isNotNull); 696 expect(libraryElement, isNotNull);
428 ClassElement classElement = libraryElement.definingCompilationUnit.types[0]; 697 ClassElement classElement = libraryElement.definingCompilationUnit.types[0];
429 expect(libraryElement, isNotNull); 698 expect(libraryElement, isNotNull);
430 String actual = context.computeDocumentationComment(classElement); 699 String actual = context.computeDocumentationComment(classElement);
431 expect(actual, "/// line 1\n/// line 2\n/// line 3"); 700 expect(actual, "/// line 1\n/// line 2\n/// line 3");
432 } 701 }
433 702
703 void test_computeDocumentationComment_exportDirective_block() {
704 String comment = '/** Comment */';
705 Source source = addSource(
706 "/test.dart",
707 '''
708 $comment
709 export 'dart:async';
710 ''');
711 LibraryElement libraryElement = context.computeLibraryElement(source);
712 expect(libraryElement, isNotNull);
713 ExportElement exportElement = libraryElement.exports[0];
714 expect(context.computeDocumentationComment(exportElement), comment);
715 }
716
717 void test_computeDocumentationComment_importDirective_block() {
718 String comment = '/** Comment */';
719 Source source = addSource(
720 "/test.dart",
721 '''
722 $comment
723 import 'dart:async';
724 ''');
725 LibraryElement libraryElement = context.computeLibraryElement(source);
726 expect(libraryElement, isNotNull);
727 ImportElement importElement = libraryElement.imports[0];
728 expect(context.computeDocumentationComment(importElement), comment);
729 }
730
731 void test_computeDocumentationComment_libraryDirective_block() {
732 String comment = '/** Comment */';
733 Source source = addSource(
734 "/test.dart",
735 '''
736 $comment
737 library lib;
738 ''');
739 LibraryElement libraryElement = context.computeLibraryElement(source);
740 expect(libraryElement, isNotNull);
741 expect(context.computeDocumentationComment(libraryElement), comment);
742 }
743
744 void test_computeDocumentationComment_null() {
745 expect(context.computeDocumentationComment(null), isNull);
746 }
747
748 void test_computeErrors_dart_malformedCode() {
749 Source source = addSource("/lib.dart", "final int , = 42;");
750 List<AnalysisError> errors = context.computeErrors(source);
751 expect(errors, isNotNull);
752 expect(errors.length > 0, isTrue);
753 }
754
434 void test_computeErrors_dart_none() { 755 void test_computeErrors_dart_none() {
435 Source source = addSource("/lib.dart", "library lib;"); 756 Source source = addSource("/lib.dart", "library lib;");
436 List<AnalysisError> errors = context.computeErrors(source); 757 List<AnalysisError> errors = context.computeErrors(source);
437 expect(errors, hasLength(0)); 758 expect(errors, hasLength(0));
438 } 759 }
439 760
440 void test_computeErrors_dart_part() { 761 void test_computeErrors_dart_part() {
441 Source librarySource = 762 Source librarySource =
442 addSource("/lib.dart", "library lib; part 'part.dart';"); 763 addSource("/lib.dart", "library lib; part 'part.dart';");
443 Source partSource = addSource("/part.dart", "part of 'lib';"); 764 Source partSource = addSource("/part.dart", "part of 'lib';");
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 // be available immediately. 861 // be available immediately.
541 _performPendingAnalysisTasks(); 862 _performPendingAnalysisTasks();
542 _flushAst(source); 863 _flushAst(source);
543 bool completed = false; 864 bool completed = false;
544 context 865 context
545 .computeResolvedCompilationUnitAsync(source, source) 866 .computeResolvedCompilationUnitAsync(source, source)
546 .then((CompilationUnit unit) { 867 .then((CompilationUnit unit) {
547 expect(unit, isNotNull); 868 expect(unit, isNotNull);
548 completed = true; 869 completed = true;
549 }); 870 });
550 return pumpEventQueue().then((_) { 871 return pumpEventQueue()
551 expect(completed, isFalse); 872 .then((_) {
552 _performPendingAnalysisTasks(); 873 expect(completed, isFalse);
553 }).then((_) => pumpEventQueue()).then((_) { 874 _performPendingAnalysisTasks();
554 expect(completed, isTrue); 875 })
555 }); 876 .then((_) => pumpEventQueue())
877 .then((_) {
878 expect(completed, isTrue);
879 });
556 } 880 }
557 881
558 Future test_computeResolvedCompilationUnitAsync_afterDispose() { 882 Future test_computeResolvedCompilationUnitAsync_afterDispose() {
559 Source source = addSource("/lib.dart", "library lib;"); 883 Source source = addSource("/lib.dart", "library lib;");
560 // Complete all pending analysis tasks and flush the AST so that it won't 884 // Complete all pending analysis tasks and flush the AST so that it won't
561 // be available immediately. 885 // be available immediately.
562 _performPendingAnalysisTasks(); 886 _performPendingAnalysisTasks();
563 _flushAst(source); 887 _flushAst(source);
564 // Dispose of the context. 888 // Dispose of the context.
565 context.dispose(); 889 context.dispose();
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 Future test_computeResolvedCompilationUnitAsync_noCacheEntry() { 958 Future test_computeResolvedCompilationUnitAsync_noCacheEntry() {
635 Source librarySource = addSource("/lib.dart", "library lib;"); 959 Source librarySource = addSource("/lib.dart", "library lib;");
636 Source partSource = addSource("/part.dart", "part of foo;"); 960 Source partSource = addSource("/part.dart", "part of foo;");
637 bool completed = false; 961 bool completed = false;
638 context 962 context
639 .computeResolvedCompilationUnitAsync(partSource, librarySource) 963 .computeResolvedCompilationUnitAsync(partSource, librarySource)
640 .then((CompilationUnit unit) { 964 .then((CompilationUnit unit) {
641 expect(unit, isNotNull); 965 expect(unit, isNotNull);
642 completed = true; 966 completed = true;
643 }); 967 });
644 return pumpEventQueue().then((_) { 968 return pumpEventQueue()
645 expect(completed, isFalse); 969 .then((_) {
646 _performPendingAnalysisTasks(); 970 expect(completed, isFalse);
647 }).then((_) => pumpEventQueue()).then((_) { 971 _performPendingAnalysisTasks();
648 expect(completed, isTrue); 972 })
649 }); 973 .then((_) => pumpEventQueue())
974 .then((_) {
975 expect(completed, isTrue);
976 });
650 } 977 }
651 978
652 void test_configurationData() { 979 void test_configurationData() {
653 var key = new ResultDescriptor('test_key', ''); 980 var key = new ResultDescriptor('test_key', 'TEST_DEFAULT');
654 var testData = ['test', 'data']; 981 var testData = ['test', 'data'];
655 context.setConfigurationData(key, testData); 982 context.setConfigurationData(key, testData);
656 expect(context.getConfigurationData(key), testData); 983 expect(context.getConfigurationData(key), testData);
657 var unusedKey = new ResultDescriptor('unused_key', ''); 984 var unusedKey = new ResultDescriptor('unused_key', 'UNUSED_DEFAULT');
658 expect(context.getConfigurationData(unusedKey), null); 985 expect(context.getConfigurationData(unusedKey), 'UNUSED_DEFAULT');
659 } 986 }
660 987
661 void test_dispose() { 988 void test_dispose() {
662 expect(context.isDisposed, isFalse); 989 expect(context.isDisposed, isFalse);
663 context.dispose(); 990 context.dispose();
664 expect(context.isDisposed, isTrue); 991 expect(context.isDisposed, isTrue);
665 } 992 }
666 993
667 void test_ensureResolvedDartUnits_definingUnit_hasResolved() { 994 void test_ensureResolvedDartUnits_definingUnit_hasResolved() {
668 Source source = addSource('/test.dart', ''); 995 Source source = addSource('/test.dart', '');
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 expect(context.exists(null), isFalse); 1100 expect(context.exists(null), isFalse);
774 } 1101 }
775 1102
776 void test_exists_overridden() { 1103 void test_exists_overridden() {
777 Source source = new TestSource(); 1104 Source source = new TestSource();
778 context.setContents(source, ""); 1105 context.setContents(source, "");
779 expect(context.exists(source), isTrue); 1106 expect(context.exists(source), isTrue);
780 } 1107 }
781 1108
782 void test_exists_true() { 1109 void test_exists_true() {
783 expect(context.exists(new AnalysisContextImplTest_Source_exists_true()), 1110 expect(context.exists(new _AnalysisContextImplTest_Source_exists_true()),
784 isTrue); 1111 isTrue);
785 } 1112 }
786 1113
1114 void test_flushResolvedUnit_updateFile_dontNotify() {
1115 String oldCode = '';
1116 String newCode = r'''
1117 import 'dart:async';
1118 ''';
1119 String path = '/test.dart';
1120 Source source = resourceProvider.newFile(path, oldCode).createSource();
1121 context.applyChanges(new ChangeSet()..addedSource(source));
1122 context.resolveCompilationUnit2(source, source);
1123 // Flush all results units.
1124 context.analysisCache.flush((target, result) {
1125 if (target.source == source) {
1126 return RESOLVED_UNIT_RESULTS.contains(result);
1127 }
1128 return false;
1129 });
1130 // Update the file, but don't notify the context.
1131 resourceProvider.updateFile(path, newCode);
1132 // Driver must detect that the file was changed and recover.
1133 CompilationUnit unit = context.resolveCompilationUnit2(source, source);
1134 expect(unit, isNotNull);
1135 }
1136
1137 void test_flushResolvedUnit_updateFile_dontNotify2() {
1138 String oldCode = r'''
1139 main() {}
1140 ''';
1141 String newCode = r'''
1142 import 'dart:async';
1143 main() {}
1144 ''';
1145 String path = '/test.dart';
1146 Source source = resourceProvider.newFile(path, oldCode).createSource();
1147 context.applyChanges(new ChangeSet()..addedSource(source));
1148 context.resolveCompilationUnit2(source, source);
1149 // Flush all results units.
1150 context.analysisCache.flush((target, result) {
1151 if (target.source == source) {
1152 if (target.source == source) {
1153 return RESOLVED_UNIT_RESULTS.contains(result);
1154 }
1155 }
1156 return false;
1157 });
1158 // Update the file, but don't notify the context.
1159 resourceProvider.updateFile(path, newCode);
1160 // Driver must detect that the file was changed and recover.
1161 CompilationUnit unit = context.resolveCompilationUnit2(source, source);
1162 expect(unit, isNotNull);
1163 }
1164
787 void test_getAnalysisOptions() { 1165 void test_getAnalysisOptions() {
788 expect(context.analysisOptions, isNotNull); 1166 expect(context.analysisOptions, isNotNull);
789 } 1167 }
790 1168
791 void test_getContents_fromSource() { 1169 void test_getContents_fromSource() {
792 String content = "library lib;"; 1170 String content = "library lib;";
793 TimestampedData<String> contents = 1171 TimestampedData<String> contents =
794 context.getContents(new TestSource('/test.dart', content)); 1172 context.getContents(new TestSource('/test.dart', content));
795 expect(contents.data.toString(), content); 1173 expect(contents.data.toString(), content);
796 } 1174 }
797 1175
1176 void test_getContents_notOverridden() {
1177 String content = "library lib;";
1178 Source source = new TestSource('/test.dart', content);
1179 context.setContents(source, "part of lib;");
1180 context.setContents(source, null);
1181 TimestampedData<String> contents = context.getContents(source);
1182 expect(contents.data.toString(), content);
1183 }
1184
798 void test_getContents_overridden() { 1185 void test_getContents_overridden() {
799 String content = "library lib;"; 1186 String content = "library lib;";
800 Source source = new TestSource(); 1187 Source source = new TestSource();
801 context.setContents(source, content); 1188 context.setContents(source, content);
802 TimestampedData<String> contents = context.getContents(source); 1189 TimestampedData<String> contents = context.getContents(source);
803 expect(contents.data.toString(), content); 1190 expect(contents.data.toString(), content);
804 } 1191 }
805
806 void test_getContents_unoverridden() {
807 String content = "library lib;";
808 Source source = new TestSource('/test.dart', content);
809 context.setContents(source, "part of lib;");
810 context.setContents(source, null);
811 TimestampedData<String> contents = context.getContents(source);
812 expect(contents.data.toString(), content);
813 }
814 1192
815 void test_getDeclaredVariables() { 1193 void test_getDeclaredVariables() {
816 expect(context.declaredVariables, isNotNull); 1194 expect(context.declaredVariables, isNotNull);
817 } 1195 }
818 1196
819 void test_getElement() { 1197 void test_getElement() {
820 LibraryElement core = 1198 LibraryElement core =
821 context.computeLibraryElement(sourceFactory.forUri("dart:core")); 1199 context.computeLibraryElement(sourceFactory.forUri("dart:core"));
822 expect(core, isNotNull); 1200 expect(core, isNotNull);
823 ClassElement classObject = 1201 ClassElement classObject =
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 Source source = addSource("/test.html", "<html></html>"); 1273 Source source = addSource("/test.html", "<html></html>");
896 AnalysisErrorInfo errorInfo = context.getErrors(source); 1274 AnalysisErrorInfo errorInfo = context.getErrors(source);
897 expect(errorInfo, isNotNull); 1275 expect(errorInfo, isNotNull);
898 List<AnalysisError> errors = errorInfo.errors; 1276 List<AnalysisError> errors = errorInfo.errors;
899 expect(errors, hasLength(0)); 1277 expect(errors, hasLength(0));
900 context.computeErrors(source); 1278 context.computeErrors(source);
901 errors = errorInfo.errors; 1279 errors = errorInfo.errors;
902 expect(errors, hasLength(0)); 1280 expect(errors, hasLength(0));
903 } 1281 }
904 1282
905 void test_getErrors_html_some() {
906 Source source = addSource(
907 "/test.html",
908 r'''
909 <html><head>
910 <script type='application/dart' src='test.dart'/>
911 </head></html>''');
912 AnalysisErrorInfo errorInfo = context.getErrors(source);
913 expect(errorInfo, isNotNull);
914 List<AnalysisError> errors = errorInfo.errors;
915 expect(errors, hasLength(0));
916 errors = context.computeErrors(source);
917 expect(errors, hasLength(2));
918 }
919
920 void test_getHtmlFilesReferencing_html() { 1283 void test_getHtmlFilesReferencing_html() {
921 Source htmlSource = addSource( 1284 Source htmlSource = addSource(
922 "/test.html", 1285 "/test.html",
923 r''' 1286 r'''
924 <html><head> 1287 <html><head>
925 <script type='application/dart' src='test.dart'/> 1288 <script type='application/dart' src='test.dart'/>
926 <script type='application/dart' src='test.js'/> 1289 <script type='application/dart' src='test.js'/>
927 </head></html>'''); 1290 </head></html>''');
928 Source librarySource = addSource("/test.dart", "library lib;"); 1291 Source librarySource = addSource("/test.dart", "library lib;");
929 Source secondHtmlSource = addSource("/test.html", "<html></html>"); 1292 Source secondHtmlSource = addSource("/test.html", "<html></html>");
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
1030 main() {}'''); 1393 main() {}''');
1031 context.computeLibraryElement(source); 1394 context.computeLibraryElement(source);
1032 sources = context.launchableClientLibrarySources; 1395 sources = context.launchableClientLibrarySources;
1033 expect(sources, unorderedEquals([source])); 1396 expect(sources, unorderedEquals([source]));
1034 } 1397 }
1035 1398
1036 void test_getLaunchableClientLibrarySources_importsHtml_implicitly() { 1399 void test_getLaunchableClientLibrarySources_importsHtml_implicitly() {
1037 List<Source> sources = context.launchableClientLibrarySources; 1400 List<Source> sources = context.launchableClientLibrarySources;
1038 expect(sources, isEmpty); 1401 expect(sources, isEmpty);
1039 addSource( 1402 addSource(
1040 "/a.dart", 1403 '/a.dart',
1041 r''' 1404 r'''
1042 import 'dart:html'; 1405 import 'dart:html';
1043 '''); 1406 ''');
1044 Source source = addSource( 1407 Source source = addSource(
1045 "/test.dart", 1408 "/test.dart",
1046 r''' 1409 r'''
1047 import 'a.dart'; 1410 import 'a.dart';
1048 main() {}'''); 1411 main() {}''');
1049 context.computeLibraryElement(source); 1412 _analyzeAll_assertFinished();
1050 sources = context.launchableClientLibrarySources; 1413 sources = context.launchableClientLibrarySources;
1051 expect(sources, unorderedEquals([source])); 1414 expect(sources, unorderedEquals([source]));
1052 } 1415 }
1053 1416
1054 void test_getLaunchableClientLibrarySources_importsHtml_implicitly2() { 1417 void test_getLaunchableClientLibrarySources_importsHtml_implicitly2() {
1055 List<Source> sources = context.launchableClientLibrarySources; 1418 List<Source> sources = context.launchableClientLibrarySources;
1056 expect(sources, isEmpty); 1419 expect(sources, isEmpty);
1057 addSource( 1420 addSource(
1058 "/a.dart", 1421 '/a.dart',
1059 r''' 1422 r'''
1060 export 'dart:html'; 1423 export 'dart:html';
1061 '''); 1424 ''');
1062 Source source = addSource( 1425 Source source = addSource(
1063 "/test.dart", 1426 "/test.dart",
1064 r''' 1427 r'''
1065 import 'a.dart'; 1428 import 'a.dart';
1066 main() {}'''); 1429 main() {}''');
1067 context.computeLibraryElement(source); 1430 _analyzeAll_assertFinished();
1068 sources = context.launchableClientLibrarySources; 1431 sources = context.launchableClientLibrarySources;
1069 expect(sources, unorderedEquals([source])); 1432 expect(sources, unorderedEquals([source]));
1070 } 1433 }
1071 1434
1072 void test_getLaunchableServerLibrarySources() { 1435 void test_getLaunchableServerLibrarySources() {
1073 expect(context.launchableServerLibrarySources, isEmpty); 1436 expect(context.launchableServerLibrarySources, isEmpty);
1074 Source source = addSource("/test.dart", "main() {}"); 1437 Source source = addSource("/test.dart", "main() {}");
1075 context.computeLibraryElement(source); 1438 context.computeLibraryElement(source);
1076 expect(context.launchableServerLibrarySources, unorderedEquals([source])); 1439 expect(context.launchableServerLibrarySources, unorderedEquals([source]));
1077 } 1440 }
1078 1441
1079 void test_getLaunchableServerLibrarySources_importsHtml_explicitly() { 1442 void test_getLaunchableServerLibrarySources_importsHtml_explicitly() {
1080 Source source = addSource( 1443 Source source = addSource(
1081 "/test.dart", 1444 "/test.dart",
1082 r''' 1445 r'''
1083 import 'dart:html'; 1446 import 'dart:html';
1084 main() {} 1447 main() {}
1085 '''); 1448 ''');
1086 context.computeLibraryElement(source); 1449 context.computeLibraryElement(source);
1087 expect(context.launchableServerLibrarySources, isEmpty); 1450 expect(context.launchableServerLibrarySources, isEmpty);
1088 } 1451 }
1089 1452
1090 void test_getLaunchableServerLibrarySources_importsHtml_implicitly() { 1453 void test_getLaunchableServerLibrarySources_importsHtml_implicitly() {
1091 addSource( 1454 addSource(
1092 "/imports_html.dart", 1455 "/imports_html.dart",
1093 r''' 1456 r'''
1094 import 'dart:html'; 1457 import 'dart:html';
1095 '''); 1458 ''');
1096 Source source = addSource( 1459 addSource(
1097 "/test.dart", 1460 "/test.dart",
1098 r''' 1461 r'''
1099 import 'imports_html.dart'; 1462 import 'imports_html.dart';
1100 main() {}'''); 1463 main() {}''');
1101 context.computeLibraryElement(source); 1464 _analyzeAll_assertFinished();
1102 expect(context.launchableServerLibrarySources, isEmpty); 1465 expect(context.launchableServerLibrarySources, isEmpty);
1103 } 1466 }
1104 1467
1105 void test_getLaunchableServerLibrarySources_noMain() { 1468 void test_getLaunchableServerLibrarySources_noMain() {
1106 Source source = addSource("/test.dart", ''); 1469 Source source = addSource("/test.dart", '');
1107 context.computeLibraryElement(source); 1470 context.computeLibraryElement(source);
1108 expect(context.launchableServerLibrarySources, isEmpty); 1471 expect(context.launchableServerLibrarySources, isEmpty);
1109 } 1472 }
1110 1473
1111 void test_getLibrariesContaining() { 1474 void test_getLibrariesContaining() {
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
1248 expect(info, isNull); 1611 expect(info, isNull);
1249 context.parseCompilationUnit(source); 1612 context.parseCompilationUnit(source);
1250 info = context.getLineInfo(source); 1613 info = context.getLineInfo(source);
1251 expect(info, isNotNull); 1614 expect(info, isNotNull);
1252 } 1615 }
1253 1616
1254 void test_getModificationStamp_fromSource() { 1617 void test_getModificationStamp_fromSource() {
1255 int stamp = 42; 1618 int stamp = 42;
1256 expect( 1619 expect(
1257 context.getModificationStamp( 1620 context.getModificationStamp(
1258 new AnalysisContextImplTest_Source_getModificationStamp_fromSource( 1621 new _AnalysisContextImplTest_Source_getModificationStamp_fromSource(
1259 stamp)), 1622 stamp)),
1260 stamp); 1623 stamp);
1261 } 1624 }
1262 1625
1263 void test_getModificationStamp_overridden() { 1626 void test_getModificationStamp_overridden() {
1264 int stamp = 42; 1627 int stamp = 42;
1265 Source source = 1628 Source source =
1266 new AnalysisContextImplTest_Source_getModificationStamp_overridden( 1629 new _AnalysisContextImplTest_Source_getModificationStamp_overridden(
1267 stamp); 1630 stamp);
1268 context.setContents(source, ""); 1631 context.setContents(source, "");
1269 expect(stamp != context.getModificationStamp(source), isTrue); 1632 expect(stamp != context.getModificationStamp(source), isTrue);
1270 } 1633 }
1271 1634
1272 void test_getPublicNamespace_element() { 1635 void test_getPublicNamespace_element() {
1273 Source source = addSource("/test.dart", "class A {}"); 1636 Source source = addSource("/test.dart", "class A {}");
1274 LibraryElement library = context.computeLibraryElement(source); 1637 LibraryElement library = context.computeLibraryElement(source);
1275 expect(library, isNotNull); 1638 expect(library, isNotNull);
1276 Namespace namespace = context.getPublicNamespace(library); 1639 Namespace namespace = context.getPublicNamespace(library);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1316 1679
1317 TestSourceWithUri source2 = 1680 TestSourceWithUri source2 =
1318 new TestSourceWithUri(filePath, Uri.parse('package:foo/file.dart')); 1681 new TestSourceWithUri(filePath, Uri.parse('package:foo/file.dart'));
1319 expected.add(source2); 1682 expected.add(source2);
1320 changeSet.addedSource(source2); 1683 changeSet.addedSource(source2);
1321 1684
1322 context.applyChanges(changeSet); 1685 context.applyChanges(changeSet);
1323 expect(context.getSourcesWithFullName(filePath), unorderedEquals(expected)); 1686 expect(context.getSourcesWithFullName(filePath), unorderedEquals(expected));
1324 } 1687 }
1325 1688
1326 void test_getStatistics() {
1327 AnalysisContextStatistics statistics = context.statistics;
1328 expect(statistics, isNotNull);
1329 // The following lines are fragile.
1330 // The values depend on the number of libraries in the SDK.
1331 // assertLength(0, statistics.getCacheRows());
1332 // assertLength(0, statistics.getExceptions());
1333 // assertLength(0, statistics.getSources());
1334 }
1335
1336 void test_handleContentsChanged() { 1689 void test_handleContentsChanged() {
1337 ContentCache contentCache = new ContentCache(); 1690 ContentCache contentCache = new ContentCache();
1338 context.contentCache = contentCache; 1691 context.contentCache = contentCache;
1339 String oldContents = 'foo() {}'; 1692 String oldContents = 'foo() {}';
1340 String newContents = 'bar() {}'; 1693 String newContents = 'bar() {}';
1341 // old contents 1694 // old contents
1342 Source source = addSource("/test.dart", oldContents); 1695 Source source = addSource("/test.dart", oldContents);
1343 _analyzeAll_assertFinished(); 1696 _analyzeAll_assertFinished();
1344 expect(context.getResolvedCompilationUnit2(source, source), isNotNull); 1697 expect(context.getResolvedCompilationUnit2(source, source), isNotNull);
1345 // new contents 1698 // new contents
1346 contentCache.setContents(source, newContents); 1699 contentCache.setContents(source, newContents);
1347 context.handleContentsChanged(source, oldContents, newContents, true); 1700 context.handleContentsChanged(source, oldContents, newContents, true);
1348 // there is some work to do 1701 // there is some work to do
1349 AnalysisResult analysisResult = context.performAnalysisTask(); 1702 AnalysisResult analysisResult = context.performAnalysisTask();
1350 expect(analysisResult.changeNotices, isNotNull); 1703 expect(analysisResult.changeNotices, isNotNull);
1351 } 1704 }
1352 1705
1706 void test_handleContentsChanged_incremental_newContentsNull() {
1707 context.analysisOptions = new AnalysisOptionsImpl()..incremental = true;
1708 ContentCache contentCache = new ContentCache();
1709 context.contentCache = contentCache;
1710 // old contents
1711 String oldContents = 'foo() {}';
1712 Source source = resourceProvider.getFile('/test.dart').createSource();
1713 contentCache.setContents(source, oldContents);
1714 expect(context.computeLibraryElement(source), isNotNull);
1715 // new contents
1716 String newContents = null;
1717 contentCache.setContents(source, newContents);
1718 context.handleContentsChanged(source, oldContents, newContents, true);
1719 expect(context.getLibraryElement(source), isNull);
1720 }
1721
1722 void test_handleContentsChanged_noOriginal_sameAsFile() {
1723 ContentCache contentCache = new ContentCache();
1724 context.contentCache = contentCache;
1725 // Add the source.
1726 String code = 'foo() {}';
1727 Source source = addSource("/test.dart", code);
1728 _analyzeAll_assertFinished();
1729 expect(context.getResolvedCompilationUnit2(source, source), isNotNull);
1730 // Update the content cache, and notify that we updated the source.
1731 // We pass "null" as "originalContents" because the was no one.
1732 contentCache.setContents(source, code);
1733 context.handleContentsChanged(source, null, code, true);
1734 expect(context.getResolvedCompilationUnit2(source, source), isNotNull);
1735 }
1736
1737 void test_handleContentsChanged_noOriginal_sameAsFile_butFileUpdated() {
1738 ContentCache contentCache = new ContentCache();
1739 context.contentCache = contentCache;
1740 // Add the source.
1741 String oldCode = 'foo() {}';
1742 String newCode = 'bar() {}';
1743 var file = resourceProvider.newFile('/test.dart', oldCode);
1744 Source source = file.createSource();
1745 context.applyChanges(new ChangeSet()..addedSource(source));
1746 _analyzeAll_assertFinished();
1747 expect(context.getResolvedCompilationUnit2(source, source), isNotNull);
1748 // Test for the race condition.
1749 // 1. Update the file.
1750 // 2. Update the content cache.
1751 // 3. Notify the context, and because this is the first time when we
1752 // update the content cache, we don't know "originalContents".
1753 // The source must be invalidated, because it has different contents now.
1754 resourceProvider.updateFile('/test.dart', newCode);
1755 contentCache.setContents(source, newCode);
1756 context.handleContentsChanged(source, null, newCode, true);
1757 expect(context.getResolvedCompilationUnit2(source, source), isNull);
1758 }
1759
1353 Future test_implicitAnalysisEvents_added() async { 1760 Future test_implicitAnalysisEvents_added() async {
1354 AnalyzedSourcesListener listener = new AnalyzedSourcesListener(); 1761 AnalyzedSourcesListener listener = new AnalyzedSourcesListener();
1355 context.implicitAnalysisEvents.listen(listener.onData); 1762 context.implicitAnalysisEvents.listen(listener.onData);
1356 // 1763 //
1357 // Create a file that references an file that is not explicitly being 1764 // Create a file that references an file that is not explicitly being
1358 // analyzed and fully analyze it. Ensure that the listener is told about 1765 // analyzed and fully analyze it. Ensure that the listener is told about
1359 // the implicitly analyzed file. 1766 // the implicitly analyzed file.
1360 // 1767 //
1361 Source sourceA = newSource('/a.dart', "library a; import 'b.dart';"); 1768 Source sourceA = newSource('/a.dart', "library a; import 'b.dart';");
1362 Source sourceB = newSource('/b.dart', "library b;"); 1769 Source sourceB = newSource('/b.dart', "library b;");
(...skipping 17 matching lines...) Expand all
1380 context.computeLibraryElement(source); 1787 context.computeLibraryElement(source);
1381 expect(context.isClientLibrary(source), isTrue); 1788 expect(context.isClientLibrary(source), isTrue);
1382 expect(context.isServerLibrary(source), isFalse); 1789 expect(context.isServerLibrary(source), isFalse);
1383 } 1790 }
1384 1791
1385 void test_isClientLibrary_html() { 1792 void test_isClientLibrary_html() {
1386 Source source = addSource("/test.html", "<html></html>"); 1793 Source source = addSource("/test.html", "<html></html>");
1387 expect(context.isClientLibrary(source), isFalse); 1794 expect(context.isClientLibrary(source), isFalse);
1388 } 1795 }
1389 1796
1797 void test_isClientLibrary_unknown() {
1798 Source source = newSource("/test.dart");
1799 expect(context.isClientLibrary(source), isFalse);
1800 }
1801
1390 void test_isServerLibrary_dart() { 1802 void test_isServerLibrary_dart() {
1391 Source source = addSource( 1803 Source source = addSource(
1392 "/test.dart", 1804 "/test.dart",
1393 r''' 1805 r'''
1394 library lib; 1806 library lib;
1395 1807
1396 main() {}'''); 1808 main() {}''');
1397 expect(context.isClientLibrary(source), isFalse); 1809 expect(context.isClientLibrary(source), isFalse);
1398 expect(context.isServerLibrary(source), isFalse); 1810 expect(context.isServerLibrary(source), isFalse);
1399 context.computeLibraryElement(source); 1811 context.computeLibraryElement(source);
1400 expect(context.isClientLibrary(source), isFalse); 1812 expect(context.isClientLibrary(source), isFalse);
1401 expect(context.isServerLibrary(source), isTrue); 1813 expect(context.isServerLibrary(source), isTrue);
1402 } 1814 }
1403 1815
1404 void test_isServerLibrary_html() { 1816 void test_isServerLibrary_html() {
1405 Source source = addSource("/test.html", "<html></html>"); 1817 Source source = addSource("/test.html", "<html></html>");
1406 expect(context.isServerLibrary(source), isFalse); 1818 expect(context.isServerLibrary(source), isFalse);
1407 } 1819 }
1408 1820
1821 void test_isServerLibrary_unknown() {
1822 Source source = newSource("/test.dart");
1823 expect(context.isServerLibrary(source), isFalse);
1824 }
1825
1826 void test_onResultInvalidated_removeSource() {
1827 Source source = addSource('/test.dart', 'main() {}');
1828 _analyzeAll_assertFinished();
1829 // listen for changes
1830 bool listenerInvoked = false;
1831 context.onResultChanged(RESOLVED_UNIT).listen((ResultChangedEvent event) {
1832 Source eventSource = event.target.source;
1833 expect(event.wasComputed, isFalse);
1834 expect(event.wasInvalidated, isTrue);
1835 expect(event.descriptor, RESOLVED_UNIT);
1836 expect(eventSource, source);
1837 listenerInvoked = true;
1838 });
1839 // apply changes
1840 expect(listenerInvoked, false);
1841 context.applyChanges(new ChangeSet()..removedSource(source));
1842 // verify
1843 expect(listenerInvoked, isTrue);
1844 }
1845
1846 void test_onResultInvalidated_setContents() {
1847 Source source = addSource('/test.dart', 'main() {}');
1848 _analyzeAll_assertFinished();
1849 // listen for changes
1850 bool listenerInvoked = false;
1851 context.onResultChanged(RESOLVED_UNIT).listen((ResultChangedEvent event) {
1852 Source eventSource = event.target.source;
1853 expect(event.wasComputed, isFalse);
1854 expect(event.wasInvalidated, isTrue);
1855 expect(event.descriptor, RESOLVED_UNIT);
1856 expect(eventSource, source);
1857 listenerInvoked = true;
1858 });
1859 // apply changes
1860 expect(listenerInvoked, false);
1861 context.setContents(source, 'class B {}');
1862 // verify
1863 expect(listenerInvoked, isTrue);
1864 }
1865
1409 void test_parseCompilationUnit_errors() { 1866 void test_parseCompilationUnit_errors() {
1410 Source source = addSource("/lib.dart", "library {"); 1867 Source source = addSource("/lib.dart", "library {");
1411 CompilationUnit compilationUnit = context.parseCompilationUnit(source); 1868 CompilationUnit compilationUnit = context.parseCompilationUnit(source);
1412 expect(compilationUnit, isNotNull); 1869 expect(compilationUnit, isNotNull);
1413 var errorInfo = context.getErrors(source); 1870 var errorInfo = context.getErrors(source);
1414 expect(errorInfo, isNotNull); 1871 expect(errorInfo, isNotNull);
1415 List<AnalysisError> errors = errorInfo.errors; 1872 List<AnalysisError> errors = errorInfo.errors;
1416 expect(errors, isNotNull); 1873 expect(errors, isNotNull);
1417 expect(errors.length > 0, isTrue); 1874 expect(errors.length > 0, isTrue);
1418 } 1875 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1478 <body> 1935 <body>
1479 </body> 1936 </body>
1480 </html>'''); 1937 </html>''');
1481 Document document = context.parseHtmlDocument(source); 1938 Document document = context.parseHtmlDocument(source);
1482 expect(document, isNotNull); 1939 expect(document, isNotNull);
1483 List<DartScript> scripts = context.computeResult(source, DART_SCRIPTS); 1940 List<DartScript> scripts = context.computeResult(source, DART_SCRIPTS);
1484 expect(scripts, hasLength(1)); 1941 expect(scripts, hasLength(1));
1485 CompilationUnit unit = context.computeResult(scripts[0], PARSED_UNIT); 1942 CompilationUnit unit = context.computeResult(scripts[0], PARSED_UNIT);
1486 ImportDirective importNode = unit.directives[0] as ImportDirective; 1943 ImportDirective importNode = unit.directives[0] as ImportDirective;
1487 expect(importNode.uriContent, isNotNull); 1944 expect(importNode.uriContent, isNotNull);
1488 expect(importNode.source, libSource); 1945 expect(importNode.uriSource, libSource);
1489 } 1946 }
1490 1947
1491 void test_performAnalysisTask_addPart() { 1948 void test_performAnalysisTask_addPart() {
1492 Source libSource = addSource( 1949 Source libSource = addSource(
1493 "/lib.dart", 1950 "/lib.dart",
1494 r''' 1951 r'''
1495 library lib; 1952 library lib;
1496 part 'part.dart';'''); 1953 part 'part.dart';''');
1497 // run all tasks without part 1954 // run all tasks without part
1498 _analyzeAll_assertFinished(); 1955 _analyzeAll_assertFinished();
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
1655 addSource("/test.dart", "library lib; part 'test-part.dart';"); 2112 addSource("/test.dart", "library lib; part 'test-part.dart';");
1656 Source partSource = addSource("/test-part.dart", "part of lib;"); 2113 Source partSource = addSource("/test-part.dart", "part of lib;");
1657 _analyzeAll_assertFinished(); 2114 _analyzeAll_assertFinished();
1658 expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull, 2115 expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1659 reason: "library resolved 1"); 2116 reason: "library resolved 1");
1660 expect( 2117 expect(
1661 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull, 2118 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1662 reason: "part resolved 1"); 2119 reason: "part resolved 1");
1663 // update and analyze #1 2120 // update and analyze #1
1664 context.setContents(partSource, "part of lib; // 1"); 2121 context.setContents(partSource, "part of lib; // 1");
1665 if (AnalysisEngine.instance.limitInvalidationInTaskModel) { 2122 if (context.analysisOptions.finerGrainedInvalidation) {
1666 expect( 2123 expect(
1667 context.getResolvedCompilationUnit2(libSource, libSource), isNotNull, 2124 context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1668 reason: "library changed 2"); 2125 reason: "library changed 2");
1669 expect( 2126 expect(
1670 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull, 2127 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1671 reason: "part changed 2"); 2128 reason: "part changed 2");
1672 } else { 2129 } else {
1673 expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull, 2130 expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull,
1674 reason: "library changed 2"); 2131 reason: "library changed 2");
1675 expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull, 2132 expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1676 reason: "part changed 2"); 2133 reason: "part changed 2");
1677 _analyzeAll_assertFinished(); 2134 _analyzeAll_assertFinished();
1678 expect( 2135 expect(
1679 context.getResolvedCompilationUnit2(libSource, libSource), isNotNull, 2136 context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1680 reason: "library resolved 2"); 2137 reason: "library resolved 2");
1681 expect( 2138 expect(
1682 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull, 2139 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1683 reason: "part resolved 2"); 2140 reason: "part resolved 2");
1684 } 2141 }
1685 // update and analyze #2 2142 // update and analyze #2
1686 context.setContents(partSource, "part of lib; // 12"); 2143 context.setContents(partSource, "part of lib; // 12");
1687 if (AnalysisEngine.instance.limitInvalidationInTaskModel) { 2144 if (context.analysisOptions.finerGrainedInvalidation) {
1688 expect( 2145 expect(
1689 context.getResolvedCompilationUnit2(libSource, libSource), isNotNull, 2146 context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1690 reason: "library changed 3"); 2147 reason: "library changed 3");
1691 expect( 2148 expect(
1692 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull, 2149 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1693 reason: "part changed 3"); 2150 reason: "part changed 3");
1694 } else { 2151 } else {
1695 expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull, 2152 expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull,
1696 reason: "library changed 3"); 2153 reason: "library changed 3");
1697 expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull, 2154 expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull,
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
1800 _removeSource(libBSource); 2257 _removeSource(libBSource);
1801 _analyzeAll_assertFinished(); 2258 _analyzeAll_assertFinished();
1802 expect( 2259 expect(
1803 context.getResolvedCompilationUnit2(libASource, libASource), isNotNull, 2260 context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
1804 reason: "libA resolved 2"); 2261 reason: "libA resolved 2");
1805 expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)), 2262 expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)),
1806 isTrue, 2263 isTrue,
1807 reason: "libA has an error"); 2264 reason: "libA has an error");
1808 } 2265 }
1809 2266
2267 void test_performAnalysisTask_interruptBy_setContents() {
2268 Source sourceA = addSource(
2269 '/a.dart',
2270 r'''
2271 library expectedToFindSemicolon
2272 ''');
2273 // Analyze to the point where some of the results stop depending on
2274 // the source content.
2275 LibrarySpecificUnit unitA = new LibrarySpecificUnit(sourceA, sourceA);
2276 for (int i = 0; i < 10000; i++) {
2277 context.performAnalysisTask();
2278 if (context.getResult(unitA, RESOLVED_UNIT3) != null) {
2279 break;
2280 }
2281 }
2282 // Update the source.
2283 // This should invalidate all the results and also reset the driver.
2284 context.setContents(sourceA, "library semicolonWasAdded;");
2285 expect(context.getResult(unitA, RESOLVED_UNIT3), isNull);
2286 expect(analysisDriver.currentWorkOrder, isNull);
2287 // Continue analysis.
2288 _analyzeAll_assertFinished();
2289 expect(context.getErrors(sourceA).errors, isEmpty);
2290 }
2291
1810 void test_performAnalysisTask_IOException() { 2292 void test_performAnalysisTask_IOException() {
1811 TestSource source = _addSourceWithException2("/test.dart", "library test;"); 2293 TestSource source = _addSourceWithException2("/test.dart", "library test;");
1812 source.generateExceptionOnRead = false; 2294 source.generateExceptionOnRead = false;
1813 _analyzeAll_assertFinished(); 2295 _analyzeAll_assertFinished();
1814 expect(source.readCount, 2); 2296 expect(source.readCount, 2);
1815 _changeSource(source, ""); 2297 _changeSource(source, "");
1816 source.generateExceptionOnRead = true; 2298 source.generateExceptionOnRead = true;
1817 _analyzeAll_assertFinished(); 2299 _analyzeAll_assertFinished();
1818 if (AnalysisEngine.instance.limitInvalidationInTaskModel) { 2300 if (context.analysisOptions.finerGrainedInvalidation) {
1819 expect(source.readCount, 7); 2301 expect(source.readCount, 7);
1820 } else { 2302 } else {
1821 expect(source.readCount, 5); 2303 expect(source.readCount, 5);
1822 } 2304 }
1823 } 2305 }
1824 2306
1825 void test_performAnalysisTask_missingPart() { 2307 void test_performAnalysisTask_missingPart() {
1826 Source source = 2308 Source source =
1827 addSource("/test.dart", "library lib; part 'no-such-file.dart';"); 2309 addSource("/test.dart", "library lib; part 'no-such-file.dart';");
1828 _analyzeAll_assertFinished(); 2310 _analyzeAll_assertFinished();
(...skipping 12 matching lines...) Expand all
1841 // while (initialTime == JavaSystem.currentTimeMillis()) { 2323 // while (initialTime == JavaSystem.currentTimeMillis()) {
1842 // Thread.sleep(1); 2324 // Thread.sleep(1);
1843 // // Force the modification time to be different. 2325 // // Force the modification time to be different.
1844 // } 2326 // }
1845 // _context.setContents(source, "library test;"); 2327 // _context.setContents(source, "library test;");
1846 // JUnitTestCase.assertTrue(initialTime != _context.getModificationStamp(sour ce)); 2328 // JUnitTestCase.assertTrue(initialTime != _context.getModificationStamp(sour ce));
1847 // _analyzeAll_assertFinished(); 2329 // _analyzeAll_assertFinished();
1848 // JUnitTestCase.assertNotNullMsg("performAnalysisTask failed to compute an e lement model", _context.getLibraryElement(source)); 2330 // JUnitTestCase.assertNotNullMsg("performAnalysisTask failed to compute an e lement model", _context.getLibraryElement(source));
1849 } 2331 }
1850 2332
1851 void test_performAnalysisTask_onResultComputed() { 2333 void test_performAnalysisTask_onResultChanged() {
1852 Set<String> libraryElementUris = new Set<String>(); 2334 Set<String> libraryElementUris = new Set<String>();
1853 Set<String> parsedUnitUris = new Set<String>(); 2335 Set<String> parsedUnitUris = new Set<String>();
1854 Set<String> resolvedUnitUris = new Set<String>(); 2336 Set<String> resolvedUnitUris = new Set<String>();
1855 // listen 2337 // listen
1856 context.onResultComputed(LIBRARY_ELEMENT).listen((event) { 2338 context.onResultChanged(LIBRARY_ELEMENT).listen((event) {
2339 expect(event.wasComputed, isTrue);
2340 expect(event.wasInvalidated, isFalse);
1857 Source librarySource = event.target; 2341 Source librarySource = event.target;
1858 libraryElementUris.add(librarySource.uri.toString()); 2342 libraryElementUris.add(librarySource.uri.toString());
1859 }); 2343 });
1860 context.onResultComputed(PARSED_UNIT).listen((event) { 2344 context.onResultChanged(PARSED_UNIT).listen((event) {
2345 expect(event.wasComputed, isTrue);
2346 expect(event.wasInvalidated, isFalse);
1861 Source source = event.target; 2347 Source source = event.target;
1862 parsedUnitUris.add(source.uri.toString()); 2348 parsedUnitUris.add(source.uri.toString());
1863 }); 2349 });
1864 context.onResultComputed(RESOLVED_UNIT).listen((event) { 2350 context.onResultChanged(RESOLVED_UNIT).listen((event) {
2351 expect(event.wasComputed, isTrue);
2352 expect(event.wasInvalidated, isFalse);
1865 LibrarySpecificUnit target = event.target; 2353 LibrarySpecificUnit target = event.target;
1866 Source librarySource = target.library; 2354 Source librarySource = target.library;
1867 resolvedUnitUris.add(librarySource.uri.toString()); 2355 resolvedUnitUris.add(librarySource.uri.toString());
1868 }); 2356 });
1869 // analyze 2357 // analyze
1870 addSource('/test.dart', 'main() {}'); 2358 addSource('/test.dart', 'main() {}');
1871 _analyzeAll_assertFinished(); 2359 _analyzeAll_assertFinished();
1872 // verify 2360 // verify
1873 expect(libraryElementUris, contains('dart:core'));
1874 expect(libraryElementUris, contains('file:///test.dart')); 2361 expect(libraryElementUris, contains('file:///test.dart'));
1875 expect(parsedUnitUris, contains('dart:core'));
1876 expect(parsedUnitUris, contains('file:///test.dart')); 2362 expect(parsedUnitUris, contains('file:///test.dart'));
1877 expect(resolvedUnitUris, contains('dart:core'));
1878 expect(resolvedUnitUris, contains('file:///test.dart')); 2363 expect(resolvedUnitUris, contains('file:///test.dart'));
1879 } 2364 }
1880 2365
2366 void test_performAnalysisTask_switchPackageVersion() {
2367 // version 1
2368 resourceProvider.newFile(
2369 '/pkgs/crypto-1/lib/crypto.dart',
2370 r'''
2371 library crypto;
2372 part 'src/hash_utils.dart';
2373 ''');
2374 resourceProvider.newFile(
2375 '/pkgs/crypto-1/lib/src/hash_utils.dart',
2376 r'''
2377 part of crypto;
2378 const _MASK_8 = 0xff;
2379 ''');
2380 // version 2
2381 resourceProvider.newFile(
2382 '/pkgs/crypto-2/lib/crypto.dart',
2383 r'''
2384 library crypto;
2385 part 'src/hash_utils.dart';
2386 ''');
2387 resourceProvider.newFile(
2388 '/pkgs/crypto-2/lib/src/hash_utils.dart',
2389 r'''
2390 part of crypto;
2391 const _MASK_8 = 0xff;
2392 ''');
2393 // use version 1
2394 context.sourceFactory = new SourceFactory(<UriResolver>[
2395 sdkResolver,
2396 resourceResolver,
2397 new PackageMapUriResolver(resourceProvider, {
2398 'crypto': [resourceProvider.getFolder('/pkgs/crypto-1/lib')]
2399 })
2400 ]);
2401 // analyze
2402 addSource(
2403 "/test.dart",
2404 r'''
2405 import 'package:crypto/crypto.dart';
2406 ''');
2407 _analyzeAll_assertFinished();
2408 // use version 2
2409 context.sourceFactory = new SourceFactory(<UriResolver>[
2410 sdkResolver,
2411 resourceResolver,
2412 new PackageMapUriResolver(resourceProvider, {
2413 'crypto': [resourceProvider.getFolder('/pkgs/crypto-2/lib')]
2414 })
2415 ]);
2416 _analyzeAll_assertFinished();
2417 _assertNoExceptions();
2418 }
2419
2420 void test_resolveCompilationUnit_existingElementModel() {
2421 Source source = addSource(
2422 '/test.dart',
2423 r'''
2424 library test;
2425
2426 String topLevelVariable;
2427 int get topLevelGetter => 0;
2428 void set topLevelSetter(int value) {}
2429 String topLevelFunction(int i) => '';
2430
2431 typedef String FunctionTypeAlias(int i);
2432
2433 enum EnumeratedType {Invalid, Valid}
2434
2435 class ClassOne {
2436 int instanceField;
2437 static int staticField;
2438
2439 ClassOne();
2440 ClassOne.named();
2441
2442 int get instanceGetter => 0;
2443 static String get staticGetter => '';
2444
2445 void set instanceSetter(int value) {}
2446 static void set staticSetter(int value) {}
2447
2448 int instanceMethod(int first, [int second = 0]) {
2449 int localVariable;
2450 int localFunction(String s) {}
2451 }
2452 static String staticMethod(int first, {int second: 0}) => '';
2453 }
2454
2455 class ClassTwo {
2456 // Implicit no-argument constructor
2457 }
2458 ''');
2459 context.resolveCompilationUnit2(source, source);
2460 LibraryElement firstElement = context.computeLibraryElement(source);
2461 _ElementGatherer gatherer = new _ElementGatherer();
2462 firstElement.accept(gatherer);
2463
2464 CacheEntry entry =
2465 context.analysisCache.get(new LibrarySpecificUnit(source, source));
2466 entry.setState(RESOLVED_UNIT1, CacheState.FLUSHED);
2467 entry.setState(RESOLVED_UNIT2, CacheState.FLUSHED);
2468 entry.setState(RESOLVED_UNIT3, CacheState.FLUSHED);
2469 entry.setState(RESOLVED_UNIT4, CacheState.FLUSHED);
2470 entry.setState(RESOLVED_UNIT5, CacheState.FLUSHED);
2471 entry.setState(RESOLVED_UNIT6, CacheState.FLUSHED);
2472 entry.setState(RESOLVED_UNIT7, CacheState.FLUSHED);
2473 entry.setState(RESOLVED_UNIT8, CacheState.FLUSHED);
2474 entry.setState(RESOLVED_UNIT9, CacheState.FLUSHED);
2475 entry.setState(RESOLVED_UNIT10, CacheState.FLUSHED);
2476 entry.setState(RESOLVED_UNIT11, CacheState.FLUSHED);
2477 entry.setState(RESOLVED_UNIT12, CacheState.FLUSHED);
2478 entry.setState(RESOLVED_UNIT, CacheState.FLUSHED);
2479
2480 context.resolveCompilationUnit2(source, source);
2481 LibraryElement secondElement = context.computeLibraryElement(source);
2482 _ElementComparer comparer = new _ElementComparer(gatherer.elements);
2483 secondElement.accept(comparer);
2484 comparer.expectNoDifferences();
2485 }
2486
1881 void test_resolveCompilationUnit_import_relative() { 2487 void test_resolveCompilationUnit_import_relative() {
1882 Source sourceA = 2488 Source sourceA =
1883 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}"); 2489 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}");
1884 addSource("/libB.dart", "library libB; class B{}"); 2490 addSource("/libB.dart", "library libB; class B{}");
1885 CompilationUnit compilationUnit = 2491 CompilationUnit compilationUnit =
1886 context.resolveCompilationUnit2(sourceA, sourceA); 2492 context.resolveCompilationUnit2(sourceA, sourceA);
1887 expect(compilationUnit, isNotNull); 2493 expect(compilationUnit, isNotNull);
1888 LibraryElement library = compilationUnit.element.library; 2494 LibraryElement library = compilationUnit.element.library;
1889 List<LibraryElement> importedLibraries = library.importedLibraries; 2495 List<LibraryElement> importedLibraries = library.importedLibraries;
1890 assertNamedElements(importedLibraries, ["dart.core", "libB"]); 2496 assertNamedElements(importedLibraries, ["dart.core", "libB"]);
1891 List<LibraryElement> visibleLibraries = library.visibleLibraries;
1892 assertNamedElements(visibleLibraries,
1893 ["dart.core", "dart.async", "dart.math", "libA", "libB"]);
1894 } 2497 }
1895 2498
1896 void test_resolveCompilationUnit_import_relative_cyclic() { 2499 void test_resolveCompilationUnit_import_relative_cyclic() {
1897 Source sourceA = 2500 Source sourceA =
1898 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}"); 2501 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}");
1899 addSource("/libB.dart", "library libB; import 'libA.dart'; class B{}"); 2502 addSource("/libB.dart", "library libB; import 'libA.dart'; class B{}");
1900 CompilationUnit compilationUnit = 2503 CompilationUnit compilationUnit =
1901 context.resolveCompilationUnit2(sourceA, sourceA); 2504 context.resolveCompilationUnit2(sourceA, sourceA);
1902 expect(compilationUnit, isNotNull); 2505 expect(compilationUnit, isNotNull);
1903 LibraryElement library = compilationUnit.element.library; 2506 LibraryElement library = compilationUnit.element.library;
1904 List<LibraryElement> importedLibraries = library.importedLibraries; 2507 List<LibraryElement> importedLibraries = library.importedLibraries;
1905 assertNamedElements(importedLibraries, ["dart.core", "libB"]); 2508 assertNamedElements(importedLibraries, ["dart.core", "libB"]);
1906 List<LibraryElement> visibleLibraries = library.visibleLibraries;
1907 assertNamedElements(visibleLibraries,
1908 ["dart.core", "dart.async", "dart.math", "libA", "libB"]);
1909 }
1910
1911 void test_resolveCompilationUnit_library() {
1912 Source source = addSource("/lib.dart", "library lib;");
1913 LibraryElement library = context.computeLibraryElement(source);
1914 CompilationUnit compilationUnit =
1915 context.resolveCompilationUnit(source, library);
1916 expect(compilationUnit, isNotNull);
1917 expect(compilationUnit.element, isNotNull);
1918 } 2509 }
1919 2510
1920 // void test_resolveCompilationUnit_sourceChangeDuringResolution() { 2511 // void test_resolveCompilationUnit_sourceChangeDuringResolution() {
1921 // _context = new _AnalysisContext_sourceChangeDuringResolution(); 2512 // _context = new _AnalysisContext_sourceChangeDuringResolution();
1922 // AnalysisContextFactory.initContextWithCore(_context); 2513 // AnalysisContextFactory.initContextWithCore(_context);
1923 // _sourceFactory = _context.sourceFactory; 2514 // _sourceFactory = _context.sourceFactory;
1924 // Source source = _addSource("/lib.dart", "library lib;"); 2515 // Source source = _addSource("/lib.dart", "library lib;");
1925 // CompilationUnit compilationUnit = 2516 // CompilationUnit compilationUnit =
1926 // _context.resolveCompilationUnit2(source, source); 2517 // _context.resolveCompilationUnit2(source, source);
1927 // expect(compilationUnit, isNotNull); 2518 // expect(compilationUnit, isNotNull);
1928 // expect(_context.getLineInfo(source), isNotNull); 2519 // expect(_context.getLineInfo(source), isNotNull);
1929 // } 2520 // }
1930 2521
2522 void test_resolveCompilationUnit_library() {
2523 Source source = addSource("/lib.dart", "library lib;");
2524 LibraryElement library = context.computeLibraryElement(source);
2525 CompilationUnit compilationUnit =
2526 context.resolveCompilationUnit(source, library);
2527 expect(compilationUnit, isNotNull);
2528 expect(compilationUnit.element, isNotNull);
2529 }
2530
1931 void test_resolveCompilationUnit_source() { 2531 void test_resolveCompilationUnit_source() {
1932 Source source = addSource("/lib.dart", "library lib;"); 2532 Source source = addSource("/lib.dart", "library lib;");
1933 CompilationUnit compilationUnit = 2533 CompilationUnit compilationUnit =
1934 context.resolveCompilationUnit2(source, source); 2534 context.resolveCompilationUnit2(source, source);
1935 expect(compilationUnit, isNotNull); 2535 expect(compilationUnit, isNotNull);
1936 } 2536 }
1937 2537
1938 void test_setAnalysisOptions() { 2538 void test_setAnalysisOptions() {
1939 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); 2539 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
1940 options.cacheSize = 42; 2540 options.cacheSize = 42;
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
2105 AnalysisDelta delta = new AnalysisDelta(); 2705 AnalysisDelta delta = new AnalysisDelta();
2106 delta.setAnalysisLevel(source, AnalysisLevel.ALL); 2706 delta.setAnalysisLevel(source, AnalysisLevel.ALL);
2107 context.applyAnalysisDelta(delta); 2707 context.applyAnalysisDelta(delta);
2108 expect(context.sourcesNeedingProcessing, contains(source)); 2708 expect(context.sourcesNeedingProcessing, contains(source));
2109 delta = new AnalysisDelta(); 2709 delta = new AnalysisDelta();
2110 delta.setAnalysisLevel(source, AnalysisLevel.NONE); 2710 delta.setAnalysisLevel(source, AnalysisLevel.NONE);
2111 context.applyAnalysisDelta(delta); 2711 context.applyAnalysisDelta(delta);
2112 expect(context.sourcesNeedingProcessing.contains(source), isFalse); 2712 expect(context.sourcesNeedingProcessing.contains(source), isFalse);
2113 } 2713 }
2114 2714
2115 void test_validateCacheConsistency_deletedSource() { 2715 void test_validateCacheConsistency_deletedFile() {
2116 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); 2716 MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
2117 var fileA = resourceProvider.newFile('/a.dart', ""); 2717 var fileA = resourceProvider.newFile('/a.dart', "");
2118 var fileB = resourceProvider.newFile('/b.dart', "import 'a.dart';"); 2718 var fileB = resourceProvider.newFile('/b.dart', "import 'a.dart';");
2119 Source sourceA = fileA.createSource(); 2719 Source sourceA = fileA.createSource();
2120 Source sourceB = fileB.createSource(); 2720 Source sourceB = fileB.createSource();
2121 context.applyChanges( 2721 context.applyChanges(
2122 new ChangeSet()..addedSource(sourceA)..addedSource(sourceB)); 2722 new ChangeSet()..addedSource(sourceA)..addedSource(sourceB));
2123 // analyze everything 2723 // analyze everything
2124 _analyzeAll_assertFinished(); 2724 _analyzeAll_assertFinished();
2125 // delete a.dart 2725 // delete a.dart
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
2171 return source; 2771 return source;
2172 } 2772 }
2173 2773
2174 /** 2774 /**
2175 * Perform analysis tasks up to 512 times and assert that it was enough. 2775 * Perform analysis tasks up to 512 times and assert that it was enough.
2176 */ 2776 */
2177 void _analyzeAll_assertFinished([int maxIterations = 512]) { 2777 void _analyzeAll_assertFinished([int maxIterations = 512]) {
2178 for (int i = 0; i < maxIterations; i++) { 2778 for (int i = 0; i < maxIterations; i++) {
2179 List<ChangeNotice> notice = context.performAnalysisTask().changeNotices; 2779 List<ChangeNotice> notice = context.performAnalysisTask().changeNotices;
2180 if (notice == null) { 2780 if (notice == null) {
2181 bool inconsistent = context.validateCacheConsistency(); 2781 return;
2182 if (!inconsistent) {
2183 return;
2184 }
2185 } 2782 }
2186 } 2783 }
2187 fail("performAnalysisTask failed to terminate after analyzing all sources"); 2784 fail("performAnalysisTask failed to terminate after analyzing all sources");
2188 } 2785 }
2189 2786
2787 void _assertNoExceptions() {
2788 MapIterator<AnalysisTarget, CacheEntry> iterator = analysisCache.iterator();
2789 String exceptionsStr = '';
2790 while (iterator.moveNext()) {
2791 CaughtException exception = iterator.value.exception;
2792 if (exception != null) {
2793 AnalysisTarget target = iterator.key;
2794 exceptionsStr +=
2795 '============= key: $target source: ${target.source}\n';
2796 exceptionsStr += exception.toString();
2797 exceptionsStr += '\n';
2798 }
2799 }
2800 if (exceptionsStr.isNotEmpty) {
2801 fail(exceptionsStr);
2802 }
2803 }
2804
2190 void _changeSource(TestSource source, String contents) { 2805 void _changeSource(TestSource source, String contents) {
2191 source.setContents(contents); 2806 source.setContents(contents);
2192 ChangeSet changeSet = new ChangeSet(); 2807 ChangeSet changeSet = new ChangeSet();
2193 changeSet.changedSource(source); 2808 changeSet.changedSource(source);
2194 context.applyChanges(changeSet); 2809 context.applyChanges(changeSet);
2195 } 2810 }
2196 2811
2197 /** 2812 /**
2198 * Search the given compilation unit for a class with the given name. Return t he class with the 2813 * Search the given compilation unit for a class with the given name. Return t he class with the
2199 * given name, or `null` if the class cannot be found. 2814 * given name, or `null` if the class cannot be found.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
2248 } 2863 }
2249 } 2864 }
2250 return false; 2865 return false;
2251 } 2866 }
2252 } 2867 }
2253 2868
2254 @reflectiveTest 2869 @reflectiveTest
2255 class LimitedInvalidateTest extends AbstractContextTest { 2870 class LimitedInvalidateTest extends AbstractContextTest {
2256 @override 2871 @override
2257 void setUp() { 2872 void setUp() {
2258 AnalysisEngine.instance.limitInvalidationInTaskModel = true;
2259 super.setUp(); 2873 super.setUp();
2260 AnalysisOptionsImpl options = 2874 AnalysisOptionsImpl options =
2261 new AnalysisOptionsImpl.from(context.analysisOptions); 2875 new AnalysisOptionsImpl.from(context.analysisOptions);
2262 options.incremental = true; 2876 options.incremental = true;
2877 options.finerGrainedInvalidation = true;
2263 context.analysisOptions = options; 2878 context.analysisOptions = options;
2264 } 2879 }
2265 2880
2881 void test_applyChanges_changedSource_removeFile() {
2882 File file = resourceProvider.newFile('/test.dart', 'main() {}');
2883 Source source = file.createSource();
2884 context.applyChanges(new ChangeSet()..addedSource(source));
2885 // Analyze all.
2886 _performPendingAnalysisTasks();
2887 expect(context.getResolvedCompilationUnit2(source, source), isNotNull);
2888 // Delete the file, but tell the context that it is changed.
2889 // This might happen as a race condition.
2890 // Or it might be a mishandling of file notification events.
2891 file.delete();
2892 context.applyChanges(new ChangeSet()..changedSource(source));
2893 // All the analysis results are gone.
2894 void noResolvedUnits() {
2895 LibrarySpecificUnit unit = new LibrarySpecificUnit(source, source);
2896 RESOLVED_UNIT_RESULTS.forEach((result) {
2897 expect(context.getResult(unit, result), isNull);
2898 });
2899 }
2900
2901 noResolvedUnits();
2902 // Analyze again.
2903 // The source does not exist, so still no resolution.
2904 _performPendingAnalysisTasks();
2905 noResolvedUnits();
2906 expect(context.getModificationStamp(source), -1);
2907 }
2908
2909 void test_class_addMethod_useAsHole_inTopLevelVariable() {
2910 Source a = addSource(
2911 '/a.dart',
2912 r'''
2913 class A {
2914 }
2915 ''');
2916 Source b = addSource(
2917 '/b.dart',
2918 r'''
2919 import 'a.dart';
2920 var B = A.foo;
2921 ''');
2922 Source c = addSource(
2923 '/c.dart',
2924 r'''
2925 import 'b.dart';
2926 var C = B;
2927 ''');
2928 _performPendingAnalysisTasks();
2929 expect(context.getErrors(b).errors, hasLength(1));
2930 // Update a.dart: add A.foo.
2931 // 'B' in b.dart is invalid, because 'B' references 'foo'
2932 // c.dart is invalid, because 'C' references 'B'
2933 context.setContents(
2934 a,
2935 r'''
2936 class A {
2937 static void foo(int p) {}
2938 }
2939 ''');
2940 _assertValidForChangedLibrary(a);
2941 _assertInvalid(a, LIBRARY_ERRORS_READY);
2942
2943 _assertValidForDependentLibrary(b);
2944 _assertInvalid(b, LIBRARY_ERRORS_READY);
2945 _assertInvalidUnits(b, RESOLVED_UNIT4);
2946
2947 _assertValidForDependentLibrary(c);
2948 _assertInvalid(c, LIBRARY_ERRORS_READY);
2949 _assertInvalidUnits(c, RESOLVED_UNIT4);
2950 }
2951
2952 void test_class_addMethod_useClass() {
2953 Source a = addSource(
2954 '/a.dart',
2955 r'''
2956 class A {}
2957 class B extends A {
2958 foo() {}
2959 }
2960 ''');
2961 Source b = addSource(
2962 '/b.dart',
2963 r'''
2964 import 'a.dart';
2965 B b = null;
2966 ''');
2967 _performPendingAnalysisTasks();
2968 // Update a.dart: remove add A.bar.
2969 // b.dart is valid, because though it uses A, it has the same element.
2970 context.setContents(
2971 a,
2972 r'''
2973 class A {}
2974 class B extends A {
2975 foo() {}
2976 bar() {}
2977 }
2978 ''');
2979 _assertValidForChangedLibrary(a);
2980 _assertInvalid(a, LIBRARY_ERRORS_READY);
2981 _assertValidForDependentLibrary(b);
2982 _assertValidAllLibraryUnitResults(b);
2983 _assertValid(b, LIBRARY_ERRORS_READY);
2984 }
2985
2986 void test_class_annotation_add_deprecated() {
2987 Source a = addSource(
2988 '/a.dart',
2989 r'''
2990 class A {}
2991 ''');
2992 Source b = addSource(
2993 '/b.dart',
2994 r'''
2995 import 'a.dart';
2996 List<A> foo() => [];
2997 ''');
2998 _performPendingAnalysisTasks();
2999 expect(context.getErrors(b).errors, hasLength(0));
3000 // Add @deprecated annotation.
3001 // b.dart has valid resolution, because A is still A, so only errors are
3002 // invalidated.
3003 context.setContents(
3004 a,
3005 r'''
3006 @deprecated
3007 class A {}
3008 ''');
3009 _assertValidForChangedLibrary(a);
3010 _assertInvalid(a, LIBRARY_ERRORS_READY);
3011 _assertValidForDependentLibrary(b);
3012 _assertInvalid(b, LIBRARY_ERRORS_READY);
3013 _assertValidAllResolution(b);
3014 // Analysis is done successfully.
3015 _performPendingAnalysisTasks();
3016 _assertValid(a, LIBRARY_ERRORS_READY);
3017 _assertValid(a, READY_RESOLVED_UNIT);
3018 _assertValid(b, LIBRARY_ERRORS_READY);
3019 _assertValid(b, READY_RESOLVED_UNIT);
3020 expect(context.getErrors(b).errors, hasLength(1));
3021 }
3022
3023 void test_class_annotation_remove_deprecated() {
3024 Source a = addSource(
3025 '/a.dart',
3026 r'''
3027 @deprecated
3028 class A {}
3029 ''');
3030 Source b = addSource(
3031 '/b.dart',
3032 r'''
3033 import 'a.dart';
3034 List<A> foo() => [];
3035 ''');
3036 _performPendingAnalysisTasks();
3037 expect(context.getErrors(b).errors, hasLength(1));
3038 // Add @deprecated annotation.
3039 // b.dart has valid resolution, because A is still A, so only errors are
3040 // invalidated.
3041 context.setContents(
3042 a,
3043 r'''
3044 class A {}
3045 ''');
3046 _assertValidForChangedLibrary(a);
3047 _assertInvalid(a, LIBRARY_ERRORS_READY);
3048 _assertValidForDependentLibrary(b);
3049 _assertInvalid(b, LIBRARY_ERRORS_READY);
3050 _assertValidAllResolution(b);
3051 // Analysis is done successfully.
3052 _performPendingAnalysisTasks();
3053 _assertValid(a, LIBRARY_ERRORS_READY);
3054 _assertValid(a, READY_RESOLVED_UNIT);
3055 _assertValid(b, LIBRARY_ERRORS_READY);
3056 _assertValid(b, READY_RESOLVED_UNIT);
3057 expect(context.getErrors(b).errors, hasLength(0));
3058 }
3059
3060 void test_class_constructor_named_changeName() {
3061 // Update a.dart: change A.named() to A.named2().
3062 // b.dart is invalid, because it references A.named().
3063 _verifyTwoLibrariesInvalidatesResolution(
3064 r'''
3065 class A {
3066 A.named();
3067 }
3068 ''',
3069 r'''
3070 class A {
3071 A.named2();
3072 }
3073 ''',
3074 r'''
3075 import 'a.dart';
3076 main() {
3077 new A.named();
3078 }
3079 ''');
3080 }
3081
3082 void test_class_constructor_named_parameters_add() {
3083 // Update a.dart: add a new parameter to A.named().
3084 // b.dart is invalid, because it references A.named().
3085 _verifyTwoLibrariesInvalidatesResolution(
3086 r'''
3087 class A {
3088 A.named();
3089 }
3090 ''',
3091 r'''
3092 class A {
3093 A.named(int p);
3094 }
3095 ''',
3096 r'''
3097 import 'a.dart';
3098 main() {
3099 new A.named();
3100 }
3101 ''');
3102 }
3103
3104 void test_class_constructor_named_parameters_change_usedSuper() {
3105 // Update a.dart: change A.named().
3106 // b.dart is invalid, because it references A.named().
3107 _verifyTwoLibrariesInvalidatesResolution(
3108 r'''
3109 class A {
3110 A.named(int a);
3111 }
3112 ''',
3113 r'''
3114 class A {
3115 A.named(String a);
3116 }
3117 ''',
3118 r'''
3119 import 'a.dart';
3120 class B extends A {
3121 B() : super.named(42);
3122 }
3123 ''');
3124 }
3125
3126 void test_class_constructor_named_parameters_remove() {
3127 // Update a.dart: remove a new parameter of A.named().
3128 // b.dart is invalid, because it references A.named().
3129 _verifyTwoLibrariesInvalidatesResolution(
3130 r'''
3131 class A {
3132 A.named(int p);
3133 }
3134 ''',
3135 r'''
3136 class A {
3137 A.named();
3138 }
3139 ''',
3140 r'''
3141 import 'a.dart';
3142 main() {
3143 new A.named();
3144 }
3145 ''');
3146 }
3147
3148 void test_class_constructor_named_remove_notUsed() {
3149 // Update a.dart: remove A.foo().
3150 // b.dart is valid, because it does not reference A.foo().
3151 _verifyTwoLibrariesAllValid(
3152 r'''
3153 class A {
3154 A.foo();
3155 A.bar();
3156 }
3157 ''',
3158 r'''
3159 class A {
3160 A.bar();
3161 }
3162 ''',
3163 r'''
3164 import 'a.dart';
3165 main() {
3166 new A.bar();
3167 }
3168 ''');
3169 }
3170
3171 void test_class_constructor_named_remove_used() {
3172 // Update a.dart: remove A.named().
3173 // b.dart is invalid, because it references A.named().
3174 _verifyTwoLibrariesInvalidatesResolution(
3175 r'''
3176 class A {
3177 A.named();
3178 }
3179 ''',
3180 r'''
3181 class A {
3182 }
3183 ''',
3184 r'''
3185 import 'a.dart';
3186 main() {
3187 new A.named();
3188 }
3189 ''');
3190 }
3191
3192 void test_class_constructor_unnamed_parameters_change_notUsedSuper() {
3193 // Update a.dart: change A().
3194 // Resolution of b.dart is valid, because it does not reference A().
3195 // Hints and verify errors are invalid, because it extends A.
3196 // TODO(scheglov) we could keep valid hints and verify errors,
3197 // because only constructor is changed - this cannot add/remove
3198 // inherited unimplemented members.
3199 _verifyTwoLibrariesInvalidHintsVerifyErrors(
3200 r'''
3201 class A {
3202 A(int a);
3203 A.named(int a);
3204 }
3205 ''',
3206 r'''
3207 class A {
3208 A(String a);
3209 A.named(int a);
3210 }
3211 ''',
3212 r'''
3213 import 'a.dart';
3214 class B extends A {
3215 B() : super.named(42);
3216 }
3217 ''');
3218 }
3219
3220 void test_class_constructor_unnamed_parameters_change_usedSuper() {
3221 // Update a.dart: change A().
3222 // b.dart is invalid, because it references A().
3223 _verifyTwoLibrariesInvalidatesResolution(
3224 r'''
3225 class A {
3226 A(int a);
3227 }
3228 ''',
3229 r'''
3230 class A {
3231 A(String a);
3232 }
3233 ''',
3234 r'''
3235 import 'a.dart';
3236 class B extends A {
3237 B() : super(42);
3238 }
3239 ''');
3240 }
3241
3242 void test_class_constructor_unnamed_parameters_remove() {
3243 // Update a.dart: change A().
3244 // b.dart is invalid, because it references A().
3245 _verifyTwoLibrariesInvalidatesResolution(
3246 r'''
3247 class A {
3248 A(int a, int b);
3249 }
3250 ''',
3251 r'''
3252 class A {
3253 A(int a);
3254 }
3255 ''',
3256 r'''
3257 import 'a.dart';
3258 main() {
3259 new A(1, 2);
3260 }
3261 ''');
3262 }
3263
3264 void test_class_constructor_unnamed_remove_notUsed() {
3265 // Update a.dart: remove A().
3266 // b.dart is invalid, because it instantiates A.
3267 _verifyTwoLibrariesInvalidatesResolution(
3268 r'''
3269 class A {
3270 A();
3271 A.named();
3272 }
3273 ''',
3274 r'''
3275 class A {
3276 A.named();
3277 }
3278 ''',
3279 r'''
3280 import 'a.dart';
3281 main() {
3282 new A.named();
3283 }
3284 ''');
3285 }
3286
3287 void test_class_constructor_unnamed_remove_used() {
3288 // Update a.dart: remove A().
3289 // b.dart is invalid, because it references A().
3290 _verifyTwoLibrariesInvalidatesResolution(
3291 r'''
3292 class A {
3293 A();
3294 }
3295 ''',
3296 r'''
3297 class A {
3298 }
3299 ''',
3300 r'''
3301 import 'a.dart';
3302 main() {
3303 new A();
3304 }
3305 ''');
3306 }
3307
3308 void test_class_method_change_notUsed() {
3309 Source a = addSource(
3310 '/a.dart',
3311 r'''
3312 class A {
3313 foo() {}
3314 bar() {}
3315 }
3316 ''');
3317 Source b = addSource(
3318 '/b.dart',
3319 r'''
3320 import 'a.dart';
3321 main(A a) {
3322 a.foo();
3323 }
3324 ''');
3325 _performPendingAnalysisTasks();
3326 // Update a.dart: remove A.bar, add A.bar2.
3327 // b.dart is valid, because it doesn't references 'bar' or 'bar2'.
3328 context.setContents(
3329 a,
3330 r'''
3331 class A {
3332 foo() {}
3333 bar2() {}
3334 }
3335 ''');
3336 _assertValidForChangedLibrary(a);
3337 _assertInvalid(a, LIBRARY_ERRORS_READY);
3338 _assertValidForDependentLibrary(b);
3339 _assertValidAllLibraryUnitResults(b);
3340 _assertValid(b, LIBRARY_ERRORS_READY);
3341 }
3342
3343 void test_class_method_change_notUsed_throughSubclass_extends() {
3344 Source a = addSource(
3345 '/a.dart',
3346 r'''
3347 class A {
3348 foo() {}
3349 bar() {}
3350 }
3351 class B extends A {}
3352 ''');
3353 Source b = addSource(
3354 '/b.dart',
3355 r'''
3356 import 'a.dart';
3357 main(B b) {
3358 a.foo();
3359 }
3360 ''');
3361 _performPendingAnalysisTasks();
3362 // Update a.dart: remove A.bar, add A.bar2.
3363 // b.dart is valid, because it doesn't references 'bar' or 'bar2'.
3364 context.setContents(
3365 a,
3366 r'''
3367 class A {
3368 foo() {}
3369 bar2() {}
3370 }
3371 class B extends A {}
3372 ''');
3373 _assertValidForChangedLibrary(a);
3374 _assertInvalid(a, LIBRARY_ERRORS_READY);
3375 _assertValidForDependentLibrary(b);
3376 _assertValidAllLibraryUnitResults(b);
3377 _assertValid(b, LIBRARY_ERRORS_READY);
3378 }
3379
3380 void test_class_method_definedInSuper_sameLibrary() {
3381 Source a = addSource(
3382 '/a.dart',
3383 r'''
3384 class A {
3385 m() {}
3386 }
3387 class B extends A {}
3388 ''');
3389 Source b = addSource(
3390 '/b.dart',
3391 r'''
3392 import 'a.dart';
3393 main(B b) {
3394 b.m();
3395 }
3396 ''');
3397 _performPendingAnalysisTasks();
3398 // Update a.dart: change A.m
3399 // This makes B changed.
3400 // b.dart is invalid, because it references B.
3401 context.setContents(
3402 a,
3403 r'''
3404 class A {
3405 m2() {}
3406 }
3407 class B extends A {}
3408 ''');
3409 _assertValidForChangedLibrary(a);
3410 _assertValidForDependentLibrary(b);
3411 _assertInvalid(a, LIBRARY_ERRORS_READY);
3412 _assertInvalid(b, LIBRARY_ERRORS_READY);
3413 }
3414
3415 void test_class_method_remove_notUsed_instantiated() {
3416 Source a = addSource(
3417 '/a.dart',
3418 r'''
3419 abstract class I {
3420 void foo();
3421 }
3422 class A implements I {
3423 void foo() {}
3424 }
3425 ''');
3426 Source b = addSource(
3427 '/b.dart',
3428 r'''
3429 import 'a.dart';
3430 main() {
3431 new A();
3432 }
3433 ''');
3434 _performPendingAnalysisTasks();
3435 // Update a.dart: remove 'A.foo'.
3436 // b.dart is valid because it does not reference 'foo'.
3437 // The class 'A' has a warning, but it is still not abstract.
3438 context.setContents(
3439 a,
3440 r'''
3441 abstract class I {
3442 void fo();
3443 }
3444 class A implements I {
3445 }
3446 ''');
3447 _assertValidForChangedLibrary(a);
3448 _assertInvalid(a, LIBRARY_ERRORS_READY);
3449
3450 _assertValidForDependentLibrary(b);
3451 _assertValidAllLibraryUnitResults(b);
3452 _assertValidAllResolution(b);
3453 _assertValidAllErrors(b);
3454 }
3455
3456 void test_class_method_remove_subclass() {
3457 Source a = addSource(
3458 '/a.dart',
3459 r'''
3460 abstract class I {
3461 void foo();
3462 }
3463 class A implements I {
3464 void foo() {}
3465 }
3466 ''');
3467 Source b = addSource(
3468 '/b.dart',
3469 r'''
3470 import 'a.dart';
3471 class B extends A {}
3472 ''');
3473 _performPendingAnalysisTasks();
3474 // Update a.dart: remove A.bar, add A.bar2.
3475 // b.dart
3476 // Resolution is valid because 'foo' is not referenced.
3477 // HINTS are invalid because 'B' might have invalid @override.
3478 // VERIFY_ERRORS are invalid because 'B' might not implement something.
3479 // Other errors are also invalid.
3480 context.setContents(
3481 a,
3482 r'''
3483 abstract class I {
3484 void foo();
3485 }
3486 class A implements I {
3487 }
3488 ''');
3489 _assertValidForChangedLibrary(a);
3490 _assertInvalid(a, LIBRARY_ERRORS_READY);
3491
3492 _assertValidForDependentLibrary(b);
3493 _assertValidAllResolution(b);
3494 _assertInvalidHintsVerifyErrors(b);
3495 }
3496
3497 void test_class_private_member() {
3498 Source a = addSource(
3499 '/a.dart',
3500 r'''
3501 class A {
3502 A();
3503 A._privateConstructor();
3504
3505 foo() {}
3506
3507 int _privateField;
3508 _privateMethod() {}
3509 int get _privateGetter => null;
3510 void set _privateSetter(_) {}
3511 }
3512 ''');
3513 Source b = addSource(
3514 '/b.dart',
3515 r'''
3516 import 'a.dart';
3517 main(A a) {
3518 a.foo();
3519 }
3520 ''');
3521 _performPendingAnalysisTasks();
3522 // Update a.dart: rename private members of A
3523 // b.dart is valid, it cannot see these private members.
3524 context.setContents(
3525 a,
3526 r'''
3527 class A {
3528 A();
3529 A._privateConstructor2();
3530
3531 foo() {}
3532
3533 int _privateField2;
3534 _privateMethod2() {}
3535 int get _privateGetter2 => null;
3536 void set _privateSetter2(_) {}
3537 }
3538 ''');
3539 _assertValidForChangedLibrary(a);
3540 _assertInvalid(a, LIBRARY_ERRORS_READY);
3541 _assertValidForDependentLibrary(b);
3542 _assertValidAllLibraryUnitResults(b);
3543 _assertValid(b, LIBRARY_ERRORS_READY);
3544 }
3545
3546 void test_enum_add() {
3547 Source a = addSource(
3548 '/a.dart',
3549 r'''
3550 main() {
3551 print(MyEnum.A);
3552 }
3553 ''');
3554 _performPendingAnalysisTasks();
3555 // Insert a new enum declaration.
3556 // No errors expected.
3557 context.setContents(
3558 a,
3559 r'''
3560 main() {
3561 print(MyEnum.A);
3562 }
3563 enum MyEnum { A, B, C }
3564 ''');
3565 _performPendingAnalysisTasks();
3566 expect(context.getErrors(a).errors, isEmpty);
3567 }
3568
3569 void test_private_class() {
3570 Source a = addSource(
3571 '/a.dart',
3572 r'''
3573 class _A {}
3574 class _B2 {}
3575 ''');
3576 Source b = addSource(
3577 '/b.dart',
3578 r'''
3579 import 'a.dart';
3580 main() {
3581 new _A();
3582 new _B();
3583 }
3584 ''');
3585 _performPendingAnalysisTasks();
3586 // Update a.dart: change _A and _B2
3587 // b.dart is valid, because _A, _B, _A2 and _B2 are all private,
3588 // so b.dart cannot see them.
3589 context.setContents(
3590 a,
3591 r'''
3592 class _A2 {}
3593 class _B {}
3594 ''');
3595 _assertValidForChangedLibrary(a);
3596 _assertInvalid(a, LIBRARY_ERRORS_READY);
3597 _assertValidForDependentLibrary(b);
3598 _assertValidAllLibraryUnitResults(b);
3599 _assertValid(b, LIBRARY_ERRORS_READY);
3600 }
3601
3602 void test_private_topLevelVariable() {
3603 Source a = addSource(
3604 '/a.dart',
3605 r'''
3606 int _V = 1;
3607 ''');
3608 Source b = addSource(
3609 '/b.dart',
3610 r'''
3611 import 'a.dart';
3612 main() {
3613 print(_A);
3614 }
3615 ''');
3616 _performPendingAnalysisTasks();
3617 // Update a.dart: change _V
3618 // b.dart is valid, because _V is private and b.dart cannot see it.
3619 context.setContents(
3620 a,
3621 r'''
3622 int _V = 2;
3623 ''');
3624 _assertValidForChangedLibrary(a);
3625 _assertInvalid(a, LIBRARY_ERRORS_READY);
3626 _assertValidForDependentLibrary(b);
3627 _assertValidAllLibraryUnitResults(b);
3628 _assertValid(b, LIBRARY_ERRORS_READY);
3629 }
3630
3631 void test_private_topLevelVariable_throughPublic() {
3632 Source a = addSource(
3633 '/a.dart',
3634 r'''
3635 int _A = 1;
3636 int B = _A + 1;
3637 ''');
3638 Source b = addSource(
3639 '/b.dart',
3640 r'''
3641 import 'a.dart';
3642 main() {
3643 print(B);
3644 }
3645 ''');
3646 _performPendingAnalysisTasks();
3647 // Update a.dart: change _A
3648 // b.dart is invalid, because it uses B, which uses _A.
3649 context.setContents(
3650 a,
3651 r'''
3652 int _A = 2;
3653 int B = _A + 1;
3654 ''');
3655 _assertValidForChangedLibrary(a);
3656 _assertInvalid(a, LIBRARY_ERRORS_READY);
3657 _assertValidForDependentLibrary(b);
3658 _assertInvalid(b, LIBRARY_ERRORS_READY);
3659 }
3660
3661 void test_sequence_applyChanges_changedSource() {
3662 Source a = addSource(
3663 '/a.dart',
3664 r'''
3665 class A {}
3666 class B {}
3667 ''');
3668 Source b = addSource(
3669 '/b.dart',
3670 r'''
3671 import 'a.dart';
3672 main() {
3673 new A();
3674 new B();
3675 }
3676 ''');
3677 _performPendingAnalysisTasks();
3678 resourceProvider.updateFile(
3679 '/a.dart',
3680 r'''
3681 class A2 {}
3682 class B {}
3683 ''');
3684 // Update a.dart: remove A, add A2.
3685 // b.dart is invalid, because it references A.
3686 var changeSet = new ChangeSet()..changedSource(a);
3687 context.applyChanges(changeSet);
3688 _assertValidForChangedLibrary(a);
3689 _assertInvalid(a, LIBRARY_ERRORS_READY);
3690 _assertValidForDependentLibrary(b);
3691 _assertInvalid(b, LIBRARY_ERRORS_READY);
3692 _assertInvalidUnits(b, RESOLVED_UNIT4);
3693 // Analyze.
3694 _performPendingAnalysisTasks();
3695 expect(context.getErrors(a).errors, hasLength(0));
3696 expect(context.getErrors(b).errors, hasLength(1));
3697 _assertValid(a, READY_RESOLVED_UNIT);
3698 _assertValid(b, READY_RESOLVED_UNIT);
3699 _assertValidAllLibraryUnitResults(a);
3700 _assertValidAllLibraryUnitResults(b);
3701 }
3702
3703 void test_sequence_class_give_take() {
3704 Source a = addSource(
3705 '/a.dart',
3706 r'''
3707 class A {}
3708 class B {}
3709 class C {}
3710 ''');
3711 Source b = addSource(
3712 '/b.dart',
3713 r'''
3714 import 'a.dart';
3715 main() {
3716 new A();
3717 new C2();
3718 }
3719 ''');
3720 _performPendingAnalysisTasks();
3721 expect(context.getErrors(b).errors, hasLength(1));
3722 // Update a.dart: remove C, add C2.
3723 // b.dart is invalid, because it references C2.
3724 context.setContents(
3725 a,
3726 r'''
3727 class A {}
3728 class B {}
3729 class C2 {}
3730 ''');
3731 _assertValidForChangedLibrary(a);
3732 _assertInvalid(a, LIBRARY_ERRORS_READY);
3733 _assertValidForDependentLibrary(b);
3734 _assertInvalid(b, LIBRARY_ERRORS_READY);
3735 _assertInvalidUnits(b, RESOLVED_UNIT4);
3736 // Now b.dart is analyzed and the error is fixed.
3737 _performPendingAnalysisTasks();
3738 expect(context.getErrors(b).errors, hasLength(0));
3739 // Update a.dart: remove C2, add C.
3740 // b.dart is invalid, because it references C2.
3741 context.setContents(
3742 a,
3743 r'''
3744 class A {}
3745 class B {}
3746 class C {}
3747 ''');
3748 _assertValidForChangedLibrary(a);
3749 _assertInvalid(a, LIBRARY_ERRORS_READY);
3750 _assertValidForDependentLibrary(b);
3751 _assertInvalid(b, LIBRARY_ERRORS_READY);
3752 _assertInvalidUnits(b, RESOLVED_UNIT4);
3753 // Now b.dart is analyzed and it again has the error.
3754 _performPendingAnalysisTasks();
3755 expect(context.getErrors(b).errors, hasLength(1));
3756 }
3757
3758 void test_sequence_class_removeMethod_overridden() {
3759 Source a = addSource(
3760 '/a.dart',
3761 r'''
3762 class A {
3763 void foo() {}
3764 }
3765 ''');
3766 Source b = addSource(
3767 '/b.dart',
3768 r'''
3769 import 'a.dart';
3770 class B extends A {
2266 @override 3771 @override
2267 void tearDown() { 3772 void foo() {}
2268 AnalysisEngine.instance.limitInvalidationInTaskModel = false; 3773 }
2269 super.tearDown(); 3774 ''');
2270 } 3775 _performPendingAnalysisTasks();
2271 3776 expect(context.getErrors(b).errors, hasLength(0));
2272 void test_noChange_thenChange() { 3777 // Update a.dart: remove add A.foo.
2273 Source sourceA = addSource( 3778 // b.dart has a new hint, because B.foo does not override anything
2274 "/a.dart", 3779 context.setContents(
2275 r''' 3780 a,
2276 library lib_a; 3781 r'''
2277 3782 class A {
3783 }
3784 ''');
3785 _assertValidForChangedLibrary(a);
3786 _assertInvalid(a, LIBRARY_ERRORS_READY);
3787 _assertValidForDependentLibrary(b);
3788 _assertValidAllResolution(b);
3789 _assertInvalidHintsVerifyErrors(b);
3790
3791 _performPendingAnalysisTasks();
3792 expect(context.getErrors(b).errors, hasLength(1));
3793 }
3794
3795 void test_sequence_clearParameterElements() {
3796 Source a = addSource(
3797 '/a.dart',
3798 r'''
3799 class A {
3800 foo(int p) {}
3801 }
3802 final a = new A();
3803 main() {
3804 a.foo(42);
3805 }
3806 ''');
3807 _performPendingAnalysisTasks();
3808 Expression find42() {
3809 CompilationUnit unit =
3810 context.getResult(new LibrarySpecificUnit(a, a), RESOLVED_UNIT);
3811 ExpressionStatement statement =
3812 AstFinder.getStatementsInTopLevelFunction(unit, 'main').single;
3813 MethodInvocation invocation = statement.expression;
3814 return invocation.argumentList.arguments[0];
3815 }
3816
3817 {
3818 Expression argument = find42();
3819 expect(argument.staticParameterElement, isNull);
3820 }
3821
3822 // Update a.dart: add type annotation for 'a'.
3823 // '42' has 'staticParameterElement'.
3824 context.setContents(
3825 a,
3826 r'''
3827 class A {
3828 foo(int p) {}
3829 }
3830 final A a = new A();
3831 main() {
3832 a.foo(42);
3833 }
3834 ''');
3835 _performPendingAnalysisTasks();
3836 {
3837 Expression argument = find42();
3838 expect(argument.staticParameterElement, isNotNull);
3839 }
3840
3841 // Update a.dart: remove type annotation for 'a'.
3842 // '42' doesn't have 'staticParameterElement'.
3843 context.setContents(
3844 a,
3845 r'''
3846 class A {
3847 foo(int p) {}
3848 }
3849 final a = new A();
3850 main() {
3851 a.foo(42);
3852 }
3853 ''');
3854 _performPendingAnalysisTasks();
3855 {
3856 Expression argument = find42();
3857 expect(argument.staticParameterElement, isNull);
3858 }
3859 }
3860
3861 void test_sequence_closureParameterTypesPropagation() {
3862 Source a = addSource(
3863 '/a.dart',
3864 r'''
3865 main() {
3866 f((p) => 4.2);
3867 }
3868 f(c(int p)) {}
3869 ''');
3870 _performPendingAnalysisTasks();
3871 LibrarySpecificUnit targetA = new LibrarySpecificUnit(a, a);
3872 // Update and analyze.
3873 String newCode = r'''
3874 newFunction() {}
3875 main() {
3876 f((p) => 4.2);
3877 }
3878 f(c(int p)) {}
3879 ''';
3880 context.setContents(a, newCode);
3881 _performPendingAnalysisTasks();
3882 // Validate "(p) => 4.2" types.
3883 CompilationUnit unit = context.getResult(targetA, RESOLVED_UNIT2);
3884 SimpleIdentifier parameterName =
3885 EngineTestCase.findSimpleIdentifier(unit, newCode, 'p) => 4.2);');
3886 expect(parameterName.staticType, context.typeProvider.dynamicType);
3887 expect(parameterName.propagatedType, context.typeProvider.intType);
3888 }
3889
3890 void test_sequence_closureParameterTypesPropagation2() {
3891 var code = r'''
3892 import 'b.dart';
3893 main(x) {
3894 x.toMap();
3895 f((p) => 'z');
3896 }
3897 f(double c(int p)) {}
3898 ''';
3899 Source a = addSource('/a.dart', code);
3900 Source b = addSource(
3901 '/b.dart',
3902 r'''
3903 import 'c.dart';
3904 ''');
3905 addSource(
3906 '/c.dart',
3907 r'''
3908 import 'd.dart';
3909 ''');
3910 Source d = addSource(
3911 '/d.dart',
3912 r'''
3913 class D {}
3914 ''');
3915 _performPendingAnalysisTasks();
3916 LibrarySpecificUnit targetA = new LibrarySpecificUnit(a, a);
3917 // Update b.dart, resolution in a.dart is not affected.
3918 context.setContents(
3919 b,
3920 r'''
3921 import 'c.dart';
3922 class B {}
3923 ''');
3924 _assertValidAllResolution(a);
3925 _performPendingAnalysisTasks();
3926 // Update d.dart, this should invalidate a.dart and type propagation
3927 // performed for the closure parameter.
3928 context.setContents(
3929 d,
3930 r'''
3931 class D {
3932 toMap() {}
3933 }
3934 ''');
3935 _assertValidUnits(a, RESOLVED_UNIT6);
3936 _assertInvalidUnits(a, RESOLVED_UNIT7);
3937 _performPendingAnalysisTasks();
3938 // Validate "(p) =>" types.
3939 {
3940 CompilationUnit unit = context.getResult(targetA, RESOLVED_UNIT2);
3941 SimpleIdentifier parameterName =
3942 EngineTestCase.findSimpleIdentifier(unit, code, 'p) =>');
3943 expect(parameterName.staticType, context.typeProvider.dynamicType);
3944 expect(parameterName.propagatedType, context.typeProvider.intType);
3945 }
3946 }
3947
3948 void test_sequence_compoundingResults_exportNamespace() {
3949 Source a = addSource(
3950 '/a.dart',
3951 r'''
3952 class A<T> {}
3953 ''');
3954 Source b = addSource(
3955 '/b.dart',
3956 r'''
3957 export 'a.dart';
3958 ''');
3959 Source c = addSource(
3960 '/c.dart',
3961 r'''
3962 import 'b.dart';
3963 main() {
3964 new A<int>();
3965 }
3966 ''');
3967 _performPendingAnalysisTasks();
3968 expect(context.getErrors(c).errors, isEmpty);
3969 // Update a.dart, so that `A<T>` has a type bound.
3970 // This should invalidate export namespace for b.dart.
3971 // Currently we invalidate the whole LIBRARY_ELEMENT4.
3972 // So, c.dart will see the new `A<T extends B>` and report an error.
3973 context.setContents(
3974 a,
3975 r'''
3976 class A<T extends B> {}
3977 class B {}
3978 ''');
3979 _assertInvalid(b, LIBRARY_ELEMENT4);
3980 // Analyze and validate that a new error is reported.
3981 _performPendingAnalysisTasks();
3982 expect(context.getErrors(c).errors, hasLength(1));
3983 _assertValid(a, LIBRARY_ERRORS_READY);
3984 _assertValid(b, LIBRARY_ERRORS_READY);
3985 _assertValid(c, LIBRARY_ERRORS_READY);
3986 }
3987
3988 void test_sequence_compoundingResults_invalidateButKeepDependency() {
3989 Source a = addSource(
3990 '/a.dart',
3991 r'''
3992 import 'b.dart';
3993 class A {}
3994 ''');
3995 Source b = addSource(
3996 '/b.dart',
3997 r'''
3998 import 'a.dart';
3999 import 'c.dart';
4000 class B {}
4001 ''');
4002 Source c = addSource(
4003 '/c.dart',
4004 r'''
4005 class C {}
4006 ''');
4007 Source d = addSource(
4008 '/d.dart',
4009 r'''
4010 export 'b.dart';
4011 ''');
4012 _performPendingAnalysisTasks();
4013 expect(context.getErrors(c).errors, isEmpty);
4014 // Update: a.dart (limited) and b.dart (limited).
4015 // This should invalidate LIBRARY_ELEMENT4 in d.dart, but it should be
4016 // done in a way that keep dependency of other results od d.dart on
4017 // LIBRARY_ELEMENT4 of d.dart, so that when we perform unlimited
4018 // invalidation of c.dart, this makes d.dart invalid.
4019 context.setContents(
4020 a,
4021 r'''
4022 import 'b.dart';
4023 class A2 {}
4024 ''');
4025 context.setContents(
4026 b,
4027 r'''
4028 import 'a.dart';
4029 import 'c.dart';
4030 class B2 {}
4031 ''');
4032 context.setContents(
4033 c,
4034 r'''
4035 import 'dart:async';
4036 class C {}
4037 ''');
4038 _assertInvalid(d, LIBRARY_ELEMENT4);
4039 _assertInvalid(d, LIBRARY_ERRORS_READY);
4040 // Analyze and validate that a new error is reported.
4041 _performPendingAnalysisTasks();
4042 _assertValid(a, EXPORT_SOURCE_CLOSURE);
4043 _assertValid(b, EXPORT_SOURCE_CLOSURE);
4044 _assertValid(c, EXPORT_SOURCE_CLOSURE);
4045 _assertValid(d, EXPORT_SOURCE_CLOSURE);
4046 _assertValid(a, LIBRARY_ERRORS_READY);
4047 _assertValid(b, LIBRARY_ERRORS_READY);
4048 _assertValid(c, LIBRARY_ERRORS_READY);
4049 _assertValid(d, LIBRARY_ERRORS_READY);
4050 }
4051
4052 void test_sequence_compoundingResults_resolvedTypeNames() {
4053 Source a = addSource(
4054 '/a.dart',
4055 r'''
4056 class A {}
4057 ''');
4058 Source b = addSource(
4059 '/b.dart',
4060 r'''
4061 class B<T> {
4062 B(p);
4063 }
4064 ''');
4065 Source c = addSource(
4066 '/c.dart',
4067 r'''
4068 export 'a.dart';
4069 export 'b.dart';
4070 ''');
4071 Source d = addSource(
4072 '/d.dart',
4073 r'''
4074 import 'c.dart';
4075 main() {
4076 new B<int>(null);
4077 }
4078 ''');
4079 _performPendingAnalysisTasks();
4080 // Update a.dart and b.dart
4081 // This should invalidate most results in a.dart and b.dart
4082 //
4083 // This should also invalidate "compounding" results in c.dart, such as
4084 // READY_LIBRARY_ELEMENT6, which represent a state of the whole source
4085 // closure, not a result of this single unit or a library.
4086 //
4087 // The reason is that although type names (RESOLVED_UNIT5) b.dart will be
4088 // eventually resolved and set into b.dart elements, it may happen
4089 // after we attempted to re-resolve c.dart, which created Member(s), and
4090 // attempts to use elements without types set.
4091 context.setContents(
4092 a,
4093 r'''
4094 class A2 {}
4095 ''');
4096 context.setContents(
4097 b,
4098 r'''
4099 class B<T> {
4100 B(T p);
4101 }
4102 ''');
4103 _assertValidForChangedLibrary(a);
4104 _assertInvalid(a, LIBRARY_ERRORS_READY);
4105 _assertValidForChangedLibrary(b);
4106 _assertInvalid(b, LIBRARY_ERRORS_READY);
4107 _assertInvalid(c, READY_LIBRARY_ELEMENT6);
4108 _assertInvalid(c, READY_LIBRARY_ELEMENT7);
4109 // Analyze and validate that all results are valid.
4110 _performPendingAnalysisTasks();
4111 _assertValid(a, LIBRARY_ERRORS_READY);
4112 _assertValid(b, LIBRARY_ERRORS_READY);
4113 _assertValid(c, LIBRARY_ERRORS_READY);
4114 _assertValid(d, LIBRARY_ERRORS_READY);
4115 }
4116
4117 void test_sequence_dependenciesWithCycles() {
4118 Source a = addSource(
4119 '/a.dart',
4120 r'''
4121 const A = 1;
4122 ''');
4123 Source b = addSource(
4124 '/b.dart',
4125 r'''
4126 import 'c.dart';
4127 const B = C1 + 1;
4128 ''');
4129 Source c = addSource(
4130 '/c.dart',
4131 r'''
4132 import 'a.dart';
4133 import 'b.dart';
4134 const C1 = A + 1;
4135 const C2 = B + 2;
4136 ''');
4137 Source d = addSource(
4138 '/d.dart',
4139 r'''
4140 import 'c.dart';
4141 const D = C2 + 1;
4142 ''');
4143 _performPendingAnalysisTasks();
4144 // Update "A" constant.
4145 // This should invalidate results in all sources.
4146 context.setContents(
4147 a,
4148 r'''
4149 const A = 2;
4150 ''');
4151 _assertInvalid(a, LIBRARY_ERRORS_READY);
4152 _assertInvalid(b, LIBRARY_ERRORS_READY);
4153 _assertInvalid(c, LIBRARY_ERRORS_READY);
4154 _assertInvalid(d, LIBRARY_ERRORS_READY);
4155 }
4156
4157 void test_sequence_duplicateField_syntheticAndNot_renameNotSynthetic() {
4158 context.analysisOptions =
4159 new AnalysisOptionsImpl.from(context.analysisOptions)
4160 ..strongMode = true;
4161 Source a = addSource(
4162 '/a.dart',
4163 r'''
4164 class A {
4165 int foo;
4166 int get foo => 1;
4167 }
4168 class B extends A {
4169 int get foo => 2;
4170 }
4171 ''');
4172 _performPendingAnalysisTasks();
4173 expect(context.getErrors(a).errors, hasLength(2));
4174 // Update a.dart: rename "int foo" to "int bar".
4175 // The strong mode "getter cannot override field" error is gone.
4176 context.setContents(
4177 a,
4178 r'''
4179 class A {
4180 int bar;
4181 int get foo => 1;
4182 }
4183 class B extends A {
4184 int get foo => 2;
4185 }
4186 ''');
4187 _performPendingAnalysisTasks();
4188 expect(context.getErrors(a).errors, isEmpty);
4189 }
4190
4191 void test_sequence_inBodyChange_addRef_deltaChange() {
4192 Source a = addSource(
4193 '/a.dart',
4194 r'''
4195 class A {
4196 }
4197 ''');
4198 Source b = addSource(
4199 '/b.dart',
4200 r'''
4201 import 'a.dart';
4202 main(A a) {
4203 }
4204 ''');
4205 _performPendingAnalysisTasks();
4206 expect(context.getErrors(b).errors, hasLength(0));
4207 // Update b.dart: in-body incremental change - start referencing 'foo'.
4208 // Should update referenced names.
4209 context.setContents(
4210 b,
4211 r'''
4212 import 'a.dart';
4213 main(A a) {
4214 a.foo;
4215 }
4216 ''');
4217 _performPendingAnalysisTasks();
4218 expect(context.getErrors(b).errors, hasLength(1));
4219 // Update a.dart: add A.foo
4220 // b.dart is invalid, because it references 'foo'.
4221 context.setContents(
4222 a,
4223 r'''
4224 class A {
4225 int foo;
4226 }
4227 ''');
4228 _assertValidForChangedLibrary(a);
4229 _assertInvalid(a, LIBRARY_ERRORS_READY);
4230 _assertValidForDependentLibrary(b);
4231 _assertInvalid(b, LIBRARY_ERRORS_READY);
4232 _assertInvalidUnits(b, RESOLVED_UNIT4);
4233 // No errors after analysis.
4234 _performPendingAnalysisTasks();
4235 expect(context.getErrors(b).errors, hasLength(0));
4236 }
4237
4238 void test_sequence_inBodyChange_removeRef_deltaChange() {
4239 Source a = addSource(
4240 '/a.dart',
4241 r'''
4242 class A {
4243 }
4244 ''');
4245 Source b = addSource(
4246 '/b.dart',
4247 r'''
4248 import 'a.dart';
4249 main(A a) {
4250 a.foo;
4251 }
4252 ''');
4253 _performPendingAnalysisTasks();
4254 expect(context.getErrors(b).errors, hasLength(1));
4255 // Update b.dart: in-body incremental change - stop referencing 'foo'.
4256 // Should update referenced names.
4257 context.setContents(
4258 b,
4259 r'''
4260 import 'a.dart';
4261 main(A a) {
4262 }
4263 ''');
4264 _performPendingAnalysisTasks();
4265 expect(context.getErrors(b).errors, hasLength(0));
4266 // Update a.dart: add A.foo
4267 // b.dart is still valid, because it does references 'foo' anymore.
4268 context.setContents(
4269 a,
4270 r'''
4271 class A {
4272 int foo;
4273 }
4274 ''');
4275 _assertValidForChangedLibrary(a);
4276 _assertInvalid(a, LIBRARY_ERRORS_READY);
4277 _assertValidForDependentLibrary(b);
4278 _assertValidAllLibraryUnitResults(b);
4279 _assertValid(b, LIBRARY_ERRORS_READY);
4280 }
4281
4282 void test_sequence_middleLimited_thenFirstFull() {
4283 Source a = addSource(
4284 '/a.dart',
4285 r'''
4286 class A {}
4287 ''');
4288 Source b = addSource(
4289 '/b.dart',
4290 r'''
4291 export 'a.dart';
4292 ''');
4293 Source c = addSource(
4294 '/c.dart',
4295 r'''
4296 import 'b.dart';
4297 A a;
4298 ''');
4299 _performPendingAnalysisTasks();
4300 // Update b.dart: limited invalidation.
4301 // Results in c.dart are valid, because the change in b.dart does not
4302 // affect anything in c.dart.
4303 context.setContents(
4304 b,
4305 r'''
4306 export 'a.dart';
4307 class B {}
4308 ''');
4309 _assertValid(a, LIBRARY_ERRORS_READY);
4310 _assertInvalid(b, LIBRARY_ERRORS_READY);
4311 _assertValid(c, LIBRARY_ERRORS_READY);
4312 _assertUnitValid(c, RESOLVED_UNIT4);
4313 _assertUnitValid(c, RESOLVED_UNIT5);
4314 // Update a.dart: unlimited invalidation.
4315 // Results RESOLVED_UNIT4, RESOLVED_UNIT5, and RESOLVED_UNIT6 in c.dart
4316 // are invalid, because after unlimited change to a.dart everything
4317 // should be invalidated.
4318 // This fixes the problem that c.dart was not re-resolving type names.
4319 context.setContents(
4320 a,
4321 r'''
4322 import 'dart:async';
4323 class A {}
4324 ''');
4325 _assertInvalid(a, LIBRARY_ERRORS_READY);
4326 _assertInvalid(b, LIBRARY_ERRORS_READY);
4327 _assertInvalid(c, LIBRARY_ERRORS_READY);
4328 _assertInvalidUnits(c, RESOLVED_UNIT4);
4329 }
4330
4331 void test_sequence_noChange_thenChange() {
4332 Source a = addSource(
4333 '/a.dart',
4334 r'''
2278 class A { 4335 class A {
2279 A(); 4336 A();
2280 } 4337 }
4338
2281 class B { 4339 class B {
2282 B(); 4340 B();
2283 } 4341 }
2284 '''); 4342 ''');
2285 Source sourceB = addSource( 4343 Source b = addSource(
2286 "/b.dart", 4344 '/b.dart',
2287 r''' 4345 r'''
2288 library lib_b;
2289 import 'a.dart'; 4346 import 'a.dart';
2290 main() { 4347 main() {
2291 new A(); 4348 new A();
2292 } 4349 }
2293 '''); 4350 ''');
2294 _performPendingAnalysisTasks(); 4351 _performPendingAnalysisTasks();
2295 expect(context.getErrors(sourceA).errors, hasLength(0)); 4352 expect(context.getErrors(a).errors, hasLength(0));
2296 expect(context.getErrors(sourceB).errors, hasLength(0)); 4353 expect(context.getErrors(b).errors, hasLength(0));
2297 var unitA = context.getResolvedCompilationUnit2(sourceA, sourceA); 4354 var unitA = context.getResolvedCompilationUnit2(a, a);
2298 var unitElementA = unitA.element; 4355 var unitElementA = unitA.element;
2299 var libraryElementA = unitElementA.library; 4356 var libraryElementA = unitElementA.library;
2300 // Update a.dart, no declaration changes. 4357 // Update a.dart, no declaration changes.
2301 context.setContents( 4358 context.setContents(
2302 sourceA, 4359 a,
2303 r''' 4360 r'''
2304 library lib_a;
2305 class A { 4361 class A {
2306 A(); 4362 A();
2307 } 4363 }
2308 class B { 4364 class B {
2309 B(); 4365 B();
2310 } 4366 }
2311 '''); 4367 ''');
2312 _assertInvalid(sourceA, LIBRARY_ERRORS_READY); 4368 _assertValidForChangedLibrary(a);
2313 _assertValid(sourceB, LIBRARY_ELEMENT); 4369 _assertInvalid(a, LIBRARY_ERRORS_READY);
4370 _assertValidForDependentLibrary(b);
4371 _assertValidAllLibraryUnitResults(b);
4372 _assertValid(b, LIBRARY_ERRORS_READY);
2314 // The a.dart's unit and element are updated incrementally. 4373 // The a.dart's unit and element are updated incrementally.
2315 // They are the same instances as initially. 4374 // They are the same instances as initially.
2316 // So, all the references from other units are still valid. 4375 // So, all the references from other units are still valid.
2317 { 4376 {
2318 LibrarySpecificUnit target = new LibrarySpecificUnit(sourceA, sourceA); 4377 LibrarySpecificUnit target = new LibrarySpecificUnit(a, a);
2319 expect(analysisCache.getValue(target, RESOLVED_UNIT1), same(unitA)); 4378 expect(analysisCache.getValue(target, RESOLVED_UNIT1), same(unitA));
2320 expect(unitA.element, same(unitElementA)); 4379 expect(unitA.element, same(unitElementA));
2321 expect(unitElementA.library, same(libraryElementA)); 4380 expect(unitElementA.library, same(libraryElementA));
2322 } 4381 }
2323 // Analyze. 4382 // Analyze.
2324 _performPendingAnalysisTasks(); 4383 _performPendingAnalysisTasks();
2325 expect(context.getErrors(sourceA).errors, hasLength(0)); 4384 expect(context.getErrors(a).errors, hasLength(0));
2326 expect(context.getErrors(sourceB).errors, hasLength(0)); 4385 expect(context.getErrors(b).errors, hasLength(0));
2327 // The a.dart's unit and element are the same. 4386 // The a.dart's unit and element are the same.
2328 { 4387 {
2329 LibrarySpecificUnit target = new LibrarySpecificUnit(sourceA, sourceA); 4388 LibrarySpecificUnit target = new LibrarySpecificUnit(a, a);
2330 expect(analysisCache.getValue(target, RESOLVED_UNIT), same(unitA)); 4389 expect(analysisCache.getValue(target, RESOLVED_UNIT), same(unitA));
2331 expect(unitA.element, same(unitElementA)); 4390 expect(unitA.element, same(unitElementA));
2332 expect(unitElementA.library, same(libraryElementA)); 4391 expect(unitElementA.library, same(libraryElementA));
2333 } 4392 }
2334 // Update a.dart, rename A to A2, invalidates b.dart, so 4393 // Add a new method to a.dart. This invalidates b.dart, so
2335 // we know that the previous update did not damage dependencies. 4394 // we know that the previous update did not damage dependencies.
2336 context.setContents( 4395 context.setContents(
2337 sourceA, 4396 a,
2338 r''' 4397 r'''
2339 library lib_a;
2340 class A { 4398 class A {
2341 A(); 4399 A();
2342 m() {} 4400 m() {}
2343 } 4401 }
2344 class B { 4402 class B {
2345 B(); 4403 B();
2346 } 4404 }
2347 '''); 4405 ''');
2348 _assertInvalid(sourceA, LIBRARY_ERRORS_READY); 4406 _assertValidForChangedLibrary(a);
2349 _assertInvalid(sourceB, LIBRARY_ELEMENT); 4407 _assertInvalid(a, LIBRARY_ERRORS_READY);
4408 _assertValidForDependentLibrary(b);
4409 _assertValidAllResolution(b);
4410 _assertValidAllErrors(b);
2350 // The a.dart's unit and element are the same. 4411 // The a.dart's unit and element are the same.
2351 { 4412 {
2352 LibrarySpecificUnit target = new LibrarySpecificUnit(sourceA, sourceA); 4413 LibrarySpecificUnit target = new LibrarySpecificUnit(a, a);
2353 expect(analysisCache.getValue(target, RESOLVED_UNIT1), same(unitA)); 4414 expect(analysisCache.getValue(target, RESOLVED_UNIT1), same(unitA));
2354 expect(unitA.element, same(unitElementA)); 4415 expect(unitA.element, same(unitElementA));
2355 expect(unitElementA.library, same(libraryElementA)); 4416 expect(unitElementA.library, same(libraryElementA));
2356 } 4417 }
2357 // Analyze. 4418 // Analyze.
2358 _performPendingAnalysisTasks(); 4419 _performPendingAnalysisTasks();
2359 expect(context.getErrors(sourceA).errors, hasLength(0)); 4420 expect(context.getErrors(a).errors, hasLength(0));
2360 expect(context.getErrors(sourceB).errors, hasLength(0)); 4421 expect(context.getErrors(b).errors, hasLength(0));
2361 } 4422 }
2362 4423
2363 void test_unusedName() { 4424 void test_sequence_parts_disableForPart() {
2364 Source sourceA = addSource( 4425 Source a = addSource(
2365 "/a.dart", 4426 '/a.dart',
2366 r''' 4427 r'''
2367 library lib_a; 4428 library my_lib;
4429 part 'b.dart';
4430 ''');
4431 Source b = addSource(
4432 '/b.dart',
4433 r'''
4434 part of my_lib;
4435 class A {}
4436 ''');
4437 _performPendingAnalysisTasks();
4438 // Update b.dart - it is a part, which we don't support.
4439 // So, invalidate everything.
4440 context.setContents(
4441 b,
4442 r'''
4443 part of my_lib;
4444 class A {}
4445 class B {}
4446 ''');
4447 _assertInvalid(a, LIBRARY_ERRORS_READY);
4448 _assertInvalid(b, LIBRARY_ERRORS_READY);
4449 _assertInvalidUnits(a, RESOLVED_UNIT2);
4450 _assertInvalidUnits(b, RESOLVED_UNIT1);
4451 }
4452
4453 void test_sequence_parts_disableWithPart() {
4454 Source a = addSource(
4455 '/a.dart',
4456 r'''
4457 library my_lib;
4458 part 'b.dart';
4459 ''');
4460 Source b = addSource(
4461 '/b.dart',
4462 r'''
4463 part of my_lib;
4464 class B {}
4465 ''');
4466 _performPendingAnalysisTasks();
4467 // Update a.dart - it is a library with a part, which we don't support.
4468 // So, invalidate everything.
4469 context.setContents(
4470 a,
4471 r'''
4472 library my_lib;
4473 part 'b.dart';
4474 class A {}
4475 ''');
4476 _assertInvalid(a, LIBRARY_ERRORS_READY);
4477 _assertInvalid(b, LIBRARY_ERRORS_READY);
4478 _assertInvalidUnits(a, RESOLVED_UNIT1);
4479 _assertInvalidUnits(b, RESOLVED_UNIT1);
4480 }
4481
4482 void test_sequence_reorder_localFunctions() {
4483 Source a = addSource(
4484 '/a.dart',
4485 r'''
4486 f1() {
4487 localFunction() {
4488 const C = 1;
4489 }
4490 }
4491 f2() {}
4492 ''');
4493 _performPendingAnalysisTasks();
4494 // Update a.dart: reorder functions.
4495 // This should not fail with FrozenHashCodeException, because identifiers
4496 // of local elements should be relative to the enclosing top-level elements.
4497 context.setContents(
4498 a,
4499 r'''
4500 f2() {}
4501 f1() {
4502 localFunction() {
4503 const C = 1;
4504 }
4505 }
4506 ''');
4507 _assertValidForChangedLibrary(a);
4508 _assertInvalid(a, LIBRARY_ERRORS_READY);
4509 }
4510
4511 void test_sequence_unitConstants_addRemove() {
4512 Source a = addSource(
4513 '/a.dart',
4514 r'''
4515 const A = 1;
4516 const B = 2;
4517 const C = 3;
4518 foo() {
4519 const V1 = 10;
4520 }
4521 bar() {
4522 const V2 = 20;
4523 }
4524 ''');
4525 _performPendingAnalysisTasks();
4526 LibrarySpecificUnit unitA = new LibrarySpecificUnit(a, a);
4527 List<ConstantEvaluationTarget> oldConstants =
4528 context.getResult(unitA, COMPILATION_UNIT_CONSTANTS);
4529 expect(oldConstants, hasLength(5));
4530 ConstVariableElement oldA = _findConstVariable(oldConstants, 'A');
4531 ConstVariableElement oldB = _findConstVariable(oldConstants, 'B');
4532 ConstVariableElement oldC = _findConstVariable(oldConstants, 'C');
4533 ConstVariableElement oldV1 = _findConstVariable(oldConstants, 'V1');
4534 ConstVariableElement oldV2 = _findConstVariable(oldConstants, 'V2');
4535 expect(context.analysisCache.get(oldA), isNotNull);
4536 expect(context.analysisCache.get(oldB), isNotNull);
4537 expect(context.analysisCache.get(oldC), isNotNull);
4538 expect(context.analysisCache.get(oldV1), isNotNull);
4539 // Update and validate new constants.
4540 context.setContents(
4541 a,
4542 r'''
4543 const A = 1;
4544 const B = 2;
4545 const D = 4;
4546 foo() {
4547 const V1 = 10;
4548 }
4549 baz() {
4550 const V3 = 30;
4551 }
4552 ''');
4553 List<ConstantEvaluationTarget> newConstants =
4554 context.getResult(unitA, COMPILATION_UNIT_CONSTANTS);
4555 expect(newConstants, hasLength(5));
4556 expect(newConstants, contains(same(oldA)));
4557 expect(newConstants, contains(same(oldB)));
4558 expect(newConstants, contains(same(oldV1)));
4559 ConstVariableElement newD = _findConstVariable(newConstants, 'D');
4560 ConstVariableElement newV3 = _findConstVariable(newConstants, 'V3');
4561 // Perform analysis, compute constant values.
4562 _performPendingAnalysisTasks();
4563 // Validate const variable values.
4564 expect(context.analysisCache.get(oldA), isNotNull);
4565 expect(context.analysisCache.get(oldB), isNotNull);
4566 expect(context.analysisCache.get(oldV1), isNotNull);
4567 expect(context.analysisCache.get(oldC), isNull);
4568 expect(context.analysisCache.get(oldV2), isNull);
4569 expect(context.analysisCache.get(newD), isNotNull);
4570 expect(context.analysisCache.get(newV3), isNotNull);
4571 expect(oldA.evaluationResult.value.toIntValue(), 1);
4572 expect(oldB.evaluationResult.value.toIntValue(), 2);
4573 expect(newD.evaluationResult.value.toIntValue(), 4);
4574 expect(oldV1.evaluationResult.value.toIntValue(), 10);
4575 expect(newV3.evaluationResult.value.toIntValue(), 30);
4576 }
4577
4578 void test_sequence_unitConstants_local_insertSpace() {
4579 Source a = addSource(
4580 '/a.dart',
4581 r'''
4582 main() {
4583 const C = 1;
4584 }
4585 ''');
4586 _performPendingAnalysisTasks();
4587 LibrarySpecificUnit unitA = new LibrarySpecificUnit(a, a);
4588 List<ConstantEvaluationTarget> oldConstants =
4589 context.getResult(unitA, COMPILATION_UNIT_CONSTANTS);
4590 expect(oldConstants, hasLength(1));
4591 ConstVariableElement C = _findConstVariable(oldConstants, 'C');
4592 expect(context.analysisCache.get(C), isNotNull);
4593 // Insert a space before the name.
4594 context.setContents(
4595 a,
4596 r'''
4597 main() {
4598 const C = 1;
4599 }
4600 ''');
4601 List<ConstantEvaluationTarget> newConstants =
4602 context.getResult(unitA, COMPILATION_UNIT_CONSTANTS);
4603 expect(newConstants, hasLength(1));
4604 expect(newConstants, contains(same(C)));
4605 // Perform analysis, compute constant values.
4606 _performPendingAnalysisTasks();
4607 // Validate const variable values.
4608 expect(context.analysisCache.get(C), isNotNull);
4609 expect(C.evaluationResult.value.toIntValue(), 1);
4610 }
4611
4612 void test_sequence_useAnyResolvedUnit() {
4613 Source a = addSource(
4614 '/a.dart',
4615 r'''
4616 class A {}
4617 class B {}
4618 ''');
4619 Source b = addSource(
4620 '/b.dart',
4621 r'''
4622 import 'a.dart';
4623 main() {
4624 new A();
4625 }
4626 ''');
4627 _performPendingAnalysisTasks();
4628 _assertValid(a, LIBRARY_ERRORS_READY);
4629 _assertValid(b, LIBRARY_ERRORS_READY);
4630 // Invalidate RESOLVED_UNIT
4631 CacheEntry entryA = context.getCacheEntry(new LibrarySpecificUnit(a, a));
4632 entryA.setState(RESOLVED_UNIT, CacheState.FLUSHED);
4633 entryA.setState(RESOLVED_UNIT1, CacheState.FLUSHED);
4634 entryA.setState(RESOLVED_UNIT2, CacheState.FLUSHED);
4635 entryA.setState(RESOLVED_UNIT3, CacheState.FLUSHED);
4636 context.setContents(
4637 a,
4638 r'''
4639 class A {}
4640 class B2 {}
4641 ''');
4642 _assertValidAllLibraryUnitResults(b);
4643 _assertValid(b, LIBRARY_ERRORS_READY);
4644 }
4645
4646 void test_sequence_useAnyResolvedUnit_needsLibraryElement() {
4647 Source a = addSource(
4648 '/a.dart',
4649 r'''
4650 class A {}
4651 class B {}
4652 ''');
4653 // Perform analysis until we get RESOLVED_UNIT1.
4654 // But it does not have 'library' set, so `unitElement.context` is `null`.
4655 LibrarySpecificUnit aUnitTarget = new LibrarySpecificUnit(a, a);
4656 while (context.getResult(aUnitTarget, RESOLVED_UNIT1) == null) {
4657 context.performAnalysisTask();
4658 }
4659 // There was a bug with exception in incremental element builder.
4660 // We should not attempt to use `unitElement.context`.
4661 // It calls `unitElement.library`, which might be not set yet.
4662 context.setContents(
4663 a,
4664 r'''
4665 class A {}
4666 class B2 {}
4667 ''');
4668 // OK, no exceptions.
4669 }
4670
4671 void test_unusedName_class_add() {
4672 Source a = addSource(
4673 '/a.dart',
4674 r'''
2368 class A {} 4675 class A {}
2369 class B {} 4676 class B {}
2370 class C {} 4677 class C {}
2371 '''); 4678 ''');
2372 Source sourceB = addSource( 4679 Source b = addSource(
2373 "/b.dart", 4680 '/b.dart',
2374 r''' 4681 r'''
2375 library lib_b;
2376 import 'a.dart'; 4682 import 'a.dart';
2377 main() { 4683 main() {
2378 new A(); 4684 new A();
2379 new C(); 4685 new C();
2380 } 4686 }
2381 '''); 4687 ''');
2382 _performPendingAnalysisTasks(); 4688 _performPendingAnalysisTasks();
2383 // Update A. 4689 _assertValid(a, LINE_INFO);
2384 context.setContents( 4690 // The class B is not referenced.
2385 sourceA, 4691 // a.dart is invalid.
2386 r''' 4692 // b.dart is valid.
2387 library lib_a; 4693 context.setContents(
4694 a,
4695 r'''
2388 class A {} 4696 class A {}
2389 class B2 {} 4697 class B2 {}
2390 class C {} 4698 class C {}
2391 '''); 4699 ''');
2392 // Only a.dart is invalidated. 4700 _assertValidForChangedLibrary(a);
2393 // Because b.dart does not use B, so it is valid. 4701 _assertInvalid(a, LIBRARY_ERRORS_READY);
2394 _assertInvalid(sourceA, LIBRARY_ERRORS_READY); 4702 _assertUnitInvalid(a, RESOLVED_UNIT);
2395 _assertValid(sourceB, LIBRARY_ERRORS_READY); 4703 _assertValidForDependentLibrary(b);
2396 } 4704 _assertValidAllLibraryUnitResults(b);
2397 4705 _assertValid(b, LIBRARY_ERRORS_READY);
2398 void test_usedName_directUser() { 4706 }
2399 Source sourceA = addSource( 4707
2400 "/a.dart", 4708 void test_usedName_class_name_asHole_inBody() {
2401 r''' 4709 Source a = addSource(
2402 library lib_a; 4710 '/a.dart',
4711 r'''
2403 class A {} 4712 class A {}
2404 class B {} 4713 class B {}
2405 class C {} 4714 class C {}
2406 '''); 4715 ''');
2407 Source sourceB = addSource( 4716 Source b = addSource(
2408 "/b.dart", 4717 '/b.dart',
2409 r''' 4718 r'''
2410 library lib_b;
2411 import 'a.dart'; 4719 import 'a.dart';
2412 main() { 4720 main() {
2413 new A(); 4721 new A();
2414 new C2(); 4722 new C2();
2415 } 4723 }
2416 '''); 4724 ''');
2417 _performPendingAnalysisTasks(); 4725 _performPendingAnalysisTasks();
2418 expect(context.getErrors(sourceB).errors, hasLength(1)); 4726 // Update a.dart: remove C, add C2.
2419 // Update a.dart, invalidates b.dart because it references "C2". 4727 // b.dart is invalid, because it references C2.
2420 context.setContents( 4728 context.setContents(
2421 sourceA, 4729 a,
2422 r''' 4730 r'''
2423 library lib_a;
2424 class A {} 4731 class A {}
2425 class B {} 4732 class B {}
2426 class C2 {} 4733 class C2 {}
2427 '''); 4734 ''');
2428 _assertInvalid(sourceA, LIBRARY_ERRORS_READY); 4735 _assertValidForChangedLibrary(a);
2429 _assertInvalid(sourceB, LIBRARY_ERRORS_READY); 4736 _assertInvalid(a, LIBRARY_ERRORS_READY);
2430 // Now b.dart is analyzed and the error is fixed. 4737 _assertValidForDependentLibrary(b);
2431 _performPendingAnalysisTasks(); 4738 _assertInvalid(b, LIBRARY_ERRORS_READY);
2432 expect(context.getErrors(sourceB).errors, hasLength(0)); 4739 _assertInvalidUnits(b, RESOLVED_UNIT4);
2433 // Update a.dart, invalidates b.dart because it references "C". 4740 }
2434 context.setContents( 4741
2435 sourceA, 4742 void test_usedName_class_name_asSuper() {
2436 r''' 4743 Source a = addSource(
2437 library lib_a; 4744 '/a.dart',
4745 r'''
4746 class A {}
4747 ''');
4748 Source b = addSource(
4749 '/b.dart',
4750 r'''
4751 import 'a.dart';
4752 class B extends A {}
4753 ''');
4754 _performPendingAnalysisTasks();
4755 // Update a.dart: remove A, add A2.
4756 // b.dart is invalid, because it references A.
4757 context.setContents(
4758 a,
4759 r'''
4760 class A2 {}
4761 ''');
4762 _assertValidForChangedLibrary(a);
4763 _assertInvalid(a, LIBRARY_ERRORS_READY);
4764 _assertValidForDependentLibrary(b);
4765 _assertInvalid(b, LIBRARY_ERRORS_READY);
4766 _assertInvalidUnits(b, RESOLVED_UNIT4);
4767 }
4768
4769 void test_usedName_class_name_asTypeBound() {
4770 Source a = addSource(
4771 '/a.dart',
4772 r'''
4773 class A {}
4774 ''');
4775 Source b = addSource(
4776 '/b.dart',
4777 r'''
4778 import 'a.dart';
4779 class B<T extends A> {
4780 T f;
4781 }
4782 ''');
4783 _performPendingAnalysisTasks();
4784 // Update a.dart: remove A, add A2.
4785 // b.dart is invalid, because it references A.
4786 context.setContents(
4787 a,
4788 r'''
4789 class A2 {}
4790 ''');
4791 _assertValidForChangedLibrary(a);
4792 _assertInvalid(a, LIBRARY_ERRORS_READY);
4793 _assertValidForDependentLibrary(b);
4794 _assertInvalid(b, LIBRARY_ERRORS_READY);
4795 _assertInvalidUnits(b, RESOLVED_UNIT4);
4796 }
4797
4798 void test_usedName_class_name_inBody() {
4799 Source a = addSource(
4800 '/a.dart',
4801 r'''
2438 class A {} 4802 class A {}
2439 class B {} 4803 class B {}
2440 class C {} 4804 class C {}
2441 '''); 4805 ''');
2442 _assertInvalid(sourceA, LIBRARY_ERRORS_READY); 4806 Source b = addSource(
2443 _assertInvalid(sourceB, LIBRARY_ERRORS_READY); 4807 '/b.dart',
2444 _performPendingAnalysisTasks(); 4808 r'''
2445 // Now b.dart is analyzed and it again has the error. 4809 import 'a.dart';
2446 expect(context.getErrors(sourceB).errors, hasLength(1)); 4810 main() {
2447 } 4811 new A();
2448 4812 new C();
2449 void test_usedName_directUser_withIncremental() { 4813 }
2450 Source sourceA = addSource( 4814 ''');
2451 "/a.dart", 4815 _performPendingAnalysisTasks();
2452 r''' 4816 // Update a.dart: remove C, add C2.
2453 library lib_a; 4817 // b.dart is invalid, because it references C.
4818 context.setContents(
4819 a,
4820 r'''
4821 class A {}
4822 class B {}
4823 class C2 {}
4824 ''');
4825 _assertValidForChangedLibrary(a);
4826 _assertValidForDependentLibrary(b);
4827 _assertInvalid(a, LIBRARY_ERRORS_READY);
4828 _assertInvalid(b, LIBRARY_ERRORS_READY);
4829 _assertInvalidUnits(b, RESOLVED_UNIT4);
4830 }
4831
4832 void test_usedName_classMethod_name_inBody() {
4833 Source a = addSource(
4834 '/a.dart',
4835 r'''
2454 class A { 4836 class A {
2455 m() {} 4837 m() {}
2456 } 4838 }
2457 '''); 4839 ''');
2458 Source sourceB = addSource( 4840 Source b = addSource(
2459 "/b.dart", 4841 '/b.dart',
2460 r''' 4842 r'''
2461 library lib_b;
2462 import 'a.dart'; 4843 import 'a.dart';
2463 main() { 4844 main() {
2464 A a = new A(); 4845 A a = new A();
2465 a.m(); 4846 a.m();
2466 } 4847 }
2467 '''); 4848 ''');
2468 _performPendingAnalysisTasks(); 4849 _performPendingAnalysisTasks();
2469 // Update A. 4850 // Update a.dart: remove A.m, add A.m2.
2470 context.setContents( 4851 // b.dart is invalid, because it references 'm'.
2471 sourceA, 4852 context.setContents(
2472 r''' 4853 a,
2473 library lib_a; 4854 r'''
2474 class A { 4855 class A {
2475 m2() {} 4856 m2() {}
2476 } 4857 }
2477 '''); 4858 ''');
2478 _assertInvalid(sourceA, LIBRARY_ERRORS_READY); 4859 _assertValidForChangedLibrary(a);
2479 _assertInvalid(sourceB, LIBRARY_ERRORS_READY); 4860 _assertInvalid(a, LIBRARY_ERRORS_READY);
2480 } 4861 _assertValidForDependentLibrary(b);
2481 4862 _assertInvalidLibraryElements(b, LIBRARY_ELEMENT4);
2482 void test_usedName_indirectUser() { 4863 _assertInvalidUnits(b, RESOLVED_UNIT4);
2483 Source sourceA = addSource( 4864 _assertInvalid(b, LIBRARY_ERRORS_READY);
2484 "/a.dart", 4865 }
2485 r''' 4866
2486 library lib_a; 4867 void test_usedName_indirect_classMethod_name_inBody() {
4868 Source a = addSource(
4869 '/a.dart',
4870 r'''
2487 class A { 4871 class A {
2488 m() {} 4872 m() {}
2489 } 4873 }
2490 '''); 4874 ''');
2491 Source sourceB = addSource( 4875 Source b = addSource(
2492 "/b.dart", 4876 '/b.dart',
2493 r''' 4877 r'''
2494 library lib_b;
2495 import 'a.dart'; 4878 import 'a.dart';
2496 class B extends A {} 4879 class B extends A {}
2497 '''); 4880 ''');
2498 Source sourceC = addSource( 4881 Source c = addSource(
2499 "/c.dart", 4882 '/c.dart',
2500 r''' 4883 r'''
2501 library lib_c;
2502 import 'b.dart'; 4884 import 'b.dart';
2503 class C extends B { 4885 main() {
2504 main() { 4886 B b = new B();
2505 m(); 4887 b.m();
2506 } 4888 }
2507 } 4889 ''');
2508 '''); 4890 _performPendingAnalysisTasks();
2509 // No errors, "A.m" exists. 4891 // Update a.dart: remove A.m, add A.m2.
2510 _performPendingAnalysisTasks(); 4892 // b.dart has valid resolution, and invalid errors.
2511 expect(context.getErrors(sourceC).errors, hasLength(0)); 4893 // c.dart is invalid, because 'main' references 'm'.
2512 // Replace "A.m" with "A.m2", invalidate both b.dart and c.dart files. 4894 context.setContents(
2513 context.setContents( 4895 a,
2514 sourceA, 4896 r'''
2515 r'''
2516 library lib_a;
2517 class A { 4897 class A {
2518 m2() {} 4898 m2() {}
2519 } 4899 }
2520 '''); 4900 ''');
2521 _assertInvalid(sourceA, LIBRARY_ERRORS_READY); 4901 _assertValidForChangedLibrary(a);
2522 _assertInvalid(sourceB, LIBRARY_ERRORS_READY); 4902 _assertInvalid(a, LIBRARY_ERRORS_READY);
2523 _assertInvalid(sourceC, LIBRARY_ERRORS_READY); 4903
2524 // There is an error in c.dart, "A.m" does not exist. 4904 _assertValidForDependentLibrary(b);
2525 _performPendingAnalysisTasks(); 4905 _assertValidAllResolution(b);
2526 expect(context.getErrors(sourceB).errors, hasLength(0)); 4906 _assertInvalidHintsVerifyErrors(b);
2527 expect(context.getErrors(sourceC).errors, hasLength(1)); 4907
2528 // Restore "A.m", invalidate both b.dart and c.dart files. 4908 _assertValidForDependentLibrary(c);
2529 context.setContents( 4909 _assertInvalidLibraryElements(c, LIBRARY_ELEMENT5);
2530 sourceA, 4910 _assertInvalidUnits(c, RESOLVED_UNIT4);
2531 r''' 4911 _assertInvalid(c, LIBRARY_ERRORS_READY);
2532 library lib_a; 4912 }
2533 class A { 4913
2534 m() {} 4914 void test_usedName_indirect_classMethod_returnType_inBody() {
2535 } 4915 Source a = addSource(
2536 '''); 4916 '/a.dart',
2537 _assertInvalid(sourceA, LIBRARY_ERRORS_READY); 4917 r'''
2538 _assertInvalid(sourceB, LIBRARY_ERRORS_READY); 4918 class A {
2539 _assertInvalid(sourceC, LIBRARY_ERRORS_READY); 4919 int m() {
2540 // No errors, "A.m" exists. 4920 return 1;
2541 _performPendingAnalysisTasks(); 4921 }
2542 expect(context.getErrors(sourceC).errors, hasLength(0)); 4922 }
4923 ''');
4924 Source b = addSource(
4925 '/b.dart',
4926 r'''
4927 import 'a.dart';
4928 class B extends A {}
4929 ''');
4930 Source c = addSource(
4931 '/c.dart',
4932 r'''
4933 import 'b.dart';
4934 main() {
4935 B b = new B();
4936 b.m();
4937 }
4938 ''');
4939 _performPendingAnalysisTasks();
4940 // Update a.dart: remove A.m, add A.m2.
4941 // b.dart is invalid, because B extends A.
4942 // c.dart is invalid, because 'main' references 'm'.
4943 context.setContents(
4944 a,
4945 r'''
4946 class A {
4947 double m() {
4948 return 1.2;
4949 }
4950 }
4951 ''');
4952 _assertInvalid(a, LIBRARY_ERRORS_READY);
4953
4954 _assertValidForDependentLibrary(b);
4955 _assertValidAllResolution(b);
4956 _assertInvalidHintsVerifyErrors(b);
4957
4958 _assertValidForDependentLibrary(c);
4959 _assertInvalidLibraryElements(c, LIBRARY_ELEMENT5);
4960 _assertInvalidUnits(c, RESOLVED_UNIT4);
4961 _assertInvalid(c, LIBRARY_ERRORS_READY);
2543 } 4962 }
2544 4963
2545 void _assertInvalid(AnalysisTarget target, ResultDescriptor descriptor) { 4964 void _assertInvalid(AnalysisTarget target, ResultDescriptor descriptor) {
2546 CacheState state = analysisCache.getState(target, descriptor); 4965 CacheState actual = analysisCache.getState(target, descriptor);
2547 expect(state, CacheState.INVALID); 4966 if (actual != CacheState.INVALID) {
4967 fail("cache state of $target $descriptor: wanted INVALID, got: $actual");
4968 }
4969 }
4970
4971 /**
4972 * Assert that [VERIFY_ERRORS] and other error results that include it,
4973 * are invalid.
4974 */
4975 void _assertInvalidHintsVerifyErrors(Source unit) {
4976 _assertUnitInvalid(unit, HINTS);
4977 _assertUnitInvalid(unit, VERIFY_ERRORS);
4978 _assertUnitInvalid(unit, LIBRARY_UNIT_ERRORS);
4979 _assertInvalid(unit, DART_ERRORS);
4980 _assertInvalid(unit, LIBRARY_ERRORS_READY);
4981 }
4982
4983 /**
4984 * Assert that [LIBRARY_ELEMENT_RESULTS] for [first] and after it are invalid.
4985 */
4986 void _assertInvalidLibraryElements(
4987 Source source, ResultDescriptor<LibraryElement> first) {
4988 bool foundFirst = false;
4989 for (ResultDescriptor<LibraryElement> result in LIBRARY_ELEMENT_RESULTS) {
4990 foundFirst = foundFirst || result == first;
4991 if (foundFirst) {
4992 _assertInvalid(source, result);
4993 }
4994 }
4995 }
4996
4997 void _assertInvalidUnits(Source unit, ResultDescriptor<CompilationUnit> first,
4998 {Source library}) {
4999 var target = new LibrarySpecificUnit(library ?? unit, unit);
5000 bool foundFirst = false;
5001 for (ResultDescriptor<CompilationUnit> result in RESOLVED_UNIT_RESULTS) {
5002 foundFirst = foundFirst || result == first;
5003 if (foundFirst) {
5004 _assertInvalid(target, result);
5005 }
5006 }
5007 }
5008
5009 void _assertUnitInvalid(Source unitSource, ResultDescriptor descriptor,
5010 {Source librarySource}) {
5011 librarySource ??= unitSource;
5012 _assertInvalid(
5013 new LibrarySpecificUnit(librarySource, unitSource), descriptor);
5014 }
5015
5016 void _assertUnitValid(Source unitSource, ResultDescriptor descriptor,
5017 {Source librarySource}) {
5018 librarySource ??= unitSource;
5019 _assertValid(
5020 new LibrarySpecificUnit(librarySource, unitSource), descriptor);
5021 }
5022
5023 void _assertUnitValidTaskResults(Source unitSource, TaskDescriptor descriptor,
5024 {Source librarySource}) {
5025 librarySource ??= unitSource;
5026 for (ResultDescriptor result in descriptor.results) {
5027 _assertUnitValid(unitSource, result, librarySource: librarySource);
5028 }
2548 } 5029 }
2549 5030
2550 void _assertValid(AnalysisTarget target, ResultDescriptor descriptor) { 5031 void _assertValid(AnalysisTarget target, ResultDescriptor descriptor) {
2551 CacheState state = analysisCache.getState(target, descriptor); 5032 CacheState state = analysisCache.getState(target, descriptor);
2552 expect(state, CacheState.VALID); 5033 expect(state, isIn([CacheState.VALID, CacheState.FLUSHED]),
5034 reason: '$descriptor in $target');
5035 }
5036
5037 /**
5038 * Assert that all error results for the given [unit] are valid.
5039 */
5040 void _assertValidAllErrors(Source unit) {
5041 for (ListResultDescriptor<AnalysisError> result in ERROR_SOURCE_RESULTS) {
5042 _assertValid(unit, result);
5043 }
5044 for (ListResultDescriptor<AnalysisError> result in ERROR_UNIT_RESULTS) {
5045 _assertUnitValid(unit, result);
5046 }
5047 }
5048
5049 void _assertValidAllLibraryUnitResults(Source source, {Source library}) {
5050 library ??= source;
5051 for (ResultDescriptor<LibraryElement> result in LIBRARY_ELEMENT_RESULTS) {
5052 if (result == LIBRARY_ELEMENT4) {
5053 continue;
5054 }
5055 _assertValid(library, result);
5056 }
5057 LibrarySpecificUnit target = new LibrarySpecificUnit(library, source);
5058 for (ResultDescriptor<CompilationUnit> result in RESOLVED_UNIT_RESULTS) {
5059 _assertValid(target, result);
5060 }
5061 }
5062
5063 void _assertValidAllResolution(Source unit) {
5064 _assertValidUnits(unit, null);
5065 _assertUnitValidTaskResults(unit, ResolveUnitTypeNamesTask.DESCRIPTOR);
5066 _assertUnitValidTaskResults(unit, ResolveUnitTask.DESCRIPTOR);
5067 _assertValidTaskResults(unit, ResolveLibraryReferencesTask.DESCRIPTOR);
5068 _assertValidTaskResults(unit, ResolveLibraryTask.DESCRIPTOR);
5069 }
5070
5071 void _assertValidForAnyLibrary(Source source) {
5072 // Source results.
5073 _assertValidTaskResults(source, ScanDartTask.DESCRIPTOR);
5074 // Library results.
5075 _assertValidTaskResults(source, BuildLibraryElementTask.DESCRIPTOR);
5076 _assertValidTaskResults(source, BuildDirectiveElementsTask.DESCRIPTOR);
5077 _assertValidTaskResults(source, BuildSourceExportClosureTask.DESCRIPTOR);
5078 _assertValidTaskResults(source, ReadyLibraryElement2Task.DESCRIPTOR);
5079 _assertValidTaskResults(source, ComputeLibraryCycleTask.DESCRIPTOR);
5080 // Unit results.
5081 _assertUnitValidTaskResults(
5082 source, BuildCompilationUnitElementTask.DESCRIPTOR);
5083 _assertUnitValidTaskResults(
5084 source, ResolveDirectiveElementsTask.DESCRIPTOR);
5085 _assertUnitValidTaskResults(source, BuildEnumMemberElementsTask.DESCRIPTOR);
5086 }
5087
5088 void _assertValidForChangedLibrary(Source source) {
5089 _assertValidForAnyLibrary(source);
5090 }
5091
5092 void _assertValidForDependentLibrary(Source source) {
5093 _assertValidForAnyLibrary(source);
5094 // Library results.
5095 _assertValidTaskResults(source, BuildPublicNamespaceTask.DESCRIPTOR);
5096 }
5097
5098 void _assertValidTaskResults(AnalysisTarget target, TaskDescriptor task) {
5099 for (ResultDescriptor result in task.results) {
5100 _assertValid(target, result);
5101 }
5102 }
5103
5104 void _assertValidUnits(Source unit, ResultDescriptor<CompilationUnit> last,
5105 {Source library}) {
5106 var target = new LibrarySpecificUnit(library ?? unit, unit);
5107 bool foundLast = false;
5108 for (ResultDescriptor<CompilationUnit> result in RESOLVED_UNIT_RESULTS) {
5109 if (!foundLast) {
5110 _assertValid(target, result);
5111 }
5112 foundLast = foundLast || result == last;
5113 }
5114 }
5115
5116 ConstVariableElement _findConstVariable(
5117 List<ConstantEvaluationTarget> constants, String name) {
5118 return constants.singleWhere((c) {
5119 return c is ConstVariableElement && c.name == name;
5120 });
2553 } 5121 }
2554 5122
2555 void _performPendingAnalysisTasks([int maxTasks = 512]) { 5123 void _performPendingAnalysisTasks([int maxTasks = 512]) {
2556 for (int i = 0; context.performAnalysisTask().hasMoreWork; i++) { 5124 for (int i = 0; context.performAnalysisTask().hasMoreWork; i++) {
2557 if (i > maxTasks) { 5125 if (i > maxTasks) {
2558 fail('Analysis did not terminate.'); 5126 fail('Analysis did not terminate.');
2559 } 5127 }
2560 } 5128 }
2561 } 5129 }
5130
5131 void _verifyTwoLibrariesAllValid(
5132 String firstCodeA, String secondCodeA, String codeB) {
5133 Source a = addSource('/a.dart', firstCodeA);
5134 Source b = addSource('/b.dart', codeB);
5135 _performPendingAnalysisTasks();
5136 context.setContents(a, secondCodeA);
5137 _assertValidForChangedLibrary(a);
5138 _assertInvalid(a, LIBRARY_ERRORS_READY);
5139 _assertValidForDependentLibrary(b);
5140 _assertValidAllResolution(b);
5141 _assertValidAllErrors(b);
5142 }
5143
5144 void _verifyTwoLibrariesInvalidatesResolution(
5145 String firstCodeA, String secondCodeA, String codeB) {
5146 Source a = addSource('/a.dart', firstCodeA);
5147 Source b = addSource('/b.dart', codeB);
5148 _performPendingAnalysisTasks();
5149 context.setContents(a, secondCodeA);
5150 _assertValidForChangedLibrary(a);
5151 _assertInvalid(a, LIBRARY_ERRORS_READY);
5152 _assertValidForDependentLibrary(b);
5153 _assertInvalid(b, LIBRARY_ERRORS_READY);
5154 _assertInvalidUnits(b, RESOLVED_UNIT4);
5155 }
5156
5157 void _verifyTwoLibrariesInvalidHintsVerifyErrors(
5158 String firstCodeA, String secondCodeA, String codeB) {
5159 Source a = addSource('/a.dart', firstCodeA);
5160 Source b = addSource('/b.dart', codeB);
5161 _performPendingAnalysisTasks();
5162 context.setContents(a, secondCodeA);
5163 _assertValidForChangedLibrary(a);
5164 _assertInvalid(a, LIBRARY_ERRORS_READY);
5165 _assertValidForDependentLibrary(b);
5166 _assertValidAllResolution(b);
5167 _assertUnitValid(b, RESOLVE_UNIT_ERRORS);
5168 _assertInvalidHintsVerifyErrors(b);
5169 }
5170 }
5171
5172 class _AnalysisContextImplTest_Source_exists_true extends TestSource {
5173 @override
5174 bool exists() => true;
5175 }
5176
5177 class _AnalysisContextImplTest_Source_getModificationStamp_fromSource
5178 extends TestSource {
5179 int stamp;
5180 _AnalysisContextImplTest_Source_getModificationStamp_fromSource(this.stamp);
5181 @override
5182 int get modificationStamp => stamp;
5183 }
5184
5185 class _AnalysisContextImplTest_Source_getModificationStamp_overridden
5186 extends TestSource {
5187 int stamp;
5188 _AnalysisContextImplTest_Source_getModificationStamp_overridden(this.stamp);
5189 @override
5190 int get modificationStamp => stamp;
2562 } 5191 }
2563 5192
2564 class _AnalysisContextImplTest_test_applyChanges_removeContainer 5193 class _AnalysisContextImplTest_test_applyChanges_removeContainer
2565 implements SourceContainer { 5194 implements SourceContainer {
2566 Source libB; 5195 Source libB;
2567 _AnalysisContextImplTest_test_applyChanges_removeContainer(this.libB); 5196 _AnalysisContextImplTest_test_applyChanges_removeContainer(this.libB);
2568 @override 5197 @override
2569 bool contains(Source source) => source == libB; 5198 bool contains(Source source) => source == libB;
2570 } 5199 }
5200
5201 /**
5202 * A visitor that can be used to compare all of the elements in an element model
5203 * with a previously created map of elements. The class [ElementGatherer] can be
5204 * used to create the map of elements.
5205 */
5206 class _ElementComparer extends GeneralizingElementVisitor {
5207 /**
5208 * The previously created map of elements.
5209 */
5210 final Map<Element, Element> previousElements;
5211
5212 /**
5213 * The number of elements that were found to have been overwritten.
5214 */
5215 int overwrittenCount = 0;
5216
5217 /**
5218 * A buffer to which a description of the overwritten elements will be written .
5219 */
5220 final StringBuffer buffer = new StringBuffer();
5221
5222 /**
5223 * Initialize a newly created visitor.
5224 */
5225 _ElementComparer(this.previousElements);
5226
5227 /**
5228 * Expect that no differences were found, causing the test to fail if that
5229 * wasn't the case.
5230 */
5231 void expectNoDifferences() {
5232 if (overwrittenCount > 0) {
5233 fail('Found $overwrittenCount overwritten elements.$buffer');
5234 }
5235 }
5236
5237 @override
5238 void visitElement(Element element) {
5239 Element previousElement = previousElements[element];
5240 if (!identical(previousElement, element)) {
5241 if (overwrittenCount == 0) {
5242 buffer.writeln();
5243 }
5244 overwrittenCount++;
5245 buffer.writeln('Overwritten element:');
5246 Element currentElement = element;
5247 while (currentElement != null) {
5248 buffer.write(' ');
5249 buffer.writeln(currentElement.toString());
5250 currentElement = currentElement.enclosingElement;
5251 }
5252 }
5253 super.visitElement(element);
5254 }
5255 }
5256
5257 /**
5258 * A visitor that can be used to collect all of the elements in an element
5259 * model.
5260 */
5261 class _ElementGatherer extends GeneralizingElementVisitor {
5262 /**
5263 * The map in which the elements are collected. The value of each key is the
5264 * key itself.
5265 */
5266 Map<Element, Element> elements = new HashMap<Element, Element>();
5267
5268 /**
5269 * Initialize the visitor.
5270 */
5271 _ElementGatherer();
5272
5273 @override
5274 void visitElement(Element element) {
5275 elements[element] = element;
5276 super.visitElement(element);
5277 }
5278 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/src/context/cache_test.dart ('k') | packages/analyzer/test/src/context/mock_sdk.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698