1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/sourcemap/tests/unit/test_source_map_generator.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,425 @@ 1.4 +/* 1.5 + * WARNING! 1.6 + * 1.7 + * Do not edit this file directly, it is built from the sources at 1.8 + * https://github.com/mozilla/source-map/ 1.9 + */ 1.10 + 1.11 +Components.utils.import('resource://test/Utils.jsm'); 1.12 +/* -*- Mode: js; js-indent-level: 2; -*- */ 1.13 +/* 1.14 + * Copyright 2011 Mozilla Foundation and contributors 1.15 + * Licensed under the New BSD license. See LICENSE or: 1.16 + * http://opensource.org/licenses/BSD-3-Clause 1.17 + */ 1.18 +define("test/source-map/test-source-map-generator", ["require", "exports", "module"], function (require, exports, module) { 1.19 + 1.20 + var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator; 1.21 + var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer; 1.22 + var SourceNode = require('source-map/source-node').SourceNode; 1.23 + var util = require('source-map/util'); 1.24 + 1.25 + exports['test some simple stuff'] = function (assert, util) { 1.26 + var map = new SourceMapGenerator({ 1.27 + file: 'foo.js', 1.28 + sourceRoot: '.' 1.29 + }); 1.30 + assert.ok(true); 1.31 + }; 1.32 + 1.33 + exports['test JSON serialization'] = function (assert, util) { 1.34 + var map = new SourceMapGenerator({ 1.35 + file: 'foo.js', 1.36 + sourceRoot: '.' 1.37 + }); 1.38 + assert.equal(map.toString(), JSON.stringify(map)); 1.39 + }; 1.40 + 1.41 + exports['test adding mappings (case 1)'] = function (assert, util) { 1.42 + var map = new SourceMapGenerator({ 1.43 + file: 'generated-foo.js', 1.44 + sourceRoot: '.' 1.45 + }); 1.46 + 1.47 + assert.doesNotThrow(function () { 1.48 + map.addMapping({ 1.49 + generated: { line: 1, column: 1 } 1.50 + }); 1.51 + }); 1.52 + }; 1.53 + 1.54 + exports['test adding mappings (case 2)'] = function (assert, util) { 1.55 + var map = new SourceMapGenerator({ 1.56 + file: 'generated-foo.js', 1.57 + sourceRoot: '.' 1.58 + }); 1.59 + 1.60 + assert.doesNotThrow(function () { 1.61 + map.addMapping({ 1.62 + generated: { line: 1, column: 1 }, 1.63 + source: 'bar.js', 1.64 + original: { line: 1, column: 1 } 1.65 + }); 1.66 + }); 1.67 + }; 1.68 + 1.69 + exports['test adding mappings (case 3)'] = function (assert, util) { 1.70 + var map = new SourceMapGenerator({ 1.71 + file: 'generated-foo.js', 1.72 + sourceRoot: '.' 1.73 + }); 1.74 + 1.75 + assert.doesNotThrow(function () { 1.76 + map.addMapping({ 1.77 + generated: { line: 1, column: 1 }, 1.78 + source: 'bar.js', 1.79 + original: { line: 1, column: 1 }, 1.80 + name: 'someToken' 1.81 + }); 1.82 + }); 1.83 + }; 1.84 + 1.85 + exports['test adding mappings (invalid)'] = function (assert, util) { 1.86 + var map = new SourceMapGenerator({ 1.87 + file: 'generated-foo.js', 1.88 + sourceRoot: '.' 1.89 + }); 1.90 + 1.91 + // Not enough info. 1.92 + assert.throws(function () { 1.93 + map.addMapping({}); 1.94 + }); 1.95 + 1.96 + // Original file position, but no source. 1.97 + assert.throws(function () { 1.98 + map.addMapping({ 1.99 + generated: { line: 1, column: 1 }, 1.100 + original: { line: 1, column: 1 } 1.101 + }); 1.102 + }); 1.103 + }; 1.104 + 1.105 + exports['test that the correct mappings are being generated'] = function (assert, util) { 1.106 + var map = new SourceMapGenerator({ 1.107 + file: 'min.js', 1.108 + sourceRoot: '/the/root' 1.109 + }); 1.110 + 1.111 + map.addMapping({ 1.112 + generated: { line: 1, column: 1 }, 1.113 + original: { line: 1, column: 1 }, 1.114 + source: 'one.js' 1.115 + }); 1.116 + map.addMapping({ 1.117 + generated: { line: 1, column: 5 }, 1.118 + original: { line: 1, column: 5 }, 1.119 + source: 'one.js' 1.120 + }); 1.121 + map.addMapping({ 1.122 + generated: { line: 1, column: 9 }, 1.123 + original: { line: 1, column: 11 }, 1.124 + source: 'one.js' 1.125 + }); 1.126 + map.addMapping({ 1.127 + generated: { line: 1, column: 18 }, 1.128 + original: { line: 1, column: 21 }, 1.129 + source: 'one.js', 1.130 + name: 'bar' 1.131 + }); 1.132 + map.addMapping({ 1.133 + generated: { line: 1, column: 21 }, 1.134 + original: { line: 2, column: 3 }, 1.135 + source: 'one.js' 1.136 + }); 1.137 + map.addMapping({ 1.138 + generated: { line: 1, column: 28 }, 1.139 + original: { line: 2, column: 10 }, 1.140 + source: 'one.js', 1.141 + name: 'baz' 1.142 + }); 1.143 + map.addMapping({ 1.144 + generated: { line: 1, column: 32 }, 1.145 + original: { line: 2, column: 14 }, 1.146 + source: 'one.js', 1.147 + name: 'bar' 1.148 + }); 1.149 + 1.150 + map.addMapping({ 1.151 + generated: { line: 2, column: 1 }, 1.152 + original: { line: 1, column: 1 }, 1.153 + source: 'two.js' 1.154 + }); 1.155 + map.addMapping({ 1.156 + generated: { line: 2, column: 5 }, 1.157 + original: { line: 1, column: 5 }, 1.158 + source: 'two.js' 1.159 + }); 1.160 + map.addMapping({ 1.161 + generated: { line: 2, column: 9 }, 1.162 + original: { line: 1, column: 11 }, 1.163 + source: 'two.js' 1.164 + }); 1.165 + map.addMapping({ 1.166 + generated: { line: 2, column: 18 }, 1.167 + original: { line: 1, column: 21 }, 1.168 + source: 'two.js', 1.169 + name: 'n' 1.170 + }); 1.171 + map.addMapping({ 1.172 + generated: { line: 2, column: 21 }, 1.173 + original: { line: 2, column: 3 }, 1.174 + source: 'two.js' 1.175 + }); 1.176 + map.addMapping({ 1.177 + generated: { line: 2, column: 28 }, 1.178 + original: { line: 2, column: 10 }, 1.179 + source: 'two.js', 1.180 + name: 'n' 1.181 + }); 1.182 + 1.183 + map = JSON.parse(map.toString()); 1.184 + 1.185 + util.assertEqualMaps(assert, map, util.testMap); 1.186 + }; 1.187 + 1.188 + exports['test that source content can be set'] = function (assert, util) { 1.189 + var map = new SourceMapGenerator({ 1.190 + file: 'min.js', 1.191 + sourceRoot: '/the/root' 1.192 + }); 1.193 + map.addMapping({ 1.194 + generated: { line: 1, column: 1 }, 1.195 + original: { line: 1, column: 1 }, 1.196 + source: 'one.js' 1.197 + }); 1.198 + map.addMapping({ 1.199 + generated: { line: 2, column: 1 }, 1.200 + original: { line: 1, column: 1 }, 1.201 + source: 'two.js' 1.202 + }); 1.203 + map.setSourceContent('one.js', 'one file content'); 1.204 + 1.205 + map = JSON.parse(map.toString()); 1.206 + assert.equal(map.sources[0], 'one.js'); 1.207 + assert.equal(map.sources[1], 'two.js'); 1.208 + assert.equal(map.sourcesContent[0], 'one file content'); 1.209 + assert.equal(map.sourcesContent[1], null); 1.210 + }; 1.211 + 1.212 + exports['test .fromSourceMap'] = function (assert, util) { 1.213 + var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap)); 1.214 + util.assertEqualMaps(assert, map.toJSON(), util.testMap); 1.215 + }; 1.216 + 1.217 + exports['test .fromSourceMap with sourcesContent'] = function (assert, util) { 1.218 + var map = SourceMapGenerator.fromSourceMap( 1.219 + new SourceMapConsumer(util.testMapWithSourcesContent)); 1.220 + util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent); 1.221 + }; 1.222 + 1.223 + exports['test applySourceMap'] = function (assert, util) { 1.224 + var node = new SourceNode(null, null, null, [ 1.225 + new SourceNode(2, 0, 'fileX', 'lineX2\n'), 1.226 + 'genA1\n', 1.227 + new SourceNode(2, 0, 'fileY', 'lineY2\n'), 1.228 + 'genA2\n', 1.229 + new SourceNode(1, 0, 'fileX', 'lineX1\n'), 1.230 + 'genA3\n', 1.231 + new SourceNode(1, 0, 'fileY', 'lineY1\n') 1.232 + ]); 1.233 + var mapStep1 = node.toStringWithSourceMap({ 1.234 + file: 'fileA' 1.235 + }).map; 1.236 + mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n'); 1.237 + mapStep1 = mapStep1.toJSON(); 1.238 + 1.239 + node = new SourceNode(null, null, null, [ 1.240 + 'gen1\n', 1.241 + new SourceNode(1, 0, 'fileA', 'lineA1\n'), 1.242 + new SourceNode(2, 0, 'fileA', 'lineA2\n'), 1.243 + new SourceNode(3, 0, 'fileA', 'lineA3\n'), 1.244 + new SourceNode(4, 0, 'fileA', 'lineA4\n'), 1.245 + new SourceNode(1, 0, 'fileB', 'lineB1\n'), 1.246 + new SourceNode(2, 0, 'fileB', 'lineB2\n'), 1.247 + 'gen2\n' 1.248 + ]); 1.249 + var mapStep2 = node.toStringWithSourceMap({ 1.250 + file: 'fileGen' 1.251 + }).map; 1.252 + mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n'); 1.253 + mapStep2 = mapStep2.toJSON(); 1.254 + 1.255 + node = new SourceNode(null, null, null, [ 1.256 + 'gen1\n', 1.257 + new SourceNode(2, 0, 'fileX', 'lineA1\n'), 1.258 + new SourceNode(2, 0, 'fileA', 'lineA2\n'), 1.259 + new SourceNode(2, 0, 'fileY', 'lineA3\n'), 1.260 + new SourceNode(4, 0, 'fileA', 'lineA4\n'), 1.261 + new SourceNode(1, 0, 'fileB', 'lineB1\n'), 1.262 + new SourceNode(2, 0, 'fileB', 'lineB2\n'), 1.263 + 'gen2\n' 1.264 + ]); 1.265 + var expectedMap = node.toStringWithSourceMap({ 1.266 + file: 'fileGen' 1.267 + }).map; 1.268 + expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n'); 1.269 + expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n'); 1.270 + expectedMap = expectedMap.toJSON(); 1.271 + 1.272 + // apply source map "mapStep1" to "mapStep2" 1.273 + var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2)); 1.274 + generator.applySourceMap(new SourceMapConsumer(mapStep1)); 1.275 + var actualMap = generator.toJSON(); 1.276 + 1.277 + util.assertEqualMaps(assert, actualMap, expectedMap); 1.278 + }; 1.279 + 1.280 + exports['test sorting with duplicate generated mappings'] = function (assert, util) { 1.281 + var map = new SourceMapGenerator({ 1.282 + file: 'test.js' 1.283 + }); 1.284 + map.addMapping({ 1.285 + generated: { line: 3, column: 0 }, 1.286 + original: { line: 2, column: 0 }, 1.287 + source: 'a.js' 1.288 + }); 1.289 + map.addMapping({ 1.290 + generated: { line: 2, column: 0 } 1.291 + }); 1.292 + map.addMapping({ 1.293 + generated: { line: 2, column: 0 } 1.294 + }); 1.295 + map.addMapping({ 1.296 + generated: { line: 1, column: 0 }, 1.297 + original: { line: 1, column: 0 }, 1.298 + source: 'a.js' 1.299 + }); 1.300 + 1.301 + util.assertEqualMaps(assert, map.toJSON(), { 1.302 + version: 3, 1.303 + file: 'test.js', 1.304 + sources: ['a.js'], 1.305 + names: [], 1.306 + mappings: 'AAAA;A;AACA' 1.307 + }); 1.308 + }; 1.309 + 1.310 + exports['test ignore duplicate mappings.'] = function (assert, util) { 1.311 + var init = { file: 'min.js', sourceRoot: '/the/root' }; 1.312 + var map1, map2; 1.313 + 1.314 + // null original source location 1.315 + var nullMapping1 = { 1.316 + generated: { line: 1, column: 0 } 1.317 + }; 1.318 + var nullMapping2 = { 1.319 + generated: { line: 2, column: 2 } 1.320 + }; 1.321 + 1.322 + map1 = new SourceMapGenerator(init); 1.323 + map2 = new SourceMapGenerator(init); 1.324 + 1.325 + map1.addMapping(nullMapping1); 1.326 + map1.addMapping(nullMapping1); 1.327 + 1.328 + map2.addMapping(nullMapping1); 1.329 + 1.330 + util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); 1.331 + 1.332 + map1.addMapping(nullMapping2); 1.333 + map1.addMapping(nullMapping1); 1.334 + 1.335 + map2.addMapping(nullMapping2); 1.336 + 1.337 + util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); 1.338 + 1.339 + // original source location 1.340 + var srcMapping1 = { 1.341 + generated: { line: 1, column: 0 }, 1.342 + original: { line: 11, column: 0 }, 1.343 + source: 'srcMapping1.js' 1.344 + }; 1.345 + var srcMapping2 = { 1.346 + generated: { line: 2, column: 2 }, 1.347 + original: { line: 11, column: 0 }, 1.348 + source: 'srcMapping2.js' 1.349 + }; 1.350 + 1.351 + map1 = new SourceMapGenerator(init); 1.352 + map2 = new SourceMapGenerator(init); 1.353 + 1.354 + map1.addMapping(srcMapping1); 1.355 + map1.addMapping(srcMapping1); 1.356 + 1.357 + map2.addMapping(srcMapping1); 1.358 + 1.359 + util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); 1.360 + 1.361 + map1.addMapping(srcMapping2); 1.362 + map1.addMapping(srcMapping1); 1.363 + 1.364 + map2.addMapping(srcMapping2); 1.365 + 1.366 + util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); 1.367 + 1.368 + // full original source and name information 1.369 + var fullMapping1 = { 1.370 + generated: { line: 1, column: 0 }, 1.371 + original: { line: 11, column: 0 }, 1.372 + source: 'fullMapping1.js', 1.373 + name: 'fullMapping1' 1.374 + }; 1.375 + var fullMapping2 = { 1.376 + generated: { line: 2, column: 2 }, 1.377 + original: { line: 11, column: 0 }, 1.378 + source: 'fullMapping2.js', 1.379 + name: 'fullMapping2' 1.380 + }; 1.381 + 1.382 + map1 = new SourceMapGenerator(init); 1.383 + map2 = new SourceMapGenerator(init); 1.384 + 1.385 + map1.addMapping(fullMapping1); 1.386 + map1.addMapping(fullMapping1); 1.387 + 1.388 + map2.addMapping(fullMapping1); 1.389 + 1.390 + util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); 1.391 + 1.392 + map1.addMapping(fullMapping2); 1.393 + map1.addMapping(fullMapping1); 1.394 + 1.395 + map2.addMapping(fullMapping2); 1.396 + 1.397 + util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON()); 1.398 + }; 1.399 + 1.400 + exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) { 1.401 + var map = new SourceMapGenerator({ 1.402 + file: 'test.js' 1.403 + }); 1.404 + map.addMapping({ 1.405 + generated: { line: 1, column: 1 }, 1.406 + original: { line: 2, column: 2 }, 1.407 + source: 'a.js', 1.408 + name: 'foo' 1.409 + }); 1.410 + map.addMapping({ 1.411 + generated: { line: 3, column: 3 }, 1.412 + original: { line: 4, column: 4 }, 1.413 + source: 'a.js', 1.414 + name: 'foo' 1.415 + }); 1.416 + util.assertEqualMaps(assert, map.toJSON(), { 1.417 + version: 3, 1.418 + file: 'test.js', 1.419 + sources: ['a.js'], 1.420 + names: ['foo'], 1.421 + mappings: 'CACEA;;GAEEA' 1.422 + }); 1.423 + }; 1.424 + 1.425 +}); 1.426 +function run_test() { 1.427 + runSourceMapTests('test/source-map/test-source-map-generator', do_throw); 1.428 +}