| 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 'package:path/path.dart' as path; | |
| 8 import 'package:stack_trace/stack_trace.dart'; | |
| 9 import 'package:unittest/unittest.dart'; | |
| 10 | |
| 11 String getStackTraceString() { | |
| 12 try { | |
| 13 throw ''; | |
| 14 } catch (_, stackTrace) { | |
| 15 return stackTrace.toString(); | |
| 16 } | |
| 17 } | |
| 18 | |
| 19 StackTrace getStackTraceObject() { | |
| 20 try { | |
| 21 throw ''; | |
| 22 } catch (_, stackTrace) { | |
| 23 return stackTrace; | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 Trace getCurrentTrace([int level]) => new Trace.current(level); | |
| 28 | |
| 29 Trace nestedGetCurrentTrace(int level) => getCurrentTrace(level); | |
| 30 | |
| 31 void main() { | |
| 32 // This just shouldn't crash. | |
| 33 test('a native stack trace is parseable', () => new Trace.current()); | |
| 34 | |
| 35 group('.parse', () { | |
| 36 test('.parse parses a VM stack trace correctly', () { | |
| 37 var trace = new Trace.parse( | |
| 38 '#0 Foo._bar (file:///home/nweiz/code/stuff.dart:42:21)\n' | |
| 39 '#1 zip.<anonymous closure>.zap (dart:async/future.dart:0:2)\n' | |
| 40 '#2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.' | |
| 41 'dart:1:100)'); | |
| 42 | |
| 43 expect(trace.frames[0].uri, | |
| 44 equals(Uri.parse("file:///home/nweiz/code/stuff.dart"))); | |
| 45 expect(trace.frames[1].uri, equals(Uri.parse("dart:async/future.dart"))); | |
| 46 expect(trace.frames[2].uri, | |
| 47 equals(Uri.parse("http://pub.dartlang.org/thing.dart"))); | |
| 48 }); | |
| 49 | |
| 50 test('parses a V8 stack trace correctly', () { | |
| 51 var trace = new Trace.parse( | |
| 52 'Error\n' | |
| 53 ' at Foo._bar (http://pub.dartlang.org/stuff.js:42:21)\n' | |
| 54 ' at http://pub.dartlang.org/stuff.js:0:2\n' | |
| 55 ' at zip.<anonymous>.zap ' | |
| 56 '(http://pub.dartlang.org/thing.js:1:100)'); | |
| 57 | |
| 58 expect(trace.frames[0].uri, | |
| 59 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 60 expect(trace.frames[1].uri, | |
| 61 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 62 expect(trace.frames[2].uri, | |
| 63 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 64 | |
| 65 trace = new Trace.parse( | |
| 66 "Exception: foo\n" | |
| 67 ' at Foo._bar (http://pub.dartlang.org/stuff.js:42:21)\n' | |
| 68 ' at http://pub.dartlang.org/stuff.js:0:2\n' | |
| 69 ' at zip.<anonymous>.zap ' | |
| 70 '(http://pub.dartlang.org/thing.js:1:100)'); | |
| 71 | |
| 72 expect(trace.frames[0].uri, | |
| 73 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 74 expect(trace.frames[1].uri, | |
| 75 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 76 expect(trace.frames[2].uri, | |
| 77 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 78 | |
| 79 trace = new Trace.parse( | |
| 80 'Exception: foo\n' | |
| 81 ' bar\n' | |
| 82 ' at Foo._bar (http://pub.dartlang.org/stuff.js:42:21)\n' | |
| 83 ' at http://pub.dartlang.org/stuff.js:0:2\n' | |
| 84 ' at zip.<anonymous>.zap ' | |
| 85 '(http://pub.dartlang.org/thing.js:1:100)'); | |
| 86 | |
| 87 expect(trace.frames[0].uri, | |
| 88 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 89 expect(trace.frames[1].uri, | |
| 90 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 91 expect(trace.frames[2].uri, | |
| 92 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 93 }); | |
| 94 | |
| 95 test('parses a Firefox/Safari stack trace correctly', () { | |
| 96 var trace = new Trace.parse( | |
| 97 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' | |
| 98 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | |
| 99 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); | |
| 100 | |
| 101 expect(trace.frames[0].uri, | |
| 102 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 103 expect(trace.frames[1].uri, | |
| 104 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 105 expect(trace.frames[2].uri, | |
| 106 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 107 | |
| 108 trace = new Trace.parse( | |
| 109 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | |
| 110 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' | |
| 111 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); | |
| 112 | |
| 113 expect(trace.frames[0].uri, | |
| 114 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 115 expect(trace.frames[1].uri, | |
| 116 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 117 expect(trace.frames[2].uri, | |
| 118 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 119 | |
| 120 trace = new Trace.parse( | |
| 121 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1\n' | |
| 122 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | |
| 123 'Foo._bar@http://pub.dartlang.org/stuff.js:42'); | |
| 124 | |
| 125 expect(trace.frames[0].uri, | |
| 126 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 127 expect(trace.frames[1].uri, | |
| 128 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 129 expect(trace.frames[2].uri, | |
| 130 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 131 }); | |
| 132 | |
| 133 test('parses a Firefox/Safari stack trace containing native code correctly', | |
| 134 () { | |
| 135 var trace = new Trace.parse( | |
| 136 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' | |
| 137 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | |
| 138 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1\n' | |
| 139 '[native code]'); | |
| 140 | |
| 141 expect(trace.frames[0].uri, | |
| 142 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 143 expect(trace.frames[1].uri, | |
| 144 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 145 expect(trace.frames[2].uri, | |
| 146 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 147 expect(trace.frames.length, equals(3)); | |
| 148 }); | |
| 149 | |
| 150 test('parses a Firefox/Safari stack trace without a method name correctly', | |
| 151 () { | |
| 152 var trace = new Trace.parse( | |
| 153 'http://pub.dartlang.org/stuff.js:42\n' | |
| 154 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | |
| 155 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); | |
| 156 | |
| 157 expect(trace.frames[0].uri, | |
| 158 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 159 expect(trace.frames[0].member, equals('<fn>')); | |
| 160 expect(trace.frames[1].uri, | |
| 161 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 162 expect(trace.frames[2].uri, | |
| 163 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 164 }); | |
| 165 | |
| 166 test('parses a Firefox/Safari stack trace with an empty line correctly', | |
| 167 () { | |
| 168 var trace = new Trace.parse( | |
| 169 'Foo._bar@http://pub.dartlang.org/stuff.js:42\n' | |
| 170 '\n' | |
| 171 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | |
| 172 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); | |
| 173 | |
| 174 expect(trace.frames[0].uri, | |
| 175 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 176 expect(trace.frames[1].uri, | |
| 177 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 178 expect(trace.frames[2].uri, | |
| 179 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 180 }); | |
| 181 | |
| 182 test('parses a Firefox/Safari stack trace with a column number correctly', | |
| 183 () { | |
| 184 var trace = new Trace.parse( | |
| 185 'Foo._bar@http://pub.dartlang.org/stuff.js:42:2\n' | |
| 186 'zip/<@http://pub.dartlang.org/stuff.js:0\n' | |
| 187 'zip.zap(12, "@)()/<")@http://pub.dartlang.org/thing.js:1'); | |
| 188 | |
| 189 expect(trace.frames[0].uri, | |
| 190 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 191 expect(trace.frames[0].line, equals(42)); | |
| 192 expect(trace.frames[0].column, equals(2)); | |
| 193 expect(trace.frames[1].uri, | |
| 194 equals(Uri.parse("http://pub.dartlang.org/stuff.js"))); | |
| 195 expect(trace.frames[2].uri, | |
| 196 equals(Uri.parse("http://pub.dartlang.org/thing.js"))); | |
| 197 }); | |
| 198 | |
| 199 test('parses a package:stack_trace stack trace correctly', () { | |
| 200 var trace = new Trace.parse( | |
| 201 'http://dartlang.org/foo/bar.dart 10:11 Foo.<fn>.bar\n' | |
| 202 'http://dartlang.org/foo/baz.dart Foo.<fn>.bar'); | |
| 203 | |
| 204 expect(trace.frames[0].uri, | |
| 205 equals(Uri.parse("http://dartlang.org/foo/bar.dart"))); | |
| 206 expect(trace.frames[1].uri, | |
| 207 equals(Uri.parse("http://dartlang.org/foo/baz.dart"))); | |
| 208 }); | |
| 209 | |
| 210 test('parses a package:stack_trace stack chain correctly', () { | |
| 211 var trace = new Trace.parse( | |
| 212 'http://dartlang.org/foo/bar.dart 10:11 Foo.<fn>.bar\n' | |
| 213 'http://dartlang.org/foo/baz.dart Foo.<fn>.bar\n' | |
| 214 '===== asynchronous gap ===========================\n' | |
| 215 'http://dartlang.org/foo/bang.dart 10:11 Foo.<fn>.bar\n' | |
| 216 'http://dartlang.org/foo/quux.dart Foo.<fn>.bar'); | |
| 217 | |
| 218 expect(trace.frames[0].uri, | |
| 219 equals(Uri.parse("http://dartlang.org/foo/bar.dart"))); | |
| 220 expect(trace.frames[1].uri, | |
| 221 equals(Uri.parse("http://dartlang.org/foo/baz.dart"))); | |
| 222 expect(trace.frames[2].uri, | |
| 223 equals(Uri.parse("http://dartlang.org/foo/bang.dart"))); | |
| 224 expect(trace.frames[3].uri, | |
| 225 equals(Uri.parse("http://dartlang.org/foo/quux.dart"))); | |
| 226 }); | |
| 227 | |
| 228 test('parses a real package:stack_trace stack trace correctly', () { | |
| 229 var traceString = new Trace.current().toString(); | |
| 230 expect(new Trace.parse(traceString).toString(), equals(traceString)); | |
| 231 }); | |
| 232 | |
| 233 test('parses an empty string correctly', () { | |
| 234 var trace = new Trace.parse(''); | |
| 235 expect(trace.frames, isEmpty); | |
| 236 expect(trace.toString(), equals('')); | |
| 237 }); | |
| 238 }); | |
| 239 | |
| 240 test('.toString() nicely formats the stack trace', () { | |
| 241 var trace = new Trace.parse(''' | |
| 242 #0 Foo._bar (foo/bar.dart:42:21) | |
| 243 #1 zip.<anonymous closure>.zap (dart:async/future.dart:0:2) | |
| 244 #2 zip.<anonymous closure>.zap (http://pub.dartlang.org/thing.dart:1:100) | |
| 245 '''); | |
| 246 | |
| 247 expect(trace.toString(), equals(''' | |
| 248 ${path.join('foo', 'bar.dart')} 42:21 Foo._bar | |
| 249 dart:async/future.dart 0:2 zip.<fn>.zap | |
| 250 http://pub.dartlang.org/thing.dart 1:100 zip.<fn>.zap | |
| 251 ''')); | |
| 252 }); | |
| 253 | |
| 254 test('.vmTrace returns a native-style trace', () { | |
| 255 var uri = path.toUri(path.absolute('foo')); | |
| 256 var trace = new Trace([ | |
| 257 new Frame(uri, 10, 20, 'Foo.<fn>'), | |
| 258 new Frame(Uri.parse('http://dartlang.org/foo.dart'), null, null, 'bar'), | |
| 259 new Frame(Uri.parse('dart:async'), 15, null, 'baz'), | |
| 260 ]); | |
| 261 | |
| 262 expect(trace.vmTrace.toString(), equals( | |
| 263 '#1 Foo.<anonymous closure> ($uri:10:20)\n' | |
| 264 '#2 bar (http://dartlang.org/foo.dart:0:0)\n' | |
| 265 '#3 baz (dart:async:15:0)\n')); | |
| 266 }); | |
| 267 | |
| 268 test('.terse folds core frames together bottom-up', () { | |
| 269 var trace = new Trace.parse(''' | |
| 270 #0 notCore (foo.dart:42:21) | |
| 271 #1 top (dart:async/future.dart:0:2) | |
| 272 #2 bottom (dart:core/uri.dart:1:100) | |
| 273 #3 alsoNotCore (bar.dart:10:20) | |
| 274 #4 top (dart:io:5:10) | |
| 275 #5 bottom (dart:async-patch/future.dart:9:11) | |
| 276 '''); | |
| 277 | |
| 278 expect(trace.terse.toString(), equals(''' | |
| 279 foo.dart 42:21 notCore | |
| 280 dart:core bottom | |
| 281 bar.dart 10:20 alsoNotCore | |
| 282 dart:async bottom | |
| 283 ''')); | |
| 284 }); | |
| 285 | |
| 286 test('.foldFrames folds frames together bottom-up', () { | |
| 287 var trace = new Trace.parse(''' | |
| 288 #0 notFoo (foo.dart:42:21) | |
| 289 #1 fooTop (bar.dart:0:2) | |
| 290 #2 fooBottom (foo.dart:1:100) | |
| 291 #3 alsoNotFoo (bar.dart:10:20) | |
| 292 #4 fooTop (dart:io/socket.dart:5:10) | |
| 293 #5 fooBottom (dart:async-patch/future.dart:9:11) | |
| 294 '''); | |
| 295 | |
| 296 var folded = trace.foldFrames((frame) => frame.member.startsWith('foo')); | |
| 297 expect(folded.toString(), equals(''' | |
| 298 foo.dart 42:21 notFoo | |
| 299 foo.dart 1:100 fooBottom | |
| 300 bar.dart 10:20 alsoNotFoo | |
| 301 dart:async-patch/future.dart 9:11 fooBottom | |
| 302 ''')); | |
| 303 }); | |
| 304 } | |
| OLD | NEW |