| 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 testing.mock_sdk; | 5 library testing.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/generated/engine.dart'; | 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/sdk.dart'; | 10 import 'package:analyzer/src/generated/sdk.dart'; |
| 11 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/source.dart'; |
| 12 | 12 |
| 13 | 13 |
| 14 class MockSdk implements DartSdk { | 14 class MockSdk implements DartSdk { |
| 15 final resource.MemoryResourceProvider provider = | |
| 16 new resource.MemoryResourceProvider(); | |
| 17 | |
| 18 static const _MockSdkLibrary LIB_CORE = | 15 static const _MockSdkLibrary LIB_CORE = |
| 19 const _MockSdkLibrary('dart:core', '/lib/core/core.dart', ''' | 16 const _MockSdkLibrary('dart:core', '/lib/core/core.dart', ''' |
| 20 library dart.core; | 17 library dart.core; |
| 21 | 18 |
| 22 class Object { | 19 class Object { |
| 23 bool operator ==(other) => identical(this, other); | 20 bool operator ==(other) => identical(this, other); |
| 24 } | 21 } |
| 25 | 22 |
| 26 class Function {} | 23 class Function {} |
| 27 class StackTrace {} | 24 class StackTrace {} |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 library dart.html; | 90 library dart.html; |
| 94 class HtmlElement {} | 91 class HtmlElement {} |
| 95 '''); | 92 '''); |
| 96 | 93 |
| 97 static const List<SdkLibrary> LIBRARIES = const [ | 94 static const List<SdkLibrary> LIBRARIES = const [ |
| 98 LIB_CORE, | 95 LIB_CORE, |
| 99 LIB_ASYNC, | 96 LIB_ASYNC, |
| 100 LIB_MATH, | 97 LIB_MATH, |
| 101 LIB_HTML,]; | 98 LIB_HTML,]; |
| 102 | 99 |
| 100 final resource.MemoryResourceProvider provider = |
| 101 new resource.MemoryResourceProvider(); |
| 102 |
| 103 MockSdk() { | 103 MockSdk() { |
| 104 LIBRARIES.forEach((_MockSdkLibrary library) { | 104 LIBRARIES.forEach((_MockSdkLibrary library) { |
| 105 provider.newFile(library.path, library.content); | 105 provider.newFile(library.path, library.content); |
| 106 }); | 106 }); |
| 107 } | 107 } |
| 108 | 108 |
| 109 // Not used | |
| 110 @override | 109 @override |
| 111 AnalysisContext get context => throw unimplemented; | 110 AnalysisContext get context => throw unimplemented; |
| 112 | 111 |
| 113 @override | 112 @override |
| 114 List<SdkLibrary> get sdkLibraries => LIBRARIES; | 113 List<SdkLibrary> get sdkLibraries => LIBRARIES; |
| 115 | 114 |
| 116 @override | 115 @override |
| 117 String get sdkVersion => throw unimplemented; | 116 String get sdkVersion => throw unimplemented; |
| 118 | 117 |
| 119 UnimplementedError get unimplemented => new UnimplementedError(); | 118 UnimplementedError get unimplemented => new UnimplementedError(); |
| 120 | 119 |
| 121 @override | 120 @override |
| 122 List<String> get uris => throw unimplemented; | 121 List<String> get uris => throw unimplemented; |
| 123 | 122 |
| 124 // Not used. | |
| 125 @override | 123 @override |
| 126 Source fromEncoding(UriKind kind, Uri uri) { | 124 Source fromFileUri(Uri uri) { |
| 127 resource.Resource file = provider.getResource(uri.path); | 125 resource.Resource file = provider.getResource(uri.path); |
| 128 if (file is resource.File) { | 126 if (file is resource.File) { |
| 129 return file.createSource(kind); | 127 return file.createSource(uri); |
| 130 } | 128 } |
| 131 return null; | 129 return null; |
| 132 } | 130 } |
| 133 | 131 |
| 134 // Not used. | |
| 135 @override | 132 @override |
| 136 SdkLibrary getSdkLibrary(String dartUri) { | 133 SdkLibrary getSdkLibrary(String dartUri) { |
| 137 // getSdkLibrary() is only used to determine whether a library is internal | 134 // getSdkLibrary() is only used to determine whether a library is internal |
| 138 // to the SDK. The mock SDK doesn't have any internals, so it's safe to | 135 // to the SDK. The mock SDK doesn't have any internals, so it's safe to |
| 139 // return null. | 136 // return null. |
| 140 return null; | 137 return null; |
| 141 } | 138 } |
| 142 | 139 |
| 143 // Not used. | |
| 144 @override | 140 @override |
| 145 Source mapDartUri(String dartUri) { | 141 Source mapDartUri(String dartUri) { |
| 146 const Map<String, String> uriToPath = const { | 142 const Map<String, String> uriToPath = const { |
| 147 "dart:core": "/lib/core/core.dart", | 143 "dart:core": "/lib/core/core.dart", |
| 148 "dart:html": "/lib/html/dartium/html_dartium.dart", | 144 "dart:html": "/lib/html/dartium/html_dartium.dart", |
| 149 "dart:async": "/lib/async/async.dart", | 145 "dart:async": "/lib/async/async.dart", |
| 150 "dart:math": "/lib/math/math.dart" | 146 "dart:math": "/lib/math/math.dart" |
| 151 }; | 147 }; |
| 152 | 148 |
| 153 String path = uriToPath[dartUri]; | 149 String path = uriToPath[dartUri]; |
| 154 if (path != null) { | 150 if (path != null) { |
| 155 resource.File file = provider.getResource(path); | 151 resource.File file = provider.getResource(path); |
| 156 return file.createSource(UriKind.DART_URI); | 152 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); |
| 153 return file.createSource(uri); |
| 157 } | 154 } |
| 158 | 155 |
| 159 // If we reach here then we tried to use a dartUri that's not in the | 156 // If we reach here then we tried to use a dartUri that's not in the |
| 160 // table above. | 157 // table above. |
| 161 throw unimplemented; | 158 throw unimplemented; |
| 162 } | 159 } |
| 163 } | 160 } |
| 164 | 161 |
| 165 | 162 |
| 166 class _MockSdkLibrary implements SdkLibrary { | 163 class _MockSdkLibrary implements SdkLibrary { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 186 bool get isInternal => throw unimplemented; | 183 bool get isInternal => throw unimplemented; |
| 187 | 184 |
| 188 @override | 185 @override |
| 189 bool get isShared => throw unimplemented; | 186 bool get isShared => throw unimplemented; |
| 190 | 187 |
| 191 @override | 188 @override |
| 192 bool get isVmLibrary => throw unimplemented; | 189 bool get isVmLibrary => throw unimplemented; |
| 193 | 190 |
| 194 UnimplementedError get unimplemented => new UnimplementedError(); | 191 UnimplementedError get unimplemented => new UnimplementedError(); |
| 195 } | 192 } |
| OLD | NEW |