Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: pkg/stack_trace/test/frame_test.dart

Issue 13102003: Add a library for manipulating stack traces. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/stack_trace/pubspec.yaml ('k') | pkg/stack_trace/test/trace_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 frame_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 getStackFrame() {
14 try {
15 throw '';
16 } catch (_, stackTrace) {
17 return stackTrace.toString().split("\n").first;
18 }
19 }
20
21 Frame getCaller([int level]) {
22 if (level == null) return new Frame.caller();
23 return new Frame.caller(level);
24 }
25
26 Frame nestedGetCaller(int level) => getCaller(level);
27
28 void main() {
29 test('parses a stack frame correctly', () {
30 var frame = new Frame.parse("#1 Foo._bar "
31 "(file:///home/nweiz/code/stuff.dart:42:21)");
32 expect(frame.uri,
33 equals(new Uri.fromString("file:///home/nweiz/code/stuff.dart")));
34 expect(frame.line, equals(42));
35 expect(frame.column, equals(21));
36 expect(frame.member, equals('Foo._bar'));
37 });
38
39 test('parses a real stack frame correctly', () {
40 var frame = new Frame.parse(getStackFrame());
41 // TODO(nweiz): use URL-style paths when such a thing exists.
42 var builder = new path.Builder(style: path.Style.posix);
43 expect(builder.basename(frame.uri.path), equals('frame_test.dart'));
44 expect(frame.line, equals(15));
45 expect(frame.column, equals(5));
46 expect(frame.member, equals('getStackFrame'));
47 });
48
49 test('converts "<anonymous closure>" to "<fn>"', () {
50 String parsedMember(String member) =>
51 new Frame.parse('#0 $member (foo:0:0)').member;
52
53 expect(parsedMember('Foo.<anonymous closure>'), equals('Foo.<fn>'));
54 expect(parsedMember('<anonymous closure>.<anonymous closure>.bar'),
55 equals('<fn>.<fn>.bar'));
56 });
57
58 test('throws a FormatException for malformed frames', () {
59 expect(() => new Frame.parse(''), throwsFormatException);
60 expect(() => new Frame.parse('#1'), throwsFormatException);
61 expect(() => new Frame.parse('#1 Foo'), throwsFormatException);
62 expect(() => new Frame.parse('#1 Foo (dart:async)'),
63 throwsFormatException);
64 expect(() => new Frame.parse('#1 Foo (dart:async:10)'),
65 throwsFormatException);
66 expect(() => new Frame.parse('#1 (dart:async:10:15)'),
67 throwsFormatException);
68 expect(() => new Frame.parse('Foo (dart:async:10:15)'),
69 throwsFormatException);
70 });
71
72 test('only considers dart URIs to be core', () {
73 bool isCore(String library) =>
74 new Frame.parse('#0 Foo ($library:0:0)').isCore;
75
76 expect(isCore('dart:core'), isTrue);
77 expect(isCore('dart:async'), isTrue);
78 expect(isCore('bart:core'), isFalse);
79 expect(isCore('sdart:core'), isFalse);
80 expect(isCore('darty:core'), isFalse);
81 });
82
83 group('.caller()', () {
84 test('with no argument returns the parent frame', () {
85 expect(getCaller().member, equals('main.<fn>.<fn>'));
86 });
87
88 test('at level 0 returns the current frame', () {
89 expect(getCaller(0).member, equals('getCaller'));
90 });
91
92 test('at level 1 returns the current frame', () {
93 expect(getCaller(1).member, equals('main.<fn>.<fn>'));
94 });
95
96 test('at level 2 returns the grandparent frame', () {
97 expect(nestedGetCaller(2).member, equals('main.<fn>.<fn>'));
98 });
99
100 test('throws an ArgumentError for negative levels', () {
101 expect(() => new Frame.caller(-1), throwsArgumentError);
102 });
103 });
104
105 group('.library', () {
106 test('returns the URI string for non-file URIs', () {
107 expect(new Frame.parse('#0 Foo (dart:async:0:0)').library,
108 equals('dart:async'));
109 expect(new Frame.parse('#0 Foo '
110 '(http://dartlang.org/stuff/thing.dart:0:0)').library,
111 equals('http://dartlang.org/stuff/thing.dart'));
112 });
113
114 test('returns the relative path for file URIs', () {
115 var absolute = path.absolute(path.join('foo', 'bar.dart'));
116 expect(new Frame.parse('#0 Foo (file://$absolute:0:0)').library,
117 equals(path.join('foo', 'bar.dart')));
118 });
119 });
120
121 group('.location', () {
122 test('returns the library and line/column numbers for non-core '
123 'libraries', () {
124 expect(new Frame.parse('#0 Foo '
125 '(http://dartlang.org/thing.dart:5:10)').location,
126 equals('http://dartlang.org/thing.dart 5:10'));
127 var absolute = path.absolute(path.join('foo', 'bar.dart'));
128 expect(new Frame.parse('#0 Foo (file://$absolute:1:2)').location,
129 equals('${path.join('foo', 'bar.dart')} 1:2'));
130 });
131
132 test('just returns the library for core libraries', () {
133 expect(new Frame.parse('#0 Foo (dart:core:5:10)').location,
134 equals('dart:core'));
135 expect(new Frame.parse('#0 Foo (dart:async-patch:1:2)').location,
136 equals('dart:async-patch'));
137 });
138 });
139
140 group('.toString()', () {
141 test('returns the library and line/column numbers for non-core '
142 'libraries', () {
143 expect(new Frame.parse('#0 Foo (http://dartlang.org/thing.dart:5:10)')
144 .toString(),
145 equals('http://dartlang.org/thing.dart 5:10 in Foo'));
146 });
147
148 test('just returns the library for core libraries', () {
149 expect(new Frame.parse('#0 Foo (dart:core:5:10)').toString(),
150 equals('dart:core in Foo'));
151 });
152
153 test('converts "<anonymous closure>" to "<fn>"', () {
154 expect(new Frame.parse('#0 Foo.<anonymous closure> (dart:core:5:10)')
155 .toString(),
156 equals('dart:core in Foo.<fn>'));
157 });
158 });
159 }
OLDNEW
« no previous file with comments | « pkg/stack_trace/pubspec.yaml ('k') | pkg/stack_trace/test/trace_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698