| OLD | NEW |
| (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 testing.abstract_context; | |
| 6 | |
| 7 import 'package:analysis_testing/mock_sdk.dart'; | |
| 8 import 'package:analyzer/file_system/file_system.dart'; | |
| 9 import 'package:analyzer/file_system/memory_file_system.dart'; | |
| 10 import 'package:analyzer/src/generated/ast.dart'; | |
| 11 import 'package:analyzer/src/generated/element.dart'; | |
| 12 import 'package:analyzer/src/generated/engine.dart'; | |
| 13 import 'package:analyzer/src/generated/sdk.dart'; | |
| 14 import 'package:analyzer/src/generated/source_io.dart'; | |
| 15 | |
| 16 | |
| 17 /** | |
| 18 * Finds an [Element] with the given [name]. | |
| 19 */ | |
| 20 Element findChildElement(Element root, String name, [ElementKind kind]) { | |
| 21 Element result = null; | |
| 22 root.accept(new _ElementVisitorFunctionWrapper((Element element) { | |
| 23 if (element.name != name) { | |
| 24 return; | |
| 25 } | |
| 26 if (kind != null && element.kind != kind) { | |
| 27 return; | |
| 28 } | |
| 29 result = element; | |
| 30 })); | |
| 31 return result; | |
| 32 } | |
| 33 | |
| 34 | |
| 35 /** | |
| 36 * A function to be called for every [Element]. | |
| 37 */ | |
| 38 typedef void _ElementVisitorFunction(Element element); | |
| 39 | |
| 40 | |
| 41 class AbstractContextTest { | |
| 42 static final DartSdk SDK = new MockSdk(); | |
| 43 static final UriResolver SDK_RESOLVER = new DartUriResolver(SDK); | |
| 44 | |
| 45 MemoryResourceProvider provider = new MemoryResourceProvider(); | |
| 46 UriResolver resourceResolver; | |
| 47 AnalysisContext context; | |
| 48 | |
| 49 Source addSource(String path, String content) { | |
| 50 File file = provider.newFile(path, content); | |
| 51 Source source = file.createSource(); | |
| 52 ChangeSet changeSet = new ChangeSet(); | |
| 53 changeSet.addedSource(source); | |
| 54 context.applyChanges(changeSet); | |
| 55 context.setContents(source, content); | |
| 56 return source; | |
| 57 } | |
| 58 | |
| 59 /** | |
| 60 * Performs all analysis tasks in [context]. | |
| 61 */ | |
| 62 void performAllAnalysisTasks() { | |
| 63 while (true) { | |
| 64 AnalysisResult result = context.performAnalysisTask(); | |
| 65 if (!result.hasMoreWork) { | |
| 66 break; | |
| 67 } | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 CompilationUnit resolveDartUnit(Source unitSource, Source librarySource) { | |
| 72 return context.resolveCompilationUnit2(unitSource, librarySource); | |
| 73 } | |
| 74 | |
| 75 CompilationUnit resolveLibraryUnit(Source source) { | |
| 76 return context.resolveCompilationUnit2(source, source); | |
| 77 } | |
| 78 | |
| 79 void setUp() { | |
| 80 resourceResolver = new ResourceUriResolver(provider); | |
| 81 context = AnalysisEngine.instance.createAnalysisContext(); | |
| 82 context.sourceFactory = new SourceFactory([SDK_RESOLVER, resourceResolver]); | |
| 83 } | |
| 84 | |
| 85 void tearDown() { | |
| 86 context = null; | |
| 87 provider = null; | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 | |
| 92 /** | |
| 93 * Wraps the given [_ElementVisitorFunction] into an instance of | |
| 94 * [engine.GeneralizingElementVisitor]. | |
| 95 */ | |
| 96 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { | |
| 97 final _ElementVisitorFunction function; | |
| 98 _ElementVisitorFunctionWrapper(this.function); | |
| 99 visitElement(Element element) { | |
| 100 function(element); | |
| 101 super.visitElement(element); | |
| 102 } | |
| 103 } | |
| OLD | NEW |