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