Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / escodegen / node_modules / source-map / test / source-map / test-source-map-consumer.js
1 /* -*- Mode: js; js-indent-level: 2; -*- */
2 /*
3  * Copyright 2011 Mozilla Foundation and contributors
4  * Licensed under the New BSD license. See LICENSE or:
5  * http://opensource.org/licenses/BSD-3-Clause
6  */
7 if (typeof define !== 'function') {
8     var define = require('amdefine')(module, require);
9 }
10 define(function (require, exports, module) {
11
12   var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;
13   var IndexedSourceMapConsumer = require('../../lib/source-map/indexed-source-map-consumer').IndexedSourceMapConsumer;
14   var BasicSourceMapConsumer = require('../../lib/source-map/basic-source-map-consumer').BasicSourceMapConsumer;
15   var SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;
16
17   exports['test that we can instantiate with a string or an object'] = function (assert, util) {
18     assert.doesNotThrow(function () {
19       var map = new SourceMapConsumer(util.testMap);
20     });
21     assert.doesNotThrow(function () {
22       var map = new SourceMapConsumer(JSON.stringify(util.testMap));
23     });
24   };
25
26   exports['test that the object returned from new SourceMapConsumer inherits from SourceMapConsumer'] = function (assert, util) {
27     assert.ok(new SourceMapConsumer(util.testMap) instanceof SourceMapConsumer);
28   }
29
30   exports['test that a BasicSourceMapConsumer is returned for sourcemaps without sections'] = function(assert, util) {
31     assert.ok(new SourceMapConsumer(util.testMap) instanceof BasicSourceMapConsumer);
32   };
33
34   exports['test that an IndexedSourceMapConsumer is returned for sourcemaps with sections'] = function(assert, util) {
35     assert.ok(new SourceMapConsumer(util.indexedTestMap) instanceof IndexedSourceMapConsumer);
36   };
37
38   exports['test that the `sources` field has the original sources'] = function (assert, util) {
39     var map;
40     var sources;
41
42     map = new SourceMapConsumer(util.testMap);
43     sources = map.sources;
44     assert.equal(sources[0], '/the/root/one.js');
45     assert.equal(sources[1], '/the/root/two.js');
46     assert.equal(sources.length, 2);
47
48     map = new SourceMapConsumer(util.indexedTestMap);
49     sources = map.sources;
50     assert.equal(sources[0], '/the/root/one.js');
51     assert.equal(sources[1], '/the/root/two.js');
52     assert.equal(sources.length, 2);
53
54     map = new SourceMapConsumer(util.indexedTestMapDifferentSourceRoots);
55     sources = map.sources;
56     assert.equal(sources[0], '/the/root/one.js');
57     assert.equal(sources[1], '/different/root/two.js');
58     assert.equal(sources.length, 2);
59
60     map = new SourceMapConsumer(util.testMapNoSourceRoot);
61     sources = map.sources;
62     assert.equal(sources[0], 'one.js');
63     assert.equal(sources[1], 'two.js');
64     assert.equal(sources.length, 2);
65
66     map = new SourceMapConsumer(util.testMapEmptySourceRoot);
67     sources = map.sources;
68     assert.equal(sources[0], 'one.js');
69     assert.equal(sources[1], 'two.js');
70     assert.equal(sources.length, 2);
71   };
72
73   exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) {
74     var map;
75     var mapping;
76
77     map = new SourceMapConsumer(util.testMap);
78
79     mapping = map.originalPositionFor({
80       line: 2,
81       column: 1
82     });
83     assert.equal(mapping.source, '/the/root/two.js');
84
85     mapping = map.originalPositionFor({
86       line: 1,
87       column: 1
88     });
89     assert.equal(mapping.source, '/the/root/one.js');
90
91
92     map = new SourceMapConsumer(util.testMapNoSourceRoot);
93
94     mapping = map.originalPositionFor({
95       line: 2,
96       column: 1
97     });
98     assert.equal(mapping.source, 'two.js');
99
100     mapping = map.originalPositionFor({
101       line: 1,
102       column: 1
103     });
104     assert.equal(mapping.source, 'one.js');
105
106
107     map = new SourceMapConsumer(util.testMapEmptySourceRoot);
108
109     mapping = map.originalPositionFor({
110       line: 2,
111       column: 1
112     });
113     assert.equal(mapping.source, 'two.js');
114
115     mapping = map.originalPositionFor({
116       line: 1,
117       column: 1
118     });
119     assert.equal(mapping.source, 'one.js');
120   };
121
122   exports['test mapping tokens back exactly'] = function (assert, util) {
123     var map = new SourceMapConsumer(util.testMap);
124
125     util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert);
126     util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert);
127     util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert);
128     util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert);
129     util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert);
130     util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert);
131     util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert);
132
133     util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert);
134     util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert);
135     util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert);
136     util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert);
137     util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert);
138     util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert);
139   };
140
141   exports['test mapping tokens back exactly in indexed source map'] = function (assert, util) {
142     var map = new SourceMapConsumer(util.indexedTestMap);
143
144     util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert);
145     util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert);
146     util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert);
147     util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert);
148     util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert);
149     util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert);
150     util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert);
151
152     util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert);
153     util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert);
154     util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert);
155     util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert);
156     util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert);
157     util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert);
158   };
159
160
161   exports['test mapping tokens back exactly'] = function (assert, util) {
162     var map = new SourceMapConsumer(util.testMap);
163
164     util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert);
165     util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert);
166     util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert);
167     util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert);
168     util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert);
169     util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert);
170     util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert);
171
172     util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert);
173     util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert);
174     util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert);
175     util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert);
176     util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert);
177     util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert);
178   };
179
180   exports['test mapping tokens fuzzy'] = function (assert, util) {
181     var map = new SourceMapConsumer(util.testMap);
182
183     // Finding original positions
184     util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true);
185     util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true);
186     util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true);
187
188     // Finding generated positions
189     util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true);
190     util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true);
191     util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true);
192   };
193
194   exports['test mapping tokens fuzzy in indexed source map'] = function (assert, util) {
195     var map = new SourceMapConsumer(util.indexedTestMap);
196
197     // Finding original positions
198     util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true);
199     util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true);
200     util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true);
201
202     // Finding generated positions
203     util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true);
204     util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true);
205     util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true);
206   };
207
208   exports['test mappings and end of lines'] = function (assert, util) {
209     var smg = new SourceMapGenerator({
210       file: 'foo.js'
211     });
212     smg.addMapping({
213       original: { line: 1, column: 1 },
214       generated: { line: 1, column: 1 },
215       source: 'bar.js'
216     });
217     smg.addMapping({
218       original: { line: 2, column: 2 },
219       generated: { line: 2, column: 2 },
220       source: 'bar.js'
221     });
222
223     var map = SourceMapConsumer.fromSourceMap(smg);
224
225     // When finding original positions, mappings end at the end of the line.
226     util.assertMapping(2, 1, null, null, null, null, map, assert, true)
227
228     // When finding generated positions, mappings do not end at the end of the line.
229     util.assertMapping(1, 1, 'bar.js', 2, 1, null, map, assert, null, true);
230   };
231
232   exports['test creating source map consumers with )]}\' prefix'] = function (assert, util) {
233     assert.doesNotThrow(function () {
234       var map = new SourceMapConsumer(")]}'" + JSON.stringify(util.testMap));
235     });
236   };
237
238   exports['test eachMapping'] = function (assert, util) {
239     var map;
240
241     map = new SourceMapConsumer(util.testMap);
242     var previousLine = -Infinity;
243     var previousColumn = -Infinity;
244     map.eachMapping(function (mapping) {
245       assert.ok(mapping.generatedLine >= previousLine);
246
247       assert.ok(mapping.source === '/the/root/one.js' || mapping.source === '/the/root/two.js');
248
249       if (mapping.generatedLine === previousLine) {
250         assert.ok(mapping.generatedColumn >= previousColumn);
251         previousColumn = mapping.generatedColumn;
252       }
253       else {
254         previousLine = mapping.generatedLine;
255         previousColumn = -Infinity;
256       }
257     });
258
259     map = new SourceMapConsumer(util.testMapNoSourceRoot);
260     map.eachMapping(function (mapping) {
261       assert.ok(mapping.source === 'one.js' || mapping.source === 'two.js');
262     });
263
264     map = new SourceMapConsumer(util.testMapEmptySourceRoot);
265     map.eachMapping(function (mapping) {
266       assert.ok(mapping.source === 'one.js' || mapping.source === 'two.js');
267     });
268   };
269
270   exports['test eachMapping for indexed source maps'] = function(assert, util) {
271     var map = new SourceMapConsumer(util.indexedTestMap);
272     var previousLine = -Infinity;
273     var previousColumn = -Infinity;
274     map.eachMapping(function (mapping) {
275       assert.ok(mapping.generatedLine >= previousLine);
276
277       if (mapping.source) {
278         assert.equal(mapping.source.indexOf(util.testMap.sourceRoot), 0);
279       }
280
281       if (mapping.generatedLine === previousLine) {
282         assert.ok(mapping.generatedColumn >= previousColumn);
283         previousColumn = mapping.generatedColumn;
284       }
285       else {
286         previousLine = mapping.generatedLine;
287         previousColumn = -Infinity;
288       }
289     });
290   };
291
292
293   exports['test iterating over mappings in a different order'] = function (assert, util) {
294     var map = new SourceMapConsumer(util.testMap);
295     var previousLine = -Infinity;
296     var previousColumn = -Infinity;
297     var previousSource = "";
298     map.eachMapping(function (mapping) {
299       assert.ok(mapping.source >= previousSource);
300
301       if (mapping.source === previousSource) {
302         assert.ok(mapping.originalLine >= previousLine);
303
304         if (mapping.originalLine === previousLine) {
305           assert.ok(mapping.originalColumn >= previousColumn);
306           previousColumn = mapping.originalColumn;
307         }
308         else {
309           previousLine = mapping.originalLine;
310           previousColumn = -Infinity;
311         }
312       }
313       else {
314         previousSource = mapping.source;
315         previousLine = -Infinity;
316         previousColumn = -Infinity;
317       }
318     }, null, SourceMapConsumer.ORIGINAL_ORDER);
319   };
320
321   exports['test iterating over mappings in a different order in indexed source maps'] = function (assert, util) {
322     var map = new SourceMapConsumer(util.indexedTestMap);
323     var previousLine = -Infinity;
324     var previousColumn = -Infinity;
325     var previousSource = "";
326     map.eachMapping(function (mapping) {
327       assert.ok(mapping.source >= previousSource);
328
329       if (mapping.source === previousSource) {
330         assert.ok(mapping.originalLine >= previousLine);
331
332         if (mapping.originalLine === previousLine) {
333           assert.ok(mapping.originalColumn >= previousColumn);
334           previousColumn = mapping.originalColumn;
335         }
336         else {
337           previousLine = mapping.originalLine;
338           previousColumn = -Infinity;
339         }
340       }
341       else {
342         previousSource = mapping.source;
343         previousLine = -Infinity;
344         previousColumn = -Infinity;
345       }
346     }, null, SourceMapConsumer.ORIGINAL_ORDER);
347   };
348
349   exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) {
350     var map = new SourceMapConsumer(util.testMap);
351     var context = {};
352     map.eachMapping(function () {
353       assert.equal(this, context);
354     }, context);
355   };
356
357   exports['test that we can set the context for `this` in eachMapping in indexed source maps'] = function (assert, util) {
358     var map = new SourceMapConsumer(util.indexedTestMap);
359     var context = {};
360     map.eachMapping(function () {
361       assert.equal(this, context);
362     }, context);
363   };
364
365   exports['test that the `sourcesContent` field has the original sources'] = function (assert, util) {
366     var map = new SourceMapConsumer(util.testMapWithSourcesContent);
367     var sourcesContent = map.sourcesContent;
368
369     assert.equal(sourcesContent[0], ' ONE.foo = function (bar) {\n   return baz(bar);\n };');
370     assert.equal(sourcesContent[1], ' TWO.inc = function (n) {\n   return n + 1;\n };');
371     assert.equal(sourcesContent.length, 2);
372   };
373
374   exports['test that we can get the original sources for the sources'] = function (assert, util) {
375     var map = new SourceMapConsumer(util.testMapWithSourcesContent);
376     var sources = map.sources;
377
378     assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n   return baz(bar);\n };');
379     assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n   return n + 1;\n };');
380     assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n   return baz(bar);\n };');
381     assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n   return n + 1;\n };');
382     assert.throws(function () {
383       map.sourceContentFor("");
384     }, Error);
385     assert.throws(function () {
386       map.sourceContentFor("/the/root/three.js");
387     }, Error);
388     assert.throws(function () {
389       map.sourceContentFor("three.js");
390     }, Error);
391   };
392
393   exports['test that we can get the original source content with relative source paths'] = function (assert, util) {
394     var map = new SourceMapConsumer(util.testMapRelativeSources);
395     var sources = map.sources;
396
397     assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n   return baz(bar);\n };');
398     assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n   return n + 1;\n };');
399     assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n   return baz(bar);\n };');
400     assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n   return n + 1;\n };');
401     assert.throws(function () {
402       map.sourceContentFor("");
403     }, Error);
404     assert.throws(function () {
405       map.sourceContentFor("/the/root/three.js");
406     }, Error);
407     assert.throws(function () {
408       map.sourceContentFor("three.js");
409     }, Error);
410   };
411
412   exports['test that we can get the original source content for the sources on an indexed source map'] = function (assert, util) {
413     var map = new SourceMapConsumer(util.indexedTestMap);
414     var sources = map.sources;
415
416     assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n   return baz(bar);\n };');
417     assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n   return n + 1;\n };');
418     assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n   return baz(bar);\n };');
419     assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n   return n + 1;\n };');
420     assert.throws(function () {
421       map.sourceContentFor("");
422     }, Error);
423     assert.throws(function () {
424       map.sourceContentFor("/the/root/three.js");
425     }, Error);
426     assert.throws(function () {
427       map.sourceContentFor("three.js");
428     }, Error);
429   };
430
431
432   exports['test sourceRoot + generatedPositionFor'] = function (assert, util) {
433     var map = new SourceMapGenerator({
434       sourceRoot: 'foo/bar',
435       file: 'baz.js'
436     });
437     map.addMapping({
438       original: { line: 1, column: 1 },
439       generated: { line: 2, column: 2 },
440       source: 'bang.coffee'
441     });
442     map.addMapping({
443       original: { line: 5, column: 5 },
444       generated: { line: 6, column: 6 },
445       source: 'bang.coffee'
446     });
447     map = new SourceMapConsumer(map.toString());
448
449     // Should handle without sourceRoot.
450     var pos = map.generatedPositionFor({
451       line: 1,
452       column: 1,
453       source: 'bang.coffee'
454     });
455
456     assert.equal(pos.line, 2);
457     assert.equal(pos.column, 2);
458
459     // Should handle with sourceRoot.
460     var pos = map.generatedPositionFor({
461       line: 1,
462       column: 1,
463       source: 'foo/bar/bang.coffee'
464     });
465
466     assert.equal(pos.line, 2);
467     assert.equal(pos.column, 2);
468   };
469
470   exports['test allGeneratedPositionsFor'] = function (assert, util) {
471     var map = new SourceMapGenerator({
472       file: 'generated.js'
473     });
474     map.addMapping({
475       original: { line: 1, column: 1 },
476       generated: { line: 2, column: 2 },
477       source: 'foo.coffee'
478     });
479     map.addMapping({
480       original: { line: 1, column: 1 },
481       generated: { line: 2, column: 2 },
482       source: 'bar.coffee'
483     });
484     map.addMapping({
485       original: { line: 2, column: 1 },
486       generated: { line: 3, column: 2 },
487       source: 'bar.coffee'
488     });
489     map.addMapping({
490       original: { line: 2, column: 2 },
491       generated: { line: 3, column: 3 },
492       source: 'bar.coffee'
493     });
494     map.addMapping({
495       original: { line: 3, column: 1 },
496       generated: { line: 4, column: 2 },
497       source: 'bar.coffee'
498     });
499     map = new SourceMapConsumer(map.toString());
500
501     var mappings = map.allGeneratedPositionsFor({
502       line: 2,
503       source: 'bar.coffee'
504     });
505
506     assert.equal(mappings.length, 2);
507     assert.equal(mappings[0].line, 3);
508     assert.equal(mappings[0].column, 2);
509     assert.equal(mappings[1].line, 3);
510     assert.equal(mappings[1].column, 3);
511   };
512
513   exports['test allGeneratedPositionsFor for line with no mappings'] = function (assert, util) {
514     var map = new SourceMapGenerator({
515       file: 'generated.js'
516     });
517     map.addMapping({
518       original: { line: 1, column: 1 },
519       generated: { line: 2, column: 2 },
520       source: 'foo.coffee'
521     });
522     map.addMapping({
523       original: { line: 1, column: 1 },
524       generated: { line: 2, column: 2 },
525       source: 'bar.coffee'
526     });
527     map.addMapping({
528       original: { line: 3, column: 1 },
529       generated: { line: 4, column: 2 },
530       source: 'bar.coffee'
531     });
532     map = new SourceMapConsumer(map.toString());
533
534     var mappings = map.allGeneratedPositionsFor({
535       line: 2,
536       source: 'bar.coffee'
537     });
538
539     assert.equal(mappings.length, 0);
540   };
541
542   exports['test allGeneratedPositionsFor source map with no mappings'] = function (assert, util) {
543     var map = new SourceMapGenerator({
544       file: 'generated.js'
545     });
546     map = new SourceMapConsumer(map.toString());
547
548     var mappings = map.allGeneratedPositionsFor({
549       line: 2,
550       source: 'bar.coffee'
551     });
552
553     assert.equal(mappings.length, 0);
554   };
555
556   exports['test computeColumnSpans'] = function (assert, util) {
557     var map = new SourceMapGenerator({
558       file: 'generated.js'
559     });
560     map.addMapping({
561       original: { line: 1, column: 1 },
562       generated: { line: 1, column: 1 },
563       source: 'foo.coffee'
564     });
565     map.addMapping({
566       original: { line: 2, column: 1 },
567       generated: { line: 2, column: 1 },
568       source: 'foo.coffee'
569     });
570     map.addMapping({
571       original: { line: 2, column: 2 },
572       generated: { line: 2, column: 10 },
573       source: 'foo.coffee'
574     });
575     map.addMapping({
576       original: { line: 2, column: 3 },
577       generated: { line: 2, column: 20 },
578       source: 'foo.coffee'
579     });
580     map.addMapping({
581       original: { line: 3, column: 1 },
582       generated: { line: 3, column: 1 },
583       source: 'foo.coffee'
584     });
585     map.addMapping({
586       original: { line: 3, column: 2 },
587       generated: { line: 3, column: 2 },
588       source: 'foo.coffee'
589     });
590     map = new SourceMapConsumer(map.toString());
591
592     map.computeColumnSpans();
593
594     var mappings = map.allGeneratedPositionsFor({
595       line: 1,
596       source: 'foo.coffee'
597     });
598
599     assert.equal(mappings.length, 1);
600     assert.equal(mappings[0].lastColumn, Infinity);
601
602     var mappings = map.allGeneratedPositionsFor({
603       line: 2,
604       source: 'foo.coffee'
605     });
606
607     assert.equal(mappings.length, 3);
608     assert.equal(mappings[0].lastColumn, 9);
609     assert.equal(mappings[1].lastColumn, 19);
610     assert.equal(mappings[2].lastColumn, Infinity);
611
612     var mappings = map.allGeneratedPositionsFor({
613       line: 3,
614       source: 'foo.coffee'
615     });
616
617     assert.equal(mappings.length, 2);
618     assert.equal(mappings[0].lastColumn, 1);
619     assert.equal(mappings[1].lastColumn, Infinity);
620   };
621
622   exports['test sourceRoot + originalPositionFor'] = function (assert, util) {
623     var map = new SourceMapGenerator({
624       sourceRoot: 'foo/bar',
625       file: 'baz.js'
626     });
627     map.addMapping({
628       original: { line: 1, column: 1 },
629       generated: { line: 2, column: 2 },
630       source: 'bang.coffee'
631     });
632     map = new SourceMapConsumer(map.toString());
633
634     var pos = map.originalPositionFor({
635       line: 2,
636       column: 2,
637     });
638
639     // Should always have the prepended source root
640     assert.equal(pos.source, 'foo/bar/bang.coffee');
641     assert.equal(pos.line, 1);
642     assert.equal(pos.column, 1);
643   };
644
645   exports['test github issue #56'] = function (assert, util) {
646     var map = new SourceMapGenerator({
647       sourceRoot: 'http://',
648       file: 'www.example.com/foo.js'
649     });
650     map.addMapping({
651       original: { line: 1, column: 1 },
652       generated: { line: 2, column: 2 },
653       source: 'www.example.com/original.js'
654     });
655     map = new SourceMapConsumer(map.toString());
656
657     var sources = map.sources;
658     assert.equal(sources.length, 1);
659     assert.equal(sources[0], 'http://www.example.com/original.js');
660   };
661
662   exports['test github issue #43'] = function (assert, util) {
663     var map = new SourceMapGenerator({
664       sourceRoot: 'http://example.com',
665       file: 'foo.js'
666     });
667     map.addMapping({
668       original: { line: 1, column: 1 },
669       generated: { line: 2, column: 2 },
670       source: 'http://cdn.example.com/original.js'
671     });
672     map = new SourceMapConsumer(map.toString());
673
674     var sources = map.sources;
675     assert.equal(sources.length, 1,
676                  'Should only be one source.');
677     assert.equal(sources[0], 'http://cdn.example.com/original.js',
678                  'Should not be joined with the sourceRoot.');
679   };
680
681   exports['test absolute path, but same host sources'] = function (assert, util) {
682     var map = new SourceMapGenerator({
683       sourceRoot: 'http://example.com/foo/bar',
684       file: 'foo.js'
685     });
686     map.addMapping({
687       original: { line: 1, column: 1 },
688       generated: { line: 2, column: 2 },
689       source: '/original.js'
690     });
691     map = new SourceMapConsumer(map.toString());
692
693     var sources = map.sources;
694     assert.equal(sources.length, 1,
695                  'Should only be one source.');
696     assert.equal(sources[0], 'http://example.com/original.js',
697                  'Source should be relative the host of the source root.');
698   };
699
700   exports['test indexed source map errors when sections are out of order by line'] = function(assert, util) {
701     // Make a deep copy of the indexedTestMap
702     var misorderedIndexedTestMap = JSON.parse(JSON.stringify(util.indexedTestMap));
703
704     misorderedIndexedTestMap.sections[0].offset = {
705       line: 2,
706       column: 0
707     };
708
709     assert.throws(function() {
710       new SourceMapConsumer(misorderedIndexedTestMap);
711     }, Error);
712   };
713
714   exports['test github issue #64'] = function (assert, util) {
715     var map = new SourceMapConsumer({
716       "version": 3,
717       "file": "foo.js",
718       "sourceRoot": "http://example.com/",
719       "sources": ["/a"],
720       "names": [],
721       "mappings": "AACA",
722       "sourcesContent": ["foo"]
723     });
724
725     assert.equal(map.sourceContentFor("a"), "foo");
726     assert.equal(map.sourceContentFor("/a"), "foo");
727   };
728
729   exports['test bug 885597'] = function (assert, util) {
730     var map = new SourceMapConsumer({
731       "version": 3,
732       "file": "foo.js",
733       "sourceRoot": "file:///Users/AlGore/Invented/The/Internet/",
734       "sources": ["/a"],
735       "names": [],
736       "mappings": "AACA",
737       "sourcesContent": ["foo"]
738     });
739
740     var s = map.sources[0];
741     assert.equal(map.sourceContentFor(s), "foo");
742   };
743
744   exports['test github issue #72, duplicate sources'] = function (assert, util) {
745     var map = new SourceMapConsumer({
746       "version": 3,
747       "file": "foo.js",
748       "sources": ["source1.js", "source1.js", "source3.js"],
749       "names": [],
750       "mappings": ";EAAC;;IAEE;;MEEE",
751       "sourceRoot": "http://example.com"
752     });
753
754     var pos = map.originalPositionFor({
755       line: 2,
756       column: 2
757     });
758     assert.equal(pos.source, 'http://example.com/source1.js');
759     assert.equal(pos.line, 1);
760     assert.equal(pos.column, 1);
761
762     var pos = map.originalPositionFor({
763       line: 4,
764       column: 4
765     });
766     assert.equal(pos.source, 'http://example.com/source1.js');
767     assert.equal(pos.line, 3);
768     assert.equal(pos.column, 3);
769
770     var pos = map.originalPositionFor({
771       line: 6,
772       column: 6
773     });
774     assert.equal(pos.source, 'http://example.com/source3.js');
775     assert.equal(pos.line, 5);
776     assert.equal(pos.column, 5);
777   };
778
779   exports['test github issue #72, duplicate names'] = function (assert, util) {
780     var map = new SourceMapConsumer({
781       "version": 3,
782       "file": "foo.js",
783       "sources": ["source.js"],
784       "names": ["name1", "name1", "name3"],
785       "mappings": ";EAACA;;IAEEA;;MAEEE",
786       "sourceRoot": "http://example.com"
787     });
788
789     var pos = map.originalPositionFor({
790       line: 2,
791       column: 2
792     });
793     assert.equal(pos.name, 'name1');
794     assert.equal(pos.line, 1);
795     assert.equal(pos.column, 1);
796
797     var pos = map.originalPositionFor({
798       line: 4,
799       column: 4
800     });
801     assert.equal(pos.name, 'name1');
802     assert.equal(pos.line, 3);
803     assert.equal(pos.column, 3);
804
805     var pos = map.originalPositionFor({
806       line: 6,
807       column: 6
808     });
809     assert.equal(pos.name, 'name3');
810     assert.equal(pos.line, 5);
811     assert.equal(pos.column, 5);
812   };
813
814   exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) {
815     var smg = new SourceMapGenerator({
816       sourceRoot: 'http://example.com/',
817       file: 'foo.js'
818     });
819     smg.addMapping({
820       original: { line: 1, column: 1 },
821       generated: { line: 2, column: 2 },
822       source: 'bar.js'
823     });
824     smg.addMapping({
825       original: { line: 2, column: 2 },
826       generated: { line: 4, column: 4 },
827       source: 'baz.js',
828       name: 'dirtMcGirt'
829     });
830     smg.setSourceContent('baz.js', 'baz.js content');
831
832     var smc = SourceMapConsumer.fromSourceMap(smg);
833     assert.equal(smc.file, 'foo.js');
834     assert.equal(smc.sourceRoot, 'http://example.com/');
835     assert.equal(smc.sources.length, 2);
836     assert.equal(smc.sources[0], 'http://example.com/bar.js');
837     assert.equal(smc.sources[1], 'http://example.com/baz.js');
838     assert.equal(smc.sourceContentFor('baz.js'), 'baz.js content');
839
840     var pos = smc.originalPositionFor({
841       line: 2,
842       column: 2
843     });
844     assert.equal(pos.line, 1);
845     assert.equal(pos.column, 1);
846     assert.equal(pos.source, 'http://example.com/bar.js');
847     assert.equal(pos.name, null);
848
849     pos = smc.generatedPositionFor({
850       line: 1,
851       column: 1,
852       source: 'http://example.com/bar.js'
853     });
854     assert.equal(pos.line, 2);
855     assert.equal(pos.column, 2);
856
857     pos = smc.originalPositionFor({
858       line: 4,
859       column: 4
860     });
861     assert.equal(pos.line, 2);
862     assert.equal(pos.column, 2);
863     assert.equal(pos.source, 'http://example.com/baz.js');
864     assert.equal(pos.name, 'dirtMcGirt');
865
866     pos = smc.generatedPositionFor({
867       line: 2,
868       column: 2,
869       source: 'http://example.com/baz.js'
870     });
871     assert.equal(pos.line, 4);
872     assert.equal(pos.column, 4);
873   };
874 });