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 stub_core_libraries.test; | |
6 | |
7 import 'dart:io'; | |
8 | |
9 import 'package:path/path.dart' as p; | |
10 import 'package:stub_core_library/stub_core_library.dart'; | |
11 import 'package:unittest/unittest.dart'; | |
12 | |
13 String sandbox; | |
14 | |
15 main() { | |
16 setUp(() { | |
17 sandbox = Directory.systemTemp.createTempSync('stub_core_library_').path; | |
18 }); | |
19 | |
20 tearDown(() => new Directory(sandbox).deleteSync(recursive: true)); | |
21 | |
22 group("imports", () { | |
23 test("dart: imports are preserved by default", () { | |
24 expectStub(""" | |
25 import "dart:core"; | |
26 import "dart:html"; | |
27 import "dart:fblthp"; | |
28 """, """ | |
29 import "dart:core"; | |
30 import "dart:html"; | |
31 import "dart:fblthp"; | |
32 """); | |
33 }); | |
34 | |
35 test("internal dart: imports are removed", () { | |
36 expectStub(""" | |
37 import "dart:_internal"; | |
38 import "dart:_blink" as _blink; | |
39 import "dart:_fblthp"; | |
40 """, ""); | |
41 }); | |
42 | |
43 test("replaced imports are replaced", () { | |
44 expectStub(""" | |
45 import "dart:html"; | |
46 import "foo.dart" as foo; | |
47 """, """ | |
48 import "dart_html.dart"; | |
49 import "bar.dart" as foo; | |
50 """, { | |
51 "dart:html": "dart_html.dart", | |
52 "foo.dart": "bar.dart" | |
53 }); | |
54 }); | |
55 | |
56 test("exports are replaced as well", () { | |
57 expectStub(""" | |
58 export "dart:html"; | |
59 export "foo.dart" show foo; | |
60 """, """ | |
61 export "dart_html.dart"; | |
62 export "bar.dart" show foo; | |
63 """, { | |
64 "dart:html": "dart_html.dart", | |
65 "foo.dart": "bar.dart" | |
66 }); | |
67 }); | |
68 }); | |
69 | |
70 test("a parted file is stubbed and included inline", () { | |
71 new File(p.join(sandbox, "part.dart")).writeAsStringSync(""" | |
72 part of lib; | |
73 void foo() => print('foo!'); | |
74 """); | |
75 | |
76 expectStub(""" | |
77 library lib; | |
78 part "part.dart"; | |
79 """, """ | |
80 library lib; | |
81 void foo() { ${unsupported("foo()")}; } | |
82 """); | |
83 }); | |
84 | |
85 test("a class declaration's native clause is removed", () { | |
86 expectStub("class Foo native 'Foo' {}", "class Foo {}"); | |
87 }); | |
88 | |
89 group("constructors", () { | |
90 test("a constructor's body is stubbed out", () { | |
91 expectStub(""" | |
92 class Foo { | |
93 Foo() { print("Created a foo!"); } | |
94 } | |
95 """, """ | |
96 class Foo { | |
97 Foo() { ${unsupported("new Foo()")}; } | |
98 } | |
99 """); | |
100 }); | |
101 | |
102 test("a constructor's empty body is stubbed out", () { | |
103 expectStub("class Foo { Foo(); }", """ | |
104 class Foo { | |
105 Foo() { ${unsupported("new Foo()")}; } | |
106 } | |
107 """); | |
108 }); | |
109 | |
110 test("a constructor's external declaration is removed", () { | |
111 expectStub("class Foo { external Foo(); }", """ | |
112 class Foo { | |
113 Foo() { ${unsupported("new Foo()")}; } | |
114 } | |
115 """); | |
116 }); | |
117 | |
118 test("a constructor's field initializers are removed", () { | |
119 expectStub(""" | |
120 class Foo { | |
121 final _field1; | |
122 final _field2; | |
123 | |
124 Foo() | |
125 : _field1 = 1, | |
126 _field2 = 2; | |
127 } | |
128 """, """ | |
129 class Foo { | |
130 Foo() { ${unsupported("new Foo()")}; } | |
131 } | |
132 """); | |
133 }); | |
134 | |
135 test("a constructor's redirecting initializers are removed", () { | |
136 expectStub(""" | |
137 class Foo { | |
138 Foo() : this.create(); | |
139 Foo.create(); | |
140 } | |
141 """, """ | |
142 class Foo { | |
143 Foo() { ${unsupported("new Foo()")}; } | |
144 Foo.create() { ${unsupported("new Foo.create()")}; } | |
145 } | |
146 """); | |
147 }); | |
148 | |
149 test("a constructor's superclass calls are preserved", () { | |
150 expectStub(""" | |
151 class Foo { | |
152 Foo(int i, int j, {int k}); | |
153 } | |
154 | |
155 class Bar extends Foo { | |
156 Bar() : super(1, 2, k: 3); | |
157 } | |
158 """, """ | |
159 class Foo { | |
160 Foo(int i, int j, {int k}) { ${unsupported("new Foo()")}; } | |
161 } | |
162 | |
163 class Bar extends Foo { | |
164 Bar() | |
165 : super(${unsupported("new Bar()")}, null) { | |
166 ${unsupported("new Bar()")}; | |
167 } | |
168 } | |
169 """); | |
170 }); | |
171 | |
172 test("a constructor's initializing formals are replaced with normal " | |
173 "parameters", () { | |
174 expectStub(""" | |
175 class Foo { | |
176 final int _i; | |
177 var _j; | |
178 final List<int> _k; | |
179 | |
180 Foo(this._i, this._j, this._k); | |
181 } | |
182 """, """ | |
183 class Foo { | |
184 Foo(int _i, _j, List<int> _k) { ${unsupported("new Foo()")}; } | |
185 } | |
186 """); | |
187 }); | |
188 | |
189 test("a const constructor isn't stubbed", () { | |
190 expectStub("class Foo { const Foo(); }", "class Foo { const Foo(); }"); | |
191 }); | |
192 | |
193 test("a const constructor's superclass calls are fully preserved", () { | |
194 expectStub(""" | |
195 class Foo { | |
196 const Foo(int i, int j, int k); | |
197 } | |
198 | |
199 class Bar extends Foo { | |
200 const Bar() : super(1, 2, 3); | |
201 } | |
202 """, """ | |
203 class Foo { | |
204 const Foo(int i, int j, int k); | |
205 } | |
206 | |
207 class Bar extends Foo { | |
208 const Bar() : super(1, 2, 3); | |
209 } | |
210 """); | |
211 }); | |
212 | |
213 test("a redirecting const constructor stops redirecting", () { | |
214 expectStub(""" | |
215 class Foo { | |
216 const Foo.named(int i, int j, int k) | |
217 : this(i, j, k); | |
218 const Foo(int i, int j, int k); | |
219 } | |
220 """, """ | |
221 class Foo { | |
222 const Foo.named(int i, int j, int k); | |
223 const Foo(int i, int j, int k); | |
224 } | |
225 """); | |
226 }); | |
227 }); | |
228 | |
229 group("functions", () { | |
230 test("stubs a top-level function", () { | |
231 expectStub("void foo() => print('hello!');", | |
232 "void foo() { ${unsupported('foo()')}; }"); | |
233 }); | |
234 | |
235 test("stubs a private top-level function", () { | |
236 expectStub("void _foo() => print('hello!');", | |
237 "void _foo() { ${unsupported('_foo()')}; }"); | |
238 }); | |
239 | |
240 test("stubs a method", () { | |
241 expectStub(""" | |
242 class Foo { | |
243 foo() => print("hello!"); | |
244 } | |
245 """, """ | |
246 class Foo { | |
247 foo() { ${unsupported('Foo.foo()')}; } | |
248 } | |
249 """); | |
250 }); | |
251 | |
252 test("empties a method in an unconstructable class", () { | |
253 expectStub(""" | |
254 class Foo { | |
255 Foo(); | |
256 foo() => print("hello!"); | |
257 } | |
258 """, """ | |
259 class Foo { | |
260 Foo() { ${unsupported('new Foo()')}; } | |
261 foo() {} | |
262 } | |
263 """); | |
264 }); | |
265 | |
266 test("removes a private instance method", () { | |
267 expectStub(""" | |
268 class Foo { | |
269 _foo() => print("hello!"); | |
270 } | |
271 """, "class Foo {}"); | |
272 }); | |
273 | |
274 test("stubs a private static method", () { | |
275 expectStub(""" | |
276 class Foo { | |
277 static _foo() => print("hello!"); | |
278 } | |
279 """, """ | |
280 class Foo { | |
281 static _foo() { ${unsupported('Foo._foo()')}; } | |
282 } | |
283 """); | |
284 }); | |
285 | |
286 test("preserves an abstract instance method", () { | |
287 expectStub("abstract class Foo { foo(); }", | |
288 "abstract class Foo { foo(); }"); | |
289 }); | |
290 | |
291 test("removes a native function body", () { | |
292 expectStub("void foo() native 'foo';", | |
293 "void foo() { ${unsupported('foo()')}; }"); | |
294 }); | |
295 }); | |
296 | |
297 group("top-level fields", () { | |
298 test("stubs out a top-level field", () { | |
299 expectStub("int foo;", """ | |
300 int get foo => ${unsupported('foo')}; | |
301 set foo(int _) { ${unsupported('foo=')}; } | |
302 """); | |
303 }); | |
304 | |
305 test("stubs out a top-level field with a value", () { | |
306 expectStub("int foo = 12;", """ | |
307 int get foo => ${unsupported('foo')}; | |
308 set foo(int _) { ${unsupported('foo=')}; } | |
309 """); | |
310 }); | |
311 | |
312 test("stubs out a final top-level field", () { | |
313 expectStub("final int foo = 12;", | |
314 "int get foo => ${unsupported('foo')};"); | |
315 }); | |
316 | |
317 test("preserves a const top-level field", () { | |
318 expectStub("const foo = 12;", "const foo = 12;"); | |
319 }); | |
320 | |
321 test("removes a private top-level field", () { | |
322 expectStub("int _foo = 12;", ""); | |
323 }); | |
324 | |
325 test("preserves a private const top-level field", () { | |
326 expectStub("const _foo = 12;", "const _foo = 12;"); | |
327 }); | |
328 | |
329 test("splits a multiple-declaration top-level field", () { | |
330 expectStub("int foo, bar, baz;", """ | |
331 int get foo => ${unsupported('foo')}; | |
332 set foo(int _) { ${unsupported('foo=')}; } | |
333 int get bar => ${unsupported('bar')}; | |
334 set bar(int _) { ${unsupported('bar=')}; } | |
335 int get baz => ${unsupported('baz')}; | |
336 set baz(int _) { ${unsupported('baz=')}; } | |
337 """); | |
338 }); | |
339 }); | |
340 | |
341 group("instance fields", () { | |
342 test("stubs out an instance field", () { | |
343 expectStub("class Foo { int foo; }", """ | |
344 class Foo { | |
345 int get foo => ${unsupported('Foo.foo')}; | |
346 set foo(int _) { ${unsupported('Foo.foo=')}; } | |
347 } | |
348 """); | |
349 }); | |
350 | |
351 test("stubs out an instance field with a value", () { | |
352 expectStub("class Foo { int foo = 12; }", """ | |
353 class Foo { | |
354 int get foo => ${unsupported('Foo.foo')}; | |
355 set foo(int _) { ${unsupported('Foo.foo=')}; } | |
356 } | |
357 """); | |
358 }); | |
359 | |
360 test("stubs out a final instance field", () { | |
361 expectStub("class Foo { final int foo = 12; }", """ | |
362 class Foo { | |
363 int get foo => ${unsupported('Foo.foo')}; | |
364 } | |
365 """); | |
366 }); | |
367 | |
368 test("removes a private instance field", () { | |
369 expectStub("class Foo { int _foo = 12; }", "class Foo { }"); | |
370 }); | |
371 | |
372 test("stubs out a static instance field", () { | |
373 expectStub("class Foo { static int foo = 12; }", """ | |
374 class Foo { | |
375 static int get foo => ${unsupported('Foo.foo')}; | |
376 static set foo(int _) { ${unsupported('Foo.foo=')}; } | |
377 } | |
378 """); | |
379 }); | |
380 | |
381 test("removes a private static instance field", () { | |
382 expectStub("class Foo { static int _foo = 12; }", "class Foo { }"); | |
383 }); | |
384 | |
385 test("preserves a static const instance field", () { | |
386 expectStub("class Foo { static const foo = 12; }", | |
387 "class Foo { static const foo = 12; }"); | |
388 }); | |
389 | |
390 test("nulls a field for an unconstructable class", () { | |
391 expectStub(""" | |
392 class Foo { | |
393 Foo(); | |
394 final foo = 12; | |
395 } | |
396 """, """ | |
397 class Foo { | |
398 Foo() { ${unsupported("new Foo()")}; } | |
399 final foo = null; | |
400 } | |
401 """); | |
402 }); | |
403 | |
404 test("splits a multiple-declaration instance field", () { | |
405 expectStub("class Foo { int foo, bar, baz; }", """ | |
406 class Foo { | |
407 int get foo => ${unsupported('Foo.foo')}; | |
408 set foo(int _) { ${unsupported('Foo.foo=')}; } | |
409 int get bar => ${unsupported('Foo.bar')}; | |
410 set bar(int _) { ${unsupported('Foo.bar=')}; } | |
411 int get baz => ${unsupported('Foo.baz')}; | |
412 set baz(int _) { ${unsupported('Foo.baz=')}; } | |
413 } | |
414 """); | |
415 }); | |
416 }); | |
417 } | |
418 | |
419 /// Expects that [source] will transform into [expected] when stubbed. | |
420 void expectStub(String source, String expected, | |
421 [Map<String, String> importReplacements]) { | |
422 expect(stubCode(source, p.join(sandbox, 'source.dart'), importReplacements), | |
423 equalsIgnoringWhitespace(expected)); | |
424 } | |
425 | |
426 /// Returns code for throwing an [UnsupportedError] for the given name. | |
427 String unsupported(String name) => 'throw new UnsupportedError("$name is ' | |
428 'unsupported on this platform.")'; | |
OLD | NEW |