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 test.parser_test; | 5 library test.parser_test; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
9 import 'package:source_maps/source_maps.dart'; | 9 import 'package:source_maps/source_maps.dart'; |
10 import 'package:source_span/source_span.dart'; | 10 import 'package:source_span/source_span.dart'; |
(...skipping 25 matching lines...) Expand all Loading... |
36 'mappings': 'AAAAA', | 36 'mappings': 'AAAAA', |
37 'file': 'output.dart' | 37 'file': 'output.dart' |
38 }; | 38 }; |
39 | 39 |
40 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME_1 = const { | 40 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME_1 = const { |
41 'version': 3, | 41 'version': 3, |
42 'sourceRoot': 'pkg/', | 42 'sourceRoot': 'pkg/', |
43 'sources': const ['input1.dart'], | 43 'sources': const ['input1.dart'], |
44 'names': const ['var1'], | 44 'names': const ['var1'], |
45 'mappings': 'AAAAA', | 45 'mappings': 'AAAAA', |
46 'file': 'output1.dart' | 46 'file': 'output.dart' |
47 }; | 47 }; |
48 | 48 |
49 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME_2 = const { | 49 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME_2 = const { |
50 'version': 3, | 50 'version': 3, |
51 'sourceRoot': 'pkg/', | 51 'sourceRoot': 'pkg/', |
52 'sources': const ['input2.dart'], | 52 'sources': const ['input2.dart'], |
53 'names': const ['var2'], | 53 'names': const ['var2'], |
54 'mappings': 'AAAAA', | 54 'mappings': 'AAAAA', |
55 'file': 'output2.dart' | 55 'file': 'output2.dart' |
56 }; | 56 }; |
57 | 57 |
| 58 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME_3 = const { |
| 59 'version': 3, |
| 60 'sourceRoot': 'pkg/', |
| 61 'sources': const ['input3.dart'], |
| 62 'names': const ['var3'], |
| 63 'mappings': 'AAAAA', |
| 64 'file': '3/output.dart' |
| 65 }; |
| 66 |
| 67 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME_3_WINDOWS = const { |
| 68 'version': 3, |
| 69 'sourceRoot': 'pkg/', |
| 70 'sources': const ['input3.dart'], |
| 71 'names': const ['var3'], |
| 72 'mappings': 'AAAAA', |
| 73 'file': '3\\output.dart' |
| 74 }; |
| 75 |
58 const List SOURCE_MAP_BUNDLE = const [ | 76 const List SOURCE_MAP_BUNDLE = const [ |
59 MAP_WITH_SOURCE_LOCATION_AND_NAME_1, | 77 MAP_WITH_SOURCE_LOCATION_AND_NAME_1, |
60 MAP_WITH_SOURCE_LOCATION_AND_NAME_2 | 78 MAP_WITH_SOURCE_LOCATION_AND_NAME_2, |
| 79 MAP_WITH_SOURCE_LOCATION_AND_NAME_3, |
| 80 ]; |
| 81 |
| 82 const List SOURCE_MAP_BUNDLE_WINDOWS = const [ |
| 83 MAP_WITH_SOURCE_LOCATION_AND_NAME_1, |
| 84 MAP_WITH_SOURCE_LOCATION_AND_NAME_2, |
| 85 MAP_WITH_SOURCE_LOCATION_AND_NAME_3_WINDOWS, |
61 ]; | 86 ]; |
62 | 87 |
63 main() { | 88 main() { |
64 test('parse', () { | 89 test('parse', () { |
65 var mapping = parseJson(EXPECTED_MAP); | 90 var mapping = parseJson(EXPECTED_MAP); |
66 check(outputVar1, mapping, inputVar1, false); | 91 check(outputVar1, mapping, inputVar1, false); |
67 check(outputVar2, mapping, inputVar2, false); | 92 check(outputVar2, mapping, inputVar2, false); |
68 check(outputFunction, mapping, inputFunction, false); | 93 check(outputFunction, mapping, inputFunction, false); |
69 check(outputExpr, mapping, inputExpr, false); | 94 check(outputExpr, mapping, inputExpr, false); |
70 }); | 95 }); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); | 173 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); |
149 inputMap['sourceRoot'] = 'pkg/'; | 174 inputMap['sourceRoot'] = 'pkg/'; |
150 var mapping = parseJson(inputMap, mapUrl: "file:///path/to/map"); | 175 var mapping = parseJson(inputMap, mapUrl: "file:///path/to/map"); |
151 expect(mapping.spanFor(0, 0).sourceUrl, | 176 expect(mapping.spanFor(0, 0).sourceUrl, |
152 Uri.parse("file:///path/to/pkg/input.dart")); | 177 Uri.parse("file:///path/to/pkg/input.dart")); |
153 }); | 178 }); |
154 | 179 |
155 group('parse with bundle', () { | 180 group('parse with bundle', () { |
156 var mapping = | 181 var mapping = |
157 parseJsonExtended(SOURCE_MAP_BUNDLE, mapUrl: "file:///path/to/map"); | 182 parseJsonExtended(SOURCE_MAP_BUNDLE, mapUrl: "file:///path/to/map"); |
| 183 |
| 184 var mappingWindows = parseJsonExtended(SOURCE_MAP_BUNDLE_WINDOWS, |
| 185 mapUrl: "c:\\path\\to\\map"); |
| 186 |
158 test('simple', () { | 187 test('simple', () { |
159 expect( | 188 expect( |
160 mapping | 189 mapping |
161 .spanForLocation(new SourceLocation(0, | 190 .spanForLocation(new SourceLocation(0, |
162 sourceUrl: new Uri.file('/path/to/output1.dart'))) | 191 sourceUrl: new Uri.file('/path/to/output.dart'))) |
163 .sourceUrl, | 192 .sourceUrl, |
164 Uri.parse("file:///path/to/pkg/input1.dart")); | 193 Uri.parse("file:///path/to/pkg/input1.dart")); |
165 expect( | 194 expect( |
166 mapping | 195 mapping |
167 .spanForLocation(new SourceLocation(0, | 196 .spanForLocation(new SourceLocation(0, |
168 sourceUrl: new Uri.file('/path/to/output2.dart'))) | 197 sourceUrl: new Uri.file('/path/to/output2.dart'))) |
169 .sourceUrl, | 198 .sourceUrl, |
170 Uri.parse("file:///path/to/pkg/input2.dart")); | 199 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 200 expect( |
| 201 mapping |
| 202 .spanForLocation(new SourceLocation(0, |
| 203 sourceUrl: new Uri.file('/path/to/3/output.dart'))) |
| 204 .sourceUrl, |
| 205 Uri.parse("file:///path/to/pkg/input3.dart")); |
171 | 206 |
172 expect( | 207 expect( |
173 mapping.spanFor(0, 0, uri: "file:///path/to/output1.dart").sourceUrl, | 208 mapping.spanFor(0, 0, uri: "file:///path/to/output.dart").sourceUrl, |
174 Uri.parse("file:///path/to/pkg/input1.dart")); | 209 Uri.parse("file:///path/to/pkg/input1.dart")); |
175 expect( | 210 expect( |
176 mapping.spanFor(0, 0, uri: "file:///path/to/output2.dart").sourceUrl, | 211 mapping.spanFor(0, 0, uri: "file:///path/to/output2.dart").sourceUrl, |
177 Uri.parse("file:///path/to/pkg/input2.dart")); | 212 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 213 expect( |
| 214 mapping.spanFor(0, 0, uri: "file:///path/to/3/output.dart").sourceUrl, |
| 215 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 216 }); |
| 217 |
| 218 test('package uris', () { |
| 219 expect( |
| 220 mapping |
| 221 .spanForLocation(new SourceLocation(0, |
| 222 sourceUrl: Uri.parse('package:1/output.dart'))) |
| 223 .sourceUrl, |
| 224 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 225 expect( |
| 226 mapping |
| 227 .spanForLocation(new SourceLocation(0, |
| 228 sourceUrl: Uri.parse('package:2/output2.dart'))) |
| 229 .sourceUrl, |
| 230 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 231 expect( |
| 232 mapping |
| 233 .spanForLocation(new SourceLocation(0, |
| 234 sourceUrl: Uri.parse('package:3/output.dart'))) |
| 235 .sourceUrl, |
| 236 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 237 |
| 238 expect(mapping.spanFor(0, 0, uri: "package:1/output.dart").sourceUrl, |
| 239 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 240 expect(mapping.spanFor(0, 0, uri: "package:2/output2.dart").sourceUrl, |
| 241 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 242 expect(mapping.spanFor(0, 0, uri: "package:3/output.dart").sourceUrl, |
| 243 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 244 }); |
| 245 |
| 246 test('windows paths', () { |
| 247 expect(mapping.spanFor(0, 0, uri: "1\\output.dart").sourceUrl, |
| 248 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 249 expect(mapping.spanFor(0, 0, uri: "packages\\2\\output2.dart").sourceUrl, |
| 250 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 251 expect(mapping.spanFor(0, 0, uri: "packages\\3\\output.dart").sourceUrl, |
| 252 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 253 |
| 254 expect(mappingWindows.spanFor(0, 0, uri: "1\\output.dart").sourceUrl, |
| 255 Uri.parse("c:pkg/input1.dart")); |
| 256 expect( |
| 257 mappingWindows |
| 258 .spanFor(0, 0, uri: "packages\\2\\output2.dart") |
| 259 .sourceUrl, |
| 260 Uri.parse("c:pkg/input2.dart")); |
| 261 expect( |
| 262 mappingWindows |
| 263 .spanFor(0, 0, uri: "packages\\3\\output.dart") |
| 264 .sourceUrl, |
| 265 Uri.parse("c:pkg/input3.dart")); |
| 266 |
| 267 expect(mappingWindows.spanFor(0, 0, uri: "1\\output.dart").sourceUrl, |
| 268 Uri.parse("c:pkg/input1.dart")); |
| 269 expect( |
| 270 mappingWindows |
| 271 .spanFor(0, 0, uri: "packages/2/output2.dart") |
| 272 .sourceUrl, |
| 273 Uri.parse("c:pkg/input2.dart")); |
| 274 expect( |
| 275 mappingWindows.spanFor(0, 0, uri: "packages/3/output.dart").sourceUrl, |
| 276 Uri.parse("c:pkg/input3.dart")); |
178 }); | 277 }); |
179 | 278 |
180 test('unmapped path', () { | 279 test('unmapped path', () { |
181 var span = mapping.spanFor(0, 0, uri: "unmapped_output.dart"); | 280 var span = mapping.spanFor(0, 0, uri: "unmapped_output.dart"); |
182 expect(span.sourceUrl, Uri.parse("unmapped_output.dart")); | 281 expect(span.sourceUrl, Uri.parse("unmapped_output.dart")); |
183 expect(span.start.line, equals(0)); | 282 expect(span.start.line, equals(0)); |
184 expect(span.start.column, equals(0)); | 283 expect(span.start.column, equals(0)); |
185 | 284 |
186 span = mapping.spanFor(10, 5, uri: "unmapped_output.dart"); | 285 span = mapping.spanFor(10, 5, uri: "unmapped_output.dart"); |
187 expect(span.sourceUrl, Uri.parse("unmapped_output.dart")); | 286 expect(span.sourceUrl, Uri.parse("unmapped_output.dart")); |
188 expect(span.start.line, equals(10)); | 287 expect(span.start.line, equals(10)); |
189 expect(span.start.column, equals(5)); | 288 expect(span.start.column, equals(5)); |
190 }); | 289 }); |
191 | 290 |
192 test('missing path', () { | 291 test('missing path', () { |
193 expect(() => mapping.spanFor(0, 0), throws); | 292 expect(() => mapping.spanFor(0, 0), throws); |
194 }); | 293 }); |
195 | 294 |
196 test('incomplete paths', () { | 295 test('incomplete paths', () { |
197 expect(mapping.spanFor(0, 0, uri: "output1.dart").sourceUrl, | 296 expect(mapping.spanFor(0, 0, uri: "output.dart").sourceUrl, |
198 Uri.parse("file:///path/to/pkg/input1.dart")); | 297 Uri.parse("file:///path/to/pkg/input1.dart")); |
199 expect(mapping.spanFor(0, 0, uri: "output2.dart").sourceUrl, | 298 expect(mapping.spanFor(0, 0, uri: "output2.dart").sourceUrl, |
200 Uri.parse("file:///path/to/pkg/input2.dart")); | 299 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 300 expect(mapping.spanFor(0, 0, uri: "3/output.dart").sourceUrl, |
| 301 Uri.parse("file:///path/to/pkg/input3.dart")); |
201 }); | 302 }); |
202 | 303 |
203 test('parseExtended', () { | 304 test('parseExtended', () { |
204 var mapping = parseExtended(JSON.encode(SOURCE_MAP_BUNDLE), | 305 var mapping = parseExtended(JSON.encode(SOURCE_MAP_BUNDLE), |
205 mapUrl: "file:///path/to/map"); | 306 mapUrl: "file:///path/to/map"); |
206 | 307 |
207 expect(mapping.spanFor(0, 0, uri: "output1.dart").sourceUrl, | 308 expect(mapping.spanFor(0, 0, uri: "output.dart").sourceUrl, |
208 Uri.parse("file:///path/to/pkg/input1.dart")); | 309 Uri.parse("file:///path/to/pkg/input1.dart")); |
209 expect(mapping.spanFor(0, 0, uri: "output2.dart").sourceUrl, | 310 expect(mapping.spanFor(0, 0, uri: "output2.dart").sourceUrl, |
210 Uri.parse("file:///path/to/pkg/input2.dart")); | 311 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 312 expect(mapping.spanFor(0, 0, uri: "3/output.dart").sourceUrl, |
| 313 Uri.parse("file:///path/to/pkg/input3.dart")); |
211 }); | 314 }); |
212 | 315 |
213 // Test that the source map can handle cases where the uri passed in is | 316 // Test that the source map can handle cases where the uri passed in is |
214 // not from the expected host but it is still unambiguous which source | 317 // not from the expected host but it is still unambiguous which source |
215 // map should be used. | 318 // map should be used. |
216 test('different paths', () { | 319 test('different paths', () { |
217 expect( | 320 expect( |
218 mapping | 321 mapping |
219 .spanForLocation(new SourceLocation(0, | 322 .spanForLocation(new SourceLocation(0, |
220 sourceUrl: Uri.parse('http://localhost/output1.dart'))) | 323 sourceUrl: Uri.parse('http://localhost/output.dart'))) |
221 .sourceUrl, | 324 .sourceUrl, |
222 Uri.parse("file:///path/to/pkg/input1.dart")); | 325 Uri.parse("file:///path/to/pkg/input1.dart")); |
223 expect( | 326 expect( |
224 mapping | 327 mapping |
225 .spanForLocation(new SourceLocation(0, | 328 .spanForLocation(new SourceLocation(0, |
226 sourceUrl: Uri.parse('http://localhost/output2.dart'))) | 329 sourceUrl: Uri.parse('http://localhost/output2.dart'))) |
227 .sourceUrl, | 330 .sourceUrl, |
228 Uri.parse("file:///path/to/pkg/input2.dart")); | 331 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 332 expect( |
| 333 mapping |
| 334 .spanForLocation(new SourceLocation(0, |
| 335 sourceUrl: Uri.parse('http://localhost/3/output.dart'))) |
| 336 .sourceUrl, |
| 337 Uri.parse("file:///path/to/pkg/input3.dart")); |
229 | 338 |
230 expect( | 339 expect( |
231 mapping.spanFor(0, 0, uri: "http://localhost/output1.dart").sourceUrl, | 340 mapping.spanFor(0, 0, uri: "http://localhost/output.dart").sourceUrl, |
232 Uri.parse("file:///path/to/pkg/input1.dart")); | 341 Uri.parse("file:///path/to/pkg/input1.dart")); |
233 expect( | 342 expect( |
234 mapping.spanFor(0, 0, uri: "http://localhost/output2.dart").sourceUrl, | 343 mapping.spanFor(0, 0, uri: "http://localhost/output2.dart").sourceUrl, |
235 Uri.parse("file:///path/to/pkg/input2.dart")); | 344 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 345 expect( |
| 346 mapping |
| 347 .spanFor(0, 0, uri: "http://localhost/3/output.dart") |
| 348 .sourceUrl, |
| 349 Uri.parse("file:///path/to/pkg/input3.dart")); |
236 }); | 350 }); |
237 }); | 351 }); |
238 | 352 |
239 test('parse and re-emit', () { | 353 test('parse and re-emit', () { |
240 for (var expected in [ | 354 for (var expected in [ |
241 EXPECTED_MAP, | 355 EXPECTED_MAP, |
242 MAP_WITH_NO_SOURCE_LOCATION, | 356 MAP_WITH_NO_SOURCE_LOCATION, |
243 MAP_WITH_SOURCE_LOCATION, | 357 MAP_WITH_SOURCE_LOCATION, |
244 MAP_WITH_SOURCE_LOCATION_AND_NAME | 358 MAP_WITH_SOURCE_LOCATION_AND_NAME |
245 ]) { | 359 ]) { |
246 var mapping = parseJson(expected); | 360 var mapping = parseJson(expected); |
247 expect(mapping.toJson(), equals(expected)); | 361 expect(mapping.toJson(), equals(expected)); |
248 | 362 |
249 mapping = parseJsonExtended(expected); | 363 mapping = parseJsonExtended(expected); |
250 expect(mapping.toJson(), equals(expected)); | 364 expect(mapping.toJson(), equals(expected)); |
251 } | 365 } |
252 // Invalid for this case | 366 // Invalid for this case |
253 expect(() => parseJson(SOURCE_MAP_BUNDLE as dynamic), throws); | 367 expect(() => parseJson(SOURCE_MAP_BUNDLE as dynamic), throws); |
254 | 368 |
255 var mapping = parseJsonExtended(SOURCE_MAP_BUNDLE); | 369 var mapping = parseJsonExtended(SOURCE_MAP_BUNDLE); |
256 expect(mapping.toJson(), equals(SOURCE_MAP_BUNDLE)); | 370 expect(mapping.toJson(), equals(SOURCE_MAP_BUNDLE)); |
257 }); | 371 }); |
258 } | 372 } |
OLD | NEW |