| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library testing.mock_sdk; | |
| 6 | |
| 7 import 'package:analyzer/file_system/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'; | |
| 10 import 'package:analyzer/src/generated/sdk.dart'; | |
| 11 import 'package:analyzer/src/generated/source.dart'; | |
| 12 | |
| 13 | |
| 14 class MockSdk implements DartSdk { | |
| 15 static const _MockSdkLibrary LIB_CORE = | |
| 16 const _MockSdkLibrary('core', '/lib/core/core.dart', ''' | |
| 17 library dart.core; | |
| 18 | |
| 19 class Object { | |
| 20 bool operator ==(other) => identical(this, other); | |
| 21 } | |
| 22 | |
| 23 class Function {} | |
| 24 class StackTrace {} | |
| 25 class Symbol {} | |
| 26 class Type {} | |
| 27 | |
| 28 abstract class Comparable<T> { | |
| 29 int compareTo(T other); | |
| 30 } | |
| 31 | |
| 32 class String implements Comparable<String> { | |
| 33 bool get isEmpty => false; | |
| 34 bool get isNotEmpty => false; | |
| 35 int get length => 0; | |
| 36 } | |
| 37 | |
| 38 class bool extends Object {} | |
| 39 abstract class num implements Comparable<num> { | |
| 40 bool operator <(num other); | |
| 41 num operator +(num other); | |
| 42 num operator -(num other); | |
| 43 num operator *(num other); | |
| 44 num operator /(num other); | |
| 45 int toInt(); | |
| 46 } | |
| 47 abstract class int extends num { | |
| 48 bool get isEven => false; | |
| 49 int operator -(); | |
| 50 } | |
| 51 class double extends num {} | |
| 52 class DateTime extends Object {} | |
| 53 class Null extends Object {} | |
| 54 | |
| 55 class Deprecated extends Object { | |
| 56 final String expires; | |
| 57 const Deprecated(this.expires); | |
| 58 } | |
| 59 const Object deprecated = const Deprecated("next release"); | |
| 60 | |
| 61 abstract class List<E> extends Object { | |
| 62 void add(E value); | |
| 63 E operator [](int index); | |
| 64 void operator []=(int index, E value); | |
| 65 } | |
| 66 class Map<K, V> extends Object {} | |
| 67 | |
| 68 external bool identical(Object a, Object b); | |
| 69 | |
| 70 void print(Object object) {} | |
| 71 '''); | |
| 72 | |
| 73 static const _MockSdkLibrary LIB_ASYNC = | |
| 74 const _MockSdkLibrary('async', '/lib/async/async.dart', ''' | |
| 75 library dart.async; | |
| 76 class Future { | |
| 77 static Future wait(List<Future> futures) => null; | |
| 78 } | |
| 79 | |
| 80 class Stream<T> {} | |
| 81 '''); | |
| 82 | |
| 83 static const _MockSdkLibrary LIB_MATH = | |
| 84 const _MockSdkLibrary('math', '/lib/math/math.dart', ''' | |
| 85 library dart.math; | |
| 86 const double E = 2.718281828459045; | |
| 87 const double PI = 3.1415926535897932; | |
| 88 num min(num a, num b) => 0; | |
| 89 num max(num a, num b) => 0; | |
| 90 class Random {} | |
| 91 '''); | |
| 92 | |
| 93 static const _MockSdkLibrary LIB_HTML = | |
| 94 const _MockSdkLibrary('html', '/lib/html/dartium/html_dartium.dart', ''' | |
| 95 library dart.html; | |
| 96 class HtmlElement {} | |
| 97 '''); | |
| 98 | |
| 99 static const List<SdkLibrary> LIBRARIES = const [ | |
| 100 LIB_CORE, | |
| 101 LIB_ASYNC, | |
| 102 LIB_MATH, | |
| 103 LIB_HTML,]; | |
| 104 | |
| 105 final resource.MemoryResourceProvider provider = | |
| 106 new resource.MemoryResourceProvider(); | |
| 107 | |
| 108 MockSdk() { | |
| 109 LIBRARIES.forEach((_MockSdkLibrary library) { | |
| 110 provider.newFile(library.path, library.content); | |
| 111 }); | |
| 112 } | |
| 113 | |
| 114 @override | |
| 115 AnalysisContext get context => throw unimplemented; | |
| 116 | |
| 117 @override | |
| 118 List<SdkLibrary> get sdkLibraries => LIBRARIES; | |
| 119 | |
| 120 @override | |
| 121 String get sdkVersion => throw unimplemented; | |
| 122 | |
| 123 UnimplementedError get unimplemented => new UnimplementedError(); | |
| 124 | |
| 125 @override | |
| 126 List<String> get uris => throw unimplemented; | |
| 127 | |
| 128 @override | |
| 129 Source fromFileUri(Uri uri) { | |
| 130 String filePath = uri.path; | |
| 131 String libPath = '/lib'; | |
| 132 if (!filePath.startsWith("$libPath/")) { | |
| 133 return null; | |
| 134 } | |
| 135 for (SdkLibrary library in LIBRARIES) { | |
| 136 String libraryPath = library.path; | |
| 137 if (filePath.replaceAll('\\', '/') == libraryPath) { | |
| 138 String path = library.shortName; | |
| 139 try { | |
| 140 resource.File file = provider.getResource(uri.path); | |
| 141 Uri dartUri = new Uri(scheme: 'dart', path: library.shortName); | |
| 142 return file.createSource(dartUri); | |
| 143 } catch (exception) { | |
| 144 return null; | |
| 145 } | |
| 146 } | |
| 147 if (filePath.startsWith("$libraryPath/")) { | |
| 148 String pathInLibrary = filePath.substring(libraryPath.length + 1); | |
| 149 String path = '${library.shortName}/${pathInLibrary}'; | |
| 150 try { | |
| 151 resource.File file = provider.getResource(uri.path); | |
| 152 Uri dartUri = new Uri(scheme: 'dart', path: path); | |
| 153 return file.createSource(dartUri); | |
| 154 } catch (exception) { | |
| 155 return null; | |
| 156 } | |
| 157 } | |
| 158 } | |
| 159 return null; | |
| 160 } | |
| 161 | |
| 162 @override | |
| 163 SdkLibrary getSdkLibrary(String dartUri) { | |
| 164 // getSdkLibrary() is only used to determine whether a library is internal | |
| 165 // to the SDK. The mock SDK doesn't have any internals, so it's safe to | |
| 166 // return null. | |
| 167 return null; | |
| 168 } | |
| 169 | |
| 170 @override | |
| 171 Source mapDartUri(String dartUri) { | |
| 172 const Map<String, String> uriToPath = const { | |
| 173 "dart:core": "/lib/core/core.dart", | |
| 174 "dart:html": "/lib/html/dartium/html_dartium.dart", | |
| 175 "dart:async": "/lib/async/async.dart", | |
| 176 "dart:math": "/lib/math/math.dart" | |
| 177 }; | |
| 178 | |
| 179 String path = uriToPath[dartUri]; | |
| 180 if (path != null) { | |
| 181 resource.File file = provider.getResource(path); | |
| 182 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); | |
| 183 return file.createSource(uri); | |
| 184 } | |
| 185 | |
| 186 // If we reach here then we tried to use a dartUri that's not in the | |
| 187 // table above. | |
| 188 throw unimplemented; | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 | |
| 193 class _MockSdkLibrary implements SdkLibrary { | |
| 194 final String shortName; | |
| 195 final String path; | |
| 196 final String content; | |
| 197 | |
| 198 const _MockSdkLibrary(this.shortName, this.path, this.content); | |
| 199 | |
| 200 @override | |
| 201 String get category => throw unimplemented; | |
| 202 | |
| 203 @override | |
| 204 bool get isDart2JsLibrary => throw unimplemented; | |
| 205 | |
| 206 @override | |
| 207 bool get isDocumented => throw unimplemented; | |
| 208 | |
| 209 @override | |
| 210 bool get isImplementation => throw unimplemented; | |
| 211 | |
| 212 @override | |
| 213 bool get isInternal => throw unimplemented; | |
| 214 | |
| 215 @override | |
| 216 bool get isShared => throw unimplemented; | |
| 217 | |
| 218 @override | |
| 219 bool get isVmLibrary => throw unimplemented; | |
| 220 | |
| 221 UnimplementedError get unimplemented => new UnimplementedError(); | |
| 222 } | |
| OLD | NEW |