Files
sdk/tests/language/nnbd/null_assertions/parameter_checks_test.dart
T
Stephen Adams 30a7f00f0b [dart2js] Add --null-assertions flag
With --enable-asserts, does automatic debug assertion insertion:
https://github.com/dart-lang/language/blob/master/accepted/future-releases/nnbd/feature-specification.md#automatic-debug-assertion-insertion

Without --enable-asserts, does a NullCheck instead of an assert.

Change-Id: I57b8c9ce9e7605b9d2355b1d96a10db3fd631917

Fixes: 42403
Change-Id: I57b8c9ce9e7605b9d2355b1d96a10db3fd631917
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/156044
Commit-Queue: Stephen Adams <sra@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
2020-07-30 22:13:04 +00:00

56 lines
1.4 KiB
Dart

// Copyright (c) 2020, 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.
// Test for null assertions for parameters in NNBD weak mode.
// Requirements=nnbd-weak
// VMOptions=--enable-asserts
// dart2jsOptions=--enable-asserts -DcheckString=false
// SharedOptions=--null-assertions
// Opt out of Null Safety:
// @dart = 2.6
import "package:expect/expect.dart";
import 'parameter_checks_opted_in.dart';
bool Function(Object) asserted(String name) {
if (const bool.fromEnvironment('checkString', defaultValue: true)) {
return (e) => e is AssertionError && e.toString().contains("$name != null");
} else {
return (e) => e is AssertionError;
}
}
main() {
Expect.throws(() {
foo1(null);
}, asserted("a"));
Expect.throws(() {
foo2(1, null);
}, asserted("b"));
Expect.throws(() {
foo3();
}, asserted("b"));
Expect.throws(() {
foo3(b: null);
}, asserted("b"));
foo4a<int>(null);
Expect.throws(() {
foo4b<int>(null);
}, asserted("a"));
foo5a<int>(null);
Expect.throws(() {
foo5b<int>(null);
}, asserted("a"));
foo6a<int, int, int>(null);
Expect.throws(() {
foo6b<int, int, int>(null);
}, asserted("a"));
Expect.throws(() {
bar().call(null);
}, asserted("x"));
}