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, {int l}); | |
Bob Nystrom
2014/07/15 17:48:34
Nit, but can you use something other than "l" here
nweiz
2014/07/15 20:09:07
Done.
| |
153 } | |
154 | |
155 class Bar extends Foo { | |
156 Bar() : super(1, 2, 3, l: 4); | |
157 } | |
158 """, """ | |
159 class Foo { | |
160 Foo(int i, int j, int k, {int l}) { ${unsupported("new Foo()")}; } | |
161 } | |
162 | |
163 class Bar extends Foo { | |
164 Bar() | |
165 : super(${unsupported("new Bar()")}, null, null) { | |
166 ${unsupported("new Bar()")}; | |
167 } | |
168 } | |
169 """); | |
170 }); | |
171 | |
172 test("a constructor's field parameters are replaced with normal " | |
Bob Nystrom
2014/07/15 17:48:34
"field parameters" -> "initializing formals".
nweiz
2014/07/15 20:09:07
Done.
| |
173 "parameters", () { | |
174 expectStub(""" | |
175 class Foo { | |
176 final int _i; | |
177 final String _j; | |
178 final List<int> _k; | |
Bob Nystrom
2014/07/15 17:48:34
Make one of these untyped with "var" and make sure
nweiz
2014/07/15 20:09:07
Done.
| |
179 | |
180 Foo(this._i, this._j, this._k); | |
181 } | |
182 """, """ | |
183 class Foo { | |
184 Foo(int _i, String _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", () { | |
Bob Nystrom
2014/07/15 17:48:34
Can you have a redirecting const constructor? If s
nweiz
2014/07/15 20:09:07
Done.
| |
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 | |
214 group("functions", () { | |
215 test("stubs a top-level function", () { | |
216 expectStub("void foo() => print('hello!');", | |
217 "void foo() { ${unsupported('foo()')}; }"); | |
218 }); | |
219 | |
220 test("stubs a private top-level function", () { | |
221 expectStub("void _foo() => print('hello!');", | |
222 "void _foo() { ${unsupported('_foo()')}; }"); | |
223 }); | |
224 | |
225 test("stubs a method", () { | |
226 expectStub(""" | |
227 class Foo { | |
228 foo() => print("hello!"); | |
229 } | |
230 """, """ | |
231 class Foo { | |
232 foo() { ${unsupported('Foo.foo()')}; } | |
233 } | |
234 """); | |
235 }); | |
236 | |
237 test("empties a method in an unconstructable class", () { | |
238 expectStub(""" | |
239 class Foo { | |
240 Foo(); | |
241 foo() => print("hello!"); | |
242 } | |
243 """, """ | |
244 class Foo { | |
245 Foo() { ${unsupported('new Foo()')}; } | |
246 foo() {} | |
247 } | |
248 """); | |
249 }); | |
250 | |
251 test("removes a private instance method", () { | |
252 expectStub(""" | |
253 class Foo { | |
254 _foo() => print("hello!"); | |
255 } | |
256 """, "class Foo {}"); | |
257 }); | |
258 | |
259 test("stubs a private static method", () { | |
260 expectStub(""" | |
261 class Foo { | |
262 static _foo() => print("hello!"); | |
263 } | |
264 """, """ | |
265 class Foo { | |
266 static _foo() { ${unsupported('Foo._foo()')}; } | |
267 } | |
268 """); | |
269 }); | |
270 | |
271 test("preserves an abstract instance method", () { | |
272 expectStub("abstract class Foo { foo(); }", | |
273 "abstract class Foo { foo(); }"); | |
274 }); | |
275 | |
276 test("removes a native function body", () { | |
277 expectStub("void foo() native 'foo';", | |
278 "void foo() { ${unsupported('foo()')}; }"); | |
279 }); | |
280 }); | |
281 | |
282 group("top-level fields", () { | |
283 test("stubs out a top-level field", () { | |
284 expectStub("int foo;", """ | |
285 int get foo => ${unsupported('foo')}; | |
286 set foo(int _) { ${unsupported('foo=')}; } | |
287 """); | |
288 }); | |
289 | |
290 test("stubs out a top-level field with a value", () { | |
291 expectStub("int foo = 12;", """ | |
292 int get foo => ${unsupported('foo')}; | |
293 set foo(int _) { ${unsupported('foo=')}; } | |
294 """); | |
295 }); | |
296 | |
297 test("stubs out a final top-level field", () { | |
298 expectStub("final int foo = 12;", | |
299 "int get foo => ${unsupported('foo')};"); | |
300 }); | |
301 | |
302 test("preserves a const top-level field", () { | |
303 expectStub("const foo = 12;", "const foo = 12;"); | |
304 }); | |
305 | |
306 test("removes a private top-level field", () { | |
307 expectStub("int _foo = 12;", ""); | |
308 }); | |
309 | |
310 test("preserves a private const top-level field", () { | |
311 expectStub("const _foo = 12;", "const _foo = 12;"); | |
312 }); | |
313 | |
314 test("splits a multiple-declaration top-level field", () { | |
315 expectStub("int foo, bar, baz;", """ | |
316 int get foo => ${unsupported('foo')}; | |
317 set foo(int _) { ${unsupported('foo=')}; } | |
318 int get bar => ${unsupported('bar')}; | |
319 set bar(int _) { ${unsupported('bar=')}; } | |
320 int get baz => ${unsupported('baz')}; | |
321 set baz(int _) { ${unsupported('baz=')}; } | |
322 """); | |
323 }); | |
324 }); | |
325 | |
326 group("instance fields", () { | |
327 test("stubs out an instance field", () { | |
328 expectStub("class Foo { int foo; }", """ | |
329 class Foo { | |
330 int get foo => ${unsupported('Foo.foo')}; | |
331 set foo(int _) { ${unsupported('Foo.foo=')}; } | |
332 } | |
333 """); | |
334 }); | |
335 | |
336 test("stubs out an instance field with a value", () { | |
337 expectStub("class Foo { int foo = 12; }", """ | |
338 class Foo { | |
339 int get foo => ${unsupported('Foo.foo')}; | |
340 set foo(int _) { ${unsupported('Foo.foo=')}; } | |
341 } | |
342 """); | |
343 }); | |
344 | |
345 test("stubs out a final instance field", () { | |
346 expectStub("class Foo { final int foo = 12; }", """ | |
347 class Foo { | |
348 int get foo => ${unsupported('Foo.foo')}; | |
349 } | |
350 """); | |
351 }); | |
352 | |
353 test("removes a private instance field", () { | |
354 expectStub("class Foo { int _foo = 12; }", "class Foo { }"); | |
355 }); | |
356 | |
357 test("stubs out a static instance field", () { | |
358 expectStub("class Foo { static int foo = 12; }", """ | |
359 class Foo { | |
360 static int get foo => ${unsupported('Foo.foo')}; | |
361 static set foo(int _) { ${unsupported('Foo.foo=')}; } | |
362 } | |
363 """); | |
364 }); | |
365 | |
366 test("removes a private static instance field", () { | |
367 expectStub("class Foo { static int _foo = 12; }", "class Foo { }"); | |
368 }); | |
369 | |
370 test("preserves a static const instance field", () { | |
371 expectStub("class Foo { static const foo = 12; }", | |
372 "class Foo { static const foo = 12; }"); | |
373 }); | |
374 | |
375 test("nulls a field for an unconstructable class", () { | |
376 expectStub(""" | |
377 class Foo { | |
378 Foo(); | |
379 final foo = 12; | |
380 } | |
381 """, """ | |
382 class Foo { | |
383 Foo() { ${unsupported("new Foo()")}; } | |
384 final foo = null; | |
385 } | |
386 """); | |
387 }); | |
388 | |
389 test("splits a multiple-declaration instance field", () { | |
390 expectStub("class Foo { int foo, bar, baz; }", """ | |
391 class Foo { | |
392 int get foo => ${unsupported('Foo.foo')}; | |
393 set foo(int _) { ${unsupported('Foo.foo=')}; } | |
394 int get bar => ${unsupported('Foo.bar')}; | |
395 set bar(int _) { ${unsupported('Foo.bar=')}; } | |
396 int get baz => ${unsupported('Foo.baz')}; | |
397 set baz(int _) { ${unsupported('Foo.baz=')}; } | |
398 } | |
399 """); | |
400 }); | |
401 }); | |
402 } | |
403 | |
404 /// Expects that [source] will transform into [expected] when stubbed. | |
405 void expectStub(String source, String expected, | |
406 [Map<String, String> importReplacements]) { | |
407 expect(stubCode(source, p.join(sandbox, 'source.dart'), importReplacements), | |
408 equalsIgnoringWhitespace(expected)); | |
409 } | |
410 | |
411 /// Returns code for throwing an [UnsupportedError] for the given name. | |
412 String unsupported(String name) => 'throw new UnsupportedError("$name is ' | |
413 'unsupported on this platform.")'; | |
OLD | NEW |