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 /// Utility functions to test the code generation tools with the resolver. |
| 6 // Note, this is just for simple tests, so we restricted the logic to only |
| 7 // support root-relative imports. For more sophisticated stuff, you should be |
| 8 // using the test helpers in `package:code_transformers`. |
| 9 library smoke.test.codegen.testing_resolver_utils; |
| 10 |
| 11 import 'package:analyzer/src/generated/element.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/java_io.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; |
| 15 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
| 16 import 'package:code_transformers/src/dart_sdk.dart' show |
| 17 testingDartSdkDirectory; |
| 18 |
| 19 class LibraryProvider { |
| 20 final AnalysisContext _analyzer; |
| 21 final Map<String, Source> _allSources; |
| 22 LibraryProvider(this._analyzer, this._allSources); |
| 23 LibraryElement libraryFor(String uri) => |
| 24 _analyzer.computeLibraryElement(_allSources[uri]); |
| 25 } |
| 26 |
| 27 LibraryProvider initAnalyzer(Map<String, String> contents) { |
| 28 var analyzer = AnalysisEngine.instance.createAnalysisContext(); |
| 29 var options = new AnalysisOptionsImpl() |
| 30 ..cacheSize = 256 |
| 31 ..preserveComments = false |
| 32 ..analyzeFunctionBodies = false; |
| 33 analyzer.analysisOptions = options; |
| 34 var sdk = new DirectoryBasedDartSdk(new JavaFile(testingDartSdkDirectory)); |
| 35 sdk.context.analysisOptions = options; |
| 36 var changes = new ChangeSet(); |
| 37 var allSources = {}; |
| 38 contents.forEach((url, code) { |
| 39 var source = new _SimpleSource(url, code, allSources); |
| 40 allSources[url] = source; |
| 41 changes.addedSource(source); |
| 42 }); |
| 43 analyzer.applyChanges(changes); |
| 44 |
| 45 analyzer.sourceFactory = new SourceFactory([ |
| 46 new DartUriResolver(sdk), |
| 47 new _SimpleUriResolver(allSources)]); |
| 48 |
| 49 return new LibraryProvider(analyzer, allSources); |
| 50 } |
| 51 |
| 52 class _SimpleUriResolver implements UriResolver { |
| 53 final Map<String, Source> allSources; |
| 54 _SimpleUriResolver(this.allSources); |
| 55 |
| 56 Source resolveAbsolute(Uri uri) => allSources['$uri']; |
| 57 |
| 58 Source fromEncoding(UriKind kind, Uri uri) => |
| 59 throw new UnimplementedError('fromEncoding not implemented'); |
| 60 |
| 61 Uri restoreAbsolute(Source source) => |
| 62 throw new UnimplementedError('restoreAbsolute not implemented'); |
| 63 } |
| 64 |
| 65 class _SimpleSource extends Source { |
| 66 final String path; |
| 67 final String rawContents; |
| 68 final Map<String, Source> allSources; |
| 69 |
| 70 _SimpleSource(this.path, this.rawContents, this.allSources); |
| 71 |
| 72 operator ==(other) => other is _SimpleSource && |
| 73 rawContents == other.rawContents; |
| 74 int get hashCode => rawContents.hashCode; |
| 75 |
| 76 bool exists() => true; |
| 77 String get encoding => '$uriKind/$path'; |
| 78 String get fullName => path; |
| 79 TimestampedData<String> get contents => |
| 80 new TimestampedData<String>(modificationStamp, rawContents); |
| 81 |
| 82 int get modificationStamp => 1; |
| 83 String get shortName => path; |
| 84 UriKind get uriKind => UriKind.FILE_URI; |
| 85 final bool isInSystemLibrary = false; |
| 86 |
| 87 // Since this is just for simple tests we just restricted this mock |
| 88 // to root-relative imports. For more sophisticated stuff, you should be |
| 89 // using the test helpers in `package:code_transformers`. |
| 90 Source resolveRelative(Uri uri) { |
| 91 if (uri.path.startsWith('/')) return allSources['${uri.path}']; |
| 92 throw new UnimplementedError('relative URIs not supported: $uri'); |
| 93 } |
| 94 |
| 95 void getContentsToReceiver(Source_ContentReceiver receiver) { |
| 96 receiver.accept(rawContents, modificationStamp); |
| 97 } |
| 98 } |
OLD | NEW |