dart-sdk/tests/language/vm/bool_check_stack_traces_test.dart
Erik Ernst 3a7eeb6315 Rename is{Strong,Weak}Mode to has{Sound,Unsound}NullSafety
Change-Id: If3912d75c5f89a741299b2fae4299d01ac928eec
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/170424
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Michael Thomsen <mit@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
Commit-Queue: Erik Ernst <eernst@google.com>
2020-11-05 14:26:53 +00:00

110 lines
2.5 KiB
Dart

// Copyright (c) 2018, 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";
class Test1 {
void bar(dynamic condition) {
if (condition) {
Expect.fail('Should not reach here');
}
Expect.fail('Should throw earlier');
}
}
void test1(dynamic condition) {
Test1 obj = new Test1();
obj.bar(condition);
}
class Test2 {
dynamic condition;
Test2(this.condition);
void bar() {
if (!condition) {
Expect.fail('Should not reach here');
}
Expect.fail('Should throw earlier');
}
}
void test2(dynamic condition) {
Test2 obj = new Test2(condition);
obj.bar();
}
class Test3 {
dynamic condition;
Test3(this.condition);
bool bazz() => condition;
void bar() {
while (bazz() || bazz()) {
Expect.fail('Should not reach here');
}
Expect.fail('Should throw earlier');
}
}
void test3(dynamic condition) {
Test3 obj = new Test3(condition);
obj.bar();
}
const dynamic test4Condition = null;
void test4(dynamic condition) {
if (test4Condition) {
Expect.fail('Should not reach here');
}
Expect.fail('Should throw earlier');
}
void test5(dynamic condition) {
if (null as dynamic) {
Expect.fail('Should not reach here');
}
Expect.fail('Should throw earlier');
}
void testStackTrace(void testCase(dynamic condition), List<int> lineNumbers) {
try {
testCase(null);
Expect.fail("Using null in a bool condition should throw TypeError");
} catch (e, stacktrace) {
print('--------- exception ---------');
print(e);
print('-------- stack trace --------');
print(stacktrace);
print('-----------------------------');
if (hasSoundNullSafety) {
Expect.isTrue(e is TypeError);
Expect.equals(
"type 'Null' is not a subtype of type 'bool'", e.toString());
} else {
Expect.isTrue(e is AssertionError);
Expect.equals('Failed assertion: boolean expression must not be null',
e.toString());
}
final String st = stacktrace.toString();
for (int lineNum in lineNumbers) {
String item = '.dart:$lineNum';
Expect.isTrue(st.contains(item), "Stack trace doesn't contain $item");
}
print('OK');
}
}
main() {
testStackTrace(test1, [9, 18]);
testStackTrace(test2, [26, 35]);
testStackTrace(test3, [45, 54]);
testStackTrace(test4, [60]); //# 01: ok
testStackTrace(test5, [67]); //# 02: ok
}