| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// General type checking tests | 5 /// General type checking tests |
| 6 library dev_compiler.test.checker_test; | 6 library dev_compiler.test.checker_test; |
| 7 | 7 |
| 8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 | 9 |
| 10 import '../testing.dart'; | 10 import '../testing.dart'; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 // Check the boolean conversion of the condition. | 45 // Check the boolean conversion of the condition. |
| 46 print((/*severe:StaticTypeError*/i) ? false : true); | 46 print((/*severe:StaticTypeError*/i) ? false : true); |
| 47 print((/*warning:DownCastImplicit*/obj) ? false : true); | 47 print((/*warning:DownCastImplicit*/obj) ? false : true); |
| 48 print((/*info:DynamicCast*/dyn) ? false : true); | 48 print((/*info:DynamicCast*/dyn) ? false : true); |
| 49 } | 49 } |
| 50 ''' | 50 ''' |
| 51 }); | 51 }); |
| 52 }); | 52 }); |
| 53 | 53 |
| 54 test('if/for/do/while statements use boolean conversion', () => testChecker({ | 54 test( |
| 55 '/main.dart': ''' | 55 'if/for/do/while statements use boolean conversion', |
| 56 () => testChecker({ |
| 57 '/main.dart': ''' |
| 56 main() { | 58 main() { |
| 57 dynamic d = 42; | 59 dynamic d = 42; |
| 58 Object obj = 42; | 60 Object obj = 42; |
| 59 int i = 42; | 61 int i = 42; |
| 60 bool b = false; | 62 bool b = false; |
| 61 | 63 |
| 62 if (b) {} | 64 if (b) {} |
| 63 if (/*info:DynamicCast*/dyn) {} | 65 if (/*info:DynamicCast*/dyn) {} |
| 64 if (/*warning:DownCastImplicit*/obj) {} | 66 if (/*warning:DownCastImplicit*/obj) {} |
| 65 if (/*severe:StaticTypeError*/i) {} | 67 if (/*severe:StaticTypeError*/i) {} |
| 66 | 68 |
| 67 while (b) {} | 69 while (b) {} |
| 68 while (/*info:DynamicCast*/dyn) {} | 70 while (/*info:DynamicCast*/dyn) {} |
| 69 while (/*warning:DownCastImplicit*/obj) {} | 71 while (/*warning:DownCastImplicit*/obj) {} |
| 70 while (/*severe:StaticTypeError*/i) {} | 72 while (/*severe:StaticTypeError*/i) {} |
| 71 | 73 |
| 72 do {} while (b); | 74 do {} while (b); |
| 73 do {} while (/*info:DynamicCast*/dyn); | 75 do {} while (/*info:DynamicCast*/dyn); |
| 74 do {} while (/*warning:DownCastImplicit*/obj); | 76 do {} while (/*warning:DownCastImplicit*/obj); |
| 75 do {} while (/*severe:StaticTypeError*/i); | 77 do {} while (/*severe:StaticTypeError*/i); |
| 76 | 78 |
| 77 for (;b;) {} | 79 for (;b;) {} |
| 78 for (;/*info:DynamicCast*/dyn;) {} | 80 for (;/*info:DynamicCast*/dyn;) {} |
| 79 for (;/*warning:DownCastImplicit*/obj;) {} | 81 for (;/*warning:DownCastImplicit*/obj;) {} |
| 80 for (;/*severe:StaticTypeError*/i;) {} | 82 for (;/*severe:StaticTypeError*/i;) {} |
| 81 } | 83 } |
| 82 ''' | 84 ''' |
| 83 })); | 85 })); |
| 84 | 86 |
| 85 test('dynamic invocation', () { | 87 test('dynamic invocation', () { |
| 86 testChecker({ | 88 testChecker({ |
| 87 '/main.dart': ''' | 89 '/main.dart': ''' |
| 88 | 90 |
| 89 class A { | 91 class A { |
| 90 dynamic call(dynamic x) => x; | 92 dynamic call(dynamic x) => x; |
| 91 } | 93 } |
| 92 class B extends A { | 94 class B extends A { |
| 93 int call(int x) => x; | 95 int call(int x) => x; |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 // num is nullable | 258 // num is nullable |
| 257 z = null; | 259 z = null; |
| 258 z = x; | 260 z = x; |
| 259 z = y; | 261 z = y; |
| 260 | 262 |
| 261 // bool is nullable | 263 // bool is nullable |
| 262 b = null; | 264 b = null; |
| 263 b = true; | 265 b = true; |
| 264 } | 266 } |
| 265 ''' | 267 ''' |
| 266 }, nonnullableTypes: <String>['int', 'double']); | 268 }, nonnullableTypes: <String>[ |
| 269 'int', |
| 270 'double' |
| 271 ]); |
| 267 }); | 272 }); |
| 268 | 273 |
| 269 test('Primitives and generics', () { | 274 test('Primitives and generics', () { |
| 270 testChecker({ | 275 testChecker({ |
| 271 '/main.dart': ''' | 276 '/main.dart': ''' |
| 272 class A<T> { | 277 class A<T> { |
| 273 // TODO(vsm): This needs a static info indicating a runtime | 278 // TODO(vsm): This needs a static info indicating a runtime |
| 274 // check at construction. | 279 // check at construction. |
| 275 T x; | 280 T x; |
| 276 | 281 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 298 | 303 |
| 299 class C<T extends num> { | 304 class C<T extends num> { |
| 300 // TODO(vsm): This needs a static info indicating a runtime | 305 // TODO(vsm): This needs a static info indicating a runtime |
| 301 // check at construction. | 306 // check at construction. |
| 302 T x; | 307 T x; |
| 303 | 308 |
| 304 // TODO(vsm): Should this be a different type of DownCast? | 309 // TODO(vsm): Should this be a different type of DownCast? |
| 305 T foo() => /*warning:DownCastImplicit*/null; | 310 T foo() => /*warning:DownCastImplicit*/null; |
| 306 } | 311 } |
| 307 ''' | 312 ''' |
| 308 }, nonnullableTypes: <String>['int', 'double']); | 313 }, nonnullableTypes: <String>[ |
| 314 'int', |
| 315 'double' |
| 316 ]); |
| 309 }); | 317 }); |
| 310 | 318 |
| 311 test('Constructors', () { | 319 test('Constructors', () { |
| 312 testChecker({ | 320 testChecker({ |
| 313 '/main.dart': ''' | 321 '/main.dart': ''' |
| 314 const num z = 25; | 322 const num z = 25; |
| 315 Object obj = "world"; | 323 Object obj = "world"; |
| 316 | 324 |
| 317 class A { | 325 class A { |
| 318 int x; | 326 int x; |
| (...skipping 1269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1588 /*severe:InvalidMethodOverride*/C m2(C value) {} | 1596 /*severe:InvalidMethodOverride*/C m2(C value) {} |
| 1589 /*severe:InvalidMethodOverride*/A m3(C value) {} | 1597 /*severe:InvalidMethodOverride*/A m3(C value) {} |
| 1590 C m4(A value) {} | 1598 C m4(A value) {} |
| 1591 /*severe:InvalidMethodOverride*/m5(value) {} | 1599 /*severe:InvalidMethodOverride*/m5(value) {} |
| 1592 /*severe:InvalidMethodOverride*/dynamic m6(dynamic value) {} | 1600 /*severe:InvalidMethodOverride*/dynamic m6(dynamic value) {} |
| 1593 } | 1601 } |
| 1594 ''' | 1602 ''' |
| 1595 }, inferFromOverrides: true); | 1603 }, inferFromOverrides: true); |
| 1596 }); | 1604 }); |
| 1597 | 1605 |
| 1598 test('unary operators', () => testChecker({ | 1606 test( |
| 1599 '/main.dart': ''' | 1607 'unary operators', |
| 1608 () => testChecker({ |
| 1609 '/main.dart': ''' |
| 1600 class A { | 1610 class A { |
| 1601 A operator ~() {} | 1611 A operator ~() {} |
| 1602 A operator +(int x) {} | 1612 A operator +(int x) {} |
| 1603 A operator -(int x) {} | 1613 A operator -(int x) {} |
| 1604 A operator -() {} | 1614 A operator -() {} |
| 1605 } | 1615 } |
| 1606 | 1616 |
| 1607 foo() => new A(); | 1617 foo() => new A(); |
| 1608 | 1618 |
| 1609 test() { | 1619 test() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1622 ++a; | 1632 ++a; |
| 1623 --a; | 1633 --a; |
| 1624 (/*info:DynamicInvoke*/++d); | 1634 (/*info:DynamicInvoke*/++d); |
| 1625 (/*info:DynamicInvoke*/--d); | 1635 (/*info:DynamicInvoke*/--d); |
| 1626 | 1636 |
| 1627 a++; | 1637 a++; |
| 1628 a--; | 1638 a--; |
| 1629 (/*info:DynamicInvoke*/d++); | 1639 (/*info:DynamicInvoke*/d++); |
| 1630 (/*info:DynamicInvoke*/d--); | 1640 (/*info:DynamicInvoke*/d--); |
| 1631 }''' | 1641 }''' |
| 1632 })); | 1642 })); |
| 1633 | 1643 |
| 1634 test('binary and index operators', () { | 1644 test('binary and index operators', () { |
| 1635 testChecker({ | 1645 testChecker({ |
| 1636 '/main.dart': ''' | 1646 '/main.dart': ''' |
| 1637 class A { | 1647 class A { |
| 1638 A operator *(B b) {} | 1648 A operator *(B b) {} |
| 1639 A operator /(B b) {} | 1649 A operator /(B b) {} |
| 1640 A operator ~/(B b) {} | 1650 A operator ~/(B b) {} |
| 1641 A operator %(B b) {} | 1651 A operator %(B b) {} |
| 1642 A operator +(B b) {} | 1652 A operator +(B b) {} |
| (...skipping 1010 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2653 f = bar as II2D; | 2663 f = bar as II2D; |
| 2654 f = bar as DD2I; | 2664 f = bar as DD2I; |
| 2655 f = bar as DI2D; | 2665 f = bar as DI2D; |
| 2656 f = bar as ID2D; | 2666 f = bar as ID2D; |
| 2657 f = bar as DD2D; | 2667 f = bar as DD2D; |
| 2658 } | 2668 } |
| 2659 ''' | 2669 ''' |
| 2660 }); | 2670 }); |
| 2661 }); | 2671 }); |
| 2662 | 2672 |
| 2663 test('custom URL mappings', () => testChecker({ | 2673 test( |
| 2664 '/main.dart': ''' | 2674 'custom URL mappings', |
| 2675 () => testChecker({ |
| 2676 '/main.dart': ''' |
| 2665 import 'dart:foobar' show Baz; | 2677 import 'dart:foobar' show Baz; |
| 2666 main() { | 2678 main() { |
| 2667 print(Baz.quux); | 2679 print(Baz.quux); |
| 2668 }''' | 2680 }''' |
| 2669 }, | 2681 }, customUrlMappings: { |
| 2670 customUrlMappings: { | 2682 'dart:foobar': '$testDirectory/checker/dart_foobar.dart' |
| 2671 'dart:foobar': '$testDirectory/checker/dart_foobar.dart' | 2683 })); |
| 2672 })); | |
| 2673 | 2684 |
| 2674 group('function modifiers', () { | 2685 group('function modifiers', () { |
| 2675 test('async', () => testChecker({ | 2686 test( |
| 2676 '/main.dart': ''' | 2687 'async', |
| 2688 () => testChecker({ |
| 2689 '/main.dart': ''' |
| 2677 import 'dart:async'; | 2690 import 'dart:async'; |
| 2678 import 'dart:math' show Random; | 2691 import 'dart:math' show Random; |
| 2679 | 2692 |
| 2680 dynamic x; | 2693 dynamic x; |
| 2681 | 2694 |
| 2682 foo1() async => x; | 2695 foo1() async => x; |
| 2683 Future foo2() async => x; | 2696 Future foo2() async => x; |
| 2684 Future<int> foo3() async => (/*info:DynamicCast*/x); | 2697 Future<int> foo3() async => (/*info:DynamicCast*/x); |
| 2685 Future<int> foo4() async => (/*severe:StaticTypeError*/new Future<int>.v
alue(/*info:DynamicCast*/x)); | 2698 Future<int> foo4() async => (/*severe:StaticTypeError*/new Future<int>.v
alue(/*info:DynamicCast*/x)); |
| 2686 | 2699 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 2701 | 2714 |
| 2702 Future<bool> get issue_264 async { | 2715 Future<bool> get issue_264 async { |
| 2703 await 42; | 2716 await 42; |
| 2704 if (new Random().nextBool()) { | 2717 if (new Random().nextBool()) { |
| 2705 return true; | 2718 return true; |
| 2706 } else { | 2719 } else { |
| 2707 return /*severe:StaticTypeError*/new Future<bool>.value(false); | 2720 return /*severe:StaticTypeError*/new Future<bool>.value(false); |
| 2708 } | 2721 } |
| 2709 } | 2722 } |
| 2710 ''' | 2723 ''' |
| 2711 })); | 2724 })); |
| 2712 | 2725 |
| 2713 test('async*', () => testChecker({ | 2726 test( |
| 2714 '/main.dart': ''' | 2727 'async*', |
| 2728 () => testChecker({ |
| 2729 '/main.dart': ''' |
| 2715 import 'dart:async'; | 2730 import 'dart:async'; |
| 2716 | 2731 |
| 2717 dynamic x; | 2732 dynamic x; |
| 2718 | 2733 |
| 2719 bar1() async* { yield x; } | 2734 bar1() async* { yield x; } |
| 2720 Stream bar2() async* { yield x; } | 2735 Stream bar2() async* { yield x; } |
| 2721 Stream<int> bar3() async* { yield (/*info:DynamicCast*/x); } | 2736 Stream<int> bar3() async* { yield (/*info:DynamicCast*/x); } |
| 2722 Stream<int> bar4() async* { yield (/*severe:StaticTypeError*/new Stream<
int>()); } | 2737 Stream<int> bar4() async* { yield (/*severe:StaticTypeError*/new Stream<
int>()); } |
| 2723 | 2738 |
| 2724 baz1() async* { yield* (/*info:DynamicCast*/x); } | 2739 baz1() async* { yield* (/*info:DynamicCast*/x); } |
| 2725 Stream baz2() async* { yield* (/*info:DynamicCast*/x); } | 2740 Stream baz2() async* { yield* (/*info:DynamicCast*/x); } |
| 2726 Stream<int> baz3() async* { yield* (/*warning:DownCastComposite*/x); } | 2741 Stream<int> baz3() async* { yield* (/*warning:DownCastComposite*/x); } |
| 2727 Stream<int> baz4() async* { yield* new Stream<int>(); } | 2742 Stream<int> baz4() async* { yield* new Stream<int>(); } |
| 2728 Stream<int> baz5() async* { yield* (/*info:InferredTypeAllocation*/new S
tream()); } | 2743 Stream<int> baz5() async* { yield* (/*info:InferredTypeAllocation*/new S
tream()); } |
| 2729 ''' | 2744 ''' |
| 2730 })); | 2745 })); |
| 2731 | 2746 |
| 2732 test('sync*', () => testChecker({ | 2747 test( |
| 2733 '/main.dart': ''' | 2748 'sync*', |
| 2749 () => testChecker({ |
| 2750 '/main.dart': ''' |
| 2734 import 'dart:async'; | 2751 import 'dart:async'; |
| 2735 | 2752 |
| 2736 dynamic x; | 2753 dynamic x; |
| 2737 | 2754 |
| 2738 bar1() sync* { yield x; } | 2755 bar1() sync* { yield x; } |
| 2739 Iterable bar2() sync* { yield x; } | 2756 Iterable bar2() sync* { yield x; } |
| 2740 Iterable<int> bar3() sync* { yield (/*info:DynamicCast*/x); } | 2757 Iterable<int> bar3() sync* { yield (/*info:DynamicCast*/x); } |
| 2741 Iterable<int> bar4() sync* { yield (/*severe:StaticTypeError*/new Iterab
le<int>()); } | 2758 Iterable<int> bar4() sync* { yield (/*severe:StaticTypeError*/new Iterab
le<int>()); } |
| 2742 | 2759 |
| 2743 baz1() sync* { yield* (/*info:DynamicCast*/x); } | 2760 baz1() sync* { yield* (/*info:DynamicCast*/x); } |
| 2744 Iterable baz2() sync* { yield* (/*info:DynamicCast*/x); } | 2761 Iterable baz2() sync* { yield* (/*info:DynamicCast*/x); } |
| 2745 Iterable<int> baz3() sync* { yield* (/*warning:DownCastComposite*/x); } | 2762 Iterable<int> baz3() sync* { yield* (/*warning:DownCastComposite*/x); } |
| 2746 Iterable<int> baz4() sync* { yield* new Iterable<int>(); } | 2763 Iterable<int> baz4() sync* { yield* new Iterable<int>(); } |
| 2747 Iterable<int> baz5() sync* { yield* (/*info:InferredTypeAllocation*/new
Iterable()); } | 2764 Iterable<int> baz5() sync* { yield* (/*info:InferredTypeAllocation*/new
Iterable()); } |
| 2748 ''' | 2765 ''' |
| 2749 })); | 2766 })); |
| 2750 }); | 2767 }); |
| 2751 } | 2768 } |
| OLD | NEW |