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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 vGetValue = null; | 103 vGetValue = null; |
104 return tmp; | 104 return tmp; |
105 } | 105 } |
106 | 106 |
107 void set v(value) { | 107 void set v(value) { |
108 h.operations.add('$s.v=$value'); | 108 h.operations.add('$s.v=$value'); |
109 } | 109 } |
110 | 110 |
111 var indexGetValue = null; | 111 var indexGetValue = null; |
112 | 112 |
113 operator [](index) { | 113 operator[](index) { |
114 h.operations.add('$s[$index]'); | 114 h.operations.add('$s[$index]'); |
115 var tmp = indexGetValue; | 115 var tmp = indexGetValue; |
116 indexGetValue = null; | 116 indexGetValue = null; |
117 return tmp; | 117 return tmp; |
118 } | 118 } |
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 |
(...skipping 15 matching lines...) Expand all Loading... |
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 _; | 156 var _; _ ??= null; |
157 _ ??= null; | |
158 | 157 |
159 new C('c').instanceTest(); | 158 new C('c').instanceTest(); |
160 new D('d').derivedInstanceTest(); | 159 new D('d').derivedInstanceTest(); |
161 | 160 |
162 // 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) |
163 xGetValue = 1; check(1, () => x ??= bad(), ['x']); //# 07: ok | 162 xGetValue = 1; check(1, () => x ??= bad(), ['x']); //# 07: ok |
164 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 |
165 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 |
166 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 |
167 { var l = 1; check(1, () => l ??= bad(), []); } //# 11: ok | 166 { var l = 1; check(1, () => l ??= bad(), []); } //# 11: ok |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 // C?.v ??= e2 is equivalent to C.v ??= e2. | 205 // C?.v ??= e2 is equivalent to C.v ??= e2. |
207 C.xGetValue = 1; // //# 29: ok | 206 C.xGetValue = 1; // //# 29: ok |
208 check(1, () => C?.x ??= bad(), ['C.x']); //# 29: continued | 207 check(1, () => C?.x ??= bad(), ['C.x']); //# 29: continued |
209 h.C.xgetValue = 1; // //# 30: ok | 208 h.C.xgetValue = 1; // //# 30: ok |
210 check(1, () => h.c?.x ??= bad(), ['h.C.x']); //# 30: continued | 209 check(1, () => h.c?.x ??= bad(), ['h.C.x']); //# 30: continued |
211 yGetValue = 1; // //# 31: ok | 210 yGetValue = 1; // //# 31: ok |
212 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 |
213 yGetValue = 1; // //# 32: ok | 212 yGetValue = 1; // //# 32: ok |
214 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 |
215 } | 214 } |
OLD | NEW |