toolkit/devtools/sourcemap/tests/unit/test_source_map_consumer.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * WARNING!
michael@0 3 *
michael@0 4 * Do not edit this file directly, it is built from the sources at
michael@0 5 * https://github.com/mozilla/source-map/
michael@0 6 */
michael@0 7
michael@0 8 Components.utils.import('resource://test/Utils.jsm');
michael@0 9 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 10 /*
michael@0 11 * Copyright 2011 Mozilla Foundation and contributors
michael@0 12 * Licensed under the New BSD license. See LICENSE or:
michael@0 13 * http://opensource.org/licenses/BSD-3-Clause
michael@0 14 */
michael@0 15 define("test/source-map/test-source-map-consumer", ["require", "exports", "module"], function (require, exports, module) {
michael@0 16
michael@0 17 var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;
michael@0 18 var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;
michael@0 19
michael@0 20 exports['test that we can instantiate with a string or an objects'] = function (assert, util) {
michael@0 21 assert.doesNotThrow(function () {
michael@0 22 var map = new SourceMapConsumer(util.testMap);
michael@0 23 });
michael@0 24 assert.doesNotThrow(function () {
michael@0 25 var map = new SourceMapConsumer(JSON.stringify(util.testMap));
michael@0 26 });
michael@0 27 };
michael@0 28
michael@0 29 exports['test that the `sources` field has the original sources'] = function (assert, util) {
michael@0 30 var map = new SourceMapConsumer(util.testMap);
michael@0 31 var sources = map.sources;
michael@0 32
michael@0 33 assert.equal(sources[0], '/the/root/one.js');
michael@0 34 assert.equal(sources[1], '/the/root/two.js');
michael@0 35 assert.equal(sources.length, 2);
michael@0 36 };
michael@0 37
michael@0 38 exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) {
michael@0 39 var map = new SourceMapConsumer(util.testMap);
michael@0 40 var mapping;
michael@0 41
michael@0 42 mapping = map.originalPositionFor({
michael@0 43 line: 2,
michael@0 44 column: 1
michael@0 45 });
michael@0 46 assert.equal(mapping.source, '/the/root/two.js');
michael@0 47
michael@0 48 mapping = map.originalPositionFor({
michael@0 49 line: 1,
michael@0 50 column: 1
michael@0 51 });
michael@0 52 assert.equal(mapping.source, '/the/root/one.js');
michael@0 53 };
michael@0 54
michael@0 55 exports['test mapping tokens back exactly'] = function (assert, util) {
michael@0 56 var map = new SourceMapConsumer(util.testMap);
michael@0 57
michael@0 58 util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert);
michael@0 59 util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert);
michael@0 60 util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert);
michael@0 61 util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert);
michael@0 62 util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert);
michael@0 63 util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert);
michael@0 64 util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert);
michael@0 65
michael@0 66 util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert);
michael@0 67 util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert);
michael@0 68 util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert);
michael@0 69 util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert);
michael@0 70 util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert);
michael@0 71 util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert);
michael@0 72 };
michael@0 73
michael@0 74 exports['test mapping tokens fuzzy'] = function (assert, util) {
michael@0 75 var map = new SourceMapConsumer(util.testMap);
michael@0 76
michael@0 77 // Finding original positions
michael@0 78 util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true);
michael@0 79 util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true);
michael@0 80 util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true);
michael@0 81
michael@0 82 // Finding generated positions
michael@0 83 util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true);
michael@0 84 util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true);
michael@0 85 util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true);
michael@0 86 };
michael@0 87
michael@0 88 exports['test creating source map consumers with )]}\' prefix'] = function (assert, util) {
michael@0 89 assert.doesNotThrow(function () {
michael@0 90 var map = new SourceMapConsumer(")]}'" + JSON.stringify(util.testMap));
michael@0 91 });
michael@0 92 };
michael@0 93
michael@0 94 exports['test eachMapping'] = function (assert, util) {
michael@0 95 var map = new SourceMapConsumer(util.testMap);
michael@0 96 var previousLine = -Infinity;
michael@0 97 var previousColumn = -Infinity;
michael@0 98 map.eachMapping(function (mapping) {
michael@0 99 assert.ok(mapping.generatedLine >= previousLine);
michael@0 100
michael@0 101 if (mapping.source) {
michael@0 102 assert.equal(mapping.source.indexOf(util.testMap.sourceRoot), 0);
michael@0 103 }
michael@0 104
michael@0 105 if (mapping.generatedLine === previousLine) {
michael@0 106 assert.ok(mapping.generatedColumn >= previousColumn);
michael@0 107 previousColumn = mapping.generatedColumn;
michael@0 108 }
michael@0 109 else {
michael@0 110 previousLine = mapping.generatedLine;
michael@0 111 previousColumn = -Infinity;
michael@0 112 }
michael@0 113 });
michael@0 114 };
michael@0 115
michael@0 116 exports['test iterating over mappings in a different order'] = function (assert, util) {
michael@0 117 var map = new SourceMapConsumer(util.testMap);
michael@0 118 var previousLine = -Infinity;
michael@0 119 var previousColumn = -Infinity;
michael@0 120 var previousSource = "";
michael@0 121 map.eachMapping(function (mapping) {
michael@0 122 assert.ok(mapping.source >= previousSource);
michael@0 123
michael@0 124 if (mapping.source === previousSource) {
michael@0 125 assert.ok(mapping.originalLine >= previousLine);
michael@0 126
michael@0 127 if (mapping.originalLine === previousLine) {
michael@0 128 assert.ok(mapping.originalColumn >= previousColumn);
michael@0 129 previousColumn = mapping.originalColumn;
michael@0 130 }
michael@0 131 else {
michael@0 132 previousLine = mapping.originalLine;
michael@0 133 previousColumn = -Infinity;
michael@0 134 }
michael@0 135 }
michael@0 136 else {
michael@0 137 previousSource = mapping.source;
michael@0 138 previousLine = -Infinity;
michael@0 139 previousColumn = -Infinity;
michael@0 140 }
michael@0 141 }, null, SourceMapConsumer.ORIGINAL_ORDER);
michael@0 142 };
michael@0 143
michael@0 144 exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) {
michael@0 145 var map = new SourceMapConsumer(util.testMap);
michael@0 146 var context = {};
michael@0 147 map.eachMapping(function () {
michael@0 148 assert.equal(this, context);
michael@0 149 }, context);
michael@0 150 };
michael@0 151
michael@0 152 exports['test that the `sourcesContent` field has the original sources'] = function (assert, util) {
michael@0 153 var map = new SourceMapConsumer(util.testMapWithSourcesContent);
michael@0 154 var sourcesContent = map.sourcesContent;
michael@0 155
michael@0 156 assert.equal(sourcesContent[0], ' ONE.foo = function (bar) {\n return baz(bar);\n };');
michael@0 157 assert.equal(sourcesContent[1], ' TWO.inc = function (n) {\n return n + 1;\n };');
michael@0 158 assert.equal(sourcesContent.length, 2);
michael@0 159 };
michael@0 160
michael@0 161 exports['test that we can get the original sources for the sources'] = function (assert, util) {
michael@0 162 var map = new SourceMapConsumer(util.testMapWithSourcesContent);
michael@0 163 var sources = map.sources;
michael@0 164
michael@0 165 assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };');
michael@0 166 assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };');
michael@0 167 assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };');
michael@0 168 assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };');
michael@0 169 assert.throws(function () {
michael@0 170 map.sourceContentFor("");
michael@0 171 }, Error);
michael@0 172 assert.throws(function () {
michael@0 173 map.sourceContentFor("/the/root/three.js");
michael@0 174 }, Error);
michael@0 175 assert.throws(function () {
michael@0 176 map.sourceContentFor("three.js");
michael@0 177 }, Error);
michael@0 178 };
michael@0 179
michael@0 180 exports['test sourceRoot + generatedPositionFor'] = function (assert, util) {
michael@0 181 var map = new SourceMapGenerator({
michael@0 182 sourceRoot: 'foo/bar',
michael@0 183 file: 'baz.js'
michael@0 184 });
michael@0 185 map.addMapping({
michael@0 186 original: { line: 1, column: 1 },
michael@0 187 generated: { line: 2, column: 2 },
michael@0 188 source: 'bang.coffee'
michael@0 189 });
michael@0 190 map.addMapping({
michael@0 191 original: { line: 5, column: 5 },
michael@0 192 generated: { line: 6, column: 6 },
michael@0 193 source: 'bang.coffee'
michael@0 194 });
michael@0 195 map = new SourceMapConsumer(map.toString());
michael@0 196
michael@0 197 // Should handle without sourceRoot.
michael@0 198 var pos = map.generatedPositionFor({
michael@0 199 line: 1,
michael@0 200 column: 1,
michael@0 201 source: 'bang.coffee'
michael@0 202 });
michael@0 203
michael@0 204 assert.equal(pos.line, 2);
michael@0 205 assert.equal(pos.column, 2);
michael@0 206
michael@0 207 // Should handle with sourceRoot.
michael@0 208 var pos = map.generatedPositionFor({
michael@0 209 line: 1,
michael@0 210 column: 1,
michael@0 211 source: 'foo/bar/bang.coffee'
michael@0 212 });
michael@0 213
michael@0 214 assert.equal(pos.line, 2);
michael@0 215 assert.equal(pos.column, 2);
michael@0 216 };
michael@0 217
michael@0 218 exports['test sourceRoot + originalPositionFor'] = function (assert, util) {
michael@0 219 var map = new SourceMapGenerator({
michael@0 220 sourceRoot: 'foo/bar',
michael@0 221 file: 'baz.js'
michael@0 222 });
michael@0 223 map.addMapping({
michael@0 224 original: { line: 1, column: 1 },
michael@0 225 generated: { line: 2, column: 2 },
michael@0 226 source: 'bang.coffee'
michael@0 227 });
michael@0 228 map = new SourceMapConsumer(map.toString());
michael@0 229
michael@0 230 var pos = map.originalPositionFor({
michael@0 231 line: 2,
michael@0 232 column: 2,
michael@0 233 });
michael@0 234
michael@0 235 // Should always have the prepended source root
michael@0 236 assert.equal(pos.source, 'foo/bar/bang.coffee');
michael@0 237 assert.equal(pos.line, 1);
michael@0 238 assert.equal(pos.column, 1);
michael@0 239 };
michael@0 240
michael@0 241 exports['test github issue #56'] = function (assert, util) {
michael@0 242 var map = new SourceMapGenerator({
michael@0 243 sourceRoot: 'http://',
michael@0 244 file: 'www.example.com/foo.js'
michael@0 245 });
michael@0 246 map.addMapping({
michael@0 247 original: { line: 1, column: 1 },
michael@0 248 generated: { line: 2, column: 2 },
michael@0 249 source: 'www.example.com/original.js'
michael@0 250 });
michael@0 251 map = new SourceMapConsumer(map.toString());
michael@0 252
michael@0 253 var sources = map.sources;
michael@0 254 assert.equal(sources.length, 1);
michael@0 255 assert.equal(sources[0], 'http://www.example.com/original.js');
michael@0 256 };
michael@0 257
michael@0 258 exports['test github issue #43'] = function (assert, util) {
michael@0 259 var map = new SourceMapGenerator({
michael@0 260 sourceRoot: 'http://example.com',
michael@0 261 file: 'foo.js'
michael@0 262 });
michael@0 263 map.addMapping({
michael@0 264 original: { line: 1, column: 1 },
michael@0 265 generated: { line: 2, column: 2 },
michael@0 266 source: 'http://cdn.example.com/original.js'
michael@0 267 });
michael@0 268 map = new SourceMapConsumer(map.toString());
michael@0 269
michael@0 270 var sources = map.sources;
michael@0 271 assert.equal(sources.length, 1,
michael@0 272 'Should only be one source.');
michael@0 273 assert.equal(sources[0], 'http://cdn.example.com/original.js',
michael@0 274 'Should not be joined with the sourceRoot.');
michael@0 275 };
michael@0 276
michael@0 277 exports['test absolute path, but same host sources'] = function (assert, util) {
michael@0 278 var map = new SourceMapGenerator({
michael@0 279 sourceRoot: 'http://example.com/foo/bar',
michael@0 280 file: 'foo.js'
michael@0 281 });
michael@0 282 map.addMapping({
michael@0 283 original: { line: 1, column: 1 },
michael@0 284 generated: { line: 2, column: 2 },
michael@0 285 source: '/original.js'
michael@0 286 });
michael@0 287 map = new SourceMapConsumer(map.toString());
michael@0 288
michael@0 289 var sources = map.sources;
michael@0 290 assert.equal(sources.length, 1,
michael@0 291 'Should only be one source.');
michael@0 292 assert.equal(sources[0], 'http://example.com/original.js',
michael@0 293 'Source should be relative the host of the source root.');
michael@0 294 };
michael@0 295
michael@0 296 exports['test github issue #64'] = function (assert, util) {
michael@0 297 var map = new SourceMapConsumer({
michael@0 298 "version": 3,
michael@0 299 "file": "foo.js",
michael@0 300 "sourceRoot": "http://example.com/",
michael@0 301 "sources": ["/a"],
michael@0 302 "names": [],
michael@0 303 "mappings": "AACA",
michael@0 304 "sourcesContent": ["foo"]
michael@0 305 });
michael@0 306
michael@0 307 assert.equal(map.sourceContentFor("a"), "foo");
michael@0 308 assert.equal(map.sourceContentFor("/a"), "foo");
michael@0 309 };
michael@0 310
michael@0 311 exports['test bug 885597'] = function (assert, util) {
michael@0 312 var map = new SourceMapConsumer({
michael@0 313 "version": 3,
michael@0 314 "file": "foo.js",
michael@0 315 "sourceRoot": "file:///Users/AlGore/Invented/The/Internet/",
michael@0 316 "sources": ["/a"],
michael@0 317 "names": [],
michael@0 318 "mappings": "AACA",
michael@0 319 "sourcesContent": ["foo"]
michael@0 320 });
michael@0 321
michael@0 322 var s = map.sources[0];
michael@0 323 assert.equal(map.sourceContentFor(s), "foo");
michael@0 324 };
michael@0 325
michael@0 326 exports['test github issue #72, duplicate sources'] = function (assert, util) {
michael@0 327 var map = new SourceMapConsumer({
michael@0 328 "version": 3,
michael@0 329 "file": "foo.js",
michael@0 330 "sources": ["source1.js", "source1.js", "source3.js"],
michael@0 331 "names": [],
michael@0 332 "mappings": ";EAAC;;IAEE;;MEEE",
michael@0 333 "sourceRoot": "http://example.com"
michael@0 334 });
michael@0 335
michael@0 336 var pos = map.originalPositionFor({
michael@0 337 line: 2,
michael@0 338 column: 2
michael@0 339 });
michael@0 340 assert.equal(pos.source, 'http://example.com/source1.js');
michael@0 341 assert.equal(pos.line, 1);
michael@0 342 assert.equal(pos.column, 1);
michael@0 343
michael@0 344 var pos = map.originalPositionFor({
michael@0 345 line: 4,
michael@0 346 column: 4
michael@0 347 });
michael@0 348 assert.equal(pos.source, 'http://example.com/source1.js');
michael@0 349 assert.equal(pos.line, 3);
michael@0 350 assert.equal(pos.column, 3);
michael@0 351
michael@0 352 var pos = map.originalPositionFor({
michael@0 353 line: 6,
michael@0 354 column: 6
michael@0 355 });
michael@0 356 assert.equal(pos.source, 'http://example.com/source3.js');
michael@0 357 assert.equal(pos.line, 5);
michael@0 358 assert.equal(pos.column, 5);
michael@0 359 };
michael@0 360
michael@0 361 exports['test github issue #72, duplicate names'] = function (assert, util) {
michael@0 362 var map = new SourceMapConsumer({
michael@0 363 "version": 3,
michael@0 364 "file": "foo.js",
michael@0 365 "sources": ["source.js"],
michael@0 366 "names": ["name1", "name1", "name3"],
michael@0 367 "mappings": ";EAACA;;IAEEA;;MAEEE",
michael@0 368 "sourceRoot": "http://example.com"
michael@0 369 });
michael@0 370
michael@0 371 var pos = map.originalPositionFor({
michael@0 372 line: 2,
michael@0 373 column: 2
michael@0 374 });
michael@0 375 assert.equal(pos.name, 'name1');
michael@0 376 assert.equal(pos.line, 1);
michael@0 377 assert.equal(pos.column, 1);
michael@0 378
michael@0 379 var pos = map.originalPositionFor({
michael@0 380 line: 4,
michael@0 381 column: 4
michael@0 382 });
michael@0 383 assert.equal(pos.name, 'name1');
michael@0 384 assert.equal(pos.line, 3);
michael@0 385 assert.equal(pos.column, 3);
michael@0 386
michael@0 387 var pos = map.originalPositionFor({
michael@0 388 line: 6,
michael@0 389 column: 6
michael@0 390 });
michael@0 391 assert.equal(pos.name, 'name3');
michael@0 392 assert.equal(pos.line, 5);
michael@0 393 assert.equal(pos.column, 5);
michael@0 394 };
michael@0 395
michael@0 396 exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) {
michael@0 397 var smg = new SourceMapGenerator({
michael@0 398 sourceRoot: 'http://example.com/',
michael@0 399 file: 'foo.js'
michael@0 400 });
michael@0 401 smg.addMapping({
michael@0 402 original: { line: 1, column: 1 },
michael@0 403 generated: { line: 2, column: 2 },
michael@0 404 source: 'bar.js'
michael@0 405 });
michael@0 406 smg.addMapping({
michael@0 407 original: { line: 2, column: 2 },
michael@0 408 generated: { line: 4, column: 4 },
michael@0 409 source: 'baz.js',
michael@0 410 name: 'dirtMcGirt'
michael@0 411 });
michael@0 412 smg.setSourceContent('baz.js', 'baz.js content');
michael@0 413
michael@0 414 var smc = SourceMapConsumer.fromSourceMap(smg);
michael@0 415 assert.equal(smc.file, 'foo.js');
michael@0 416 assert.equal(smc.sourceRoot, 'http://example.com/');
michael@0 417 assert.equal(smc.sources.length, 2);
michael@0 418 assert.equal(smc.sources[0], 'http://example.com/bar.js');
michael@0 419 assert.equal(smc.sources[1], 'http://example.com/baz.js');
michael@0 420 assert.equal(smc.sourceContentFor('baz.js'), 'baz.js content');
michael@0 421
michael@0 422 var pos = smc.originalPositionFor({
michael@0 423 line: 2,
michael@0 424 column: 2
michael@0 425 });
michael@0 426 assert.equal(pos.line, 1);
michael@0 427 assert.equal(pos.column, 1);
michael@0 428 assert.equal(pos.source, 'http://example.com/bar.js');
michael@0 429 assert.equal(pos.name, null);
michael@0 430
michael@0 431 pos = smc.generatedPositionFor({
michael@0 432 line: 1,
michael@0 433 column: 1,
michael@0 434 source: 'http://example.com/bar.js'
michael@0 435 });
michael@0 436 assert.equal(pos.line, 2);
michael@0 437 assert.equal(pos.column, 2);
michael@0 438
michael@0 439 pos = smc.originalPositionFor({
michael@0 440 line: 4,
michael@0 441 column: 4
michael@0 442 });
michael@0 443 assert.equal(pos.line, 2);
michael@0 444 assert.equal(pos.column, 2);
michael@0 445 assert.equal(pos.source, 'http://example.com/baz.js');
michael@0 446 assert.equal(pos.name, 'dirtMcGirt');
michael@0 447
michael@0 448 pos = smc.generatedPositionFor({
michael@0 449 line: 2,
michael@0 450 column: 2,
michael@0 451 source: 'http://example.com/baz.js'
michael@0 452 });
michael@0 453 assert.equal(pos.line, 4);
michael@0 454 assert.equal(pos.column, 4);
michael@0 455 };
michael@0 456 });
michael@0 457 function run_test() {
michael@0 458 runSourceMapTests('test/source-map/test-source-map-consumer', do_throw);
michael@0 459 }

mercurial