OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, 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 import "package:expect/expect.dart"; | |
6 import 'dart:async'; | |
7 import 'dart:io' as io; | |
8 import "package:async_helper/async_helper.dart"; | |
9 import '../mock_compiler.dart'; | |
10 import '../mock_libraries.dart'; | |
11 import '../output_collector.dart'; | |
12 import 'package:compiler/compiler.dart'; | |
13 import 'package:compiler/src/common/names.dart' show Identifiers; | |
14 import 'package:compiler/src/dart_backend/dart_backend.dart'; | |
15 import 'package:compiler/src/elements/elements.dart'; | |
16 import 'package:compiler/src/tree/tree.dart'; | |
17 | |
18 const ioLib = r''' | |
19 library io; | |
20 class Platform { | |
21 static int operatingSystem; | |
22 } | |
23 '''; | |
24 | |
25 const htmlLib = r''' | |
26 library html; | |
27 Window __window; | |
28 Window get window => __window; | |
29 abstract class Window { | |
30 Navigator get navigator; | |
31 } | |
32 abstract class Navigator { | |
33 String get userAgent; | |
34 } | |
35 '''; | |
36 | |
37 /** | |
38 * Library name is assumed to be 'mylib' in 'mylib.dart' file. | |
39 */ | |
40 testDart2Dart(String mainSrc, {String librarySrc, | |
41 String expectedResult, | |
42 bool minify: false, | |
43 bool stripTypes: false}) { | |
44 | |
45 // If expectedResult is not provided, check that source string remains the | |
46 // same. | |
47 if (expectedResult == null) { | |
48 Expect.equals(null, librarySrc); | |
49 expectedResult = mainSrc; | |
50 } | |
51 | |
52 fileUri(path) => new Uri(scheme: 'file', path: path); | |
53 | |
54 final scriptUri = fileUri('script.dart'); | |
55 final libUri = fileUri('mylib.dart'); | |
56 | |
57 provider(uri) { | |
58 if (uri == scriptUri) return new Future.value(mainSrc); | |
59 if (uri.toString() == libUri.toString()) { | |
60 return new Future.value(librarySrc); | |
61 } | |
62 if (uri.path.endsWith('/dart2dart.platform')) { | |
63 return new io.File.fromUri(uri).readAsBytes(); | |
64 } else if (uri.path.endsWith('/core.dart')) { | |
65 return new Future.value(buildLibrarySource(DEFAULT_CORE_LIBRARY)); | |
66 } else if (uri.path.endsWith('/core_patch.dart')) { | |
67 return new Future.value(DEFAULT_PATCH_CORE_SOURCE); | |
68 } else if (uri.path.endsWith('/io.dart')) { | |
69 return new Future.value(ioLib); | |
70 } else if (uri.path.endsWith('/js_helper.dart')) { | |
71 return new Future.value(buildLibrarySource(DEFAULT_JS_HELPER_LIBRARY)); | |
72 } else if (uri.path.endsWith('/html_dart2js.dart')) { | |
73 // TODO(smok): The file should change to html_dartium at some point. | |
74 return new Future.value(htmlLib); | |
75 } else if (uri.path.endsWith('/foreign_helper.dart')) { | |
76 return new Future.value( | |
77 buildLibrarySource(DEFAULT_FOREIGN_HELPER_LIBRARY)); | |
78 } else if (uri.path.endsWith('/isolate_helper.dart')) { | |
79 return new Future.value( | |
80 buildLibrarySource(DEFAULT_ISOLATE_HELPER_LIBRARY)); | |
81 } | |
82 return new Future.value(''); | |
83 } | |
84 | |
85 handler(uri, begin, end, message, kind) { | |
86 if (identical(kind, Diagnostic.ERROR) || identical(kind, Diagnostic.CRASH))
{ | |
87 Expect.fail('$uri: $begin-$end: $message [$kind]'); | |
88 } | |
89 } | |
90 | |
91 final options = <String>['--output-type=dart']; | |
92 // Some tests below are using dart:io. | |
93 if (minify) options.add('--minify'); | |
94 if (stripTypes) options.add('--force-strip=types'); | |
95 | |
96 asyncTest(() { | |
97 OutputCollector outputCollector = new OutputCollector(); | |
98 return compile( | |
99 scriptUri, | |
100 Uri.base.resolve('sdk/'), | |
101 fileUri('packageRoot/'), | |
102 provider, | |
103 handler, | |
104 options, | |
105 outputCollector).then((_) { | |
106 String code = outputCollector.getOutput('', 'dart'); | |
107 Expect.equals(expectedResult, code, | |
108 'expected:\n$expectedResult\nactual:\n$code'); | |
109 }); | |
110 }); | |
111 } | |
112 | |
113 testSimpleFileUnparse() { | |
114 final src = ''' | |
115 should_be_dropped() { | |
116 } | |
117 | |
118 should_be_kept() { | |
119 } | |
120 | |
121 main() { | |
122 should_be_kept(); | |
123 } | |
124 '''; | |
125 testDart2Dart(src, expectedResult: ''' | |
126 should_be_kept() {} | |
127 main() { | |
128 should_be_kept(); | |
129 } | |
130 '''); | |
131 } | |
132 testTopLevelField() { | |
133 testDart2Dart(''' | |
134 final String x = "asd"; | |
135 main() { | |
136 x; | |
137 } | |
138 '''); | |
139 } | |
140 | |
141 testSimpleTopLevelClass() { | |
142 testDart2Dart(''' | |
143 main() { | |
144 new A(); | |
145 } | |
146 class A { | |
147 A() {} | |
148 } | |
149 '''); | |
150 } | |
151 | |
152 testClassWithSynthesizedConstructor() { | |
153 testDart2Dart(''' | |
154 main() { | |
155 new A(); | |
156 } | |
157 class A {} | |
158 '''); | |
159 } | |
160 | |
161 testClassWithMethod() { | |
162 testDart2Dart(r''' | |
163 main() { | |
164 var a = new A(); | |
165 a.foo(); | |
166 } | |
167 class A { | |
168 void foo() {} | |
169 } | |
170 '''); | |
171 } | |
172 | |
173 testExtendsImplements() { | |
174 testDart2Dart(''' | |
175 main() { | |
176 new B<Object>(); | |
177 } | |
178 class A<T> {} | |
179 class B<T> extends A<T> {} | |
180 '''); | |
181 } | |
182 | |
183 testVariableDefinitions() { | |
184 testDart2Dart(''' | |
185 main() { | |
186 var x, y; | |
187 final String s = null; | |
188 } | |
189 '''); | |
190 testDart2Dart(''' | |
191 main() { | |
192 final int x = 0, y = 0; | |
193 final String s = null; | |
194 } | |
195 '''); | |
196 testDart2Dart(''' | |
197 foo(f, g) {} | |
198 main() { | |
199 foo(1, 2); | |
200 } | |
201 '''); | |
202 testDart2Dart(''' | |
203 foo(f(arg)) {} | |
204 main() { | |
205 foo(main); | |
206 } | |
207 '''); | |
208 // A couple of static/finals inside a class. | |
209 testDart2Dart(''' | |
210 main() { | |
211 A.a; | |
212 A.b; | |
213 } | |
214 class A { | |
215 static const String a = "5"; | |
216 static const String b = "4"; | |
217 } | |
218 '''); | |
219 // Class member of typedef-ed function type. | |
220 // Maybe typedef should be included in the result too, but it | |
221 // works fine without it. | |
222 testDart2Dart(''' | |
223 typedef void foofunc(_0); | |
224 main() { | |
225 new A((arg) {}); | |
226 } | |
227 class A { | |
228 A(foofunc this.handler); | |
229 final foofunc handler; | |
230 } | |
231 '''); | |
232 } | |
233 | |
234 testGetSet() { | |
235 // Top-level get/set. | |
236 testDart2Dart(''' | |
237 set foo(arg) {} | |
238 get foo { | |
239 return 5; | |
240 } | |
241 main() { | |
242 foo; | |
243 foo = 5; | |
244 } | |
245 '''); | |
246 // Field get/set. | |
247 testDart2Dart(''' | |
248 main() { | |
249 var a = new A(); | |
250 a.foo; | |
251 a.foo = 5; | |
252 } | |
253 class A { | |
254 set foo(a) {} | |
255 get foo { | |
256 return 5; | |
257 } | |
258 } | |
259 '''); | |
260 // Typed get/set. | |
261 testDart2Dart(''' | |
262 String get foo { | |
263 return "a"; | |
264 } | |
265 main() { | |
266 foo; | |
267 } | |
268 '''); | |
269 } | |
270 | |
271 testAbstractClass() { | |
272 testDart2Dart(''' | |
273 main() { | |
274 A.foo; | |
275 } | |
276 abstract class A { | |
277 static final num foo = 0; | |
278 } | |
279 '''); | |
280 } | |
281 | |
282 testConflictSendsRename() { | |
283 // Various Send-s to current library and external library. Verify that | |
284 // everything is renamed correctly in conflicting class names and global | |
285 // functions. | |
286 var librarySrc = ''' | |
287 library mylib; | |
288 | |
289 globalfoo() {} | |
290 var globalVar; | |
291 var globalVarInitialized = 6, globalVarInitialized2 = 7; | |
292 | |
293 class A { | |
294 A(){} | |
295 A.fromFoo(){} | |
296 static staticfoo(){} | |
297 foo(){} | |
298 static const field = 5; | |
299 } | |
300 '''; | |
301 var mainSrc = ''' | |
302 import 'mylib.dart' as mylib; | |
303 | |
304 globalfoo() {} | |
305 var globalVar; | |
306 var globalVarInitialized = 6, globalVarInitialized2 = 7; | |
307 | |
308 class A { | |
309 A(){} | |
310 A.fromFoo(){} | |
311 static staticfoo(){} | |
312 foo(){} | |
313 static const field = 5; | |
314 } | |
315 | |
316 main() { | |
317 globalVar; | |
318 globalVarInitialized; | |
319 globalVarInitialized2; | |
320 globalfoo(); | |
321 A.field; | |
322 A.staticfoo(); | |
323 new A(); | |
324 new A.fromFoo(); | |
325 new A().foo(); | |
326 | |
327 mylib.globalVar; | |
328 mylib.globalVarInitialized; | |
329 mylib.globalVarInitialized2; | |
330 mylib.globalfoo(); | |
331 mylib.A.field; | |
332 mylib.A.staticfoo(); | |
333 new mylib.A(); | |
334 new mylib.A.fromFoo(); | |
335 new mylib.A().foo(); | |
336 } | |
337 '''; | |
338 var expectedResult = ''' | |
339 globalfoo() {} | |
340 var globalVar; | |
341 var globalVarInitialized = 6; | |
342 var globalVarInitialized2 = 7; | |
343 class A { | |
344 A() {} | |
345 A.fromFoo() {} | |
346 static staticfoo() {} | |
347 foo() {} | |
348 static const field = 5; | |
349 } | |
350 globalfoo_A() {} | |
351 var globalVar_A; | |
352 var globalVarInitialized_A = 6; | |
353 var globalVarInitialized2_A = 7; | |
354 class A_A { | |
355 A_A() {} | |
356 A_A.fromFoo_A() {} | |
357 static staticfoo_A() {} | |
358 foo() {} | |
359 static const field_A = 5; | |
360 } | |
361 main() { | |
362 globalVar_A; | |
363 globalVarInitialized_A; | |
364 globalVarInitialized2_A; | |
365 globalfoo_A(); | |
366 A_A.field_A; | |
367 A_A.staticfoo_A(); | |
368 new A_A(); | |
369 new A_A.fromFoo_A(); | |
370 new A_A().foo(); | |
371 globalVar; | |
372 globalVarInitialized; | |
373 globalVarInitialized2; | |
374 globalfoo(); | |
375 A.field; | |
376 A.staticfoo(); | |
377 new A(); | |
378 new A.fromFoo(); | |
379 new A().foo(); | |
380 } | |
381 '''; | |
382 testDart2Dart(mainSrc, librarySrc: librarySrc, | |
383 expectedResult: expectedResult); | |
384 } | |
385 | |
386 testNoConflictSendsRename() { | |
387 // Various Send-s to current library and external library. Nothing should be | |
388 // renamed here, only library prefixes must be cut. | |
389 var librarySrc = ''' | |
390 library mylib; | |
391 | |
392 globalfoo() {} | |
393 | |
394 class A { | |
395 A(){} | |
396 A.fromFoo(){} | |
397 static staticfoo(){} | |
398 foo(){} | |
399 static const field = 5; | |
400 } | |
401 '''; | |
402 var mainSrc = ''' | |
403 import 'mylib.dart' as mylib; | |
404 | |
405 myglobalfoo() {} | |
406 | |
407 class MyA { | |
408 MyA(){} | |
409 MyA.myfromFoo(){} | |
410 static mystaticfoo(){} | |
411 myfoo(){} | |
412 static const myfield = 5; | |
413 } | |
414 | |
415 main() { | |
416 myglobalfoo(); | |
417 MyA.myfield; | |
418 MyA.mystaticfoo(); | |
419 new MyA(); | |
420 new MyA.myfromFoo(); | |
421 new MyA().myfoo(); | |
422 | |
423 mylib.globalfoo(); | |
424 mylib.A.field; | |
425 mylib.A.staticfoo(); | |
426 new mylib.A(); | |
427 new mylib.A.fromFoo(); | |
428 new mylib.A().foo(); | |
429 } | |
430 '''; | |
431 var expectedResult = ''' | |
432 globalfoo() {} | |
433 class A { | |
434 A() {} | |
435 A.fromFoo() {} | |
436 static staticfoo() {} | |
437 foo() {} | |
438 static const field = 5; | |
439 } | |
440 myglobalfoo() {} | |
441 class MyA { | |
442 MyA() {} | |
443 MyA.myfromFoo() {} | |
444 static mystaticfoo() {} | |
445 myfoo() {} | |
446 static const myfield = 5; | |
447 } | |
448 main() { | |
449 myglobalfoo(); | |
450 MyA.myfield; | |
451 MyA.mystaticfoo(); | |
452 new MyA(); | |
453 new MyA.myfromFoo(); | |
454 new MyA().myfoo(); | |
455 globalfoo(); | |
456 A.field; | |
457 A.staticfoo(); | |
458 new A(); | |
459 new A.fromFoo(); | |
460 new A().foo(); | |
461 } | |
462 '''; | |
463 testDart2Dart(mainSrc, librarySrc: librarySrc, | |
464 expectedResult: expectedResult); | |
465 } | |
466 | |
467 testConflictLibraryClassRename() { | |
468 var librarySrc = ''' | |
469 library mylib; | |
470 | |
471 topfoo() {} | |
472 | |
473 class A { | |
474 foo() {} | |
475 } | |
476 '''; | |
477 var mainSrc = ''' | |
478 import 'mylib.dart' as mylib; | |
479 topfoo() { | |
480 var x = 5; | |
481 } | |
482 class A { | |
483 num foo() {} | |
484 A.fromFoo() {} | |
485 mylib.A myliba; | |
486 List<A> mylist; | |
487 } | |
488 mylib.A getA() => null; | |
489 main() { | |
490 var a = new mylib.A(); | |
491 a.foo(); | |
492 var b = new A.fromFoo(); | |
493 b.foo(); | |
494 var GREATVAR = b.myliba; | |
495 b.mylist; | |
496 a = getA(); | |
497 topfoo(); | |
498 mylib.topfoo(); | |
499 } | |
500 '''; | |
501 var expectedResult = ''' | |
502 topfoo() {} | |
503 class A { | |
504 foo() {} | |
505 } | |
506 topfoo_A() { | |
507 var x = 5; | |
508 } | |
509 class A_A { | |
510 num foo() {} | |
511 A_A.fromFoo() {} | |
512 A myliba; | |
513 List<A_A> mylist; | |
514 } | |
515 A getA() => null; | |
516 main() { | |
517 var a = new A(); | |
518 a.foo(); | |
519 var b = new A_A.fromFoo(); | |
520 b.foo(); | |
521 var GREATVAR = b.myliba; | |
522 b.mylist; | |
523 a = getA(); | |
524 topfoo_A(); | |
525 topfoo(); | |
526 } | |
527 '''; | |
528 testDart2Dart(mainSrc, librarySrc: librarySrc, | |
529 expectedResult: expectedResult); | |
530 } | |
531 | |
532 testClassExtendsWithArgs() { | |
533 testDart2Dart(''' | |
534 main() { | |
535 new B<Object>(); | |
536 } | |
537 class A<T extends Object> {} | |
538 class B<T extends Object> extends A<T> {} | |
539 ''', expectedResult: ''' | |
540 main() { | |
541 new B<Object>(); | |
542 } | |
543 class A<T> {} | |
544 class B<T> extends A<T> {} | |
545 '''); | |
546 } | |
547 | |
548 testStaticInvocation() { | |
549 testDart2Dart(''' | |
550 main() { | |
551 var x = double.parseDouble("1"); | |
552 } | |
553 '''); | |
554 } | |
555 | |
556 testLibraryGetSet() { | |
557 var librarySrc = ''' | |
558 library mylib; | |
559 | |
560 get topgetset => 5; | |
561 set topgetset(arg) {} | |
562 '''; | |
563 var mainSrc = ''' | |
564 import 'mylib.dart' as mylib; | |
565 | |
566 get topgetset => 6; | |
567 set topgetset(arg) {} | |
568 | |
569 main() { | |
570 topgetset; | |
571 topgetset = 6; | |
572 | |
573 mylib.topgetset; | |
574 mylib.topgetset = 5; | |
575 } | |
576 '''; | |
577 var expectedResult = ''' | |
578 get topgetset => 5; | |
579 set topgetset(arg) {} | |
580 get topgetset_A => 6; | |
581 set topgetset_A(arg) {} | |
582 main() { | |
583 topgetset_A; | |
584 topgetset_A = 6; | |
585 topgetset; | |
586 topgetset = 5; | |
587 } | |
588 '''; | |
589 testDart2Dart(mainSrc, librarySrc: librarySrc, | |
590 expectedResult: expectedResult); | |
591 } | |
592 | |
593 testFieldTypeOutput() { | |
594 testDart2Dart(''' | |
595 main() { | |
596 new A().field; | |
597 } | |
598 class B {} | |
599 class A { | |
600 B field; | |
601 } | |
602 '''); | |
603 } | |
604 | |
605 class DynoMap implements Map<Element, ElementAst> { | |
606 final compiler; | |
607 DynoMap(this.compiler); | |
608 | |
609 ElementAst operator[](AstElement element) { | |
610 return new ElementAst(element.resolvedAst.node, | |
611 element.resolvedAst.elements); | |
612 } | |
613 | |
614 noSuchMethod(Invocation invocation) => throw 'unimplemented method'; | |
615 } | |
616 | |
617 PlaceholderCollector collectPlaceholders(compiler, element) { | |
618 DartBackend backend = compiler.backend; | |
619 return new PlaceholderCollector( | |
620 compiler.reporter, | |
621 backend.mirrorRenamer, | |
622 new Set<String>(), | |
623 new DynoMap(compiler), | |
624 compiler.mainFunction) | |
625 ..collect(element); | |
626 } | |
627 | |
628 testLocalFunctionPlaceholder() { | |
629 var src = ''' | |
630 main() { | |
631 function localfoo() {} | |
632 localfoo(); | |
633 } | |
634 '''; | |
635 MockCompiler compiler = new MockCompiler.internal(emitJavaScript: false); | |
636 asyncTest(() => compiler.init().then((_) { | |
637 assert(compiler.backend is DartBackend); | |
638 compiler.parseScript(src); | |
639 FunctionElement mainElement = compiler.mainApp.find(Identifiers.main); | |
640 compiler.processQueue(compiler.enqueuer.resolution, mainElement); | |
641 PlaceholderCollector collector = collectPlaceholders(compiler, mainElement); | |
642 FunctionExpression mainNode = mainElement.node; | |
643 Block body = mainNode.body; | |
644 FunctionDeclaration functionDeclaration = body.statements.nodes.head; | |
645 FunctionExpression fooNode = functionDeclaration.function; | |
646 LocalPlaceholder fooPlaceholder = | |
647 collector.functionScopes[mainElement].localPlaceholders.first; | |
648 Expect.isTrue(fooPlaceholder.nodes.contains(fooNode.name)); | |
649 })); | |
650 } | |
651 | |
652 testTypeVariablesAreRenamed() { | |
653 // Somewhat a hack: we require all the references of the identifier | |
654 // to be renamed in the same way for the whole library. Hence | |
655 // if we have a class and type variable with the same name, they | |
656 // both should be renamed. | |
657 var librarySrc = ''' | |
658 library mylib; | |
659 typedef void MyFunction<T extends num>(T arg); | |
660 class T {} | |
661 class B<T> {} | |
662 class A<T> extends B<T> { T f; } | |
663 '''; | |
664 var mainSrc = ''' | |
665 import 'mylib.dart' as mylib; | |
666 typedef void MyFunction<T extends num>(T arg); | |
667 class T {} | |
668 class B<T> {} | |
669 class A<T> extends B<T> { T f; } | |
670 | |
671 main() { | |
672 MyFunction myf1; | |
673 mylib.MyFunction myf2; | |
674 new A<int>().f; | |
675 new T(); | |
676 | |
677 new mylib.A<int>().f; | |
678 new mylib.T(); | |
679 } | |
680 '''; | |
681 var expectedResult = ''' | |
682 typedef void MyFunction<T_B extends num>(T_B _0); | |
683 class T {} | |
684 class B<T_B> {} | |
685 class A<T_B> extends B<T_B> { | |
686 T_B f; | |
687 } | |
688 typedef void MyFunction_A<T_B extends num>(T_B _0); | |
689 class T_A {} | |
690 class B_A<T_B> {} | |
691 class A_A<T_B> extends B_A<T_B> { | |
692 T_B f; | |
693 } | |
694 main() { | |
695 MyFunction_A myf1; | |
696 MyFunction myf2; | |
697 new A_A<int>().f; | |
698 new T_A(); | |
699 new A<int>().f; | |
700 new T(); | |
701 } | |
702 '''; | |
703 testDart2Dart(mainSrc, librarySrc: librarySrc, | |
704 expectedResult: expectedResult); | |
705 } | |
706 | |
707 testClassTypeArgumentBound() { | |
708 var librarySrc = ''' | |
709 library mylib; | |
710 | |
711 class I {} | |
712 class A<T extends I> {} | |
713 | |
714 '''; | |
715 var mainSrc = ''' | |
716 import 'mylib.dart' as mylib; | |
717 | |
718 class I {} | |
719 class A<T extends I> {} | |
720 | |
721 main() { | |
722 new A(); | |
723 new mylib.A(); | |
724 } | |
725 '''; | |
726 var expectedResult = ''' | |
727 class I {} | |
728 class A<T extends I> {} | |
729 class I_A {} | |
730 class A_A<T extends I_A> {} | |
731 main() { | |
732 new A_A(); | |
733 new A(); | |
734 } | |
735 '''; | |
736 testDart2Dart(mainSrc, librarySrc: librarySrc, | |
737 expectedResult: expectedResult); | |
738 } | |
739 | |
740 testDoubleMains() { | |
741 var librarySrc = ''' | |
742 library mylib; | |
743 main() {} | |
744 '''; | |
745 var mainSrc = ''' | |
746 import 'mylib.dart' as mylib; | |
747 main() { | |
748 mylib.main(); | |
749 } | |
750 '''; | |
751 var expectedResult = ''' | |
752 main_A() {} | |
753 main() { | |
754 main_A(); | |
755 } | |
756 '''; | |
757 testDart2Dart(mainSrc, librarySrc: librarySrc, | |
758 expectedResult: expectedResult); | |
759 } | |
760 | |
761 testStaticAccessIoLib() { | |
762 var src = ''' | |
763 import 'dart:io'; | |
764 | |
765 main() { | |
766 Platform.operatingSystem; | |
767 } | |
768 '''; | |
769 var expectedResult = ''' | |
770 import "dart:io"; | |
771 main() { | |
772 Platform.operatingSystem; | |
773 } | |
774 '''; | |
775 testDart2Dart(src, expectedResult: expectedResult); | |
776 } | |
777 | |
778 testMinification() { | |
779 var src = ''' | |
780 class ClassWithVeryVeryLongName {} | |
781 main() { | |
782 new ClassWithVeryVeryLongName(); | |
783 } | |
784 '''; | |
785 var expectedResult = | |
786 'class A{}' | |
787 'main(){new A();}'; | |
788 testDart2Dart(src, expectedResult: expectedResult, minify: true); | |
789 } | |
790 | |
791 testClosureLocalsMinified() { | |
792 var src = ''' | |
793 main() { | |
794 var a = 7; | |
795 void foo1(a,b) { | |
796 void foo2(c,d) { | |
797 var E = a; | |
798 } | |
799 foo2(b, a); | |
800 } | |
801 foo1(a, 8); | |
802 } | |
803 '''; | |
804 var expectedResult = | |
805 'main(){var A=7; B(A,C){ D(E,F){var G=A;}D(C,A);}B(A,8);}'; | |
806 testDart2Dart(src, expectedResult: expectedResult, minify: true); | |
807 } | |
808 | |
809 testParametersMinified() { | |
810 var src = ''' | |
811 class A { | |
812 var a; | |
813 static foo(arg1) { | |
814 // Should not rename arg1 to a. | |
815 arg1 = 5; | |
816 } | |
817 } | |
818 | |
819 fooglobal(arg,{optionalarg: 7}) { | |
820 arg = 6; | |
821 } | |
822 | |
823 main() { | |
824 new A().a; | |
825 A.foo(8); | |
826 fooglobal(8); | |
827 } | |
828 '''; | |
829 var expectedResult = | |
830 'class B{var E;static C(A){A=5;}}D(A,{optionalarg: 7}){A=6;}' | |
831 'main(){new B().E;B.C(8);D(8);}'; | |
832 testDart2Dart(src, expectedResult: expectedResult, minify: true); | |
833 } | |
834 | |
835 testDeclarationTypePlaceholders() { | |
836 var src = ''' | |
837 String globalfield; | |
838 const String globalconstfield; | |
839 | |
840 void foo(String arg) {} | |
841 | |
842 main() { | |
843 String localvar; | |
844 foo("5"); | |
845 } | |
846 '''; | |
847 var expectedResult = ''' | |
848 foo( arg) {} | |
849 main() { | |
850 var localvar; | |
851 foo("5"); | |
852 } | |
853 '''; | |
854 testDart2Dart(src, expectedResult: expectedResult, stripTypes: true); | |
855 } | |
856 | |
857 testPlatformLibraryMemberNamesAreFixed() { | |
858 var src = ''' | |
859 import 'dart:html'; | |
860 | |
861 class A { | |
862 static String get userAgent => window.navigator.userAgent; | |
863 } | |
864 | |
865 main() { | |
866 A.userAgent; | |
867 } | |
868 '''; | |
869 var expectedResult = ''' | |
870 import "dart:html"; | |
871 class A { | |
872 static String get userAgent_A => window.navigator.userAgent; | |
873 } | |
874 main() { | |
875 A.userAgent_A; | |
876 } | |
877 '''; | |
878 testDart2Dart(src, expectedResult: expectedResult); | |
879 } | |
880 | |
881 testConflictsWithCoreLib() { | |
882 var src = ''' | |
883 import 'dart:core' as fisk; | |
884 | |
885 print(x) { throw 'fisk'; } | |
886 | |
887 main() { | |
888 fisk.print('corelib'); | |
889 print('local'); | |
890 } | |
891 '''; | |
892 var expectedResult = """ | |
893 print_A(x) { | |
894 throw 'fisk'; | |
895 } | |
896 main() { | |
897 print('corelib'); | |
898 print_A('local'); | |
899 } | |
900 """; | |
901 testDart2Dart(src, expectedResult: expectedResult); | |
902 } | |
903 | |
904 testUnresolvedNamedConstructor1() { | |
905 var src = ''' | |
906 class A { | |
907 } | |
908 | |
909 main() { | |
910 new A.named(); | |
911 } | |
912 '''; | |
913 var expectedResult = """ | |
914 main() { | |
915 new Unresolved(); | |
916 } | |
917 """; | |
918 testDart2Dart(src, expectedResult: expectedResult); | |
919 } | |
920 | |
921 testUnresolvedNamedConstructor2() { | |
922 var src = ''' | |
923 class A { | |
924 A() {} | |
925 } | |
926 | |
927 main() { | |
928 new A(); | |
929 new A.named(); | |
930 } | |
931 '''; | |
932 var expectedResult = """ | |
933 class A { | |
934 A() {} | |
935 } | |
936 main() { | |
937 new A(); | |
938 new Unresolved(); | |
939 } | |
940 """; | |
941 testDart2Dart(src, expectedResult: expectedResult); | |
942 } | |
943 | |
944 testUnresolvedNamedConstructor3() { | |
945 var src = ''' | |
946 class A { | |
947 static method() {} | |
948 } | |
949 | |
950 main() { | |
951 A.method(); | |
952 new A.named(); | |
953 } | |
954 '''; | |
955 var expectedResult = """ | |
956 class A { | |
957 static method() {} | |
958 } | |
959 main() { | |
960 A.method(); | |
961 new Unresolved(); | |
962 } | |
963 """; | |
964 testDart2Dart(src, expectedResult: expectedResult); | |
965 } | |
966 | |
967 testClassAndNamedMixinDeclarations() { | |
968 test(String declarations, {String expectedDeclarations}) { | |
969 const String mainSource = 'main() => new A();'; | |
970 if (expectedDeclarations == null) { | |
971 expectedDeclarations = declarations; | |
972 } | |
973 testDart2Dart('$declarations\n$mainSource\n', | |
974 expectedResult: '$expectedDeclarations\n$mainSource\n'); | |
975 } | |
976 | |
977 test('class A {}'); | |
978 test('class A<T> {}'); | |
979 test('class A<T extends num> {}'); | |
980 test('class A<T extends Object> {}', expectedDeclarations: 'class A<T> {}'); | |
981 test('class A extends Object {}', expectedDeclarations: 'class A {}'); | |
982 | |
983 test(''' | |
984 class S1 {} | |
985 class A extends S1 {}'''); | |
986 | |
987 test(''' | |
988 class S1 {} | |
989 class A implements S1 {}'''); | |
990 | |
991 test(''' | |
992 class S1 {} | |
993 class S2 {} | |
994 class A extends S1 implements S2 {}'''); | |
995 | |
996 test(''' | |
997 class S1 {} | |
998 class S2 {} | |
999 class S3 {} | |
1000 class A extends S1 implements S2, S3 {}'''); | |
1001 | |
1002 test(''' | |
1003 class S1 {} | |
1004 class S2 {} | |
1005 class A implements S1, S2 {}'''); | |
1006 | |
1007 test(''' | |
1008 class S1 {} | |
1009 class S2 {} | |
1010 class A extends Object implements S1, S2 {}''', | |
1011 expectedDeclarations: ''' | |
1012 class S1 {} | |
1013 class S2 {} | |
1014 class A implements S1, S2 {}'''); | |
1015 | |
1016 test(''' | |
1017 class S1 {} | |
1018 class A extends Object with S1 {}'''); | |
1019 | |
1020 test(''' | |
1021 class S1 {} | |
1022 class A = Object with S1;'''); | |
1023 | |
1024 test(''' | |
1025 class S1 {} | |
1026 class S2 {} | |
1027 class A extends S1 with S2 {}'''); | |
1028 | |
1029 test(''' | |
1030 class S1 {} | |
1031 class S2 {} | |
1032 class A = S1 with S2;'''); | |
1033 | |
1034 test(''' | |
1035 class S1 {} | |
1036 class S2 {} | |
1037 class S3 {} | |
1038 class A extends S1 with S2, S3 {}'''); | |
1039 | |
1040 test(''' | |
1041 class S1 {} | |
1042 class S2 {} | |
1043 class S3 {} | |
1044 class A = S1 with S2, S3;'''); | |
1045 | |
1046 test(''' | |
1047 class S1 {} | |
1048 class S2 {} | |
1049 class S3 {} | |
1050 class S4 {} | |
1051 class S5 {} | |
1052 class A extends S1 with S2, S3 implements S4, S5 {}'''); | |
1053 | |
1054 test(''' | |
1055 class S1 {} | |
1056 class S2 {} | |
1057 class S3 {} | |
1058 class S4 {} | |
1059 class S5 {} | |
1060 class A = S1 with S2, S3 implements S4, S5;'''); | |
1061 | |
1062 test(''' | |
1063 class S1 {} | |
1064 class A extends Object with S1 implements S1 {}''', | |
1065 expectedDeclarations: ''' | |
1066 class S1 {} | |
1067 class A extends Object with S1 {}'''); | |
1068 | |
1069 test(''' | |
1070 class S1 {} | |
1071 class A = Object with S1 implements S1;''', | |
1072 expectedDeclarations: ''' | |
1073 class S1 {} | |
1074 class A = Object with S1;'''); | |
1075 | |
1076 test(''' | |
1077 class S1<T1> {} | |
1078 class S2<T2> {} | |
1079 class S3<T3> {} | |
1080 class S4<T4> {} | |
1081 class S5<T5, T6> {} | |
1082 class A<U1, U2, U3, U4, U5> extends S1<U1> with S2<U2>, S3<U3> ''' | |
1083 '''implements S4<U4>, S5<U5, S5<U5, int>> {}'''); | |
1084 } | |
1085 | |
1086 main() { | |
1087 testSimpleFileUnparse(); | |
1088 testTopLevelField(); | |
1089 testSimpleTopLevelClass(); | |
1090 testClassWithSynthesizedConstructor(); | |
1091 testClassWithMethod(); | |
1092 testExtendsImplements(); | |
1093 testVariableDefinitions(); | |
1094 testGetSet(); | |
1095 testAbstractClass(); | |
1096 testConflictSendsRename(); | |
1097 testNoConflictSendsRename(); | |
1098 testConflictLibraryClassRename(); | |
1099 testClassExtendsWithArgs(); | |
1100 testStaticInvocation(); | |
1101 testLibraryGetSet(); | |
1102 testFieldTypeOutput(); | |
1103 testTypeVariablesAreRenamed(); | |
1104 testClassTypeArgumentBound(); | |
1105 testDoubleMains(); | |
1106 testStaticAccessIoLib(); | |
1107 testLocalFunctionPlaceholder(); | |
1108 testMinification(); | |
1109 testClosureLocalsMinified(); | |
1110 testParametersMinified(); | |
1111 testDeclarationTypePlaceholders(); | |
1112 testPlatformLibraryMemberNamesAreFixed(); | |
1113 testConflictsWithCoreLib(); | |
1114 testUnresolvedNamedConstructor1(); | |
1115 testUnresolvedNamedConstructor2(); | |
1116 testUnresolvedNamedConstructor3(); | |
1117 testClassAndNamedMixinDeclarations(); | |
1118 } | |
1119 | |
OLD | NEW |