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

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

Issue 1053673008: Initial re-write of analysis context for the new task model (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library test.src.context.context_test;
6
7 import 'dart:async';
8 import 'dart:collection';
9
10 import 'package:analyzer/src/cancelable_future.dart';
11 import 'package:analyzer/src/context/cache.dart';
12 import 'package:analyzer/src/context/context.dart';
13 import 'package:analyzer/src/generated/ast.dart';
14 import 'package:analyzer/src/generated/element.dart';
15 import 'package:analyzer/src/generated/engine.dart'
16 show
17 AnalysisContext,
18 AnalysisContextStatistics,
19 AnalysisDelta,
20 AnalysisEngine,
21 AnalysisErrorInfo,
22 AnalysisLevel,
23 AnalysisNotScheduledError,
24 AnalysisOptions,
25 AnalysisOptionsImpl,
26 AnalysisResult,
27 ChangeNotice,
28 ChangeSet,
29 IncrementalAnalysisCache,
30 TimestampedData;
31 import 'package:analyzer/src/generated/error.dart';
32 import 'package:analyzer/src/generated/html.dart' as ht;
33 import 'package:analyzer/src/generated/java_engine.dart';
34 import 'package:analyzer/src/generated/java_engine_io.dart';
35 import 'package:analyzer/src/generated/java_io.dart';
36 import 'package:analyzer/src/generated/resolver.dart';
37 import 'package:analyzer/src/generated/scanner.dart';
38 import 'package:analyzer/src/generated/sdk.dart';
39 import 'package:analyzer/src/generated/sdk_io.dart';
40 import 'package:analyzer/src/generated/source.dart';
41 import 'package:analyzer/src/generated/source_io.dart';
42 import 'package:analyzer/src/generated/testing/ast_factory.dart';
43 import 'package:analyzer/src/generated/testing/element_factory.dart';
44 import 'package:analyzer/src/generated/testing/test_type_provider.dart';
45 import 'package:analyzer/src/generated/utilities_dart.dart';
46 import 'package:analyzer/src/plugin/engine_plugin.dart';
47 import 'package:analyzer/src/plugin/plugin_impl.dart';
48 import 'package:unittest/unittest.dart';
49 import 'package:watcher/src/utils.dart';
50
51 import '../../generated/engine_test.dart';
52 import '../../generated/test_support.dart';
53 import '../../reflective_tests.dart';
54
55 main() {
56 groupSep = ' | ';
57 runReflectiveTests(AnalysisContextImplTest);
58 }
59
60 contextWithCore() {
61 return new AnalysisContextForTests();
62 }
63
64 /**
65 * An analysis context that has a fake SDK that is much smaller and faster for
66 * testing purposes.
67 */
68 class AnalysisContextForTests extends AnalysisContextImpl {
69 AnalysisContextForTests() {
70 initWithCore();
71 }
72
73 @override
74 void set analysisOptions(AnalysisOptions options) {
75 AnalysisOptions currentOptions = analysisOptions;
76 bool needsRecompute = currentOptions.analyzeFunctionBodiesPredicate !=
77 options.analyzeFunctionBodiesPredicate ||
78 currentOptions.generateImplicitErrors !=
79 options.generateImplicitErrors ||
80 currentOptions.generateSdkErrors != options.generateSdkErrors ||
81 currentOptions.dart2jsHint != options.dart2jsHint ||
82 (currentOptions.hint && !options.hint) ||
83 currentOptions.preserveComments != options.preserveComments ||
84 currentOptions.enableNullAwareOperators !=
85 options.enableNullAwareOperators ||
86 currentOptions.enableStrictCallChecks != options.enableStrictCallChecks;
87 if (needsRecompute) {
88 fail(
89 "Cannot set options that cause the sources to be reanalyzed in a test context");
90 }
91 super.analysisOptions = options;
92 }
93
94 @override
95 LibraryResolverFactory get libraryResolverFactory => null;
96
97 @override
98 bool exists(Source source) =>
99 super.exists(source) || sourceFactory.dartSdk.context.exists(source);
100
101 @override
102 TimestampedData<String> getContents(Source source) {
103 if (source.isInSystemLibrary) {
104 return sourceFactory.dartSdk.context.getContents(source);
105 }
106 return super.getContents(source);
107 }
108
109 @override
110 int getModificationStamp(Source source) {
111 if (source.isInSystemLibrary) {
112 return sourceFactory.dartSdk.context.getModificationStamp(source);
113 }
114 return super.getModificationStamp(source);
115 }
116
117 /**
118 * Initialize the given analysis context with a fake core library already reso lved.
119 *
120 * @param context the context to be initialized (not `null`)
121 * @return the analysis context that was created
122 */
123 void initWithCore() {
124 DirectoryBasedDartSdk sdk = new FakeSdk(new JavaFile("/fake/sdk"));
125 SourceFactory sourceFactory =
126 new SourceFactory([new DartUriResolver(sdk), new FileUriResolver()]);
127 this.sourceFactory = sourceFactory;
128 AnalysisContext coreContext = sdk.context;
129 //
130 // dart:core
131 //
132 TestTypeProvider provider = new TestTypeProvider();
133 typeProvider = provider;
134 CompilationUnitElementImpl coreUnit =
135 new CompilationUnitElementImpl("core.dart");
136 Source coreSource = sourceFactory.forUri(DartSdk.DART_CORE);
137 coreContext.setContents(coreSource, "");
138 coreUnit.source = coreSource;
139 ClassElementImpl proxyClassElement = ElementFactory.classElement2("_Proxy");
140 coreUnit.types = <ClassElement>[
141 provider.boolType.element,
142 provider.deprecatedType.element,
143 provider.doubleType.element,
144 provider.functionType.element,
145 provider.intType.element,
146 provider.iterableType.element,
147 provider.iteratorType.element,
148 provider.listType.element,
149 provider.mapType.element,
150 provider.nullType.element,
151 provider.numType.element,
152 provider.objectType.element,
153 proxyClassElement,
154 provider.stackTraceType.element,
155 provider.stringType.element,
156 provider.symbolType.element,
157 provider.typeType.element
158 ];
159 coreUnit.functions = <FunctionElement>[
160 ElementFactory.functionElement3("identical", provider.boolType.element,
161 <ClassElement>[
162 provider.objectType.element,
163 provider.objectType.element
164 ], null),
165 ElementFactory.functionElement3("print", VoidTypeImpl.instance.element,
166 <ClassElement>[provider.objectType.element], null)
167 ];
168 TopLevelVariableElement proxyTopLevelVariableElt = ElementFactory
169 .topLevelVariableElement3("proxy", true, false, proxyClassElement.type);
170 TopLevelVariableElement deprecatedTopLevelVariableElt = ElementFactory
171 .topLevelVariableElement3(
172 "deprecated", true, false, provider.deprecatedType);
173 coreUnit.accessors = <PropertyAccessorElement>[
174 proxyTopLevelVariableElt.getter,
175 deprecatedTopLevelVariableElt.getter
176 ];
177 coreUnit.topLevelVariables = <TopLevelVariableElement>[
178 proxyTopLevelVariableElt,
179 deprecatedTopLevelVariableElt
180 ];
181 LibraryElementImpl coreLibrary = new LibraryElementImpl.forNode(
182 coreContext, AstFactory.libraryIdentifier2(["dart", "core"]));
183 coreLibrary.definingCompilationUnit = coreUnit;
184 //
185 // dart:async
186 //
187 CompilationUnitElementImpl asyncUnit =
188 new CompilationUnitElementImpl("async.dart");
189 Source asyncSource = sourceFactory.forUri(DartSdk.DART_ASYNC);
190 coreContext.setContents(asyncSource, "");
191 asyncUnit.source = asyncSource;
192 // Future
193 ClassElementImpl futureElement =
194 ElementFactory.classElement2("Future", ["T"]);
195 InterfaceType futureType = futureElement.type;
196 // factory Future.value([value])
197 ConstructorElementImpl futureConstructor =
198 ElementFactory.constructorElement2(futureElement, "value");
199 futureConstructor.parameters = <ParameterElement>[
200 ElementFactory.positionalParameter2("value", provider.dynamicType)
201 ];
202 futureConstructor.factory = true;
203 (futureConstructor.type as FunctionTypeImpl).typeArguments =
204 futureElement.type.typeArguments;
205 futureElement.constructors = <ConstructorElement>[futureConstructor];
206 // Future then(onValue(T value), { Function onError });
207 List<ParameterElement> parameters = <ParameterElement>[
208 ElementFactory.requiredParameter2(
209 "value", futureElement.typeParameters[0].type)
210 ];
211 FunctionTypeAliasElementImpl aliasElement =
212 new FunctionTypeAliasElementImpl.forNode(null);
213 aliasElement.synthetic = true;
214 aliasElement.parameters = parameters;
215 aliasElement.returnType = provider.dynamicType;
216 aliasElement.enclosingElement = asyncUnit;
217 FunctionTypeImpl aliasType = new FunctionTypeImpl.con2(aliasElement);
218 aliasElement.shareTypeParameters(futureElement.typeParameters);
219 aliasType.typeArguments = futureElement.type.typeArguments;
220 MethodElement thenMethod = ElementFactory.methodElementWithParameters(
221 "then", futureElement.type.typeArguments, futureType, [
222 ElementFactory.requiredParameter2("onValue", aliasType),
223 ElementFactory.namedParameter2("onError", provider.functionType)
224 ]);
225 futureElement.methods = <MethodElement>[thenMethod];
226 // Completer
227 ClassElementImpl completerElement =
228 ElementFactory.classElement2("Completer", ["T"]);
229 ConstructorElementImpl completerConstructor =
230 ElementFactory.constructorElement2(completerElement, null);
231 (completerConstructor.type as FunctionTypeImpl).typeArguments =
232 completerElement.type.typeArguments;
233 completerElement.constructors = <ConstructorElement>[completerConstructor];
234 asyncUnit.types = <ClassElement>[
235 completerElement,
236 futureElement,
237 ElementFactory.classElement2("Stream", ["T"])
238 ];
239 LibraryElementImpl asyncLibrary = new LibraryElementImpl.forNode(
240 coreContext, AstFactory.libraryIdentifier2(["dart", "async"]));
241 asyncLibrary.definingCompilationUnit = asyncUnit;
242 //
243 // dart:html
244 //
245 CompilationUnitElementImpl htmlUnit =
246 new CompilationUnitElementImpl("html_dartium.dart");
247 Source htmlSource = sourceFactory.forUri(DartSdk.DART_HTML);
248 coreContext.setContents(htmlSource, "");
249 htmlUnit.source = htmlSource;
250 ClassElementImpl elementElement = ElementFactory.classElement2("Element");
251 InterfaceType elementType = elementElement.type;
252 ClassElementImpl canvasElement =
253 ElementFactory.classElement("CanvasElement", elementType);
254 ClassElementImpl contextElement =
255 ElementFactory.classElement2("CanvasRenderingContext");
256 InterfaceType contextElementType = contextElement.type;
257 ClassElementImpl context2dElement = ElementFactory.classElement(
258 "CanvasRenderingContext2D", contextElementType);
259 canvasElement.methods = <MethodElement>[
260 ElementFactory.methodElement(
261 "getContext", contextElementType, [provider.stringType])
262 ];
263 canvasElement.accessors = <PropertyAccessorElement>[
264 ElementFactory.getterElement("context2D", false, context2dElement.type)
265 ];
266 canvasElement.fields = canvasElement.accessors
267 .map((PropertyAccessorElement accessor) => accessor.variable)
268 .toList();
269 ClassElementImpl documentElement =
270 ElementFactory.classElement("Document", elementType);
271 ClassElementImpl htmlDocumentElement =
272 ElementFactory.classElement("HtmlDocument", documentElement.type);
273 htmlDocumentElement.methods = <MethodElement>[
274 ElementFactory.methodElement(
275 "query", elementType, <DartType>[provider.stringType])
276 ];
277 htmlUnit.types = <ClassElement>[
278 ElementFactory.classElement("AnchorElement", elementType),
279 ElementFactory.classElement("BodyElement", elementType),
280 ElementFactory.classElement("ButtonElement", elementType),
281 canvasElement,
282 contextElement,
283 context2dElement,
284 ElementFactory.classElement("DivElement", elementType),
285 documentElement,
286 elementElement,
287 htmlDocumentElement,
288 ElementFactory.classElement("InputElement", elementType),
289 ElementFactory.classElement("SelectElement", elementType)
290 ];
291 htmlUnit.functions = <FunctionElement>[
292 ElementFactory.functionElement3("query", elementElement,
293 <ClassElement>[provider.stringType.element], ClassElement.EMPTY_LIST)
294 ];
295 TopLevelVariableElementImpl document = ElementFactory
296 .topLevelVariableElement3(
297 "document", false, true, htmlDocumentElement.type);
298 htmlUnit.topLevelVariables = <TopLevelVariableElement>[document];
299 htmlUnit.accessors = <PropertyAccessorElement>[document.getter];
300 LibraryElementImpl htmlLibrary = new LibraryElementImpl.forNode(
301 coreContext, AstFactory.libraryIdentifier2(["dart", "dom", "html"]));
302 htmlLibrary.definingCompilationUnit = htmlUnit;
303 //
304 // dart:math
305 //
306 CompilationUnitElementImpl mathUnit =
307 new CompilationUnitElementImpl("math.dart");
308 Source mathSource = sourceFactory.forUri("dart:math");
309 coreContext.setContents(mathSource, "");
310 mathUnit.source = mathSource;
311 FunctionElement cosElement = ElementFactory.functionElement3("cos",
312 provider.doubleType.element, <ClassElement>[provider.numType.element],
313 ClassElement.EMPTY_LIST);
314 TopLevelVariableElement ln10Element = ElementFactory
315 .topLevelVariableElement3("LN10", true, false, provider.doubleType);
316 TopLevelVariableElement piElement = ElementFactory.topLevelVariableElement3(
317 "PI", true, false, provider.doubleType);
318 ClassElementImpl randomElement = ElementFactory.classElement2("Random");
319 randomElement.abstract = true;
320 ConstructorElementImpl randomConstructor =
321 ElementFactory.constructorElement2(randomElement, null);
322 randomConstructor.factory = true;
323 ParameterElementImpl seedParam = new ParameterElementImpl("seed", 0);
324 seedParam.parameterKind = ParameterKind.POSITIONAL;
325 seedParam.type = provider.intType;
326 randomConstructor.parameters = <ParameterElement>[seedParam];
327 randomElement.constructors = <ConstructorElement>[randomConstructor];
328 FunctionElement sinElement = ElementFactory.functionElement3("sin",
329 provider.doubleType.element, <ClassElement>[provider.numType.element],
330 ClassElement.EMPTY_LIST);
331 FunctionElement sqrtElement = ElementFactory.functionElement3("sqrt",
332 provider.doubleType.element, <ClassElement>[provider.numType.element],
333 ClassElement.EMPTY_LIST);
334 mathUnit.accessors = <PropertyAccessorElement>[
335 ln10Element.getter,
336 piElement.getter
337 ];
338 mathUnit.functions = <FunctionElement>[cosElement, sinElement, sqrtElement];
339 mathUnit.topLevelVariables = <TopLevelVariableElement>[
340 ln10Element,
341 piElement
342 ];
343 mathUnit.types = <ClassElement>[randomElement];
344 LibraryElementImpl mathLibrary = new LibraryElementImpl.forNode(
345 coreContext, AstFactory.libraryIdentifier2(["dart", "math"]));
346 mathLibrary.definingCompilationUnit = mathUnit;
347 //
348 // Set empty sources for the rest of the libraries.
349 //
350 Source source = sourceFactory.forUri("dart:_interceptors");
351 coreContext.setContents(source, "");
352 source = sourceFactory.forUri("dart:_js_helper");
353 coreContext.setContents(source, "");
354 //
355 // Record the elements.
356 //
357 HashMap<Source, LibraryElement> elementMap =
358 new HashMap<Source, LibraryElement>();
359 elementMap[coreSource] = coreLibrary;
360 elementMap[asyncSource] = asyncLibrary;
361 elementMap[htmlSource] = htmlLibrary;
362 elementMap[mathSource] = mathLibrary;
363 recordLibraryElements(elementMap);
364 }
365
366 /**
367 * Set the analysis options, even if they would force re-analysis. This method should only be
368 * invoked before the fake SDK is initialized.
369 *
370 * @param options the analysis options to be set
371 */
372 void _internalSetAnalysisOptions(AnalysisOptions options) {
373 super.analysisOptions = options;
374 }
375 }
376
377 @reflectiveTest
378 class AnalysisContextImplTest extends EngineTestCase {
379 /**
380 * An analysis context whose source factory is [sourceFactory].
381 */
382 AnalysisContextImpl _context;
383
384 /**
385 * The source factory associated with the analysis [context].
386 */
387 SourceFactory _sourceFactory;
388
389 void fail_applyChanges_change_flush_element() {
390 _context = contextWithCore();
391 _sourceFactory = _context.sourceFactory;
392 Source librarySource = _addSource("/lib.dart", r'''
393 library lib;
394 int a = 0;''');
395 expect(_context.computeLibraryElement(librarySource), isNotNull);
396 _context.setContents(librarySource, r'''
397 library lib;
398 int aa = 0;''');
399 expect(_context.getLibraryElement(librarySource), isNull);
400 }
401
402 Future fail_applyChanges_change_multiple() {
403 _context = contextWithCore();
404 SourcesChangedListener listener = new SourcesChangedListener();
405 _context.onSourcesChanged.listen(listener.onData);
406 _sourceFactory = _context.sourceFactory;
407 String libraryContents1 = r'''
408 library lib;
409 part 'part.dart';
410 int a = 0;''';
411 Source librarySource = _addSource("/lib.dart", libraryContents1);
412 String partContents1 = r'''
413 part of lib;
414 int b = a;''';
415 Source partSource = _addSource("/part.dart", partContents1);
416 _context.computeLibraryElement(librarySource);
417 String libraryContents2 = r'''
418 library lib;
419 part 'part.dart';
420 int aa = 0;''';
421 _context.setContents(librarySource, libraryContents2);
422 String partContents2 = r'''
423 part of lib;
424 int b = aa;''';
425 _context.setContents(partSource, partContents2);
426 _context.computeLibraryElement(librarySource);
427 CompilationUnit libraryUnit =
428 _context.resolveCompilationUnit2(librarySource, librarySource);
429 expect(libraryUnit, isNotNull);
430 CompilationUnit partUnit =
431 _context.resolveCompilationUnit2(partSource, librarySource);
432 expect(partUnit, isNotNull);
433 TopLevelVariableDeclaration declaration =
434 libraryUnit.declarations[0] as TopLevelVariableDeclaration;
435 Element declarationElement = declaration.variables.variables[0].element;
436 TopLevelVariableDeclaration use =
437 partUnit.declarations[0] as TopLevelVariableDeclaration;
438 Element useElement = (use.variables.variables[
439 0].initializer as SimpleIdentifier).staticElement;
440 expect((useElement as PropertyAccessorElement).variable,
441 same(declarationElement));
442 return pumpEventQueue().then((_) {
443 listener.assertEvent(wereSourcesAdded: true);
444 listener.assertEvent(changedSources: [librarySource]);
445 listener.assertEvent(wereSourcesAdded: true);
446 listener.assertEvent(changedSources: [partSource]);
447 listener.assertEvent(changedSources: [librarySource]);
448 listener.assertEvent(changedSources: [partSource]);
449 listener.assertNoMoreEvents();
450 });
451 }
452
453 void fail_applyChanges_empty() {
454 _context.applyChanges(new ChangeSet());
455 expect(_context.performAnalysisTask().changeNotices, isNull);
456 // This test appears to be flaky. If it is named "test_" it fails, if it's
457 // named "fail_" it doesn't fail. I'm guessing that it's dependent on some
458 // other test being run (or not).
459 fail('Should have failed');
460 }
461
462 void fail_applyChanges_overriddenSource() {
463 // Note: addSource adds the source to the contentCache.
464 Source source = _addSource("/test.dart", "library test;");
465 _context.computeErrors(source);
466 while (!_context.sourcesNeedingProcessing.isEmpty) {
467 _context.performAnalysisTask();
468 }
469 // Adding the source as a changedSource should have no effect since
470 // it is already overridden in the content cache.
471 ChangeSet changeSet = new ChangeSet();
472 changeSet.changedSource(source);
473 _context.applyChanges(changeSet);
474 expect(_context.sourcesNeedingProcessing, hasLength(0));
475 }
476
477 Future fail_applyChanges_remove() {
478 _context = contextWithCore();
479 SourcesChangedListener listener = new SourcesChangedListener();
480 _context.onSourcesChanged.listen(listener.onData);
481 _sourceFactory = _context.sourceFactory;
482 String libAContents = r'''
483 library libA;
484 import 'libB.dart';''';
485 Source libA = _addSource("/libA.dart", libAContents);
486 String libBContents = "library libB;";
487 Source libB = _addSource("/libB.dart", libBContents);
488 LibraryElement libAElement = _context.computeLibraryElement(libA);
489 expect(libAElement, isNotNull);
490 List<LibraryElement> importedLibraries = libAElement.importedLibraries;
491 expect(importedLibraries, hasLength(2));
492 _context.computeErrors(libA);
493 _context.computeErrors(libB);
494 expect(_context.sourcesNeedingProcessing, hasLength(0));
495 _context.setContents(libB, null);
496 _removeSource(libB);
497 List<Source> sources = _context.sourcesNeedingProcessing;
498 expect(sources, hasLength(1));
499 expect(sources[0], same(libA));
500 libAElement = _context.computeLibraryElement(libA);
501 importedLibraries = libAElement.importedLibraries;
502 expect(importedLibraries, hasLength(1));
503 return pumpEventQueue().then((_) {
504 listener.assertEvent(wereSourcesAdded: true);
505 listener.assertEvent(changedSources: [libA]);
506 listener.assertEvent(wereSourcesAdded: true);
507 listener.assertEvent(changedSources: [libB]);
508 listener.assertEvent(changedSources: [libB]);
509 listener.assertEvent(wereSourcesRemovedOrDeleted: true);
510 listener.assertNoMoreEvents();
511 });
512 }
513
514 Future fail_applyChanges_removeContainer() {
515 _context = contextWithCore();
516 SourcesChangedListener listener = new SourcesChangedListener();
517 _context.onSourcesChanged.listen(listener.onData);
518 _sourceFactory = _context.sourceFactory;
519 String libAContents = r'''
520 library libA;
521 import 'libB.dart';''';
522 Source libA = _addSource("/libA.dart", libAContents);
523 String libBContents = "library libB;";
524 Source libB = _addSource("/libB.dart", libBContents);
525 _context.computeLibraryElement(libA);
526 _context.computeErrors(libA);
527 _context.computeErrors(libB);
528 expect(_context.sourcesNeedingProcessing, hasLength(0));
529 ChangeSet changeSet = new ChangeSet();
530 SourceContainer removedContainer =
531 new _AnalysisContextImplTest_test_applyChanges_removeContainer(libB);
532 changeSet.removedContainer(removedContainer);
533 _context.applyChanges(changeSet);
534 List<Source> sources = _context.sourcesNeedingProcessing;
535 expect(sources, hasLength(1));
536 expect(sources[0], same(libA));
537 return pumpEventQueue().then((_) {
538 listener.assertEvent(wereSourcesAdded: true);
539 listener.assertEvent(changedSources: [libA]);
540 listener.assertEvent(wereSourcesAdded: true);
541 listener.assertEvent(changedSources: [libB]);
542 listener.assertEvent(wereSourcesRemovedOrDeleted: true);
543 listener.assertNoMoreEvents();
544 });
545 }
546
547 void fail_computeDocumentationComment_block() {
548 _context = contextWithCore();
549 _sourceFactory = _context.sourceFactory;
550 String comment = "/** Comment */";
551 Source source = _addSource("/test.dart", """
552 $comment
553 class A {}""");
554 LibraryElement libraryElement = _context.computeLibraryElement(source);
555 expect(libraryElement, isNotNull);
556 ClassElement classElement = libraryElement.definingCompilationUnit.types[0];
557 expect(libraryElement, isNotNull);
558 expect(_context.computeDocumentationComment(classElement), comment);
559 }
560
561 void fail_computeDocumentationComment_none() {
562 _context = contextWithCore();
563 _sourceFactory = _context.sourceFactory;
564 Source source = _addSource("/test.dart", "class A {}");
565 LibraryElement libraryElement = _context.computeLibraryElement(source);
566 expect(libraryElement, isNotNull);
567 ClassElement classElement = libraryElement.definingCompilationUnit.types[0];
568 expect(libraryElement, isNotNull);
569 expect(_context.computeDocumentationComment(classElement), isNull);
570 }
571
572 void fail_computeDocumentationComment_singleLine_multiple_EOL_n() {
573 _context = contextWithCore();
574 _sourceFactory = _context.sourceFactory;
575 String comment = "/// line 1\n/// line 2\n/// line 3\n";
576 Source source = _addSource("/test.dart", "${comment}class A {}");
577 LibraryElement libraryElement = _context.computeLibraryElement(source);
578 expect(libraryElement, isNotNull);
579 ClassElement classElement = libraryElement.definingCompilationUnit.types[0];
580 expect(libraryElement, isNotNull);
581 String actual = _context.computeDocumentationComment(classElement);
582 expect(actual, "/// line 1\n/// line 2\n/// line 3");
583 }
584
585 void fail_computeDocumentationComment_singleLine_multiple_EOL_rn() {
586 _context = contextWithCore();
587 _sourceFactory = _context.sourceFactory;
588 String comment = "/// line 1\r\n/// line 2\r\n/// line 3\r\n";
589 Source source = _addSource("/test.dart", "${comment}class A {}");
590 LibraryElement libraryElement = _context.computeLibraryElement(source);
591 expect(libraryElement, isNotNull);
592 ClassElement classElement = libraryElement.definingCompilationUnit.types[0];
593 expect(libraryElement, isNotNull);
594 String actual = _context.computeDocumentationComment(classElement);
595 expect(actual, "/// line 1\n/// line 2\n/// line 3");
596 }
597
598 void fail_computeErrors_dart_none() {
599 Source source = _addSource("/lib.dart", "library lib;");
600 List<AnalysisError> errors = _context.computeErrors(source);
601 expect(errors, hasLength(0));
602 }
603
604 void fail_computeErrors_dart_part() {
605 Source librarySource =
606 _addSource("/lib.dart", "library lib; part 'part.dart';");
607 Source partSource = _addSource("/part.dart", "part of 'lib';");
608 _context.parseCompilationUnit(librarySource);
609 List<AnalysisError> errors = _context.computeErrors(partSource);
610 expect(errors, isNotNull);
611 expect(errors.length > 0, isTrue);
612 }
613
614 void fail_computeErrors_dart_some() {
615 Source source = _addSource("/lib.dart", "library 'lib';");
616 List<AnalysisError> errors = _context.computeErrors(source);
617 expect(errors, isNotNull);
618 expect(errors.length > 0, isTrue);
619 }
620
621 void fail_computeErrors_html_none() {
622 Source source = _addSource("/test.html", "<html></html>");
623 List<AnalysisError> errors = _context.computeErrors(source);
624 expect(errors, hasLength(0));
625 }
626
627 void fail_computeHtmlElement_valid() {
628 Source source = _addSource("/test.html", "<html></html>");
629 HtmlElement element = _context.computeHtmlElement(source);
630 expect(element, isNotNull);
631 expect(_context.computeHtmlElement(source), same(element));
632 }
633
634 void fail_computeImportedLibraries_none() {
635 Source source = _addSource("/test.dart", "library test;");
636 expect(_context.computeImportedLibraries(source), hasLength(0));
637 }
638
639 void fail_computeImportedLibraries_some() {
640 // addSource("/lib1.dart", "library lib1;");
641 // addSource("/lib2.dart", "library lib2;");
642 Source source = _addSource(
643 "/test.dart", "library test; import 'lib1.dart'; import 'lib2.dart';");
644 expect(_context.computeImportedLibraries(source), hasLength(2));
645 }
646
647 void fail_computeKindOf_html() {
648 Source source = _addSource("/test.html", "");
649 expect(_context.computeKindOf(source), same(SourceKind.HTML));
650 }
651
652 void fail_computeLibraryElement() {
653 _context = contextWithCore();
654 _sourceFactory = _context.sourceFactory;
655 Source source = _addSource("/test.dart", "library lib;");
656 LibraryElement element = _context.computeLibraryElement(source);
657 expect(element, isNotNull);
658 }
659
660 void fail_computeResolvableCompilationUnit_dart_exception() {
661 TestSource source = _addSourceWithException("/test.dart");
662 try {
663 _context.computeResolvableCompilationUnit(source);
664 fail("Expected AnalysisException");
665 } on AnalysisException {
666 // Expected
667 }
668 }
669
670 void fail_computeResolvableCompilationUnit_html_exception() {
671 Source source = _addSource("/lib.html", "<html></html>");
672 try {
673 _context.computeResolvableCompilationUnit(source);
674 fail("Expected AnalysisException");
675 } on AnalysisException {
676 // Expected
677 }
678 }
679
680 void fail_computeResolvableCompilationUnit_valid() {
681 Source source = _addSource("/lib.dart", "library lib;");
682 CompilationUnit parsedUnit = _context.parseCompilationUnit(source);
683 expect(parsedUnit, isNotNull);
684 CompilationUnit resolvedUnit =
685 _context.computeResolvableCompilationUnit(source);
686 expect(resolvedUnit, isNotNull);
687 expect(resolvedUnit, same(parsedUnit));
688 }
689
690 Future fail_computeResolvedCompilationUnitAsync() {
691 _context = contextWithCore();
692 _sourceFactory = _context.sourceFactory;
693 Source source = _addSource("/lib.dart", "library lib;");
694 // Complete all pending analysis tasks and flush the AST so that it won't
695 // be available immediately.
696 _performPendingAnalysisTasks();
697 CacheEntry entry = _context.getReadableSourceEntryOrNull(source);
698 entry.flushAstStructures();
699 bool completed = false;
700 _context
701 .computeResolvedCompilationUnitAsync(source, source)
702 .then((CompilationUnit unit) {
703 expect(unit, isNotNull);
704 completed = true;
705 });
706 return pumpEventQueue().then((_) {
707 expect(completed, isFalse);
708 _performPendingAnalysisTasks();
709 }).then((_) => pumpEventQueue()).then((_) {
710 expect(completed, isTrue);
711 });
712 }
713
714 Future fail_computeResolvedCompilationUnitAsync_afterDispose() {
715 _context = contextWithCore();
716 _sourceFactory = _context.sourceFactory;
717 Source source = _addSource("/lib.dart", "library lib;");
718 // Complete all pending analysis tasks and flush the AST so that it won't
719 // be available immediately.
720 _performPendingAnalysisTasks();
721 CacheEntry entry = _context.getReadableSourceEntryOrNull(source);
722 entry.flushAstStructures();
723 // Dispose of the context.
724 _context.dispose();
725 // Any attempt to start an asynchronous computation should return a future
726 // which completes with error.
727 CancelableFuture<CompilationUnit> future =
728 _context.computeResolvedCompilationUnitAsync(source, source);
729 bool completed = false;
730 future.then((CompilationUnit unit) {
731 fail('Future should have completed with error');
732 }, onError: (error) {
733 expect(error, new isInstanceOf<AnalysisNotScheduledError>());
734 completed = true;
735 });
736 return pumpEventQueue().then((_) {
737 expect(completed, isTrue);
738 });
739 }
740
741 Future fail_computeResolvedCompilationUnitAsync_cancel() {
742 _context = contextWithCore();
743 _sourceFactory = _context.sourceFactory;
744 Source source = _addSource("/lib.dart", "library lib;");
745 // Complete all pending analysis tasks and flush the AST so that it won't
746 // be available immediately.
747 _performPendingAnalysisTasks();
748 CacheEntry entry = _context.getReadableSourceEntryOrNull(source);
749 entry.flushAstStructures();
750 CancelableFuture<CompilationUnit> future =
751 _context.computeResolvedCompilationUnitAsync(source, source);
752 bool completed = false;
753 future.then((CompilationUnit unit) {
754 fail('Future should have been canceled');
755 }, onError: (error) {
756 expect(error, new isInstanceOf<FutureCanceledError>());
757 completed = true;
758 });
759 expect(completed, isFalse);
760 expect(_context.pendingFutureSources_forTesting, isNotEmpty);
761 future.cancel();
762 expect(_context.pendingFutureSources_forTesting, isEmpty);
763 return pumpEventQueue().then((_) {
764 expect(completed, isTrue);
765 expect(_context.pendingFutureSources_forTesting, isEmpty);
766 });
767 }
768
769 Future fail_computeResolvedCompilationUnitAsync_dispose() {
770 _context = contextWithCore();
771 _sourceFactory = _context.sourceFactory;
772 Source source = _addSource("/lib.dart", "library lib;");
773 // Complete all pending analysis tasks and flush the AST so that it won't
774 // be available immediately.
775 _performPendingAnalysisTasks();
776 CacheEntry entry = _context.getReadableSourceEntryOrNull(source);
777 entry.flushAstStructures();
778 CancelableFuture<CompilationUnit> future =
779 _context.computeResolvedCompilationUnitAsync(source, source);
780 bool completed = false;
781 future.then((CompilationUnit unit) {
782 fail('Future should have completed with error');
783 }, onError: (error) {
784 expect(error, new isInstanceOf<AnalysisNotScheduledError>());
785 completed = true;
786 });
787 expect(completed, isFalse);
788 expect(_context.pendingFutureSources_forTesting, isNotEmpty);
789 // Disposing of the context should cause all pending futures to complete
790 // with AnalysisNotScheduled, so that no clients are left hanging.
791 _context.dispose();
792 expect(_context.pendingFutureSources_forTesting, isEmpty);
793 return pumpEventQueue().then((_) {
794 expect(completed, isTrue);
795 expect(_context.pendingFutureSources_forTesting, isEmpty);
796 });
797 }
798
799 Future fail_computeResolvedCompilationUnitAsync_unrelatedLibrary() {
800 _context = contextWithCore();
801 _sourceFactory = _context.sourceFactory;
802 Source librarySource = _addSource("/lib.dart", "library lib;");
803 Source partSource = _addSource("/part.dart", "part of foo;");
804 bool completed = false;
805 _context
806 .computeResolvedCompilationUnitAsync(partSource, librarySource)
807 .then((_) {
808 // TODO(brianwilkerson) Uncomment the line below (and figure out why
809 // invoking 'fail' directly causes a failing test to fail.
810 //fail('Expected resolution to fail');
811 }, onError: (e) {
812 expect(e, new isInstanceOf<AnalysisNotScheduledError>());
813 completed = true;
814 });
815 return pumpEventQueue().then((_) {
816 expect(completed, isFalse);
817 _performPendingAnalysisTasks();
818 }).then((_) => pumpEventQueue()).then((_) {
819 expect(completed, isTrue);
820 });
821 }
822
823 void fail_extractContext() {
824 fail("Implement this");
825 }
826
827 void fail_getElement_constructor_named() {
828 Source source = _addSource("/lib.dart", r'''
829 class A {
830 A.named() {}
831 }''');
832 _analyzeAll_assertFinished();
833 LibraryElement library = _context.computeLibraryElement(source);
834 ClassElement classA = _findClass(library.definingCompilationUnit, "A");
835 ConstructorElement constructor = classA.constructors[0];
836 ElementLocation location = constructor.location;
837 Element element = _context.getElement(location);
838 expect(element, same(constructor));
839 }
840
841 void fail_getElement_constructor_unnamed() {
842 Source source = _addSource("/lib.dart", r'''
843 class A {
844 A() {}
845 }''');
846 _analyzeAll_assertFinished();
847 LibraryElement library = _context.computeLibraryElement(source);
848 ClassElement classA = _findClass(library.definingCompilationUnit, "A");
849 ConstructorElement constructor = classA.constructors[0];
850 ElementLocation location = constructor.location;
851 Element element = _context.getElement(location);
852 expect(element, same(constructor));
853 }
854
855 void fail_getElement_enum() {
856 Source source = _addSource('/test.dart', 'enum MyEnum {A, B, C}');
857 _analyzeAll_assertFinished();
858 LibraryElement library = _context.computeLibraryElement(source);
859 ClassElement myEnum = library.definingCompilationUnit.getEnum('MyEnum');
860 ElementLocation location = myEnum.location;
861 Element element = _context.getElement(location);
862 expect(element, same(myEnum));
863 }
864
865 void fail_getErrors_dart_none() {
866 Source source = _addSource("/lib.dart", "library lib;");
867 var errorInfo = _context.getErrors(source);
868 expect(errorInfo, isNotNull);
869 List<AnalysisError> errors = errorInfo.errors;
870 expect(errors, hasLength(0));
871 _context.computeErrors(source);
872 errors = errorInfo.errors;
873 expect(errors, hasLength(0));
874 }
875
876 void fail_getErrors_dart_some() {
877 Source source = _addSource("/lib.dart", "library 'lib';");
878 var errorInfo = _context.getErrors(source);
879 expect(errorInfo, isNotNull);
880 List<AnalysisError> errors = errorInfo.errors;
881 expect(errors, hasLength(0));
882 _context.computeErrors(source);
883 errors = errorInfo.errors;
884 expect(errors, hasLength(1));
885 }
886
887 void fail_getErrors_html_none() {
888 Source source = _addSource("/test.html", "<html></html>");
889 AnalysisErrorInfo errorInfo = _context.getErrors(source);
890 expect(errorInfo, isNotNull);
891 List<AnalysisError> errors = errorInfo.errors;
892 expect(errors, hasLength(0));
893 _context.computeErrors(source);
894 errors = errorInfo.errors;
895 expect(errors, hasLength(0));
896 }
897
898 void fail_getErrors_html_some() {
899 Source source = _addSource("/test.html", r'''
900 <html><head>
901 <script type='application/dart' src='test.dart'/>
902 </head></html>''');
903 AnalysisErrorInfo errorInfo = _context.getErrors(source);
904 expect(errorInfo, isNotNull);
905 List<AnalysisError> errors = errorInfo.errors;
906 expect(errors, hasLength(0));
907 _context.computeErrors(source);
908 errors = errorInfo.errors;
909 expect(errors, hasLength(1));
910 }
911
912 void fail_getHtmlElement_html() {
913 Source source = _addSource("/test.html", "<html></html>");
914 HtmlElement element = _context.getHtmlElement(source);
915 expect(element, isNull);
916 _context.computeHtmlElement(source);
917 element = _context.getHtmlElement(source);
918 expect(element, isNotNull);
919 }
920
921 void fail_getHtmlFilesReferencing_library() {
922 Source htmlSource = _addSource("/test.html", r'''
923 <html><head>
924 <script type='application/dart' src='test.dart'/>
925 <script type='application/dart' src='test.js'/>
926 </head></html>''');
927 Source librarySource = _addSource("/test.dart", "library lib;");
928 List<Source> result = _context.getHtmlFilesReferencing(librarySource);
929 expect(result, hasLength(0));
930 _context.parseHtmlUnit(htmlSource);
931 result = _context.getHtmlFilesReferencing(librarySource);
932 expect(result, hasLength(1));
933 expect(result[0], htmlSource);
934 }
935
936 void fail_getHtmlFilesReferencing_part() {
937 _context = contextWithCore();
938 _sourceFactory = _context.sourceFactory;
939 Source htmlSource = _addSource("/test.html", r'''
940 <html><head>
941 <script type='application/dart' src='test.dart'/>
942 <script type='application/dart' src='test.js'/>
943 </head></html>''');
944 Source librarySource =
945 _addSource("/test.dart", "library lib; part 'part.dart';");
946 Source partSource = _addSource("/part.dart", "part of lib;");
947 _context.computeLibraryElement(librarySource);
948 List<Source> result = _context.getHtmlFilesReferencing(partSource);
949 expect(result, hasLength(0));
950 _context.parseHtmlUnit(htmlSource);
951 result = _context.getHtmlFilesReferencing(partSource);
952 expect(result, hasLength(1));
953 expect(result[0], htmlSource);
954 }
955
956 void fail_getHtmlSources() {
957 List<Source> sources = _context.htmlSources;
958 expect(sources, hasLength(0));
959 Source source = _addSource("/test.html", "");
960 _context.computeKindOf(source);
961 sources = _context.htmlSources;
962 expect(sources, hasLength(1));
963 expect(sources[0], source);
964 }
965
966 void fail_getKindOf_html() {
967 Source source = _addSource("/test.html", "");
968 expect(_context.getKindOf(source), same(SourceKind.HTML));
969 }
970
971 void fail_getLibrariesContaining() {
972 _context = contextWithCore();
973 _sourceFactory = _context.sourceFactory;
974 Source librarySource = _addSource("/lib.dart", r'''
975 library lib;
976 part 'part.dart';''');
977 Source partSource = _addSource("/part.dart", "part of lib;");
978 _context.computeLibraryElement(librarySource);
979 List<Source> result = _context.getLibrariesContaining(librarySource);
980 expect(result, hasLength(1));
981 expect(result[0], librarySource);
982 result = _context.getLibrariesContaining(partSource);
983 expect(result, hasLength(1));
984 expect(result[0], librarySource);
985 }
986
987 void fail_getLibrariesReferencedFromHtml() {
988 _context = contextWithCore();
989 _sourceFactory = _context.sourceFactory;
990 Source htmlSource = _addSource("/test.html", r'''
991 <html><head>
992 <script type='application/dart' src='test.dart'/>
993 <script type='application/dart' src='test.js'/>
994 </head></html>''');
995 Source librarySource = _addSource("/test.dart", "library lib;");
996 _context.computeLibraryElement(librarySource);
997 _context.parseHtmlUnit(htmlSource);
998 List<Source> result = _context.getLibrariesReferencedFromHtml(htmlSource);
999 expect(result, hasLength(1));
1000 expect(result[0], librarySource);
1001 }
1002
1003 void fail_getLibraryElement() {
1004 _context = contextWithCore();
1005 _sourceFactory = _context.sourceFactory;
1006 Source source = _addSource("/test.dart", "library lib;");
1007 LibraryElement element = _context.getLibraryElement(source);
1008 expect(element, isNull);
1009 _context.computeLibraryElement(source);
1010 element = _context.getLibraryElement(source);
1011 expect(element, isNotNull);
1012 }
1013
1014 void fail_getPublicNamespace_element() {
1015 _context = contextWithCore();
1016 _sourceFactory = _context.sourceFactory;
1017 Source source = _addSource("/test.dart", "class A {}");
1018 LibraryElement library = _context.computeLibraryElement(source);
1019 expect(library, isNotNull);
1020 Namespace namespace = _context.getPublicNamespace(library);
1021 expect(namespace, isNotNull);
1022 EngineTestCase.assertInstanceOf(
1023 (obj) => obj is ClassElement, ClassElement, namespace.get("A"));
1024 }
1025
1026 void fail_getRefactoringUnsafeSources() {
1027 // not sources initially
1028 List<Source> sources = _context.refactoringUnsafeSources;
1029 expect(sources, hasLength(0));
1030 // add new source, unresolved
1031 Source source = _addSource("/test.dart", "library lib;");
1032 sources = _context.refactoringUnsafeSources;
1033 expect(sources, hasLength(1));
1034 expect(sources[0], source);
1035 // resolve source
1036 _context.computeLibraryElement(source);
1037 sources = _context.refactoringUnsafeSources;
1038 expect(sources, hasLength(0));
1039 }
1040
1041 void fail_getResolvedCompilationUnit_library() {
1042 _context = contextWithCore();
1043 _sourceFactory = _context.sourceFactory;
1044 Source source = _addSource("/lib.dart", "library libb;");
1045 LibraryElement library = _context.computeLibraryElement(source);
1046 expect(_context.getResolvedCompilationUnit(source, library), isNotNull);
1047 _context.setContents(source, "library lib;");
1048 expect(_context.getResolvedCompilationUnit(source, library), isNull);
1049 }
1050
1051 void fail_getResolvedCompilationUnit_source_dart() {
1052 _context = contextWithCore();
1053 _sourceFactory = _context.sourceFactory;
1054 Source source = _addSource("/lib.dart", "library lib;");
1055 expect(_context.getResolvedCompilationUnit2(source, source), isNull);
1056 _context.resolveCompilationUnit2(source, source);
1057 expect(_context.getResolvedCompilationUnit2(source, source), isNotNull);
1058 }
1059
1060 void fail_getResolvedHtmlUnit() {
1061 _context = contextWithCore();
1062 _sourceFactory = _context.sourceFactory;
1063 Source source = _addSource("/test.html", "<html></html>");
1064 expect(_context.getResolvedHtmlUnit(source), isNull);
1065 _context.resolveHtmlUnit(source);
1066 expect(_context.getResolvedHtmlUnit(source), isNotNull);
1067 }
1068
1069 void fail_mergeContext() {
1070 fail("Implement this");
1071 }
1072
1073 void fail_parseCompilationUnit_errors() {
1074 Source source = _addSource("/lib.dart", "library {");
1075 CompilationUnit compilationUnit = _context.parseCompilationUnit(source);
1076 expect(compilationUnit, isNotNull);
1077 var errorInfo = _context.getErrors(source);
1078 expect(errorInfo, isNotNull);
1079 List<AnalysisError> errors = errorInfo.errors;
1080 expect(errors, isNotNull);
1081 expect(errors.length > 0, isTrue);
1082 }
1083
1084 void fail_parseCompilationUnit_exception() {
1085 Source source = _addSourceWithException("/test.dart");
1086 try {
1087 _context.parseCompilationUnit(source);
1088 fail("Expected AnalysisException");
1089 } on AnalysisException {
1090 // Expected
1091 }
1092 }
1093
1094 void fail_parseCompilationUnit_noErrors() {
1095 Source source = _addSource("/lib.dart", "library lib;");
1096 CompilationUnit compilationUnit = _context.parseCompilationUnit(source);
1097 expect(compilationUnit, isNotNull);
1098 AnalysisErrorInfo errorInfo = _context.getErrors(source);
1099 expect(errorInfo, isNotNull);
1100 expect(errorInfo.errors, hasLength(0));
1101 }
1102
1103 void fail_parseCompilationUnit_nonExistentSource() {
1104 Source source =
1105 new FileBasedSource.con1(FileUtilities2.createFile("/test.dart"));
1106 try {
1107 _context.parseCompilationUnit(source);
1108 fail("Expected AnalysisException because file does not exist");
1109 } on AnalysisException {
1110 // Expected result
1111 }
1112 }
1113
1114 void fail_parseHtmlUnit_noErrors() {
1115 Source source = _addSource("/lib.html", "<html></html>");
1116 ht.HtmlUnit unit = _context.parseHtmlUnit(source);
1117 expect(unit, isNotNull);
1118 }
1119
1120 void fail_parseHtmlUnit_resolveDirectives() {
1121 Source libSource = _addSource("/lib.dart", r'''
1122 library lib;
1123 class ClassA {}''');
1124 Source source = _addSource("/lib.html", r'''
1125 <html>
1126 <head>
1127 <script type='application/dart'>
1128 import 'lib.dart';
1129 ClassA v = null;
1130 </script>
1131 </head>
1132 <body>
1133 </body>
1134 </html>''');
1135 ht.HtmlUnit unit = _context.parseHtmlUnit(source);
1136 expect(unit, isNotNull);
1137 // import directive should be resolved
1138 ht.XmlTagNode htmlNode = unit.tagNodes[0];
1139 ht.XmlTagNode headNode = htmlNode.tagNodes[0];
1140 ht.HtmlScriptTagNode scriptNode = headNode.tagNodes[0];
1141 CompilationUnit script = scriptNode.script;
1142 ImportDirective importNode = script.directives[0] as ImportDirective;
1143 expect(importNode.uriContent, isNotNull);
1144 expect(importNode.source, libSource);
1145 }
1146
1147 void fail_performAnalysisTask_addPart() {
1148 Source libSource = _addSource("/lib.dart", r'''
1149 library lib;
1150 part 'part.dart';''');
1151 // run all tasks without part
1152 _analyzeAll_assertFinished();
1153 // add part and run all tasks
1154 Source partSource = _addSource("/part.dart", r'''
1155 part of lib;
1156 ''');
1157 _analyzeAll_assertFinished();
1158 // "libSource" should be here
1159 List<Source> librariesWithPart =
1160 _context.getLibrariesContaining(partSource);
1161 expect(librariesWithPart, unorderedEquals([libSource]));
1162 }
1163
1164 void fail_performAnalysisTask_changeLibraryContents() {
1165 Source libSource =
1166 _addSource("/test.dart", "library lib; part 'test-part.dart';");
1167 Source partSource = _addSource("/test-part.dart", "part of lib;");
1168 _analyzeAll_assertFinished();
1169 expect(
1170 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1171 reason: "library resolved 1");
1172 expect(
1173 _context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1174 reason: "part resolved 1");
1175 // update and analyze #1
1176 _context.setContents(libSource, "library lib;");
1177 expect(_context.getResolvedCompilationUnit2(libSource, libSource), isNull,
1178 reason: "library changed 2");
1179 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1180 reason: "part changed 2");
1181 _analyzeAll_assertFinished();
1182 expect(
1183 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1184 reason: "library resolved 2");
1185 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1186 reason: "part resolved 2");
1187 // update and analyze #2
1188 _context.setContents(libSource, "library lib; part 'test-part.dart';");
1189 expect(_context.getResolvedCompilationUnit2(libSource, libSource), isNull,
1190 reason: "library changed 3");
1191 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1192 reason: "part changed 3");
1193 _analyzeAll_assertFinished();
1194 expect(
1195 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1196 reason: "library resolved 2");
1197 expect(
1198 _context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1199 reason: "part resolved 3");
1200 }
1201
1202 void fail_performAnalysisTask_changeLibraryThenPartContents() {
1203 Source libSource =
1204 _addSource("/test.dart", "library lib; part 'test-part.dart';");
1205 Source partSource = _addSource("/test-part.dart", "part of lib;");
1206 _analyzeAll_assertFinished();
1207 expect(
1208 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1209 reason: "library resolved 1");
1210 expect(
1211 _context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1212 reason: "part resolved 1");
1213 // update and analyze #1
1214 _context.setContents(libSource, "library lib;");
1215 expect(_context.getResolvedCompilationUnit2(libSource, libSource), isNull,
1216 reason: "library changed 2");
1217 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1218 reason: "part changed 2");
1219 _analyzeAll_assertFinished();
1220 expect(
1221 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1222 reason: "library resolved 2");
1223 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1224 reason: "part resolved 2");
1225 // update and analyze #2
1226 _context.setContents(partSource, "part of lib; // 1");
1227 // Assert that changing the part's content does not effect the library
1228 // now that it is no longer part of that library
1229 expect(
1230 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1231 reason: "library changed 3");
1232 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1233 reason: "part changed 3");
1234 _analyzeAll_assertFinished();
1235 expect(
1236 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1237 reason: "library resolved 3");
1238 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1239 reason: "part resolved 3");
1240 }
1241
1242 void fail_performAnalysisTask_changePartContents_makeItAPart() {
1243 Source libSource = _addSource("/lib.dart", r'''
1244 library lib;
1245 part 'part.dart';
1246 void f(x) {}''');
1247 Source partSource = _addSource("/part.dart", "void g() { f(null); }");
1248 _analyzeAll_assertFinished();
1249 expect(
1250 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1251 reason: "library resolved 1");
1252 expect(
1253 _context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1254 reason: "part resolved 1");
1255 // update and analyze
1256 _context.setContents(partSource, r'''
1257 part of lib;
1258 void g() { f(null); }''');
1259 expect(_context.getResolvedCompilationUnit2(libSource, libSource), isNull,
1260 reason: "library changed 2");
1261 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1262 reason: "part changed 2");
1263 _analyzeAll_assertFinished();
1264 expect(
1265 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1266 reason: "library resolved 2");
1267 expect(
1268 _context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1269 reason: "part resolved 2");
1270 expect(_context.getErrors(libSource).errors, hasLength(0));
1271 expect(_context.getErrors(partSource).errors, hasLength(0));
1272 }
1273
1274 /**
1275 * https://code.google.com/p/dart/issues/detail?id=12424
1276 */
1277 void fail_performAnalysisTask_changePartContents_makeItNotPart() {
1278 Source libSource = _addSource("/lib.dart", r'''
1279 library lib;
1280 part 'part.dart';
1281 void f(x) {}''');
1282 Source partSource = _addSource("/part.dart", r'''
1283 part of lib;
1284 void g() { f(null); }''');
1285 _analyzeAll_assertFinished();
1286 expect(_context.getErrors(libSource).errors, hasLength(0));
1287 expect(_context.getErrors(partSource).errors, hasLength(0));
1288 // Remove 'part' directive, which should make "f(null)" an error.
1289 _context.setContents(partSource, r'''
1290 //part of lib;
1291 void g() { f(null); }''');
1292 _analyzeAll_assertFinished();
1293 expect(_context.getErrors(libSource).errors.length != 0, isTrue);
1294 }
1295
1296 void fail_performAnalysisTask_changePartContents_noSemanticChanges() {
1297 Source libSource =
1298 _addSource("/test.dart", "library lib; part 'test-part.dart';");
1299 Source partSource = _addSource("/test-part.dart", "part of lib;");
1300 _analyzeAll_assertFinished();
1301 expect(
1302 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1303 reason: "library resolved 1");
1304 expect(
1305 _context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1306 reason: "part resolved 1");
1307 // update and analyze #1
1308 _context.setContents(partSource, "part of lib; // 1");
1309 expect(_context.getResolvedCompilationUnit2(libSource, libSource), isNull,
1310 reason: "library changed 2");
1311 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1312 reason: "part changed 2");
1313 _analyzeAll_assertFinished();
1314 expect(
1315 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1316 reason: "library resolved 2");
1317 expect(
1318 _context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1319 reason: "part resolved 2");
1320 // update and analyze #2
1321 _context.setContents(partSource, "part of lib; // 12");
1322 expect(_context.getResolvedCompilationUnit2(libSource, libSource), isNull,
1323 reason: "library changed 3");
1324 expect(_context.getResolvedCompilationUnit2(partSource, libSource), isNull,
1325 reason: "part changed 3");
1326 _analyzeAll_assertFinished();
1327 expect(
1328 _context.getResolvedCompilationUnit2(libSource, libSource), isNotNull,
1329 reason: "library resolved 3");
1330 expect(
1331 _context.getResolvedCompilationUnit2(partSource, libSource), isNotNull,
1332 reason: "part resolved 3");
1333 }
1334
1335 void fail_performAnalysisTask_getContentException_dart() {
1336 // add source that throw an exception on "get content"
1337 Source source = new _Source_getContent_throwException('test.dart');
1338 {
1339 ChangeSet changeSet = new ChangeSet();
1340 changeSet.addedSource(source);
1341 _context.applyChanges(changeSet);
1342 }
1343 // prepare errors
1344 _analyzeAll_assertFinished();
1345 List<AnalysisError> errors = _context.getErrors(source).errors;
1346 // validate errors
1347 expect(errors, hasLength(1));
1348 AnalysisError error = errors[0];
1349 expect(error.source, same(source));
1350 expect(error.errorCode, ScannerErrorCode.UNABLE_GET_CONTENT);
1351 }
1352
1353 void fail_performAnalysisTask_getContentException_html() {
1354 // add source that throw an exception on "get content"
1355 Source source = new _Source_getContent_throwException('test.html');
1356 {
1357 ChangeSet changeSet = new ChangeSet();
1358 changeSet.addedSource(source);
1359 _context.applyChanges(changeSet);
1360 }
1361 // prepare errors
1362 _analyzeAll_assertFinished();
1363 List<AnalysisError> errors = _context.getErrors(source).errors;
1364 // validate errors
1365 expect(errors, hasLength(1));
1366 AnalysisError error = errors[0];
1367 expect(error.source, same(source));
1368 expect(error.errorCode, ScannerErrorCode.UNABLE_GET_CONTENT);
1369 }
1370
1371 void fail_performAnalysisTask_importedLibraryAdd() {
1372 Source libASource =
1373 _addSource("/libA.dart", "library libA; import 'libB.dart';");
1374 _analyzeAll_assertFinished();
1375 expect(
1376 _context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
1377 reason: "libA resolved 1");
1378 expect(_hasAnalysisErrorWithErrorSeverity(_context.getErrors(libASource)),
1379 isTrue, reason: "libA has an error");
1380 // add libB.dart and analyze
1381 Source libBSource = _addSource("/libB.dart", "library libB;");
1382 _analyzeAll_assertFinished();
1383 expect(
1384 _context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
1385 reason: "libA resolved 2");
1386 expect(
1387 _context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull,
1388 reason: "libB resolved 2");
1389 expect(!_hasAnalysisErrorWithErrorSeverity(_context.getErrors(libASource)),
1390 isTrue, reason: "libA doesn't have errors");
1391 }
1392
1393 void fail_performAnalysisTask_importedLibraryAdd_html() {
1394 Source htmlSource = _addSource("/page.html", r'''
1395 <html><body><script type="application/dart">
1396 import '/libB.dart';
1397 main() {print('hello dart');}
1398 </script></body></html>''');
1399 _analyzeAll_assertFinished();
1400 expect(_context.getResolvedHtmlUnit(htmlSource), isNotNull,
1401 reason: "htmlUnit resolved 1");
1402 expect(_hasAnalysisErrorWithErrorSeverity(_context.getErrors(htmlSource)),
1403 isTrue, reason: "htmlSource has an error");
1404 // add libB.dart and analyze
1405 Source libBSource = _addSource("/libB.dart", "library libB;");
1406 _analyzeAll_assertFinished();
1407 expect(_context.getResolvedHtmlUnit(htmlSource), isNotNull,
1408 reason: "htmlUnit resolved 1");
1409 expect(
1410 _context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull,
1411 reason: "libB resolved 2");
1412 // TODO (danrubel) commented out to fix red bots
1413 // AnalysisErrorInfo errors = _context.getErrors(htmlSource);
1414 // expect(
1415 // !_hasAnalysisErrorWithErrorSeverity(errors),
1416 // isTrue,
1417 // reason: "htmlSource doesn't have errors");
1418 }
1419
1420 void fail_performAnalysisTask_importedLibraryDelete() {
1421 Source libASource =
1422 _addSource("/libA.dart", "library libA; import 'libB.dart';");
1423 Source libBSource = _addSource("/libB.dart", "library libB;");
1424 _analyzeAll_assertFinished();
1425 expect(
1426 _context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
1427 reason: "libA resolved 1");
1428 expect(
1429 _context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull,
1430 reason: "libB resolved 1");
1431 expect(!_hasAnalysisErrorWithErrorSeverity(_context.getErrors(libASource)),
1432 isTrue, reason: "libA doesn't have errors");
1433 // remove libB.dart content and analyze
1434 _context.setContents(libBSource, null);
1435 _analyzeAll_assertFinished();
1436 expect(
1437 _context.getResolvedCompilationUnit2(libASource, libASource), isNotNull,
1438 reason: "libA resolved 2");
1439 expect(_hasAnalysisErrorWithErrorSeverity(_context.getErrors(libASource)),
1440 isTrue, reason: "libA has an error");
1441 }
1442
1443 void fail_performAnalysisTask_importedLibraryDelete_html() {
1444 // NOTE: This was failing before converting to the new task model.
1445 Source htmlSource = _addSource("/page.html", r'''
1446 <html><body><script type="application/dart">
1447 import 'libB.dart';
1448 main() {print('hello dart');}
1449 </script></body></html>''');
1450 Source libBSource = _addSource("/libB.dart", "library libB;");
1451 _analyzeAll_assertFinished();
1452 expect(_context.getResolvedHtmlUnit(htmlSource), isNotNull,
1453 reason: "htmlUnit resolved 1");
1454 expect(
1455 _context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull,
1456 reason: "libB resolved 1");
1457 expect(!_hasAnalysisErrorWithErrorSeverity(_context.getErrors(htmlSource)),
1458 isTrue, reason: "htmlSource doesn't have errors");
1459 // remove libB.dart content and analyze
1460 _context.setContents(libBSource, null);
1461 _analyzeAll_assertFinished();
1462 expect(_context.getResolvedHtmlUnit(htmlSource), isNotNull,
1463 reason: "htmlUnit resolved 1");
1464 AnalysisErrorInfo errors = _context.getErrors(htmlSource);
1465 expect(_hasAnalysisErrorWithErrorSeverity(errors), isTrue,
1466 reason: "htmlSource has an error");
1467 }
1468
1469 void fail_performAnalysisTask_IOException() {
1470 TestSource source = _addSourceWithException2("/test.dart", "library test;");
1471 int oldTimestamp = _context.getModificationStamp(source);
1472 source.generateExceptionOnRead = false;
1473 _analyzeAll_assertFinished();
1474 expect(source.readCount, 1);
1475 source.generateExceptionOnRead = true;
1476 do {
1477 _changeSource(source, "");
1478 // Ensure that the timestamp differs,
1479 // so that analysis engine notices the change
1480 } while (oldTimestamp == _context.getModificationStamp(source));
1481 _analyzeAll_assertFinished();
1482 expect(source.readCount, 2);
1483 }
1484
1485 void fail_performAnalysisTask_missingPart() {
1486 Source source =
1487 _addSource("/test.dart", "library lib; part 'no-such-file.dart';");
1488 _analyzeAll_assertFinished();
1489 expect(_context.getLibraryElement(source), isNotNull,
1490 reason: "performAnalysisTask failed to compute an element model");
1491 }
1492
1493 void fail_recordLibraryElements() {
1494 fail("Implement this");
1495 }
1496
1497 void fail_resolveCompilationUnit_import_relative() {
1498 _context = contextWithCore();
1499 Source sourceA =
1500 _addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}");
1501 _addSource("/libB.dart", "library libB; class B{}");
1502 CompilationUnit compilationUnit =
1503 _context.resolveCompilationUnit2(sourceA, sourceA);
1504 expect(compilationUnit, isNotNull);
1505 LibraryElement library = compilationUnit.element.library;
1506 List<LibraryElement> importedLibraries = library.importedLibraries;
1507 assertNamedElements(importedLibraries, ["dart.core", "libB"]);
1508 List<LibraryElement> visibleLibraries = library.visibleLibraries;
1509 assertNamedElements(visibleLibraries, ["dart.core", "libA", "libB"]);
1510 }
1511
1512 void fail_resolveCompilationUnit_import_relative_cyclic() {
1513 _context = contextWithCore();
1514 Source sourceA =
1515 _addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}");
1516 _addSource("/libB.dart", "library libB; import 'libA.dart'; class B{}");
1517 CompilationUnit compilationUnit =
1518 _context.resolveCompilationUnit2(sourceA, sourceA);
1519 expect(compilationUnit, isNotNull);
1520 LibraryElement library = compilationUnit.element.library;
1521 List<LibraryElement> importedLibraries = library.importedLibraries;
1522 assertNamedElements(importedLibraries, ["dart.core", "libB"]);
1523 List<LibraryElement> visibleLibraries = library.visibleLibraries;
1524 assertNamedElements(visibleLibraries, ["dart.core", "libA", "libB"]);
1525 }
1526
1527 void fail_resolveCompilationUnit_library() {
1528 _context = contextWithCore();
1529 _sourceFactory = _context.sourceFactory;
1530 Source source = _addSource("/lib.dart", "library lib;");
1531 LibraryElement library = _context.computeLibraryElement(source);
1532 CompilationUnit compilationUnit =
1533 _context.resolveCompilationUnit(source, library);
1534 expect(compilationUnit, isNotNull);
1535 expect(compilationUnit.element, isNotNull);
1536 }
1537
1538 void fail_resolveCompilationUnit_source() {
1539 _context = contextWithCore();
1540 _sourceFactory = _context.sourceFactory;
1541 Source source = _addSource("/lib.dart", "library lib;");
1542 CompilationUnit compilationUnit =
1543 _context.resolveCompilationUnit2(source, source);
1544 expect(compilationUnit, isNotNull);
1545 }
1546
1547 void fail_resolveHtmlUnit() {
1548 Source source = _addSource("/lib.html", "<html></html>");
1549 ht.HtmlUnit unit = _context.resolveHtmlUnit(source);
1550 expect(unit, isNotNull);
1551 }
1552
1553 void fail_setAnalysisOptions_reduceAnalysisPriorityOrder() {
1554 AnalysisOptionsImpl options =
1555 new AnalysisOptionsImpl.con1(_context.analysisOptions);
1556 List<Source> sources = new List<Source>();
1557 for (int index = 0; index < options.cacheSize; index++) {
1558 sources.add(_addSource("/lib.dart$index", ""));
1559 }
1560 _context.analysisPriorityOrder = sources;
1561 int oldPriorityOrderSize = _getPriorityOrder(_context).length;
1562 options.cacheSize = options.cacheSize - 10;
1563 _context.analysisOptions = options;
1564 expect(oldPriorityOrderSize > _getPriorityOrder(_context).length, isTrue);
1565 }
1566
1567 void fail_setAnalysisPriorityOrder_lessThanCacheSize() {
1568 AnalysisOptions options = _context.analysisOptions;
1569 List<Source> sources = new List<Source>();
1570 for (int index = 0; index < options.cacheSize; index++) {
1571 sources.add(_addSource("/lib.dart$index", ""));
1572 }
1573 _context.analysisPriorityOrder = sources;
1574 expect(options.cacheSize > _getPriorityOrder(_context).length, isTrue);
1575 }
1576
1577 Future fail_setChangedContents_libraryWithPart() {
1578 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
1579 options.incremental = true;
1580 _context = new AnalysisContextForTests();
1581 _context.analysisOptions = options;
1582 SourcesChangedListener listener = new SourcesChangedListener();
1583 _context.onSourcesChanged.listen(listener.onData);
1584 _sourceFactory = _context.sourceFactory;
1585 String oldCode = r'''
1586 library lib;
1587 part 'part.dart';
1588 int a = 0;''';
1589 Source librarySource = _addSource("/lib.dart", oldCode);
1590 String partContents = r'''
1591 part of lib;
1592 int b = a;''';
1593 Source partSource = _addSource("/part.dart", partContents);
1594 LibraryElement element = _context.computeLibraryElement(librarySource);
1595 CompilationUnit unit =
1596 _context.getResolvedCompilationUnit(librarySource, element);
1597 expect(unit, isNotNull);
1598 int offset = oldCode.indexOf("int a") + 4;
1599 String newCode = r'''
1600 library lib;
1601 part 'part.dart';
1602 int ya = 0;''';
1603 expect(_getIncrementalAnalysisCache(_context), isNull);
1604 _context.setChangedContents(librarySource, newCode, offset, 0, 1);
1605 expect(_context.getContents(librarySource).data, newCode);
1606 IncrementalAnalysisCache incrementalCache =
1607 _getIncrementalAnalysisCache(_context);
1608 expect(incrementalCache.librarySource, librarySource);
1609 expect(incrementalCache.resolvedUnit, same(unit));
1610 expect(_context.getResolvedCompilationUnit2(partSource, librarySource),
1611 isNull);
1612 expect(incrementalCache.newContents, newCode);
1613 return pumpEventQueue().then((_) {
1614 listener.assertEvent(wereSourcesAdded: true);
1615 listener.assertEvent(changedSources: [librarySource]);
1616 listener.assertEvent(wereSourcesAdded: true);
1617 listener.assertEvent(changedSources: [partSource]);
1618 listener.assertEvent(changedSources: [librarySource]);
1619 listener.assertNoMoreEvents();
1620 });
1621 }
1622
1623 void fail_setContents_unchanged_consistentModificationTime() {
1624 String contents = "// foo";
1625 Source source = _addSource("/test.dart", contents);
1626 // do all, no tasks
1627 _analyzeAll_assertFinished();
1628 {
1629 AnalysisResult result = _context.performAnalysisTask();
1630 expect(result.changeNotices, isNull);
1631 }
1632 // set the same contents, still no tasks
1633 _context.setContents(source, contents);
1634 {
1635 AnalysisResult result = _context.performAnalysisTask();
1636 expect(result.changeNotices, isNull);
1637 }
1638 }
1639
1640 void fail_unreadableSource() {
1641 _context = contextWithCore();
1642 _sourceFactory = _context.sourceFactory;
1643 Source test1 = _addSource("/test1.dart", r'''
1644 import 'test2.dart';
1645 library test1;''');
1646 Source test2 = _addSource("/test2.dart", r'''
1647 import 'test1.dart';
1648 import 'test3.dart';
1649 library test2;''');
1650 Source test3 = _addSourceWithException("/test3.dart");
1651 _analyzeAll_assertFinished();
1652 // test1 and test2 should have been successfully analyzed
1653 // despite the fact that test3 couldn't be read.
1654 expect(_context.computeLibraryElement(test1), isNotNull);
1655 expect(_context.computeLibraryElement(test2), isNotNull);
1656 expect(_context.computeLibraryElement(test3), isNull);
1657 }
1658
1659 @override
1660 void setUp() {
1661 EnginePlugin enginePlugin = AnalysisEngine.instance.enginePlugin;
1662 if (enginePlugin.taskExtensionPoint == null) {
1663 ExtensionManager manager = new ExtensionManager();
1664 manager.processPlugins([enginePlugin]);
1665 }
1666
1667 _context = new AnalysisContextImpl();
1668 _sourceFactory = new SourceFactory([
1669 new DartUriResolver(DirectoryBasedDartSdk.defaultSdk),
1670 new FileUriResolver()
1671 ]);
1672 _context.sourceFactory = _sourceFactory;
1673 AnalysisOptionsImpl options =
1674 new AnalysisOptionsImpl.con1(_context.analysisOptions);
1675 options.cacheSize = 256;
1676 _context.analysisOptions = options;
1677 }
1678
1679 @override
1680 void tearDown() {
1681 _context = null;
1682 _sourceFactory = null;
1683 super.tearDown();
1684 }
1685
1686 Future test_applyChanges_add() {
1687 SourcesChangedListener listener = new SourcesChangedListener();
1688 _context.onSourcesChanged.listen(listener.onData);
1689 expect(_context.sourcesNeedingProcessing.isEmpty, isTrue);
1690 Source source =
1691 new FileBasedSource.con1(FileUtilities2.createFile("/test.dart"));
1692 ChangeSet changeSet = new ChangeSet();
1693 changeSet.addedSource(source);
1694 _context.applyChanges(changeSet);
1695 expect(_context.sourcesNeedingProcessing.contains(source), isTrue);
1696 return pumpEventQueue().then((_) {
1697 listener.assertEvent(wereSourcesAdded: true);
1698 listener.assertNoMoreEvents();
1699 });
1700 }
1701
1702 Future test_applyChanges_change() {
1703 SourcesChangedListener listener = new SourcesChangedListener();
1704 _context.onSourcesChanged.listen(listener.onData);
1705 expect(_context.sourcesNeedingProcessing.isEmpty, isTrue);
1706 Source source =
1707 new FileBasedSource.con1(FileUtilities2.createFile("/test.dart"));
1708 ChangeSet changeSet1 = new ChangeSet();
1709 changeSet1.addedSource(source);
1710 _context.applyChanges(changeSet1);
1711 expect(_context.sourcesNeedingProcessing.contains(source), isTrue);
1712 Source source2 =
1713 new FileBasedSource.con1(FileUtilities2.createFile("/test2.dart"));
1714 ChangeSet changeSet2 = new ChangeSet();
1715 changeSet2.addedSource(source2);
1716 changeSet2.changedSource(source);
1717 _context.applyChanges(changeSet2);
1718 return pumpEventQueue().then((_) {
1719 listener.assertEvent(wereSourcesAdded: true);
1720 listener.assertEvent(wereSourcesAdded: true, changedSources: [source]);
1721 listener.assertNoMoreEvents();
1722 });
1723 }
1724
1725 Future test_applyChanges_change_content() {
1726 SourcesChangedListener listener = new SourcesChangedListener();
1727 _context.onSourcesChanged.listen(listener.onData);
1728 expect(_context.sourcesNeedingProcessing.isEmpty, isTrue);
1729 Source source =
1730 new FileBasedSource.con1(FileUtilities2.createFile("/test.dart"));
1731 ChangeSet changeSet1 = new ChangeSet();
1732 changeSet1.addedSource(source);
1733 _context.applyChanges(changeSet1);
1734 expect(_context.sourcesNeedingProcessing.contains(source), isTrue);
1735 Source source2 =
1736 new FileBasedSource.con1(FileUtilities2.createFile("/test2.dart"));
1737 ChangeSet changeSet2 = new ChangeSet();
1738 changeSet2.addedSource(source2);
1739 changeSet2.changedContent(source, 'library test;');
1740 _context.applyChanges(changeSet2);
1741 return pumpEventQueue().then((_) {
1742 listener.assertEvent(wereSourcesAdded: true);
1743 listener.assertEvent(wereSourcesAdded: true, changedSources: [source]);
1744 listener.assertNoMoreEvents();
1745 });
1746 }
1747
1748 Future test_applyChanges_change_range() {
1749 SourcesChangedListener listener = new SourcesChangedListener();
1750 _context.onSourcesChanged.listen(listener.onData);
1751 expect(_context.sourcesNeedingProcessing.isEmpty, isTrue);
1752 Source source =
1753 new FileBasedSource.con1(FileUtilities2.createFile("/test.dart"));
1754 ChangeSet changeSet1 = new ChangeSet();
1755 changeSet1.addedSource(source);
1756 _context.applyChanges(changeSet1);
1757 expect(_context.sourcesNeedingProcessing.contains(source), isTrue);
1758 Source source2 =
1759 new FileBasedSource.con1(FileUtilities2.createFile("/test2.dart"));
1760 ChangeSet changeSet2 = new ChangeSet();
1761 changeSet2.addedSource(source2);
1762 changeSet2.changedRange(source, 'library test;', 0, 0, 13);
1763 _context.applyChanges(changeSet2);
1764 return pumpEventQueue().then((_) {
1765 listener.assertEvent(wereSourcesAdded: true);
1766 listener.assertEvent(wereSourcesAdded: true, changedSources: [source]);
1767 listener.assertNoMoreEvents();
1768 });
1769 }
1770
1771 void test_computeDocumentationComment_null() {
1772 expect(_context.computeDocumentationComment(null), isNull);
1773 }
1774
1775 void test_computeExportedLibraries_none() {
1776 Source source = _addSource("/test.dart", "library test;");
1777 expect(_context.computeExportedLibraries(source), hasLength(0));
1778 }
1779
1780 void test_computeExportedLibraries_some() {
1781 // addSource("/lib1.dart", "library lib1;");
1782 // addSource("/lib2.dart", "library lib2;");
1783 Source source = _addSource(
1784 "/test.dart", "library test; export 'lib1.dart'; export 'lib2.dart';");
1785 expect(_context.computeExportedLibraries(source), hasLength(2));
1786 }
1787
1788 void test_computeHtmlElement_nonHtml() {
1789 Source source = _addSource("/test.dart", "library test;");
1790 expect(_context.computeHtmlElement(source), isNull);
1791 }
1792
1793 void test_computeKindOf_library() {
1794 Source source = _addSource("/test.dart", "library lib;");
1795 expect(_context.computeKindOf(source), same(SourceKind.LIBRARY));
1796 }
1797
1798 void test_computeKindOf_libraryAndPart() {
1799 Source source = _addSource("/test.dart", "library lib; part of lib;");
1800 expect(_context.computeKindOf(source), same(SourceKind.LIBRARY));
1801 }
1802
1803 void test_computeKindOf_part() {
1804 Source source = _addSource("/test.dart", "part of lib;");
1805 expect(_context.computeKindOf(source), same(SourceKind.PART));
1806 }
1807
1808 void test_computeLineInfo_dart() {
1809 Source source = _addSource("/test.dart", r'''
1810 library lib;
1811
1812 main() {}''');
1813 LineInfo info = _context.computeLineInfo(source);
1814 expect(info, isNotNull);
1815 }
1816
1817 void test_computeLineInfo_html() {
1818 Source source = _addSource("/test.html", r'''
1819 <html>
1820 <body>
1821 <h1>A</h1>
1822 </body>
1823 </html>''');
1824 LineInfo info = _context.computeLineInfo(source);
1825 expect(info, isNotNull);
1826 }
1827
1828 void test_dispose() {
1829 expect(_context.isDisposed, isFalse);
1830 _context.dispose();
1831 expect(_context.isDisposed, isTrue);
1832 }
1833
1834 void test_exists_false() {
1835 TestSource source = new TestSource();
1836 source.exists2 = false;
1837 expect(_context.exists(source), isFalse);
1838 }
1839
1840 void test_exists_null() {
1841 expect(_context.exists(null), isFalse);
1842 }
1843
1844 void test_exists_overridden() {
1845 Source source = new TestSource();
1846 _context.setContents(source, "");
1847 expect(_context.exists(source), isTrue);
1848 }
1849
1850 void test_exists_true() {
1851 expect(_context.exists(new AnalysisContextImplTest_Source_exists_true()),
1852 isTrue);
1853 }
1854
1855 void test_getAnalysisOptions() {
1856 expect(_context.analysisOptions, isNotNull);
1857 }
1858
1859 void test_getContents_fromSource() {
1860 String content = "library lib;";
1861 TimestampedData<String> contents =
1862 _context.getContents(new TestSource('/test.dart', content));
1863 expect(contents.data.toString(), content);
1864 }
1865
1866 void test_getContents_overridden() {
1867 String content = "library lib;";
1868 Source source = new TestSource();
1869 _context.setContents(source, content);
1870 TimestampedData<String> contents = _context.getContents(source);
1871 expect(contents.data.toString(), content);
1872 }
1873
1874 void test_getContents_unoverridden() {
1875 String content = "library lib;";
1876 Source source = new TestSource('/test.dart', content);
1877 _context.setContents(source, "part of lib;");
1878 _context.setContents(source, null);
1879 TimestampedData<String> contents = _context.getContents(source);
1880 expect(contents.data.toString(), content);
1881 }
1882
1883 void test_getDeclaredVariables() {
1884 _context = contextWithCore();
1885 expect(_context.declaredVariables, isNotNull);
1886 }
1887
1888 void test_getElement() {
1889 _context = contextWithCore();
1890 _sourceFactory = _context.sourceFactory;
1891 LibraryElement core =
1892 _context.computeLibraryElement(_sourceFactory.forUri("dart:core"));
1893 expect(core, isNotNull);
1894 ClassElement classObject =
1895 _findClass(core.definingCompilationUnit, "Object");
1896 expect(classObject, isNotNull);
1897 ElementLocation location = classObject.location;
1898 Element element = _context.getElement(location);
1899 expect(element, same(classObject));
1900 }
1901
1902 void test_getHtmlElement_dart() {
1903 Source source = _addSource("/test.dart", "");
1904 expect(_context.getHtmlElement(source), isNull);
1905 expect(_context.computeHtmlElement(source), isNull);
1906 expect(_context.getHtmlElement(source), isNull);
1907 }
1908
1909 void test_getHtmlFilesReferencing_html() {
1910 _context = contextWithCore();
1911 _sourceFactory = _context.sourceFactory;
1912 Source htmlSource = _addSource("/test.html", r'''
1913 <html><head>
1914 <script type='application/dart' src='test.dart'/>
1915 <script type='application/dart' src='test.js'/>
1916 </head></html>''');
1917 Source librarySource = _addSource("/test.dart", "library lib;");
1918 Source secondHtmlSource = _addSource("/test.html", "<html></html>");
1919 _context.computeLibraryElement(librarySource);
1920 List<Source> result = _context.getHtmlFilesReferencing(secondHtmlSource);
1921 expect(result, hasLength(0));
1922 _context.parseHtmlUnit(htmlSource);
1923 result = _context.getHtmlFilesReferencing(secondHtmlSource);
1924 expect(result, hasLength(0));
1925 }
1926
1927 void test_getKindOf_library() {
1928 Source source = _addSource("/test.dart", "library lib;");
1929 expect(_context.getKindOf(source), same(SourceKind.UNKNOWN));
1930 _context.computeKindOf(source);
1931 expect(_context.getKindOf(source), same(SourceKind.LIBRARY));
1932 }
1933
1934 void test_getKindOf_part() {
1935 Source source = _addSource("/test.dart", "part of lib;");
1936 expect(_context.getKindOf(source), same(SourceKind.UNKNOWN));
1937 _context.computeKindOf(source);
1938 expect(_context.getKindOf(source), same(SourceKind.PART));
1939 }
1940
1941 void test_getKindOf_unknown() {
1942 Source source = _addSource("/test.css", "");
1943 expect(_context.getKindOf(source), same(SourceKind.UNKNOWN));
1944 }
1945
1946 void test_getLaunchableClientLibrarySources() {
1947 _context = contextWithCore();
1948 _sourceFactory = _context.sourceFactory;
1949 List<Source> sources = _context.launchableClientLibrarySources;
1950 expect(sources, hasLength(0));
1951 Source source = _addSource("/test.dart", r'''
1952 import 'dart:html';
1953 main() {}''');
1954 _context.computeLibraryElement(source);
1955 sources = _context.launchableClientLibrarySources;
1956 expect(sources, hasLength(1));
1957 }
1958
1959 void test_getLaunchableServerLibrarySources() {
1960 _context = contextWithCore();
1961 _sourceFactory = _context.sourceFactory;
1962 List<Source> sources = _context.launchableServerLibrarySources;
1963 expect(sources, hasLength(0));
1964 Source source = _addSource("/test.dart", "main() {}");
1965 _context.computeLibraryElement(source);
1966 sources = _context.launchableServerLibrarySources;
1967 expect(sources, hasLength(1));
1968 }
1969
1970 void test_getLibrariesDependingOn() {
1971 _context = contextWithCore();
1972 _sourceFactory = _context.sourceFactory;
1973 Source libASource = _addSource("/libA.dart", "library libA;");
1974 _addSource("/libB.dart", "library libB;");
1975 Source lib1Source = _addSource("/lib1.dart", r'''
1976 library lib1;
1977 import 'libA.dart';
1978 export 'libB.dart';''');
1979 Source lib2Source = _addSource("/lib2.dart", r'''
1980 library lib2;
1981 import 'libB.dart';
1982 export 'libA.dart';''');
1983 _context.computeLibraryElement(lib1Source);
1984 _context.computeLibraryElement(lib2Source);
1985 List<Source> result = _context.getLibrariesDependingOn(libASource);
1986 expect(result, unorderedEquals([lib1Source, lib2Source]));
1987 }
1988
1989 void test_getLibrariesReferencedFromHtml_no() {
1990 _context = contextWithCore();
1991 _sourceFactory = _context.sourceFactory;
1992 Source htmlSource = _addSource("/test.html", r'''
1993 <html><head>
1994 <script type='application/dart' src='test.js'/>
1995 </head></html>''');
1996 _addSource("/test.dart", "library lib;");
1997 _context.parseHtmlUnit(htmlSource);
1998 List<Source> result = _context.getLibrariesReferencedFromHtml(htmlSource);
1999 expect(result, hasLength(0));
2000 }
2001
2002 void test_getLibrarySources() {
2003 List<Source> sources = _context.librarySources;
2004 int originalLength = sources.length;
2005 Source source = _addSource("/test.dart", "library lib;");
2006 _context.computeKindOf(source);
2007 sources = _context.librarySources;
2008 expect(sources, hasLength(originalLength + 1));
2009 for (Source returnedSource in sources) {
2010 if (returnedSource == source) {
2011 return;
2012 }
2013 }
2014 fail("The added source was not in the list of library sources");
2015 }
2016
2017 void test_getLineInfo() {
2018 Source source = _addSource("/test.dart", r'''
2019 library lib;
2020
2021 main() {}''');
2022 LineInfo info = _context.getLineInfo(source);
2023 expect(info, isNull);
2024 _context.parseCompilationUnit(source);
2025 info = _context.getLineInfo(source);
2026 expect(info, isNotNull);
2027 }
2028
2029 void test_getModificationStamp_fromSource() {
2030 int stamp = 42;
2031 expect(_context.getModificationStamp(
2032 new AnalysisContextImplTest_Source_getModificationStamp_fromSource(
2033 stamp)), stamp);
2034 }
2035
2036 void test_getModificationStamp_overridden() {
2037 int stamp = 42;
2038 Source source =
2039 new AnalysisContextImplTest_Source_getModificationStamp_overridden(
2040 stamp);
2041 _context.setContents(source, "");
2042 expect(stamp != _context.getModificationStamp(source), isTrue);
2043 }
2044
2045 void test_getResolvedCompilationUnit_library_null() {
2046 _context = contextWithCore();
2047 _sourceFactory = _context.sourceFactory;
2048 Source source = _addSource("/lib.dart", "library lib;");
2049 expect(_context.getResolvedCompilationUnit(source, null), isNull);
2050 }
2051
2052 void test_getResolvedCompilationUnit_source_html() {
2053 _context = contextWithCore();
2054 _sourceFactory = _context.sourceFactory;
2055 Source source = _addSource("/test.html", "<html></html>");
2056 expect(_context.getResolvedCompilationUnit2(source, source), isNull);
2057 expect(_context.resolveCompilationUnit2(source, source), isNull);
2058 expect(_context.getResolvedCompilationUnit2(source, source), isNull);
2059 }
2060
2061 void test_getSourceFactory() {
2062 expect(_context.sourceFactory, same(_sourceFactory));
2063 }
2064
2065 void test_getSourcesWithFullName() {
2066 String filePath = '/foo/lib/file.dart';
2067 List<Source> expected = <Source>[];
2068 ChangeSet changeSet = new ChangeSet();
2069
2070 TestSourceWithUri source1 =
2071 new TestSourceWithUri(filePath, Uri.parse('file://$filePath'));
2072 expected.add(source1);
2073 changeSet.addedSource(source1);
2074
2075 TestSourceWithUri source2 =
2076 new TestSourceWithUri(filePath, Uri.parse('package:foo/file.dart'));
2077 expected.add(source2);
2078 changeSet.addedSource(source2);
2079
2080 _context.applyChanges(changeSet);
2081 expect(
2082 _context.getSourcesWithFullName(filePath), unorderedEquals(expected));
2083 }
2084
2085 void test_getStatistics() {
2086 AnalysisContextStatistics statistics = _context.statistics;
2087 expect(statistics, isNotNull);
2088 // The following lines are fragile.
2089 // The values depend on the number of libraries in the SDK.
2090 // assertLength(0, statistics.getCacheRows());
2091 // assertLength(0, statistics.getExceptions());
2092 // assertLength(0, statistics.getSources());
2093 }
2094
2095 // void test_resolveCompilationUnit_sourceChangeDuringResolution() {
2096 // _context = new _AnalysisContext_sourceChangeDuringResolution();
2097 // AnalysisContextFactory.initContextWithCore(_context);
2098 // _sourceFactory = _context.sourceFactory;
2099 // Source source = _addSource("/lib.dart", "library lib;");
2100 // CompilationUnit compilationUnit =
2101 // _context.resolveCompilationUnit2(source, source);
2102 // expect(compilationUnit, isNotNull);
2103 // expect(_context.getLineInfo(source), isNotNull);
2104 // }
2105
2106 void test_isClientLibrary_dart() {
2107 _context = contextWithCore();
2108 _sourceFactory = _context.sourceFactory;
2109 Source source = _addSource("/test.dart", r'''
2110 import 'dart:html';
2111
2112 main() {}''');
2113 expect(_context.isClientLibrary(source), isFalse);
2114 expect(_context.isServerLibrary(source), isFalse);
2115 _context.computeLibraryElement(source);
2116 expect(_context.isClientLibrary(source), isTrue);
2117 expect(_context.isServerLibrary(source), isFalse);
2118 }
2119
2120 void test_isClientLibrary_html() {
2121 Source source = _addSource("/test.html", "<html></html>");
2122 expect(_context.isClientLibrary(source), isFalse);
2123 }
2124
2125 void test_isServerLibrary_dart() {
2126 _context = contextWithCore();
2127 _sourceFactory = _context.sourceFactory;
2128 Source source = _addSource("/test.dart", r'''
2129 library lib;
2130
2131 main() {}''');
2132 expect(_context.isClientLibrary(source), isFalse);
2133 expect(_context.isServerLibrary(source), isFalse);
2134 _context.computeLibraryElement(source);
2135 expect(_context.isClientLibrary(source), isFalse);
2136 expect(_context.isServerLibrary(source), isTrue);
2137 }
2138
2139 void test_isServerLibrary_html() {
2140 Source source = _addSource("/test.html", "<html></html>");
2141 expect(_context.isServerLibrary(source), isFalse);
2142 }
2143
2144 void test_parseCompilationUnit_html() {
2145 Source source = _addSource("/test.html", "<html></html>");
2146 expect(_context.parseCompilationUnit(source), isNull);
2147 }
2148
2149 void test_performAnalysisTask_modifiedAfterParse() {
2150 // TODO(scheglov) no threads in Dart
2151 // Source source = _addSource("/test.dart", "library lib;");
2152 // int initialTime = _context.getModificationStamp(source);
2153 // List<Source> sources = new List<Source>();
2154 // sources.add(source);
2155 // _context.analysisPriorityOrder = sources;
2156 // _context.parseCompilationUnit(source);
2157 // while (initialTime == JavaSystem.currentTimeMillis()) {
2158 // Thread.sleep(1);
2159 // // Force the modification time to be different.
2160 // }
2161 // _context.setContents(source, "library test;");
2162 // JUnitTestCase.assertTrue(initialTime != _context.getModificationStamp(sour ce));
2163 // _analyzeAll_assertFinished();
2164 // JUnitTestCase.assertNotNullMsg("performAnalysisTask failed to compute an e lement model", _context.getLibraryElement(source));
2165 }
2166
2167 void test_setAnalysisOptions() {
2168 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
2169 options.cacheSize = 42;
2170 options.dart2jsHint = false;
2171 options.hint = false;
2172 _context.analysisOptions = options;
2173 AnalysisOptions result = _context.analysisOptions;
2174 expect(result.cacheSize, options.cacheSize);
2175 expect(result.dart2jsHint, options.dart2jsHint);
2176 expect(result.hint, options.hint);
2177 }
2178
2179 void test_setAnalysisPriorityOrder_empty() {
2180 _context.analysisPriorityOrder = new List<Source>();
2181 }
2182
2183 void test_setAnalysisPriorityOrder_nonEmpty() {
2184 List<Source> sources = new List<Source>();
2185 sources.add(_addSource("/lib.dart", "library lib;"));
2186 _context.analysisPriorityOrder = sources;
2187 }
2188
2189 void test_setChangedContents_notResolved() {
2190 _context = contextWithCore();
2191 AnalysisOptionsImpl options =
2192 new AnalysisOptionsImpl.con1(_context.analysisOptions);
2193 options.incremental = true;
2194 _context.analysisOptions = options;
2195 _sourceFactory = _context.sourceFactory;
2196 String oldCode = r'''
2197 library lib;
2198 int a = 0;''';
2199 Source librarySource = _addSource("/lib.dart", oldCode);
2200 int offset = oldCode.indexOf("int a") + 4;
2201 String newCode = r'''
2202 library lib;
2203 int ya = 0;''';
2204 _context.setChangedContents(librarySource, newCode, offset, 0, 1);
2205 expect(_context.getContents(librarySource).data, newCode);
2206 expect(_getIncrementalAnalysisCache(_context), isNull);
2207 }
2208
2209 Future test_setContents_libraryWithPart() {
2210 _context = contextWithCore();
2211 SourcesChangedListener listener = new SourcesChangedListener();
2212 _context.onSourcesChanged.listen(listener.onData);
2213 _sourceFactory = _context.sourceFactory;
2214 String libraryContents1 = r'''
2215 library lib;
2216 part 'part.dart';
2217 int a = 0;''';
2218 Source librarySource = _addSource("/lib.dart", libraryContents1);
2219 String partContents1 = r'''
2220 part of lib;
2221 int b = a;''';
2222 Source partSource = _addSource("/part.dart", partContents1);
2223 _context.computeLibraryElement(librarySource);
2224 IncrementalAnalysisCache incrementalCache = new IncrementalAnalysisCache(
2225 librarySource, librarySource, null, null, null, 0, 0, 0);
2226 _setIncrementalAnalysisCache(_context, incrementalCache);
2227 expect(_getIncrementalAnalysisCache(_context), same(incrementalCache));
2228 String libraryContents2 = r'''
2229 library lib;
2230 part 'part.dart';
2231 int aa = 0;''';
2232 _context.setContents(librarySource, libraryContents2);
2233 expect(_context.getResolvedCompilationUnit2(partSource, librarySource),
2234 isNull);
2235 expect(_getIncrementalAnalysisCache(_context), isNull);
2236 return pumpEventQueue().then((_) {
2237 listener.assertEvent(wereSourcesAdded: true);
2238 listener.assertEvent(changedSources: [librarySource]);
2239 listener.assertEvent(wereSourcesAdded: true);
2240 listener.assertEvent(changedSources: [partSource]);
2241 listener.assertEvent(changedSources: [librarySource]);
2242 listener.assertNoMoreEvents();
2243 });
2244 }
2245
2246 void test_setContents_null() {
2247 _context = contextWithCore();
2248 _sourceFactory = _context.sourceFactory;
2249 Source librarySource = _addSource("/lib.dart", r'''
2250 library lib;
2251 int a = 0;''');
2252 _context.computeLibraryElement(librarySource);
2253 IncrementalAnalysisCache incrementalCache = new IncrementalAnalysisCache(
2254 librarySource, librarySource, null, null, null, 0, 0, 0);
2255 _setIncrementalAnalysisCache(_context, incrementalCache);
2256 expect(_getIncrementalAnalysisCache(_context), same(incrementalCache));
2257 _context.setContents(librarySource, null);
2258 expect(_context.getResolvedCompilationUnit2(librarySource, librarySource),
2259 isNull);
2260 expect(_getIncrementalAnalysisCache(_context), isNull);
2261 }
2262
2263 void test_setSourceFactory() {
2264 expect(_context.sourceFactory, _sourceFactory);
2265 SourceFactory factory = new SourceFactory([]);
2266 _context.sourceFactory = factory;
2267 expect(_context.sourceFactory, factory);
2268 }
2269
2270 void test_updateAnalysis() {
2271 expect(_context.sourcesNeedingProcessing.isEmpty, isTrue);
2272 Source source =
2273 new FileBasedSource.con1(FileUtilities2.createFile("/test.dart"));
2274 AnalysisDelta delta = new AnalysisDelta();
2275 delta.setAnalysisLevel(source, AnalysisLevel.ALL);
2276 _context.applyAnalysisDelta(delta);
2277 expect(_context.sourcesNeedingProcessing.contains(source), isTrue);
2278 delta = new AnalysisDelta();
2279 delta.setAnalysisLevel(source, AnalysisLevel.NONE);
2280 _context.applyAnalysisDelta(delta);
2281 expect(_context.sourcesNeedingProcessing.contains(source), isFalse);
2282 }
2283
2284 void xtest_performAnalysisTask_stress() {
2285 int maxCacheSize = 4;
2286 AnalysisOptionsImpl options =
2287 new AnalysisOptionsImpl.con1(_context.analysisOptions);
2288 options.cacheSize = maxCacheSize;
2289 _context.analysisOptions = options;
2290 int sourceCount = maxCacheSize + 2;
2291 List<Source> sources = new List<Source>();
2292 ChangeSet changeSet = new ChangeSet();
2293 for (int i = 0; i < sourceCount; i++) {
2294 Source source = _addSource("/lib$i.dart", "library lib$i;");
2295 sources.add(source);
2296 changeSet.addedSource(source);
2297 }
2298 _context.applyChanges(changeSet);
2299 _context.analysisPriorityOrder = sources;
2300 for (int i = 0; i < 1000; i++) {
2301 List<ChangeNotice> notice = _context.performAnalysisTask().changeNotices;
2302 if (notice == null) {
2303 //System.out.println("test_performAnalysisTask_stress: " + i);
2304 break;
2305 }
2306 }
2307 List<ChangeNotice> notice = _context.performAnalysisTask().changeNotices;
2308 if (notice != null) {
2309 fail(
2310 "performAnalysisTask failed to terminate after analyzing all sources") ;
2311 }
2312 }
2313
2314 Source _addSource(String fileName, String contents) {
2315 Source source =
2316 new FileBasedSource.con1(FileUtilities2.createFile(fileName));
2317 ChangeSet changeSet = new ChangeSet();
2318 changeSet.addedSource(source);
2319 _context.applyChanges(changeSet);
2320 _context.setContents(source, contents);
2321 return source;
2322 }
2323
2324 TestSource _addSourceWithException(String fileName) {
2325 return _addSourceWithException2(fileName, "");
2326 }
2327
2328 TestSource _addSourceWithException2(String fileName, String contents) {
2329 TestSource source = new TestSource(fileName, contents);
2330 source.generateExceptionOnRead = true;
2331 ChangeSet changeSet = new ChangeSet();
2332 changeSet.addedSource(source);
2333 _context.applyChanges(changeSet);
2334 return source;
2335 }
2336
2337 /**
2338 * Perform analysis tasks up to 512 times and asserts that that was enough.
2339 */
2340 void _analyzeAll_assertFinished() {
2341 _analyzeAll_assertFinished2(512);
2342 }
2343
2344 /**
2345 * Perform analysis tasks up to the given number of times and asserts that tha t was enough.
2346 *
2347 * @param maxIterations the maximum number of tasks to perform
2348 */
2349 void _analyzeAll_assertFinished2(int maxIterations) {
2350 for (int i = 0; i < maxIterations; i++) {
2351 List<ChangeNotice> notice = _context.performAnalysisTask().changeNotices;
2352 if (notice == null) {
2353 return;
2354 }
2355 }
2356 fail("performAnalysisTask failed to terminate after analyzing all sources");
2357 }
2358
2359 void _changeSource(TestSource source, String contents) {
2360 source.setContents(contents);
2361 ChangeSet changeSet = new ChangeSet();
2362 changeSet.changedSource(source);
2363 _context.applyChanges(changeSet);
2364 }
2365
2366 /**
2367 * Search the given compilation unit for a class with the given name. Return t he class with the
2368 * given name, or `null` if the class cannot be found.
2369 *
2370 * @param unit the compilation unit being searched
2371 * @param className the name of the class being searched for
2372 * @return the class with the given name
2373 */
2374 ClassElement _findClass(CompilationUnitElement unit, String className) {
2375 for (ClassElement classElement in unit.types) {
2376 if (classElement.displayName == className) {
2377 return classElement;
2378 }
2379 }
2380 return null;
2381 }
2382
2383 IncrementalAnalysisCache _getIncrementalAnalysisCache(
2384 AnalysisContextImpl context2) {
2385 return context2.test_incrementalAnalysisCache;
2386 }
2387
2388 List<Source> _getPriorityOrder(AnalysisContextImpl context2) {
2389 return context2.test_priorityOrder;
2390 }
2391
2392 void _performPendingAnalysisTasks([int maxTasks = 20]) {
2393 for (int i = 0; _context.performAnalysisTask().hasMoreWork; i++) {
2394 if (i > maxTasks) {
2395 fail('Analysis did not terminate.');
2396 }
2397 }
2398 }
2399
2400 void _removeSource(Source source) {
2401 ChangeSet changeSet = new ChangeSet();
2402 changeSet.removedSource(source);
2403 _context.applyChanges(changeSet);
2404 }
2405
2406 void _setIncrementalAnalysisCache(
2407 AnalysisContextImpl context, IncrementalAnalysisCache incrementalCache) {
2408 context.test_incrementalAnalysisCache = incrementalCache;
2409 }
2410
2411 /**
2412 * Returns `true` if there is an [AnalysisError] with [ErrorSeverity.ERROR] in
2413 * the given [AnalysisErrorInfo].
2414 */
2415 static bool _hasAnalysisErrorWithErrorSeverity(AnalysisErrorInfo errorInfo) {
2416 List<AnalysisError> errors = errorInfo.errors;
2417 for (AnalysisError analysisError in errors) {
2418 if (analysisError.errorCode.errorSeverity == ErrorSeverity.ERROR) {
2419 return true;
2420 }
2421 }
2422 return false;
2423 }
2424 }
2425
2426 class FakeSdk extends DirectoryBasedDartSdk {
2427 FakeSdk(JavaFile arg0) : super(arg0);
2428
2429 @override
2430 LibraryMap initialLibraryMap(bool useDart2jsPaths) {
2431 LibraryMap map = new LibraryMap();
2432 _addLibrary(map, DartSdk.DART_ASYNC, false, "async.dart");
2433 _addLibrary(map, DartSdk.DART_CORE, false, "core.dart");
2434 _addLibrary(map, DartSdk.DART_HTML, false, "html_dartium.dart");
2435 _addLibrary(map, "dart:math", false, "math.dart");
2436 _addLibrary(map, "dart:_interceptors", true, "_interceptors.dart");
2437 _addLibrary(map, "dart:_js_helper", true, "_js_helper.dart");
2438 return map;
2439 }
2440
2441 void _addLibrary(LibraryMap map, String uri, bool isInternal, String path) {
2442 SdkLibraryImpl library = new SdkLibraryImpl(uri);
2443 if (isInternal) {
2444 library.category = "Internal";
2445 }
2446 library.path = path;
2447 map.setLibrary(uri, library);
2448 }
2449 }
2450
2451 class _AnalysisContextImplTest_test_applyChanges_removeContainer
2452 implements SourceContainer {
2453 Source libB;
2454 _AnalysisContextImplTest_test_applyChanges_removeContainer(this.libB);
2455 @override
2456 bool contains(Source source) => source == libB;
2457 }
2458
2459 class _Source_getContent_throwException extends NonExistingSource {
2460 _Source_getContent_throwException(String name)
2461 : super(name, UriKind.FILE_URI);
2462
2463 @override
2464 TimestampedData<String> get contents {
2465 throw 'Read error';
2466 }
2467
2468 @override
2469 bool exists() => true;
2470 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698