1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/sourcemap/tests/unit/test_source_map_consumer.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,459 @@ 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-consumer", ["require", "exports", "module"], function (require, exports, module) { 1.19 + 1.20 + var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer; 1.21 + var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator; 1.22 + 1.23 + exports['test that we can instantiate with a string or an objects'] = function (assert, util) { 1.24 + assert.doesNotThrow(function () { 1.25 + var map = new SourceMapConsumer(util.testMap); 1.26 + }); 1.27 + assert.doesNotThrow(function () { 1.28 + var map = new SourceMapConsumer(JSON.stringify(util.testMap)); 1.29 + }); 1.30 + }; 1.31 + 1.32 + exports['test that the `sources` field has the original sources'] = function (assert, util) { 1.33 + var map = new SourceMapConsumer(util.testMap); 1.34 + var sources = map.sources; 1.35 + 1.36 + assert.equal(sources[0], '/the/root/one.js'); 1.37 + assert.equal(sources[1], '/the/root/two.js'); 1.38 + assert.equal(sources.length, 2); 1.39 + }; 1.40 + 1.41 + exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) { 1.42 + var map = new SourceMapConsumer(util.testMap); 1.43 + var mapping; 1.44 + 1.45 + mapping = map.originalPositionFor({ 1.46 + line: 2, 1.47 + column: 1 1.48 + }); 1.49 + assert.equal(mapping.source, '/the/root/two.js'); 1.50 + 1.51 + mapping = map.originalPositionFor({ 1.52 + line: 1, 1.53 + column: 1 1.54 + }); 1.55 + assert.equal(mapping.source, '/the/root/one.js'); 1.56 + }; 1.57 + 1.58 + exports['test mapping tokens back exactly'] = function (assert, util) { 1.59 + var map = new SourceMapConsumer(util.testMap); 1.60 + 1.61 + util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert); 1.62 + util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert); 1.63 + util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert); 1.64 + util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert); 1.65 + util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert); 1.66 + util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert); 1.67 + util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert); 1.68 + 1.69 + util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert); 1.70 + util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert); 1.71 + util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert); 1.72 + util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert); 1.73 + util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert); 1.74 + util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert); 1.75 + }; 1.76 + 1.77 + exports['test mapping tokens fuzzy'] = function (assert, util) { 1.78 + var map = new SourceMapConsumer(util.testMap); 1.79 + 1.80 + // Finding original positions 1.81 + util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true); 1.82 + util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true); 1.83 + util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true); 1.84 + 1.85 + // Finding generated positions 1.86 + util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true); 1.87 + util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true); 1.88 + util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true); 1.89 + }; 1.90 + 1.91 + exports['test creating source map consumers with )]}\' prefix'] = function (assert, util) { 1.92 + assert.doesNotThrow(function () { 1.93 + var map = new SourceMapConsumer(")]}'" + JSON.stringify(util.testMap)); 1.94 + }); 1.95 + }; 1.96 + 1.97 + exports['test eachMapping'] = function (assert, util) { 1.98 + var map = new SourceMapConsumer(util.testMap); 1.99 + var previousLine = -Infinity; 1.100 + var previousColumn = -Infinity; 1.101 + map.eachMapping(function (mapping) { 1.102 + assert.ok(mapping.generatedLine >= previousLine); 1.103 + 1.104 + if (mapping.source) { 1.105 + assert.equal(mapping.source.indexOf(util.testMap.sourceRoot), 0); 1.106 + } 1.107 + 1.108 + if (mapping.generatedLine === previousLine) { 1.109 + assert.ok(mapping.generatedColumn >= previousColumn); 1.110 + previousColumn = mapping.generatedColumn; 1.111 + } 1.112 + else { 1.113 + previousLine = mapping.generatedLine; 1.114 + previousColumn = -Infinity; 1.115 + } 1.116 + }); 1.117 + }; 1.118 + 1.119 + exports['test iterating over mappings in a different order'] = function (assert, util) { 1.120 + var map = new SourceMapConsumer(util.testMap); 1.121 + var previousLine = -Infinity; 1.122 + var previousColumn = -Infinity; 1.123 + var previousSource = ""; 1.124 + map.eachMapping(function (mapping) { 1.125 + assert.ok(mapping.source >= previousSource); 1.126 + 1.127 + if (mapping.source === previousSource) { 1.128 + assert.ok(mapping.originalLine >= previousLine); 1.129 + 1.130 + if (mapping.originalLine === previousLine) { 1.131 + assert.ok(mapping.originalColumn >= previousColumn); 1.132 + previousColumn = mapping.originalColumn; 1.133 + } 1.134 + else { 1.135 + previousLine = mapping.originalLine; 1.136 + previousColumn = -Infinity; 1.137 + } 1.138 + } 1.139 + else { 1.140 + previousSource = mapping.source; 1.141 + previousLine = -Infinity; 1.142 + previousColumn = -Infinity; 1.143 + } 1.144 + }, null, SourceMapConsumer.ORIGINAL_ORDER); 1.145 + }; 1.146 + 1.147 + exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) { 1.148 + var map = new SourceMapConsumer(util.testMap); 1.149 + var context = {}; 1.150 + map.eachMapping(function () { 1.151 + assert.equal(this, context); 1.152 + }, context); 1.153 + }; 1.154 + 1.155 + exports['test that the `sourcesContent` field has the original sources'] = function (assert, util) { 1.156 + var map = new SourceMapConsumer(util.testMapWithSourcesContent); 1.157 + var sourcesContent = map.sourcesContent; 1.158 + 1.159 + assert.equal(sourcesContent[0], ' ONE.foo = function (bar) {\n return baz(bar);\n };'); 1.160 + assert.equal(sourcesContent[1], ' TWO.inc = function (n) {\n return n + 1;\n };'); 1.161 + assert.equal(sourcesContent.length, 2); 1.162 + }; 1.163 + 1.164 + exports['test that we can get the original sources for the sources'] = function (assert, util) { 1.165 + var map = new SourceMapConsumer(util.testMapWithSourcesContent); 1.166 + var sources = map.sources; 1.167 + 1.168 + assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };'); 1.169 + assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };'); 1.170 + assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };'); 1.171 + assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };'); 1.172 + assert.throws(function () { 1.173 + map.sourceContentFor(""); 1.174 + }, Error); 1.175 + assert.throws(function () { 1.176 + map.sourceContentFor("/the/root/three.js"); 1.177 + }, Error); 1.178 + assert.throws(function () { 1.179 + map.sourceContentFor("three.js"); 1.180 + }, Error); 1.181 + }; 1.182 + 1.183 + exports['test sourceRoot + generatedPositionFor'] = function (assert, util) { 1.184 + var map = new SourceMapGenerator({ 1.185 + sourceRoot: 'foo/bar', 1.186 + file: 'baz.js' 1.187 + }); 1.188 + map.addMapping({ 1.189 + original: { line: 1, column: 1 }, 1.190 + generated: { line: 2, column: 2 }, 1.191 + source: 'bang.coffee' 1.192 + }); 1.193 + map.addMapping({ 1.194 + original: { line: 5, column: 5 }, 1.195 + generated: { line: 6, column: 6 }, 1.196 + source: 'bang.coffee' 1.197 + }); 1.198 + map = new SourceMapConsumer(map.toString()); 1.199 + 1.200 + // Should handle without sourceRoot. 1.201 + var pos = map.generatedPositionFor({ 1.202 + line: 1, 1.203 + column: 1, 1.204 + source: 'bang.coffee' 1.205 + }); 1.206 + 1.207 + assert.equal(pos.line, 2); 1.208 + assert.equal(pos.column, 2); 1.209 + 1.210 + // Should handle with sourceRoot. 1.211 + var pos = map.generatedPositionFor({ 1.212 + line: 1, 1.213 + column: 1, 1.214 + source: 'foo/bar/bang.coffee' 1.215 + }); 1.216 + 1.217 + assert.equal(pos.line, 2); 1.218 + assert.equal(pos.column, 2); 1.219 + }; 1.220 + 1.221 + exports['test sourceRoot + originalPositionFor'] = function (assert, util) { 1.222 + var map = new SourceMapGenerator({ 1.223 + sourceRoot: 'foo/bar', 1.224 + file: 'baz.js' 1.225 + }); 1.226 + map.addMapping({ 1.227 + original: { line: 1, column: 1 }, 1.228 + generated: { line: 2, column: 2 }, 1.229 + source: 'bang.coffee' 1.230 + }); 1.231 + map = new SourceMapConsumer(map.toString()); 1.232 + 1.233 + var pos = map.originalPositionFor({ 1.234 + line: 2, 1.235 + column: 2, 1.236 + }); 1.237 + 1.238 + // Should always have the prepended source root 1.239 + assert.equal(pos.source, 'foo/bar/bang.coffee'); 1.240 + assert.equal(pos.line, 1); 1.241 + assert.equal(pos.column, 1); 1.242 + }; 1.243 + 1.244 + exports['test github issue #56'] = function (assert, util) { 1.245 + var map = new SourceMapGenerator({ 1.246 + sourceRoot: 'http://', 1.247 + file: 'www.example.com/foo.js' 1.248 + }); 1.249 + map.addMapping({ 1.250 + original: { line: 1, column: 1 }, 1.251 + generated: { line: 2, column: 2 }, 1.252 + source: 'www.example.com/original.js' 1.253 + }); 1.254 + map = new SourceMapConsumer(map.toString()); 1.255 + 1.256 + var sources = map.sources; 1.257 + assert.equal(sources.length, 1); 1.258 + assert.equal(sources[0], 'http://www.example.com/original.js'); 1.259 + }; 1.260 + 1.261 + exports['test github issue #43'] = function (assert, util) { 1.262 + var map = new SourceMapGenerator({ 1.263 + sourceRoot: 'http://example.com', 1.264 + file: 'foo.js' 1.265 + }); 1.266 + map.addMapping({ 1.267 + original: { line: 1, column: 1 }, 1.268 + generated: { line: 2, column: 2 }, 1.269 + source: 'http://cdn.example.com/original.js' 1.270 + }); 1.271 + map = new SourceMapConsumer(map.toString()); 1.272 + 1.273 + var sources = map.sources; 1.274 + assert.equal(sources.length, 1, 1.275 + 'Should only be one source.'); 1.276 + assert.equal(sources[0], 'http://cdn.example.com/original.js', 1.277 + 'Should not be joined with the sourceRoot.'); 1.278 + }; 1.279 + 1.280 + exports['test absolute path, but same host sources'] = function (assert, util) { 1.281 + var map = new SourceMapGenerator({ 1.282 + sourceRoot: 'http://example.com/foo/bar', 1.283 + file: 'foo.js' 1.284 + }); 1.285 + map.addMapping({ 1.286 + original: { line: 1, column: 1 }, 1.287 + generated: { line: 2, column: 2 }, 1.288 + source: '/original.js' 1.289 + }); 1.290 + map = new SourceMapConsumer(map.toString()); 1.291 + 1.292 + var sources = map.sources; 1.293 + assert.equal(sources.length, 1, 1.294 + 'Should only be one source.'); 1.295 + assert.equal(sources[0], 'http://example.com/original.js', 1.296 + 'Source should be relative the host of the source root.'); 1.297 + }; 1.298 + 1.299 + exports['test github issue #64'] = function (assert, util) { 1.300 + var map = new SourceMapConsumer({ 1.301 + "version": 3, 1.302 + "file": "foo.js", 1.303 + "sourceRoot": "http://example.com/", 1.304 + "sources": ["/a"], 1.305 + "names": [], 1.306 + "mappings": "AACA", 1.307 + "sourcesContent": ["foo"] 1.308 + }); 1.309 + 1.310 + assert.equal(map.sourceContentFor("a"), "foo"); 1.311 + assert.equal(map.sourceContentFor("/a"), "foo"); 1.312 + }; 1.313 + 1.314 + exports['test bug 885597'] = function (assert, util) { 1.315 + var map = new SourceMapConsumer({ 1.316 + "version": 3, 1.317 + "file": "foo.js", 1.318 + "sourceRoot": "file:///Users/AlGore/Invented/The/Internet/", 1.319 + "sources": ["/a"], 1.320 + "names": [], 1.321 + "mappings": "AACA", 1.322 + "sourcesContent": ["foo"] 1.323 + }); 1.324 + 1.325 + var s = map.sources[0]; 1.326 + assert.equal(map.sourceContentFor(s), "foo"); 1.327 + }; 1.328 + 1.329 + exports['test github issue #72, duplicate sources'] = function (assert, util) { 1.330 + var map = new SourceMapConsumer({ 1.331 + "version": 3, 1.332 + "file": "foo.js", 1.333 + "sources": ["source1.js", "source1.js", "source3.js"], 1.334 + "names": [], 1.335 + "mappings": ";EAAC;;IAEE;;MEEE", 1.336 + "sourceRoot": "http://example.com" 1.337 + }); 1.338 + 1.339 + var pos = map.originalPositionFor({ 1.340 + line: 2, 1.341 + column: 2 1.342 + }); 1.343 + assert.equal(pos.source, 'http://example.com/source1.js'); 1.344 + assert.equal(pos.line, 1); 1.345 + assert.equal(pos.column, 1); 1.346 + 1.347 + var pos = map.originalPositionFor({ 1.348 + line: 4, 1.349 + column: 4 1.350 + }); 1.351 + assert.equal(pos.source, 'http://example.com/source1.js'); 1.352 + assert.equal(pos.line, 3); 1.353 + assert.equal(pos.column, 3); 1.354 + 1.355 + var pos = map.originalPositionFor({ 1.356 + line: 6, 1.357 + column: 6 1.358 + }); 1.359 + assert.equal(pos.source, 'http://example.com/source3.js'); 1.360 + assert.equal(pos.line, 5); 1.361 + assert.equal(pos.column, 5); 1.362 + }; 1.363 + 1.364 + exports['test github issue #72, duplicate names'] = function (assert, util) { 1.365 + var map = new SourceMapConsumer({ 1.366 + "version": 3, 1.367 + "file": "foo.js", 1.368 + "sources": ["source.js"], 1.369 + "names": ["name1", "name1", "name3"], 1.370 + "mappings": ";EAACA;;IAEEA;;MAEEE", 1.371 + "sourceRoot": "http://example.com" 1.372 + }); 1.373 + 1.374 + var pos = map.originalPositionFor({ 1.375 + line: 2, 1.376 + column: 2 1.377 + }); 1.378 + assert.equal(pos.name, 'name1'); 1.379 + assert.equal(pos.line, 1); 1.380 + assert.equal(pos.column, 1); 1.381 + 1.382 + var pos = map.originalPositionFor({ 1.383 + line: 4, 1.384 + column: 4 1.385 + }); 1.386 + assert.equal(pos.name, 'name1'); 1.387 + assert.equal(pos.line, 3); 1.388 + assert.equal(pos.column, 3); 1.389 + 1.390 + var pos = map.originalPositionFor({ 1.391 + line: 6, 1.392 + column: 6 1.393 + }); 1.394 + assert.equal(pos.name, 'name3'); 1.395 + assert.equal(pos.line, 5); 1.396 + assert.equal(pos.column, 5); 1.397 + }; 1.398 + 1.399 + exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) { 1.400 + var smg = new SourceMapGenerator({ 1.401 + sourceRoot: 'http://example.com/', 1.402 + file: 'foo.js' 1.403 + }); 1.404 + smg.addMapping({ 1.405 + original: { line: 1, column: 1 }, 1.406 + generated: { line: 2, column: 2 }, 1.407 + source: 'bar.js' 1.408 + }); 1.409 + smg.addMapping({ 1.410 + original: { line: 2, column: 2 }, 1.411 + generated: { line: 4, column: 4 }, 1.412 + source: 'baz.js', 1.413 + name: 'dirtMcGirt' 1.414 + }); 1.415 + smg.setSourceContent('baz.js', 'baz.js content'); 1.416 + 1.417 + var smc = SourceMapConsumer.fromSourceMap(smg); 1.418 + assert.equal(smc.file, 'foo.js'); 1.419 + assert.equal(smc.sourceRoot, 'http://example.com/'); 1.420 + assert.equal(smc.sources.length, 2); 1.421 + assert.equal(smc.sources[0], 'http://example.com/bar.js'); 1.422 + assert.equal(smc.sources[1], 'http://example.com/baz.js'); 1.423 + assert.equal(smc.sourceContentFor('baz.js'), 'baz.js content'); 1.424 + 1.425 + var pos = smc.originalPositionFor({ 1.426 + line: 2, 1.427 + column: 2 1.428 + }); 1.429 + assert.equal(pos.line, 1); 1.430 + assert.equal(pos.column, 1); 1.431 + assert.equal(pos.source, 'http://example.com/bar.js'); 1.432 + assert.equal(pos.name, null); 1.433 + 1.434 + pos = smc.generatedPositionFor({ 1.435 + line: 1, 1.436 + column: 1, 1.437 + source: 'http://example.com/bar.js' 1.438 + }); 1.439 + assert.equal(pos.line, 2); 1.440 + assert.equal(pos.column, 2); 1.441 + 1.442 + pos = smc.originalPositionFor({ 1.443 + line: 4, 1.444 + column: 4 1.445 + }); 1.446 + assert.equal(pos.line, 2); 1.447 + assert.equal(pos.column, 2); 1.448 + assert.equal(pos.source, 'http://example.com/baz.js'); 1.449 + assert.equal(pos.name, 'dirtMcGirt'); 1.450 + 1.451 + pos = smc.generatedPositionFor({ 1.452 + line: 2, 1.453 + column: 2, 1.454 + source: 'http://example.com/baz.js' 1.455 + }); 1.456 + assert.equal(pos.line, 4); 1.457 + assert.equal(pos.column, 4); 1.458 + }; 1.459 +}); 1.460 +function run_test() { 1.461 + runSourceMapTests('test/source-map/test-source-map-consumer', do_throw); 1.462 +}