OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library trace_test; | 5 library trace_test; |
6 | 6 |
| 7 import 'dart:io'; |
| 8 |
7 import 'package:pathos/path.dart' as path; | 9 import 'package:pathos/path.dart' as path; |
8 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
9 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
10 | 12 |
11 String getStackTraceString() { | 13 String getStackTraceString() { |
12 try { | 14 try { |
13 throw ''; | 15 throw ''; |
14 } catch (_, stackTrace) { | 16 } catch (_, stackTrace) { |
15 return stackTrace.toString(); | 17 return stackTrace.toString(); |
16 } | 18 } |
17 } | 19 } |
18 | 20 |
19 StackTrace getStackTraceObject() { | 21 StackTrace getStackTraceObject() { |
20 try { | 22 try { |
21 throw ''; | 23 throw ''; |
22 } catch (_, stackTrace) { | 24 } catch (_, stackTrace) { |
23 return stackTrace; | 25 return stackTrace; |
24 } | 26 } |
25 } | 27 } |
26 | 28 |
27 Trace getCurrentTrace([int level]) => new Trace.current(level); | 29 Trace getCurrentTrace([int level]) => new Trace.current(level); |
28 | 30 |
29 Trace nestedGetCurrentTrace(int level) => getCurrentTrace(level); | 31 Trace nestedGetCurrentTrace(int level) => getCurrentTrace(level); |
30 | 32 |
31 void main() { | 33 void main() { |
32 group('.parse', () { | 34 test('parses a stack trace correctly', () { |
33 test('.parse parses a VM stack trace correctly', () { | 35 var trace = new Trace.parse(''' |
34 var trace = new Trace.parse( | 36 #0 Foo._bar (file:///home/nweiz/code/stuff.dart:42:21) |
35 '#0 Foo._bar (file:///home/nweiz/code/stuff.dart:42:21)\n' | 37 #1 zip.<anonymous closure>.zap (dart:async/future.dart:0:2) |
36 '#1 zip.<anonymous closure>.zap (dart:async/future.dart:0:2)\n' | 38 #2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.dart:1:100) |
37 '#2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.' | 39 '''); |
38 'dart:1:100)'); | |
39 | 40 |
40 expect(trace.frames[0].uri, | 41 expect(trace.frames[0].uri, |
41 equals(Uri.parse("file:///home/nweiz/code/stuff.dart"))); | 42 equals(Uri.parse("file:///home/nweiz/code/stuff.dart"))); |
42 expect(trace.frames[1].uri, equals(Uri.parse("dart:async/future.dart"))); | 43 expect(trace.frames[1].uri, equals(Uri.parse("dart:async/future.dart"))); |
43 expect(trace.frames[2].uri, | 44 expect(trace.frames[2].uri, |
44 equals(Uri.parse("http://pub.dartlang.org/thing.dart"))); | 45 equals(Uri.parse("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>')); |
45 }); | 70 }); |
46 | 71 |
47 test('parses a V8 stack trace correctly', () { | 72 test('at level 0 returns a trace starting at the current frame', () { |
48 var trace = new Trace.parse( | 73 var trace = new Trace.current(0); |
49 'Error\n' | 74 expect(trace.frames.first.member, equals('main.<fn>.<fn>')); |
50 ' at Foo._bar (http://pub.dartlang.org/stuff.js:42:21)\n' | |
51 ' at http://pub.dartlang.org/stuff.js:0:2\n' | |
52 ' at zip.<anonymous>.zap ' | |
53 '(http://pub.dartlang.org/thing.js:1:100)'); | |
54 | |
55 expect(trace.frames[0].uri, | |
56 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
57 expect(trace.frames[1].uri, | |
58 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
59 expect(trace.frames[2].uri, | |
60 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
61 }); | 75 }); |
62 | 76 |
63 test('parses a Firefox stack trace correctly', () { | 77 test('at level 1 returns a trace starting at the parent frame', () { |
64 var trace = new Trace.parse( | 78 var trace = getCurrentTrace(1); |
65 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' | 79 expect(trace.frames.first.member, equals('main.<fn>.<fn>')); |
66 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | 80 }); |
67 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); | |
68 | 81 |
69 expect(trace.frames[0].uri, | 82 test('at level 2 returns a trace starting at the grandparent frame', () { |
70 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | 83 var trace = nestedGetCurrentTrace(2); |
71 expect(trace.frames[1].uri, | 84 expect(trace.frames.first.member, equals('main.<fn>.<fn>')); |
72 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | 85 }); |
73 expect(trace.frames[2].uri, | |
74 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
75 | 86 |
76 trace = new Trace.parse( | 87 test('throws an ArgumentError for negative levels', () { |
77 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | 88 expect(() => new Trace.current(-1), throwsArgumentError); |
78 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' | |
79 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); | |
80 | |
81 expect(trace.frames[0].uri, | |
82 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
83 expect(trace.frames[1].uri, | |
84 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
85 expect(trace.frames[2].uri, | |
86 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
87 | |
88 trace = new Trace.parse( | |
89 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1\n' | |
90 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | |
91 'Foo._bar@http://pub.dartlang.org/stuff.js:42'); | |
92 | |
93 expect(trace.frames[0].uri, | |
94 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
95 expect(trace.frames[1].uri, | |
96 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
97 expect(trace.frames[2].uri, | |
98 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
99 }); | 89 }); |
100 }); | 90 }); |
101 | 91 |
102 test('.toString() nicely formats the stack trace', () { | 92 test('.toString() nicely formats the stack trace', () { |
103 var trace = new Trace.parse(''' | 93 var trace = new Trace.parse(''' |
104 #0 Foo._bar (foo/bar.dart:42:21) | 94 #0 Foo._bar (foo/bar.dart:42:21) |
105 #1 zip.<anonymous closure>.zap (dart:async/future.dart:0:2) | 95 #1 zip.<anonymous closure>.zap (dart:async/future.dart:0:2) |
106 #2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.dart:1:100) | 96 #2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.dart:1:100) |
107 '''); | 97 '''); |
108 | 98 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 143 |
154 var folded = trace.foldFrames((frame) => frame.member.startsWith('foo')); | 144 var folded = trace.foldFrames((frame) => frame.member.startsWith('foo')); |
155 expect(folded.toString(), equals(''' | 145 expect(folded.toString(), equals(''' |
156 foo.dart 42:21 notFoo | 146 foo.dart 42:21 notFoo |
157 foo.dart 1:100 fooBottom | 147 foo.dart 1:100 fooBottom |
158 bar.dart 10:20 alsoNotFoo | 148 bar.dart 10:20 alsoNotFoo |
159 dart:async-patch/future.dart 9:11 fooBottom | 149 dart:async-patch/future.dart 9:11 fooBottom |
160 ''')); | 150 ''')); |
161 }); | 151 }); |
162 } | 152 } |
OLD | NEW |