| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Verify that '??' binds tighter than '?:' and less tightly than '||'. | |
| 6 | |
| 7 import "package:expect/expect.dart"; | |
| 8 | |
| 9 assertionError(e) => e is AssertionError; | |
| 10 | |
| 11 // Determine whether the VM is running in checked mode. | |
| 12 bool get checkedMode { | |
| 13 try { | |
| 14 var x = 'foo'; | |
| 15 int y = x; | |
| 16 return false; | |
| 17 } catch (_) { | |
| 18 return true; | |
| 19 } | |
| 20 } | |
| 21 | |
| 22 main() { | |
| 23 // Make sure the "none" test fails if "??" is not implemented. This makes | |
| 24 // status files easier to maintain. | |
| 25 var _ = null ?? null; | |
| 26 | |
| 27 // "a ?? b ?? c" should be legal, and should evaluate to the first non-null | |
| 28 // value (or null if there are no non-null values). | |
| 29 Expect.equals(1, 1 ?? 2 ?? 3); //# 01: ok | |
| 30 Expect.equals(2, null ?? 2 ?? 3); //# 02: ok | |
| 31 Expect.equals(3, null ?? null ?? 3); //# 03: ok | |
| 32 Expect.equals(null, null ?? null ?? null); //# 04: ok | |
| 33 | |
| 34 // "a ?? b ? c : d" should parse as "(a ?? b) ? c : d", therefore provided | |
| 35 // that a is true, b need not be a bool. An incorrect parse of | |
| 36 // "a ?? (b ? c : d)" would require b to be a bool to avoid a static type | |
| 37 // warning. | |
| 38 Expect.equals(2, true ?? 1 ? 2 : 3); //# 05: ok | |
| 39 | |
| 40 // "a ?? b || c" should parse as "a ?? (b || c)", therefore it's a static | |
| 41 // type warning if b doesn't have type bool. An incorrect parse of | |
| 42 // "(a ?? b) || c" would allow b to have any type provided that a is bool. | |
| 43 Expect.equals(false, false ?? 1 || true); //# 06: static type warning | |
| 44 | |
| 45 // "a || b ?? c" should parse as "(a || b) ?? c", therefore it is a static | |
| 46 // type warning if b doesn't have type bool. An incorrect parse of | |
| 47 // "a || (b ?? c)" would allow b to have any type provided that c is bool. | |
| 48 if (checkedMode) { | |
| 49 Expect.throws(() => false || 1 ?? true, assertionError); //# 07: static type
warning | |
| 50 } else { | |
| 51 Expect.equals(false, false || 1 ?? true); // //# 07: continued | |
| 52 } | |
| 53 | |
| 54 if (checkedMode) { | |
| 55 // An incorrect parse of "a || (b ?? c)" would result in no checked-mode | |
| 56 // error. | |
| 57 Expect.throws(() => false || null ?? true, assertionError); //# 08: ok | |
| 58 } else { | |
| 59 // An incorrect parse of "a || (b ?? c)" would result in c being evaluated. | |
| 60 int i = 0; // //# 08: contin
ued | |
| 61 Expect.equals(false, false || null ?? i++ == 0); // //# 08: contin
ued | |
| 62 Expect.equals(0, i); // //# 08: contin
ued | |
| 63 } | |
| 64 } | |
| OLD | NEW |