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/context.dart' as newContext; |
| 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 final resource.MemoryResourceProvider provider = |
| 188 new resource.MemoryResourceProvider(); |
| 189 |
| 190 /** |
| 191 * The [AnalysisContext] which is used for all of the sources. |
| 192 */ |
| 193 InternalAnalysisContext _analysisContext; |
| 194 |
| 195 MockSdk() { |
| 196 LIBRARIES.forEach((_MockSdkLibrary library) { |
| 197 provider.newFile(library.path, library.content); |
| 198 }); |
| 199 } |
| 200 |
| 201 @override |
| 202 AnalysisContext get context { |
| 203 if (_analysisContext == null) { |
| 204 if (AnalysisEngine.instance.useTaskModel) { |
| 205 _analysisContext = new newContext.SdkAnalysisContext(); |
| 206 } else { |
| 207 _analysisContext = new SdkAnalysisContext(); |
| 208 } |
| 209 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); |
| 210 _analysisContext.sourceFactory = factory; |
| 211 ChangeSet changeSet = new ChangeSet(); |
| 212 for (String uri in uris) { |
| 213 Source source = factory.forUri(uri); |
| 214 changeSet.addedSource(source); |
| 215 } |
| 216 _analysisContext.applyChanges(changeSet); |
| 217 } |
| 218 return _analysisContext; |
| 219 } |
| 220 |
| 221 @override |
| 222 List<SdkLibrary> get sdkLibraries => LIBRARIES; |
| 223 |
| 224 @override |
| 225 String get sdkVersion => throw unimplemented; |
| 226 |
| 227 UnimplementedError get unimplemented => new UnimplementedError(); |
| 228 |
| 229 @override |
| 230 List<String> get uris { |
| 231 List<String> uris = <String>[]; |
| 232 for (SdkLibrary library in LIBRARIES) { |
| 233 uris.add(library.shortName); |
| 234 } |
| 235 return uris; |
| 236 } |
| 237 |
| 238 @override |
| 239 Source fromFileUri(Uri uri) { |
| 240 String filePath = uri.path; |
| 241 String libPath = '/lib'; |
| 242 if (!filePath.startsWith("$libPath/")) { |
| 243 return null; |
| 244 } |
| 245 for (SdkLibrary library in LIBRARIES) { |
| 246 String libraryPath = library.path; |
| 247 if (filePath.replaceAll('\\', '/') == libraryPath) { |
| 248 try { |
| 249 resource.File file = provider.getResource(uri.path); |
| 250 Uri dartUri = Uri.parse(library.shortName); |
| 251 return file.createSource(dartUri); |
| 252 } catch (exception) { |
| 253 return null; |
| 254 } |
| 255 } |
| 256 if (filePath.startsWith("$libraryPath/")) { |
| 257 String pathInLibrary = filePath.substring(libraryPath.length + 1); |
| 258 String path = '${library.shortName}/${pathInLibrary}'; |
| 259 try { |
| 260 resource.File file = provider.getResource(uri.path); |
| 261 Uri dartUri = new Uri(scheme: 'dart', path: path); |
| 262 return file.createSource(dartUri); |
| 263 } catch (exception) { |
| 264 return null; |
| 265 } |
| 266 } |
| 267 } |
| 268 return null; |
| 269 } |
| 270 |
| 271 @override |
| 272 SdkLibrary getSdkLibrary(String dartUri) { |
| 273 // getSdkLibrary() is only used to determine whether a library is internal |
| 274 // to the SDK. The mock SDK doesn't have any internals, so it's safe to |
| 275 // return null. |
| 276 return null; |
| 277 } |
| 278 |
| 279 @override |
| 280 Source mapDartUri(String dartUri) { |
| 281 const Map<String, String> uriToPath = const { |
| 282 "dart:core": "/lib/core/core.dart", |
| 283 "dart:html": "/lib/html/dartium/html_dartium.dart", |
| 284 "dart:async": "/lib/async/async.dart", |
| 285 "dart:collection": "/lib/collection/collection.dart", |
| 286 "dart:convert": "/lib/convert/convert.dart", |
| 287 "dart:math": "/lib/math/math.dart" |
| 288 }; |
| 289 |
| 290 String path = uriToPath[dartUri]; |
| 291 if (path != null) { |
| 292 resource.File file = provider.getResource(path); |
| 293 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); |
| 294 return file.createSource(uri); |
| 295 } |
| 296 |
| 297 // If we reach here then we tried to use a dartUri that's not in the |
| 298 // table above. |
| 299 return null; |
| 300 } |
| 301 } |
| 302 |
| 303 class _MockSdkLibrary implements SdkLibrary { |
| 304 final String shortName; |
| 305 final String path; |
| 306 final String content; |
| 307 |
| 308 const _MockSdkLibrary(this.shortName, this.path, this.content); |
| 309 |
| 310 @override |
| 311 String get category => throw unimplemented; |
| 312 |
| 313 @override |
| 314 bool get isDart2JsLibrary => throw unimplemented; |
| 315 |
| 316 @override |
| 317 bool get isDocumented => throw unimplemented; |
| 318 |
| 319 @override |
| 320 bool get isImplementation => throw unimplemented; |
| 321 |
| 322 @override |
| 323 bool get isInternal => throw unimplemented; |
| 324 |
| 325 @override |
| 326 bool get isShared => throw unimplemented; |
| 327 |
| 328 @override |
| 329 bool get isVmLibrary => throw unimplemented; |
| 330 |
| 331 UnimplementedError get unimplemented => new UnimplementedError(); |
| 332 } |
OLD | NEW |