Files
sdk/tests/language/assert/assert_test.dart
T
Lasse R.H. Nielsen 98b4635aa9 Make TypeError no longer implement AssertionError.
This means that TypeError no longer inherits a spurious `message` member.
The message of a platform-thrown TypeError is still available as `toString()`.

Fixes #40317

Bug: http://dartbug.com/40317
Change-Id: I77312859ebae3f92c2e56aeea6283b075b71c8d5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/136129
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
2020-02-21 14:02:57 +00:00

91 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";
int testTrue() {
int i = 0;
try {
assert(true);
} on AssertionError {
i = 1;
}
return i;
}
int testFalse() {
int i = 0;
try {
assert(false);
} on AssertionError {
i = 1;
}
return i;
}
unknown(dynamic a) {
return a ? true : false;
}
int testBoolean(bool value) {
int i = 0;
try {
assert(value);
} on AssertionError {
i = 1;
}
return i;
}
int testDynamic(dynamic value) {
int i = 0;
try {
assert(value);
} on AssertionError {
i = 1;
}
return i;
}
testMessage(value, message) {
try {
assert(value, message);
return null;
} on AssertionError catch (error) {
return error;
}
return null;
}
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.throwsTypeError(() => testDynamic(null));
Expect.throwsTypeError(() => testDynamic(42));
Expect.throwsTypeError(() => testDynamic(() => true));
Expect.throwsTypeError(() => testDynamic(() => false));
Expect.throwsTypeError(() => testDynamic(() => 42));
Expect.throwsTypeError(() => testDynamic(() => null));
Expect.equals(1234, testMessage(false, 1234).message);
Expect.equals('hi', testMessage(false, 'hi').message);
// These assertions throw a type error because boolean conversion failed.
Expect.throwsTypeError(() => testMessage(null, 1234));
Expect.throwsTypeError(() => testMessage(null, 'hi'));
Expect.throwsTypeError(() => testMessage(() => null, 'hi'));
Expect.throwsTypeError(() => testMessage(() => false, 'hi'));
Expect.throwsTypeError(() => testMessage(() => true, 'hi'));
}