| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, 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 // Dart test for testing the ternary operator. |
| 5 |
| 6 import "package:expect/expect.dart"; |
| 7 |
| 8 // Test that `Null` acts like the bottom type - less than any other type. |
| 9 |
| 10 bool isCheckedMode = () { bool c = false; assert(c = true); return c; } (); |
| 11 |
| 12 typedef R Fun<A, R>(A argument); |
| 13 |
| 14 class C<T> { |
| 15 const C(); |
| 16 T returns() => null; |
| 17 void accepts(T x) {} |
| 18 } |
| 19 |
| 20 class NullBound<T extends num> {} |
| 21 |
| 22 class ListBound<T extends Iterable<Null>> {} |
| 23 |
| 24 main() { |
| 25 testClassTypes(); |
| 26 testFunctionTypes(); |
| 27 } |
| 28 |
| 29 void testClassTypes() { |
| 30 var cn = new C<Null>(); |
| 31 |
| 32 Expect.isTrue(cn is C<Null>, "C<Null> is C<Null>"); |
| 33 Expect.isTrue(cn is C<Object>, "C<Null> is C<Object>"); |
| 34 Expect.isTrue(cn is C<int>, "C<Null> is C<int>"); |
| 35 |
| 36 Expect.isNotNull(cn as C<Null>, "C<Null> as C<Null>"); |
| 37 Expect.isNotNull(cn as C<Object>, "C<Null> as C<Object>"); |
| 38 Expect.isNotNull(cn as C<int>, "C<Null> as C<int>"); |
| 39 |
| 40 var ccn = new C<C<Null>>(); |
| 41 |
| 42 Expect.isTrue(ccn is C<C<Null>>); |
| 43 Expect.isTrue(ccn is C<C<Object>>); |
| 44 Expect.isTrue(ccn is C<C<int>>); |
| 45 |
| 46 Expect.isNotNull(ccn as C<C<Null>>); |
| 47 Expect.isNotNull(ccn as C<C<Object>>); |
| 48 Expect.isNotNull(ccn as C<C<int>>); |
| 49 |
| 50 var ci = new C<int>(); |
| 51 Expect.isFalse(ci is C<Null>); |
| 52 |
| 53 var co = new C<Object>(); |
| 54 Expect.isFalse(co is C<Null>); |
| 55 |
| 56 if (!isCheckedMode) return; |
| 57 |
| 58 List<int> li1 = const <Null>[]; |
| 59 |
| 60 C<Null> x1 = cn; |
| 61 C<Object> x2 = cn; |
| 62 |
| 63 Expect.identical(x1 , cn); |
| 64 Expect.identical(x2 , cn); |
| 65 |
| 66 const C<Null> cocn = const C<Null>(); |
| 67 const C<Object> coco = cocn; |
| 68 const C<int> coci = cocn; |
| 69 |
| 70 Expect.identical(cocn, coco); |
| 71 Expect.identical(cocn, coci); |
| 72 |
| 73 Expect.throws(() { |
| 74 Null x = "string" as dynamic; |
| 75 use(x); // Avoid "x unused" warning. |
| 76 }); |
| 77 |
| 78 Expect.throws(() { |
| 79 Null x = new Object(); |
| 80 use(x); // Avoid "x unused" warning. |
| 81 }); |
| 82 |
| 83 NullBound<int> nb = new NullBound<Null>(); // Should not fail. |
| 84 use(nb); // Avoid "nb unused" warning. |
| 85 ListBound<List<Null>> lb = new ListBound<Null>(); // Should not fails |
| 86 use(lb); // Avoid "nb unused" warning. |
| 87 } |
| 88 |
| 89 void testFunctionTypes() { |
| 90 T1 t1 = new T1(); |
| 91 T2 t2 = new T2(); |
| 92 T1 t = t2; |
| 93 |
| 94 Fun<int, Null> f1 = t1.foo; |
| 95 Fun<Null, int> f2 = t.bar; |
| 96 f1 = t1.baz; |
| 97 f2 = t.qux; |
| 98 use(f1); |
| 99 use(f2); |
| 100 |
| 101 var l = new List<Fun<Null, Null>>(); |
| 102 Expect.isTrue(l is List<Fun<Null, int>>); |
| 103 l = new List<Fun<int, int>>(); |
| 104 Expect.isTrue(l is List<Fun<Null, num>>); |
| 105 |
| 106 Expect.isTrue(((int _) => null) is Fun<int, Null>); |
| 107 |
| 108 Null fun(int x) => null; |
| 109 Fun<Null, int> fun2 = fun; // Safe assignment. |
| 110 if (fun2 is Fun<int, Null>) { |
| 111 // If int->Null is *subtype* of Null->int (which it should be), |
| 112 // then type promotion succeeds. |
| 113 // If type promotion succeeds, the static type is int->Null, otherwise |
| 114 // it's Null->int. |
| 115 fun2(42); // Should not give a warning after type promotion. |
| 116 fun2(null).abs(); /// 03: static type warning, runtime error |
| 117 } |
| 118 } |
| 119 |
| 120 class T1 { |
| 121 Null foo(int x) => null; |
| 122 int bar(Null x) => null; |
| 123 Null baz(int x) => null; |
| 124 int qux(Null x) => null; |
| 125 } |
| 126 |
| 127 class T2 extends T1 { |
| 128 Null foo(Null x) => null; /// 01: static type warning |
| 129 Null bar(Null x) => null; |
| 130 int baz(int x) => x; /// 02: static type warning |
| 131 int qux(int x) => x; |
| 132 } |
| 133 |
| 134 // Avoid "variable not used" warnings. |
| 135 use(x) { |
| 136 return identical(x, x); |
| 137 } |
| OLD | NEW |