|
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-generator", ["require", "exports", "module"], function (require, exports, module) { |
|
16 |
|
17 var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator; |
|
18 var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer; |
|
19 var SourceNode = require('source-map/source-node').SourceNode; |
|
20 var util = require('source-map/util'); |
|
21 |
|
22 exports['test some simple stuff'] = function (assert, util) { |
|
23 var map = new SourceMapGenerator({ |
|
24 file: 'foo.js', |
|
25 sourceRoot: '.' |
|
26 }); |
|
27 assert.ok(true); |
|
28 }; |
|
29 |
|
30 exports['test JSON serialization'] = function (assert, util) { |
|
31 var map = new SourceMapGenerator({ |
|
32 file: 'foo.js', |
|
33 sourceRoot: '.' |
|
34 }); |
|
35 assert.equal(map.toString(), JSON.stringify(map)); |
|
36 }; |
|
37 |
|
38 exports['test adding mappings (case 1)'] = function (assert, util) { |
|
39 var map = new SourceMapGenerator({ |
|
40 file: 'generated-foo.js', |
|
41 sourceRoot: '.' |
|
42 }); |
|
43 |
|
44 assert.doesNotThrow(function () { |
|
45 map.addMapping({ |
|
46 generated: { line: 1, column: 1 } |
|
47 }); |
|
48 }); |
|
49 }; |
|
50 |
|
51 exports['test adding mappings (case 2)'] = function (assert, util) { |
|
52 var map = new SourceMapGenerator({ |
|
53 file: 'generated-foo.js', |
|
54 sourceRoot: '.' |
|
55 }); |
|
56 |
|
57 assert.doesNotThrow(function () { |
|
58 map.addMapping({ |
|
59 generated: { line: 1, column: 1 }, |
|
60 source: 'bar.js', |
|
61 original: { line: 1, column: 1 } |
|
62 }); |
|
63 }); |
|
64 }; |
|
65 |
|
66 exports['test adding mappings (case 3)'] = function (assert, util) { |
|
67 var map = new SourceMapGenerator({ |
|
68 file: 'generated-foo.js', |
|
69 sourceRoot: '.' |
|
70 }); |
|
71 |
|
72 assert.doesNotThrow(function () { |
|
73 map.addMapping({ |
|
74 generated: { line: 1, column: 1 }, |
|
75 source: 'bar.js', |
|
76 original: { line: 1, column: 1 }, |
|
77 name: 'someToken' |
|
78 }); |
|
79 }); |
|
80 }; |
|
81 |
|
82 exports['test adding mappings (invalid)'] = function (assert, util) { |
|
83 var map = new SourceMapGenerator({ |
|
84 file: 'generated-foo.js', |
|
85 sourceRoot: '.' |
|
86 }); |
|
87 |
|
88 // Not enough info. |
|
89 assert.throws(function () { |
|
90 map.addMapping({}); |
|
91 }); |
|
92 |
|
93 // Original file position, but no source. |
|
94 assert.throws(function () { |
|
95 map.addMapping({ |
|
96 generated: { line: 1, column: 1 }, |
|
97 original: { line: 1, column: 1 } |
|
98 }); |
|
99 }); |
|
100 }; |
|
101 |
|
102 exports['test that the correct mappings are being generated'] = function (assert, util) { |
|
103 var map = new SourceMapGenerator({ |
|
104 file: 'min.js', |
|
105 sourceRoot: '/the/root' |
|
106 }); |
|
107 |
|
108 map.addMapping({ |
|
109 generated: { line: 1, column: 1 }, |
|
110 original: { line: 1, column: 1 }, |
|
111 source: 'one.js' |
|
112 }); |
|
113 map.addMapping({ |
|
114 generated: { line: 1, column: 5 }, |
|
115 original: { line: 1, column: 5 }, |
|
116 source: 'one.js' |
|
117 }); |
|
118 map.addMapping({ |
|
119 generated: { line: 1, column: 9 }, |
|
120 original: { line: 1, column: 11 }, |
|
121 source: 'one.js' |
|
122 }); |
|
123 map.addMapping({ |
|
124 generated: { line: 1, column: 18 }, |
|
125 original: { line: 1, column: 21 }, |
|
126 source: 'one.js', |
|
127 name: 'bar' |
|
128 }); |
|
129 map.addMapping({ |
|
130 generated: { line: 1, column: 21 }, |
|
131 original: { line: 2, column: 3 }, |
|
132 source: 'one.js' |
|
133 }); |
|
134 map.addMapping({ |
|
135 generated: { line: 1, column: 28 }, |
|
136 original: { line: 2, column: 10 }, |
|
137 source: 'one.js', |
|
138 name: 'baz' |
|
139 }); |
|
140 map.addMapping({ |
|
141 generated: { line: 1, column: 32 }, |
|
142 original: { line: 2, column: 14 }, |
|
143 source: 'one.js', |
|
144 name: 'bar' |
|
145 }); |
|
146 |
|
147 map.addMapping({ |
|
148 generated: { line: 2, column: 1 }, |
|
149 original: { line: 1, column: 1 }, |
|
150 source: 'two.js' |
|
151 }); |
|
152 map.addMapping({ |
|
153 generated: { line: 2, column: 5 }, |
|
154 original: { line: 1, column: 5 }, |
|
155 source: 'two.js' |
|
156 }); |
|
157 map.addMapping({ |
|
158 generated: { line: 2, column: 9 }, |
|
159 original: { line: 1, column: 11 }, |
|
160 source: 'two.js' |
|
161 }); |
|
162 map.addMapping({ |
|
163 generated: { line: 2, column: 18 }, |
|
164 original: { line: 1, column: 21 }, |
|
165 source: 'two.js', |
|
166 name: 'n' |
|
167 }); |
|
168 map.addMapping({ |
|
169 generated: { line: 2, column: 21 }, |
|
170 original: { line: 2, column: 3 }, |
|
171 source: 'two.js' |
|
172 }); |
|
173 map.addMapping({ |
|
174 generated: { line: 2, column: 28 }, |
|
175 original: { line: 2, column: 10 }, |
|
176 source: 'two.js', |
|
177 name: 'n' |
|
178 }); |
|
179 |
|
180 map = JSON.parse(map.toString()); |
|
181 |
|
182 util.assertEqualMaps(assert, map, util.testMap); |
|
183 }; |
|
184 |
|
185 exports['test that source content can be set'] = function (assert, util) { |
|
186 var map = new SourceMapGenerator({ |
|
187 file: 'min.js', |
|
188 sourceRoot: '/the/root' |
|
189 }); |
|
190 map.addMapping({ |
|
191 generated: { line: 1, column: 1 }, |
|
192 original: { line: 1, column: 1 }, |
|
193 source: 'one.js' |
|
194 }); |
|
195 map.addMapping({ |
|
196 generated: { line: 2, column: 1 }, |
|
197 original: { line: 1, column: 1 }, |
|
198 source: 'two.js' |
|
199 }); |
|
200 map.setSourceContent('one.js', 'one file content'); |
|
201 |
|
202 map = JSON.parse(map.toString()); |
|
203 assert.equal(map.sources[0], 'one.js'); |
|
204 assert.equal(map.sources[1], 'two.js'); |
|
205 assert.equal(map.sourcesContent[0], 'one file content'); |
|
206 assert.equal(map.sourcesContent[1], null); |
|
207 }; |
|
208 |
|
209 exports['test .fromSourceMap'] = function (assert, util) { |
|
210 var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap)); |
|
211 util.assertEqualMaps(assert, map.toJSON(), util.testMap); |
|
212 }; |
|
213 |
|
214 exports['test .fromSourceMap with sourcesContent'] = function (assert, util) { |
|
215 var map = SourceMapGenerator.fromSourceMap( |
|
216 new SourceMapConsumer(util.testMapWithSourcesContent)); |
|
217 util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent); |
|
218 }; |
|
219 |
|
220 exports['test applySourceMap'] = function (assert, util) { |
|
221 var node = new SourceNode(null, null, null, [ |
|
222 new SourceNode(2, 0, 'fileX', 'lineX2\n'), |
|
223 'genA1\n', |
|
224 new SourceNode(2, 0, 'fileY', 'lineY2\n'), |
|
225 'genA2\n', |
|
226 new SourceNode(1, 0, 'fileX', 'lineX1\n'), |
|
227 'genA3\n', |
|
228 new SourceNode(1, 0, 'fileY', 'lineY1\n') |
|
229 ]); |
|
230 var mapStep1 = node.toStringWithSourceMap({ |
|
231 file: 'fileA' |
|
232 }).map; |
|
233 mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n'); |
|
234 mapStep1 = mapStep1.toJSON(); |
|
235 |
|
236 node = new SourceNode(null, null, null, [ |
|
237 'gen1\n', |
|
238 new SourceNode(1, 0, 'fileA', 'lineA1\n'), |
|
239 new SourceNode(2, 0, 'fileA', 'lineA2\n'), |
|
240 new SourceNode(3, 0, 'fileA', 'lineA3\n'), |
|
241 new SourceNode(4, 0, 'fileA', 'lineA4\n'), |
|
242 new SourceNode(1, 0, 'fileB', 'lineB1\n'), |
|
243 new SourceNode(2, 0, 'fileB', 'lineB2\n'), |
|
244 'gen2\n' |
|
245 ]); |
|
246 var mapStep2 = node.toStringWithSourceMap({ |
|
247 file: 'fileGen' |
|
248 }).map; |
|
249 mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n'); |
|
250 mapStep2 = mapStep2.toJSON(); |
|
251 |
|
252 node = new SourceNode(null, null, null, [ |
|
253 'gen1\n', |
|
254 new SourceNode(2, 0, 'fileX', 'lineA1\n'), |
|
255 new SourceNode(2, 0, 'fileA', 'lineA2\n'), |
|
256 new SourceNode(2, 0, 'fileY', 'lineA3\n'), |
|
257 new SourceNode(4, 0, 'fileA', 'lineA4\n'), |
|
258 new SourceNode(1, 0, 'fileB', 'lineB1\n'), |
|
259 new SourceNode(2, 0, 'fileB', 'lineB2\n'), |
|
260 'gen2\n' |
|
261 ]); |
|
262 var expectedMap = node.toStringWithSourceMap({ |
|
263 file: 'fileGen' |
|
264 }).map; |
|
265 expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n'); |
|
266 expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n'); |
|
267 expectedMap = expectedMap.toJSON(); |
|
268 |
|
269 // apply source map "mapStep1" to "mapStep2" |
|
270 var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2)); |
|
271 generator.applySourceMap(new SourceMapConsumer(mapStep1)); |
|
272 var actualMap = generator.toJSON(); |
|
273 |
|
274 util.assertEqualMaps(assert, actualMap, expectedMap); |
|
275 }; |
|
276 |
|
277 exports['test sorting with duplicate generated mappings'] = function (assert, util) { |
|
278 var map = new SourceMapGenerator({ |
|
279 file: 'test.js' |
|
280 }); |
|
281 map.addMapping({ |
|
282 generated: { line: 3, column: 0 }, |
|
283 original: { line: 2, column: 0 }, |
|
284 source: 'a.js' |
|
285 }); |
|
286 map.addMapping({ |
|
287 generated: { line: 2, column: 0 } |
|
288 }); |
|
289 map.addMapping({ |
|
290 generated: { line: 2, column: 0 } |
|
291 }); |
|
292 map.addMapping({ |
|
293 generated: { line: 1, column: 0 }, |
|
294 original: { line: 1, column: 0 }, |
|
295 source: 'a.js' |
|
296 }); |
|
297 |
|
298 util.assertEqualMaps(assert, map.toJSON(), { |
|
299 version: 3, |
|
300 file: 'test.js', |
|
301 sources: ['a.js'], |
|
302 names: [], |
|
303 mappings: 'AAAA;A;AACA' |
|
304 }); |
|
305 }; |
|
306 |
|
307 exports['test ignore duplicate mappings.'] = function (assert, util) { |
|
308 var init = { file: 'min.js', sourceRoot: '/the/root' }; |
|
309 var map1, map2; |
|
310 |
|
311 // null original source location |
|
312 var nullMapping1 = { |
|
313 generated: { line: 1, column: 0 } |
|
314 }; |
|
315 var nullMapping2 = { |
|
316 generated: { line: 2, column: 2 } |
|
317 }; |
|
318 |
|
319 map1 = new SourceMapGenerator(init); |
|
320 map2 = new SourceMapGenerator(init); |
|
321 |
|
322 map1.addMapping(nullMapping1); |
|
323 map1.addMapping(nullMapping1); |
|
324 |
|
325 map2.addMapping(nullMapping1); |
|
326 |
|
327 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); |
|
328 |
|
329 map1.addMapping(nullMapping2); |
|
330 map1.addMapping(nullMapping1); |
|
331 |
|
332 map2.addMapping(nullMapping2); |
|
333 |
|
334 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); |
|
335 |
|
336 // original source location |
|
337 var srcMapping1 = { |
|
338 generated: { line: 1, column: 0 }, |
|
339 original: { line: 11, column: 0 }, |
|
340 source: 'srcMapping1.js' |
|
341 }; |
|
342 var srcMapping2 = { |
|
343 generated: { line: 2, column: 2 }, |
|
344 original: { line: 11, column: 0 }, |
|
345 source: 'srcMapping2.js' |
|
346 }; |
|
347 |
|
348 map1 = new SourceMapGenerator(init); |
|
349 map2 = new SourceMapGenerator(init); |
|
350 |
|
351 map1.addMapping(srcMapping1); |
|
352 map1.addMapping(srcMapping1); |
|
353 |
|
354 map2.addMapping(srcMapping1); |
|
355 |
|
356 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); |
|
357 |
|
358 map1.addMapping(srcMapping2); |
|
359 map1.addMapping(srcMapping1); |
|
360 |
|
361 map2.addMapping(srcMapping2); |
|
362 |
|
363 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); |
|
364 |
|
365 // full original source and name information |
|
366 var fullMapping1 = { |
|
367 generated: { line: 1, column: 0 }, |
|
368 original: { line: 11, column: 0 }, |
|
369 source: 'fullMapping1.js', |
|
370 name: 'fullMapping1' |
|
371 }; |
|
372 var fullMapping2 = { |
|
373 generated: { line: 2, column: 2 }, |
|
374 original: { line: 11, column: 0 }, |
|
375 source: 'fullMapping2.js', |
|
376 name: 'fullMapping2' |
|
377 }; |
|
378 |
|
379 map1 = new SourceMapGenerator(init); |
|
380 map2 = new SourceMapGenerator(init); |
|
381 |
|
382 map1.addMapping(fullMapping1); |
|
383 map1.addMapping(fullMapping1); |
|
384 |
|
385 map2.addMapping(fullMapping1); |
|
386 |
|
387 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); |
|
388 |
|
389 map1.addMapping(fullMapping2); |
|
390 map1.addMapping(fullMapping1); |
|
391 |
|
392 map2.addMapping(fullMapping2); |
|
393 |
|
394 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); |
|
395 }; |
|
396 |
|
397 exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) { |
|
398 var map = new SourceMapGenerator({ |
|
399 file: 'test.js' |
|
400 }); |
|
401 map.addMapping({ |
|
402 generated: { line: 1, column: 1 }, |
|
403 original: { line: 2, column: 2 }, |
|
404 source: 'a.js', |
|
405 name: 'foo' |
|
406 }); |
|
407 map.addMapping({ |
|
408 generated: { line: 3, column: 3 }, |
|
409 original: { line: 4, column: 4 }, |
|
410 source: 'a.js', |
|
411 name: 'foo' |
|
412 }); |
|
413 util.assertEqualMaps(assert, map.toJSON(), { |
|
414 version: 3, |
|
415 file: 'test.js', |
|
416 sources: ['a.js'], |
|
417 names: ['foo'], |
|
418 mappings: 'CACEA;;GAEEA' |
|
419 }); |
|
420 }; |
|
421 |
|
422 }); |
|
423 function run_test() { |
|
424 runSourceMapTests('test/source-map/test-source-map-generator', do_throw); |
|
425 } |