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 // Verify semantics of the ??= operator, including order of operations, by | 5 // Verify semantics of the ??= operator, including order of operations, by |
6 // keeping track of the operations performed. | 6 // keeping track of the operations performed. |
7 | 7 |
8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
9 import "if_null_assignment_helper.dart" as h; | 9 import "if_null_assignment_helper.dart" as h; |
10 | 10 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 119 |
120 void operator[]=(index, value) { | 120 void operator[]=(index, value) { |
121 h.operations.add('$s[$index]=$value'); | 121 h.operations.add('$s[$index]=$value'); |
122 } | 122 } |
123 | 123 |
124 final finalOne = 1; | 124 final finalOne = 1; |
125 final finalNull = null; | 125 final finalNull = null; |
126 | 126 |
127 void instanceTest() { | 127 void instanceTest() { |
128 // v ??= e is equivalent to ((x) => x == null ? v = e : x)(v) | 128 // v ??= e is equivalent to ((x) => x == null ? v = e : x)(v) |
129 vGetValue = 1; check(1, () => v ??= bad(), ['$s.v']); /// 01: ok | 129 vGetValue = 1; check(1, () => v ??= bad(), ['$s.v']); //# 01: ok |
130 yGetValue = 1; check(1, () => v ??= y, ['$s.v', 'y', '$s.v=1']); /// 02: ok | 130 yGetValue = 1; check(1, () => v ??= y, ['$s.v', 'y', '$s.v=1']); //# 02: ok |
131 check(1, () => finalOne ??= bad(), []); /// 03: static type warning | 131 check(1, () => finalOne ??= bad(), []); //# 03: static type warning |
132 yGetValue = 1; checkThrows(noMethod, () => finalNull ??= y, ['y']); /// 04:
static type warning | 132 yGetValue = 1; checkThrows(noMethod, () => finalNull ??= y, ['y']); //# 04:
static type warning |
133 } | 133 } |
134 } | 134 } |
135 | 135 |
136 class D extends C { | 136 class D extends C { |
137 D(String s) : super(s); | 137 D(String s) : super(s); |
138 | 138 |
139 get v => bad(); | 139 get v => bad(); |
140 | 140 |
141 void set v(value) { | 141 void set v(value) { |
142 bad(); | 142 bad(); |
143 } | 143 } |
144 | 144 |
145 void derivedInstanceTest() { | 145 void derivedInstanceTest() { |
146 // super.v ??= e is equivalent to | 146 // super.v ??= e is equivalent to |
147 // ((x) => x == null ? super.v = e : x)(super.v) | 147 // ((x) => x == null ? super.v = e : x)(super.v) |
148 vGetValue = 1; check(1, () => super.v ??= bad(), ['$s.v']); /// 05: ok | 148 vGetValue = 1; check(1, () => super.v ??= bad(), ['$s.v']); //# 05: ok |
149 yGetValue = 1; check(1, () => super.v ??= y, ['$s.v', 'y', '$s.v=1']); /// 0
6: ok | 149 yGetValue = 1; check(1, () => super.v ??= y, ['$s.v', 'y', '$s.v=1']); //# 0
6: ok |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 main() { | 153 main() { |
154 // Make sure the "none" test fails if "??=" is not implemented. This makes | 154 // Make sure the "none" test fails if "??=" is not implemented. This makes |
155 // status files easier to maintain. | 155 // status files easier to maintain. |
156 var _; _ ??= null; | 156 var _; _ ??= null; |
157 | 157 |
158 new C('c').instanceTest(); | 158 new C('c').instanceTest(); |
159 new D('d').derivedInstanceTest(); | 159 new D('d').derivedInstanceTest(); |
160 | 160 |
161 // v ??= e is equivalent to ((x) => x == null ? v = e : x)(v) | 161 // v ??= e is equivalent to ((x) => x == null ? v = e : x)(v) |
162 xGetValue = 1; check(1, () => x ??= bad(), ['x']); /// 07: ok | 162 xGetValue = 1; check(1, () => x ??= bad(), ['x']); //# 07: ok |
163 yGetValue = 1; check(1, () => x ??= y, ['x', 'y', 'x=1']); /// 08: ok | 163 yGetValue = 1; check(1, () => x ??= y, ['x', 'y', 'x=1']); //# 08: ok |
164 h.xGetValue = 1; check(1, () => h.x ??= bad(), ['h.x']); /// 09: ok | 164 h.xGetValue = 1; check(1, () => h.x ??= bad(), ['h.x']); //# 09: ok |
165 yGetValue = 1; check(1, () => h.x ??= y, ['h.x', 'y', 'h.x=1']); /// 10: ok | 165 yGetValue = 1; check(1, () => h.x ??= y, ['h.x', 'y', 'h.x=1']); //# 10: ok |
166 { var l = 1; check(1, () => l ??= bad(), []); } /// 11: ok | 166 { var l = 1; check(1, () => l ??= bad(), []); } //# 11: ok |
167 { var l; yGetValue = 1; check(1, () => l ??= y, ['y']); Expect.equals(1, l); }
/// 12: ok | 167 { var l; yGetValue = 1; check(1, () => l ??= y, ['y']); Expect.equals(1, l); }
//# 12: ok |
168 { final l = 1; check(1, () => l ??= bad(), []); } /// 13: static type warning | 168 { final l = 1; check(1, () => l ??= bad(), []); } //# 13: static type warning |
169 { final l = null; yGetValue = 1; checkThrows(noMethod, () => l ??= y, ['y']);
} /// 14: static type warning | 169 { final l = null; yGetValue = 1; checkThrows(noMethod, () => l ??= y, ['y']);
} //# 14: static type warning |
170 check(C, () => C ??= bad(), []); /// 15: static type warning | 170 check(C, () => C ??= bad(), []); //# 15: static type warning |
171 h ??= null; /// 29: compile-time error | 171 h ??= null; //# 29: compile-time error |
172 h[0] ??= null; /// 30: compile-time error | 172 h[0] ??= null; //# 30: compile-time error |
173 | 173 |
174 // C.v ??= e is equivalent to ((x) => x == null ? C.v = e : x)(C.v) | 174 // C.v ??= e is equivalent to ((x) => x == null ? C.v = e : x)(C.v) |
175 C.xGetValue = 1; check(1, () => C.x ??= bad(), ['C.x']); /// 16: ok | 175 C.xGetValue = 1; check(1, () => C.x ??= bad(), ['C.x']); //# 16: ok |
176 yGetValue = 1; check(1, () => C.x ??= y, ['C.x', 'y', 'C.x=1']); /// 17: ok | 176 yGetValue = 1; check(1, () => C.x ??= y, ['C.x', 'y', 'C.x=1']); //# 17: ok |
177 h.C.xGetValue = 1; check(1, () => h.C.x ??= bad(), ['h.C.x']); /// 18: ok | 177 h.C.xGetValue = 1; check(1, () => h.C.x ??= bad(), ['h.C.x']); //# 18: ok |
178 yGetValue = 1; check(1, () => h.C.x ??= y, ['h.C.x', 'y', 'h.C.x=1']); /// 19:
ok | 178 yGetValue = 1; check(1, () => h.C.x ??= y, ['h.C.x', 'y', 'h.C.x=1']); //# 19:
ok |
179 | 179 |
180 // e1.v ??= e2 is equivalent to | 180 // e1.v ??= e2 is equivalent to |
181 // ((x) => ((y) => y == null ? x.v = e2 : y)(x.v))(e1) | 181 // ((x) => ((y) => y == null ? x.v = e2 : y)(x.v))(e1) |
182 xGetValue = new C('x'); xGetValue.vGetValue = 1; /// 20: ok | 182 xGetValue = new C('x'); xGetValue.vGetValue = 1; //# 20: ok |
183 check(1, () => x.v ??= bad(), ['x', 'x.v']); // /// 20: continued | 183 check(1, () => x.v ??= bad(), ['x', 'x.v']); // //# 20: continued |
184 xGetValue = new C('x'); yGetValue = 1; // /// 21: ok | 184 xGetValue = new C('x'); yGetValue = 1; // //# 21: ok |
185 check(1, () => x.v ??= y, ['x', 'x.v', 'y', 'x.v=1']); /// 21: continued | 185 check(1, () => x.v ??= y, ['x', 'x.v', 'y', 'x.v=1']); //# 21: continued |
186 fValue = new C('f()'); fValue.vGetValue = 1; // /// 22: ok | 186 fValue = new C('f()'); fValue.vGetValue = 1; // //# 22: ok |
187 check(1, () => f().v ??= bad(), ['f()', 'f().v']); /// 22: continued | 187 check(1, () => f().v ??= bad(), ['f()', 'f().v']); //# 22: continued |
188 fValue = new C('f()'); yGetValue = 1; // /// 23: ok | 188 fValue = new C('f()'); yGetValue = 1; // //# 23: ok |
189 check(1, () => f().v ??= y, ['f()', 'f().v', 'y', 'f().v=1']); /// 23: continu
ed | 189 check(1, () => f().v ??= y, ['f()', 'f().v', 'y', 'f().v=1']); //# 23: continu
ed |
190 | 190 |
191 // e1[e2] ??= e3 is equivalent to | 191 // e1[e2] ??= e3 is equivalent to |
192 // ((a, i) => ((x) => x == null ? a[i] = e3 : x)(a[i]))(e1, e2) | 192 // ((a, i) => ((x) => x == null ? a[i] = e3 : x)(a[i]))(e1, e2) |
193 xGetValue = new C('x'); yGetValue = 1; xGetValue.indexGetValue = 2; /// 24: ok | 193 xGetValue = new C('x'); yGetValue = 1; xGetValue.indexGetValue = 2; //# 24: ok |
194 check(2, () => x[y] ??= bad(), ['x', 'y', 'x[1]']); // /// 24:
continued | 194 check(2, () => x[y] ??= bad(), ['x', 'y', 'x[1]']); // //# 24:
continued |
195 xGetValue = new C('x'); yGetValue = 1; zGetValue = 2; // /// 25: ok | 195 xGetValue = new C('x'); yGetValue = 1; zGetValue = 2; // //# 25: ok |
196 check(2, () => x[y] ??= z, ['x', 'y', 'x[1]', 'z', 'x[1]=2']); /// 25: continu
ed | 196 check(2, () => x[y] ??= z, ['x', 'y', 'x[1]', 'z', 'x[1]=2']); //# 25: continu
ed |
197 | 197 |
198 // e1?.v ??= e2 is equivalent to ((x) => x == null ? null : x.v ??= e2)(e1). | 198 // e1?.v ??= e2 is equivalent to ((x) => x == null ? null : x.v ??= e2)(e1). |
199 check(null, () => x?.v ??= bad(), ['x']); /// 26: ok | 199 check(null, () => x?.v ??= bad(), ['x']); //# 26: ok |
200 xGetValue = new C('x'); xGetValue.vGetValue = 1; /// 27: ok | 200 xGetValue = new C('x'); xGetValue.vGetValue = 1; //# 27: ok |
201 check(1, () => x?.v ??= bad(), ['x', 'x.v']); // /// 27: continued | 201 check(1, () => x?.v ??= bad(), ['x', 'x.v']); // //# 27: continued |
202 xGetValue = new C('x'); yGetValue = 1; // /// 28: ok | 202 xGetValue = new C('x'); yGetValue = 1; // //# 28: ok |
203 check(1, () => x?.v ??= y, ['x', 'x.v', 'y', 'x.v=1']); /// 28: continued | 203 check(1, () => x?.v ??= y, ['x', 'x.v', 'y', 'x.v=1']); //# 28: continued |
204 | 204 |
205 // C?.v ??= e2 is equivalent to C.v ??= e2. | 205 // C?.v ??= e2 is equivalent to C.v ??= e2. |
206 C.xGetValue = 1; // /// 29: ok | 206 C.xGetValue = 1; // //# 29: ok |
207 check(1, () => C?.x ??= bad(), ['C.x']); /// 29: continued | 207 check(1, () => C?.x ??= bad(), ['C.x']); //# 29: continued |
208 h.C.xgetValue = 1; // /// 30: ok | 208 h.C.xgetValue = 1; // //# 30: ok |
209 check(1, () => h.c?.x ??= bad(), ['h.C.x']); /// 30: continued | 209 check(1, () => h.c?.x ??= bad(), ['h.C.x']); //# 30: continued |
210 yGetValue = 1; // /// 31: ok | 210 yGetValue = 1; // //# 31: ok |
211 check(1, () => C?.x ??= y, ['C.x', 'y', 'C.x=1']); /// 31: continued | 211 check(1, () => C?.x ??= y, ['C.x', 'y', 'C.x=1']); //# 31: continued |
212 yGetValue = 1; // /// 32: ok | 212 yGetValue = 1; // //# 32: ok |
213 check(1, () => h.C?.x ??= y, ['h.C.x', 'y', 'h.C.x=1']); /// 32: continued | 213 check(1, () => h.C?.x ??= y, ['h.C.x', 'y', 'h.C.x=1']); //# 32: continued |
214 } | 214 } |
OLD | NEW |