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