| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library testing.abstract_context; | 5 library testing.abstract_context; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/dart/element/visitor.dart'; | 9 import 'package:analyzer/dart/element/visitor.dart'; |
| 10 import 'package:analyzer/exception/exception.dart'; | 10 import 'package:analyzer/exception/exception.dart'; |
| 11 import 'package:analyzer/file_system/file_system.dart'; | 11 import 'package:analyzer/file_system/file_system.dart'; |
| 12 import 'package:analyzer/file_system/memory_file_system.dart'; | 12 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 13 import 'package:analyzer/source/package_map_resolver.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'; |
| 14 import 'package:analyzer/src/generated/engine.dart'; | 17 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/engine.dart' as engine; |
| 15 import 'package:analyzer/src/generated/sdk.dart'; | 19 import 'package:analyzer/src/generated/sdk.dart'; |
| 16 import 'package:analyzer/src/generated/source_io.dart'; | 20 import 'package:analyzer/src/generated/source_io.dart'; |
| 17 | 21 |
| 18 import 'mock_sdk.dart'; | 22 import 'mock_sdk.dart'; |
| 19 | 23 |
| 20 /** | 24 /** |
| 21 * Finds an [Element] with the given [name]. | 25 * Finds an [Element] with the given [name]. |
| 22 */ | 26 */ |
| 23 Element findChildElement(Element root, String name, [ElementKind kind]) { | 27 Element findChildElement(Element root, String name, [ElementKind kind]) { |
| 24 Element result = null; | 28 Element result = null; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 39 */ | 43 */ |
| 40 typedef void _ElementVisitorFunction(Element element); | 44 typedef void _ElementVisitorFunction(Element element); |
| 41 | 45 |
| 42 class AbstractContextTest { | 46 class AbstractContextTest { |
| 43 static final DartSdk SDK = new MockSdk(); | 47 static final DartSdk SDK = new MockSdk(); |
| 44 static final UriResolver SDK_RESOLVER = new DartUriResolver(SDK); | 48 static final UriResolver SDK_RESOLVER = new DartUriResolver(SDK); |
| 45 | 49 |
| 46 MemoryResourceProvider provider; | 50 MemoryResourceProvider provider; |
| 47 Map<String, List<Folder>> packageMap; | 51 Map<String, List<Folder>> packageMap; |
| 48 UriResolver resourceResolver; | 52 UriResolver resourceResolver; |
| 49 AnalysisContext context; | 53 |
| 54 AnalysisContext _context; |
| 55 |
| 56 StringBuffer _logBuffer = new StringBuffer(); |
| 57 FileContentOverlay _fileContentOverlay = new FileContentOverlay(); |
| 58 AnalysisDriver _driver; |
| 59 |
| 60 AnalysisContext get context { |
| 61 if (enableNewAnalysisDriver) { |
| 62 throw new StateError('Should not be used with the new analysis driver.'); |
| 63 } |
| 64 return _context; |
| 65 } |
| 66 |
| 67 AnalysisDriver get driver { |
| 68 if (enableNewAnalysisDriver) { |
| 69 return _driver; |
| 70 } |
| 71 throw new StateError('Should be used with the new analysis driver.'); |
| 72 } |
| 73 |
| 74 /** |
| 75 * Return `true` if the new analysis driver should be used by these tests. |
| 76 */ |
| 77 bool get enableNewAnalysisDriver => false; |
| 50 | 78 |
| 51 Source addPackageSource(String packageName, String filePath, String content) { | 79 Source addPackageSource(String packageName, String filePath, String content) { |
| 52 packageMap[packageName] = [(newFolder('/pubcache/$packageName'))]; | 80 packageMap[packageName] = [(newFolder('/pubcache/$packageName'))]; |
| 53 File file = newFile('/pubcache/$packageName/$filePath', content); | 81 File file = newFile('/pubcache/$packageName/$filePath', content); |
| 54 return file.createSource(); | 82 return file.createSource(); |
| 55 } | 83 } |
| 56 | 84 |
| 57 Source addSource(String path, String content, [Uri uri]) { | 85 Source addSource(String path, String content, [Uri uri]) { |
| 58 File file = newFile(path, content); | 86 if (enableNewAnalysisDriver) { |
| 59 Source source = file.createSource(uri); | 87 _fileContentOverlay[path] = content; |
| 60 ChangeSet changeSet = new ChangeSet(); | 88 return null; |
| 61 changeSet.addedSource(source); | 89 } else { |
| 62 context.applyChanges(changeSet); | 90 File file = newFile(path, content); |
| 63 context.setContents(source, content); | 91 Source source = file.createSource(uri); |
| 64 return source; | 92 ChangeSet changeSet = new ChangeSet(); |
| 93 changeSet.addedSource(source); |
| 94 context.applyChanges(changeSet); |
| 95 context.setContents(source, content); |
| 96 return source; |
| 97 } |
| 65 } | 98 } |
| 66 | 99 |
| 67 File newFile(String path, [String content]) => | 100 File newFile(String path, [String content]) => |
| 68 provider.newFile(provider.convertPath(path), content ?? ''); | 101 provider.newFile(provider.convertPath(path), content ?? ''); |
| 69 | 102 |
| 70 Folder newFolder(String path) => | 103 Folder newFolder(String path) => |
| 71 provider.newFolder(provider.convertPath(path)); | 104 provider.newFolder(provider.convertPath(path)); |
| 72 | 105 |
| 73 /** | 106 /** |
| 74 * Performs all analysis tasks in [context]. | 107 * Performs all analysis tasks in [context]. |
| 75 */ | 108 */ |
| 76 void performAllAnalysisTasks() { | 109 void performAllAnalysisTasks() { |
| 77 while (true) { | 110 while (true) { |
| 78 AnalysisResult result = context.performAnalysisTask(); | 111 engine.AnalysisResult result = context.performAnalysisTask(); |
| 79 if (!result.hasMoreWork) { | 112 if (!result.hasMoreWork) { |
| 80 break; | 113 break; |
| 81 } | 114 } |
| 82 } | 115 } |
| 83 } | 116 } |
| 84 | 117 |
| 85 void processRequiredPlugins() { | 118 void processRequiredPlugins() { |
| 86 AnalysisEngine.instance.processRequiredPlugins(); | 119 AnalysisEngine.instance.processRequiredPlugins(); |
| 87 } | 120 } |
| 88 | 121 |
| 89 CompilationUnit resolveDartUnit(Source unitSource, Source librarySource) { | 122 CompilationUnit resolveDartUnit(Source unitSource, Source librarySource) { |
| 90 return context.resolveCompilationUnit2(unitSource, librarySource); | 123 return context.resolveCompilationUnit2(unitSource, librarySource); |
| 91 } | 124 } |
| 92 | 125 |
| 93 CompilationUnit resolveLibraryUnit(Source source) { | 126 CompilationUnit resolveLibraryUnit(Source source) { |
| 94 return context.resolveCompilationUnit2(source, source); | 127 return context.resolveCompilationUnit2(source, source); |
| 95 } | 128 } |
| 96 | 129 |
| 97 void setUp() { | 130 void setUp() { |
| 98 processRequiredPlugins(); | 131 processRequiredPlugins(); |
| 99 setupResourceProvider(); | 132 setupResourceProvider(); |
| 100 resourceResolver = new ResourceUriResolver(provider); | 133 resourceResolver = new ResourceUriResolver(provider); |
| 101 packageMap = new Map<String, List<Folder>>(); | 134 packageMap = new Map<String, List<Folder>>(); |
| 102 PackageMapUriResolver packageResolver = | 135 PackageMapUriResolver packageResolver = |
| 103 new PackageMapUriResolver(provider, packageMap); | 136 new PackageMapUriResolver(provider, packageMap); |
| 104 context = AnalysisEngine.instance.createAnalysisContext(); | 137 SourceFactory sourceFactory = |
| 105 context.sourceFactory = | |
| 106 new SourceFactory([SDK_RESOLVER, packageResolver, resourceResolver]); | 138 new SourceFactory([SDK_RESOLVER, packageResolver, resourceResolver]); |
| 139 if (enableNewAnalysisDriver) { |
| 140 PerformanceLog log = new PerformanceLog(_logBuffer); |
| 141 AnalysisDriverScheduler scheduler = new AnalysisDriverScheduler(log); |
| 142 _driver = new AnalysisDriver( |
| 143 scheduler, |
| 144 log, |
| 145 provider, |
| 146 new MemoryByteStore(), |
| 147 _fileContentOverlay, |
| 148 sourceFactory, |
| 149 new AnalysisOptionsImpl()); |
| 150 scheduler.start(); |
| 151 } else { |
| 152 _context = AnalysisEngine.instance.createAnalysisContext(); |
| 153 context.sourceFactory = sourceFactory; |
| 154 } |
| 107 AnalysisEngine.instance.logger = PrintLogger.instance; | 155 AnalysisEngine.instance.logger = PrintLogger.instance; |
| 108 } | 156 } |
| 109 | 157 |
| 110 void setupResourceProvider() { | 158 void setupResourceProvider() { |
| 111 provider = new MemoryResourceProvider(); | 159 provider = new MemoryResourceProvider(); |
| 112 } | 160 } |
| 113 | 161 |
| 114 void tearDown() { | 162 void tearDown() { |
| 115 context = null; | 163 _context = null; |
| 116 provider = null; | 164 provider = null; |
| 117 AnalysisEngine.instance.logger = null; | 165 AnalysisEngine.instance.logger = null; |
| 118 } | 166 } |
| 119 } | 167 } |
| 120 | 168 |
| 121 /** | 169 /** |
| 122 * Instances of the class [PrintLogger] print all of the errors. | 170 * Instances of the class [PrintLogger] print all of the errors. |
| 123 */ | 171 */ |
| 124 class PrintLogger implements Logger { | 172 class PrintLogger implements Logger { |
| 125 static final Logger instance = new PrintLogger(); | 173 static final Logger instance = new PrintLogger(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 146 * [engine.GeneralizingElementVisitor]. | 194 * [engine.GeneralizingElementVisitor]. |
| 147 */ | 195 */ |
| 148 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { | 196 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { |
| 149 final _ElementVisitorFunction function; | 197 final _ElementVisitorFunction function; |
| 150 _ElementVisitorFunctionWrapper(this.function); | 198 _ElementVisitorFunctionWrapper(this.function); |
| 151 visitElement(Element element) { | 199 visitElement(Element element) { |
| 152 function(element); | 200 function(element); |
| 153 super.visitElement(element); | 201 super.visitElement(element); |
| 154 } | 202 } |
| 155 } | 203 } |
| OLD | NEW |