| 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 analyzer.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/context.dart'; | |
| 10 import 'package:analyzer/src/generated/engine.dart'; | |
| 11 import 'package:analyzer/src/generated/sdk.dart'; | |
| 12 import 'package:analyzer/src/generated/source.dart'; | |
| 13 | |
| 14 class MockSdk implements DartSdk { | |
| 15 static const _MockSdkLibrary LIB_CORE = const _MockSdkLibrary( | |
| 16 'dart:core', | |
| 17 '/lib/core/core.dart', | |
| 18 ''' | |
| 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( | |
| 112 'dart:async', | |
| 113 '/lib/async/async.dart', | |
| 114 ''' | |
| 115 library dart.async; | |
| 116 | |
| 117 import 'dart:math'; | |
| 118 | |
| 119 class Future<T> { | |
| 120 factory Future.delayed(Duration duration, [T computation()]) => null; | |
| 121 factory Future.value([value]) => null; | |
| 122 static Future wait(List<Future> futures) => null; | |
| 123 } | |
| 124 | |
| 125 class Stream<T> {} | |
| 126 abstract class StreamTransformer<S, T> {} | |
| 127 '''); | |
| 128 | |
| 129 static const _MockSdkLibrary LIB_COLLECTION = const _MockSdkLibrary( | |
| 130 'dart:collection', | |
| 131 '/lib/collection/collection.dart', | |
| 132 ''' | |
| 133 library dart.collection; | |
| 134 | |
| 135 abstract class HashMap<K, V> implements Map<K, V> {} | |
| 136 '''); | |
| 137 | |
| 138 static const _MockSdkLibrary LIB_CONVERT = const _MockSdkLibrary( | |
| 139 'dart:convert', | |
| 140 '/lib/convert/convert.dart', | |
| 141 ''' | |
| 142 library dart.convert; | |
| 143 | |
| 144 import 'dart:async'; | |
| 145 | |
| 146 abstract class Converter<S, T> implements StreamTransformer {} | |
| 147 class JsonDecoder extends Converter<String, Object> {} | |
| 148 '''); | |
| 149 | |
| 150 static const _MockSdkLibrary LIB_MATH = const _MockSdkLibrary( | |
| 151 'dart:math', | |
| 152 '/lib/math/math.dart', | |
| 153 ''' | |
| 154 library dart.math; | |
| 155 const double E = 2.718281828459045; | |
| 156 const double PI = 3.1415926535897932; | |
| 157 const double LN10 = 2.302585092994046; | |
| 158 num min(num a, num b) => 0; | |
| 159 num max(num a, num b) => 0; | |
| 160 external double cos(num x); | |
| 161 external double sin(num x); | |
| 162 external double sqrt(num x); | |
| 163 class Random { | |
| 164 bool nextBool() => true; | |
| 165 double nextDouble() => 2.0; | |
| 166 int nextInt() => 1; | |
| 167 } | |
| 168 '''); | |
| 169 | |
| 170 static const _MockSdkLibrary LIB_HTML = const _MockSdkLibrary( | |
| 171 'dart:html', | |
| 172 '/lib/html/dartium/html_dartium.dart', | |
| 173 ''' | |
| 174 library dart.html; | |
| 175 class HtmlElement {} | |
| 176 '''); | |
| 177 | |
| 178 static const List<SdkLibrary> LIBRARIES = const [ | |
| 179 LIB_CORE, | |
| 180 LIB_ASYNC, | |
| 181 LIB_COLLECTION, | |
| 182 LIB_CONVERT, | |
| 183 LIB_MATH, | |
| 184 LIB_HTML, | |
| 185 ]; | |
| 186 | |
| 187 static const List<SdkLibrary> LIBRARIES_NO_ASYNC = const [ | |
| 188 LIB_CORE, | |
| 189 ]; | |
| 190 | |
| 191 final resource.MemoryResourceProvider provider = | |
| 192 new resource.MemoryResourceProvider(); | |
| 193 | |
| 194 /** | |
| 195 * The [AnalysisContext] which is used for all of the sources. | |
| 196 */ | |
| 197 InternalAnalysisContext _analysisContext; | |
| 198 | |
| 199 MockSdk({bool dartAsync: true}) { | |
| 200 List<SdkLibrary> libraries = dartAsync ? LIBRARIES : LIBRARIES_NO_ASYNC; | |
| 201 libraries.forEach((_MockSdkLibrary library) { | |
| 202 provider.newFile(library.path, library.content); | |
| 203 }); | |
| 204 } | |
| 205 | |
| 206 @override | |
| 207 AnalysisContext get context { | |
| 208 if (_analysisContext == null) { | |
| 209 _analysisContext = new SdkAnalysisContext(); | |
| 210 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); | |
| 211 _analysisContext.sourceFactory = factory; | |
| 212 } | |
| 213 return _analysisContext; | |
| 214 } | |
| 215 | |
| 216 @override | |
| 217 List<SdkLibrary> get sdkLibraries => LIBRARIES; | |
| 218 | |
| 219 @override | |
| 220 String get sdkVersion => throw unimplemented; | |
| 221 | |
| 222 UnimplementedError get unimplemented => new UnimplementedError(); | |
| 223 | |
| 224 @override | |
| 225 List<String> get uris { | |
| 226 List<String> uris = <String>[]; | |
| 227 for (SdkLibrary library in LIBRARIES) { | |
| 228 uris.add(library.shortName); | |
| 229 } | |
| 230 return uris; | |
| 231 } | |
| 232 | |
| 233 @override | |
| 234 Source fromFileUri(Uri uri) { | |
| 235 String filePath = uri.path; | |
| 236 String libPath = '/lib'; | |
| 237 if (!filePath.startsWith("$libPath/")) { | |
| 238 return null; | |
| 239 } | |
| 240 for (SdkLibrary library in LIBRARIES) { | |
| 241 String libraryPath = library.path; | |
| 242 if (filePath.replaceAll('\\', '/') == libraryPath) { | |
| 243 try { | |
| 244 resource.File file = provider.getResource(uri.path); | |
| 245 Uri dartUri = Uri.parse(library.shortName); | |
| 246 return file.createSource(dartUri); | |
| 247 } catch (exception) { | |
| 248 return null; | |
| 249 } | |
| 250 } | |
| 251 if (filePath.startsWith("$libraryPath/")) { | |
| 252 String pathInLibrary = filePath.substring(libraryPath.length + 1); | |
| 253 String path = '${library.shortName}/$pathInLibrary'; | |
| 254 try { | |
| 255 resource.File file = provider.getResource(uri.path); | |
| 256 Uri dartUri = new Uri(scheme: 'dart', path: path); | |
| 257 return file.createSource(dartUri); | |
| 258 } catch (exception) { | |
| 259 return null; | |
| 260 } | |
| 261 } | |
| 262 } | |
| 263 return null; | |
| 264 } | |
| 265 | |
| 266 @override | |
| 267 SdkLibrary getSdkLibrary(String dartUri) { | |
| 268 // getSdkLibrary() is only used to determine whether a library is internal | |
| 269 // to the SDK. The mock SDK doesn't have any internals, so it's safe to | |
| 270 // return null. | |
| 271 return null; | |
| 272 } | |
| 273 | |
| 274 @override | |
| 275 Source mapDartUri(String dartUri) { | |
| 276 const Map<String, String> uriToPath = const { | |
| 277 "dart:core": "/lib/core/core.dart", | |
| 278 "dart:html": "/lib/html/dartium/html_dartium.dart", | |
| 279 "dart:async": "/lib/async/async.dart", | |
| 280 "dart:collection": "/lib/collection/collection.dart", | |
| 281 "dart:convert": "/lib/convert/convert.dart", | |
| 282 "dart:math": "/lib/math/math.dart" | |
| 283 }; | |
| 284 | |
| 285 String path = uriToPath[dartUri]; | |
| 286 if (path != null) { | |
| 287 resource.File file = provider.getResource(path); | |
| 288 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); | |
| 289 return file.createSource(uri); | |
| 290 } | |
| 291 | |
| 292 // If we reach here then we tried to use a dartUri that's not in the | |
| 293 // table above. | |
| 294 return null; | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 class _MockSdkLibrary implements SdkLibrary { | |
| 299 final String shortName; | |
| 300 final String path; | |
| 301 final String content; | |
| 302 | |
| 303 const _MockSdkLibrary(this.shortName, this.path, this.content); | |
| 304 | |
| 305 @override | |
| 306 String get category => throw unimplemented; | |
| 307 | |
| 308 @override | |
| 309 bool get isDart2JsLibrary => throw unimplemented; | |
| 310 | |
| 311 @override | |
| 312 bool get isDocumented => throw unimplemented; | |
| 313 | |
| 314 @override | |
| 315 bool get isImplementation => throw unimplemented; | |
| 316 | |
| 317 @override | |
| 318 bool get isInternal => throw unimplemented; | |
| 319 | |
| 320 @override | |
| 321 bool get isShared => throw unimplemented; | |
| 322 | |
| 323 @override | |
| 324 bool get isVmLibrary => throw unimplemented; | |
| 325 | |
| 326 UnimplementedError get unimplemented => new UnimplementedError(); | |
| 327 } | |
| OLD | NEW |