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:unittest/unittest.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 'common.dart'; | 11 import 'common.dart'; |
11 | 12 |
12 const Map<String, dynamic> MAP_WITH_NO_SOURCE_LOCATION = const { | 13 const Map<String, dynamic> MAP_WITH_NO_SOURCE_LOCATION = const { |
13 'version': 3, | 14 'version': 3, |
14 'sourceRoot': '', | 15 'sourceRoot': '', |
15 'sources': const ['input.dart'], | 16 'sources': const ['input.dart'], |
16 'names': const [], | 17 'names': const [], |
17 'mappings': 'A', | 18 'mappings': 'A', |
18 'file': 'output.dart' | 19 'file': 'output.dart' |
19 }; | 20 }; |
20 | 21 |
21 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION = const { | 22 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION = const { |
22 'version': 3, | 23 'version': 3, |
23 'sourceRoot': '', | 24 'sourceRoot': '', |
24 'sources': const ['input.dart'], | 25 'sources': const ['input.dart'], |
25 'names': const [], | 26 'names': const [], |
26 'mappings': 'AAAA', | 27 'mappings': 'AAAA', |
27 'file': 'output.dart' | 28 'file': 'output.dart' |
28 }; | 29 }; |
29 | 30 |
30 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME = const { | 31 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME = const { |
31 'version': 3, | 32 'version': 3, |
32 'sourceRoot': '', | 33 'sourceRoot': '', |
33 'sources': const ['input.dart'], | 34 'sources': const ['input.dart'], |
34 'names': const ['var'], | 35 'names': const ['var'], |
35 'mappings': 'AAAAA', | 36 'mappings': 'AAAAA', |
36 'file': 'output.dart' | 37 'file': 'output.dart' |
37 }; | 38 }; |
38 | 39 |
| 40 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME_1 = const { |
| 41 'version': 3, |
| 42 'sourceRoot': 'pkg/', |
| 43 'sources': const ['input1.dart'], |
| 44 'names': const ['var1'], |
| 45 'mappings': 'AAAAA', |
| 46 'file': 'output.dart' |
| 47 }; |
| 48 |
| 49 const Map<String, dynamic> MAP_WITH_SOURCE_LOCATION_AND_NAME_2 = const { |
| 50 'version': 3, |
| 51 'sourceRoot': 'pkg/', |
| 52 'sources': const ['input2.dart'], |
| 53 'names': const ['var2'], |
| 54 'mappings': 'AAAAA', |
| 55 'file': 'output2.dart' |
| 56 }; |
| 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 List SOURCE_MAP_BUNDLE = const [ |
| 68 MAP_WITH_SOURCE_LOCATION_AND_NAME_1, |
| 69 MAP_WITH_SOURCE_LOCATION_AND_NAME_2, |
| 70 MAP_WITH_SOURCE_LOCATION_AND_NAME_3, |
| 71 ]; |
| 72 |
39 main() { | 73 main() { |
40 test('parse', () { | 74 test('parse', () { |
41 var mapping = parseJson(EXPECTED_MAP); | 75 var mapping = parseJson(EXPECTED_MAP); |
42 check(outputVar1, mapping, inputVar1, false); | 76 check(outputVar1, mapping, inputVar1, false); |
43 check(outputVar2, mapping, inputVar2, false); | 77 check(outputVar2, mapping, inputVar2, false); |
44 check(outputFunction, mapping, inputFunction, false); | 78 check(outputFunction, mapping, inputFunction, false); |
45 check(outputExpr, mapping, inputExpr, false); | 79 check(outputExpr, mapping, inputExpr, false); |
46 }); | 80 }); |
47 | 81 |
48 test('parse + json', () { | 82 test('parse + json', () { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 expect(entry.sourceColumn, 0); | 132 expect(entry.sourceColumn, 0); |
99 expect(entry.sourceLine, 0); | 133 expect(entry.sourceLine, 0); |
100 expect(entry.sourceNameId, 0); | 134 expect(entry.sourceNameId, 0); |
101 }); | 135 }); |
102 | 136 |
103 test('parse with source root', () { | 137 test('parse with source root', () { |
104 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); | 138 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); |
105 inputMap['sourceRoot'] = '/pkg/'; | 139 inputMap['sourceRoot'] = '/pkg/'; |
106 var mapping = parseJson(inputMap); | 140 var mapping = parseJson(inputMap); |
107 expect(mapping.spanFor(0, 0).sourceUrl, Uri.parse("/pkg/input.dart")); | 141 expect(mapping.spanFor(0, 0).sourceUrl, Uri.parse("/pkg/input.dart")); |
| 142 expect( |
| 143 mapping |
| 144 .spanForLocation( |
| 145 new SourceLocation(0, sourceUrl: Uri.parse("ignored.dart"))) |
| 146 .sourceUrl, |
| 147 Uri.parse("/pkg/input.dart")); |
108 | 148 |
109 var newSourceRoot = '/new/'; | 149 var newSourceRoot = '/new/'; |
110 | 150 |
111 mapping.sourceRoot = newSourceRoot; | 151 mapping.sourceRoot = newSourceRoot; |
112 inputMap["sourceRoot"] = newSourceRoot; | 152 inputMap["sourceRoot"] = newSourceRoot; |
113 | 153 |
114 expect(mapping.toJson(), equals(inputMap)); | 154 expect(mapping.toJson(), equals(inputMap)); |
115 }); | 155 }); |
116 | 156 |
117 test('parse with map URL', () { | 157 test('parse with map URL', () { |
118 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); | 158 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); |
119 inputMap['sourceRoot'] = 'pkg/'; | 159 inputMap['sourceRoot'] = 'pkg/'; |
120 var mapping = parseJson(inputMap, mapUrl: "file:///path/to/map"); | 160 var mapping = parseJson(inputMap, mapUrl: "file:///path/to/map"); |
121 expect(mapping.spanFor(0, 0).sourceUrl, | 161 expect(mapping.spanFor(0, 0).sourceUrl, |
122 Uri.parse("file:///path/to/pkg/input.dart")); | 162 Uri.parse("file:///path/to/pkg/input.dart")); |
123 }); | 163 }); |
124 | 164 |
| 165 group('parse with bundle', () { |
| 166 var mapping = |
| 167 parseJsonExtended(SOURCE_MAP_BUNDLE, mapUrl: "file:///path/to/map"); |
| 168 |
| 169 test('simple', () { |
| 170 expect( |
| 171 mapping |
| 172 .spanForLocation(new SourceLocation(0, |
| 173 sourceUrl: new Uri.file('/path/to/output.dart'))) |
| 174 .sourceUrl, |
| 175 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 176 expect( |
| 177 mapping |
| 178 .spanForLocation(new SourceLocation(0, |
| 179 sourceUrl: new Uri.file('/path/to/output2.dart'))) |
| 180 .sourceUrl, |
| 181 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 182 expect( |
| 183 mapping |
| 184 .spanForLocation(new SourceLocation(0, |
| 185 sourceUrl: new Uri.file('/path/to/3/output.dart'))) |
| 186 .sourceUrl, |
| 187 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 188 |
| 189 expect( |
| 190 mapping.spanFor(0, 0, uri: "file:///path/to/output.dart").sourceUrl, |
| 191 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 192 expect( |
| 193 mapping.spanFor(0, 0, uri: "file:///path/to/output2.dart").sourceUrl, |
| 194 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 195 expect( |
| 196 mapping.spanFor(0, 0, uri: "file:///path/to/3/output.dart").sourceUrl, |
| 197 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 198 }); |
| 199 |
| 200 test('package uris', () { |
| 201 expect( |
| 202 mapping |
| 203 .spanForLocation(new SourceLocation(0, |
| 204 sourceUrl: Uri.parse('package:1/output.dart'))) |
| 205 .sourceUrl, |
| 206 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 207 expect( |
| 208 mapping |
| 209 .spanForLocation(new SourceLocation(0, |
| 210 sourceUrl: Uri.parse('package:2/output2.dart'))) |
| 211 .sourceUrl, |
| 212 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 213 expect( |
| 214 mapping |
| 215 .spanForLocation(new SourceLocation(0, |
| 216 sourceUrl: Uri.parse('package:3/output.dart'))) |
| 217 .sourceUrl, |
| 218 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 219 |
| 220 expect(mapping.spanFor(0, 0, uri: "package:1/output.dart").sourceUrl, |
| 221 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 222 expect(mapping.spanFor(0, 0, uri: "package:2/output2.dart").sourceUrl, |
| 223 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 224 expect(mapping.spanFor(0, 0, uri: "package:3/output.dart").sourceUrl, |
| 225 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 226 }); |
| 227 |
| 228 test('unmapped path', () { |
| 229 var span = mapping.spanFor(0, 0, uri: "unmapped_output.dart"); |
| 230 expect(span.sourceUrl, Uri.parse("unmapped_output.dart")); |
| 231 expect(span.start.line, equals(0)); |
| 232 expect(span.start.column, equals(0)); |
| 233 |
| 234 span = mapping.spanFor(10, 5, uri: "unmapped_output.dart"); |
| 235 expect(span.sourceUrl, Uri.parse("unmapped_output.dart")); |
| 236 expect(span.start.line, equals(10)); |
| 237 expect(span.start.column, equals(5)); |
| 238 }); |
| 239 |
| 240 test('missing path', () { |
| 241 expect(() => mapping.spanFor(0, 0), throws); |
| 242 }); |
| 243 |
| 244 test('incomplete paths', () { |
| 245 expect(mapping.spanFor(0, 0, uri: "output.dart").sourceUrl, |
| 246 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 247 expect(mapping.spanFor(0, 0, uri: "output2.dart").sourceUrl, |
| 248 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 249 expect(mapping.spanFor(0, 0, uri: "3/output.dart").sourceUrl, |
| 250 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 251 }); |
| 252 |
| 253 test('parseExtended', () { |
| 254 var mapping = parseExtended(JSON.encode(SOURCE_MAP_BUNDLE), |
| 255 mapUrl: "file:///path/to/map"); |
| 256 |
| 257 expect(mapping.spanFor(0, 0, uri: "output.dart").sourceUrl, |
| 258 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 259 expect(mapping.spanFor(0, 0, uri: "output2.dart").sourceUrl, |
| 260 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 261 expect(mapping.spanFor(0, 0, uri: "3/output.dart").sourceUrl, |
| 262 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 263 }); |
| 264 |
| 265 test('build bundle incrementally', () { |
| 266 var mapping = new MappingBundle(); |
| 267 |
| 268 mapping.addMapping(parseJson(MAP_WITH_SOURCE_LOCATION_AND_NAME_1, |
| 269 mapUrl: "file:///path/to/map")); |
| 270 expect(mapping.spanFor(0, 0, uri: "output.dart").sourceUrl, |
| 271 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 272 |
| 273 expect(mapping.containsMapping("output2.dart"), isFalse); |
| 274 mapping.addMapping(parseJson(MAP_WITH_SOURCE_LOCATION_AND_NAME_2, |
| 275 mapUrl: "file:///path/to/map")); |
| 276 expect(mapping.containsMapping("output2.dart"), isTrue); |
| 277 expect(mapping.spanFor(0, 0, uri: "output2.dart").sourceUrl, |
| 278 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 279 |
| 280 expect(mapping.containsMapping("3/output.dart"), isFalse); |
| 281 mapping.addMapping(parseJson(MAP_WITH_SOURCE_LOCATION_AND_NAME_3, |
| 282 mapUrl: "file:///path/to/map")); |
| 283 expect(mapping.containsMapping("3/output.dart"), isTrue); |
| 284 expect(mapping.spanFor(0, 0, uri: "3/output.dart").sourceUrl, |
| 285 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 286 }); |
| 287 |
| 288 // Test that the source map can handle cases where the uri passed in is |
| 289 // not from the expected host but it is still unambiguous which source |
| 290 // map should be used. |
| 291 test('different paths', () { |
| 292 expect( |
| 293 mapping |
| 294 .spanForLocation(new SourceLocation(0, |
| 295 sourceUrl: Uri.parse('http://localhost/output.dart'))) |
| 296 .sourceUrl, |
| 297 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 298 expect( |
| 299 mapping |
| 300 .spanForLocation(new SourceLocation(0, |
| 301 sourceUrl: Uri.parse('http://localhost/output2.dart'))) |
| 302 .sourceUrl, |
| 303 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 304 expect( |
| 305 mapping |
| 306 .spanForLocation(new SourceLocation(0, |
| 307 sourceUrl: Uri.parse('http://localhost/3/output.dart'))) |
| 308 .sourceUrl, |
| 309 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 310 |
| 311 expect( |
| 312 mapping.spanFor(0, 0, uri: "http://localhost/output.dart").sourceUrl, |
| 313 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 314 expect( |
| 315 mapping.spanFor(0, 0, uri: "http://localhost/output2.dart").sourceUrl, |
| 316 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 317 expect( |
| 318 mapping |
| 319 .spanFor(0, 0, uri: "http://localhost/3/output.dart") |
| 320 .sourceUrl, |
| 321 Uri.parse("file:///path/to/pkg/input3.dart")); |
| 322 }); |
| 323 }); |
| 324 |
125 test('parse and re-emit', () { | 325 test('parse and re-emit', () { |
126 for (var expected in [ | 326 for (var expected in [ |
127 EXPECTED_MAP, | 327 EXPECTED_MAP, |
128 MAP_WITH_NO_SOURCE_LOCATION, | 328 MAP_WITH_NO_SOURCE_LOCATION, |
129 MAP_WITH_SOURCE_LOCATION, | 329 MAP_WITH_SOURCE_LOCATION, |
130 MAP_WITH_SOURCE_LOCATION_AND_NAME]) { | 330 MAP_WITH_SOURCE_LOCATION_AND_NAME |
| 331 ]) { |
131 var mapping = parseJson(expected); | 332 var mapping = parseJson(expected); |
132 expect(mapping.toJson(), equals(expected)); | 333 expect(mapping.toJson(), equals(expected)); |
| 334 |
| 335 mapping = parseJsonExtended(expected); |
| 336 expect(mapping.toJson(), equals(expected)); |
133 } | 337 } |
| 338 // Invalid for this case |
| 339 expect(() => parseJson(SOURCE_MAP_BUNDLE as dynamic), throws); |
| 340 |
| 341 var mapping = parseJsonExtended(SOURCE_MAP_BUNDLE); |
| 342 expect(mapping.toJson(), equals(SOURCE_MAP_BUNDLE)); |
134 }); | 343 }); |
135 } | 344 } |
OLD | NEW |