OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// This file tests stack_trace's ability to parse live stack traces. It's a |
| 6 /// dual of dartium_test.dart, since method names can differ somewhat from |
| 7 /// platform to platform. No similar file exists for dart2js since the specific |
| 8 /// method names there are implementation details. |
| 9 @TestOn('vm') |
| 10 |
| 11 import 'package:path/path.dart' as path; |
| 12 import 'package:stack_trace/stack_trace.dart'; |
| 13 import 'package:test/test.dart'; |
| 14 |
| 15 String getStackTraceString() { |
| 16 try { |
| 17 throw ''; |
| 18 } catch (_, stackTrace) { |
| 19 return stackTrace.toString(); |
| 20 } |
| 21 } |
| 22 |
| 23 StackTrace getStackTraceObject() { |
| 24 try { |
| 25 throw ''; |
| 26 } catch (_, stackTrace) { |
| 27 return stackTrace; |
| 28 } |
| 29 } |
| 30 |
| 31 Frame getCaller([int level]) { |
| 32 if (level == null) return new Frame.caller(); |
| 33 return new Frame.caller(level); |
| 34 } |
| 35 |
| 36 Frame nestedGetCaller(int level) => getCaller(level); |
| 37 |
| 38 Trace getCurrentTrace([int level]) => new Trace.current(level); |
| 39 |
| 40 Trace nestedGetCurrentTrace(int level) => getCurrentTrace(level); |
| 41 |
| 42 void main() { |
| 43 group('Trace', () { |
| 44 test('.parse parses a real stack trace correctly', () { |
| 45 var string = getStackTraceString(); |
| 46 var trace = new Trace.parse(string); |
| 47 expect(path.url.basename(trace.frames.first.uri.path), |
| 48 equals('vm_test.dart')); |
| 49 expect(trace.frames.first.member, equals('getStackTraceString')); |
| 50 }); |
| 51 |
| 52 test('converts from a native stack trace correctly', () { |
| 53 var trace = new Trace.from(getStackTraceObject()); |
| 54 expect(path.url.basename(trace.frames.first.uri.path), |
| 55 equals('vm_test.dart')); |
| 56 expect(trace.frames.first.member, equals('getStackTraceObject')); |
| 57 }); |
| 58 |
| 59 test('.from handles a stack overflow trace correctly', () { |
| 60 overflow() => overflow(); |
| 61 |
| 62 var trace; |
| 63 try { |
| 64 overflow(); |
| 65 } catch (_, stackTrace) { |
| 66 trace = new Trace.from(stackTrace); |
| 67 } |
| 68 |
| 69 expect(trace.frames.first.member, equals('main.<fn>.<fn>.overflow')); |
| 70 }); |
| 71 |
| 72 group('.current()', () { |
| 73 test('with no argument returns a trace starting at the current frame', |
| 74 () { |
| 75 var trace = new Trace.current(); |
| 76 expect(trace.frames.first.member, equals('main.<fn>.<fn>.<fn>')); |
| 77 }); |
| 78 |
| 79 test('at level 0 returns a trace starting at the current frame', () { |
| 80 var trace = new Trace.current(0); |
| 81 expect(trace.frames.first.member, equals('main.<fn>.<fn>.<fn>')); |
| 82 }); |
| 83 |
| 84 test('at level 1 returns a trace starting at the parent frame', () { |
| 85 var trace = getCurrentTrace(1); |
| 86 expect(trace.frames.first.member, equals('main.<fn>.<fn>.<fn>')); |
| 87 }); |
| 88 |
| 89 test('at level 2 returns a trace starting at the grandparent frame', () { |
| 90 var trace = nestedGetCurrentTrace(2); |
| 91 expect(trace.frames.first.member, equals('main.<fn>.<fn>.<fn>')); |
| 92 }); |
| 93 |
| 94 test('throws an ArgumentError for negative levels', () { |
| 95 expect(() => new Trace.current(-1), throwsArgumentError); |
| 96 }); |
| 97 }); |
| 98 }); |
| 99 |
| 100 group('Frame.caller()', () { |
| 101 test('with no argument returns the parent frame', () { |
| 102 expect(getCaller().member, equals('main.<fn>.<fn>')); |
| 103 }); |
| 104 |
| 105 test('at level 0 returns the current frame', () { |
| 106 expect(getCaller(0).member, equals('getCaller')); |
| 107 }); |
| 108 |
| 109 test('at level 1 returns the current frame', () { |
| 110 expect(getCaller(1).member, equals('main.<fn>.<fn>')); |
| 111 }); |
| 112 |
| 113 test('at level 2 returns the grandparent frame', () { |
| 114 expect(nestedGetCaller(2).member, equals('main.<fn>.<fn>')); |
| 115 }); |
| 116 |
| 117 test('throws an ArgumentError for negative levels', () { |
| 118 expect(() => new Frame.caller(-1), throwsArgumentError); |
| 119 }); |
| 120 }); |
| 121 } |
OLD | NEW |