|
1 /* |
|
2 * WARNING! |
|
3 * |
|
4 * Do not edit this file directly, it is built from the sources at |
|
5 * https://github.com/mozilla/source-map/ |
|
6 */ |
|
7 |
|
8 Components.utils.import('resource://test/Utils.jsm'); |
|
9 /* -*- Mode: js; js-indent-level: 2; -*- */ |
|
10 /* |
|
11 * Copyright 2011 Mozilla Foundation and contributors |
|
12 * Licensed under the New BSD license. See LICENSE or: |
|
13 * http://opensource.org/licenses/BSD-3-Clause |
|
14 */ |
|
15 define("test/source-map/test-source-map-consumer", ["require", "exports", "module"], function (require, exports, module) { |
|
16 |
|
17 var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer; |
|
18 var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator; |
|
19 |
|
20 exports['test that we can instantiate with a string or an objects'] = function (assert, util) { |
|
21 assert.doesNotThrow(function () { |
|
22 var map = new SourceMapConsumer(util.testMap); |
|
23 }); |
|
24 assert.doesNotThrow(function () { |
|
25 var map = new SourceMapConsumer(JSON.stringify(util.testMap)); |
|
26 }); |
|
27 }; |
|
28 |
|
29 exports['test that the `sources` field has the original sources'] = function (assert, util) { |
|
30 var map = new SourceMapConsumer(util.testMap); |
|
31 var sources = map.sources; |
|
32 |
|
33 assert.equal(sources[0], '/the/root/one.js'); |
|
34 assert.equal(sources[1], '/the/root/two.js'); |
|
35 assert.equal(sources.length, 2); |
|
36 }; |
|
37 |
|
38 exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) { |
|
39 var map = new SourceMapConsumer(util.testMap); |
|
40 var mapping; |
|
41 |
|
42 mapping = map.originalPositionFor({ |
|
43 line: 2, |
|
44 column: 1 |
|
45 }); |
|
46 assert.equal(mapping.source, '/the/root/two.js'); |
|
47 |
|
48 mapping = map.originalPositionFor({ |
|
49 line: 1, |
|
50 column: 1 |
|
51 }); |
|
52 assert.equal(mapping.source, '/the/root/one.js'); |
|
53 }; |
|
54 |
|
55 exports['test mapping tokens back exactly'] = function (assert, util) { |
|
56 var map = new SourceMapConsumer(util.testMap); |
|
57 |
|
58 util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert); |
|
59 util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert); |
|
60 util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert); |
|
61 util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert); |
|
62 util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert); |
|
63 util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert); |
|
64 util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert); |
|
65 |
|
66 util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert); |
|
67 util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert); |
|
68 util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert); |
|
69 util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert); |
|
70 util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert); |
|
71 util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert); |
|
72 }; |
|
73 |
|
74 exports['test mapping tokens fuzzy'] = function (assert, util) { |
|
75 var map = new SourceMapConsumer(util.testMap); |
|
76 |
|
77 // Finding original positions |
|
78 util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true); |
|
79 util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true); |
|
80 util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true); |
|
81 |
|
82 // Finding generated positions |
|
83 util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true); |
|
84 util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true); |
|
85 util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true); |
|
86 }; |
|
87 |
|
88 exports['test creating source map consumers with )]}\' prefix'] = function (assert, util) { |
|
89 assert.doesNotThrow(function () { |
|
90 var map = new SourceMapConsumer(")]}'" + JSON.stringify(util.testMap)); |
|
91 }); |
|
92 }; |
|
93 |
|
94 exports['test eachMapping'] = function (assert, util) { |
|
95 var map = new SourceMapConsumer(util.testMap); |
|
96 var previousLine = -Infinity; |
|
97 var previousColumn = -Infinity; |
|
98 map.eachMapping(function (mapping) { |
|
99 assert.ok(mapping.generatedLine >= previousLine); |
|
100 |
|
101 if (mapping.source) { |
|
102 assert.equal(mapping.source.indexOf(util.testMap.sourceRoot), 0); |
|
103 } |
|
104 |
|
105 if (mapping.generatedLine === previousLine) { |
|
106 assert.ok(mapping.generatedColumn >= previousColumn); |
|
107 previousColumn = mapping.generatedColumn; |
|
108 } |
|
109 else { |
|
110 previousLine = mapping.generatedLine; |
|
111 previousColumn = -Infinity; |
|
112 } |
|
113 }); |
|
114 }; |
|
115 |
|
116 exports['test iterating over mappings in a different order'] = function (assert, util) { |
|
117 var map = new SourceMapConsumer(util.testMap); |
|
118 var previousLine = -Infinity; |
|
119 var previousColumn = -Infinity; |
|
120 var previousSource = ""; |
|
121 map.eachMapping(function (mapping) { |
|
122 assert.ok(mapping.source >= previousSource); |
|
123 |
|
124 if (mapping.source === previousSource) { |
|
125 assert.ok(mapping.originalLine >= previousLine); |
|
126 |
|
127 if (mapping.originalLine === previousLine) { |
|
128 assert.ok(mapping.originalColumn >= previousColumn); |
|
129 previousColumn = mapping.originalColumn; |
|
130 } |
|
131 else { |
|
132 previousLine = mapping.originalLine; |
|
133 previousColumn = -Infinity; |
|
134 } |
|
135 } |
|
136 else { |
|
137 previousSource = mapping.source; |
|
138 previousLine = -Infinity; |
|
139 previousColumn = -Infinity; |
|
140 } |
|
141 }, null, SourceMapConsumer.ORIGINAL_ORDER); |
|
142 }; |
|
143 |
|
144 exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) { |
|
145 var map = new SourceMapConsumer(util.testMap); |
|
146 var context = {}; |
|
147 map.eachMapping(function () { |
|
148 assert.equal(this, context); |
|
149 }, context); |
|
150 }; |
|
151 |
|
152 exports['test that the `sourcesContent` field has the original sources'] = function (assert, util) { |
|
153 var map = new SourceMapConsumer(util.testMapWithSourcesContent); |
|
154 var sourcesContent = map.sourcesContent; |
|
155 |
|
156 assert.equal(sourcesContent[0], ' ONE.foo = function (bar) {\n return baz(bar);\n };'); |
|
157 assert.equal(sourcesContent[1], ' TWO.inc = function (n) {\n return n + 1;\n };'); |
|
158 assert.equal(sourcesContent.length, 2); |
|
159 }; |
|
160 |
|
161 exports['test that we can get the original sources for the sources'] = function (assert, util) { |
|
162 var map = new SourceMapConsumer(util.testMapWithSourcesContent); |
|
163 var sources = map.sources; |
|
164 |
|
165 assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };'); |
|
166 assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };'); |
|
167 assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };'); |
|
168 assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };'); |
|
169 assert.throws(function () { |
|
170 map.sourceContentFor(""); |
|
171 }, Error); |
|
172 assert.throws(function () { |
|
173 map.sourceContentFor("/the/root/three.js"); |
|
174 }, Error); |
|
175 assert.throws(function () { |
|
176 map.sourceContentFor("three.js"); |
|
177 }, Error); |
|
178 }; |
|
179 |
|
180 exports['test sourceRoot + generatedPositionFor'] = function (assert, util) { |
|
181 var map = new SourceMapGenerator({ |
|
182 sourceRoot: 'foo/bar', |
|
183 file: 'baz.js' |
|
184 }); |
|
185 map.addMapping({ |
|
186 original: { line: 1, column: 1 }, |
|
187 generated: { line: 2, column: 2 }, |
|
188 source: 'bang.coffee' |
|
189 }); |
|
190 map.addMapping({ |
|
191 original: { line: 5, column: 5 }, |
|
192 generated: { line: 6, column: 6 }, |
|
193 source: 'bang.coffee' |
|
194 }); |
|
195 map = new SourceMapConsumer(map.toString()); |
|
196 |
|
197 // Should handle without sourceRoot. |
|
198 var pos = map.generatedPositionFor({ |
|
199 line: 1, |
|
200 column: 1, |
|
201 source: 'bang.coffee' |
|
202 }); |
|
203 |
|
204 assert.equal(pos.line, 2); |
|
205 assert.equal(pos.column, 2); |
|
206 |
|
207 // Should handle with sourceRoot. |
|
208 var pos = map.generatedPositionFor({ |
|
209 line: 1, |
|
210 column: 1, |
|
211 source: 'foo/bar/bang.coffee' |
|
212 }); |
|
213 |
|
214 assert.equal(pos.line, 2); |
|
215 assert.equal(pos.column, 2); |
|
216 }; |
|
217 |
|
218 exports['test sourceRoot + originalPositionFor'] = function (assert, util) { |
|
219 var map = new SourceMapGenerator({ |
|
220 sourceRoot: 'foo/bar', |
|
221 file: 'baz.js' |
|
222 }); |
|
223 map.addMapping({ |
|
224 original: { line: 1, column: 1 }, |
|
225 generated: { line: 2, column: 2 }, |
|
226 source: 'bang.coffee' |
|
227 }); |
|
228 map = new SourceMapConsumer(map.toString()); |
|
229 |
|
230 var pos = map.originalPositionFor({ |
|
231 line: 2, |
|
232 column: 2, |
|
233 }); |
|
234 |
|
235 // Should always have the prepended source root |
|
236 assert.equal(pos.source, 'foo/bar/bang.coffee'); |
|
237 assert.equal(pos.line, 1); |
|
238 assert.equal(pos.column, 1); |
|
239 }; |
|
240 |
|
241 exports['test github issue #56'] = function (assert, util) { |
|
242 var map = new SourceMapGenerator({ |
|
243 sourceRoot: 'http://', |
|
244 file: 'www.example.com/foo.js' |
|
245 }); |
|
246 map.addMapping({ |
|
247 original: { line: 1, column: 1 }, |
|
248 generated: { line: 2, column: 2 }, |
|
249 source: 'www.example.com/original.js' |
|
250 }); |
|
251 map = new SourceMapConsumer(map.toString()); |
|
252 |
|
253 var sources = map.sources; |
|
254 assert.equal(sources.length, 1); |
|
255 assert.equal(sources[0], 'http://www.example.com/original.js'); |
|
256 }; |
|
257 |
|
258 exports['test github issue #43'] = function (assert, util) { |
|
259 var map = new SourceMapGenerator({ |
|
260 sourceRoot: 'http://example.com', |
|
261 file: 'foo.js' |
|
262 }); |
|
263 map.addMapping({ |
|
264 original: { line: 1, column: 1 }, |
|
265 generated: { line: 2, column: 2 }, |
|
266 source: 'http://cdn.example.com/original.js' |
|
267 }); |
|
268 map = new SourceMapConsumer(map.toString()); |
|
269 |
|
270 var sources = map.sources; |
|
271 assert.equal(sources.length, 1, |
|
272 'Should only be one source.'); |
|
273 assert.equal(sources[0], 'http://cdn.example.com/original.js', |
|
274 'Should not be joined with the sourceRoot.'); |
|
275 }; |
|
276 |
|
277 exports['test absolute path, but same host sources'] = function (assert, util) { |
|
278 var map = new SourceMapGenerator({ |
|
279 sourceRoot: 'http://example.com/foo/bar', |
|
280 file: 'foo.js' |
|
281 }); |
|
282 map.addMapping({ |
|
283 original: { line: 1, column: 1 }, |
|
284 generated: { line: 2, column: 2 }, |
|
285 source: '/original.js' |
|
286 }); |
|
287 map = new SourceMapConsumer(map.toString()); |
|
288 |
|
289 var sources = map.sources; |
|
290 assert.equal(sources.length, 1, |
|
291 'Should only be one source.'); |
|
292 assert.equal(sources[0], 'http://example.com/original.js', |
|
293 'Source should be relative the host of the source root.'); |
|
294 }; |
|
295 |
|
296 exports['test github issue #64'] = function (assert, util) { |
|
297 var map = new SourceMapConsumer({ |
|
298 "version": 3, |
|
299 "file": "foo.js", |
|
300 "sourceRoot": "http://example.com/", |
|
301 "sources": ["/a"], |
|
302 "names": [], |
|
303 "mappings": "AACA", |
|
304 "sourcesContent": ["foo"] |
|
305 }); |
|
306 |
|
307 assert.equal(map.sourceContentFor("a"), "foo"); |
|
308 assert.equal(map.sourceContentFor("/a"), "foo"); |
|
309 }; |
|
310 |
|
311 exports['test bug 885597'] = function (assert, util) { |
|
312 var map = new SourceMapConsumer({ |
|
313 "version": 3, |
|
314 "file": "foo.js", |
|
315 "sourceRoot": "file:///Users/AlGore/Invented/The/Internet/", |
|
316 "sources": ["/a"], |
|
317 "names": [], |
|
318 "mappings": "AACA", |
|
319 "sourcesContent": ["foo"] |
|
320 }); |
|
321 |
|
322 var s = map.sources[0]; |
|
323 assert.equal(map.sourceContentFor(s), "foo"); |
|
324 }; |
|
325 |
|
326 exports['test github issue #72, duplicate sources'] = function (assert, util) { |
|
327 var map = new SourceMapConsumer({ |
|
328 "version": 3, |
|
329 "file": "foo.js", |
|
330 "sources": ["source1.js", "source1.js", "source3.js"], |
|
331 "names": [], |
|
332 "mappings": ";EAAC;;IAEE;;MEEE", |
|
333 "sourceRoot": "http://example.com" |
|
334 }); |
|
335 |
|
336 var pos = map.originalPositionFor({ |
|
337 line: 2, |
|
338 column: 2 |
|
339 }); |
|
340 assert.equal(pos.source, 'http://example.com/source1.js'); |
|
341 assert.equal(pos.line, 1); |
|
342 assert.equal(pos.column, 1); |
|
343 |
|
344 var pos = map.originalPositionFor({ |
|
345 line: 4, |
|
346 column: 4 |
|
347 }); |
|
348 assert.equal(pos.source, 'http://example.com/source1.js'); |
|
349 assert.equal(pos.line, 3); |
|
350 assert.equal(pos.column, 3); |
|
351 |
|
352 var pos = map.originalPositionFor({ |
|
353 line: 6, |
|
354 column: 6 |
|
355 }); |
|
356 assert.equal(pos.source, 'http://example.com/source3.js'); |
|
357 assert.equal(pos.line, 5); |
|
358 assert.equal(pos.column, 5); |
|
359 }; |
|
360 |
|
361 exports['test github issue #72, duplicate names'] = function (assert, util) { |
|
362 var map = new SourceMapConsumer({ |
|
363 "version": 3, |
|
364 "file": "foo.js", |
|
365 "sources": ["source.js"], |
|
366 "names": ["name1", "name1", "name3"], |
|
367 "mappings": ";EAACA;;IAEEA;;MAEEE", |
|
368 "sourceRoot": "http://example.com" |
|
369 }); |
|
370 |
|
371 var pos = map.originalPositionFor({ |
|
372 line: 2, |
|
373 column: 2 |
|
374 }); |
|
375 assert.equal(pos.name, 'name1'); |
|
376 assert.equal(pos.line, 1); |
|
377 assert.equal(pos.column, 1); |
|
378 |
|
379 var pos = map.originalPositionFor({ |
|
380 line: 4, |
|
381 column: 4 |
|
382 }); |
|
383 assert.equal(pos.name, 'name1'); |
|
384 assert.equal(pos.line, 3); |
|
385 assert.equal(pos.column, 3); |
|
386 |
|
387 var pos = map.originalPositionFor({ |
|
388 line: 6, |
|
389 column: 6 |
|
390 }); |
|
391 assert.equal(pos.name, 'name3'); |
|
392 assert.equal(pos.line, 5); |
|
393 assert.equal(pos.column, 5); |
|
394 }; |
|
395 |
|
396 exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) { |
|
397 var smg = new SourceMapGenerator({ |
|
398 sourceRoot: 'http://example.com/', |
|
399 file: 'foo.js' |
|
400 }); |
|
401 smg.addMapping({ |
|
402 original: { line: 1, column: 1 }, |
|
403 generated: { line: 2, column: 2 }, |
|
404 source: 'bar.js' |
|
405 }); |
|
406 smg.addMapping({ |
|
407 original: { line: 2, column: 2 }, |
|
408 generated: { line: 4, column: 4 }, |
|
409 source: 'baz.js', |
|
410 name: 'dirtMcGirt' |
|
411 }); |
|
412 smg.setSourceContent('baz.js', 'baz.js content'); |
|
413 |
|
414 var smc = SourceMapConsumer.fromSourceMap(smg); |
|
415 assert.equal(smc.file, 'foo.js'); |
|
416 assert.equal(smc.sourceRoot, 'http://example.com/'); |
|
417 assert.equal(smc.sources.length, 2); |
|
418 assert.equal(smc.sources[0], 'http://example.com/bar.js'); |
|
419 assert.equal(smc.sources[1], 'http://example.com/baz.js'); |
|
420 assert.equal(smc.sourceContentFor('baz.js'), 'baz.js content'); |
|
421 |
|
422 var pos = smc.originalPositionFor({ |
|
423 line: 2, |
|
424 column: 2 |
|
425 }); |
|
426 assert.equal(pos.line, 1); |
|
427 assert.equal(pos.column, 1); |
|
428 assert.equal(pos.source, 'http://example.com/bar.js'); |
|
429 assert.equal(pos.name, null); |
|
430 |
|
431 pos = smc.generatedPositionFor({ |
|
432 line: 1, |
|
433 column: 1, |
|
434 source: 'http://example.com/bar.js' |
|
435 }); |
|
436 assert.equal(pos.line, 2); |
|
437 assert.equal(pos.column, 2); |
|
438 |
|
439 pos = smc.originalPositionFor({ |
|
440 line: 4, |
|
441 column: 4 |
|
442 }); |
|
443 assert.equal(pos.line, 2); |
|
444 assert.equal(pos.column, 2); |
|
445 assert.equal(pos.source, 'http://example.com/baz.js'); |
|
446 assert.equal(pos.name, 'dirtMcGirt'); |
|
447 |
|
448 pos = smc.generatedPositionFor({ |
|
449 line: 2, |
|
450 column: 2, |
|
451 source: 'http://example.com/baz.js' |
|
452 }); |
|
453 assert.equal(pos.line, 4); |
|
454 assert.equal(pos.column, 4); |
|
455 }; |
|
456 }); |
|
457 function run_test() { |
|
458 runSourceMapTests('test/source-map/test-source-map-consumer', do_throw); |
|
459 } |