michael@0: /* michael@0: * WARNING! michael@0: * michael@0: * Do not edit this file directly, it is built from the sources at michael@0: * https://github.com/mozilla/source-map/ michael@0: */ michael@0: michael@0: Components.utils.import('resource://test/Utils.jsm'); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define("test/source-map/test-source-map-generator", ["require", "exports", "module"], function (require, exports, module) { michael@0: michael@0: var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator; michael@0: var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer; michael@0: var SourceNode = require('source-map/source-node').SourceNode; michael@0: var util = require('source-map/util'); michael@0: michael@0: exports['test some simple stuff'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'foo.js', michael@0: sourceRoot: '.' michael@0: }); michael@0: assert.ok(true); michael@0: }; michael@0: michael@0: exports['test JSON serialization'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'foo.js', michael@0: sourceRoot: '.' michael@0: }); michael@0: assert.equal(map.toString(), JSON.stringify(map)); michael@0: }; michael@0: michael@0: exports['test adding mappings (case 1)'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'generated-foo.js', michael@0: sourceRoot: '.' michael@0: }); michael@0: michael@0: assert.doesNotThrow(function () { michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 1 } michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: exports['test adding mappings (case 2)'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'generated-foo.js', michael@0: sourceRoot: '.' michael@0: }); michael@0: michael@0: assert.doesNotThrow(function () { michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 1 }, michael@0: source: 'bar.js', michael@0: original: { line: 1, column: 1 } michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: exports['test adding mappings (case 3)'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'generated-foo.js', michael@0: sourceRoot: '.' michael@0: }); michael@0: michael@0: assert.doesNotThrow(function () { michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 1 }, michael@0: source: 'bar.js', michael@0: original: { line: 1, column: 1 }, michael@0: name: 'someToken' michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: exports['test adding mappings (invalid)'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'generated-foo.js', michael@0: sourceRoot: '.' michael@0: }); michael@0: michael@0: // Not enough info. michael@0: assert.throws(function () { michael@0: map.addMapping({}); michael@0: }); michael@0: michael@0: // Original file position, but no source. michael@0: assert.throws(function () { michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 1 }, michael@0: original: { line: 1, column: 1 } michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: exports['test that the correct mappings are being generated'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'min.js', michael@0: sourceRoot: '/the/root' michael@0: }); michael@0: michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 1 }, michael@0: original: { line: 1, column: 1 }, michael@0: source: 'one.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 5 }, michael@0: original: { line: 1, column: 5 }, michael@0: source: 'one.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 9 }, michael@0: original: { line: 1, column: 11 }, michael@0: source: 'one.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 18 }, michael@0: original: { line: 1, column: 21 }, michael@0: source: 'one.js', michael@0: name: 'bar' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 21 }, michael@0: original: { line: 2, column: 3 }, michael@0: source: 'one.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 28 }, michael@0: original: { line: 2, column: 10 }, michael@0: source: 'one.js', michael@0: name: 'baz' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 32 }, michael@0: original: { line: 2, column: 14 }, michael@0: source: 'one.js', michael@0: name: 'bar' michael@0: }); michael@0: michael@0: map.addMapping({ michael@0: generated: { line: 2, column: 1 }, michael@0: original: { line: 1, column: 1 }, michael@0: source: 'two.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 2, column: 5 }, michael@0: original: { line: 1, column: 5 }, michael@0: source: 'two.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 2, column: 9 }, michael@0: original: { line: 1, column: 11 }, michael@0: source: 'two.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 2, column: 18 }, michael@0: original: { line: 1, column: 21 }, michael@0: source: 'two.js', michael@0: name: 'n' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 2, column: 21 }, michael@0: original: { line: 2, column: 3 }, michael@0: source: 'two.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 2, column: 28 }, michael@0: original: { line: 2, column: 10 }, michael@0: source: 'two.js', michael@0: name: 'n' michael@0: }); michael@0: michael@0: map = JSON.parse(map.toString()); michael@0: michael@0: util.assertEqualMaps(assert, map, util.testMap); michael@0: }; michael@0: michael@0: exports['test that source content can be set'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'min.js', michael@0: sourceRoot: '/the/root' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 1 }, michael@0: original: { line: 1, column: 1 }, michael@0: source: 'one.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 2, column: 1 }, michael@0: original: { line: 1, column: 1 }, michael@0: source: 'two.js' michael@0: }); michael@0: map.setSourceContent('one.js', 'one file content'); michael@0: michael@0: map = JSON.parse(map.toString()); michael@0: assert.equal(map.sources[0], 'one.js'); michael@0: assert.equal(map.sources[1], 'two.js'); michael@0: assert.equal(map.sourcesContent[0], 'one file content'); michael@0: assert.equal(map.sourcesContent[1], null); michael@0: }; michael@0: michael@0: exports['test .fromSourceMap'] = function (assert, util) { michael@0: var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap)); michael@0: util.assertEqualMaps(assert, map.toJSON(), util.testMap); michael@0: }; michael@0: michael@0: exports['test .fromSourceMap with sourcesContent'] = function (assert, util) { michael@0: var map = SourceMapGenerator.fromSourceMap( michael@0: new SourceMapConsumer(util.testMapWithSourcesContent)); michael@0: util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent); michael@0: }; michael@0: michael@0: exports['test applySourceMap'] = function (assert, util) { michael@0: var node = new SourceNode(null, null, null, [ michael@0: new SourceNode(2, 0, 'fileX', 'lineX2\n'), michael@0: 'genA1\n', michael@0: new SourceNode(2, 0, 'fileY', 'lineY2\n'), michael@0: 'genA2\n', michael@0: new SourceNode(1, 0, 'fileX', 'lineX1\n'), michael@0: 'genA3\n', michael@0: new SourceNode(1, 0, 'fileY', 'lineY1\n') michael@0: ]); michael@0: var mapStep1 = node.toStringWithSourceMap({ michael@0: file: 'fileA' michael@0: }).map; michael@0: mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n'); michael@0: mapStep1 = mapStep1.toJSON(); michael@0: michael@0: node = new SourceNode(null, null, null, [ michael@0: 'gen1\n', michael@0: new SourceNode(1, 0, 'fileA', 'lineA1\n'), michael@0: new SourceNode(2, 0, 'fileA', 'lineA2\n'), michael@0: new SourceNode(3, 0, 'fileA', 'lineA3\n'), michael@0: new SourceNode(4, 0, 'fileA', 'lineA4\n'), michael@0: new SourceNode(1, 0, 'fileB', 'lineB1\n'), michael@0: new SourceNode(2, 0, 'fileB', 'lineB2\n'), michael@0: 'gen2\n' michael@0: ]); michael@0: var mapStep2 = node.toStringWithSourceMap({ michael@0: file: 'fileGen' michael@0: }).map; michael@0: mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n'); michael@0: mapStep2 = mapStep2.toJSON(); michael@0: michael@0: node = new SourceNode(null, null, null, [ michael@0: 'gen1\n', michael@0: new SourceNode(2, 0, 'fileX', 'lineA1\n'), michael@0: new SourceNode(2, 0, 'fileA', 'lineA2\n'), michael@0: new SourceNode(2, 0, 'fileY', 'lineA3\n'), michael@0: new SourceNode(4, 0, 'fileA', 'lineA4\n'), michael@0: new SourceNode(1, 0, 'fileB', 'lineB1\n'), michael@0: new SourceNode(2, 0, 'fileB', 'lineB2\n'), michael@0: 'gen2\n' michael@0: ]); michael@0: var expectedMap = node.toStringWithSourceMap({ michael@0: file: 'fileGen' michael@0: }).map; michael@0: expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n'); michael@0: expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n'); michael@0: expectedMap = expectedMap.toJSON(); michael@0: michael@0: // apply source map "mapStep1" to "mapStep2" michael@0: var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2)); michael@0: generator.applySourceMap(new SourceMapConsumer(mapStep1)); michael@0: var actualMap = generator.toJSON(); michael@0: michael@0: util.assertEqualMaps(assert, actualMap, expectedMap); michael@0: }; michael@0: michael@0: exports['test sorting with duplicate generated mappings'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'test.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 3, column: 0 }, michael@0: original: { line: 2, column: 0 }, michael@0: source: 'a.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 2, column: 0 } michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 2, column: 0 } michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 0 }, michael@0: original: { line: 1, column: 0 }, michael@0: source: 'a.js' michael@0: }); michael@0: michael@0: util.assertEqualMaps(assert, map.toJSON(), { michael@0: version: 3, michael@0: file: 'test.js', michael@0: sources: ['a.js'], michael@0: names: [], michael@0: mappings: 'AAAA;A;AACA' michael@0: }); michael@0: }; michael@0: michael@0: exports['test ignore duplicate mappings.'] = function (assert, util) { michael@0: var init = { file: 'min.js', sourceRoot: '/the/root' }; michael@0: var map1, map2; michael@0: michael@0: // null original source location michael@0: var nullMapping1 = { michael@0: generated: { line: 1, column: 0 } michael@0: }; michael@0: var nullMapping2 = { michael@0: generated: { line: 2, column: 2 } michael@0: }; michael@0: michael@0: map1 = new SourceMapGenerator(init); michael@0: map2 = new SourceMapGenerator(init); michael@0: michael@0: map1.addMapping(nullMapping1); michael@0: map1.addMapping(nullMapping1); michael@0: michael@0: map2.addMapping(nullMapping1); michael@0: michael@0: util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); michael@0: michael@0: map1.addMapping(nullMapping2); michael@0: map1.addMapping(nullMapping1); michael@0: michael@0: map2.addMapping(nullMapping2); michael@0: michael@0: util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); michael@0: michael@0: // original source location michael@0: var srcMapping1 = { michael@0: generated: { line: 1, column: 0 }, michael@0: original: { line: 11, column: 0 }, michael@0: source: 'srcMapping1.js' michael@0: }; michael@0: var srcMapping2 = { michael@0: generated: { line: 2, column: 2 }, michael@0: original: { line: 11, column: 0 }, michael@0: source: 'srcMapping2.js' michael@0: }; michael@0: michael@0: map1 = new SourceMapGenerator(init); michael@0: map2 = new SourceMapGenerator(init); michael@0: michael@0: map1.addMapping(srcMapping1); michael@0: map1.addMapping(srcMapping1); michael@0: michael@0: map2.addMapping(srcMapping1); michael@0: michael@0: util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); michael@0: michael@0: map1.addMapping(srcMapping2); michael@0: map1.addMapping(srcMapping1); michael@0: michael@0: map2.addMapping(srcMapping2); michael@0: michael@0: util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); michael@0: michael@0: // full original source and name information michael@0: var fullMapping1 = { michael@0: generated: { line: 1, column: 0 }, michael@0: original: { line: 11, column: 0 }, michael@0: source: 'fullMapping1.js', michael@0: name: 'fullMapping1' michael@0: }; michael@0: var fullMapping2 = { michael@0: generated: { line: 2, column: 2 }, michael@0: original: { line: 11, column: 0 }, michael@0: source: 'fullMapping2.js', michael@0: name: 'fullMapping2' michael@0: }; michael@0: michael@0: map1 = new SourceMapGenerator(init); michael@0: map2 = new SourceMapGenerator(init); michael@0: michael@0: map1.addMapping(fullMapping1); michael@0: map1.addMapping(fullMapping1); michael@0: michael@0: map2.addMapping(fullMapping1); michael@0: michael@0: util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); michael@0: michael@0: map1.addMapping(fullMapping2); michael@0: map1.addMapping(fullMapping1); michael@0: michael@0: map2.addMapping(fullMapping2); michael@0: michael@0: util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); michael@0: }; michael@0: michael@0: exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) { michael@0: var map = new SourceMapGenerator({ michael@0: file: 'test.js' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 1, column: 1 }, michael@0: original: { line: 2, column: 2 }, michael@0: source: 'a.js', michael@0: name: 'foo' michael@0: }); michael@0: map.addMapping({ michael@0: generated: { line: 3, column: 3 }, michael@0: original: { line: 4, column: 4 }, michael@0: source: 'a.js', michael@0: name: 'foo' michael@0: }); michael@0: util.assertEqualMaps(assert, map.toJSON(), { michael@0: version: 3, michael@0: file: 'test.js', michael@0: sources: ['a.js'], michael@0: names: ['foo'], michael@0: mappings: 'CACEA;;GAEEA' michael@0: }); michael@0: }; michael@0: michael@0: }); michael@0: function run_test() { michael@0: runSourceMapTests('test/source-map/test-source-map-generator', do_throw); michael@0: }