| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.src.mock_sdk; | 5 library analyzer.test.src.context.mock_sdk; |
| 6 | 6 |
| 7 import 'package:analyzer/file_system/file_system.dart' as resource; | 7 import 'package:analyzer/file_system/file_system.dart' as resource; |
| 8 import 'package:analyzer/file_system/memory_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'; | 9 import 'package:analyzer/src/context/cache.dart'; |
| 10 import 'package:analyzer/src/context/context.dart'; | 10 import 'package:analyzer/src/context/context.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart' | 11 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine; |
| 12 show AnalysisEngine, ChangeSet; | |
| 13 import 'package:analyzer/src/generated/sdk.dart'; | 12 import 'package:analyzer/src/generated/sdk.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
| 14 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; |
| 15 import 'package:analyzer/src/summary/summary_file_builder.dart'; |
| 15 | 16 |
| 16 class MockSdk implements DartSdk { | 17 const String librariesContent = r''' |
| 17 static const _MockSdkLibrary LIB_CORE = const _MockSdkLibrary( | 18 const Map<String, LibraryInfo> libraries = const { |
| 18 'dart:core', | 19 "async": const LibraryInfo("async/async.dart"), |
| 19 '/lib/core/core.dart', | 20 "collection": const LibraryInfo("collection/collection.dart"), |
| 20 ''' | 21 "convert": const LibraryInfo("convert/convert.dart"), |
| 22 "core": const LibraryInfo("core/core.dart"), |
| 23 "html": const LibraryInfo("html/dartium/html_dartium.dart"), |
| 24 "math": const LibraryInfo("math/math.dart"), |
| 25 "_foreign_helper": const LibraryInfo("_internal/js_runtime/lib/foreign_helper.
dart"), |
| 26 }; |
| 27 '''; |
| 28 |
| 29 const String sdkRoot = '/sdk'; |
| 30 |
| 31 const _MockSdkLibrary _LIB_ASYNC = const _MockSdkLibrary( |
| 32 'dart:async', |
| 33 '$sdkRoot/lib/async/async.dart', |
| 34 ''' |
| 35 library dart.async; |
| 36 |
| 37 import 'dart:math'; |
| 38 |
| 39 part 'stream.dart'; |
| 40 |
| 41 class Future<T> { |
| 42 factory Future(computation()) => null; |
| 43 factory Future.delayed(Duration duration, [T computation()]) => null; |
| 44 factory Future.value([T value]) => null; |
| 45 |
| 46 static Future<List/*<T>*/> wait/*<T>*/( |
| 47 Iterable<Future/*<T>*/> futures) => null; |
| 48 Future/*<R>*/ then/*<R>*/(onValue(T value)) => null; |
| 49 |
| 50 Future<T> whenComplete(action()); |
| 51 } |
| 52 |
| 53 abstract class Completer<T> { |
| 54 factory Completer() => new _AsyncCompleter<T>(); |
| 55 factory Completer.sync() => new _SyncCompleter<T>(); |
| 56 Future<T> get future; |
| 57 void complete([value]); |
| 58 void completeError(Object error, [StackTrace stackTrace]); |
| 59 bool get isCompleted; |
| 60 } |
| 61 ''', |
| 62 const <String, String>{ |
| 63 '$sdkRoot/lib/async/stream.dart': r''' |
| 64 part of dart.async; |
| 65 class Stream<T> { |
| 66 Future<T> get first; |
| 67 } |
| 68 abstract class StreamTransformer<S, T> {} |
| 69 ''' |
| 70 }); |
| 71 |
| 72 const _MockSdkLibrary _LIB_COLLECTION = const _MockSdkLibrary( |
| 73 'dart:collection', |
| 74 '$sdkRoot/lib/collection/collection.dart', |
| 75 ''' |
| 76 library dart.collection; |
| 77 |
| 78 abstract class HashMap<K, V> implements Map<K, V> {} |
| 79 '''); |
| 80 |
| 81 const _MockSdkLibrary _LIB_CONVERT = const _MockSdkLibrary( |
| 82 'dart:convert', |
| 83 '$sdkRoot/lib/convert/convert.dart', |
| 84 ''' |
| 85 library dart.convert; |
| 86 |
| 87 import 'dart:async'; |
| 88 |
| 89 abstract class Converter<S, T> implements StreamTransformer {} |
| 90 class JsonDecoder extends Converter<String, Object> {} |
| 91 '''); |
| 92 |
| 93 const _MockSdkLibrary _LIB_CORE = const _MockSdkLibrary( |
| 94 'dart:core', |
| 95 '$sdkRoot/lib/core/core.dart', |
| 96 ''' |
| 21 library dart.core; | 97 library dart.core; |
| 22 | 98 |
| 23 import 'dart:async'; | 99 import 'dart:async'; |
| 24 | 100 |
| 25 class Object { | 101 class Object { |
| 26 bool operator ==(other) => identical(this, other); | 102 bool operator ==(other) => identical(this, other); |
| 27 String toString() => 'a string'; | 103 String toString() => 'a string'; |
| 28 int get hashCode => 0; | 104 int get hashCode => 0; |
| 29 } | 105 } |
| 30 | 106 |
| 31 class Function {} | 107 class Function {} |
| 32 class StackTrace {} | 108 class StackTrace {} |
| 33 class Symbol {} | 109 class Symbol {} |
| 34 class Type {} | 110 class Type {} |
| 35 | 111 |
| 36 abstract class Comparable<T> { | 112 abstract class Comparable<T> { |
| 37 int compareTo(T other); | 113 int compareTo(T other); |
| 38 } | 114 } |
| 39 | 115 |
| 40 abstract class String implements Comparable<String> { | 116 abstract class Pattern {} |
| 117 abstract class String implements Comparable<String>, Pattern { |
| 41 external factory String.fromCharCodes(Iterable<int> charCodes, | 118 external factory String.fromCharCodes(Iterable<int> charCodes, |
| 42 [int start = 0, int end]); | 119 [int start = 0, int end]); |
| 120 String operator +(String other) => null; |
| 43 bool get isEmpty => false; | 121 bool get isEmpty => false; |
| 44 bool get isNotEmpty => false; | 122 bool get isNotEmpty => false; |
| 45 int get length => 0; | 123 int get length => 0; |
| 124 String substring(int len) => null; |
| 46 String toUpperCase(); | 125 String toUpperCase(); |
| 47 List<int> get codeUnits; | 126 List<int> get codeUnits; |
| 48 } | 127 } |
| 128 abstract class RegExp implements Pattern { |
| 129 external factory RegExp(String source); |
| 130 } |
| 49 | 131 |
| 50 class bool extends Object {} | 132 class bool extends Object {} |
| 51 abstract class num implements Comparable<num> { | 133 abstract class num implements Comparable<num> { |
| 52 bool operator <(num other); | 134 bool operator <(num other); |
| 53 bool operator <=(num other); | 135 bool operator <=(num other); |
| 54 bool operator >(num other); | 136 bool operator >(num other); |
| 55 bool operator >=(num other); | 137 bool operator >=(num other); |
| 56 num operator +(num other); | 138 num operator +(num other); |
| 57 num operator -(num other); | 139 num operator -(num other); |
| 58 num operator *(num other); | 140 num operator *(num other); |
| 59 num operator /(num other); | 141 num operator /(num other); |
| 142 int operator ^(int other); |
| 143 int operator &(int other); |
| 144 int operator |(int other); |
| 145 int operator <<(int other); |
| 146 int operator >>(int other); |
| 147 int operator ~/(num other); |
| 148 num operator %(num other); |
| 149 int operator ~(); |
| 60 int toInt(); | 150 int toInt(); |
| 151 double toDouble(); |
| 61 num abs(); | 152 num abs(); |
| 62 int round(); | 153 int round(); |
| 63 } | 154 } |
| 64 abstract class int extends num { | 155 abstract class int extends num { |
| 65 bool get isEven => false; | 156 bool get isEven => false; |
| 66 int operator -(); | 157 int operator -(); |
| 67 external static int parse(String source, | 158 external static int parse(String source, |
| 68 { int radix, | 159 { int radix, |
| 69 int onError(String source) }); | 160 int onError(String source) }); |
| 70 } | 161 } |
| 71 class double extends num {} | 162 |
| 163 abstract class double extends num { |
| 164 static const double NAN = 0.0 / 0.0; |
| 165 static const double INFINITY = 1.0 / 0.0; |
| 166 static const double NEGATIVE_INFINITY = -INFINITY; |
| 167 static const double MIN_POSITIVE = 5e-324; |
| 168 static const double MAX_FINITE = 1.7976931348623157e+308; |
| 169 |
| 170 double remainder(num other); |
| 171 double operator +(num other); |
| 172 double operator -(num other); |
| 173 double operator *(num other); |
| 174 double operator %(num other); |
| 175 double operator /(num other); |
| 176 int operator ~/(num other); |
| 177 double operator -(); |
| 178 double abs(); |
| 179 double get sign; |
| 180 int round(); |
| 181 int floor(); |
| 182 int ceil(); |
| 183 int truncate(); |
| 184 double roundToDouble(); |
| 185 double floorToDouble(); |
| 186 double ceilToDouble(); |
| 187 double truncateToDouble(); |
| 188 external static double parse(String source, |
| 189 [double onError(String source)]); |
| 190 } |
| 191 |
| 72 class DateTime extends Object {} | 192 class DateTime extends Object {} |
| 73 class Null extends Object {} | 193 class Null extends Object {} |
| 74 | 194 |
| 75 class Deprecated extends Object { | 195 class Deprecated extends Object { |
| 76 final String expires; | 196 final String expires; |
| 77 const Deprecated(this.expires); | 197 const Deprecated(this.expires); |
| 78 } | 198 } |
| 79 const Object deprecated = const Deprecated("next release"); | 199 const Object deprecated = const Deprecated("next release"); |
| 80 | 200 |
| 81 class Iterator<E> { | 201 class Iterator<E> { |
| 82 bool moveNext(); | 202 bool moveNext(); |
| 83 E get current; | 203 E get current; |
| 84 } | 204 } |
| 85 | 205 |
| 86 abstract class Iterable<E> { | 206 abstract class Iterable<E> { |
| 87 Iterator<E> get iterator; | 207 Iterator<E> get iterator; |
| 88 bool get isEmpty; | 208 bool get isEmpty; |
| 209 E get first; |
| 210 |
| 211 Iterable/*<R>*/ map/*<R>*/(/*=R*/ f(E e)); |
| 212 |
| 213 /*=R*/ fold/*<R>*/(/*=R*/ initialValue, |
| 214 /*=R*/ combine(/*=R*/ previousValue, E element)); |
| 215 |
| 216 Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(E element)); |
| 217 |
| 218 List<E> toList(); |
| 89 } | 219 } |
| 90 | 220 |
| 91 abstract class List<E> implements Iterable<E> { | 221 class List<E> implements Iterable<E> { |
| 92 void add(E value); | 222 List(); |
| 93 E operator [](int index); | 223 void add(E value) {} |
| 94 void operator []=(int index, E value); | 224 void addAll(Iterable<E> iterable) {} |
| 225 E operator [](int index) => null; |
| 226 void operator []=(int index, E value) {} |
| 95 Iterator<E> get iterator => null; | 227 Iterator<E> get iterator => null; |
| 96 void clear(); | 228 void clear() {} |
| 229 |
| 230 bool get isEmpty => false; |
| 231 E get first => null; |
| 232 |
| 233 Iterable/*<R>*/ map/*<R>*/(/*=R*/ f(E e)) => null; |
| 234 |
| 235 /*=R*/ fold/*<R>*/(/*=R*/ initialValue, |
| 236 /*=R*/ combine(/*=R*/ previousValue, E element)) => null; |
| 237 |
| 97 } | 238 } |
| 98 | 239 |
| 99 abstract class Map<K, V> extends Object { | 240 class Map<K, V> extends Object { |
| 100 Iterable<K> get keys; | 241 Iterable<K> get keys => null; |
| 242 V operator [](K key) => null; |
| 243 void operator []=(K key, V value) {} |
| 101 } | 244 } |
| 102 | 245 |
| 103 external bool identical(Object a, Object b); | 246 external bool identical(Object a, Object b); |
| 104 | 247 |
| 105 void print(Object object) {} | 248 void print(Object object) {} |
| 106 | 249 |
| 107 class _Override { | 250 class _Proxy { const _Proxy(); } |
| 108 const _Override(); | 251 const Object proxy = const _Proxy(); |
| 109 } | 252 |
| 253 class _Override { const _Override(); } |
| 110 const Object override = const _Override(); | 254 const Object override = const _Override(); |
| 111 '''); | 255 '''); |
| 112 | 256 |
| 113 static const _MockSdkLibrary LIB_ASYNC = const _MockSdkLibrary( | 257 const _MockSdkLibrary _LIB_FOREIGN_HELPER = const _MockSdkLibrary( |
| 114 'dart:async', | 258 'dart:_foreign_helper', |
| 115 '/lib/async/async.dart', | 259 '$sdkRoot/lib/_foreign_helper/_foreign_helper.dart', |
| 116 ''' | 260 ''' |
| 117 library dart.async; | 261 library dart._foreign_helper; |
| 118 | 262 |
| 119 import 'dart:math'; | 263 JS(String typeDescription, String codeTemplate, |
| 120 | 264 [arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11]) |
| 121 part 'stream.dart'; | 265 {} |
| 122 | |
| 123 class Future<T> { | |
| 124 factory Future.delayed(Duration duration, [T computation()]) => null; | |
| 125 factory Future.value([value]) => null; | |
| 126 static Future wait(List<Future> futures) => null; | |
| 127 } | |
| 128 ''', | |
| 129 const <_MockSdkFile>[ | |
| 130 const _MockSdkFile( | |
| 131 '/lib/async/stream.dart', | |
| 132 r''' | |
| 133 part of dart.async; | |
| 134 class Stream<T> {} | |
| 135 abstract class StreamTransformer<S, T> {} | |
| 136 ''') | |
| 137 ]); | |
| 138 | |
| 139 static const _MockSdkLibrary LIB_COLLECTION = const _MockSdkLibrary( | |
| 140 'dart:collection', | |
| 141 '/lib/collection/collection.dart', | |
| 142 ''' | |
| 143 library dart.collection; | |
| 144 | |
| 145 abstract class HashMap<K, V> implements Map<K, V> {} | |
| 146 '''); | 266 '''); |
| 147 | 267 |
| 148 static const _MockSdkLibrary LIB_CONVERT = const _MockSdkLibrary( | 268 const _MockSdkLibrary _LIB_HTML = const _MockSdkLibrary( |
| 149 'dart:convert', | 269 'dart:html', |
| 150 '/lib/convert/convert.dart', | 270 '$sdkRoot/lib/html/dartium/html_dartium.dart', |
| 151 ''' | 271 ''' |
| 152 library dart.convert; | 272 library dart.html; |
| 153 | 273 class HtmlElement {} |
| 154 import 'dart:async'; | |
| 155 | |
| 156 abstract class Converter<S, T> implements StreamTransformer {} | |
| 157 class JsonDecoder extends Converter<String, Object> {} | |
| 158 '''); | 274 '''); |
| 159 | 275 |
| 160 static const _MockSdkLibrary LIB_MATH = const _MockSdkLibrary( | 276 const _MockSdkLibrary _LIB_MATH = const _MockSdkLibrary( |
| 161 'dart:math', | 277 'dart:math', |
| 162 '/lib/math/math.dart', | 278 '$sdkRoot/lib/math/math.dart', |
| 163 ''' | 279 ''' |
| 164 library dart.math; | 280 library dart.math; |
| 281 |
| 165 const double E = 2.718281828459045; | 282 const double E = 2.718281828459045; |
| 166 const double PI = 3.1415926535897932; | 283 const double PI = 3.1415926535897932; |
| 167 const double LN10 = 2.302585092994046; | 284 const double LN10 = 2.302585092994046; |
| 168 num min(num a, num b) => 0; | 285 |
| 169 num max(num a, num b) => 0; | 286 num/*=T*/ min/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; |
| 287 num/*=T*/ max/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; |
| 288 |
| 170 external double cos(num x); | 289 external double cos(num x); |
| 171 external double sin(num x); | 290 external double sin(num x); |
| 172 external double sqrt(num x); | 291 external double sqrt(num x); |
| 173 class Random { | 292 class Random { |
| 174 bool nextBool() => true; | 293 bool nextBool() => true; |
| 175 double nextDouble() => 2.0; | 294 double nextDouble() => 2.0; |
| 176 int nextInt() => 1; | 295 int nextInt() => 1; |
| 177 } | 296 } |
| 178 '''); | 297 '''); |
| 179 | 298 |
| 180 static const _MockSdkLibrary LIB_HTML = const _MockSdkLibrary( | 299 const List<SdkLibrary> _LIBRARIES = const [ |
| 181 'dart:html', | 300 _LIB_CORE, |
| 182 '/lib/html/dartium/html_dartium.dart', | 301 _LIB_ASYNC, |
| 183 ''' | 302 _LIB_COLLECTION, |
| 184 library dart.html; | 303 _LIB_CONVERT, |
| 185 class HtmlElement {} | 304 _LIB_FOREIGN_HELPER, |
| 186 '''); | 305 _LIB_MATH, |
| 306 _LIB_HTML, |
| 307 ]; |
| 187 | 308 |
| 188 static const List<SdkLibrary> LIBRARIES = const [ | 309 class MockSdk implements DartSdk { |
| 189 LIB_CORE, | 310 static const Map<String, String> FULL_URI_MAP = const { |
| 190 LIB_ASYNC, | 311 "dart:core": "$sdkRoot/lib/core/core.dart", |
| 191 LIB_COLLECTION, | 312 "dart:html": "$sdkRoot/lib/html/dartium/html_dartium.dart", |
| 192 LIB_CONVERT, | 313 "dart:async": "$sdkRoot/lib/async/async.dart", |
| 193 LIB_MATH, | 314 "dart:async/stream.dart": "$sdkRoot/lib/async/stream.dart", |
| 194 LIB_HTML, | 315 "dart:collection": "$sdkRoot/lib/collection/collection.dart", |
| 195 ]; | 316 "dart:convert": "$sdkRoot/lib/convert/convert.dart", |
| 317 "dart:_foreign_helper": "$sdkRoot/lib/_foreign_helper/_foreign_helper.dart", |
| 318 "dart:math": "$sdkRoot/lib/math/math.dart" |
| 319 }; |
| 196 | 320 |
| 197 final resource.MemoryResourceProvider provider = | 321 static const Map<String, String> NO_ASYNC_URI_MAP = const { |
| 198 new resource.MemoryResourceProvider(); | 322 "dart:core": "$sdkRoot/lib/core/core.dart", |
| 323 }; |
| 324 |
| 325 final resource.MemoryResourceProvider provider; |
| 326 |
| 327 final Map<String, String> uriMap; |
| 199 | 328 |
| 200 /** | 329 /** |
| 201 * The [AnalysisContextImpl] which is used for all of the sources. | 330 * The [AnalysisContextImpl] which is used for all of the sources. |
| 202 */ | 331 */ |
| 203 AnalysisContextImpl _analysisContext; | 332 AnalysisContextImpl _analysisContext; |
| 204 | 333 |
| 205 MockSdk() { | 334 @override |
| 206 LIBRARIES.forEach((_MockSdkLibrary library) { | 335 final List<SdkLibrary> sdkLibraries; |
| 336 |
| 337 /** |
| 338 * The cached linked bundle of the SDK. |
| 339 */ |
| 340 PackageBundle _bundle; |
| 341 |
| 342 MockSdk({bool dartAsync: true, resource.ResourceProvider resourceProvider}) |
| 343 : provider = resourceProvider ?? new resource.MemoryResourceProvider(), |
| 344 sdkLibraries = dartAsync ? _LIBRARIES : [_LIB_CORE], |
| 345 uriMap = dartAsync ? FULL_URI_MAP : NO_ASYNC_URI_MAP { |
| 346 for (_MockSdkLibrary library in sdkLibraries) { |
| 207 provider.newFile(library.path, library.content); | 347 provider.newFile(library.path, library.content); |
| 208 library.parts.forEach((file) { | 348 library.parts.forEach((String path, String content) { |
| 209 provider.newFile(file.path, file.content); | 349 provider.newFile(path, content); |
| 210 }); | 350 }); |
| 211 }); | 351 } |
| 352 provider.newFile( |
| 353 '/_internal/sdk_library_metadata/lib/libraries.dart', librariesContent); |
| 212 } | 354 } |
| 213 | 355 |
| 214 @override | 356 @override |
| 215 AnalysisContextImpl get context { | 357 AnalysisContextImpl get context { |
| 216 if (_analysisContext == null) { | 358 if (_analysisContext == null) { |
| 217 _analysisContext = new _SdkAnalysisContext(this); | 359 _analysisContext = new _SdkAnalysisContext(this); |
| 218 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); | 360 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); |
| 219 _analysisContext.sourceFactory = factory; | 361 _analysisContext.sourceFactory = factory; |
| 220 ChangeSet changeSet = new ChangeSet(); | |
| 221 for (String uri in uris) { | |
| 222 Source source = factory.forUri(uri); | |
| 223 changeSet.addedSource(source); | |
| 224 } | |
| 225 _analysisContext.applyChanges(changeSet); | |
| 226 } | 362 } |
| 227 return _analysisContext; | 363 return _analysisContext; |
| 228 } | 364 } |
| 229 | 365 |
| 230 @override | 366 @override |
| 231 List<SdkLibrary> get sdkLibraries => LIBRARIES; | 367 String get sdkVersion => throw new UnimplementedError(); |
| 232 | 368 |
| 233 @override | 369 @override |
| 234 String get sdkVersion => throw unimplemented; | 370 List<String> get uris => |
| 235 | 371 sdkLibraries.map((SdkLibrary library) => library.shortName).toList(); |
| 236 UnimplementedError get unimplemented => new UnimplementedError(); | |
| 237 | |
| 238 @override | |
| 239 List<String> get uris { | |
| 240 List<String> uris = <String>[]; | |
| 241 for (SdkLibrary library in LIBRARIES) { | |
| 242 uris.add(library.shortName); | |
| 243 } | |
| 244 return uris; | |
| 245 } | |
| 246 | 372 |
| 247 @override | 373 @override |
| 248 Source fromFileUri(Uri uri) { | 374 Source fromFileUri(Uri uri) { |
| 249 String filePath = uri.path; | 375 String filePath = uri.path; |
| 250 String libPath = '/lib'; | 376 String libPath = '$sdkRoot/lib'; |
| 251 if (!filePath.startsWith("$libPath/")) { | 377 if (!filePath.startsWith("$libPath/")) { |
| 252 return null; | 378 return null; |
| 253 } | 379 } |
| 254 for (SdkLibrary library in LIBRARIES) { | 380 for (SdkLibrary library in sdkLibraries) { |
| 255 String libraryPath = library.path; | 381 String libraryPath = library.path; |
| 256 if (filePath.replaceAll('\\', '/') == libraryPath) { | 382 if (filePath.replaceAll('\\', '/') == libraryPath) { |
| 257 try { | 383 try { |
| 258 resource.File file = provider.getResource(uri.path); | 384 resource.File file = provider.getResource(uri.path); |
| 259 Uri dartUri = Uri.parse(library.shortName); | 385 Uri dartUri = Uri.parse(library.shortName); |
| 260 return file.createSource(dartUri); | 386 return file.createSource(dartUri); |
| 261 } catch (exception) { | 387 } catch (exception) { |
| 262 return null; | 388 return null; |
| 263 } | 389 } |
| 264 } | 390 } |
| 265 if (filePath.startsWith("$libraryPath/")) { | 391 if (filePath.startsWith("$libraryPath/")) { |
| 266 String pathInLibrary = filePath.substring(libraryPath.length + 1); | 392 String pathInLibrary = filePath.substring(libraryPath.length + 1); |
| 267 String path = '${library.shortName}/${pathInLibrary}'; | 393 String path = '${library.shortName}/$pathInLibrary'; |
| 268 try { | 394 try { |
| 269 resource.File file = provider.getResource(uri.path); | 395 resource.File file = provider.getResource(uri.path); |
| 270 Uri dartUri = new Uri(scheme: 'dart', path: path); | 396 Uri dartUri = new Uri(scheme: 'dart', path: path); |
| 271 return file.createSource(dartUri); | 397 return file.createSource(dartUri); |
| 272 } catch (exception) { | 398 } catch (exception) { |
| 273 return null; | 399 return null; |
| 274 } | 400 } |
| 275 } | 401 } |
| 276 } | 402 } |
| 277 return null; | 403 return null; |
| 278 } | 404 } |
| 279 | 405 |
| 280 @override | 406 @override |
| 407 PackageBundle getLinkedBundle() { |
| 408 if (_bundle == null) { |
| 409 List<Source> librarySources = sdkLibraries |
| 410 .map((SdkLibrary library) => mapDartUri(library.shortName)) |
| 411 .toList(); |
| 412 List<int> bytes = new SummaryBuilder( |
| 413 librarySources, context, context.analysisOptions.strongMode) |
| 414 .build(); |
| 415 _bundle = new PackageBundle.fromBuffer(bytes); |
| 416 } |
| 417 return _bundle; |
| 418 } |
| 419 |
| 420 @override |
| 281 SdkLibrary getSdkLibrary(String dartUri) { | 421 SdkLibrary getSdkLibrary(String dartUri) { |
| 282 // getSdkLibrary() is only used to determine whether a library is internal | 422 // getSdkLibrary() is only used to determine whether a library is internal |
| 283 // to the SDK. The mock SDK doesn't have any internals, so it's safe to | 423 // to the SDK. The mock SDK doesn't have any internals, so it's safe to |
| 284 // return null. | 424 // return null. |
| 285 return null; | 425 return null; |
| 286 } | 426 } |
| 287 | 427 |
| 288 @override | 428 @override |
| 289 Source mapDartUri(String dartUri) { | 429 Source mapDartUri(String dartUri) { |
| 290 const Map<String, String> uriToPath = const { | 430 String path = uriMap[dartUri]; |
| 291 "dart:core": "/lib/core/core.dart", | |
| 292 "dart:html": "/lib/html/dartium/html_dartium.dart", | |
| 293 "dart:async": "/lib/async/async.dart", | |
| 294 "dart:async/stream.dart": "/lib/async/stream.dart", | |
| 295 "dart:collection": "/lib/collection/collection.dart", | |
| 296 "dart:convert": "/lib/convert/convert.dart", | |
| 297 "dart:math": "/lib/math/math.dart" | |
| 298 }; | |
| 299 | |
| 300 String path = uriToPath[dartUri]; | |
| 301 if (path != null) { | 431 if (path != null) { |
| 302 resource.File file = provider.getResource(path); | 432 resource.File file = provider.getResource(path); |
| 303 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); | 433 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); |
| 304 return file.createSource(uri); | 434 return file.createSource(uri); |
| 305 } | 435 } |
| 306 | 436 |
| 307 // If we reach here then we tried to use a dartUri that's not in the | 437 // If we reach here then we tried to use a dartUri that's not in the |
| 308 // table above. | 438 // table above. |
| 309 return null; | 439 return null; |
| 310 } | 440 } |
| 441 |
| 442 /** |
| 443 * This method is used to apply patches to [MockSdk]. It may be called only |
| 444 * before analysis, i.e. before the analysis context was created. |
| 445 */ |
| 446 void updateUriFile(String uri, String updateContent(String content)) { |
| 447 assert(_analysisContext == null); |
| 448 String path = FULL_URI_MAP[uri]; |
| 449 assert(path != null); |
| 450 String content = provider.getFile(path).readAsStringSync(); |
| 451 String newContent = updateContent(content); |
| 452 provider.updateFile(path, newContent); |
| 453 } |
| 311 } | 454 } |
| 312 | 455 |
| 313 class _MockSdkLibrary implements SdkLibrary { | 456 class _MockSdkLibrary implements SdkLibrary { |
| 314 final String shortName; | 457 final String shortName; |
| 315 final String path; | 458 final String path; |
| 316 final String content; | 459 final String content; |
| 317 final List<_MockSdkFile> parts; | 460 final Map<String, String> parts; |
| 318 | 461 |
| 319 const _MockSdkLibrary(this.shortName, this.path, this.content, | 462 const _MockSdkLibrary(this.shortName, this.path, this.content, |
| 320 [this.parts = const <_MockSdkFile>[]]); | 463 [this.parts = const <String, String>{}]); |
| 321 | 464 |
| 322 @override | 465 @override |
| 323 String get category => throw unimplemented; | 466 String get category => throw new UnimplementedError(); |
| 324 | 467 |
| 325 @override | 468 @override |
| 326 bool get isDart2JsLibrary => throw unimplemented; | 469 bool get isDart2JsLibrary => throw new UnimplementedError(); |
| 327 | 470 |
| 328 @override | 471 @override |
| 329 bool get isDocumented => throw unimplemented; | 472 bool get isDocumented => throw new UnimplementedError(); |
| 330 | 473 |
| 331 @override | 474 @override |
| 332 bool get isImplementation => throw unimplemented; | 475 bool get isImplementation => throw new UnimplementedError(); |
| 333 | 476 |
| 334 @override | 477 @override |
| 335 bool get isInternal => throw unimplemented; | 478 bool get isInternal => throw new UnimplementedError(); |
| 336 | 479 |
| 337 @override | 480 @override |
| 338 bool get isShared => throw unimplemented; | 481 bool get isShared => throw new UnimplementedError(); |
| 339 | 482 |
| 340 @override | 483 @override |
| 341 bool get isVmLibrary => throw unimplemented; | 484 bool get isVmLibrary => throw new UnimplementedError(); |
| 342 | |
| 343 UnimplementedError get unimplemented => new UnimplementedError(); | |
| 344 } | |
| 345 | |
| 346 class _MockSdkFile { | |
| 347 final String path; | |
| 348 final String content; | |
| 349 | |
| 350 const _MockSdkFile(this.path, this.content); | |
| 351 } | 485 } |
| 352 | 486 |
| 353 /** | 487 /** |
| 354 * An [AnalysisContextImpl] that only contains sources for a Dart SDK. | 488 * An [AnalysisContextImpl] that only contains sources for a Dart SDK. |
| 355 */ | 489 */ |
| 356 class _SdkAnalysisContext extends AnalysisContextImpl { | 490 class _SdkAnalysisContext extends AnalysisContextImpl { |
| 357 final DartSdk sdk; | 491 final DartSdk sdk; |
| 358 | 492 |
| 359 _SdkAnalysisContext(this.sdk); | 493 _SdkAnalysisContext(this.sdk); |
| 360 | 494 |
| 361 @override | 495 @override |
| 362 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { | 496 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { |
| 363 if (factory == null) { | 497 if (factory == null) { |
| 364 return super.createCacheFromSourceFactory(factory); | 498 return super.createCacheFromSourceFactory(factory); |
| 365 } | 499 } |
| 366 return new AnalysisCache(<CachePartition>[ | 500 return new AnalysisCache( |
| 367 AnalysisEngine.instance.partitionManager_new.forSdk(sdk) | 501 <CachePartition>[AnalysisEngine.instance.partitionManager.forSdk(sdk)]); |
| 368 ]); | |
| 369 } | 502 } |
| 370 } | 503 } |
| OLD | NEW |