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

Side by Side Diff: packages/analyzer/test/src/context/mock_sdk.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
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 library test.src.mock_sdk;
6
7 import 'package:analyzer/file_system/file_system.dart' as resource;
8 import 'package:analyzer/file_system/memory_file_system.dart' as resource;
9 import 'package:analyzer/src/context/cache.dart';
10 import 'package:analyzer/src/context/context.dart';
11 import 'package:analyzer/src/generated/engine.dart'
12 show AnalysisEngine, ChangeSet;
13 import 'package:analyzer/src/generated/sdk.dart';
14 import 'package:analyzer/src/generated/source.dart';
15
16 class MockSdk implements DartSdk {
17 static const _MockSdkLibrary LIB_CORE = const _MockSdkLibrary(
18 'dart:core',
19 '/lib/core/core.dart',
20 '''
21 library dart.core;
22
23 import 'dart:async';
24
25 class Object {
26 bool operator ==(other) => identical(this, other);
27 String toString() => 'a string';
28 int get hashCode => 0;
29 }
30
31 class Function {}
32 class StackTrace {}
33 class Symbol {}
34 class Type {}
35
36 abstract class Comparable<T> {
37 int compareTo(T other);
38 }
39
40 abstract class String implements Comparable<String> {
41 external factory String.fromCharCodes(Iterable<int> charCodes,
42 [int start = 0, int end]);
43 bool get isEmpty => false;
44 bool get isNotEmpty => false;
45 int get length => 0;
46 String toUpperCase();
47 List<int> get codeUnits;
48 }
49
50 class bool extends Object {}
51 abstract class num implements Comparable<num> {
52 bool operator <(num other);
53 bool operator <=(num other);
54 bool operator >(num other);
55 bool operator >=(num other);
56 num operator +(num other);
57 num operator -(num other);
58 num operator *(num other);
59 num operator /(num other);
60 int toInt();
61 num abs();
62 int round();
63 }
64 abstract class int extends num {
65 bool get isEven => false;
66 int operator -();
67 external static int parse(String source,
68 { int radix,
69 int onError(String source) });
70 }
71 class double extends num {}
72 class DateTime extends Object {}
73 class Null extends Object {}
74
75 class Deprecated extends Object {
76 final String expires;
77 const Deprecated(this.expires);
78 }
79 const Object deprecated = const Deprecated("next release");
80
81 class Iterator<E> {
82 bool moveNext();
83 E get current;
84 }
85
86 abstract class Iterable<E> {
87 Iterator<E> get iterator;
88 bool get isEmpty;
89 }
90
91 abstract class List<E> implements Iterable<E> {
92 void add(E value);
93 E operator [](int index);
94 void operator []=(int index, E value);
95 Iterator<E> get iterator => null;
96 void clear();
97 }
98
99 abstract class Map<K, V> extends Object {
100 Iterable<K> get keys;
101 }
102
103 external bool identical(Object a, Object b);
104
105 void print(Object object) {}
106
107 class _Override {
108 const _Override();
109 }
110 const Object override = const _Override();
111 ''');
112
113 static const _MockSdkLibrary LIB_ASYNC = const _MockSdkLibrary(
114 'dart:async',
115 '/lib/async/async.dart',
116 '''
117 library dart.async;
118
119 import 'dart:math';
120
121 class Future<T> {
122 factory Future.delayed(Duration duration, [T computation()]) => null;
123 factory Future.value([value]) => null;
124 static Future wait(List<Future> futures) => null;
125 }
126
127 class Stream<T> {}
128 abstract class StreamTransformer<S, T> {}
129 ''');
130
131 static const _MockSdkLibrary LIB_COLLECTION = const _MockSdkLibrary(
132 'dart:collection',
133 '/lib/collection/collection.dart',
134 '''
135 library dart.collection;
136
137 abstract class HashMap<K, V> implements Map<K, V> {}
138 ''');
139
140 static const _MockSdkLibrary LIB_CONVERT = const _MockSdkLibrary(
141 'dart:convert',
142 '/lib/convert/convert.dart',
143 '''
144 library dart.convert;
145
146 import 'dart:async';
147
148 abstract class Converter<S, T> implements StreamTransformer {}
149 class JsonDecoder extends Converter<String, Object> {}
150 ''');
151
152 static const _MockSdkLibrary LIB_MATH = const _MockSdkLibrary(
153 'dart:math',
154 '/lib/math/math.dart',
155 '''
156 library dart.math;
157 const double E = 2.718281828459045;
158 const double PI = 3.1415926535897932;
159 const double LN10 = 2.302585092994046;
160 num min(num a, num b) => 0;
161 num max(num a, num b) => 0;
162 external double cos(num x);
163 external double sin(num x);
164 external double sqrt(num x);
165 class Random {
166 bool nextBool() => true;
167 double nextDouble() => 2.0;
168 int nextInt() => 1;
169 }
170 ''');
171
172 static const _MockSdkLibrary LIB_HTML = const _MockSdkLibrary(
173 'dart:html',
174 '/lib/html/dartium/html_dartium.dart',
175 '''
176 library dart.html;
177 class HtmlElement {}
178 ''');
179
180 static const List<SdkLibrary> LIBRARIES = const [
181 LIB_CORE,
182 LIB_ASYNC,
183 LIB_COLLECTION,
184 LIB_CONVERT,
185 LIB_MATH,
186 LIB_HTML,
187 ];
188
189 final resource.MemoryResourceProvider provider =
190 new resource.MemoryResourceProvider();
191
192 /**
193 * The [AnalysisContextImpl] which is used for all of the sources.
194 */
195 AnalysisContextImpl _analysisContext;
196
197 MockSdk() {
198 LIBRARIES.forEach((_MockSdkLibrary library) {
199 provider.newFile(library.path, library.content);
200 });
201 }
202
203 @override
204 AnalysisContextImpl get context {
205 if (_analysisContext == null) {
206 _analysisContext = new _SdkAnalysisContext(this);
207 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]);
208 _analysisContext.sourceFactory = factory;
209 ChangeSet changeSet = new ChangeSet();
210 for (String uri in uris) {
211 Source source = factory.forUri(uri);
212 changeSet.addedSource(source);
213 }
214 _analysisContext.applyChanges(changeSet);
215 }
216 return _analysisContext;
217 }
218
219 @override
220 List<SdkLibrary> get sdkLibraries => LIBRARIES;
221
222 @override
223 String get sdkVersion => throw unimplemented;
224
225 UnimplementedError get unimplemented => new UnimplementedError();
226
227 @override
228 List<String> get uris {
229 List<String> uris = <String>[];
230 for (SdkLibrary library in LIBRARIES) {
231 uris.add(library.shortName);
232 }
233 return uris;
234 }
235
236 @override
237 Source fromFileUri(Uri uri) {
238 String filePath = uri.path;
239 String libPath = '/lib';
240 if (!filePath.startsWith("$libPath/")) {
241 return null;
242 }
243 for (SdkLibrary library in LIBRARIES) {
244 String libraryPath = library.path;
245 if (filePath.replaceAll('\\', '/') == libraryPath) {
246 try {
247 resource.File file = provider.getResource(uri.path);
248 Uri dartUri = Uri.parse(library.shortName);
249 return file.createSource(dartUri);
250 } catch (exception) {
251 return null;
252 }
253 }
254 if (filePath.startsWith("$libraryPath/")) {
255 String pathInLibrary = filePath.substring(libraryPath.length + 1);
256 String path = '${library.shortName}/${pathInLibrary}';
257 try {
258 resource.File file = provider.getResource(uri.path);
259 Uri dartUri = new Uri(scheme: 'dart', path: path);
260 return file.createSource(dartUri);
261 } catch (exception) {
262 return null;
263 }
264 }
265 }
266 return null;
267 }
268
269 @override
270 SdkLibrary getSdkLibrary(String dartUri) {
271 // getSdkLibrary() is only used to determine whether a library is internal
272 // to the SDK. The mock SDK doesn't have any internals, so it's safe to
273 // return null.
274 return null;
275 }
276
277 @override
278 Source mapDartUri(String dartUri) {
279 const Map<String, String> uriToPath = const {
280 "dart:core": "/lib/core/core.dart",
281 "dart:html": "/lib/html/dartium/html_dartium.dart",
282 "dart:async": "/lib/async/async.dart",
283 "dart:collection": "/lib/collection/collection.dart",
284 "dart:convert": "/lib/convert/convert.dart",
285 "dart:math": "/lib/math/math.dart"
286 };
287
288 String path = uriToPath[dartUri];
289 if (path != null) {
290 resource.File file = provider.getResource(path);
291 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5));
292 return file.createSource(uri);
293 }
294
295 // If we reach here then we tried to use a dartUri that's not in the
296 // table above.
297 return null;
298 }
299 }
300
301 class _MockSdkLibrary implements SdkLibrary {
302 final String shortName;
303 final String path;
304 final String content;
305
306 const _MockSdkLibrary(this.shortName, this.path, this.content);
307
308 @override
309 String get category => throw unimplemented;
310
311 @override
312 bool get isDart2JsLibrary => throw unimplemented;
313
314 @override
315 bool get isDocumented => throw unimplemented;
316
317 @override
318 bool get isImplementation => throw unimplemented;
319
320 @override
321 bool get isInternal => throw unimplemented;
322
323 @override
324 bool get isShared => throw unimplemented;
325
326 @override
327 bool get isVmLibrary => throw unimplemented;
328
329 UnimplementedError get unimplemented => new UnimplementedError();
330 }
331
332 /**
333 * An [AnalysisContextImpl] that only contains sources for a Dart SDK.
334 */
335 class _SdkAnalysisContext extends AnalysisContextImpl {
336 final DartSdk sdk;
337
338 _SdkAnalysisContext(this.sdk);
339
340 @override
341 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) {
342 if (factory == null) {
343 return super.createCacheFromSourceFactory(factory);
344 }
345 return new AnalysisCache(<CachePartition>[
346 AnalysisEngine.instance.partitionManager_new.forSdk(sdk)
347 ]);
348 }
349 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/src/context/context_test.dart ('k') | packages/analyzer/test/src/context/test_all.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698