| 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 analyzer.test.src.context.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' |
| 12 show AnalysisEngine, ChangeSet; | 12 show AnalysisEngine, ChangeSet; |
| 13 import 'package:analyzer/src/generated/sdk.dart'; | 13 import 'package:analyzer/src/generated/sdk.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 15 | 15 |
| 16 const _MockSdkLibrary _LIB_ASYNC = const _MockSdkLibrary( |
| 17 'dart:async', |
| 18 '/lib/async/async.dart', |
| 19 ''' |
| 20 library dart.async; |
| 21 |
| 22 import 'dart:math'; |
| 23 |
| 24 part 'stream.dart'; |
| 25 |
| 26 class Future<T> { |
| 27 factory Future(computation()) => null; |
| 28 factory Future.delayed(Duration duration, [T computation()]) => null; |
| 29 factory Future.value([T value]) => null; |
| 30 |
| 31 static Future<List/*<T>*/> wait/*<T>*/( |
| 32 Iterable<Future/*<T>*/> futures) => null; |
| 33 Future/*<R>*/ then/*<R>*/(/*=R*/ onValue(T value)) => null; |
| 34 } |
| 35 ''', |
| 36 const <String, String>{ |
| 37 '/lib/async/stream.dart': r''' |
| 38 part of dart.async; |
| 39 class Stream<T> {} |
| 40 abstract class StreamTransformer<S, T> {} |
| 41 ''' |
| 42 }); |
| 43 |
| 44 const _MockSdkLibrary _LIB_COLLECTION = const _MockSdkLibrary( |
| 45 'dart:collection', |
| 46 '/lib/collection/collection.dart', |
| 47 ''' |
| 48 library dart.collection; |
| 49 |
| 50 abstract class HashMap<K, V> implements Map<K, V> {} |
| 51 '''); |
| 52 |
| 53 const _MockSdkLibrary _LIB_CONVERT = const _MockSdkLibrary( |
| 54 'dart:convert', |
| 55 '/lib/convert/convert.dart', |
| 56 ''' |
| 57 library dart.convert; |
| 58 |
| 59 import 'dart:async'; |
| 60 |
| 61 abstract class Converter<S, T> implements StreamTransformer {} |
| 62 class JsonDecoder extends Converter<String, Object> {} |
| 63 '''); |
| 64 |
| 16 const _MockSdkLibrary _LIB_CORE = const _MockSdkLibrary( | 65 const _MockSdkLibrary _LIB_CORE = const _MockSdkLibrary( |
| 17 'dart:core', | 66 'dart:core', |
| 18 '/lib/core/core.dart', | 67 '/lib/core/core.dart', |
| 19 ''' | 68 ''' |
| 20 library dart.core; | 69 library dart.core; |
| 21 | 70 |
| 22 import 'dart:async'; | 71 import 'dart:async'; |
| 23 | 72 |
| 24 class Object { | 73 class Object { |
| 25 bool operator ==(other) => identical(this, other); | 74 bool operator ==(other) => identical(this, other); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 external bool identical(Object a, Object b); | 159 external bool identical(Object a, Object b); |
| 111 | 160 |
| 112 void print(Object object) {} | 161 void print(Object object) {} |
| 113 | 162 |
| 114 class _Override { | 163 class _Override { |
| 115 const _Override(); | 164 const _Override(); |
| 116 } | 165 } |
| 117 const Object override = const _Override(); | 166 const Object override = const _Override(); |
| 118 '''); | 167 '''); |
| 119 | 168 |
| 120 const _MockSdkLibrary _LIB_ASYNC = const _MockSdkLibrary( | |
| 121 'dart:async', | |
| 122 '/lib/async/async.dart', | |
| 123 ''' | |
| 124 library dart.async; | |
| 125 | |
| 126 import 'dart:math'; | |
| 127 | |
| 128 part 'stream.dart'; | |
| 129 | |
| 130 class Future<T> { | |
| 131 factory Future(computation()) => null; | |
| 132 factory Future.delayed(Duration duration, [T computation()]) => null; | |
| 133 factory Future.value([T value]) => null; | |
| 134 | |
| 135 static Future<List/*<T>*/> wait/*<T>*/( | |
| 136 Iterable<Future/*<T>*/> futures) => null; | |
| 137 Future/*<R>*/ then/*<R>*/(/*=R*/ onValue(T value)) => null; | |
| 138 } | |
| 139 ''', | |
| 140 const <String, String>{ | |
| 141 '/lib/async/stream.dart': r''' | |
| 142 part of dart.async; | |
| 143 class Stream<T> {} | |
| 144 abstract class StreamTransformer<S, T> {} | |
| 145 ''' | |
| 146 }); | |
| 147 | |
| 148 const _MockSdkLibrary _LIB_COLLECTION = const _MockSdkLibrary( | |
| 149 'dart:collection', | |
| 150 '/lib/collection/collection.dart', | |
| 151 ''' | |
| 152 library dart.collection; | |
| 153 | |
| 154 abstract class HashMap<K, V> implements Map<K, V> {} | |
| 155 '''); | |
| 156 | |
| 157 const _MockSdkLibrary _LIB_CONVERT = const _MockSdkLibrary( | |
| 158 'dart:convert', | |
| 159 '/lib/convert/convert.dart', | |
| 160 ''' | |
| 161 library dart.convert; | |
| 162 | |
| 163 import 'dart:async'; | |
| 164 | |
| 165 abstract class Converter<S, T> implements StreamTransformer {} | |
| 166 class JsonDecoder extends Converter<String, Object> {} | |
| 167 '''); | |
| 168 | |
| 169 const _MockSdkLibrary _LIB_FOREIGN_HELPER = const _MockSdkLibrary( | 169 const _MockSdkLibrary _LIB_FOREIGN_HELPER = const _MockSdkLibrary( |
| 170 'dart:_foreign_helper', | 170 'dart:_foreign_helper', |
| 171 '/lib/_foreign_helper/_foreign_helper.dart', | 171 '/lib/_foreign_helper/_foreign_helper.dart', |
| 172 ''' | 172 ''' |
| 173 library dart._foreign_helper; | 173 library dart._foreign_helper; |
| 174 | 174 |
| 175 JS(String typeDescription, String codeTemplate, | 175 JS(String typeDescription, String codeTemplate, |
| 176 [arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11]) | 176 [arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11]) |
| 177 {} | 177 {} |
| 178 '''); | 178 '''); |
| 179 | 179 |
| 180 const _MockSdkLibrary _LIB_HTML = const _MockSdkLibrary( |
| 181 'dart:html', |
| 182 '/lib/html/dartium/html_dartium.dart', |
| 183 ''' |
| 184 library dart.html; |
| 185 class HtmlElement {} |
| 186 '''); |
| 187 |
| 180 const _MockSdkLibrary _LIB_MATH = const _MockSdkLibrary( | 188 const _MockSdkLibrary _LIB_MATH = const _MockSdkLibrary( |
| 181 'dart:math', | 189 'dart:math', |
| 182 '/lib/math/math.dart', | 190 '/lib/math/math.dart', |
| 183 ''' | 191 ''' |
| 184 library dart.math; | 192 library dart.math; |
| 185 | 193 |
| 186 const double E = 2.718281828459045; | 194 const double E = 2.718281828459045; |
| 187 const double PI = 3.1415926535897932; | 195 const double PI = 3.1415926535897932; |
| 188 const double LN10 = 2.302585092994046; | 196 const double LN10 = 2.302585092994046; |
| 189 | 197 |
| 190 num/*=T*/ min/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; | 198 num/*=T*/ min/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; |
| 191 num/*=T*/ max/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; | 199 num/*=T*/ max/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; |
| 192 | 200 |
| 193 external double cos(num x); | 201 external double cos(num x); |
| 194 external double sin(num x); | 202 external double sin(num x); |
| 195 external double sqrt(num x); | 203 external double sqrt(num x); |
| 196 class Random { | 204 class Random { |
| 197 bool nextBool() => true; | 205 bool nextBool() => true; |
| 198 double nextDouble() => 2.0; | 206 double nextDouble() => 2.0; |
| 199 int nextInt() => 1; | 207 int nextInt() => 1; |
| 200 } | 208 } |
| 201 '''); | 209 '''); |
| 202 | 210 |
| 203 const _MockSdkLibrary _LIB_HTML = const _MockSdkLibrary( | |
| 204 'dart:html', | |
| 205 '/lib/html/dartium/html_dartium.dart', | |
| 206 ''' | |
| 207 library dart.html; | |
| 208 class HtmlElement {} | |
| 209 '''); | |
| 210 | |
| 211 const List<SdkLibrary> _LIBRARIES = const [ | 211 const List<SdkLibrary> _LIBRARIES = const [ |
| 212 _LIB_CORE, | 212 _LIB_CORE, |
| 213 _LIB_ASYNC, | 213 _LIB_ASYNC, |
| 214 _LIB_COLLECTION, | 214 _LIB_COLLECTION, |
| 215 _LIB_CONVERT, | 215 _LIB_CONVERT, |
| 216 _LIB_FOREIGN_HELPER, | 216 _LIB_FOREIGN_HELPER, |
| 217 _LIB_MATH, | 217 _LIB_MATH, |
| 218 _LIB_HTML, | 218 _LIB_HTML, |
| 219 ]; | 219 ]; |
| 220 | 220 |
| 221 class MockSdk implements DartSdk { | 221 class MockSdk implements DartSdk { |
| 222 static const Map<String, String> FULL_URI_MAP = const { |
| 223 "dart:core": "/lib/core/core.dart", |
| 224 "dart:html": "/lib/html/dartium/html_dartium.dart", |
| 225 "dart:async": "/lib/async/async.dart", |
| 226 "dart:async/stream.dart": "/lib/async/stream.dart", |
| 227 "dart:collection": "/lib/collection/collection.dart", |
| 228 "dart:convert": "/lib/convert/convert.dart", |
| 229 "dart:_foreign_helper": "/lib/_foreign_helper/_foreign_helper.dart", |
| 230 "dart:math": "/lib/math/math.dart" |
| 231 }; |
| 232 |
| 233 static const Map<String, String> NO_ASYNC_URI_MAP = const { |
| 234 "dart:core": "/lib/core/core.dart", |
| 235 }; |
| 236 |
| 222 final resource.MemoryResourceProvider provider = | 237 final resource.MemoryResourceProvider provider = |
| 223 new resource.MemoryResourceProvider(); | 238 new resource.MemoryResourceProvider(); |
| 224 | 239 |
| 240 final Map<String, String> uriMap; |
| 241 |
| 225 /** | 242 /** |
| 226 * The [AnalysisContextImpl] which is used for all of the sources. | 243 * The [AnalysisContextImpl] which is used for all of the sources. |
| 227 */ | 244 */ |
| 228 AnalysisContextImpl _analysisContext; | 245 AnalysisContextImpl _analysisContext; |
| 229 | 246 |
| 247 @override |
| 248 final List<SdkLibrary> sdkLibraries; |
| 249 |
| 230 MockSdk({bool dartAsync: true}) | 250 MockSdk({bool dartAsync: true}) |
| 231 : sdkLibraries = dartAsync ? _LIBRARIES : [_LIB_CORE] { | 251 : sdkLibraries = dartAsync ? _LIBRARIES : [_LIB_CORE], |
| 252 uriMap = dartAsync ? FULL_URI_MAP : NO_ASYNC_URI_MAP { |
| 232 for (_MockSdkLibrary library in sdkLibraries) { | 253 for (_MockSdkLibrary library in sdkLibraries) { |
| 233 provider.newFile(library.path, library.content); | 254 provider.newFile(library.path, library.content); |
| 234 library.parts.forEach((String path, String content) { | 255 library.parts.forEach((String path, String content) { |
| 235 provider.newFile(path, content); | 256 provider.newFile(path, content); |
| 236 }); | 257 }); |
| 237 } | 258 } |
| 238 } | 259 } |
| 239 | 260 |
| 240 @override | 261 @override |
| 241 AnalysisContextImpl get context { | 262 AnalysisContextImpl get context { |
| 242 if (_analysisContext == null) { | 263 if (_analysisContext == null) { |
| 243 _analysisContext = new _SdkAnalysisContext(this); | 264 _analysisContext = new _SdkAnalysisContext(this); |
| 244 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); | 265 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); |
| 245 _analysisContext.sourceFactory = factory; | 266 _analysisContext.sourceFactory = factory; |
| 246 } | 267 } |
| 247 return _analysisContext; | 268 return _analysisContext; |
| 248 } | 269 } |
| 249 | 270 |
| 250 @override | 271 @override |
| 251 final List<SdkLibrary> sdkLibraries; | |
| 252 | |
| 253 @override | |
| 254 String get sdkVersion => throw new UnimplementedError(); | 272 String get sdkVersion => throw new UnimplementedError(); |
| 255 | 273 |
| 256 @override | 274 @override |
| 257 List<String> get uris => | 275 List<String> get uris => |
| 258 sdkLibraries.map((SdkLibrary library) => library.shortName).toList(); | 276 sdkLibraries.map((SdkLibrary library) => library.shortName).toList(); |
| 259 | 277 |
| 260 @override | 278 @override |
| 261 Source fromFileUri(Uri uri) { | 279 Source fromFileUri(Uri uri) { |
| 262 String filePath = uri.path; | 280 String filePath = uri.path; |
| 263 String libPath = '/lib'; | 281 String libPath = '/lib'; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 293 @override | 311 @override |
| 294 SdkLibrary getSdkLibrary(String dartUri) { | 312 SdkLibrary getSdkLibrary(String dartUri) { |
| 295 // getSdkLibrary() is only used to determine whether a library is internal | 313 // getSdkLibrary() is only used to determine whether a library is internal |
| 296 // to the SDK. The mock SDK doesn't have any internals, so it's safe to | 314 // to the SDK. The mock SDK doesn't have any internals, so it's safe to |
| 297 // return null. | 315 // return null. |
| 298 return null; | 316 return null; |
| 299 } | 317 } |
| 300 | 318 |
| 301 @override | 319 @override |
| 302 Source mapDartUri(String dartUri) { | 320 Source mapDartUri(String dartUri) { |
| 303 const Map<String, String> uriToPath = const { | 321 String path = uriMap[dartUri]; |
| 304 "dart:core": "/lib/core/core.dart", | |
| 305 "dart:html": "/lib/html/dartium/html_dartium.dart", | |
| 306 "dart:async": "/lib/async/async.dart", | |
| 307 "dart:async/stream.dart": "/lib/async/stream.dart", | |
| 308 "dart:collection": "/lib/collection/collection.dart", | |
| 309 "dart:convert": "/lib/convert/convert.dart", | |
| 310 "dart:_foreign_helper": "/lib/_foreign_helper/_foreign_helper.dart", | |
| 311 "dart:math": "/lib/math/math.dart" | |
| 312 }; | |
| 313 | |
| 314 String path = uriToPath[dartUri]; | |
| 315 if (path != null) { | 322 if (path != null) { |
| 316 resource.File file = provider.getResource(path); | 323 resource.File file = provider.getResource(path); |
| 317 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); | 324 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); |
| 318 return file.createSource(uri); | 325 return file.createSource(uri); |
| 319 } | 326 } |
| 320 | 327 |
| 321 // If we reach here then we tried to use a dartUri that's not in the | 328 // If we reach here then we tried to use a dartUri that's not in the |
| 322 // table above. | 329 // table above. |
| 323 return null; | 330 return null; |
| 324 } | 331 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 | 372 |
| 366 @override | 373 @override |
| 367 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { | 374 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { |
| 368 if (factory == null) { | 375 if (factory == null) { |
| 369 return super.createCacheFromSourceFactory(factory); | 376 return super.createCacheFromSourceFactory(factory); |
| 370 } | 377 } |
| 371 return new AnalysisCache( | 378 return new AnalysisCache( |
| 372 <CachePartition>[AnalysisEngine.instance.partitionManager.forSdk(sdk)]); | 379 <CachePartition>[AnalysisEngine.instance.partitionManager.forSdk(sdk)]); |
| 373 } | 380 } |
| 374 } | 381 } |
| OLD | NEW |