Migrate tests to language/stack_trace.

Change-Id: I4f6ac937327a7e610976668377b9cccb03d89d9c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/146583
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
This commit is contained in:
Lasse Reichstein Holst Nielsen 2020-05-07 13:47:57 +00:00 committed by commit-bot@chromium.org
parent 9e76548a2c
commit 58c86e254b
11 changed files with 808 additions and 49 deletions

View file

@ -0,0 +1,35 @@
// Copyright (c) 2015, 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.
// Tests that `await` can handle a future with a non-native stack trace.
import "dart:async";
import "package:expect/expect.dart";
class Blah implements StackTrace {
Blah(this._trace);
toString() {
return "Blah " + _trace.toString();
}
var _trace;
}
foo() {
var x = "\nBloop\nBleep\n";
return Future.error(42, Blah(x));
}
main() async {
try {
await foo();
Expect.fail("Should not reach here.");
} on int catch (e, s) {
Expect.equals(42, e);
Expect.equals("Blah \nBloop\nBleep\n", s.toString());
return;
}
Expect.fail("Unreachable.");
}

View file

@ -3,6 +3,8 @@
// BSD-style license that can be found in the LICENSE file.
// Dart test program for testing throw statement
// Tests that thrown errors contain their stack trace.
import "package:expect/expect.dart";
class MyException {
@ -30,6 +32,7 @@ class Helper1 {
func5();
} on ArgumentError catch (e) {
i = 100;
// Error contains stack trace, even if we don't ask for one with (e, s).
Expect.isNotNull(e.stackTrace, "Errors need a stackTrace on throw");
}
return i;
@ -73,54 +76,7 @@ class Helper2 {
}
}
class Helper3 {
static int func1() {
return func2();
}
static int func2() {
return func3();
}
static int func3() {
return func4();
}
static int func4() {
var i = 0;
try {
i = 10;
func5();
} on MyException catch (e) {
i = 300;
try {
// There should be no stackTrace in this normal exception object.
// We should get a NoSuchMethodError.
var trace = e.stackTrace;
// ^^^^^^^^^^
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER
// [cfe] The getter 'stackTrace' isn't defined for the class 'MyException'.
} on NoSuchMethodError catch (e) {
Expect.isNotNull(e.stackTrace, "Error needs a stackTrace on throw");
}
}
return i;
}
static List func5() {
// Throw an Exception (any random object).
throw new MyException("MyException in func5");
}
}
class ErrorStackTraceTest {
static testMain() {
Expect.equals(100, Helper1.func1());
Expect.equals(200, Helper2.func1());
Expect.equals(300, Helper3.func1());
}
}
main() {
ErrorStackTraceTest.testMain();
Expect.equals(100, Helper1.func1());
Expect.equals(200, Helper2.func1());
}

View file

@ -0,0 +1,17 @@
// Copyright (c) 2013, 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.
// Test that a stack trace is properly terminated (issue 8850).
import "package:expect/expect.dart";
void main() {
var ex = new Exception("fail");
try {
throw ex;
} on Exception catch (e, st) {
Expect.equals(ex, e);
Expect.isTrue(st.toString().length > 0);
}
}

View file

@ -1,3 +1,7 @@
// Copyright (c) 2015, 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 "dart:async";
import "package:expect/expect.dart";

View file

@ -0,0 +1,60 @@
// Copyright (c) 2017, 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.
// Test that stack traces are properly demangled in constructors (#28740).
// Regression test for http://dartbug.com/28740
import "package:expect/expect.dart";
class SomeClass {
SomeClass.namedConstructor() {
throw Exception();
}
SomeClass() {
throw Exception();
}
factory SomeClass.useFactory() {
throw Exception();
}
}
class OnlyHasFactory {
factory OnlyHasFactory() {
throw Exception();
}
}
void main() {
try {
SomeClass();
} on Exception catch (e, st) {
final stString = st.toString();
Expect.isTrue(stString.contains("new SomeClass"));
Expect.isFalse(stString.contains("SomeClass."));
}
try {
SomeClass.namedConstructor();
} on Exception catch (e, st) {
final stString = st.toString();
Expect.isTrue(stString.contains("new SomeClass.namedConstructor"));
}
try {
OnlyHasFactory();
} on Exception catch (e, st) {
final stString = st.toString();
Expect.isTrue(stString.contains("new OnlyHasFactory"));
Expect.isFalse(stString.contains("OnlyHasFactory."));
}
try {
SomeClass.useFactory();
} on Exception catch (e, st) {
final stString = st.toString();
Expect.isTrue(stString.contains("new SomeClass.useFactory"));
}
}

View file

@ -0,0 +1,54 @@
// (c) 2013, 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";
@pragma("vm:entry-point") // Prevents obfuscation
void func1() {
throw new Exception("Test full stacktrace");
}
@pragma("vm:entry-point") // Prevents obfuscation
void func2() {
func1();
}
@pragma("vm:entry-point") // Prevents obfuscation
void func3() {
try {
func2();
} on Object catch (e, s) {
var fullTrace = s.toString();
Expect.isTrue(fullTrace.contains("func1"));
Expect.isTrue(fullTrace.contains("func2"));
Expect.isTrue(fullTrace.contains("func3"));
Expect.isTrue(fullTrace.contains("func4"));
Expect.isTrue(fullTrace.contains("func5"));
Expect.isTrue(fullTrace.contains("func6"));
Expect.isTrue(fullTrace.contains("main"));
}
}
@pragma("vm:entry-point") // Prevents obfuscation
int func4() {
func3();
return 1;
}
@pragma("vm:entry-point") // Prevents obfuscation
int func5() {
func4();
return 1;
}
@pragma("vm:entry-point") // Prevents obfuscation
int func6() {
func5();
return 1;
}
main() {
var i = func6();
Expect.equals(1, i);
}

View file

@ -0,0 +1,75 @@
// (c) 2013, 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";
@pragma("vm:entry-point") // Prevent obfuscation.
void func1() {
throw new Exception("Test full stacktrace");
}
@pragma("vm:entry-point") // Prevent obfuscation.
void func2() {
func1();
}
@pragma("vm:entry-point") // Prevent obfuscation.
void func3() {
try {
func2();
} on Object catch (e, s) {
var fullTrace = s.toString();
Expect.isTrue(fullTrace.contains("func1"));
Expect.isTrue(fullTrace.contains("func2"));
Expect.isTrue(fullTrace.contains("func3"));
Expect.isTrue(fullTrace.contains("func4"));
Expect.isTrue(fullTrace.contains("func5"));
Expect.isTrue(fullTrace.contains("func6"));
Expect.isTrue(fullTrace.contains("func7"));
Expect.isTrue(fullTrace.contains("main"));
rethrow; // This is a rethrow.
}
}
@pragma("vm:entry-point") // Prevent obfuscation.
int func4() {
func3();
return 1;
}
@pragma("vm:entry-point") // Prevent obfuscation.
int func5() {
try {
func4();
} on Object catch (e, s) {
var fullTrace = s.toString();
Expect.isTrue(fullTrace.contains("func1"));
Expect.isTrue(fullTrace.contains("func2"));
Expect.isTrue(fullTrace.contains("func3"));
Expect.isTrue(fullTrace.contains("func4"));
Expect.isTrue(fullTrace.contains("func5"));
Expect.isTrue(fullTrace.contains("func6"));
Expect.isTrue(fullTrace.contains("func7"));
Expect.isTrue(fullTrace.contains("main"));
}
return 1;
}
@pragma("vm:entry-point") // Prevent obfuscation.
int func6() {
func5();
return 1;
}
@pragma("vm:entry-point") // Prevent obfuscation.
int func7() {
func6();
return 1;
}
main() {
var i = func7();
Expect.equals(1, i);
}

View file

@ -0,0 +1,74 @@
// (c) 2013, 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";
@pragma("vm:entry-point") // Prevent obfuscation
void func1() {
throw new Exception("Test full stacktrace");
}
@pragma("vm:entry-point") // Prevent obfuscation
void func2() {
func1();
}
@pragma("vm:entry-point") // Prevent obfuscation
void func3() {
try {
func2();
} on Object catch (e, s) {
var fullTrace = s.toString();
Expect.isTrue(fullTrace.contains("func1"));
Expect.isTrue(fullTrace.contains("func2"));
Expect.isTrue(fullTrace.contains("func3"));
Expect.isTrue(fullTrace.contains("func4"));
Expect.isTrue(fullTrace.contains("func5"));
Expect.isTrue(fullTrace.contains("func6"));
Expect.isTrue(fullTrace.contains("func7"));
Expect.isTrue(fullTrace.contains("main"));
throw new Exception("This is not a rethrow");
}
}
@pragma("vm:entry-point") // Prevent obfuscation
int func4() {
func3();
return 1;
}
@pragma("vm:entry-point") // Prevent obfuscation
int func5() {
try {
func4();
} on Object catch (e, s) {
var fullTrace = s.toString();
Expect.isFalse(fullTrace.contains("func1"));
Expect.isFalse(fullTrace.contains("func2"));
Expect.isTrue(fullTrace.contains("func3"));
Expect.isTrue(fullTrace.contains("func4"));
Expect.isTrue(fullTrace.contains("func5"));
Expect.isTrue(fullTrace.contains("func6"));
Expect.isTrue(fullTrace.contains("func7"));
Expect.isTrue(fullTrace.contains("main"));
}
return 1;
}
@pragma("vm:entry-point") // Prevent obfuscation
int func6() {
func5();
return 1;
}
@pragma("vm:entry-point") // Prevent obfuscation
int func7() {
func6();
return 1;
}
main() {
var i = func7();
Expect.equals(1, i);
}

View file

@ -0,0 +1,179 @@
// Copyright (c) 2014, 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.
class SubclassOfError extends Error {}
fail() => throw "Fail";
// == Rethrow, skipping through typed handlers. ==
@pragma("vm:entry-point") // Prevent obfuscation
aa1() {
try {
bb1();
fail();
} on Error catch (error
, stacktrace // //# withtraceparameter: ok
) {
expectTrace(
['gg1', 'ff1', 'ee1', 'dd1', 'cc1', 'bb1', 'aa1'], error.stackTrace);
expectTrace(['gg1', 'ff1', 'ee1', 'dd1', 'cc1', 'bb1', 'aa1'], stacktrace); // //# withtraceparameter: continued
}
}
@pragma("vm:entry-point") // Prevent obfuscation
bb1() => cc1();
@pragma("vm:entry-point") // Prevent obfuscation
cc1() {
try {
dd1();
} on String catch (_) {
fail();
} on int catch (_) {
fail();
}
}
@pragma("vm:entry-point") // Prevent obfuscation
dd1() => ee1();
@pragma("vm:entry-point") // Prevent obfuscation
ee1() {
try {
ff1();
} catch (_) {
rethrow;
}
}
@pragma("vm:entry-point") // Prevent obfuscation
ff1() => gg1();
@pragma("vm:entry-point") // Prevent obfuscation
gg1() => throw new SubclassOfError();
// == Rethrow, rethrow again in typed handler. ==
@pragma("vm:entry-point") // Prevent obfuscation
aa2() {
try {
bb2();
fail();
} on Error catch (error
, stacktrace // //# withtraceparameter: continued
) {
expectTrace(
['gg2', 'ff2', 'ee2', 'dd2', 'cc2', 'bb2', 'aa2'], error.stackTrace);
expectTrace(['gg2', 'ff2', 'ee2', 'dd2', 'cc2', 'bb2', 'aa2'], stacktrace); // //# withtraceparameter: continued
}
}
@pragma("vm:entry-point") // Prevent obfuscation
bb2() => cc2();
@pragma("vm:entry-point") // Prevent obfuscation
cc2() {
try {
dd2();
} on SubclassOfError catch (_) {
rethrow;
} on int catch (_) {
fail();
}
}
@pragma("vm:entry-point") // Prevent obfuscation
dd2() => ee2();
@pragma("vm:entry-point") // Prevent obfuscation
ee2() {
try {
ff2();
} catch (e) {
rethrow;
}
}
@pragma("vm:entry-point") // Prevent obfuscation
ff2() => gg2();
@pragma("vm:entry-point") // Prevent obfuscation
gg2() => throw new SubclassOfError();
// == Rethrow, with intervening catch without a trace parameter.
@pragma("vm:entry-point") // Prevent obfuscation
aa3() {
try {
bb3();
fail();
} on Error catch (error
, stacktrace // //# withtraceparameter: continued
) {
expectTrace(
['gg3', 'ff3', 'ee3', 'dd3', 'cc3', 'bb3', 'aa3'], error.stackTrace);
expectTrace(['cc3', 'bb3', 'aa3'], stacktrace); // //# withtraceparameter: continued
}
}
@pragma("vm:entry-point") // Prevent obfuscation
bb3() => cc3();
@pragma("vm:entry-point") // Prevent obfuscation
cc3() {
try {
dd3();
} catch (e) {
throw e;
}
}
@pragma("vm:entry-point") // Prevent obfuscation
dd3() => ee3();
@pragma("vm:entry-point") // Prevent obfuscation
ee3() {
try {
ff3();
} catch (_) {
rethrow;
}
}
@pragma("vm:entry-point") // Prevent obfuscation
ff3() => gg3();
@pragma("vm:entry-point") // Prevent obfuscation
gg3() => throw new SubclassOfError();
expectTrace(functionNames, stacktrace) {
// Note we don't expect functionNames to cover the whole trace, only the
// top portion, because the frames below main are an implementation detail.
var traceLines = stacktrace.toString().split('\n');
var expectedIndex = 0;
var actualIndex = 0;
print(stacktrace);
print(functionNames);
while (expectedIndex < functionNames.length) {
var expected = functionNames[expectedIndex];
var actual = traceLines[actualIndex];
if (actual.indexOf(expected) == -1) {
if (expectedIndex == 0) {
actualIndex++; // Skip over some helper frames at the top
} else {
throw "Expected: $expected actual: $actual";
}
} else {
actualIndex++;
expectedIndex++;
}
}
}
main() {
aa1();
aa2();
aa3();
}

View file

@ -0,0 +1,167 @@
// Copyright (c) 2014, 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.
class NotASubclassOfError {}
fail() => throw "Fail";
// == Rethrow, skipping through typed handlers. ==
@pragma("vm:entry-point")
aa1() {
try {
bb1();
fail();
} catch (exception, stacktrace) {
expectTrace(['gg1', 'ff1', 'ee1', 'dd1', 'cc1', 'bb1', 'aa1'], stacktrace);
}
}
@pragma("vm:entry-point")
bb1() => cc1();
@pragma("vm:entry-point")
cc1() {
try {
dd1();
} on String catch (_) {
fail();
} on int catch (_) {
fail();
}
}
@pragma("vm:entry-point")
dd1() => ee1();
@pragma("vm:entry-point")
ee1() {
try {
ff1();
} catch (_) {
rethrow;
}
}
@pragma("vm:entry-point")
ff1() => gg1();
@pragma("vm:entry-point")
gg1() => throw new NotASubclassOfError();
// == Rethrow, rethrow again in typed handler. ==
@pragma("vm:entry-point")
aa2() {
try {
bb2();
fail();
} catch (exception, stacktrace) {
expectTrace(['gg2', 'ff2', 'ee2', 'dd2', 'cc2', 'bb2', 'aa2'], stacktrace);
}
}
@pragma("vm:entry-point")
bb2() => cc2();
@pragma("vm:entry-point")
cc2() {
try {
dd2();
} on NotASubclassOfError catch (_) {
rethrow;
} on int catch (_) {
fail();
}
}
@pragma("vm:entry-point")
dd2() => ee2();
@pragma("vm:entry-point")
ee2() {
try {
ff2();
} catch (e) {
rethrow;
}
}
@pragma("vm:entry-point")
ff2() => gg2();
@pragma("vm:entry-point")
gg2() => throw new NotASubclassOfError();
// == Rethrow, with intervening catch without a trace parameter.
@pragma("vm:entry-point")
aa3() {
try {
bb3();
fail();
} catch (exception, stacktrace) {
expectTrace(['cc3', 'bb3', 'aa3'], stacktrace);
}
}
@pragma("vm:entry-point")
bb3() => cc3();
@pragma("vm:entry-point")
cc3() {
try {
dd3();
} catch (e) {
throw e;
}
}
@pragma("vm:entry-point")
dd3() => ee3();
@pragma("vm:entry-point")
ee3() {
try {
ff3();
} catch (e) {
rethrow;
}
}
@pragma("vm:entry-point")
ff3() => gg3();
@pragma("vm:entry-point")
gg3() => throw new NotASubclassOfError();
expectTrace(functionNames, stacktrace) {
// Note we don't expect functionNames to cover the whole trace, only the
// top portion, because the frames below main are an implementation detail.
var traceLines = stacktrace.toString().split('\n');
var expectedIndex = 0;
var actualIndex = 0;
print(stacktrace);
print(functionNames);
while (expectedIndex < functionNames.length) {
var expected = functionNames[expectedIndex];
var actual = traceLines[actualIndex];
if (actual.indexOf(expected) == -1) {
if (expectedIndex == 0) {
actualIndex++; // Skip over some helper frames at the top
} else {
throw "Expected: $expected actual: $actual";
}
} else {
actualIndex++;
expectedIndex++;
}
}
}
main() {
aa1();
aa2();
aa3();
}

View file

@ -0,0 +1,138 @@
// 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.
// Dart test program for testing throw statement
import "package:expect/expect.dart";
class MyException {
const MyException(String message) : message_ = message;
final String message_;
}
class Helper {
static int f1(int i) {
try {
i = func();
i = 10;
} on MyException catch (exception, stacktrace) {
i = 50;
print(exception.message_);
Expect.isNotNull(stacktrace);
print(stacktrace);
}
try {
int j;
i = func1();
i = 200;
} on MyException catch (exception, stacktrace) {
i = 50;
print(exception.message_);
Expect.isNotNull(stacktrace);
print(stacktrace);
}
try {
int j;
i = func2();
i = 200;
} on MyException catch (exception, stacktrace) {
i = 50;
print(exception.message_);
Expect.isNotNull(stacktrace);
print(stacktrace);
} finally {
i = i + 800;
}
return i;
}
static int func() {
int i = 0;
while (i < 10) {
i++;
}
if (i > 0) {
throw new MyException("Exception Test for stack trace being printed");
}
return 10;
}
static int func1() {
try {
func();
} on MyException catch (exception) {
throw new MyException("Exception Test for stack trace being printed");
;
}
return 10;
}
static int func2() {
try {
func();
} on MyException catch (exception) {
rethrow;
}
return 10;
}
}
class StackTraceTest {
static testMain() {
Expect.equals(850, Helper.f1(1));
}
}
// Test that the full stack trace is generated for rethrow.
class RethrowStacktraceTest {
var config = 0;
@pragma("vm:entry-point") // Prevent obfuscation
issue12940() {
throw "Progy";
}
b() {
issue12940();
}
c() {
if (config == 0) {
try {
b();
} catch (e) {
rethrow;
}
} else {
try {
b();
} catch (e, s) {
rethrow;
}
}
}
d() {
c();
}
testBoth() {
for (config = 0; config < 2; config++) {
try {
d();
} catch (e, s) {
Expect.isTrue(s.toString().contains("issue12940"));
}
}
}
static testMain() {
var test = new RethrowStacktraceTest();
test.testBoth();
}
}
main() {
StackTraceTest.testMain();
RethrowStacktraceTest.testMain();
}