OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'package:source_maps/source_maps.dart'; | 5 import 'package:source_maps/source_maps.dart'; |
6 import 'package:source_span/source_span.dart'; | 6 import 'package:source_span/source_span.dart'; |
7 import 'package:stack_trace/stack_trace.dart'; | 7 import 'package:stack_trace/stack_trace.dart'; |
8 import 'package:source_map_stack_trace/source_map_stack_trace.dart'; | 8 import 'package:source_map_stack_trace/source_map_stack_trace.dart'; |
9 import 'package:test/test.dart'; | 9 import 'package:test/test.dart'; |
10 | 10 |
11 /// A simple [Mapping] for tests that don't need anything special. | 11 /// A simple [Mapping] for tests that don't need anything special. |
12 final _simpleMapping = parseJson( | 12 final _simpleMapping = parseJson( |
13 (new SourceMapBuilder() | 13 (new SourceMapBuilder() |
14 ..addSpan( | 14 ..addSpan( |
15 new SourceMapSpan.identifier( | 15 new SourceMapSpan.identifier( |
16 new SourceLocation(1, | 16 new SourceLocation(1, |
17 line: 1, column: 3, sourceUrl: "foo.dart"), | 17 line: 1, column: 3, sourceUrl: "foo.dart"), |
18 "qux"), | 18 "qux"), |
19 new SourceSpan( | 19 new SourceSpan( |
20 new SourceLocation(8, line: 5, column: 0), | 20 new SourceLocation(8, line: 5, column: 0), |
21 new SourceLocation(18, line: 15, column: 0), | 21 new SourceLocation(18, line: 15, column: 0), |
22 "\n" * 10))) | 22 "\n" * 10))) |
23 .build("foo.dart.js.map")); | 23 .build("foo.dart.js.map")); |
24 | 24 |
25 void main() { | 25 void main() { |
26 test("maps a JS line and column to a Dart line and span", () { | 26 test("maps a JS line and column to a Dart line and span", () { |
27 var trace = new Trace.parse("foo.dart.js 10:11 foo"); | 27 var trace = new Trace.parse("foo.dart.js 10:11 foo"); |
28 var frame = mapStackTrace(_simpleMapping, trace).frames.first; | 28 var frame = _mapTrace(_simpleMapping, trace).frames.first; |
29 expect(frame.uri, equals(Uri.parse("foo.dart"))); | 29 expect(frame.uri, equals(Uri.parse("foo.dart"))); |
30 | 30 |
31 // These are +1 because stack_trace is 1-based whereas source_span is | 31 // These are +1 because stack_trace is 1-based whereas source_span is |
32 // 0-basd. | 32 // 0-basd. |
33 expect(frame.line, equals(2)); | 33 expect(frame.line, equals(2)); |
34 expect(frame.column, equals(4)); | 34 expect(frame.column, equals(4)); |
35 }); | 35 }); |
36 | 36 |
37 test("ignores JS frames without line info", () { | 37 test("ignores JS frames without line info", () { |
38 var trace = new Trace.parse(""" | 38 var trace = new Trace.parse(""" |
39 foo.dart.js 10:11 foo | 39 foo.dart.js 10:11 foo |
40 foo.dart.js bar | 40 foo.dart.js bar |
41 foo.dart.js 10:11 baz | 41 foo.dart.js 10:11 baz |
42 """); | 42 """); |
43 var frames = mapStackTrace(_simpleMapping, trace).frames; | 43 var frames = _mapTrace(_simpleMapping, trace).frames; |
44 | 44 |
45 expect(frames.length, equals(2)); | 45 expect(frames.length, equals(2)); |
46 expect(frames.first.member, equals("foo")); | 46 expect(frames.first.member, equals("foo")); |
47 expect(frames.last.member, equals("baz")); | 47 expect(frames.last.member, equals("baz")); |
48 }); | 48 }); |
49 | 49 |
50 test("ignores JS frames without corresponding spans", () { | 50 test("ignores JS frames without corresponding spans", () { |
51 var trace = new Trace.parse(""" | 51 var trace = new Trace.parse(""" |
52 foo.dart.js 10:11 foo | 52 foo.dart.js 10:11 foo |
53 foo.dart.js 1:1 bar | 53 foo.dart.js 1:1 bar |
54 foo.dart.js 10:11 baz | 54 foo.dart.js 10:11 baz |
55 """); | 55 """); |
56 | 56 |
57 var frames = mapStackTrace(_simpleMapping, trace).frames; | 57 var frames = _mapTrace(_simpleMapping, trace).frames; |
58 | 58 |
59 expect(frames.length, equals(2)); | 59 expect(frames.length, equals(2)); |
60 expect(frames.first.member, equals("foo")); | 60 expect(frames.first.member, equals("foo")); |
61 expect(frames.last.member, equals("baz")); | 61 expect(frames.last.member, equals("baz")); |
62 }); | 62 }); |
63 | 63 |
64 test("falls back to column 0 for unlisted column", () { | 64 test("falls back to column 0 for unlisted column", () { |
65 var trace = new Trace.parse("foo.dart.js 10 foo"); | 65 var trace = new Trace.parse("foo.dart.js 10 foo"); |
66 var builder = new SourceMapBuilder() | 66 var builder = new SourceMapBuilder() |
67 ..addSpan( | 67 ..addSpan( |
68 new SourceMapSpan.identifier( | 68 new SourceMapSpan.identifier( |
69 new SourceLocation(1, | 69 new SourceLocation(1, |
70 line: 1, column: 3, sourceUrl: "foo.dart"), | 70 line: 1, column: 3, sourceUrl: "foo.dart"), |
71 "qux"), | 71 "qux"), |
72 new SourceSpan( | 72 new SourceSpan( |
73 new SourceLocation(8, line: 5, column: 0), | 73 new SourceLocation(8, line: 5, column: 0), |
74 new SourceLocation(12, line: 9, column: 1), | 74 new SourceLocation(12, line: 9, column: 1), |
75 "\n" * 4)); | 75 "\n" * 4)); |
76 | 76 |
77 var mapping = parseJson(builder.build("foo.dart.js.map")); | 77 var mapping = parseJson(builder.build("foo.dart.js.map")); |
78 var frame = mapStackTrace(mapping, trace).frames.first; | 78 var frame = _mapTrace(mapping, trace).frames.first; |
79 expect(frame.uri, equals(Uri.parse("foo.dart"))); | 79 expect(frame.uri, equals(Uri.parse("foo.dart"))); |
80 expect(frame.line, equals(2)); | 80 expect(frame.line, equals(2)); |
81 expect(frame.column, equals(4)); | 81 expect(frame.column, equals(4)); |
82 }); | 82 }); |
83 | 83 |
84 test("uses package: URIs for frames within packageRoot", () { | 84 test("uses package: URIs for frames within packageRoot", () { |
85 var trace = new Trace.parse("foo.dart.js 10 foo"); | 85 var trace = new Trace.parse("foo.dart.js 10 foo"); |
86 var builder = new SourceMapBuilder() | 86 var builder = new SourceMapBuilder() |
87 ..addSpan( | 87 ..addSpan( |
88 new SourceMapSpan.identifier( | 88 new SourceMapSpan.identifier( |
89 new SourceLocation(1, | 89 new SourceLocation(1, |
90 line: 1, column: 3, sourceUrl: "packages/foo/foo.dart"), | 90 line: 1, column: 3, sourceUrl: "packages/foo/foo.dart"), |
91 "qux"), | 91 "qux"), |
92 new SourceSpan( | 92 new SourceSpan( |
93 new SourceLocation(8, line: 5, column: 0), | 93 new SourceLocation(8, line: 5, column: 0), |
94 new SourceLocation(12, line: 9, column: 1), | 94 new SourceLocation(12, line: 9, column: 1), |
95 "\n" * 4)); | 95 "\n" * 4)); |
96 | 96 |
97 var mapping = parseJson(builder.build("foo.dart.js.map")); | 97 var mapping = parseJson(builder.build("foo.dart.js.map")); |
98 var frame = mapStackTrace(mapping, trace, packageRoot: "packages/") | 98 var frame = _mapTrace(mapping, trace, packageRoot: "packages/") |
99 .frames.first; | 99 .frames.first; |
100 expect(frame.uri, equals(Uri.parse("package:foo/foo.dart"))); | 100 expect(frame.uri, equals(Uri.parse("package:foo/foo.dart"))); |
101 expect(frame.line, equals(2)); | 101 expect(frame.line, equals(2)); |
102 expect(frame.column, equals(4)); | 102 expect(frame.column, equals(4)); |
103 }); | 103 }); |
104 | 104 |
105 test("uses dart: URIs for frames within sdkRoot", () { | 105 test("uses dart: URIs for frames within sdkRoot", () { |
106 var trace = new Trace.parse("foo.dart.js 10 foo"); | 106 var trace = new Trace.parse("foo.dart.js 10 foo"); |
107 var builder = new SourceMapBuilder() | 107 var builder = new SourceMapBuilder() |
108 ..addSpan( | 108 ..addSpan( |
109 new SourceMapSpan.identifier( | 109 new SourceMapSpan.identifier( |
110 new SourceLocation(1, | 110 new SourceLocation(1, |
111 line: 1, column: 3, sourceUrl: "sdk/lib/async/foo.dart"), | 111 line: 1, column: 3, sourceUrl: "sdk/lib/async/foo.dart"), |
112 "qux"), | 112 "qux"), |
113 new SourceSpan( | 113 new SourceSpan( |
114 new SourceLocation(8, line: 5, column: 0), | 114 new SourceLocation(8, line: 5, column: 0), |
115 new SourceLocation(12, line: 9, column: 1), | 115 new SourceLocation(12, line: 9, column: 1), |
116 "\n" * 4)); | 116 "\n" * 4)); |
117 | 117 |
118 var mapping = parseJson(builder.build("foo.dart.js.map")); | 118 var mapping = parseJson(builder.build("foo.dart.js.map")); |
119 var frame = mapStackTrace(mapping, trace, sdkRoot: "sdk/").frames.first; | 119 var frame = _mapTrace(mapping, trace, sdkRoot: "sdk/").frames.first; |
120 expect(frame.uri, equals(Uri.parse("dart:async/foo.dart"))); | 120 expect(frame.uri, equals(Uri.parse("dart:async/foo.dart"))); |
121 expect(frame.line, equals(2)); | 121 expect(frame.line, equals(2)); |
122 expect(frame.column, equals(4)); | 122 expect(frame.column, equals(4)); |
123 }); | 123 }); |
124 | 124 |
125 test("converts a stack chain", () { | 125 test("converts a stack chain", () { |
126 var trace = new Chain([ | 126 var trace = new Chain([ |
127 new Trace.parse("foo.dart.js 10:11 foo"), | 127 new Trace.parse("foo.dart.js 10:11 foo"), |
128 new Trace.parse("foo.dart.js 10:11 bar") | 128 new Trace.parse("foo.dart.js 10:11 bar") |
129 ]); | 129 ]); |
130 var traces = mapStackTrace(_simpleMapping, trace).traces; | 130 var traces = _mapChain(_simpleMapping, trace).traces; |
131 | 131 |
132 var frame = traces.first.frames.single; | 132 var frame = traces.first.frames.single; |
133 expect(frame.uri, equals(Uri.parse("foo.dart"))); | 133 expect(frame.uri, equals(Uri.parse("foo.dart"))); |
134 expect(frame.member, equals("foo")); | 134 expect(frame.member, equals("foo")); |
135 expect(frame.line, equals(2)); | 135 expect(frame.line, equals(2)); |
136 expect(frame.column, equals(4)); | 136 expect(frame.column, equals(4)); |
137 | 137 |
138 frame = traces.last.frames.single; | 138 frame = traces.last.frames.single; |
139 expect(frame.uri, equals(Uri.parse("foo.dart"))); | 139 expect(frame.uri, equals(Uri.parse("foo.dart"))); |
140 expect(frame.member, equals("bar")); | 140 expect(frame.member, equals("bar")); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 expect(_prettify(r"Foo_bar__baz"), equals("Foo.bar._baz")); | 183 expect(_prettify(r"Foo_bar__baz"), equals("Foo.bar._baz")); |
184 }); | 184 }); |
185 | 185 |
186 test("lots of stuff", () { | 186 test("lots of stuff", () { |
187 expect(_prettify(r"lib$Foo.static.lib$Foo_closure.call$0/<"), | 187 expect(_prettify(r"lib$Foo.static.lib$Foo_closure.call$0/<"), |
188 equals("Foo.<fn>")); | 188 equals("Foo.<fn>")); |
189 }); | 189 }); |
190 }); | 190 }); |
191 } | 191 } |
192 | 192 |
| 193 /// Like [mapStackTrace], but is guaranteed to return a [Trace] so it can be |
| 194 /// inspected. |
| 195 Trace _mapTrace(Mapping sourceMap, StackTrace stackTrace, |
| 196 {bool minified: false, packageRoot, sdkRoot}) { |
| 197 return new Trace.from(mapStackTrace(sourceMap, stackTrace, |
| 198 minified: minified, packageRoot: packageRoot, sdkRoot: sdkRoot)); |
| 199 } |
| 200 |
| 201 /// Like [mapStackTrace], but is guaranteed to return a [Chain] so it can be |
| 202 /// inspected. |
| 203 Chain _mapChain(Mapping sourceMap, StackTrace stackTrace, |
| 204 {bool minified: false, packageRoot, sdkRoot}) { |
| 205 return new Chain.forTrace(mapStackTrace(sourceMap, stackTrace, |
| 206 minified: minified, packageRoot: packageRoot, sdkRoot: sdkRoot)); |
| 207 } |
| 208 |
193 /// Runs the mapper's prettification logic on [member] and returns the result. | 209 /// Runs the mapper's prettification logic on [member] and returns the result. |
194 String _prettify(String member) { | 210 String _prettify(String member) { |
195 var trace = new Trace([new Frame(Uri.parse("foo.dart.js"), 10, 11, member)]); | 211 var trace = new Trace([new Frame(Uri.parse("foo.dart.js"), 10, 11, member)]); |
196 return mapStackTrace(_simpleMapping, trace).frames.first.member; | 212 return _mapTrace(_simpleMapping, trace).frames.first.member; |
197 } | 213 } |
OLD | NEW |