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 Iterable<E> {} | |
62 | |
63 abstract class List<E> extends Object implements Iterable { | |
64 void add(E value); | |
65 E operator [](int index); | |
66 void operator []=(int index, E value); | |
67 } | |
68 class Map<K, V> extends Object {} | |
69 | |
70 external bool identical(Object a, Object b); | |
71 | |
72 void print(Object object) {} | |
73 | |
74 typedef int Comparator<T>(T a, T b); | |
75 '''); | |
76 | |
77 static const _MockSdkLibrary LIB_ASYNC = | |
78 const _MockSdkLibrary('async', '/lib/async/async.dart', ''' | |
79 library dart.async; | |
80 class Future<T> { | |
81 static Future wait(List<Future> futures) => null; | |
82 } | |
83 | |
84 class Stream<T> {} | |
85 '''); | |
86 | |
87 static const _MockSdkLibrary LIB_MATH = | |
88 const _MockSdkLibrary('math', '/lib/math/math.dart', ''' | |
89 library dart.math; | |
90 const double E = 2.718281828459045; | |
91 const double PI = 3.1415926535897932; | |
92 num min(num a, num b) => 0; | |
93 num max(num a, num b) => 0; | |
94 class Random {} | |
95 '''); | |
96 | |
97 static const _MockSdkLibrary LIB_HTML = | |
98 const _MockSdkLibrary('html', '/lib/html/dartium/html_dartium.dart', ''' | |
99 library dart.html; | |
100 class HtmlElement {} | |
101 '''); | |
102 | |
103 static const List<SdkLibrary> LIBRARIES = const [ | |
104 LIB_CORE, | |
105 LIB_ASYNC, | |
106 LIB_MATH, | |
107 LIB_HTML,]; | |
108 | |
109 final resource.MemoryResourceProvider provider = | |
110 new resource.MemoryResourceProvider(); | |
111 | |
112 /** | |
113 * The [AnalysisContext] which is used for all of the sources. | |
114 */ | |
115 InternalAnalysisContext _analysisContext; | |
116 | |
117 MockSdk() { | |
118 LIBRARIES.forEach((_MockSdkLibrary library) { | |
119 provider.newFile(library.path, library.content); | |
120 }); | |
121 } | |
122 | |
123 @override | |
124 AnalysisContext get context { | |
125 if (_analysisContext == null) { | |
126 _analysisContext = new SdkAnalysisContext(); | |
127 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); | |
128 _analysisContext.sourceFactory = factory; | |
129 ChangeSet changeSet = new ChangeSet(); | |
130 for (String uri in uris) { | |
131 Source source = factory.forUri(uri); | |
132 changeSet.addedSource(source); | |
133 } | |
134 _analysisContext.applyChanges(changeSet); | |
135 } | |
136 return _analysisContext; | |
137 } | |
138 | |
139 @override | |
140 List<SdkLibrary> get sdkLibraries => LIBRARIES; | |
141 | |
142 @override | |
143 String get sdkVersion => throw unimplemented; | |
144 | |
145 UnimplementedError get unimplemented => new UnimplementedError(); | |
146 | |
147 @override | |
148 List<String> get uris { | |
149 List<String> uris = <String>[]; | |
150 for (SdkLibrary library in LIBRARIES) { | |
151 uris.add('dart:' + library.shortName); | |
152 } | |
153 return uris; | |
154 } | |
155 | |
156 @override | |
157 Source fromFileUri(Uri uri) { | |
158 String filePath = uri.path; | |
159 String libPath = '/lib'; | |
160 if (!filePath.startsWith("$libPath/")) { | |
161 return null; | |
162 } | |
163 for (SdkLibrary library in LIBRARIES) { | |
164 String libraryPath = library.path; | |
165 if (filePath.replaceAll('\\', '/') == libraryPath) { | |
166 String path = library.shortName; | |
167 try { | |
168 resource.File file = provider.getResource(uri.path); | |
169 Uri dartUri = new Uri(scheme: 'dart', path: library.shortName); | |
170 return file.createSource(dartUri); | |
171 } catch (exception) { | |
172 return null; | |
173 } | |
174 } | |
175 if (filePath.startsWith("$libraryPath/")) { | |
176 String pathInLibrary = filePath.substring(libraryPath.length + 1); | |
177 String path = '${library.shortName}/${pathInLibrary}'; | |
178 try { | |
179 resource.File file = provider.getResource(uri.path); | |
180 Uri dartUri = new Uri(scheme: 'dart', path: path); | |
181 return file.createSource(dartUri); | |
182 } catch (exception) { | |
183 return null; | |
184 } | |
185 } | |
186 } | |
187 return null; | |
188 } | |
189 | |
190 @override | |
191 SdkLibrary getSdkLibrary(String dartUri) { | |
192 // getSdkLibrary() is only used to determine whether a library is internal | |
193 // to the SDK. The mock SDK doesn't have any internals, so it's safe to | |
194 // return null. | |
195 return null; | |
196 } | |
197 | |
198 @override | |
199 Source mapDartUri(String dartUri) { | |
200 const Map<String, String> uriToPath = const { | |
201 "dart:core": "/lib/core/core.dart", | |
202 "dart:html": "/lib/html/dartium/html_dartium.dart", | |
203 "dart:async": "/lib/async/async.dart", | |
204 "dart:math": "/lib/math/math.dart" | |
205 }; | |
206 | |
207 String path = uriToPath[dartUri]; | |
208 if (path != null) { | |
209 resource.File file = provider.getResource(path); | |
210 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); | |
211 return file.createSource(uri); | |
212 } | |
213 | |
214 // If we reach here then we tried to use a dartUri that's not in the | |
215 // table above. | |
216 throw unimplemented; | |
217 } | |
218 } | |
219 | |
220 | |
221 class _MockSdkLibrary implements SdkLibrary { | |
222 final String shortName; | |
223 final String path; | |
224 final String content; | |
225 | |
226 const _MockSdkLibrary(this.shortName, this.path, this.content); | |
227 | |
228 @override | |
229 String get category => throw unimplemented; | |
230 | |
231 @override | |
232 bool get isDart2JsLibrary => throw unimplemented; | |
233 | |
234 @override | |
235 bool get isDocumented => throw unimplemented; | |
236 | |
237 @override | |
238 bool get isImplementation => throw unimplemented; | |
239 | |
240 @override | |
241 bool get isInternal => throw unimplemented; | |
242 | |
243 @override | |
244 bool get isShared => throw unimplemented; | |
245 | |
246 @override | |
247 bool get isVmLibrary => throw unimplemented; | |
248 | |
249 UnimplementedError get unimplemented => new UnimplementedError(); | |
250 } | |
OLD | NEW |