Files
sdk/tests/language/assert/with_message_test.dart
T
Martin Kustermann 76548a7ab9 [tests] Rewrite various tests to not depend on <obj>.runtimeType.toString()
In dart2wasm applications are by default deployed in `-O2` mode which
implies `--minify`.

Given this is the default configuration for customers, we want good
testing of this configuration and not large numbers of approved failures.

=> Rewrite various tests to not depend on `<obj>.runtimeType.toString()`

Change-Id: I1108b28c63b8bec6ad94df0d7b878b3339776df9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/436281
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2025-06-24 01:16:24 -07:00

91 lines
2.0 KiB
Dart

// Copyright (c) 2016, 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.
import "package:expect/expect.dart";
main() {
var assertsEnabled = false;
assert((assertsEnabled = true));
if (!assertsEnabled) return;
// TODO(rnystrom): Test cases where the first argument to assert() is a
// function.
testAssertFails();
testAssertDoesNotFail();
testNullMessage();
testDoesNotEvaluateMessageIfAssertSucceeds();
testMessageExpressionThatThrows();
testCallsToStringOnMessageLazily();
}
/// A class with a custom toString() that tracks when it is called.
class ToString {
bool calledToString = false;
String toString() {
calledToString = true;
return "toString!";
}
}
testAssertFails() {
try {
assert(false, "Oops");
Expect.fail("Assert should throw.");
} catch (e) {
Expect.isTrue(e.toString().contains("Oops"));
}
}
testAssertDoesNotFail() {
try {
assert(true, "Oops");
} catch (e) {
Expect.fail("Assert should not throw.");
}
}
testNullMessage() {
try {
assert(false, null);
Expect.fail("Assert should throw.");
} catch (e) {
Expect.isTrue(e.toString().contains("is not true"));
}
}
testDoesNotEvaluateMessageIfAssertSucceeds() {
try {
var evaluated = false;
assert(true, evaluated = true);
Expect.isFalse(evaluated);
} catch (e) {
Expect.fail("Assert should not throw.");
}
}
testMessageExpressionThatThrows() {
try {
assert(false, throw "dang");
Expect.fail("Should throw");
} catch (e) {
Expect.equals(e, "dang");
}
}
testCallsToStringOnMessageLazily() {
var toString = new ToString();
try {
assert(false, toString);
Expect.fail("Assert should throw.");
} catch (e) {
Expect.isFalse(toString.calledToString);
Expect.type<AssertionError>(e);
e as AssertionError;
Expect.identical(toString, e.message);
Expect.isFalse(toString.calledToString);
}
}