dart-sdk/tests/language_2/if_null_precedence_test.dart
Bob Nystrom a719910d78 Migrate block 116.
Interesting bits:

- Removed some behavior that is now unreachable in the presence of
  type errors.
- Made if_null_behavior_test not a multitest since it doesn't need to
  be any more.
- Likewise, if_null_evaluation_order_test never needed to be a
  multitest.

R=lrn@google.com

Review-Url: https://codereview.chromium.org/3003933002 .
2017-08-29 15:50:49 -07:00

40 lines
1.7 KiB
Dart

// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Verify that '??' binds tighter than '?:' and less tightly than '||'.
import "package:expect/expect.dart";
main() {
// Make sure the "none" test fails if "??" is not implemented. This makes
// status files easier to maintain.
var _ = null ?? null;
// "a ?? b ?? c" should be legal, and should evaluate to the first non-null
// value (or null if there are no non-null values).
Expect.equals(1, 1 ?? 2 ?? 3);
Expect.equals(2, null ?? 2 ?? 3);
Expect.equals(3, null ?? null ?? 3);
Expect.equals(null, null ?? null ?? null);
// "a ?? b ? c : d" should parse as "(a ?? b) ? c : d", therefore provided
// that a is true, b need not be a bool. An incorrect parse of
// "a ?? (b ? c : d)" would require b to be a bool to avoid a static type
// warning.
Expect.equals(2, true ?? 1 ? 2 : 3);
// "a ?? b || c" should parse as "a ?? (b || c)", therefore it's a static
// type warning if b doesn't have type bool. An incorrect parse of
// "(a ?? b) || c" would allow b to have any type provided that a is bool.
false ?? 1 || true; //# 06: compile-time error
// "a || b ?? c" should parse as "(a || b) ?? c", therefore it is a static
// type warning if b doesn't have type bool. An incorrect parse of
// "a || (b ?? c)" would allow b to have any type provided that c is bool.
false || 1 ?? true; //# 07: compile-time error
// An incorrect parse of "a || (b ?? c)" would result in no checked-mode
// error.
Expect.throwsAssertionError(() => false || null ?? true);
}