Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: pkg/smoke/test/codegen/testing_resolver_utils.dart

Issue 194813007: Add codegen support in smoke (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 dartSdkDirectory;
17
18 class LibraryProvider {
19 final AnalysisContext _analyzer;
20 final Map<String, Source> _allSources;
21 LibraryProvider(this._analyzer, this._allSources);
22 LibraryElement libraryFor(String uri) =>
23 _analyzer.computeLibraryElement(_allSources[uri]);
24 }
25
26 LibraryProvider initAnalyzer(Map<String, String> contents) {
27 var analyzer = AnalysisEngine.instance.createAnalysisContext();
28 var options = new AnalysisOptionsImpl()
29 ..cacheSize = 256
30 ..preserveComments = false
31 ..analyzeFunctionBodies = false;
32 analyzer.analysisOptions = options;
33 var sdk = new DirectoryBasedDartSdk(new JavaFile(dartSdkDirectory));
34 sdk.context.analysisOptions = options;
35 var changes = new ChangeSet();
36 var allSources = {};
37 contents.forEach((url, code) {
38 var source = new _SimpleSource(url, code, allSources);
39 allSources[url] = source;
40 changes.addedSource(source);
41 });
42 analyzer.applyChanges(changes);
43
44 analyzer.sourceFactory = new SourceFactory([
45 new DartUriResolver(sdk),
46 new _SimpleUriResolver(allSources)]);
47
48 return new LibraryProvider(analyzer, allSources);
49 }
50
51 class _SimpleUriResolver implements UriResolver {
52 final Map<String, Source> allSources;
53 _SimpleUriResolver(this.allSources);
54
55 Source resolveAbsolute(Uri uri) => allSources['$uri'];
56
57 Source fromEncoding(UriKind kind, Uri uri) =>
58 throw new UnimplementedError('fromEncoding not implemented');
59
60 Uri restoreAbsolute(Source source) =>
61 throw new UnimplementedError('restoreAbsolute not implemented');
62 }
63
64 class _SimpleSource extends Source {
65 final String path;
66 final String rawContents;
67 final Map<String, Source> allSources;
68
69 _SimpleSource(this.path, this.rawContents, this.allSources);
70
71 operator ==(other) => other is _SimpleSource &&
72 rawContents == other.rawContents;
73 int get hashCode => rawContents.hashCode;
74
75 bool exists() => true;
76 String get encoding => '$uriKind/$path';
77 String get fullName => path;
78 TimestampedData<String> get contents =>
79 new TimestampedData<String>(modificationStamp, rawContents);
80
81 int get modificationStamp => 1;
82 String get shortName => path;
83 UriKind get uriKind => UriKind.FILE_URI;
84 final bool isInSystemLibrary = false;
85
86 // Since this is just for simple tests we just restricted this mock
87 // to root-relative imports. For more sophisticated stuff, you should be
88 // using the test helpers in `package:code_transformers`.
89 Source resolveRelative(Uri uri) {
90 if (uri.path.startsWith('/')) return allSources['${uri.path}'];
91 throw new UnimplementedError('relative URIs not supported: $uri');
92 }
93
94 void getContentsToReceiver(Source_ContentReceiver receiver) {
95 receiver.accept(rawContents, modificationStamp);
96 }
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698