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