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

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

Issue 1193143002: Next steps toward HTML support (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address comments Created 5 years, 6 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
« no previous file with comments | « pkg/analyzer/lib/src/task/html.dart ('k') | pkg/analyzer/test/src/task/dart_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 5230b3b0e34e4c4585fe25d1f51aa366cf875cc2..e6ecc6c12f99b7b677d8acc00f33d5bf54421a56 100644
--- a/pkg/analyzer/test/src/context/context_test.dart
+++ b/pkg/analyzer/test/src/context/context_test.dart
@@ -35,6 +35,7 @@ import 'package:analyzer/src/generated/resolver.dart';
import 'package:analyzer/src/generated/scanner.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/task/dart.dart';
+import 'package:html/dom.dart' show Document;
import 'package:path/path.dart' as pathos;
import 'package:unittest/unittest.dart';
import 'package:watcher/src/utils.dart';
@@ -43,7 +44,6 @@ import '../../generated/engine_test.dart';
import '../../generated/test_support.dart';
import '../../reflective_tests.dart';
import 'abstract_context.dart';
-import 'package:html/dom.dart' show Document;
main() {
groupSep = ' | ';
@@ -61,600 +61,63 @@ class AnalysisContextImplTest extends AbstractContextTest {
fail('Should have failed');
}
- void test_applyChanges_overriddenSource() {
- // Note: addSource adds the source to the contentCache.
- Source source = addSource("/test.dart", "library test;");
- context.computeErrors(source);
- while (!context.sourcesNeedingProcessing.isEmpty) {
- context.performAnalysisTask();
- }
- // Adding the source as a changedSource should have no effect since
- // it is already overridden in the content cache.
- ChangeSet changeSet = new ChangeSet();
- changeSet.changedSource(source);
- context.applyChanges(changeSet);
- expect(context.sourcesNeedingProcessing, hasLength(0));
- }
-
- Future test_applyChanges_remove() {
- SourcesChangedListener listener = new SourcesChangedListener();
- context.onSourcesChanged.listen(listener.onData);
- String libAContents = r'''
-library libA;
-import 'libB.dart';''';
- Source libA = addSource("/libA.dart", libAContents);
- String libBContents = "library libB;";
- Source libB = addSource("/libB.dart", libBContents);
- LibraryElement libAElement = context.computeLibraryElement(libA);
- expect(libAElement, isNotNull);
- List<LibraryElement> importedLibraries = libAElement.importedLibraries;
- expect(importedLibraries, hasLength(2));
- context.computeErrors(libA);
- context.computeErrors(libB);
- expect(context.sourcesNeedingProcessing, hasLength(0));
- context.setContents(libB, null);
- _removeSource(libB);
- List<Source> sources = context.sourcesNeedingProcessing;
- expect(sources, hasLength(1));
- expect(sources[0], same(libA));
- libAElement = context.computeLibraryElement(libA);
- importedLibraries = libAElement.importedLibraries;
- expect(importedLibraries, hasLength(1));
- return pumpEventQueue().then((_) {
- listener.assertEvent(wereSourcesAdded: true);
- listener.assertEvent(wereSourcesAdded: true);
- listener.assertEvent(wereSourcesRemovedOrDeleted: true);
- listener.assertNoMoreEvents();
- });
- }
-
- Future test_applyChanges_removeContainer() {
- SourcesChangedListener listener = new SourcesChangedListener();
- context.onSourcesChanged.listen(listener.onData);
- String libAContents = r'''
-library libA;
-import 'libB.dart';''';
- Source libA = addSource("/libA.dart", libAContents);
- String libBContents = "library libB;";
- Source libB = addSource("/libB.dart", libBContents);
- context.computeLibraryElement(libA);
- context.computeErrors(libA);
- context.computeErrors(libB);
- expect(context.sourcesNeedingProcessing, hasLength(0));
- ChangeSet changeSet = new ChangeSet();
- SourceContainer removedContainer =
- new _AnalysisContextImplTest_test_applyChanges_removeContainer(libB);
- changeSet.removedContainer(removedContainer);
- context.applyChanges(changeSet);
- List<Source> sources = context.sourcesNeedingProcessing;
- expect(sources, hasLength(1));
- expect(sources[0], same(libA));
- return pumpEventQueue().then((_) {
- listener.assertEvent(wereSourcesAdded: true);
- listener.assertEvent(wereSourcesAdded: true);
- listener.assertEvent(wereSourcesRemovedOrDeleted: true);
- listener.assertNoMoreEvents();
- });
- }
-
- void test_computeErrors_dart_none() {
- Source source = addSource("/lib.dart", "library lib;");
- List<AnalysisError> errors = context.computeErrors(source);
- expect(errors, hasLength(0));
- }
-
- void test_computeErrors_dart_part() {
- Source librarySource =
- addSource("/lib.dart", "library lib; part 'part.dart';");
- Source partSource = addSource("/part.dart", "part of 'lib';");
- context.parseCompilationUnit(librarySource);
- List<AnalysisError> errors = context.computeErrors(partSource);
- expect(errors, isNotNull);
- expect(errors.length > 0, isTrue);
- }
-
- void test_computeErrors_dart_some() {
- Source source = addSource("/lib.dart", "library 'lib';");
- List<AnalysisError> errors = context.computeErrors(source);
- expect(errors, isNotNull);
- expect(errors.length > 0, isTrue);
- }
-
- void test_computeErrors_html_none() {
- Source source = addSource("/test.html", "<!DOCTYPE html><html></html>");
- List<AnalysisError> errors = context.computeErrors(source);
- expect(errors, hasLength(0));
- }
-
void fail_computeHtmlElement_valid() {
- Source source = addSource("/test.html", "<html></html>");
- HtmlElement element = context.computeHtmlElement(source);
- expect(element, isNotNull);
- expect(context.computeHtmlElement(source), same(element));
- }
-
- void test_computeImportedLibraries_none() {
- Source source = addSource("/test.dart", "library test;");
- expect(context.computeImportedLibraries(source), hasLength(0));
- }
-
- void test_computeImportedLibraries_some() {
- Source source = addSource(
- "/test.dart", "library test; import 'lib1.dart'; import 'lib2.dart';");
- expect(context.computeImportedLibraries(source), hasLength(2));
- }
-
- void fail_computeResolvableCompilationUnit_dart_exception() {
- TestSource source = _addSourceWithException("/test.dart");
- try {
- context.computeResolvableCompilationUnit(source);
- fail("Expected AnalysisException");
- } on AnalysisException {
- // Expected
- }
- }
-
- void fail_computeResolvableCompilationUnit_html_exception() {
- Source source = addSource("/lib.html", "<html></html>");
- try {
- context.computeResolvableCompilationUnit(source);
- fail("Expected AnalysisException");
- } on AnalysisException {
- // Expected
- }
- }
-
- void fail_computeResolvableCompilationUnit_valid() {
- Source source = addSource("/lib.dart", "library lib;");
- CompilationUnit parsedUnit = context.parseCompilationUnit(source);
- expect(parsedUnit, isNotNull);
- CompilationUnit resolvedUnit =
- context.computeResolvableCompilationUnit(source);
- expect(resolvedUnit, isNotNull);
- expect(resolvedUnit, same(parsedUnit));
- }
-
- Future test_computeResolvedCompilationUnitAsync_dispose() {
- Source source = addSource("/lib.dart", "library lib;");
- // Complete all pending analysis tasks and flush the AST so that it won't
- // be available immediately.
- _performPendingAnalysisTasks();
- _flushAst(source);
- bool completed = false;
- CancelableFuture<CompilationUnit> future =
- context.computeResolvedCompilationUnitAsync(source, source);
- future.then((CompilationUnit unit) {
- fail('Future should have completed with error');
- }, onError: (error) {
- expect(error, new isInstanceOf<AnalysisNotScheduledError>());
- completed = true;
- });
- expect(completed, isFalse);
- expect(context.pendingFutureSources_forTesting, isNotEmpty);
- // Disposing of the context should cause all pending futures to complete
- // with AnalysisNotScheduled, so that no clients are left hanging.
- context.dispose();
- expect(context.pendingFutureSources_forTesting, isEmpty);
- return pumpEventQueue().then((_) {
- expect(completed, isTrue);
- expect(context.pendingFutureSources_forTesting, isEmpty);
- });
- }
-
- Future test_computeResolvedCompilationUnitAsync_noCacheEntry() {
- Source librarySource = addSource("/lib.dart", "library lib;");
- Source partSource = addSource("/part.dart", "part of foo;");
- bool completed = false;
- context
- .computeResolvedCompilationUnitAsync(partSource, librarySource)
- .then((CompilationUnit unit) {
- expect(unit, isNotNull);
- completed = true;
- });
- return pumpEventQueue().then((_) {
- expect(completed, isFalse);
- _performPendingAnalysisTasks();
- }).then((_) => pumpEventQueue()).then((_) {
- expect(completed, isTrue);
- });
- }
-
- void fail_extractContext() {
- fail("Implement this");
- }
-
- void test_getElement_constructor_named() {
- Source source = addSource("/lib.dart", r'''
-class A {
- A.named() {}
-}''');
- _analyzeAll_assertFinished();
- LibraryElement library = context.computeLibraryElement(source);
- ClassElement classA = _findClass(library.definingCompilationUnit, "A");
- ConstructorElement constructor = classA.constructors[0];
- ElementLocation location = constructor.location;
- Element element = context.getElement(location);
- expect(element, same(constructor));
- }
-
- void test_getElement_constructor_unnamed() {
- Source source = addSource("/lib.dart", r'''
-class A {
- A() {}
-}''');
- _analyzeAll_assertFinished();
- LibraryElement library = context.computeLibraryElement(source);
- ClassElement classA = _findClass(library.definingCompilationUnit, "A");
- ConstructorElement constructor = classA.constructors[0];
- ElementLocation location = constructor.location;
- Element element = context.getElement(location);
- expect(element, same(constructor));
- }
-
- void test_getElement_enum() {
- Source source = addSource('/test.dart', 'enum MyEnum {A, B, C}');
- _analyzeAll_assertFinished();
- LibraryElement library = context.computeLibraryElement(source);
- ClassElement myEnum = library.definingCompilationUnit.getEnum('MyEnum');
- ElementLocation location = myEnum.location;
- Element element = context.getElement(location);
- expect(element, same(myEnum));
- }
-
- void test_getErrors_dart_none() {
- Source source = addSource("/lib.dart", "library lib;");
- var errorInfo = context.getErrors(source);
- expect(errorInfo, isNotNull);
- List<AnalysisError> errors = errorInfo.errors;
- expect(errors, hasLength(0));
- context.computeErrors(source);
- errors = errorInfo.errors;
- expect(errors, hasLength(0));
- }
-
- void test_getErrors_dart_some() {
- Source source = addSource("/lib.dart", "library 'lib';");
- var errorInfo = context.getErrors(source);
- expect(errorInfo, isNotNull);
- List<AnalysisError> errors = errorInfo.errors;
- expect(errors, hasLength(0));
- errors = context.computeErrors(source);
- expect(errors, hasLength(1));
- }
-
- void test_getErrors_html_none() {
- Source source = addSource("/test.html", "<html></html>");
- AnalysisErrorInfo errorInfo = context.getErrors(source);
- expect(errorInfo, isNotNull);
- List<AnalysisError> errors = errorInfo.errors;
- expect(errors, hasLength(0));
- context.computeErrors(source);
- errors = errorInfo.errors;
- expect(errors, hasLength(0));
- }
-
- void test_getErrors_html_some() {
- Source source = addSource("/test.html", r'''
-<html><head>
-<script type='application/dart' src='test.dart'/>
-</head></html>''');
- AnalysisErrorInfo errorInfo = context.getErrors(source);
- expect(errorInfo, isNotNull);
- List<AnalysisError> errors = errorInfo.errors;
- expect(errors, hasLength(0));
- errors = context.computeErrors(source);
- expect(errors, hasLength(3));
- }
-
- void fail_getHtmlElement_html() {
- Source source = addSource("/test.html", "<html></html>");
- HtmlElement element = context.getHtmlElement(source);
- expect(element, isNull);
- context.computeHtmlElement(source);
- element = context.getHtmlElement(source);
- expect(element, isNotNull);
- }
-
- void test_getHtmlFilesReferencing_library() {
- Source htmlSource = addSource("/test.html", r'''
-<!DOCTYPE html>
-<html><head>
-<script type='application/dart' src='test.dart'/>
-<script type='application/dart' src='test.js'/>
-</head></html>''');
- Source librarySource = addSource("/test.dart", "library lib;");
- context.computeLibraryElement(librarySource);
- List<Source> result = context.getHtmlFilesReferencing(librarySource);
- expect(result, hasLength(0));
- context.computeHtmlElement(htmlSource);
- result = context.getHtmlFilesReferencing(librarySource);
- expect(result, hasLength(1));
- expect(result[0], htmlSource);
- }
-
- void test_getHtmlFilesReferencing_part() {
- Source htmlSource = addSource("/test.html", r'''
-<!DOCTYPE html>
-<html><head>
-<script type='application/dart' src='test.dart'/>
-<script type='application/dart' src='test.js'/>
-</head></html>''');
- Source librarySource =
- addSource("/test.dart", "library lib; part 'part.dart';");
- Source partSource = addSource("/part.dart", "part of lib;");
- context.computeLibraryElement(librarySource);
- List<Source> result = context.getHtmlFilesReferencing(partSource);
- expect(result, hasLength(0));
- context.computeHtmlElement(htmlSource);
- result = context.getHtmlFilesReferencing(partSource);
- expect(result, hasLength(1));
- expect(result[0], htmlSource);
- }
-
- void test_getHtmlSources() {
- List<Source> sources = context.htmlSources;
- expect(sources, hasLength(0));
- Source source = addSource("/test.html", "");
- sources = context.htmlSources;
- expect(sources, hasLength(1));
- expect(sources[0], source);
- }
-
- void test_getLibrariesReferencedFromHtml() {
- Source htmlSource = addSource("/test.html", r'''
-<!DOCTYPE html>
-<html><head>
-<script type='application/dart' src='test.dart'/>
-<script type='application/dart' src='test.js'/>
-</head></html>''');
- Source librarySource = addSource("/test.dart", "library lib;");
- context.computeLibraryElement(librarySource);
- context.computeHtmlElement(htmlSource);
- List<Source> result = context.getLibrariesReferencedFromHtml(htmlSource);
- expect(result, hasLength(1));
- expect(result[0], librarySource);
- }
-
- void test_getResolvedCompilationUnit_library() {
- Source source = addSource("/lib.dart", "library libb;");
- LibraryElement library = context.computeLibraryElement(source);
- context.computeErrors(source); // Force the resolved unit to be built.
- expect(context.getResolvedCompilationUnit(source, library), isNotNull);
- context.setContents(source, "library lib;");
- expect(context.getResolvedCompilationUnit(source, library), isNull);
- }
-
- void fail_getResolvedHtmlUnit() {
- Source source = addSource("/test.html", "<html></html>");
- expect(context.getResolvedHtmlUnit(source), isNull);
- context.resolveHtmlUnit(source);
- expect(context.getResolvedHtmlUnit(source), isNotNull);
- }
-
- void fail_mergeContext() {
- fail("Implement this");
- }
-
- void test_parseDocument() {
- Source source = addSource("/lib.html", "<!DOCTYPE html><html></html>");
- Document unit = context.parseHtmlDocument(source);
- expect(unit, isNotNull);
- }
-
- void fail_parseHtmlUnit_noErrors() {
- Source source = addSource("/lib.html", "<html></html>");
- ht.HtmlUnit unit = context.parseHtmlUnit(source);
- expect(unit, isNotNull);
- }
-
- void fail_parseHtmlUnit_resolveDirectives() {
- Source libSource = addSource("/lib.dart", r'''
-library lib;
-class ClassA {}''');
- Source source = addSource("/lib.html", r'''
-<!DOCTYPE html>
-<html>
-<head>
- <script type='application/dart'>
- import 'lib.dart';
- ClassA v = null;
- </script>
-</head>
-<body>
-</body>
-</html>''');
- ht.HtmlUnit unit = context.parseHtmlUnit(source);
- expect(unit, isNotNull);
- // import directive should be resolved
- ht.XmlTagNode htmlNode = unit.tagNodes[0];
- ht.XmlTagNode headNode = htmlNode.tagNodes[0];
- ht.HtmlScriptTagNode scriptNode = headNode.tagNodes[0];
- CompilationUnit script = scriptNode.script;
- ImportDirective importNode = script.directives[0] as ImportDirective;
- expect(importNode.uriContent, isNotNull);
- expect(importNode.source, libSource);
- }
-
- void test_performAnalysisTask_addPart() {
- Source libSource = addSource("/lib.dart", r'''
-library lib;
-part 'part.dart';''');
- // run all tasks without part
- _analyzeAll_assertFinished();
- expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libSource)),
- isTrue, reason: "lib has errors");
- // add part and run all tasks
- Source partSource = addSource("/part.dart", r'''
-part of lib;
-''');
- _analyzeAll_assertFinished();
- // "libSource" should be here
- List<Source> librariesWithPart = context.getLibrariesContaining(partSource);
- expect(librariesWithPart, unorderedEquals([libSource]));
- expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libSource)),
- isFalse, reason: "lib doesn't have errors");
- expect(
- context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
- reason: "part resolved");
- }
-
- void test_performAnalysisTask_changeLibraryContents() {
- Source libSource =
- addSource("/test.dart", "library lib; part 'test-part.dart';");
- Source partSource = addSource("/test-part.dart", "part of lib;");
- _analyzeAll_assertFinished();
- expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
- reason: "library resolved 1");
- expect(
- context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
- reason: "part resolved 1");
- // update and analyze #1
- context.setContents(libSource, "library lib;");
- 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), isNull,
- reason: "part resolved 2");
- // update and analyze #2
- context.setContents(libSource, "library lib; part 'test-part.dart';");
- 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 2");
- expect(
- context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
- reason: "part resolved 3");
- }
-
- void test_performAnalysisTask_changeLibraryThenPartContents() {
- Source libSource =
- addSource("/test.dart", "library lib; part 'test-part.dart';");
- Source partSource = addSource("/test-part.dart", "part of lib;");
- _analyzeAll_assertFinished();
- expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
- reason: "library resolved 1");
- expect(
- context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
- reason: "part resolved 1");
- // update and analyze #1
- context.setContents(libSource, "library lib;");
- 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), isNull,
- reason: "part resolved 2");
- // update and analyze #2
- context.setContents(partSource, "part of lib; // 1");
- // Assert that changing the part's content does not effect the library
- // now that it is no longer part of that library
- expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
- 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), isNull,
- reason: "part resolved 3");
- }
-
- void test_performAnalysisTask_changePartContents_makeItAPart() {
- Source libSource = addSource("/lib.dart", r'''
-library lib;
-part 'part.dart';
-void f(x) {}''');
- Source partSource = addSource("/part.dart", "void g() { f(null); }");
- _analyzeAll_assertFinished();
- expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
- reason: "library resolved 1");
- expect(
- context.getResolvedCompilationUnit2(partSource, partSource), isNotNull,
- reason: "part resolved 1");
- // update and analyze
- context.setContents(partSource, r'''
-part of lib;
-void g() { f(null); }''');
- expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull,
- reason: "library changed 2");
- expect(context.getResolvedCompilationUnit2(partSource, partSource), 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.getErrors(libSource).errors, hasLength(0));
- expect(context.getErrors(partSource).errors, hasLength(0));
- }
-
- /**
- * https://code.google.com/p/dart/issues/detail?id=12424
- */
- void test_performAnalysisTask_changePartContents_makeItNotPart() {
- Source libSource = addSource("/lib.dart", r'''
-library lib;
-part 'part.dart';
-void f(x) {}''');
- Source partSource = addSource("/part.dart", r'''
-part of lib;
-void g() { f(null); }''');
- _analyzeAll_assertFinished();
- expect(context.getErrors(libSource).errors, hasLength(0));
- expect(context.getErrors(partSource).errors, hasLength(0));
- // Remove 'part' directive, which should make "f(null)" an error.
- context.setContents(partSource, r'''
-//part of lib;
-void g() { f(null); }''');
- _analyzeAll_assertFinished();
- expect(context.getErrors(libSource).errors.length != 0, isTrue);
- }
-
- void test_performAnalysisTask_changePartContents_noSemanticChanges() {
- Source libSource =
- addSource("/test.dart", "library lib; part 'test-part.dart';");
- Source partSource = addSource("/test-part.dart", "part of lib;");
- _analyzeAll_assertFinished();
- expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
- reason: "library resolved 1");
- expect(
- context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
- reason: "part resolved 1");
- // update and analyze #1
- context.setContents(partSource, "part of lib; // 1");
- 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");
- 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");
+ Source source = addSource("/test.html", "<html></html>");
+ HtmlElement element = context.computeHtmlElement(source);
+ expect(element, isNotNull);
+ expect(context.computeHtmlElement(source), same(element));
+ }
+
+ void fail_extractContext() {
+ fail("Implement this");
+ }
+
+ void fail_getHtmlElement_html() {
+ Source source = addSource("/test.html", "<html></html>");
+ HtmlElement element = context.getHtmlElement(source);
+ expect(element, isNull);
+ context.computeHtmlElement(source);
+ element = context.getHtmlElement(source);
+ expect(element, isNotNull);
+ }
+
+ void fail_getResolvedHtmlUnit() {
+ Source source = addSource("/test.html", "<html></html>");
+ expect(context.getResolvedHtmlUnit(source), isNull);
+ context.resolveHtmlUnit(source);
+ expect(context.getResolvedHtmlUnit(source), isNotNull);
+ }
+
+ void fail_mergeContext() {
+ fail("Implement this");
+ }
+
+ void fail_parseHtmlUnit_resolveDirectives() {
+ Source libSource = addSource("/lib.dart", r'''
+library lib;
+class ClassA {}''');
+ Source source = addSource("/lib.html", r'''
+<!DOCTYPE html>
+<html>
+<head>
+ <script type='application/dart'>
+ import 'lib.dart';
+ ClassA v = null;
+ </script>
+</head>
+<body>
+</body>
+</html>''');
+ ht.HtmlUnit unit = context.parseHtmlUnit(source);
+ expect(unit, isNotNull);
+ // import directive should be resolved
+ ht.XmlTagNode htmlNode = unit.tagNodes[0];
+ ht.XmlTagNode headNode = htmlNode.tagNodes[0];
+ ht.HtmlScriptTagNode scriptNode = headNode.tagNodes[0];
+ CompilationUnit script = scriptNode.script;
+ ImportDirective importNode = script.directives[0] as ImportDirective;
+ expect(importNode.uriContent, isNotNull);
+ expect(importNode.source, libSource);
}
void fail_performAnalysisTask_getContentException_dart() {
@@ -693,28 +156,6 @@ void g() { f(null); }''');
expect(error.errorCode, ScannerErrorCode.UNABLE_GET_CONTENT);
}
- void test_performAnalysisTask_importedLibraryAdd() {
- Source libASource =
- addSource("/libA.dart", "library libA; import 'libB.dart';");
- _analyzeAll_assertFinished();
- expect(
- context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
- reason: "libA resolved 1");
- expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)),
- isTrue, reason: "libA has an error");
- // add libB.dart and analyze
- Source libBSource = addSource("/libB.dart", "library libB;");
- _analyzeAll_assertFinished();
- expect(
- context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
- reason: "libA resolved 2");
- expect(
- context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull,
- reason: "libB resolved 2");
- expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)),
- isFalse, reason: "libA doesn't have errors");
- }
-
void fail_performAnalysisTask_importedLibraryAdd_html() {
Source htmlSource = addSource("/page.html", r'''
<html><body><script type="application/dart">
@@ -742,29 +183,6 @@ void g() { f(null); }''');
// reason: "htmlSource doesn't have errors");
}
- void test_performAnalysisTask_importedLibraryDelete() {
- Source libASource =
- addSource("/libA.dart", "library libA; import 'libB.dart';");
- Source libBSource = addSource("/libB.dart", "library libB;");
- _analyzeAll_assertFinished();
- expect(
- context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
- reason: "libA resolved 1");
- expect(
- context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull,
- reason: "libB resolved 1");
- expect(!_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)),
- isTrue, reason: "libA doesn't have errors");
- // remove libB.dart and analyze
- _removeSource(libBSource);
- _analyzeAll_assertFinished();
- expect(
- context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
- reason: "libA resolved 2");
- expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)),
- isTrue, reason: "libA has an error");
- }
-
void fail_performAnalysisTask_importedLibraryDelete_html() {
// NOTE: This was failing before converting to the new task model.
Source htmlSource = addSource("/page.html", r'''
@@ -791,36 +209,6 @@ void g() { f(null); }''');
reason: "htmlSource has an error");
}
- void test_performAnalysisTask_onResultComputed() {
- Set<String> libraryElementUris = new Set<String>();
- Set<String> parsedUnitUris = new Set<String>();
- Set<String> resolvedUnitUris = new Set<String>();
- // listen
- context.onResultComputed(LIBRARY_ELEMENT).listen((event) {
- Source librarySource = event.target;
- libraryElementUris.add(librarySource.uri.toString());
- });
- context.onResultComputed(PARSED_UNIT).listen((event) {
- Source source = event.target;
- parsedUnitUris.add(source.uri.toString());
- });
- context.onResultComputed(RESOLVED_UNIT).listen((event) {
- LibrarySpecificUnit target = event.target;
- Source librarySource = target.library;
- resolvedUnitUris.add(librarySource.uri.toString());
- });
- // analyze
- addSource('/test.dart', 'main() {}');
- _analyzeAll_assertFinished();
- // verify
- expect(libraryElementUris, contains('dart:core'));
- expect(libraryElementUris, contains('file:///test.dart'));
- expect(parsedUnitUris, contains('dart:core'));
- expect(parsedUnitUris, contains('file:///test.dart'));
- expect(resolvedUnitUris, contains('dart:core'));
- expect(resolvedUnitUris, contains('file:///test.dart'));
- }
-
void fail_performAnalysisTask_IOException() {
TestSource source = _addSourceWithException2("/test.dart", "library test;");
int oldTimestamp = context.getModificationStamp(source);
@@ -837,58 +225,10 @@ void g() { f(null); }''');
expect(source.readCount, 2);
}
- void test_performAnalysisTask_missingPart() {
- Source source =
- addSource("/test.dart", "library lib; part 'no-such-file.dart';");
- _analyzeAll_assertFinished();
- expect(context.getLibraryElement(source), isNotNull,
- reason: "performAnalysisTask failed to compute an element model");
- }
-
void fail_recordLibraryElements() {
fail("Implement this");
}
- void test_resolveCompilationUnit_import_relative() {
- Source sourceA =
- addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}");
- addSource("/libB.dart", "library libB; class B{}");
- CompilationUnit compilationUnit =
- context.resolveCompilationUnit2(sourceA, sourceA);
- expect(compilationUnit, isNotNull);
- LibraryElement library = compilationUnit.element.library;
- List<LibraryElement> importedLibraries = library.importedLibraries;
- assertNamedElements(importedLibraries, ["dart.core", "libB"]);
- List<LibraryElement> visibleLibraries = library.visibleLibraries;
- assertNamedElements(visibleLibraries, [
- "dart.core",
- "dart.async",
- "dart.math",
- "libA",
- "libB"
- ]);
- }
-
- void test_resolveCompilationUnit_import_relative_cyclic() {
- Source sourceA =
- addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}");
- addSource("/libB.dart", "library libB; import 'libA.dart'; class B{}");
- CompilationUnit compilationUnit =
- context.resolveCompilationUnit2(sourceA, sourceA);
- expect(compilationUnit, isNotNull);
- LibraryElement library = compilationUnit.element.library;
- List<LibraryElement> importedLibraries = library.importedLibraries;
- assertNamedElements(importedLibraries, ["dart.core", "libB"]);
- List<LibraryElement> visibleLibraries = library.visibleLibraries;
- assertNamedElements(visibleLibraries, [
- "dart.core",
- "dart.async",
- "dart.math",
- "libA",
- "libB"
- ]);
- }
-
void fail_resolveHtmlUnit() {
Source source = addSource("/lib.html", "<html></html>");
ht.HtmlUnit unit = context.resolveHtmlUnit(source);
@@ -963,24 +303,6 @@ int ya = 0;''';
});
}
- void test_setContents_unchanged_consistentModificationTime() {
- String contents = "// foo";
- Source source = addSource("/test.dart", contents);
- context.setContents(source, contents);
- // do all, no tasks
- _analyzeAll_assertFinished();
- {
- AnalysisResult result = context.performAnalysisTask();
- expect(result.changeNotices, isNull);
- }
- // set the same contents, still no tasks
- context.setContents(source, contents);
- {
- AnalysisResult result = context.performAnalysisTask();
- expect(result.changeNotices, isNull);
- }
- }
-
void fail_unreadableSource() {
Source test1 = addSource("/test1.dart", r'''
import 'test2.dart';
@@ -1136,7 +458,83 @@ int b = aa;''';
context.applyChanges(changeSet2);
return pumpEventQueue().then((_) {
listener.assertEvent(wereSourcesAdded: true);
- listener.assertEvent(wereSourcesAdded: true, changedSources: [source]);
+ listener.assertEvent(wereSourcesAdded: true, changedSources: [source]);
+ listener.assertNoMoreEvents();
+ });
+ }
+
+ void test_applyChanges_overriddenSource() {
+ // Note: addSource adds the source to the contentCache.
+ Source source = addSource("/test.dart", "library test;");
+ context.computeErrors(source);
+ while (!context.sourcesNeedingProcessing.isEmpty) {
+ context.performAnalysisTask();
+ }
+ // Adding the source as a changedSource should have no effect since
+ // it is already overridden in the content cache.
+ ChangeSet changeSet = new ChangeSet();
+ changeSet.changedSource(source);
+ context.applyChanges(changeSet);
+ expect(context.sourcesNeedingProcessing, hasLength(0));
+ }
+
+ Future test_applyChanges_remove() {
+ SourcesChangedListener listener = new SourcesChangedListener();
+ context.onSourcesChanged.listen(listener.onData);
+ String libAContents = r'''
+library libA;
+import 'libB.dart';''';
+ Source libA = addSource("/libA.dart", libAContents);
+ String libBContents = "library libB;";
+ Source libB = addSource("/libB.dart", libBContents);
+ LibraryElement libAElement = context.computeLibraryElement(libA);
+ expect(libAElement, isNotNull);
+ List<LibraryElement> importedLibraries = libAElement.importedLibraries;
+ expect(importedLibraries, hasLength(2));
+ context.computeErrors(libA);
+ context.computeErrors(libB);
+ expect(context.sourcesNeedingProcessing, hasLength(0));
+ context.setContents(libB, null);
+ _removeSource(libB);
+ List<Source> sources = context.sourcesNeedingProcessing;
+ expect(sources, hasLength(1));
+ expect(sources[0], same(libA));
+ libAElement = context.computeLibraryElement(libA);
+ importedLibraries = libAElement.importedLibraries;
+ expect(importedLibraries, hasLength(1));
+ return pumpEventQueue().then((_) {
+ listener.assertEvent(wereSourcesAdded: true);
+ listener.assertEvent(wereSourcesAdded: true);
+ listener.assertEvent(wereSourcesRemovedOrDeleted: true);
+ listener.assertNoMoreEvents();
+ });
+ }
+
+ Future test_applyChanges_removeContainer() {
+ SourcesChangedListener listener = new SourcesChangedListener();
+ context.onSourcesChanged.listen(listener.onData);
+ String libAContents = r'''
+library libA;
+import 'libB.dart';''';
+ Source libA = addSource("/libA.dart", libAContents);
+ String libBContents = "library libB;";
+ Source libB = addSource("/libB.dart", libBContents);
+ context.computeLibraryElement(libA);
+ context.computeErrors(libA);
+ context.computeErrors(libB);
+ expect(context.sourcesNeedingProcessing, hasLength(0));
+ ChangeSet changeSet = new ChangeSet();
+ SourceContainer removedContainer =
+ new _AnalysisContextImplTest_test_applyChanges_removeContainer(libB);
+ changeSet.removedContainer(removedContainer);
+ context.applyChanges(changeSet);
+ List<Source> sources = context.sourcesNeedingProcessing;
+ expect(sources, hasLength(1));
+ expect(sources[0], same(libA));
+ return pumpEventQueue().then((_) {
+ listener.assertEvent(wereSourcesAdded: true);
+ listener.assertEvent(wereSourcesAdded: true);
+ listener.assertEvent(wereSourcesRemovedOrDeleted: true);
listener.assertNoMoreEvents();
});
}
@@ -1188,6 +586,35 @@ class A {}""");
expect(actual, "/// line 1\n/// line 2\n/// line 3");
}
+ void test_computeErrors_dart_none() {
+ Source source = addSource("/lib.dart", "library lib;");
+ List<AnalysisError> errors = context.computeErrors(source);
+ expect(errors, hasLength(0));
+ }
+
+ void test_computeErrors_dart_part() {
+ Source librarySource =
+ addSource("/lib.dart", "library lib; part 'part.dart';");
+ Source partSource = addSource("/part.dart", "part of 'lib';");
+ context.parseCompilationUnit(librarySource);
+ List<AnalysisError> errors = context.computeErrors(partSource);
+ expect(errors, isNotNull);
+ expect(errors.length > 0, isTrue);
+ }
+
+ void test_computeErrors_dart_some() {
+ Source source = addSource("/lib.dart", "library 'lib';");
+ List<AnalysisError> errors = context.computeErrors(source);
+ expect(errors, isNotNull);
+ expect(errors.length > 0, isTrue);
+ }
+
+ void test_computeErrors_html_none() {
+ Source source = addSource("/test.html", "<!DOCTYPE html><html></html>");
+ List<AnalysisError> errors = context.computeErrors(source);
+ expect(errors, hasLength(0));
+ }
+
void test_computeExportedLibraries_none() {
Source source = addSource("/test.dart", "library test;");
expect(context.computeExportedLibraries(source), hasLength(0));
@@ -1206,6 +633,17 @@ class A {}""");
expect(context.computeHtmlElement(source), isNull);
}
+ void test_computeImportedLibraries_none() {
+ Source source = addSource("/test.dart", "library test;");
+ expect(context.computeImportedLibraries(source), hasLength(0));
+ }
+
+ void test_computeImportedLibraries_some() {
+ Source source = addSource(
+ "/test.dart", "library test; import 'lib1.dart'; import 'lib2.dart';");
+ expect(context.computeImportedLibraries(source), hasLength(2));
+ }
+
void test_computeKindOf_html() {
Source source = addSource("/test.html", "");
expect(context.computeKindOf(source), same(SourceKind.HTML));
@@ -1252,6 +690,27 @@ main() {}''');
expect(info, isNotNull);
}
+ Future test_computeResolvedCompilationUnitAsync() {
+ Source source = addSource("/lib.dart", "library lib;");
+ // Complete all pending analysis tasks and flush the AST so that it won't
+ // be available immediately.
+ _performPendingAnalysisTasks();
+ _flushAst(source);
+ bool completed = false;
+ context
+ .computeResolvedCompilationUnitAsync(source, source)
+ .then((CompilationUnit unit) {
+ expect(unit, isNotNull);
+ completed = true;
+ });
+ return pumpEventQueue().then((_) {
+ expect(completed, isFalse);
+ _performPendingAnalysisTasks();
+ }).then((_) => pumpEventQueue()).then((_) {
+ expect(completed, isTrue);
+ });
+ }
+
Future test_computeResolvedCompilationUnitAsync_afterDispose() {
Source source = addSource("/lib.dart", "library lib;");
// Complete all pending analysis tasks and flush the AST so that it won't
@@ -1276,6 +735,76 @@ main() {}''');
});
}
+ Future test_computeResolvedCompilationUnitAsync_cancel() {
+ Source source = addSource("/lib.dart", "library lib;");
+ // Complete all pending analysis tasks and flush the AST so that it won't
+ // be available immediately.
+ _performPendingAnalysisTasks();
+ _flushAst(source);
+ CancelableFuture<CompilationUnit> future =
+ context.computeResolvedCompilationUnitAsync(source, source);
+ bool completed = false;
+ future.then((CompilationUnit unit) {
+ fail('Future should have been canceled');
+ }, onError: (error) {
+ expect(error, new isInstanceOf<FutureCanceledError>());
+ completed = true;
+ });
+ expect(completed, isFalse);
+ expect(context.pendingFutureSources_forTesting, isNotEmpty);
+ future.cancel();
+ expect(context.pendingFutureSources_forTesting, isEmpty);
+ return pumpEventQueue().then((_) {
+ expect(completed, isTrue);
+ expect(context.pendingFutureSources_forTesting, isEmpty);
+ });
+ }
+
+ Future test_computeResolvedCompilationUnitAsync_dispose() {
+ Source source = addSource("/lib.dart", "library lib;");
+ // Complete all pending analysis tasks and flush the AST so that it won't
+ // be available immediately.
+ _performPendingAnalysisTasks();
+ _flushAst(source);
+ bool completed = false;
+ CancelableFuture<CompilationUnit> future =
+ context.computeResolvedCompilationUnitAsync(source, source);
+ future.then((CompilationUnit unit) {
+ fail('Future should have completed with error');
+ }, onError: (error) {
+ expect(error, new isInstanceOf<AnalysisNotScheduledError>());
+ completed = true;
+ });
+ expect(completed, isFalse);
+ expect(context.pendingFutureSources_forTesting, isNotEmpty);
+ // Disposing of the context should cause all pending futures to complete
+ // with AnalysisNotScheduled, so that no clients are left hanging.
+ context.dispose();
+ expect(context.pendingFutureSources_forTesting, isEmpty);
+ return pumpEventQueue().then((_) {
+ expect(completed, isTrue);
+ expect(context.pendingFutureSources_forTesting, isEmpty);
+ });
+ }
+
+ Future test_computeResolvedCompilationUnitAsync_noCacheEntry() {
+ Source librarySource = addSource("/lib.dart", "library lib;");
+ Source partSource = addSource("/part.dart", "part of foo;");
+ bool completed = false;
+ context
+ .computeResolvedCompilationUnitAsync(partSource, librarySource)
+ .then((CompilationUnit unit) {
+ expect(unit, isNotNull);
+ completed = true;
+ });
+ return pumpEventQueue().then((_) {
+ expect(completed, isFalse);
+ _performPendingAnalysisTasks();
+ }).then((_) => pumpEventQueue()).then((_) {
+ expect(completed, isTrue);
+ });
+ }
+
void test_dispose() {
expect(context.isDisposed, isFalse);
context.dispose();
@@ -1308,7 +837,7 @@ main() {}''');
expect(nextResult.result, RESOLVED_UNIT);
}
- void test_ensureResolvedDartUnits_partUnit_notResolved() {
+ void test_ensureResolvedDartUnits_partUnit_hasResolved() {
Source libSource1 = addSource('/lib1.dart', r'''
library lib;
part 'part.dart';
@@ -1326,21 +855,15 @@ part of lib;
new LibrarySpecificUnit(libSource2, partSource);
analysisDriver.computeResult(partTarget1, RESOLVED_UNIT);
analysisDriver.computeResult(partTarget2, RESOLVED_UNIT);
- // flush
- context.getCacheEntry(partTarget1).setState(
- RESOLVED_UNIT, CacheState.FLUSHED);
- context.getCacheEntry(partTarget2).setState(
- RESOLVED_UNIT, CacheState.FLUSHED);
- // schedule recomputing
+ CompilationUnit unit1 =
+ context.getCacheEntry(partTarget1).getValue(RESOLVED_UNIT);
+ CompilationUnit unit2 =
+ context.getCacheEntry(partTarget2).getValue(RESOLVED_UNIT);
List<CompilationUnit> units = context.ensureResolvedDartUnits(partSource);
- expect(units, isNull);
- // should be the next result to compute
- TargetedResult nextResult = context.dartWorkManager.getNextResult();
- expect(nextResult.target, anyOf(partTarget1, partTarget2));
- expect(nextResult.result, RESOLVED_UNIT);
+ expect(units, unorderedEquals([unit1, unit2]));
}
- void test_ensureResolvedDartUnits_partUnit_hasResolved() {
+ void test_ensureResolvedDartUnits_partUnit_notResolved() {
Source libSource1 = addSource('/lib1.dart', r'''
library lib;
part 'part.dart';
@@ -1358,12 +881,18 @@ part of lib;
new LibrarySpecificUnit(libSource2, partSource);
analysisDriver.computeResult(partTarget1, RESOLVED_UNIT);
analysisDriver.computeResult(partTarget2, RESOLVED_UNIT);
- CompilationUnit unit1 =
- context.getCacheEntry(partTarget1).getValue(RESOLVED_UNIT);
- CompilationUnit unit2 =
- context.getCacheEntry(partTarget2).getValue(RESOLVED_UNIT);
+ // flush
+ context.getCacheEntry(partTarget1).setState(
+ RESOLVED_UNIT, CacheState.FLUSHED);
+ context.getCacheEntry(partTarget2).setState(
+ RESOLVED_UNIT, CacheState.FLUSHED);
+ // schedule recomputing
List<CompilationUnit> units = context.ensureResolvedDartUnits(partSource);
- expect(units, unorderedEquals([unit1, unit2]));
+ expect(units, isNull);
+ // should be the next result to compute
+ TargetedResult nextResult = context.dartWorkManager.getNextResult();
+ expect(nextResult.target, anyOf(partTarget1, partTarget2));
+ expect(nextResult.result, RESOLVED_UNIT);
}
void test_exists_false() {
@@ -1431,6 +960,89 @@ part of lib;
expect(element, same(classObject));
}
+ void test_getElement_constructor_named() {
+ Source source = addSource("/lib.dart", r'''
+class A {
+ A.named() {}
+}''');
+ _analyzeAll_assertFinished();
+ LibraryElement library = context.computeLibraryElement(source);
+ ClassElement classA = _findClass(library.definingCompilationUnit, "A");
+ ConstructorElement constructor = classA.constructors[0];
+ ElementLocation location = constructor.location;
+ Element element = context.getElement(location);
+ expect(element, same(constructor));
+ }
+
+ void test_getElement_constructor_unnamed() {
+ Source source = addSource("/lib.dart", r'''
+class A {
+ A() {}
+}''');
+ _analyzeAll_assertFinished();
+ LibraryElement library = context.computeLibraryElement(source);
+ ClassElement classA = _findClass(library.definingCompilationUnit, "A");
+ ConstructorElement constructor = classA.constructors[0];
+ ElementLocation location = constructor.location;
+ Element element = context.getElement(location);
+ expect(element, same(constructor));
+ }
+
+ void test_getElement_enum() {
+ Source source = addSource('/test.dart', 'enum MyEnum {A, B, C}');
+ _analyzeAll_assertFinished();
+ LibraryElement library = context.computeLibraryElement(source);
+ ClassElement myEnum = library.definingCompilationUnit.getEnum('MyEnum');
+ ElementLocation location = myEnum.location;
+ Element element = context.getElement(location);
+ expect(element, same(myEnum));
+ }
+
+ void test_getErrors_dart_none() {
+ Source source = addSource("/lib.dart", "library lib;");
+ var errorInfo = context.getErrors(source);
+ expect(errorInfo, isNotNull);
+ List<AnalysisError> errors = errorInfo.errors;
+ expect(errors, hasLength(0));
+ context.computeErrors(source);
+ errors = errorInfo.errors;
+ expect(errors, hasLength(0));
+ }
+
+ void test_getErrors_dart_some() {
+ Source source = addSource("/lib.dart", "library 'lib';");
+ var errorInfo = context.getErrors(source);
+ expect(errorInfo, isNotNull);
+ List<AnalysisError> errors = errorInfo.errors;
+ expect(errors, hasLength(0));
+ errors = context.computeErrors(source);
+ expect(errors, hasLength(1));
+ }
+
+ void test_getErrors_html_none() {
+ Source source = addSource("/test.html", "<html></html>");
+ AnalysisErrorInfo errorInfo = context.getErrors(source);
+ expect(errorInfo, isNotNull);
+ List<AnalysisError> errors = errorInfo.errors;
+ expect(errors, hasLength(0));
+ context.computeErrors(source);
+ errors = errorInfo.errors;
+ expect(errors, hasLength(0));
+ }
+
+ void test_getErrors_html_some() {
+ Source source = addSource("/test.html", r'''
+<html><head>
+<script type='application/dart' src='test.dart'/>
+</head></html>''');
+ AnalysisErrorInfo errorInfo = context.getErrors(source);
+ expect(errorInfo, isNotNull);
+ List<AnalysisError> errors = errorInfo.errors;
+ expect(errors, hasLength(0));
+ errors = context.computeErrors(source);
+ expect(errors, hasLength(3));
+ }
+
void test_getHtmlElement_dart() {
Source source = addSource("/test.dart", "");
expect(context.getHtmlElement(source), isNull);
@@ -1454,6 +1066,51 @@ part of lib;
expect(result, hasLength(0));
}
+ void test_getHtmlFilesReferencing_library() {
+ Source htmlSource = addSource("/test.html", r'''
+<!DOCTYPE html>
+<html><head>
+<script type='application/dart' src='test.dart'/>
+<script type='application/dart' src='test.js'/>
+</head></html>''');
+ Source librarySource = addSource("/test.dart", "library lib;");
+ context.computeLibraryElement(librarySource);
+ List<Source> result = context.getHtmlFilesReferencing(librarySource);
+ expect(result, hasLength(0));
+ context.computeHtmlElement(htmlSource);
+ result = context.getHtmlFilesReferencing(librarySource);
+ expect(result, hasLength(1));
+ expect(result[0], htmlSource);
+ }
+
+ void test_getHtmlFilesReferencing_part() {
+ Source htmlSource = addSource("/test.html", r'''
+<!DOCTYPE html>
+<html><head>
+<script type='application/dart' src='test.dart'/>
+<script type='application/dart' src='test.js'/>
+</head></html>''');
+ Source librarySource =
+ addSource("/test.dart", "library lib; part 'part.dart';");
+ Source partSource = addSource("/part.dart", "part of lib;");
+ context.computeLibraryElement(librarySource);
+ List<Source> result = context.getHtmlFilesReferencing(partSource);
+ expect(result, hasLength(0));
+ context.computeHtmlElement(htmlSource);
+ result = context.getHtmlFilesReferencing(partSource);
+ expect(result, hasLength(1));
+ expect(result[0], htmlSource);
+ }
+
+ void test_getHtmlSources() {
+ List<Source> sources = context.htmlSources;
+ expect(sources, hasLength(0));
+ Source source = addSource("/test.html", "");
+ sources = context.htmlSources;
+ expect(sources, hasLength(1));
+ expect(sources[0], source);
+ }
+
void test_getKindOf_html() {
Source source = addSource("/test.html", "");
expect(context.getKindOf(source), same(SourceKind.HTML));
@@ -1589,6 +1246,21 @@ export 'libA.dart';''');
expect(result, unorderedEquals([lib1Source, lib2Source]));
}
+ void test_getLibrariesReferencedFromHtml() {
+ Source htmlSource = addSource("/test.html", r'''
+<!DOCTYPE html>
+<html><head>
+<script type='application/dart' src='test.dart'/>
+<script type='application/dart' src='test.js'/>
+</head></html>''');
+ Source librarySource = addSource("/test.dart", "library lib;");
+ context.computeLibraryElement(librarySource);
+ context.computeHtmlElement(htmlSource);
+ List<Source> result = context.getLibrariesReferencedFromHtml(htmlSource);
+ expect(result, hasLength(1));
+ expect(result[0], librarySource);
+ }
+
void test_getLibrariesReferencedFromHtml_none() {
Source htmlSource = addSource("/test.html", r'''
<html><head>
@@ -1662,6 +1334,15 @@ main() {}''');
(obj) => obj is ClassElement, ClassElement, namespace.get("A"));
}
+ void test_getResolvedCompilationUnit_library() {
+ Source source = addSource("/lib.dart", "library libb;");
+ LibraryElement library = context.computeLibraryElement(source);
+ context.computeErrors(source); // Force the resolved unit to be built.
+ expect(context.getResolvedCompilationUnit(source, library), isNotNull);
+ context.setContents(source, "library lib;");
+ expect(context.getResolvedCompilationUnit(source, library), isNull);
+ }
+
void test_getResolvedCompilationUnit_library_null() {
Source source = addSource("/lib.dart", "library lib;");
expect(context.getResolvedCompilationUnit(source, null), isNull);
@@ -1786,41 +1467,271 @@ main() {}''');
}
}
- void test_parseCompilationUnit_html() {
- Source source = addSource("/test.html", "<html></html>");
- expect(context.parseCompilationUnit(source), isNull);
+ void test_parseCompilationUnit_html() {
+ Source source = addSource("/test.html", "<html></html>");
+ expect(context.parseCompilationUnit(source), isNull);
+ }
+
+ void test_parseCompilationUnit_noErrors() {
+ Source source = addSource("/lib.dart", "library lib;");
+ CompilationUnit compilationUnit = context.parseCompilationUnit(source);
+ expect(compilationUnit, isNotNull);
+ AnalysisErrorInfo errorInfo = context.getErrors(source);
+ expect(errorInfo, isNotNull);
+ expect(errorInfo.errors, hasLength(0));
+ }
+
+ void test_parseCompilationUnit_nonExistentSource() {
+ Source source = newSource('/test.dart');
+ resourceProvider.deleteFile('/test.dart');
+ try {
+ context.parseCompilationUnit(source);
+ fail("Expected AnalysisException because file does not exist");
+ } on AnalysisException {
+ // Expected result
+ }
+ }
+
+ void test_parseHtmlDocument() {
+ Source source = addSource("/lib.html", "<!DOCTYPE html><html></html>");
+ Document document = context.parseHtmlDocument(source);
+ expect(document, isNotNull);
+ }
+
+ void test_performAnalysisTask_addPart() {
+ Source libSource = addSource("/lib.dart", r'''
+library lib;
+part 'part.dart';''');
+ // run all tasks without part
+ _analyzeAll_assertFinished();
+ expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libSource)),
+ isTrue, reason: "lib has errors");
+ // add part and run all tasks
+ Source partSource = addSource("/part.dart", r'''
+part of lib;
+''');
+ _analyzeAll_assertFinished();
+ // "libSource" should be here
+ List<Source> librariesWithPart = context.getLibrariesContaining(partSource);
+ expect(librariesWithPart, unorderedEquals([libSource]));
+ expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libSource)),
+ isFalse, reason: "lib doesn't have errors");
+ expect(
+ context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
+ reason: "part resolved");
+ }
+
+ void test_performAnalysisTask_changeLibraryContents() {
+ Source libSource =
+ addSource("/test.dart", "library lib; part 'test-part.dart';");
+ Source partSource = addSource("/test-part.dart", "part of lib;");
+ _analyzeAll_assertFinished();
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
+ reason: "library resolved 1");
+ expect(
+ context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
+ reason: "part resolved 1");
+ // update and analyze #1
+ context.setContents(libSource, "library lib;");
+ 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), isNull,
+ reason: "part resolved 2");
+ // update and analyze #2
+ context.setContents(libSource, "library lib; part 'test-part.dart';");
+ 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 2");
+ expect(
+ context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
+ reason: "part resolved 3");
+ }
+
+ void test_performAnalysisTask_changeLibraryThenPartContents() {
+ Source libSource =
+ addSource("/test.dart", "library lib; part 'test-part.dart';");
+ Source partSource = addSource("/test-part.dart", "part of lib;");
+ _analyzeAll_assertFinished();
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
+ reason: "library resolved 1");
+ expect(
+ context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
+ reason: "part resolved 1");
+ // update and analyze #1
+ context.setContents(libSource, "library lib;");
+ 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), isNull,
+ reason: "part resolved 2");
+ // update and analyze #2
+ context.setContents(partSource, "part of lib; // 1");
+ // Assert that changing the part's content does not effect the library
+ // now that it is no longer part of that library
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
+ 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), isNull,
+ reason: "part resolved 3");
+ }
+
+ void test_performAnalysisTask_changePartContents_makeItAPart() {
+ Source libSource = addSource("/lib.dart", r'''
+library lib;
+part 'part.dart';
+void f(x) {}''');
+ Source partSource = addSource("/part.dart", "void g() { f(null); }");
+ _analyzeAll_assertFinished();
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
+ reason: "library resolved 1");
+ expect(
+ context.getResolvedCompilationUnit2(partSource, partSource), isNotNull,
+ reason: "part resolved 1");
+ // update and analyze
+ context.setContents(partSource, r'''
+part of lib;
+void g() { f(null); }''');
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNull,
+ reason: "library changed 2");
+ expect(context.getResolvedCompilationUnit2(partSource, partSource), 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.getErrors(libSource).errors, hasLength(0));
+ expect(context.getErrors(partSource).errors, hasLength(0));
+ }
+
+ /**
+ * https://code.google.com/p/dart/issues/detail?id=12424
+ */
+ void test_performAnalysisTask_changePartContents_makeItNotPart() {
+ Source libSource = addSource("/lib.dart", r'''
+library lib;
+part 'part.dart';
+void f(x) {}''');
+ Source partSource = addSource("/part.dart", r'''
+part of lib;
+void g() { f(null); }''');
+ _analyzeAll_assertFinished();
+ expect(context.getErrors(libSource).errors, hasLength(0));
+ expect(context.getErrors(partSource).errors, hasLength(0));
+ // Remove 'part' directive, which should make "f(null)" an error.
+ context.setContents(partSource, r'''
+//part of lib;
+void g() { f(null); }''');
+ _analyzeAll_assertFinished();
+ expect(context.getErrors(libSource).errors.length != 0, isTrue);
+ }
+
+ void test_performAnalysisTask_changePartContents_noSemanticChanges() {
+ Source libSource =
+ addSource("/test.dart", "library lib; part 'test-part.dart';");
+ Source partSource = addSource("/test-part.dart", "part of lib;");
+ _analyzeAll_assertFinished();
+ expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
+ reason: "library resolved 1");
+ expect(
+ context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
+ reason: "part resolved 1");
+ // update and analyze #1
+ context.setContents(partSource, "part of lib; // 1");
+ 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");
+ 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_parseCompilationUnit_noErrors() {
- Source source = addSource("/lib.dart", "library lib;");
- CompilationUnit compilationUnit = context.parseCompilationUnit(source);
- expect(compilationUnit, isNotNull);
- AnalysisErrorInfo errorInfo = context.getErrors(source);
- expect(errorInfo, isNotNull);
- expect(errorInfo.errors, hasLength(0));
+ void test_performAnalysisTask_importedLibraryAdd() {
+ Source libASource =
+ addSource("/libA.dart", "library libA; import 'libB.dart';");
+ _analyzeAll_assertFinished();
+ expect(
+ context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
+ reason: "libA resolved 1");
+ expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)),
+ isTrue, reason: "libA has an error");
+ // add libB.dart and analyze
+ Source libBSource = addSource("/libB.dart", "library libB;");
+ _analyzeAll_assertFinished();
+ expect(
+ context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
+ reason: "libA resolved 2");
+ expect(
+ context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull,
+ reason: "libB resolved 2");
+ expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)),
+ isFalse, reason: "libA doesn't have errors");
}
- void test_parseCompilationUnit_nonExistentSource() {
- Source source = newSource('/test.dart');
- resourceProvider.deleteFile('/test.dart');
- try {
- context.parseCompilationUnit(source);
- fail("Expected AnalysisException because file does not exist");
- } on AnalysisException {
- // Expected result
- }
+ void test_performAnalysisTask_importedLibraryDelete() {
+ Source libASource =
+ addSource("/libA.dart", "library libA; import 'libB.dart';");
+ Source libBSource = addSource("/libB.dart", "library libB;");
+ _analyzeAll_assertFinished();
+ expect(
+ context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
+ reason: "libA resolved 1");
+ expect(
+ context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull,
+ reason: "libB resolved 1");
+ expect(!_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)),
+ isTrue, reason: "libA doesn't have errors");
+ // remove libB.dart and analyze
+ _removeSource(libBSource);
+ _analyzeAll_assertFinished();
+ expect(
+ context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
+ reason: "libA resolved 2");
+ expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)),
+ isTrue, reason: "libA has an error");
}
-// void test_resolveCompilationUnit_sourceChangeDuringResolution() {
-// _context = new _AnalysisContext_sourceChangeDuringResolution();
-// AnalysisContextFactory.initContextWithCore(_context);
-// _sourceFactory = _context.sourceFactory;
-// Source source = _addSource("/lib.dart", "library lib;");
-// CompilationUnit compilationUnit =
-// _context.resolveCompilationUnit2(source, source);
-// expect(compilationUnit, isNotNull);
-// expect(_context.getLineInfo(source), isNotNull);
-// }
+ void test_performAnalysisTask_missingPart() {
+ Source source =
+ addSource("/test.dart", "library lib; part 'no-such-file.dart';");
+ _analyzeAll_assertFinished();
+ expect(context.getLibraryElement(source), isNotNull,
+ reason: "performAnalysisTask failed to compute an element model");
+ }
void test_performAnalysisTask_modifiedAfterParse() {
// TODO(scheglov) no threads in Dart
@@ -1840,6 +1751,87 @@ main() {}''');
// JUnitTestCase.assertNotNullMsg("performAnalysisTask failed to compute an element model", _context.getLibraryElement(source));
}
+ void test_performAnalysisTask_onResultComputed() {
+ Set<String> libraryElementUris = new Set<String>();
+ Set<String> parsedUnitUris = new Set<String>();
+ Set<String> resolvedUnitUris = new Set<String>();
+ // listen
+ context.onResultComputed(LIBRARY_ELEMENT).listen((event) {
+ Source librarySource = event.target;
+ libraryElementUris.add(librarySource.uri.toString());
+ });
+ context.onResultComputed(PARSED_UNIT).listen((event) {
+ Source source = event.target;
+ parsedUnitUris.add(source.uri.toString());
+ });
+ context.onResultComputed(RESOLVED_UNIT).listen((event) {
+ LibrarySpecificUnit target = event.target;
+ Source librarySource = target.library;
+ resolvedUnitUris.add(librarySource.uri.toString());
+ });
+ // analyze
+ addSource('/test.dart', 'main() {}');
+ _analyzeAll_assertFinished();
+ // verify
+ expect(libraryElementUris, contains('dart:core'));
+ expect(libraryElementUris, contains('file:///test.dart'));
+ expect(parsedUnitUris, contains('dart:core'));
+ expect(parsedUnitUris, contains('file:///test.dart'));
+ expect(resolvedUnitUris, contains('dart:core'));
+ expect(resolvedUnitUris, contains('file:///test.dart'));
+ }
+
+// void test_resolveCompilationUnit_sourceChangeDuringResolution() {
+// _context = new _AnalysisContext_sourceChangeDuringResolution();
+// AnalysisContextFactory.initContextWithCore(_context);
+// _sourceFactory = _context.sourceFactory;
+// Source source = _addSource("/lib.dart", "library lib;");
+// CompilationUnit compilationUnit =
+// _context.resolveCompilationUnit2(source, source);
+// expect(compilationUnit, isNotNull);
+// expect(_context.getLineInfo(source), isNotNull);
+// }
+
+ void test_resolveCompilationUnit_import_relative() {
+ Source sourceA =
+ addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}");
+ addSource("/libB.dart", "library libB; class B{}");
+ CompilationUnit compilationUnit =
+ context.resolveCompilationUnit2(sourceA, sourceA);
+ expect(compilationUnit, isNotNull);
+ LibraryElement library = compilationUnit.element.library;
+ List<LibraryElement> importedLibraries = library.importedLibraries;
+ assertNamedElements(importedLibraries, ["dart.core", "libB"]);
+ List<LibraryElement> visibleLibraries = library.visibleLibraries;
+ assertNamedElements(visibleLibraries, [
+ "dart.core",
+ "dart.async",
+ "dart.math",
+ "libA",
+ "libB"
+ ]);
+ }
+
+ void test_resolveCompilationUnit_import_relative_cyclic() {
+ Source sourceA =
+ addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}");
+ addSource("/libB.dart", "library libB; import 'libA.dart'; class B{}");
+ CompilationUnit compilationUnit =
+ context.resolveCompilationUnit2(sourceA, sourceA);
+ expect(compilationUnit, isNotNull);
+ LibraryElement library = compilationUnit.element.library;
+ List<LibraryElement> importedLibraries = library.importedLibraries;
+ assertNamedElements(importedLibraries, ["dart.core", "libB"]);
+ List<LibraryElement> visibleLibraries = library.visibleLibraries;
+ assertNamedElements(visibleLibraries, [
+ "dart.core",
+ "dart.async",
+ "dart.math",
+ "libA",
+ "libB"
+ ]);
+ }
+
void test_resolveCompilationUnit_library() {
Source source = addSource("/lib.dart", "library lib;");
LibraryElement library = context.computeLibraryElement(source);
@@ -1945,6 +1937,24 @@ int a = 0;''');
expect(_getIncrementalAnalysisCache(context), isNull);
}
+ void test_setContents_unchanged_consistentModificationTime() {
+ String contents = "// foo";
+ Source source = addSource("/test.dart", contents);
+ context.setContents(source, contents);
+ // do all, no tasks
+ _analyzeAll_assertFinished();
+ {
+ AnalysisResult result = context.performAnalysisTask();
+ expect(result.changeNotices, isNull);
+ }
+ // set the same contents, still no tasks
+ context.setContents(source, contents);
+ {
+ AnalysisResult result = context.performAnalysisTask();
+ expect(result.changeNotices, isNull);
+ }
+ }
+
void test_setSourceFactory() {
expect(context.sourceFactory, sourceFactory);
SourceFactory factory = new SourceFactory([]);
@@ -1965,52 +1975,6 @@ int a = 0;''');
expect(context.sourcesNeedingProcessing.contains(source), isFalse);
}
- Future test_computeResolvedCompilationUnitAsync() {
- Source source = addSource("/lib.dart", "library lib;");
- // Complete all pending analysis tasks and flush the AST so that it won't
- // be available immediately.
- _performPendingAnalysisTasks();
- _flushAst(source);
- bool completed = false;
- context
- .computeResolvedCompilationUnitAsync(source, source)
- .then((CompilationUnit unit) {
- expect(unit, isNotNull);
- completed = true;
- });
- return pumpEventQueue().then((_) {
- expect(completed, isFalse);
- _performPendingAnalysisTasks();
- }).then((_) => pumpEventQueue()).then((_) {
- expect(completed, isTrue);
- });
- }
-
- Future test_computeResolvedCompilationUnitAsync_cancel() {
- Source source = addSource("/lib.dart", "library lib;");
- // Complete all pending analysis tasks and flush the AST so that it won't
- // be available immediately.
- _performPendingAnalysisTasks();
- _flushAst(source);
- CancelableFuture<CompilationUnit> future =
- context.computeResolvedCompilationUnitAsync(source, source);
- bool completed = false;
- future.then((CompilationUnit unit) {
- fail('Future should have been canceled');
- }, onError: (error) {
- expect(error, new isInstanceOf<FutureCanceledError>());
- completed = true;
- });
- expect(completed, isFalse);
- expect(context.pendingFutureSources_forTesting, isNotEmpty);
- future.cancel();
- expect(context.pendingFutureSources_forTesting, isEmpty);
- return pumpEventQueue().then((_) {
- expect(completed, isTrue);
- expect(context.pendingFutureSources_forTesting, isEmpty);
- });
- }
-
void xtest_performAnalysisTask_stress() {
int maxCacheSize = 4;
AnalysisOptionsImpl options =
« no previous file with comments | « pkg/analyzer/lib/src/task/html.dart ('k') | pkg/analyzer/test/src/task/dart_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698