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