| 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'; |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 AnalysisContextImpl _analysisContext; | 332 AnalysisContextImpl _analysisContext; |
| 333 | 333 |
| 334 @override | 334 @override |
| 335 final List<SdkLibrary> sdkLibraries; | 335 final List<SdkLibrary> sdkLibraries; |
| 336 | 336 |
| 337 /** | 337 /** |
| 338 * The cached linked bundle of the SDK. | 338 * The cached linked bundle of the SDK. |
| 339 */ | 339 */ |
| 340 PackageBundle _bundle; | 340 PackageBundle _bundle; |
| 341 | 341 |
| 342 MockSdk({bool dartAsync: true, resource.ResourceProvider resourceProvider}) | 342 MockSdk( |
| 343 {bool dartAsync: true, resource.MemoryResourceProvider resourceProvider}) |
| 343 : provider = resourceProvider ?? new resource.MemoryResourceProvider(), | 344 : provider = resourceProvider ?? new resource.MemoryResourceProvider(), |
| 344 sdkLibraries = dartAsync ? _LIBRARIES : [_LIB_CORE], | 345 sdkLibraries = dartAsync ? _LIBRARIES : [_LIB_CORE], |
| 345 uriMap = dartAsync ? FULL_URI_MAP : NO_ASYNC_URI_MAP { | 346 uriMap = dartAsync ? FULL_URI_MAP : NO_ASYNC_URI_MAP { |
| 346 for (_MockSdkLibrary library in sdkLibraries) { | 347 for (_MockSdkLibrary library in sdkLibraries) { |
| 347 provider.newFile(library.path, library.content); | 348 provider.newFile(provider.convertPath(library.path), library.content); |
| 348 library.parts.forEach((String path, String content) { | 349 library.parts.forEach((String path, String content) { |
| 349 provider.newFile(path, content); | 350 provider.newFile(provider.convertPath(path), content); |
| 350 }); | 351 }); |
| 351 } | 352 } |
| 352 provider.newFile( | 353 provider.newFile( |
| 353 '/_internal/sdk_library_metadata/lib/libraries.dart', librariesContent); | 354 provider |
| 355 .convertPath('/_internal/sdk_library_metadata/lib/libraries.dart'), |
| 356 librariesContent); |
| 354 } | 357 } |
| 355 | 358 |
| 356 @override | 359 @override |
| 357 AnalysisContextImpl get context { | 360 AnalysisContextImpl get context { |
| 358 if (_analysisContext == null) { | 361 if (_analysisContext == null) { |
| 359 _analysisContext = new _SdkAnalysisContext(this); | 362 _analysisContext = new _SdkAnalysisContext(this); |
| 360 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); | 363 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); |
| 361 _analysisContext.sourceFactory = factory; | 364 _analysisContext.sourceFactory = factory; |
| 362 } | 365 } |
| 363 return _analysisContext; | 366 return _analysisContext; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 374 Source fromFileUri(Uri uri) { | 377 Source fromFileUri(Uri uri) { |
| 375 String filePath = uri.path; | 378 String filePath = uri.path; |
| 376 String libPath = '$sdkRoot/lib'; | 379 String libPath = '$sdkRoot/lib'; |
| 377 if (!filePath.startsWith("$libPath/")) { | 380 if (!filePath.startsWith("$libPath/")) { |
| 378 return null; | 381 return null; |
| 379 } | 382 } |
| 380 for (SdkLibrary library in sdkLibraries) { | 383 for (SdkLibrary library in sdkLibraries) { |
| 381 String libraryPath = library.path; | 384 String libraryPath = library.path; |
| 382 if (filePath.replaceAll('\\', '/') == libraryPath) { | 385 if (filePath.replaceAll('\\', '/') == libraryPath) { |
| 383 try { | 386 try { |
| 384 resource.File file = provider.getResource(uri.path); | 387 resource.File file = |
| 388 provider.getResource(provider.convertPath(filePath)); |
| 385 Uri dartUri = Uri.parse(library.shortName); | 389 Uri dartUri = Uri.parse(library.shortName); |
| 386 return file.createSource(dartUri); | 390 return file.createSource(dartUri); |
| 387 } catch (exception) { | 391 } catch (exception) { |
| 388 return null; | 392 return null; |
| 389 } | 393 } |
| 390 } | 394 } |
| 391 if (filePath.startsWith("$libraryPath/")) { | 395 if (filePath.startsWith("$libraryPath/")) { |
| 392 String pathInLibrary = filePath.substring(libraryPath.length + 1); | 396 String pathInLibrary = filePath.substring(libraryPath.length + 1); |
| 393 String path = '${library.shortName}/$pathInLibrary'; | 397 String path = '${library.shortName}/$pathInLibrary'; |
| 394 try { | 398 try { |
| 395 resource.File file = provider.getResource(uri.path); | 399 resource.File file = |
| 400 provider.getResource(provider.convertPath(filePath)); |
| 396 Uri dartUri = new Uri(scheme: 'dart', path: path); | 401 Uri dartUri = new Uri(scheme: 'dart', path: path); |
| 397 return file.createSource(dartUri); | 402 return file.createSource(dartUri); |
| 398 } catch (exception) { | 403 } catch (exception) { |
| 399 return null; | 404 return null; |
| 400 } | 405 } |
| 401 } | 406 } |
| 402 } | 407 } |
| 403 return null; | 408 return null; |
| 404 } | 409 } |
| 405 | 410 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 422 // getSdkLibrary() is only used to determine whether a library is internal | 427 // getSdkLibrary() is only used to determine whether a library is internal |
| 423 // to the SDK. The mock SDK doesn't have any internals, so it's safe to | 428 // to the SDK. The mock SDK doesn't have any internals, so it's safe to |
| 424 // return null. | 429 // return null. |
| 425 return null; | 430 return null; |
| 426 } | 431 } |
| 427 | 432 |
| 428 @override | 433 @override |
| 429 Source mapDartUri(String dartUri) { | 434 Source mapDartUri(String dartUri) { |
| 430 String path = uriMap[dartUri]; | 435 String path = uriMap[dartUri]; |
| 431 if (path != null) { | 436 if (path != null) { |
| 432 resource.File file = provider.getResource(path); | 437 resource.File file = provider.getResource(provider.convertPath(path)); |
| 433 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); | 438 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); |
| 434 return file.createSource(uri); | 439 return file.createSource(uri); |
| 435 } | 440 } |
| 436 | |
| 437 // If we reach here then we tried to use a dartUri that's not in the | 441 // If we reach here then we tried to use a dartUri that's not in the |
| 438 // table above. | 442 // table above. |
| 439 return null; | 443 return null; |
| 440 } | 444 } |
| 441 | 445 |
| 442 /** | 446 /** |
| 443 * This method is used to apply patches to [MockSdk]. It may be called only | 447 * 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. | 448 * before analysis, i.e. before the analysis context was created. |
| 445 */ | 449 */ |
| 446 void updateUriFile(String uri, String updateContent(String content)) { | 450 void updateUriFile(String uri, String updateContent(String content)) { |
| 447 assert(_analysisContext == null); | 451 assert(_analysisContext == null); |
| 448 String path = FULL_URI_MAP[uri]; | 452 String path = FULL_URI_MAP[uri]; |
| 449 assert(path != null); | 453 assert(path != null); |
| 454 path = provider.convertPath(path); |
| 450 String content = provider.getFile(path).readAsStringSync(); | 455 String content = provider.getFile(path).readAsStringSync(); |
| 451 String newContent = updateContent(content); | 456 String newContent = updateContent(content); |
| 452 provider.updateFile(path, newContent); | 457 provider.updateFile(path, newContent); |
| 453 } | 458 } |
| 454 } | 459 } |
| 455 | 460 |
| 456 class _MockSdkLibrary implements SdkLibrary { | 461 class _MockSdkLibrary implements SdkLibrary { |
| 457 final String shortName; | 462 final String shortName; |
| 458 final String path; | 463 final String path; |
| 459 final String content; | 464 final String content; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 | 499 |
| 495 @override | 500 @override |
| 496 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { | 501 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { |
| 497 if (factory == null) { | 502 if (factory == null) { |
| 498 return super.createCacheFromSourceFactory(factory); | 503 return super.createCacheFromSourceFactory(factory); |
| 499 } | 504 } |
| 500 return new AnalysisCache( | 505 return new AnalysisCache( |
| 501 <CachePartition>[AnalysisEngine.instance.partitionManager.forSdk(sdk)]); | 506 <CachePartition>[AnalysisEngine.instance.partitionManager.forSdk(sdk)]); |
| 502 } | 507 } |
| 503 } | 508 } |
| OLD | NEW |