| 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 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 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), |
| 46 new _SimpleUriResolver(allSources)]); | 46 new _SimpleUriResolver(allSources)]); |
| 47 | 47 |
| 48 return new LibraryProvider(analyzer, allSources); | 48 return new LibraryProvider(analyzer, allSources); |
| 49 } | 49 } |
| 50 | 50 |
| 51 class _SimpleUriResolver implements UriResolver { | 51 class _SimpleUriResolver implements UriResolver { |
| 52 final Map<String, Source> allSources; | 52 final Map<String, Source> allSources; |
| 53 _SimpleUriResolver(this.allSources); | 53 _SimpleUriResolver(this.allSources); |
| 54 | 54 |
| 55 Source resolveAbsolute(Uri uri) => allSources['$uri']; | 55 Source resolveAbsolute(Uri uri) => allSources['$uri']; |
| 56 | 56 |
| 57 Source fromEncoding(UriKind kind, Uri uri) => | 57 Source fromEncoding(UriKind kind, Uri uri) => |
| 58 throw new UnimplementedError('fromEncoding not implemented'); | 58 throw new UnimplementedError('fromEncoding not implemented'); |
| 59 | 59 |
| 60 Uri restoreAbsolute(Source source) => | 60 Uri restoreAbsolute(Source source) => |
| 61 throw new UnimplementedError('restoreAbsolute not implemented'); | 61 throw new UnimplementedError('restoreAbsolute not implemented'); |
| 62 } | 62 } |
| 63 | 63 |
| 64 class _SimpleSource extends Source { | 64 class _SimpleSource extends Source { |
| 65 final Uri uri; |
| 65 final String path; | 66 final String path; |
| 66 final String rawContents; | 67 final String rawContents; |
| 67 final Map<String, Source> allSources; | 68 final Map<String, Source> allSources; |
| 68 | 69 |
| 69 _SimpleSource(this.path, this.rawContents, this.allSources); | 70 _SimpleSource(this.path, this.rawContents, this.allSources) |
| 71 : uri = Uri.parse('file:///path'); |
| 70 | 72 |
| 71 operator ==(other) => other is _SimpleSource && | 73 operator ==(other) => other is _SimpleSource && |
| 72 rawContents == other.rawContents; | 74 rawContents == other.rawContents; |
| 73 int get hashCode => rawContents.hashCode; | 75 int get hashCode => rawContents.hashCode; |
| 74 | 76 |
| 75 bool exists() => true; | 77 bool exists() => true; |
| 76 String get encoding => '$uriKind/$path'; | 78 String get encoding => '$uriKind/$path'; |
| 77 String get fullName => path; | 79 String get fullName => path; |
| 78 TimestampedData<String> get contents => | 80 TimestampedData<String> get contents => |
| 79 new TimestampedData<String>(modificationStamp, rawContents); | 81 new TimestampedData<String>(modificationStamp, rawContents); |
| 80 | 82 |
| 81 int get modificationStamp => 1; | 83 int get modificationStamp => 1; |
| 82 String get shortName => path; | 84 String get shortName => path; |
| 83 UriKind get uriKind => UriKind.FILE_URI; | 85 UriKind get uriKind => UriKind.FILE_URI; |
| 84 final bool isInSystemLibrary = false; | 86 final bool isInSystemLibrary = false; |
| 85 | 87 |
| 86 // Since this is just for simple tests we just restricted this mock | 88 // Since this is just for simple tests we just restricted this mock |
| 87 // to root-relative imports. For more sophisticated stuff, you should be | 89 // to root-relative imports. For more sophisticated stuff, you should be |
| 88 // using the test helpers in `package:code_transformers`. | 90 // using the test helpers in `package:code_transformers`. |
| 89 Source resolveRelative(Uri uri) { | 91 Source resolveRelative(Uri uri) { |
| 90 if (uri.path.startsWith('/')) return allSources['${uri.path}']; | 92 if (uri.path.startsWith('/')) return allSources['${uri.path}']; |
| 91 throw new UnimplementedError('relative URIs not supported: $uri'); | 93 throw new UnimplementedError('relative URIs not supported: $uri'); |
| 92 } | 94 } |
| 93 | 95 |
| 96 // Since this is just for simple tests we just restricted this mock |
| 97 // to root-relative imports. For more sophisticated stuff, you should be |
| 98 // using the test helpers in `package:code_transformers`. |
| 99 Uri resolveRelativeUri(Uri uri) { |
| 100 if (!uri.path.startsWith('/')) { |
| 101 throw new UnimplementedError('relative URIs not supported: $uri'); |
| 102 } |
| 103 return uri; |
| 104 } |
| 105 |
| 94 void getContentsToReceiver(Source_ContentReceiver receiver) { | 106 void getContentsToReceiver(Source_ContentReceiver receiver) { |
| 95 receiver.accept(rawContents, modificationStamp); | 107 receiver.accept(rawContents, modificationStamp); |
| 96 } | 108 } |
| 97 } | 109 } |
| OLD | NEW |