OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import 'dart:async'; |
| 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/dart/element/visitor.dart'; |
| 10 import 'package:analyzer/exception/exception.dart'; |
| 11 import 'package:analyzer/file_system/file_system.dart'; |
| 12 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 13 import 'package:analyzer/source/package_map_resolver.dart'; |
| 14 import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 15 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 16 import 'package:analyzer/src/dart/analysis/file_state.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/engine.dart' as engine; |
| 19 import 'package:analyzer/src/generated/sdk.dart'; |
| 20 import 'package:analyzer/src/generated/source_io.dart'; |
| 21 |
| 22 import 'mock_sdk.dart'; |
| 23 |
| 24 /** |
| 25 * Finds an [Element] with the given [name]. |
| 26 */ |
| 27 Element findChildElement(Element root, String name, [ElementKind kind]) { |
| 28 Element result = null; |
| 29 root.accept(new _ElementVisitorFunctionWrapper((Element element) { |
| 30 if (element.name != name) { |
| 31 return; |
| 32 } |
| 33 if (kind != null && element.kind != kind) { |
| 34 return; |
| 35 } |
| 36 result = element; |
| 37 })); |
| 38 return result; |
| 39 } |
| 40 |
| 41 /** |
| 42 * A function to be called for every [Element]. |
| 43 */ |
| 44 typedef void _ElementVisitorFunction(Element element); |
| 45 |
| 46 class AbstractContextTest { |
| 47 MemoryResourceProvider provider; |
| 48 DartSdk sdk; |
| 49 Map<String, List<Folder>> packageMap; |
| 50 UriResolver resourceResolver; |
| 51 |
| 52 StringBuffer _logBuffer = new StringBuffer(); |
| 53 FileContentOverlay _fileContentOverlay = new FileContentOverlay(); |
| 54 AnalysisDriver _driver; |
| 55 |
| 56 AnalysisDriver get driver => _driver; |
| 57 |
| 58 Source addMetaPackageSource() => addPackageSource( |
| 59 'meta', |
| 60 'meta.dart', |
| 61 r''' |
| 62 library meta; |
| 63 |
| 64 const Required required = const Required(); |
| 65 |
| 66 class Required { |
| 67 final String reason; |
| 68 const Required([this.reason]); |
| 69 } |
| 70 '''); |
| 71 |
| 72 Source addPackageSource(String packageName, String filePath, String content) { |
| 73 packageMap[packageName] = [(newFolder('/pubcache/$packageName'))]; |
| 74 File file = newFile('/pubcache/$packageName/$filePath', content); |
| 75 return file.createSource(); |
| 76 } |
| 77 |
| 78 Source addSource(String path, String content, [Uri uri]) { |
| 79 driver.addFile(path); |
| 80 driver.changeFile(path); |
| 81 _fileContentOverlay[path] = content; |
| 82 return null; |
| 83 } |
| 84 |
| 85 File newFile(String path, [String content]) => |
| 86 provider.newFile(provider.convertPath(path), content ?? ''); |
| 87 |
| 88 Folder newFolder(String path) => |
| 89 provider.newFolder(provider.convertPath(path)); |
| 90 |
| 91 void processRequiredPlugins() { |
| 92 AnalysisEngine.instance.processRequiredPlugins(); |
| 93 } |
| 94 |
| 95 Future<CompilationUnit> resolveLibraryUnit(Source source) async { |
| 96 return (await driver.getResult(source.fullName))?.unit; |
| 97 } |
| 98 |
| 99 void setUp() { |
| 100 processRequiredPlugins(); |
| 101 setupResourceProvider(); |
| 102 sdk = new MockSdk(resourceProvider: provider); |
| 103 resourceResolver = new ResourceUriResolver(provider); |
| 104 packageMap = new Map<String, List<Folder>>(); |
| 105 PackageMapUriResolver packageResolver = |
| 106 new PackageMapUriResolver(provider, packageMap); |
| 107 SourceFactory sourceFactory = new SourceFactory( |
| 108 [new DartUriResolver(sdk), packageResolver, resourceResolver]); |
| 109 PerformanceLog log = new PerformanceLog(_logBuffer); |
| 110 AnalysisDriverScheduler scheduler = new AnalysisDriverScheduler(log); |
| 111 _driver = new AnalysisDriver( |
| 112 scheduler, |
| 113 log, |
| 114 provider, |
| 115 new MemoryByteStore(), |
| 116 _fileContentOverlay, |
| 117 null, |
| 118 sourceFactory, |
| 119 new AnalysisOptionsImpl()); |
| 120 scheduler.start(); |
| 121 AnalysisEngine.instance.logger = PrintLogger.instance; |
| 122 } |
| 123 |
| 124 void setupResourceProvider() { |
| 125 provider = new MemoryResourceProvider(); |
| 126 } |
| 127 |
| 128 void tearDown() { |
| 129 provider = null; |
| 130 AnalysisEngine.instance.clearCaches(); |
| 131 AnalysisEngine.instance.logger = null; |
| 132 } |
| 133 } |
| 134 |
| 135 /** |
| 136 * Instances of the class [PrintLogger] print all of the errors. |
| 137 */ |
| 138 class PrintLogger implements Logger { |
| 139 static final Logger instance = new PrintLogger(); |
| 140 |
| 141 @override |
| 142 void logError(String message, [CaughtException exception]) { |
| 143 print(message); |
| 144 if (exception != null) { |
| 145 print(exception); |
| 146 } |
| 147 } |
| 148 |
| 149 @override |
| 150 void logInformation(String message, [CaughtException exception]) { |
| 151 print(message); |
| 152 if (exception != null) { |
| 153 print(exception); |
| 154 } |
| 155 } |
| 156 } |
| 157 |
| 158 /** |
| 159 * Wraps the given [_ElementVisitorFunction] into an instance of |
| 160 * [engine.GeneralizingElementVisitor]. |
| 161 */ |
| 162 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { |
| 163 final _ElementVisitorFunction function; |
| 164 |
| 165 _ElementVisitorFunctionWrapper(this.function); |
| 166 |
| 167 @override |
| 168 visitElement(Element element) { |
| 169 function(element); |
| 170 super.visitElement(element); |
| 171 } |
| 172 } |
OLD | NEW |