| 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 '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': 'output1.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 List SOURCE_MAP_BUNDLE = const [ |
| 59 MAP_WITH_SOURCE_LOCATION_AND_NAME_1, |
| 60 MAP_WITH_SOURCE_LOCATION_AND_NAME_2 |
| 61 ]; |
| 62 |
| 39 main() { | 63 main() { |
| 40 test('parse', () { | 64 test('parse', () { |
| 41 var mapping = parseJson(EXPECTED_MAP); | 65 var mapping = parseJson(EXPECTED_MAP); |
| 42 check(outputVar1, mapping, inputVar1, false); | 66 check(outputVar1, mapping, inputVar1, false); |
| 43 check(outputVar2, mapping, inputVar2, false); | 67 check(outputVar2, mapping, inputVar2, false); |
| 44 check(outputFunction, mapping, inputFunction, false); | 68 check(outputFunction, mapping, inputFunction, false); |
| 45 check(outputExpr, mapping, inputExpr, false); | 69 check(outputExpr, mapping, inputExpr, false); |
| 46 }); | 70 }); |
| 47 | 71 |
| 48 test('parse + json', () { | 72 test('parse + json', () { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 expect(entry.sourceColumn, 0); | 122 expect(entry.sourceColumn, 0); |
| 99 expect(entry.sourceLine, 0); | 123 expect(entry.sourceLine, 0); |
| 100 expect(entry.sourceNameId, 0); | 124 expect(entry.sourceNameId, 0); |
| 101 }); | 125 }); |
| 102 | 126 |
| 103 test('parse with source root', () { | 127 test('parse with source root', () { |
| 104 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); | 128 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); |
| 105 inputMap['sourceRoot'] = '/pkg/'; | 129 inputMap['sourceRoot'] = '/pkg/'; |
| 106 var mapping = parseJson(inputMap); | 130 var mapping = parseJson(inputMap); |
| 107 expect(mapping.spanFor(0, 0).sourceUrl, Uri.parse("/pkg/input.dart")); | 131 expect(mapping.spanFor(0, 0).sourceUrl, Uri.parse("/pkg/input.dart")); |
| 132 expect( |
| 133 mapping |
| 134 .spanForLocation( |
| 135 new SourceLocation(0, sourceUrl: Uri.parse("ignored.dart"))) |
| 136 .sourceUrl, |
| 137 Uri.parse("/pkg/input.dart")); |
| 108 | 138 |
| 109 var newSourceRoot = '/new/'; | 139 var newSourceRoot = '/new/'; |
| 110 | 140 |
| 111 mapping.sourceRoot = newSourceRoot; | 141 mapping.sourceRoot = newSourceRoot; |
| 112 inputMap["sourceRoot"] = newSourceRoot; | 142 inputMap["sourceRoot"] = newSourceRoot; |
| 113 | 143 |
| 114 expect(mapping.toJson(), equals(inputMap)); | 144 expect(mapping.toJson(), equals(inputMap)); |
| 115 }); | 145 }); |
| 116 | 146 |
| 117 test('parse with map URL', () { | 147 test('parse with map URL', () { |
| 118 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); | 148 var inputMap = new Map.from(MAP_WITH_SOURCE_LOCATION); |
| 119 inputMap['sourceRoot'] = 'pkg/'; | 149 inputMap['sourceRoot'] = 'pkg/'; |
| 120 var mapping = parseJson(inputMap, mapUrl: "file:///path/to/map"); | 150 var mapping = parseJson(inputMap, mapUrl: "file:///path/to/map"); |
| 121 expect(mapping.spanFor(0, 0).sourceUrl, | 151 expect(mapping.spanFor(0, 0).sourceUrl, |
| 122 Uri.parse("file:///path/to/pkg/input.dart")); | 152 Uri.parse("file:///path/to/pkg/input.dart")); |
| 123 }); | 153 }); |
| 124 | 154 |
| 155 group('parse with bundle', () { |
| 156 var mapping = |
| 157 parseJsonExtended(SOURCE_MAP_BUNDLE, mapUrl: "file:///path/to/map"); |
| 158 test('simple', () { |
| 159 expect( |
| 160 mapping |
| 161 .spanForLocation(new SourceLocation(0, |
| 162 sourceUrl: new Uri.file('/path/to/output1.dart'))) |
| 163 .sourceUrl, |
| 164 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 165 expect( |
| 166 mapping |
| 167 .spanForLocation(new SourceLocation(0, |
| 168 sourceUrl: new Uri.file('/path/to/output2.dart'))) |
| 169 .sourceUrl, |
| 170 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 171 |
| 172 expect( |
| 173 mapping.spanFor(0, 0, uri: "file:///path/to/output1.dart").sourceUrl, |
| 174 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 175 expect( |
| 176 mapping.spanFor(0, 0, uri: "file:///path/to/output2.dart").sourceUrl, |
| 177 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 178 }); |
| 179 |
| 180 test('unmapped path', () { |
| 181 var span = mapping.spanFor(0, 0, uri: "unmapped_output.dart"); |
| 182 expect(span.sourceUrl, Uri.parse("unmapped_output.dart")); |
| 183 expect(span.start.line, equals(0)); |
| 184 expect(span.start.column, equals(0)); |
| 185 |
| 186 span = mapping.spanFor(10, 5, uri: "unmapped_output.dart"); |
| 187 expect(span.sourceUrl, Uri.parse("unmapped_output.dart")); |
| 188 expect(span.start.line, equals(10)); |
| 189 expect(span.start.column, equals(5)); |
| 190 }); |
| 191 |
| 192 test('missing path', () { |
| 193 expect(() => mapping.spanFor(0, 0), throws); |
| 194 }); |
| 195 |
| 196 test('incomplete paths', () { |
| 197 expect(mapping.spanFor(0, 0, uri: "output1.dart").sourceUrl, |
| 198 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 199 expect(mapping.spanFor(0, 0, uri: "output2.dart").sourceUrl, |
| 200 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 201 }); |
| 202 |
| 203 test('parseExtended', () { |
| 204 var mapping = parseExtended(JSON.encode(SOURCE_MAP_BUNDLE), |
| 205 mapUrl: "file:///path/to/map"); |
| 206 |
| 207 expect(mapping.spanFor(0, 0, uri: "output1.dart").sourceUrl, |
| 208 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 209 expect(mapping.spanFor(0, 0, uri: "output2.dart").sourceUrl, |
| 210 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 211 }); |
| 212 |
| 213 // 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 |
| 215 // map should be used. |
| 216 test('different paths', () { |
| 217 expect( |
| 218 mapping |
| 219 .spanForLocation(new SourceLocation(0, |
| 220 sourceUrl: Uri.parse('http://localhost/output1.dart'))) |
| 221 .sourceUrl, |
| 222 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 223 expect( |
| 224 mapping |
| 225 .spanForLocation(new SourceLocation(0, |
| 226 sourceUrl: Uri.parse('http://localhost/output2.dart'))) |
| 227 .sourceUrl, |
| 228 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 229 |
| 230 expect( |
| 231 mapping.spanFor(0, 0, uri: "http://localhost/output1.dart").sourceUrl, |
| 232 Uri.parse("file:///path/to/pkg/input1.dart")); |
| 233 expect( |
| 234 mapping.spanFor(0, 0, uri: "http://localhost/output2.dart").sourceUrl, |
| 235 Uri.parse("file:///path/to/pkg/input2.dart")); |
| 236 }); |
| 237 }); |
| 238 |
| 125 test('parse and re-emit', () { | 239 test('parse and re-emit', () { |
| 126 for (var expected in [ | 240 for (var expected in [ |
| 127 EXPECTED_MAP, | 241 EXPECTED_MAP, |
| 128 MAP_WITH_NO_SOURCE_LOCATION, | 242 MAP_WITH_NO_SOURCE_LOCATION, |
| 129 MAP_WITH_SOURCE_LOCATION, | 243 MAP_WITH_SOURCE_LOCATION, |
| 130 MAP_WITH_SOURCE_LOCATION_AND_NAME]) { | 244 MAP_WITH_SOURCE_LOCATION_AND_NAME |
| 245 ]) { |
| 131 var mapping = parseJson(expected); | 246 var mapping = parseJson(expected); |
| 132 expect(mapping.toJson(), equals(expected)); | 247 expect(mapping.toJson(), equals(expected)); |
| 248 |
| 249 mapping = parseJsonExtended(expected); |
| 250 expect(mapping.toJson(), equals(expected)); |
| 133 } | 251 } |
| 252 // Invalid for this case |
| 253 expect(() => parseJson(SOURCE_MAP_BUNDLE), throws); |
| 254 |
| 255 var mapping = parseJsonExtended(SOURCE_MAP_BUNDLE); |
| 256 expect(mapping.toJson(), equals(SOURCE_MAP_BUNDLE)); |
| 134 }); | 257 }); |
| 135 } | 258 } |
| OLD | NEW |