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 library trace_test; |
| 6 |
| 7 import 'dart:uri'; |
| 8 |
| 9 import 'package:pathos/path.dart' as path; |
| 10 import 'package:stack_trace/stack_trace.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 String getStackTraceString() { |
| 14 try { |
| 15 throw ''; |
| 16 } catch (_, stackTrace) { |
| 17 return stackTrace.toString(); |
| 18 } |
| 19 } |
| 20 |
| 21 StackTrace getStackTraceObject() { |
| 22 try { |
| 23 throw ''; |
| 24 } catch (_, stackTrace) { |
| 25 return stackTrace; |
| 26 } |
| 27 } |
| 28 |
| 29 Trace getCurrentTrace([int level]) => new Trace.current(level); |
| 30 |
| 31 Trace nestedGetCurrentTrace(int level) => getCurrentTrace(level); |
| 32 |
| 33 void main() { |
| 34 test('parses a stack trace correctly', () { |
| 35 var trace = new Trace.parse(''' |
| 36 #0 Foo._bar (file:///home/nweiz/code/stuff.dart:42:21) |
| 37 #1 zip.<anonymous closure>.zap (dart:async:0:2) |
| 38 #2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.dart:1:100) |
| 39 '''); |
| 40 |
| 41 expect(trace.frames[0].uri, |
| 42 equals(new Uri.fromString("file:///home/nweiz/code/stuff.dart"))); |
| 43 expect(trace.frames[1].uri, equals(new Uri.fromString("dart:async"))); |
| 44 expect(trace.frames[2].uri, |
| 45 equals(new Uri.fromString("http://pub.dartlang.org/thing.dart"))); |
| 46 }); |
| 47 |
| 48 test('parses a real stack trace correctly', () { |
| 49 var trace = new Trace.parse(getStackTraceString()); |
| 50 // TODO(nweiz): use URL-style paths when such a thing exists. |
| 51 var builder = new path.Builder(style: path.Style.posix); |
| 52 expect(builder.basename(trace.frames.first.uri.path), |
| 53 equals('trace_test.dart')); |
| 54 expect(trace.frames.first.member, equals('getStackTraceString')); |
| 55 }); |
| 56 |
| 57 test('converts from a native stack trace correctly', () { |
| 58 var trace = new Trace.from(getStackTraceObject()); |
| 59 // TODO(nweiz): use URL-style paths when such a thing exists. |
| 60 var builder = new path.Builder(style: path.Style.posix); |
| 61 expect(builder.basename(trace.frames.first.uri.path), |
| 62 equals('trace_test.dart')); |
| 63 expect(trace.frames.first.member, equals('getStackTraceObject')); |
| 64 }); |
| 65 |
| 66 group('.current()', () { |
| 67 test('with no argument returns a trace starting at the current frame', () { |
| 68 var trace = new Trace.current(); |
| 69 expect(trace.frames.first.member, equals('main.<fn>.<fn>')); |
| 70 }); |
| 71 |
| 72 test('at level 0 returns a trace starting at the current frame', () { |
| 73 var trace = new Trace.current(0); |
| 74 expect(trace.frames.first.member, equals('main.<fn>.<fn>')); |
| 75 }); |
| 76 |
| 77 test('at level 1 returns a trace starting at the parent frame', () { |
| 78 var trace = getCurrentTrace(1); |
| 79 expect(trace.frames.first.member, equals('main.<fn>.<fn>')); |
| 80 }); |
| 81 |
| 82 test('at level 2 returns a trace starting at the grandparent frame', () { |
| 83 var trace = nestedGetCurrentTrace(2); |
| 84 expect(trace.frames.first.member, equals('main.<fn>.<fn>')); |
| 85 }); |
| 86 |
| 87 test('throws an ArgumentError for negative levels', () { |
| 88 expect(() => new Trace.current(-1), throwsArgumentError); |
| 89 }); |
| 90 }); |
| 91 |
| 92 test('.toString() nicely formats the stack trace', () { |
| 93 var absolute = path.absolute(path.join('foo', 'bar.dart')); |
| 94 var trace = new Trace.parse(''' |
| 95 #0 Foo._bar (file://$absolute:42:21) |
| 96 #1 zip.<anonymous closure>.zap (dart:async:0:2) |
| 97 #2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.dart:1:100) |
| 98 '''); |
| 99 |
| 100 expect(trace.toString(), equals(''' |
| 101 ${path.join('foo', 'bar.dart')} 42:21 Foo._bar |
| 102 dart:async zip.<fn>.zap |
| 103 http://pub.dartlang.org/thing.dart 1:100 zip.<fn>.zap |
| 104 ''')); |
| 105 }); |
| 106 |
| 107 test('.stackTrace forwards to .toString()', () { |
| 108 var trace = new Trace.current(); |
| 109 expect(trace.stackTrace, equals(trace.toString())); |
| 110 }); |
| 111 |
| 112 test('.fullStackTrace forwards to .toString()', () { |
| 113 var trace = new Trace.current(); |
| 114 expect(trace.fullStackTrace, equals(trace.toString())); |
| 115 }); |
| 116 |
| 117 test('.terse folds core frames together bottom-up', () { |
| 118 var trace = new Trace.parse(''' |
| 119 #0 notCore (foo.dart:42:21) |
| 120 #1 top (dart:async:0:2) |
| 121 #2 bottom (dart:core:1:100) |
| 122 #3 alsoNotCore (bar.dart:10:20) |
| 123 #4 top (dart:io:5:10) |
| 124 #5 bottom (dart:async-patch:9:11) |
| 125 '''); |
| 126 |
| 127 expect(trace.terse.toString(), equals(''' |
| 128 foo.dart 42:21 notCore |
| 129 dart:core bottom |
| 130 bar.dart 10:20 alsoNotCore |
| 131 dart:async bottom |
| 132 ''')); |
| 133 }); |
| 134 } |
OLD | NEW |