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