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