Files
sdk/tests/language/assert/assert_test.dart
T
Robert Nystrom 688801cb27 Migrate language_2/assert to NNBD.
Change-Id: I4daa09afa52f76076374591b3e3f3420a46b169b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134240
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2020-02-06 22:47:25 +00:00

92 lines
2.1 KiB
Dart

// Copyright (c) 2011, 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.
// VMOptions=--enable-asserts
// dart2jsOptions=--enable-asserts
// Dart test program testing assert statements.
import "package:expect/expect.dart";
testTrue() {
int i = 0;
try {
assert(true);
} on AssertionError {
i = 1;
}
return i;
}
testFalse() {
int i = 0;
try {
assert(false);
} on AssertionError {
i = 1;
}
return i;
}
unknown(dynamic a) {
return a ? true : false;
}
testBoolean(bool value) {
int i = 0;
try {
assert(value);
} on AssertionError {
i = 1;
}
return i;
}
testDynamic(dynamic value) {
int i = 0;
try {
assert(value);
} on AssertionError {
i = 1;
}
return i;
}
testMessage(value, message) {
try {
assert(value, message);
return null;
} catch (error) {
// Catch any type to allow the Boolean conversion to throw either
// AssertionError or TypeError.
return error;
}
}
main() {
Expect.equals(0, testTrue());
Expect.equals(0, testBoolean(true));
Expect.equals(0, testDynamic(unknown(true)));
Expect.equals(1, testFalse());
Expect.equals(1, testBoolean(false));
Expect.equals(1, testDynamic(unknown(false)));
Expect.equals(1, testDynamic(null));
Expect.equals(1, testDynamic(42));
Expect.equals(1, testDynamic(() => true));
Expect.equals(1, testDynamic(() => false));
Expect.equals(1, testDynamic(() => 42));
Expect.equals(1, testDynamic(() => null));
Expect.equals(1234, testMessage(false, 1234).message);
Expect.equals('hi', testMessage(false, 'hi').message);
// These errors do not have the message because boolean conversion failed.
Expect.notEquals(1234, testMessage(null, 1234).message);
Expect.notEquals('hi', testMessage(null, 'hi').message);
Expect.notEquals('hi', testMessage(() => null, 'hi').message);
Expect.notEquals('hi', testMessage(() => false, 'hi').message);
Expect.notEquals('hi', testMessage(() => true, 'hi').message);
}