mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 04:27:17 +00:00
688801cb27
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>
89 lines
2 KiB
Dart
89 lines
2 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.isTrue(e.toString().contains("Instance of 'ToString'"));
|
|
Expect.isFalse(toString.calledToString);
|
|
}
|
|
}
|