| 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 class MockSdk implements DartSdk { | 16 const _MockSdkLibrary _LIB_CORE = const _MockSdkLibrary( |
| 17 static const _MockSdkLibrary LIB_CORE = const _MockSdkLibrary( | 17 'dart:core', |
| 18 'dart:core', | 18 '/lib/core/core.dart', |
| 19 '/lib/core/core.dart', | 19 ''' |
| 20 ''' | |
| 21 library dart.core; | 20 library dart.core; |
| 22 | 21 |
| 23 import 'dart:async'; | 22 import 'dart:async'; |
| 24 | 23 |
| 25 class Object { | 24 class Object { |
| 26 bool operator ==(other) => identical(this, other); | 25 bool operator ==(other) => identical(this, other); |
| 27 String toString() => 'a string'; | 26 String toString() => 'a string'; |
| 28 int get hashCode => 0; | 27 int get hashCode => 0; |
| 29 } | 28 } |
| 30 | 29 |
| 31 class Function {} | 30 class Function {} |
| 32 class StackTrace {} | 31 class StackTrace {} |
| 33 class Symbol {} | 32 class Symbol {} |
| 34 class Type {} | 33 class Type {} |
| 35 | 34 |
| 36 abstract class Comparable<T> { | 35 abstract class Comparable<T> { |
| 37 int compareTo(T other); | 36 int compareTo(T other); |
| 38 } | 37 } |
| 39 | 38 |
| 40 abstract class String implements Comparable<String> { | 39 abstract class String implements Comparable<String> { |
| 41 external factory String.fromCharCodes(Iterable<int> charCodes, | 40 external factory String.fromCharCodes(Iterable<int> charCodes, |
| 42 [int start = 0, int end]); | 41 [int start = 0, int end]); |
| 42 String operator +(String other) => null; |
| 43 bool get isEmpty => false; | 43 bool get isEmpty => false; |
| 44 bool get isNotEmpty => false; | 44 bool get isNotEmpty => false; |
| 45 int get length => 0; | 45 int get length => 0; |
| 46 String substring(int len) => null; |
| 46 String toUpperCase(); | 47 String toUpperCase(); |
| 47 List<int> get codeUnits; | 48 List<int> get codeUnits; |
| 48 } | 49 } |
| 49 | 50 |
| 50 class bool extends Object {} | 51 class bool extends Object {} |
| 51 abstract class num implements Comparable<num> { | 52 abstract class num implements Comparable<num> { |
| 52 bool operator <(num other); | 53 bool operator <(num other); |
| 53 bool operator <=(num other); | 54 bool operator <=(num other); |
| 54 bool operator >(num other); | 55 bool operator >(num other); |
| 55 bool operator >=(num other); | 56 bool operator >=(num other); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 external bool identical(Object a, Object b); | 109 external bool identical(Object a, Object b); |
| 109 | 110 |
| 110 void print(Object object) {} | 111 void print(Object object) {} |
| 111 | 112 |
| 112 class _Override { | 113 class _Override { |
| 113 const _Override(); | 114 const _Override(); |
| 114 } | 115 } |
| 115 const Object override = const _Override(); | 116 const Object override = const _Override(); |
| 116 '''); | 117 '''); |
| 117 | 118 |
| 118 static const _MockSdkLibrary LIB_ASYNC = const _MockSdkLibrary( | 119 const _MockSdkLibrary _LIB_ASYNC = const _MockSdkLibrary( |
| 119 'dart:async', | 120 'dart:async', |
| 120 '/lib/async/async.dart', | 121 '/lib/async/async.dart', |
| 121 ''' | 122 ''' |
| 122 library dart.async; | 123 library dart.async; |
| 123 | 124 |
| 124 import 'dart:math'; | 125 import 'dart:math'; |
| 125 | 126 |
| 126 part 'stream.dart'; | 127 part 'stream.dart'; |
| 127 | 128 |
| 128 class Future<T> { | 129 class Future<T> { |
| 130 factory Future(computation()) => null; |
| 129 factory Future.delayed(Duration duration, [T computation()]) => null; | 131 factory Future.delayed(Duration duration, [T computation()]) => null; |
| 130 factory Future.value([value]) => null; | 132 factory Future.value([T value]) => null; |
| 131 | 133 |
| 132 static Future<List</*<T>*/> wait/*<T>*/( | 134 static Future<List/*<T>*/> wait/*<T>*/( |
| 133 Iterable<Future/*<T>*/> futures) => null; | 135 Iterable<Future/*<T>*/> futures) => null; |
| 134 Future/*<R>*/ then/*<R>*/(/*=R*/ onValue(T value)) => null; | 136 Future/*<R>*/ then/*<R>*/(/*=R*/ onValue(T value)) => null; |
| 135 } | 137 } |
| 136 ''', | 138 ''', |
| 137 const <_MockSdkFile>[ | 139 const <String, String>{ |
| 138 const _MockSdkFile( | 140 '/lib/async/stream.dart': r''' |
| 139 '/lib/async/stream.dart', | |
| 140 r''' | |
| 141 part of dart.async; | 141 part of dart.async; |
| 142 class Stream<T> {} | 142 class Stream<T> {} |
| 143 abstract class StreamTransformer<S, T> {} | 143 abstract class StreamTransformer<S, T> {} |
| 144 ''') | 144 ''' |
| 145 ]); | 145 }); |
| 146 | 146 |
| 147 static const _MockSdkLibrary LIB_COLLECTION = const _MockSdkLibrary( | 147 const _MockSdkLibrary _LIB_COLLECTION = const _MockSdkLibrary( |
| 148 'dart:collection', | 148 'dart:collection', |
| 149 '/lib/collection/collection.dart', | 149 '/lib/collection/collection.dart', |
| 150 ''' | 150 ''' |
| 151 library dart.collection; | 151 library dart.collection; |
| 152 | 152 |
| 153 abstract class HashMap<K, V> implements Map<K, V> {} | 153 abstract class HashMap<K, V> implements Map<K, V> {} |
| 154 '''); | 154 '''); |
| 155 | 155 |
| 156 static const _MockSdkLibrary LIB_CONVERT = const _MockSdkLibrary( | 156 const _MockSdkLibrary _LIB_CONVERT = const _MockSdkLibrary( |
| 157 'dart:convert', | 157 'dart:convert', |
| 158 '/lib/convert/convert.dart', | 158 '/lib/convert/convert.dart', |
| 159 ''' | 159 ''' |
| 160 library dart.convert; | 160 library dart.convert; |
| 161 | 161 |
| 162 import 'dart:async'; | 162 import 'dart:async'; |
| 163 | 163 |
| 164 abstract class Converter<S, T> implements StreamTransformer {} | 164 abstract class Converter<S, T> implements StreamTransformer {} |
| 165 class JsonDecoder extends Converter<String, Object> {} | 165 class JsonDecoder extends Converter<String, Object> {} |
| 166 '''); | 166 '''); |
| 167 | 167 |
| 168 static const _MockSdkLibrary LIB_MATH = const _MockSdkLibrary( | 168 const _MockSdkLibrary _LIB_FOREIGN_HELPER = const _MockSdkLibrary( |
| 169 'dart:math', | 169 'dart:_foreign_helper', |
| 170 '/lib/math/math.dart', | 170 '/lib/_foreign_helper/_foreign_helper.dart', |
| 171 ''' | 171 ''' |
| 172 library dart._foreign_helper; |
| 173 |
| 174 JS(String typeDescription, String codeTemplate, |
| 175 [arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11]) |
| 176 {} |
| 177 '''); |
| 178 |
| 179 const _MockSdkLibrary _LIB_MATH = const _MockSdkLibrary( |
| 180 'dart:math', |
| 181 '/lib/math/math.dart', |
| 182 ''' |
| 172 library dart.math; | 183 library dart.math; |
| 173 | 184 |
| 174 const double E = 2.718281828459045; | 185 const double E = 2.718281828459045; |
| 175 const double PI = 3.1415926535897932; | 186 const double PI = 3.1415926535897932; |
| 176 const double LN10 = 2.302585092994046; | 187 const double LN10 = 2.302585092994046; |
| 177 | 188 |
| 178 num/*=T*/ min/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; | 189 num/*=T*/ min/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; |
| 179 num/*=T*/ max/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; | 190 num/*=T*/ max/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) => null; |
| 180 | 191 |
| 181 external double cos(num x); | 192 external double cos(num x); |
| 182 external double sin(num x); | 193 external double sin(num x); |
| 183 external double sqrt(num x); | 194 external double sqrt(num x); |
| 184 class Random { | 195 class Random { |
| 185 bool nextBool() => true; | 196 bool nextBool() => true; |
| 186 double nextDouble() => 2.0; | 197 double nextDouble() => 2.0; |
| 187 int nextInt() => 1; | 198 int nextInt() => 1; |
| 188 } | 199 } |
| 189 '''); | 200 '''); |
| 190 | 201 |
| 191 static const _MockSdkLibrary LIB_HTML = const _MockSdkLibrary( | 202 const _MockSdkLibrary _LIB_HTML = const _MockSdkLibrary( |
| 192 'dart:html', | 203 'dart:html', |
| 193 '/lib/html/dartium/html_dartium.dart', | 204 '/lib/html/dartium/html_dartium.dart', |
| 194 ''' | 205 ''' |
| 195 library dart.html; | 206 library dart.html; |
| 196 class HtmlElement {} | 207 class HtmlElement {} |
| 197 '''); | 208 '''); |
| 198 | 209 |
| 199 static const List<SdkLibrary> LIBRARIES = const [ | 210 const List<SdkLibrary> _LIBRARIES = const [ |
| 200 LIB_CORE, | 211 _LIB_CORE, |
| 201 LIB_ASYNC, | 212 _LIB_ASYNC, |
| 202 LIB_COLLECTION, | 213 _LIB_COLLECTION, |
| 203 LIB_CONVERT, | 214 _LIB_CONVERT, |
| 204 LIB_MATH, | 215 _LIB_FOREIGN_HELPER, |
| 205 LIB_HTML, | 216 _LIB_MATH, |
| 206 ]; | 217 _LIB_HTML, |
| 218 ]; |
| 207 | 219 |
| 220 class MockSdk implements DartSdk { |
| 208 final resource.MemoryResourceProvider provider = | 221 final resource.MemoryResourceProvider provider = |
| 209 new resource.MemoryResourceProvider(); | 222 new resource.MemoryResourceProvider(); |
| 210 | 223 |
| 211 /** | 224 /** |
| 212 * The [AnalysisContextImpl] which is used for all of the sources. | 225 * The [AnalysisContextImpl] which is used for all of the sources. |
| 213 */ | 226 */ |
| 214 AnalysisContextImpl _analysisContext; | 227 AnalysisContextImpl _analysisContext; |
| 215 | 228 |
| 216 MockSdk() { | 229 MockSdk({bool dartAsync: true}) |
| 217 LIBRARIES.forEach((_MockSdkLibrary library) { | 230 : sdkLibraries = dartAsync ? _LIBRARIES : [_LIB_CORE] { |
| 231 for (_MockSdkLibrary library in sdkLibraries) { |
| 218 provider.newFile(library.path, library.content); | 232 provider.newFile(library.path, library.content); |
| 219 library.parts.forEach((file) { | 233 library.parts.forEach((String path, String content) { |
| 220 provider.newFile(file.path, file.content); | 234 provider.newFile(path, content); |
| 221 }); | 235 }); |
| 222 }); | 236 } |
| 223 } | 237 } |
| 224 | 238 |
| 225 @override | 239 @override |
| 226 AnalysisContextImpl get context { | 240 AnalysisContextImpl get context { |
| 227 if (_analysisContext == null) { | 241 if (_analysisContext == null) { |
| 228 _analysisContext = new _SdkAnalysisContext(this); | 242 _analysisContext = new _SdkAnalysisContext(this); |
| 229 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); | 243 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); |
| 230 _analysisContext.sourceFactory = factory; | 244 _analysisContext.sourceFactory = factory; |
| 231 } | 245 } |
| 232 return _analysisContext; | 246 return _analysisContext; |
| 233 } | 247 } |
| 234 | 248 |
| 235 @override | 249 @override |
| 236 List<SdkLibrary> get sdkLibraries => LIBRARIES; | 250 final List<SdkLibrary> sdkLibraries; |
| 237 | 251 |
| 238 @override | 252 @override |
| 239 String get sdkVersion => throw unimplemented; | 253 String get sdkVersion => throw new UnimplementedError(); |
| 240 | |
| 241 UnimplementedError get unimplemented => new UnimplementedError(); | |
| 242 | 254 |
| 243 @override | 255 @override |
| 244 List<String> get uris { | 256 List<String> get uris => |
| 245 List<String> uris = <String>[]; | 257 sdkLibraries.map((SdkLibrary library) => library.shortName).toList(); |
| 246 for (SdkLibrary library in LIBRARIES) { | |
| 247 uris.add(library.shortName); | |
| 248 } | |
| 249 return uris; | |
| 250 } | |
| 251 | 258 |
| 252 @override | 259 @override |
| 253 Source fromFileUri(Uri uri) { | 260 Source fromFileUri(Uri uri) { |
| 254 String filePath = uri.path; | 261 String filePath = uri.path; |
| 255 String libPath = '/lib'; | 262 String libPath = '/lib'; |
| 256 if (!filePath.startsWith("$libPath/")) { | 263 if (!filePath.startsWith("$libPath/")) { |
| 257 return null; | 264 return null; |
| 258 } | 265 } |
| 259 for (SdkLibrary library in LIBRARIES) { | 266 for (SdkLibrary library in sdkLibraries) { |
| 260 String libraryPath = library.path; | 267 String libraryPath = library.path; |
| 261 if (filePath.replaceAll('\\', '/') == libraryPath) { | 268 if (filePath.replaceAll('\\', '/') == libraryPath) { |
| 262 try { | 269 try { |
| 263 resource.File file = provider.getResource(uri.path); | 270 resource.File file = provider.getResource(uri.path); |
| 264 Uri dartUri = Uri.parse(library.shortName); | 271 Uri dartUri = Uri.parse(library.shortName); |
| 265 return file.createSource(dartUri); | 272 return file.createSource(dartUri); |
| 266 } catch (exception) { | 273 } catch (exception) { |
| 267 return null; | 274 return null; |
| 268 } | 275 } |
| 269 } | 276 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 292 | 299 |
| 293 @override | 300 @override |
| 294 Source mapDartUri(String dartUri) { | 301 Source mapDartUri(String dartUri) { |
| 295 const Map<String, String> uriToPath = const { | 302 const Map<String, String> uriToPath = const { |
| 296 "dart:core": "/lib/core/core.dart", | 303 "dart:core": "/lib/core/core.dart", |
| 297 "dart:html": "/lib/html/dartium/html_dartium.dart", | 304 "dart:html": "/lib/html/dartium/html_dartium.dart", |
| 298 "dart:async": "/lib/async/async.dart", | 305 "dart:async": "/lib/async/async.dart", |
| 299 "dart:async/stream.dart": "/lib/async/stream.dart", | 306 "dart:async/stream.dart": "/lib/async/stream.dart", |
| 300 "dart:collection": "/lib/collection/collection.dart", | 307 "dart:collection": "/lib/collection/collection.dart", |
| 301 "dart:convert": "/lib/convert/convert.dart", | 308 "dart:convert": "/lib/convert/convert.dart", |
| 309 "dart:_foreign_helper": "/lib/_foreign_helper/_foreign_helper.dart", |
| 302 "dart:math": "/lib/math/math.dart" | 310 "dart:math": "/lib/math/math.dart" |
| 303 }; | 311 }; |
| 304 | 312 |
| 305 String path = uriToPath[dartUri]; | 313 String path = uriToPath[dartUri]; |
| 306 if (path != null) { | 314 if (path != null) { |
| 307 resource.File file = provider.getResource(path); | 315 resource.File file = provider.getResource(path); |
| 308 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); | 316 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); |
| 309 return file.createSource(uri); | 317 return file.createSource(uri); |
| 310 } | 318 } |
| 311 | 319 |
| 312 // If we reach here then we tried to use a dartUri that's not in the | 320 // If we reach here then we tried to use a dartUri that's not in the |
| 313 // table above. | 321 // table above. |
| 314 return null; | 322 return null; |
| 315 } | 323 } |
| 316 } | 324 } |
| 317 | 325 |
| 318 class _MockSdkFile { | |
| 319 final String path; | |
| 320 final String content; | |
| 321 | |
| 322 const _MockSdkFile(this.path, this.content); | |
| 323 } | |
| 324 | |
| 325 class _MockSdkLibrary implements SdkLibrary { | 326 class _MockSdkLibrary implements SdkLibrary { |
| 326 final String shortName; | 327 final String shortName; |
| 327 final String path; | 328 final String path; |
| 328 final String content; | 329 final String content; |
| 329 final List<_MockSdkFile> parts; | 330 final Map<String, String> parts; |
| 330 | 331 |
| 331 const _MockSdkLibrary(this.shortName, this.path, this.content, | 332 const _MockSdkLibrary(this.shortName, this.path, this.content, |
| 332 [this.parts = const <_MockSdkFile>[]]); | 333 [this.parts = const <String, String>{}]); |
| 333 | 334 |
| 334 @override | 335 @override |
| 335 String get category => throw unimplemented; | 336 String get category => throw new UnimplementedError(); |
| 336 | 337 |
| 337 @override | 338 @override |
| 338 bool get isDart2JsLibrary => throw unimplemented; | 339 bool get isDart2JsLibrary => throw new UnimplementedError(); |
| 339 | 340 |
| 340 @override | 341 @override |
| 341 bool get isDocumented => throw unimplemented; | 342 bool get isDocumented => throw new UnimplementedError(); |
| 342 | 343 |
| 343 @override | 344 @override |
| 344 bool get isImplementation => throw unimplemented; | 345 bool get isImplementation => throw new UnimplementedError(); |
| 345 | 346 |
| 346 @override | 347 @override |
| 347 bool get isInternal => throw unimplemented; | 348 bool get isInternal => throw new UnimplementedError(); |
| 348 | 349 |
| 349 @override | 350 @override |
| 350 bool get isShared => throw unimplemented; | 351 bool get isShared => throw new UnimplementedError(); |
| 351 | 352 |
| 352 @override | 353 @override |
| 353 bool get isVmLibrary => throw unimplemented; | 354 bool get isVmLibrary => throw new UnimplementedError(); |
| 354 | |
| 355 UnimplementedError get unimplemented => new UnimplementedError(); | |
| 356 } | 355 } |
| 357 | 356 |
| 358 /** | 357 /** |
| 359 * An [AnalysisContextImpl] that only contains sources for a Dart SDK. | 358 * An [AnalysisContextImpl] that only contains sources for a Dart SDK. |
| 360 */ | 359 */ |
| 361 class _SdkAnalysisContext extends AnalysisContextImpl { | 360 class _SdkAnalysisContext extends AnalysisContextImpl { |
| 362 final DartSdk sdk; | 361 final DartSdk sdk; |
| 363 | 362 |
| 364 _SdkAnalysisContext(this.sdk); | 363 _SdkAnalysisContext(this.sdk); |
| 365 | 364 |
| 366 @override | 365 @override |
| 367 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { | 366 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { |
| 368 if (factory == null) { | 367 if (factory == null) { |
| 369 return super.createCacheFromSourceFactory(factory); | 368 return super.createCacheFromSourceFactory(factory); |
| 370 } | 369 } |
| 371 return new AnalysisCache( | 370 return new AnalysisCache( |
| 372 <CachePartition>[AnalysisEngine.instance.partitionManager.forSdk(sdk)]); | 371 <CachePartition>[AnalysisEngine.instance.partitionManager.forSdk(sdk)]); |
| 373 } | 372 } |
| 374 } | 373 } |
| OLD | NEW |