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

Unified Diff: pkg/analyzer/test/src/context/context_test.dart

Issue 2834773003: Remove the limited invalidation feature. (Closed)
Patch Set: Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/test/src/context/context_test.dart
diff --git a/pkg/analyzer/test/src/context/context_test.dart b/pkg/analyzer/test/src/context/context_test.dart
index 441cad9c485222d6c705c63c4f422c986a8c73e9..26ceedfd7784fca6028fb1fadd5e70720af833d5 100644
--- a/pkg/analyzer/test/src/context/context_test.dart
+++ b/pkg/analyzer/test/src/context/context_test.dart
@@ -20,7 +20,6 @@ import 'package:analyzer/source/package_map_resolver.dart';
import 'package:analyzer/src/cancelable_future.dart';
import 'package:analyzer/src/context/cache.dart';
import 'package:analyzer/src/context/context.dart';
-import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/scanner/scanner.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/resolver.dart';
@@ -29,7 +28,6 @@ import 'package:analyzer/src/generated/utilities_collection.dart';
import 'package:analyzer/src/task/dart.dart';
import 'package:analyzer/src/task/html.dart';
import 'package:analyzer/task/dart.dart';
-import 'package:analyzer/task/general.dart';
import 'package:analyzer/task/model.dart';
import 'package:html/dom.dart' show Document;
import 'package:test/test.dart';
@@ -38,13 +36,11 @@ import 'package:watcher/src/utils.dart';
import '../../generated/engine_test.dart';
import '../../generated/test_support.dart';
-import '../../utils.dart';
import 'abstract_context.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(AnalysisContextImplTest);
- defineReflectiveTests(LimitedInvalidateTest);
});
}
@@ -2237,48 +2233,28 @@ void g() { f(null); }''');
reason: "part resolved 1");
// update and analyze #1
context.setContents(partSource, "part of lib; // 1");
- if (context.analysisOptions.finerGrainedInvalidation) {
- expect(
- context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
- reason: "library changed 2");
- expect(
- context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
- reason: "part changed 2");
- } else {
- expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull,
- reason: "library changed 2");
- expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull,
- reason: "part changed 2");
- _analyzeAll_assertFinished();
- expect(
- context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
- reason: "library resolved 2");
- expect(
- context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
- reason: "part resolved 2");
- }
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull,
+ reason: "library changed 2");
+ expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull,
+ reason: "part changed 2");
+ _analyzeAll_assertFinished();
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
+ reason: "library resolved 2");
+ expect(
+ context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
+ reason: "part resolved 2");
// update and analyze #2
context.setContents(partSource, "part of lib; // 12");
- if (context.analysisOptions.finerGrainedInvalidation) {
- expect(
- context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
- reason: "library changed 3");
- expect(
- context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
- reason: "part changed 3");
- } else {
- expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull,
- reason: "library changed 3");
- expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull,
- reason: "part changed 3");
- _analyzeAll_assertFinished();
- expect(
- context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
- reason: "library resolved 3");
- expect(
- context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
- reason: "part resolved 3");
- }
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull,
+ reason: "library changed 3");
+ expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull,
+ reason: "part changed 3");
+ _analyzeAll_assertFinished();
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
+ reason: "library resolved 3");
+ expect(
+ context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
+ reason: "part resolved 3");
}
void test_performAnalysisTask_getContentException_dart() {
@@ -2415,11 +2391,7 @@ library expectedToFindSemicolon
_changeSource(source, "");
source.generateExceptionOnRead = true;
_analyzeAll_assertFinished();
- if (context.analysisOptions.finerGrainedInvalidation) {
- expect(source.readCount, 7);
- } else {
- expect(source.readCount, 5);
- }
+ expect(source.readCount, 5);
}
void test_performAnalysisTask_missingPart() {
@@ -3022,2308 +2994,6 @@ int a = 0;''');
}
}
-@reflectiveTest
-class LimitedInvalidateTest extends AbstractContextTest {
- @override
- void setUp() {
- super.setUp();
- AnalysisOptionsImpl options =
- new AnalysisOptionsImpl.from(context.analysisOptions);
- options.incremental = true;
- options.finerGrainedInvalidation = true;
- context.analysisOptions = options;
- }
-
- void test_applyChanges_changedSource_removeFile() {
- File file = resourceProvider.newFile('/test.dart', 'main() {}');
- Source source = file.createSource();
- context.applyChanges(new ChangeSet()..addedSource(source));
- // Analyze all.
- _performPendingAnalysisTasks();
- expect(context.getResolvedCompilationUnit2(source, source), isNotNull);
- // Delete the file, but tell the context that it is changed.
- // This might happen as a race condition.
- // Or it might be a mishandling of file notification events.
- file.delete();
- context.applyChanges(new ChangeSet()..changedSource(source));
- // All the analysis results are gone.
- void noResolvedUnits() {
- LibrarySpecificUnit unit = new LibrarySpecificUnit(source, source);
- RESOLVED_UNIT_RESULTS.forEach((result) {
- expect(context.getResult(unit, result), isNull);
- });
- }
-
- noResolvedUnits();
- // Analyze again.
- // The source does not exist, so still no resolution.
- _performPendingAnalysisTasks();
- noResolvedUnits();
- expect(context.getModificationStamp(source), -1);
- }
-
- void test_class_addMethod_useAsHole_inTopLevelVariable() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-var B = A.foo;
-''');
- Source c = addSource(
- '/c.dart',
- r'''
-import 'b.dart';
-var C = B;
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(1));
- // Update a.dart: add A.foo.
- // 'B' in b.dart is invalid, because 'B' references 'foo'
- // c.dart is invalid, because 'C' references 'B'
- context.setContents(
- a,
- r'''
-class A {
- static void foo(int p) {}
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
-
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
-
- _assertValidForDependentLibrary(c);
- _assertInvalid(c, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(c, RESOLVED_UNIT4);
- }
-
- void test_class_addMethod_useClass() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-class B extends A {
- foo() {}
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-B b = null;
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove add A.bar.
- // b.dart is valid, because though it uses A, it has the same element.
- context.setContents(
- a,
- r'''
-class A {}
-class B extends A {
- foo() {}
- bar() {}
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_class_annotation_add_deprecated() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-List<A> foo() => [];
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(0));
- // Add @deprecated annotation.
- // b.dart has valid resolution, because A is still A, so only errors are
- // invalidated.
- context.setContents(
- a,
- r'''
-@deprecated
-class A {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertValidAllResolution(b);
- // Analysis is done successfully.
- _performPendingAnalysisTasks();
- _assertValid(a, LIBRARY_ERRORS_READY);
- _assertValid(a, READY_RESOLVED_UNIT);
- _assertValid(b, LIBRARY_ERRORS_READY);
- _assertValid(b, READY_RESOLVED_UNIT);
- expect(context.getErrors(b).errors, hasLength(1));
- }
-
- void test_class_annotation_remove_deprecated() {
- Source a = addSource(
- '/a.dart',
- r'''
-@deprecated
-class A {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-List<A> foo() => [];
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(1));
- // Add @deprecated annotation.
- // b.dart has valid resolution, because A is still A, so only errors are
- // invalidated.
- context.setContents(
- a,
- r'''
-class A {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertValidAllResolution(b);
- // Analysis is done successfully.
- _performPendingAnalysisTasks();
- _assertValid(a, LIBRARY_ERRORS_READY);
- _assertValid(a, READY_RESOLVED_UNIT);
- _assertValid(b, LIBRARY_ERRORS_READY);
- _assertValid(b, READY_RESOLVED_UNIT);
- expect(context.getErrors(b).errors, hasLength(0));
- }
-
- void test_class_constructor_named_changeName() {
- // Update a.dart: change A.named() to A.named2().
- // b.dart is invalid, because it references A.named().
- _verifyTwoLibrariesInvalidatesResolution(
- r'''
-class A {
- A.named();
-}
-''',
- r'''
-class A {
- A.named2();
-}
-''',
- r'''
-import 'a.dart';
-main() {
- new A.named();
-}
-''');
- }
-
- void test_class_constructor_named_parameters_add() {
- // Update a.dart: add a new parameter to A.named().
- // b.dart is invalid, because it references A.named().
- _verifyTwoLibrariesInvalidatesResolution(
- r'''
-class A {
- A.named();
-}
-''',
- r'''
-class A {
- A.named(int p);
-}
-''',
- r'''
-import 'a.dart';
-main() {
- new A.named();
-}
-''');
- }
-
- void test_class_constructor_named_parameters_change_usedSuper() {
- // Update a.dart: change A.named().
- // b.dart is invalid, because it references A.named().
- _verifyTwoLibrariesInvalidatesResolution(
- r'''
-class A {
- A.named(int a);
-}
-''',
- r'''
-class A {
- A.named(String a);
-}
-''',
- r'''
-import 'a.dart';
-class B extends A {
- B() : super.named(42);
-}
-''');
- }
-
- void test_class_constructor_named_parameters_remove() {
- // Update a.dart: remove a new parameter of A.named().
- // b.dart is invalid, because it references A.named().
- _verifyTwoLibrariesInvalidatesResolution(
- r'''
-class A {
- A.named(int p);
-}
-''',
- r'''
-class A {
- A.named();
-}
-''',
- r'''
-import 'a.dart';
-main() {
- new A.named();
-}
-''');
- }
-
- void test_class_constructor_named_remove_notUsed() {
- // Update a.dart: remove A.foo().
- // b.dart is valid, because it does not reference A.foo().
- _verifyTwoLibrariesAllValid(
- r'''
-class A {
- A.foo();
- A.bar();
-}
-''',
- r'''
-class A {
- A.bar();
-}
-''',
- r'''
-import 'a.dart';
-main() {
- new A.bar();
-}
-''');
- }
-
- void test_class_constructor_named_remove_used() {
- // Update a.dart: remove A.named().
- // b.dart is invalid, because it references A.named().
- _verifyTwoLibrariesInvalidatesResolution(
- r'''
-class A {
- A.named();
-}
-''',
- r'''
-class A {
-}
-''',
- r'''
-import 'a.dart';
-main() {
- new A.named();
-}
-''');
- }
-
- void test_class_constructor_unnamed_parameters_change_notUsedSuper() {
- // Update a.dart: change A().
- // Resolution of b.dart is valid, because it does not reference A().
- // Hints and verify errors are invalid, because it extends A.
- // TODO(scheglov) we could keep valid hints and verify errors,
- // because only constructor is changed - this cannot add/remove
- // inherited unimplemented members.
- _verifyTwoLibrariesInvalidHintsVerifyErrors(
- r'''
-class A {
- A(int a);
- A.named(int a);
-}
-''',
- r'''
-class A {
- A(String a);
- A.named(int a);
-}
-''',
- r'''
-import 'a.dart';
-class B extends A {
- B() : super.named(42);
-}
-''');
- }
-
- void test_class_constructor_unnamed_parameters_change_usedSuper() {
- // Update a.dart: change A().
- // b.dart is invalid, because it references A().
- _verifyTwoLibrariesInvalidatesResolution(
- r'''
-class A {
- A(int a);
-}
-''',
- r'''
-class A {
- A(String a);
-}
-''',
- r'''
-import 'a.dart';
-class B extends A {
- B() : super(42);
-}
-''');
- }
-
- void test_class_constructor_unnamed_parameters_remove() {
- // Update a.dart: change A().
- // b.dart is invalid, because it references A().
- _verifyTwoLibrariesInvalidatesResolution(
- r'''
-class A {
- A(int a, int b);
-}
-''',
- r'''
-class A {
- A(int a);
-}
-''',
- r'''
-import 'a.dart';
-main() {
- new A(1, 2);
-}
-''');
- }
-
- void test_class_constructor_unnamed_remove_notUsed() {
- // Update a.dart: remove A().
- // b.dart is invalid, because it instantiates A.
- _verifyTwoLibrariesInvalidatesResolution(
- r'''
-class A {
- A();
- A.named();
-}
-''',
- r'''
-class A {
- A.named();
-}
-''',
- r'''
-import 'a.dart';
-main() {
- new A.named();
-}
-''');
- }
-
- void test_class_constructor_unnamed_remove_used() {
- // Update a.dart: remove A().
- // b.dart is invalid, because it references A().
- _verifyTwoLibrariesInvalidatesResolution(
- r'''
-class A {
- A();
-}
-''',
- r'''
-class A {
-}
-''',
- r'''
-import 'a.dart';
-main() {
- new A();
-}
-''');
- }
-
- void test_class_method_change_notUsed() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- foo() {}
- bar() {}
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main(A a) {
- a.foo();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove A.bar, add A.bar2.
- // b.dart is valid, because it doesn't references 'bar' or 'bar2'.
- context.setContents(
- a,
- r'''
-class A {
- foo() {}
- bar2() {}
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_class_method_change_notUsed_throughSubclass_extends() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- foo() {}
- bar() {}
-}
-class B extends A {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main(B b) {
- a.foo();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove A.bar, add A.bar2.
- // b.dart is valid, because it doesn't references 'bar' or 'bar2'.
- context.setContents(
- a,
- r'''
-class A {
- foo() {}
- bar2() {}
-}
-class B extends A {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_class_method_definedInSuper_sameLibrary() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- m() {}
-}
-class B extends A {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main(B b) {
- b.m();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: change A.m
- // This makes B changed.
- // b.dart is invalid, because it references B.
- context.setContents(
- a,
- r'''
-class A {
- m2() {}
-}
-class B extends A {}
-''');
- _assertValidForChangedLibrary(a);
- _assertValidForDependentLibrary(b);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_class_method_remove_notUsed_instantiated() {
- Source a = addSource(
- '/a.dart',
- r'''
-abstract class I {
- void foo();
-}
-class A implements I {
- void foo() {}
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- new A();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove 'A.foo'.
- // b.dart is valid because it does not reference 'foo'.
- // The class 'A' has a warning, but it is still not abstract.
- context.setContents(
- a,
- r'''
-abstract class I {
- void fo();
-}
-class A implements I {
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
-
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValidAllResolution(b);
- _assertValidAllErrors(b);
- }
-
- void test_class_method_remove_subclass() {
- Source a = addSource(
- '/a.dart',
- r'''
-abstract class I {
- void foo();
-}
-class A implements I {
- void foo() {}
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-class B extends A {}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove A.bar, add A.bar2.
- // b.dart
- // Resolution is valid because 'foo' is not referenced.
- // HINTS are invalid because 'B' might have invalid @override.
- // VERIFY_ERRORS are invalid because 'B' might not implement something.
- // Other errors are also invalid.
- context.setContents(
- a,
- r'''
-abstract class I {
- void foo();
-}
-class A implements I {
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
-
- _assertValidForDependentLibrary(b);
- _assertValidAllResolution(b);
- _assertInvalidHintsVerifyErrors(b);
- }
-
- void test_class_private_member() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- A();
- A._privateConstructor();
-
- foo() {}
-
- int _privateField;
- _privateMethod() {}
- int get _privateGetter => null;
- void set _privateSetter(_) {}
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main(A a) {
- a.foo();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: rename private members of A
- // b.dart is valid, it cannot see these private members.
- context.setContents(
- a,
- r'''
-class A {
- A();
- A._privateConstructor2();
-
- foo() {}
-
- int _privateField2;
- _privateMethod2() {}
- int get _privateGetter2 => null;
- void set _privateSetter2(_) {}
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_enum_add() {
- Source a = addSource(
- '/a.dart',
- r'''
-main() {
- print(MyEnum.A);
-}
-''');
- _performPendingAnalysisTasks();
- // Insert a new enum declaration.
- // No errors expected.
- context.setContents(
- a,
- r'''
-main() {
- print(MyEnum.A);
-}
-enum MyEnum { A, B, C }
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(a).errors, isEmpty);
- }
-
- void test_private_class() {
- Source a = addSource(
- '/a.dart',
- r'''
-class _A {}
-class _B2 {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- new _A();
- new _B();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: change _A and _B2
- // b.dart is valid, because _A, _B, _A2 and _B2 are all private,
- // so b.dart cannot see them.
- context.setContents(
- a,
- r'''
-class _A2 {}
-class _B {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_private_topLevelVariable() {
- Source a = addSource(
- '/a.dart',
- r'''
-int _V = 1;
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- print(_A);
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: change _V
- // b.dart is valid, because _V is private and b.dart cannot see it.
- context.setContents(
- a,
- r'''
-int _V = 2;
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_private_topLevelVariable_throughPublic() {
- Source a = addSource(
- '/a.dart',
- r'''
-int _A = 1;
-int B = _A + 1;
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- print(B);
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: change _A
- // b.dart is invalid, because it uses B, which uses _A.
- context.setContents(
- a,
- r'''
-int _A = 2;
-int B = _A + 1;
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_sequence_applyChanges_changedSource() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-class B {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- new A();
- new B();
-}
-''');
- _performPendingAnalysisTasks();
- resourceProvider.updateFile(
- resourceProvider.convertPath('/a.dart'),
- r'''
-class A2 {}
-class B {}
-''');
- // Update a.dart: remove A, add A2.
- // b.dart is invalid, because it references A.
- var changeSet = new ChangeSet()..changedSource(a);
- context.applyChanges(changeSet);
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- // Analyze.
- _performPendingAnalysisTasks();
- expect(context.getErrors(a).errors, hasLength(0));
- expect(context.getErrors(b).errors, hasLength(1));
- _assertValid(a, READY_RESOLVED_UNIT);
- _assertValid(b, READY_RESOLVED_UNIT);
- _assertValidAllLibraryUnitResults(a);
- _assertValidAllLibraryUnitResults(b);
- }
-
- void test_sequence_class_give_take() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-class B {}
-class C {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- new A();
- new C2();
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(1));
- // Update a.dart: remove C, add C2.
- // b.dart is invalid, because it references C2.
- context.setContents(
- a,
- r'''
-class A {}
-class B {}
-class C2 {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- // Now b.dart is analyzed and the error is fixed.
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(0));
- // Update a.dart: remove C2, add C.
- // b.dart is invalid, because it references C2.
- context.setContents(
- a,
- r'''
-class A {}
-class B {}
-class C {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- // Now b.dart is analyzed and it again has the error.
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(1));
- }
-
- void test_sequence_class_removeMethod_overridden() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- void foo() {}
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-class B extends A {
- @override
- void foo() {}
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(0));
- // Update a.dart: remove add A.foo.
- // b.dart has a new hint, because B.foo does not override anything
- context.setContents(
- a,
- r'''
-class A {
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllResolution(b);
- _assertInvalidHintsVerifyErrors(b);
-
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(1));
- }
-
- void test_sequence_clearParameterElements() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- foo(int p) {}
-}
-final a = new A();
-main() {
- a.foo(42);
-}
-''');
- _performPendingAnalysisTasks();
- Expression find42() {
- CompilationUnit unit =
- context.getResult(new LibrarySpecificUnit(a, a), RESOLVED_UNIT);
- ExpressionStatement statement =
- AstFinder.getStatementsInTopLevelFunction(unit, 'main').single;
- MethodInvocation invocation = statement.expression;
- return invocation.argumentList.arguments[0];
- }
-
- {
- Expression argument = find42();
- expect(argument.staticParameterElement, isNull);
- }
-
- // Update a.dart: add type annotation for 'a'.
- // '42' has 'staticParameterElement'.
- context.setContents(
- a,
- r'''
-class A {
- foo(int p) {}
-}
-final A a = new A();
-main() {
- a.foo(42);
-}
-''');
- _performPendingAnalysisTasks();
- {
- Expression argument = find42();
- expect(argument.staticParameterElement, isNotNull);
- }
-
- // Update a.dart: remove type annotation for 'a'.
- // '42' doesn't have 'staticParameterElement'.
- context.setContents(
- a,
- r'''
-class A {
- foo(int p) {}
-}
-final a = new A();
-main() {
- a.foo(42);
-}
-''');
- _performPendingAnalysisTasks();
- {
- Expression argument = find42();
- expect(argument.staticParameterElement, isNull);
- }
- }
-
- void test_sequence_closureParameterTypesPropagation() {
- Source a = addSource(
- '/a.dart',
- r'''
-main() {
- f((p) => 4.2);
-}
-f(c(int p)) {}
-''');
- _performPendingAnalysisTasks();
- LibrarySpecificUnit targetA = new LibrarySpecificUnit(a, a);
- // Update and analyze.
- String newCode = r'''
-newFunction() {}
-main() {
- f((p) => 4.2);
-}
-f(c(int p)) {}
-''';
- context.setContents(a, newCode);
- _performPendingAnalysisTasks();
- // Validate "(p) => 4.2" types.
- CompilationUnit unit = context.getResult(targetA, RESOLVED_UNIT2);
- SimpleIdentifier parameterName =
- EngineTestCase.findSimpleIdentifier(unit, newCode, 'p) => 4.2);');
- expect(parameterName.staticType, context.typeProvider.dynamicType);
- expect(parameterName.propagatedType, context.typeProvider.intType);
- }
-
- void test_sequence_closureParameterTypesPropagation2() {
- var code = r'''
-import 'b.dart';
-main(x) {
- x.toMap();
- f((p) => 'z');
-}
-f(double c(int p)) {}
-''';
- Source a = addSource('/a.dart', code);
- Source b = addSource(
- '/b.dart',
- r'''
-import 'c.dart';
-''');
- addSource(
- '/c.dart',
- r'''
-import 'd.dart';
-''');
- Source d = addSource(
- '/d.dart',
- r'''
-class D {}
-''');
- _performPendingAnalysisTasks();
- LibrarySpecificUnit targetA = new LibrarySpecificUnit(a, a);
- // Update b.dart, resolution in a.dart is not affected.
- context.setContents(
- b,
- r'''
-import 'c.dart';
-class B {}
-''');
- _assertValidAllResolution(a);
- _performPendingAnalysisTasks();
- // Update d.dart, this should invalidate a.dart and type propagation
- // performed for the closure parameter.
- context.setContents(
- d,
- r'''
-class D {
- toMap() {}
-}
-''');
- _assertValidUnits(a, RESOLVED_UNIT6);
- _assertInvalidUnits(a, RESOLVED_UNIT7);
- _performPendingAnalysisTasks();
- // Validate "(p) =>" types.
- {
- CompilationUnit unit = context.getResult(targetA, RESOLVED_UNIT2);
- SimpleIdentifier parameterName =
- EngineTestCase.findSimpleIdentifier(unit, code, 'p) =>');
- expect(parameterName.staticType, context.typeProvider.dynamicType);
- expect(parameterName.propagatedType, context.typeProvider.intType);
- }
- }
-
- void test_sequence_compoundingResults_exportNamespace() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A<T> {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-export 'a.dart';
-''');
- Source c = addSource(
- '/c.dart',
- r'''
-import 'b.dart';
-main() {
- new A<int>();
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(c).errors, isEmpty);
- // Update a.dart, so that `A<T>` has a type bound.
- // This should invalidate export namespace for b.dart.
- // Currently we invalidate the whole LIBRARY_ELEMENT4.
- // So, c.dart will see the new `A<T extends B>` and report an error.
- context.setContents(
- a,
- r'''
-class A<T extends B> {}
-class B {}
-''');
- _assertInvalid(b, LIBRARY_ELEMENT4);
- // Analyze and validate that a new error is reported.
- _performPendingAnalysisTasks();
- expect(context.getErrors(c).errors, hasLength(1));
- _assertValid(a, LIBRARY_ERRORS_READY);
- _assertValid(b, LIBRARY_ERRORS_READY);
- _assertValid(c, LIBRARY_ERRORS_READY);
- }
-
- void test_sequence_compoundingResults_invalidateButKeepDependency() {
- Source a = addSource(
- '/a.dart',
- r'''
-import 'b.dart';
-class A {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-import 'c.dart';
-class B {}
-''');
- Source c = addSource(
- '/c.dart',
- r'''
-class C {}
-''');
- Source d = addSource(
- '/d.dart',
- r'''
-export 'b.dart';
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(c).errors, isEmpty);
- // Update: a.dart (limited) and b.dart (limited).
- // This should invalidate LIBRARY_ELEMENT4 in d.dart, but it should be
- // done in a way that keep dependency of other results od d.dart on
- // LIBRARY_ELEMENT4 of d.dart, so that when we perform unlimited
- // invalidation of c.dart, this makes d.dart invalid.
- context.setContents(
- a,
- r'''
-import 'b.dart';
-class A2 {}
-''');
- context.setContents(
- b,
- r'''
-import 'a.dart';
-import 'c.dart';
-class B2 {}
-''');
- context.setContents(
- c,
- r'''
-import 'dart:async';
-class C {}
-''');
- _assertInvalid(d, LIBRARY_ELEMENT4);
- _assertInvalid(d, LIBRARY_ERRORS_READY);
- // Analyze and validate that a new error is reported.
- _performPendingAnalysisTasks();
- _assertValid(a, EXPORT_SOURCE_CLOSURE);
- _assertValid(b, EXPORT_SOURCE_CLOSURE);
- _assertValid(c, EXPORT_SOURCE_CLOSURE);
- _assertValid(d, EXPORT_SOURCE_CLOSURE);
- _assertValid(a, LIBRARY_ERRORS_READY);
- _assertValid(b, LIBRARY_ERRORS_READY);
- _assertValid(c, LIBRARY_ERRORS_READY);
- _assertValid(d, LIBRARY_ERRORS_READY);
- }
-
- void test_sequence_compoundingResults_resolvedTypeNames() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-class B<T> {
- B(p);
-}
-''');
- Source c = addSource(
- '/c.dart',
- r'''
-export 'a.dart';
-export 'b.dart';
-''');
- Source d = addSource(
- '/d.dart',
- r'''
-import 'c.dart';
-main() {
- new B<int>(null);
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart and b.dart
- // This should invalidate most results in a.dart and b.dart
- //
- // This should also invalidate "compounding" results in c.dart, such as
- // READY_LIBRARY_ELEMENT6, which represent a state of the whole source
- // closure, not a result of this single unit or a library.
- //
- // The reason is that although type names (RESOLVED_UNIT5) b.dart will be
- // eventually resolved and set into b.dart elements, it may happen
- // after we attempted to re-resolve c.dart, which created Member(s), and
- // attempts to use elements without types set.
- context.setContents(
- a,
- r'''
-class A2 {}
-''');
- context.setContents(
- b,
- r'''
-class B<T> {
- B(T p);
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForChangedLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalid(c, READY_LIBRARY_ELEMENT6);
- _assertInvalid(c, READY_LIBRARY_ELEMENT7);
- // Analyze and validate that all results are valid.
- _performPendingAnalysisTasks();
- _assertValid(a, LIBRARY_ERRORS_READY);
- _assertValid(b, LIBRARY_ERRORS_READY);
- _assertValid(c, LIBRARY_ERRORS_READY);
- _assertValid(d, LIBRARY_ERRORS_READY);
- }
-
- void test_sequence_dependenciesWithCycles() {
- Source a = addSource(
- '/a.dart',
- r'''
-const A = 1;
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'c.dart';
-const B = C1 + 1;
-''');
- Source c = addSource(
- '/c.dart',
- r'''
-import 'a.dart';
-import 'b.dart';
-const C1 = A + 1;
-const C2 = B + 2;
-''');
- Source d = addSource(
- '/d.dart',
- r'''
-import 'c.dart';
-const D = C2 + 1;
-''');
- _performPendingAnalysisTasks();
- // Update "A" constant.
- // This should invalidate results in all sources.
- context.setContents(
- a,
- r'''
-const A = 2;
-''');
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalid(c, LIBRARY_ERRORS_READY);
- _assertInvalid(d, LIBRARY_ERRORS_READY);
- }
-
- void test_sequence_duplicateField_syntheticAndNot_renameNotSynthetic() {
- context.analysisOptions =
- new AnalysisOptionsImpl.from(context.analysisOptions)
- ..strongMode = true;
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- int foo;
- int get foo => 1;
-}
-class B extends A {
- int get foo => 2;
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(a).errors, hasLength(1));
- // Update a.dart: rename "int foo" to "int bar".
- context.setContents(
- a,
- r'''
-class A {
- int bar;
- int get foo => 1;
-}
-class B extends A {
- int get foo => 2;
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(a).errors, isEmpty);
- }
-
- void test_sequence_inBodyChange_addRef_deltaChange() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main(A a) {
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(0));
- // Update b.dart: in-body incremental change - start referencing 'foo'.
- // Should update referenced names.
- context.setContents(
- b,
- r'''
-import 'a.dart';
-main(A a) {
- a.foo;
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(1));
- // Update a.dart: add A.foo
- // b.dart is invalid, because it references 'foo'.
- context.setContents(
- a,
- r'''
-class A {
- int foo;
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- // No errors after analysis.
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(0));
- }
-
- void test_sequence_inBodyChange_removeRef_deltaChange() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main(A a) {
- a.foo;
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(1));
- // Update b.dart: in-body incremental change - stop referencing 'foo'.
- // Should update referenced names.
- context.setContents(
- b,
- r'''
-import 'a.dart';
-main(A a) {
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(b).errors, hasLength(0));
- // Update a.dart: add A.foo
- // b.dart is still valid, because it does references 'foo' anymore.
- context.setContents(
- a,
- r'''
-class A {
- int foo;
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_sequence_middleLimited_thenFirstFull() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-export 'a.dart';
-''');
- Source c = addSource(
- '/c.dart',
- r'''
-import 'b.dart';
-A a;
-''');
- _performPendingAnalysisTasks();
- // Update b.dart: limited invalidation.
- // Results in c.dart are valid, because the change in b.dart does not
- // affect anything in c.dart.
- context.setContents(
- b,
- r'''
-export 'a.dart';
-class B {}
-''');
- _assertValid(a, LIBRARY_ERRORS_READY);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertValid(c, LIBRARY_ERRORS_READY);
- _assertUnitValid(c, RESOLVED_UNIT4);
- _assertUnitValid(c, RESOLVED_UNIT5);
- // Update a.dart: unlimited invalidation.
- // Results RESOLVED_UNIT4, RESOLVED_UNIT5, and RESOLVED_UNIT6 in c.dart
- // are invalid, because after unlimited change to a.dart everything
- // should be invalidated.
- // This fixes the problem that c.dart was not re-resolving type names.
- context.setContents(
- a,
- r'''
-import 'dart:async';
-class A {}
-''');
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalid(c, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(c, RESOLVED_UNIT4);
- }
-
- void test_sequence_noChange_thenChange() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- A();
-}
-
-class B {
- B();
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- new A();
-}
-''');
- _performPendingAnalysisTasks();
- expect(context.getErrors(a).errors, hasLength(0));
- expect(context.getErrors(b).errors, hasLength(0));
- var unitA = context.getResolvedCompilationUnit2(a, a);
- var unitElementA = resolutionMap.elementDeclaredByCompilationUnit(unitA);
- var libraryElementA = unitElementA.library;
- // Update a.dart, no declaration changes.
- context.setContents(
- a,
- r'''
-class A {
- A();
-}
-class B {
- B();
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- // The a.dart's unit and element are updated incrementally.
- // They are the same instances as initially.
- // So, all the references from other units are still valid.
- {
- LibrarySpecificUnit target = new LibrarySpecificUnit(a, a);
- expect(analysisCache.getValue(target, RESOLVED_UNIT1), same(unitA));
- expect(unitA.element, same(unitElementA));
- expect(unitElementA.library, same(libraryElementA));
- }
- // Analyze.
- _performPendingAnalysisTasks();
- expect(context.getErrors(a).errors, hasLength(0));
- expect(context.getErrors(b).errors, hasLength(0));
- // The a.dart's unit and element are the same.
- {
- LibrarySpecificUnit target = new LibrarySpecificUnit(a, a);
- expect(analysisCache.getValue(target, RESOLVED_UNIT), same(unitA));
- expect(unitA.element, same(unitElementA));
- expect(unitElementA.library, same(libraryElementA));
- }
- // Add a new method to a.dart. This invalidates b.dart, so
- // we know that the previous update did not damage dependencies.
- context.setContents(
- a,
- r'''
-class A {
- A();
- m() {}
-}
-class B {
- B();
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllResolution(b);
- _assertValidAllErrors(b);
- // The a.dart's unit and element are the same.
- {
- LibrarySpecificUnit target = new LibrarySpecificUnit(a, a);
- expect(analysisCache.getValue(target, RESOLVED_UNIT1), same(unitA));
- expect(unitA.element, same(unitElementA));
- expect(unitElementA.library, same(libraryElementA));
- }
- // Analyze.
- _performPendingAnalysisTasks();
- expect(context.getErrors(a).errors, hasLength(0));
- expect(context.getErrors(b).errors, hasLength(0));
- }
-
- void test_sequence_parts_disableForPart() {
- Source a = addSource(
- '/a.dart',
- r'''
-library my_lib;
-part 'b.dart';
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-part of my_lib;
-class A {}
-''');
- _performPendingAnalysisTasks();
- // Update b.dart - it is a part, which we don't support.
- // So, invalidate everything.
- context.setContents(
- b,
- r'''
-part of my_lib;
-class A {}
-class B {}
-''');
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(a, RESOLVED_UNIT2);
- _assertInvalidUnits(b, RESOLVED_UNIT1);
- }
-
- void test_sequence_parts_disableWithPart() {
- Source a = addSource(
- '/a.dart',
- r'''
-library my_lib;
-part 'b.dart';
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-part of my_lib;
-class B {}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart - it is a library with a part, which we don't support.
- // So, invalidate everything.
- context.setContents(
- a,
- r'''
-library my_lib;
-part 'b.dart';
-class A {}
-''');
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(a, RESOLVED_UNIT1);
- _assertInvalidUnits(b, RESOLVED_UNIT1);
- }
-
- void test_sequence_reorder_localFunctions() {
- Source a = addSource(
- '/a.dart',
- r'''
-f1() {
- localFunction() {
- const C = 1;
- }
-}
-f2() {}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: reorder functions.
- // This should not fail with FrozenHashCodeException, because identifiers
- // of local elements should be relative to the enclosing top-level elements.
- context.setContents(
- a,
- r'''
-f2() {}
-f1() {
- localFunction() {
- const C = 1;
- }
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- }
-
- void test_sequence_unitConstants_addRemove() {
- Source a = addSource(
- '/a.dart',
- r'''
-const A = 1;
-const B = 2;
-const C = 3;
-foo() {
- const V1 = 10;
-}
-bar() {
- const V2 = 20;
-}
-''');
- _performPendingAnalysisTasks();
- LibrarySpecificUnit unitA = new LibrarySpecificUnit(a, a);
- List<ConstantEvaluationTarget> oldConstants =
- context.getResult(unitA, COMPILATION_UNIT_CONSTANTS);
- expect(oldConstants, hasLength(5));
- ConstVariableElement oldA = _findConstVariable(oldConstants, 'A');
- ConstVariableElement oldB = _findConstVariable(oldConstants, 'B');
- ConstVariableElement oldC = _findConstVariable(oldConstants, 'C');
- ConstVariableElement oldV1 = _findConstVariable(oldConstants, 'V1');
- ConstVariableElement oldV2 = _findConstVariable(oldConstants, 'V2');
- expect(context.analysisCache.get(oldA), isNotNull);
- expect(context.analysisCache.get(oldB), isNotNull);
- expect(context.analysisCache.get(oldC), isNotNull);
- expect(context.analysisCache.get(oldV1), isNotNull);
- // Update and validate new constants.
- context.setContents(
- a,
- r'''
-const A = 1;
-const B = 2;
-const D = 4;
-foo() {
- const V1 = 10;
-}
-baz() {
- const V3 = 30;
-}
-''');
- List<ConstantEvaluationTarget> newConstants =
- context.getResult(unitA, COMPILATION_UNIT_CONSTANTS);
- expect(newConstants, hasLength(5));
- expect(newConstants, contains(same(oldA)));
- expect(newConstants, contains(same(oldB)));
- expect(newConstants, contains(same(oldV1)));
- ConstVariableElement newD = _findConstVariable(newConstants, 'D');
- ConstVariableElement newV3 = _findConstVariable(newConstants, 'V3');
- // Perform analysis, compute constant values.
- _performPendingAnalysisTasks();
- // Validate const variable values.
- expect(context.analysisCache.get(oldA), isNotNull);
- expect(context.analysisCache.get(oldB), isNotNull);
- expect(context.analysisCache.get(oldV1), isNotNull);
- expect(context.analysisCache.get(oldC), isNull);
- expect(context.analysisCache.get(oldV2), isNull);
- expect(context.analysisCache.get(newD), isNotNull);
- expect(context.analysisCache.get(newV3), isNotNull);
- expect(oldA.evaluationResult.value.toIntValue(), 1);
- expect(oldB.evaluationResult.value.toIntValue(), 2);
- expect(newD.evaluationResult.value.toIntValue(), 4);
- expect(oldV1.evaluationResult.value.toIntValue(), 10);
- expect(newV3.evaluationResult.value.toIntValue(), 30);
- }
-
- void test_sequence_unitConstants_local_insertSpace() {
- Source a = addSource(
- '/a.dart',
- r'''
-main() {
- const C = 1;
-}
-''');
- _performPendingAnalysisTasks();
- LibrarySpecificUnit unitA = new LibrarySpecificUnit(a, a);
- List<ConstantEvaluationTarget> oldConstants =
- context.getResult(unitA, COMPILATION_UNIT_CONSTANTS);
- expect(oldConstants, hasLength(1));
- ConstVariableElement C = _findConstVariable(oldConstants, 'C');
- expect(context.analysisCache.get(C), isNotNull);
- // Insert a space before the name.
- context.setContents(
- a,
- r'''
-main() {
- const C = 1;
-}
-''');
- List<ConstantEvaluationTarget> newConstants =
- context.getResult(unitA, COMPILATION_UNIT_CONSTANTS);
- expect(newConstants, hasLength(1));
- expect(newConstants, contains(same(C)));
- // Perform analysis, compute constant values.
- _performPendingAnalysisTasks();
- // Validate const variable values.
- expect(context.analysisCache.get(C), isNotNull);
- expect(C.evaluationResult.value.toIntValue(), 1);
- }
-
- void test_sequence_useAnyResolvedUnit() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-class B {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- new A();
-}
-''');
- _performPendingAnalysisTasks();
- _assertValid(a, LIBRARY_ERRORS_READY);
- _assertValid(b, LIBRARY_ERRORS_READY);
- // Invalidate RESOLVED_UNIT
- CacheEntry entryA = context.getCacheEntry(new LibrarySpecificUnit(a, a));
- entryA.setState(RESOLVED_UNIT, CacheState.FLUSHED);
- entryA.setState(RESOLVED_UNIT1, CacheState.FLUSHED);
- entryA.setState(RESOLVED_UNIT2, CacheState.FLUSHED);
- entryA.setState(RESOLVED_UNIT3, CacheState.FLUSHED);
- context.setContents(
- a,
- r'''
-class A {}
-class B2 {}
-''');
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_sequence_useAnyResolvedUnit_needsLibraryElement() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-class B {}
-''');
- // Perform analysis until we get RESOLVED_UNIT1.
- // But it does not have 'library' set, so `unitElement.context` is `null`.
- LibrarySpecificUnit aUnitTarget = new LibrarySpecificUnit(a, a);
- while (context.getResult(aUnitTarget, RESOLVED_UNIT1) == null) {
- context.performAnalysisTask();
- }
- // There was a bug with exception in incremental element builder.
- // We should not attempt to use `unitElement.context`.
- // It calls `unitElement.library`, which might be not set yet.
- context.setContents(
- a,
- r'''
-class A {}
-class B2 {}
-''');
- // OK, no exceptions.
- }
-
- void test_unusedName_class_add() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-class B {}
-class C {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- new A();
- new C();
-}
-''');
- _performPendingAnalysisTasks();
- _assertValid(a, LINE_INFO);
- // The class B is not referenced.
- // a.dart is invalid.
- // b.dart is valid.
- context.setContents(
- a,
- r'''
-class A {}
-class B2 {}
-class C {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertUnitInvalid(a, RESOLVED_UNIT);
- _assertValidForDependentLibrary(b);
- _assertValidAllLibraryUnitResults(b);
- _assertValid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_usedName_class_name_asHole_inBody() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-class B {}
-class C {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- new A();
- new C2();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove C, add C2.
- // b.dart is invalid, because it references C2.
- context.setContents(
- a,
- r'''
-class A {}
-class B {}
-class C2 {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- }
-
- void test_usedName_class_name_asSuper() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-class B extends A {}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove A, add A2.
- // b.dart is invalid, because it references A.
- context.setContents(
- a,
- r'''
-class A2 {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- }
-
- void test_usedName_class_name_asTypeBound() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-class B<T extends A> {
- T f;
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove A, add A2.
- // b.dart is invalid, because it references A.
- context.setContents(
- a,
- r'''
-class A2 {}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- }
-
- void test_usedName_class_name_inBody() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {}
-class B {}
-class C {}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- new A();
- new C();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove C, add C2.
- // b.dart is invalid, because it references C.
- context.setContents(
- a,
- r'''
-class A {}
-class B {}
-class C2 {}
-''');
- _assertValidForChangedLibrary(a);
- _assertValidForDependentLibrary(b);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- }
-
- void test_usedName_classMethod_name_inBody() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- m() {}
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-main() {
- A a = new A();
- a.m();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove A.m, add A.m2.
- // b.dart is invalid, because it references 'm'.
- context.setContents(
- a,
- r'''
-class A {
- m2() {}
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalidLibraryElements(b, LIBRARY_ELEMENT4);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- }
-
- void test_usedName_indirect_classMethod_name_inBody() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- m() {}
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-class B extends A {}
-''');
- Source c = addSource(
- '/c.dart',
- r'''
-import 'b.dart';
-main() {
- B b = new B();
- b.m();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove A.m, add A.m2.
- // b.dart has valid resolution, and invalid errors.
- // c.dart is invalid, because 'main' references 'm'.
- context.setContents(
- a,
- r'''
-class A {
- m2() {}
-}
-''');
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
-
- _assertValidForDependentLibrary(b);
- _assertValidAllResolution(b);
- _assertInvalidHintsVerifyErrors(b);
-
- _assertValidForDependentLibrary(c);
- _assertInvalidLibraryElements(c, LIBRARY_ELEMENT5);
- _assertInvalidUnits(c, RESOLVED_UNIT4);
- _assertInvalid(c, LIBRARY_ERRORS_READY);
- }
-
- void test_usedName_indirect_classMethod_returnType_inBody() {
- Source a = addSource(
- '/a.dart',
- r'''
-class A {
- int m() {
- return 1;
- }
-}
-''');
- Source b = addSource(
- '/b.dart',
- r'''
-import 'a.dart';
-class B extends A {}
-''');
- Source c = addSource(
- '/c.dart',
- r'''
-import 'b.dart';
-main() {
- B b = new B();
- b.m();
-}
-''');
- _performPendingAnalysisTasks();
- // Update a.dart: remove A.m, add A.m2.
- // b.dart is invalid, because B extends A.
- // c.dart is invalid, because 'main' references 'm'.
- context.setContents(
- a,
- r'''
-class A {
- double m() {
- return 1.2;
- }
-}
-''');
- _assertInvalid(a, LIBRARY_ERRORS_READY);
-
- _assertValidForDependentLibrary(b);
- _assertValidAllResolution(b);
- _assertInvalidHintsVerifyErrors(b);
-
- _assertValidForDependentLibrary(c);
- _assertInvalidLibraryElements(c, LIBRARY_ELEMENT5);
- _assertInvalidUnits(c, RESOLVED_UNIT4);
- _assertInvalid(c, LIBRARY_ERRORS_READY);
- }
-
- void _assertInvalid(AnalysisTarget target, ResultDescriptor descriptor) {
- CacheState actual = analysisCache.getState(target, descriptor);
- if (actual != CacheState.INVALID) {
- fail("cache state of $target $descriptor: wanted INVALID, got: $actual");
- }
- }
-
- /**
- * Assert that [VERIFY_ERRORS] and other error results that include it,
- * are invalid.
- */
- void _assertInvalidHintsVerifyErrors(Source unit) {
- _assertUnitInvalid(unit, HINTS);
- _assertUnitInvalid(unit, VERIFY_ERRORS);
- _assertUnitInvalid(unit, LIBRARY_UNIT_ERRORS);
- _assertInvalid(unit, DART_ERRORS);
- _assertInvalid(unit, LIBRARY_ERRORS_READY);
- }
-
- /**
- * Assert that [LIBRARY_ELEMENT_RESULTS] for [first] and after it are invalid.
- */
- void _assertInvalidLibraryElements(
- Source source, ResultDescriptor<LibraryElement> first) {
- bool foundFirst = false;
- for (ResultDescriptor<LibraryElement> result in LIBRARY_ELEMENT_RESULTS) {
- foundFirst = foundFirst || result == first;
- if (foundFirst) {
- _assertInvalid(source, result);
- }
- }
- }
-
- void _assertInvalidUnits(Source unit, ResultDescriptor<CompilationUnit> first,
- {Source library}) {
- var target = new LibrarySpecificUnit(library ?? unit, unit);
- bool foundFirst = false;
- for (ResultDescriptor<CompilationUnit> result in RESOLVED_UNIT_RESULTS) {
- foundFirst = foundFirst || result == first;
- if (foundFirst) {
- _assertInvalid(target, result);
- }
- }
- }
-
- void _assertUnitInvalid(Source unitSource, ResultDescriptor descriptor,
- {Source librarySource}) {
- librarySource ??= unitSource;
- _assertInvalid(
- new LibrarySpecificUnit(librarySource, unitSource), descriptor);
- }
-
- void _assertUnitValid(Source unitSource, ResultDescriptor descriptor,
- {Source librarySource}) {
- librarySource ??= unitSource;
- _assertValid(
- new LibrarySpecificUnit(librarySource, unitSource), descriptor);
- }
-
- void _assertUnitValidTaskResults(Source unitSource, TaskDescriptor descriptor,
- {Source librarySource}) {
- librarySource ??= unitSource;
- for (ResultDescriptor result in descriptor.results) {
- _assertUnitValid(unitSource, result, librarySource: librarySource);
- }
- }
-
- void _assertValid(AnalysisTarget target, ResultDescriptor descriptor) {
- CacheState state = analysisCache.getState(target, descriptor);
- expect(state, isIn([CacheState.VALID, CacheState.FLUSHED]),
- reason: '$descriptor in $target');
- }
-
- /**
- * Assert that all error results for the given [unit] are valid.
- */
- void _assertValidAllErrors(Source unit) {
- for (ListResultDescriptor<AnalysisError> result in ERROR_SOURCE_RESULTS) {
- _assertValid(unit, result);
- }
- for (ListResultDescriptor<AnalysisError> result in ERROR_UNIT_RESULTS) {
- _assertUnitValid(unit, result);
- }
- }
-
- void _assertValidAllLibraryUnitResults(Source source, {Source library}) {
- library ??= source;
- for (ResultDescriptor<LibraryElement> result in LIBRARY_ELEMENT_RESULTS) {
- if (result == LIBRARY_ELEMENT4) {
- continue;
- }
- _assertValid(library, result);
- }
- LibrarySpecificUnit target = new LibrarySpecificUnit(library, source);
- for (ResultDescriptor<CompilationUnit> result in RESOLVED_UNIT_RESULTS) {
- _assertValid(target, result);
- }
- }
-
- void _assertValidAllResolution(Source unit) {
- _assertValidUnits(unit, null);
- _assertUnitValidTaskResults(unit, ResolveUnitTypeNamesTask.DESCRIPTOR);
- _assertUnitValidTaskResults(unit, ResolveUnitTask.DESCRIPTOR);
- _assertValidTaskResults(unit, ResolveLibraryReferencesTask.DESCRIPTOR);
- _assertValidTaskResults(unit, ResolveLibraryTask.DESCRIPTOR);
- }
-
- void _assertValidForAnyLibrary(Source source) {
- // Source results.
- _assertValidTaskResults(source, ScanDartTask.DESCRIPTOR);
- // Library results.
- _assertValidTaskResults(source, BuildLibraryElementTask.DESCRIPTOR);
- _assertValidTaskResults(source, BuildDirectiveElementsTask.DESCRIPTOR);
- _assertValidTaskResults(source, BuildSourceExportClosureTask.DESCRIPTOR);
- _assertValidTaskResults(source, ReadyLibraryElement2Task.DESCRIPTOR);
- _assertValidTaskResults(source, ComputeLibraryCycleTask.DESCRIPTOR);
- // Unit results.
- _assertUnitValidTaskResults(
- source, BuildCompilationUnitElementTask.DESCRIPTOR);
- _assertUnitValidTaskResults(
- source, ResolveDirectiveElementsTask.DESCRIPTOR);
- _assertUnitValidTaskResults(source, BuildEnumMemberElementsTask.DESCRIPTOR);
- }
-
- void _assertValidForChangedLibrary(Source source) {
- _assertValidForAnyLibrary(source);
- }
-
- void _assertValidForDependentLibrary(Source source) {
- _assertValidForAnyLibrary(source);
- // Library results.
- _assertValidTaskResults(source, BuildPublicNamespaceTask.DESCRIPTOR);
- }
-
- void _assertValidTaskResults(AnalysisTarget target, TaskDescriptor task) {
- for (ResultDescriptor result in task.results) {
- _assertValid(target, result);
- }
- }
-
- void _assertValidUnits(Source unit, ResultDescriptor<CompilationUnit> last,
- {Source library}) {
- var target = new LibrarySpecificUnit(library ?? unit, unit);
- bool foundLast = false;
- for (ResultDescriptor<CompilationUnit> result in RESOLVED_UNIT_RESULTS) {
- if (!foundLast) {
- _assertValid(target, result);
- }
- foundLast = foundLast || result == last;
- }
- }
-
- ConstVariableElement _findConstVariable(
- List<ConstantEvaluationTarget> constants, String name) {
- return constants.singleWhere((c) {
- return c is ConstVariableElement && c.name == name;
- });
- }
-
- void _performPendingAnalysisTasks([int maxTasks = 512]) {
- for (int i = 0; context.performAnalysisTask().hasMoreWork; i++) {
- if (i > maxTasks) {
- fail('Analysis did not terminate.');
- }
- }
- }
-
- void _verifyTwoLibrariesAllValid(
- String firstCodeA, String secondCodeA, String codeB) {
- Source a = addSource('/a.dart', firstCodeA);
- Source b = addSource('/b.dart', codeB);
- _performPendingAnalysisTasks();
- context.setContents(a, secondCodeA);
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllResolution(b);
- _assertValidAllErrors(b);
- }
-
- void _verifyTwoLibrariesInvalidatesResolution(
- String firstCodeA, String secondCodeA, String codeB) {
- Source a = addSource('/a.dart', firstCodeA);
- Source b = addSource('/b.dart', codeB);
- _performPendingAnalysisTasks();
- context.setContents(a, secondCodeA);
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertInvalid(b, LIBRARY_ERRORS_READY);
- _assertInvalidUnits(b, RESOLVED_UNIT4);
- }
-
- void _verifyTwoLibrariesInvalidHintsVerifyErrors(
- String firstCodeA, String secondCodeA, String codeB) {
- Source a = addSource('/a.dart', firstCodeA);
- Source b = addSource('/b.dart', codeB);
- _performPendingAnalysisTasks();
- context.setContents(a, secondCodeA);
- _assertValidForChangedLibrary(a);
- _assertInvalid(a, LIBRARY_ERRORS_READY);
- _assertValidForDependentLibrary(b);
- _assertValidAllResolution(b);
- _assertUnitValid(b, RESOLVE_UNIT_ERRORS);
- _assertInvalidHintsVerifyErrors(b);
- }
-}
-
class _AnalysisContextImplTest_Source_exists_true extends TestSource {
@override
bool exists() => true;
« no previous file with comments | « pkg/analyzer/test/src/context/builder_test.dart ('k') | pkg/analyzer/test/src/task/incremental_element_builder_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698