toolkit/devtools/sourcemap/tests/unit/Utils.jsm

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 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 2 /*
michael@0 3 * Copyright 2011 Mozilla Foundation and contributors
michael@0 4 * Licensed under the New BSD license. See LICENSE or:
michael@0 5 * http://opensource.org/licenses/BSD-3-Clause
michael@0 6 */
michael@0 7
michael@0 8 /*
michael@0 9 * WARNING!
michael@0 10 *
michael@0 11 * Do not edit this file directly, it is built from the sources at
michael@0 12 * https://github.com/mozilla/source-map/
michael@0 13 */
michael@0 14
michael@0 15 Components.utils.import('resource://gre/modules/devtools/Require.jsm');
michael@0 16 Components.utils.import('resource://gre/modules/devtools/SourceMap.jsm');
michael@0 17
michael@0 18 this.EXPORTED_SYMBOLS = [ "define", "runSourceMapTests" ];
michael@0 19 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 20 /*
michael@0 21 * Copyright 2011 Mozilla Foundation and contributors
michael@0 22 * Licensed under the New BSD license. See LICENSE or:
michael@0 23 * http://opensource.org/licenses/BSD-3-Clause
michael@0 24 */
michael@0 25 define('test/source-map/assert', ['exports'], function (exports) {
michael@0 26
michael@0 27 let do_throw = function (msg) {
michael@0 28 throw new Error(msg);
michael@0 29 };
michael@0 30
michael@0 31 exports.init = function (throw_fn) {
michael@0 32 do_throw = throw_fn;
michael@0 33 };
michael@0 34
michael@0 35 exports.doesNotThrow = function (fn) {
michael@0 36 try {
michael@0 37 fn();
michael@0 38 }
michael@0 39 catch (e) {
michael@0 40 do_throw(e.message);
michael@0 41 }
michael@0 42 };
michael@0 43
michael@0 44 exports.equal = function (actual, expected, msg) {
michael@0 45 msg = msg || String(actual) + ' != ' + String(expected);
michael@0 46 if (actual != expected) {
michael@0 47 do_throw(msg);
michael@0 48 }
michael@0 49 };
michael@0 50
michael@0 51 exports.ok = function (val, msg) {
michael@0 52 msg = msg || String(val) + ' is falsey';
michael@0 53 if (!Boolean(val)) {
michael@0 54 do_throw(msg);
michael@0 55 }
michael@0 56 };
michael@0 57
michael@0 58 exports.strictEqual = function (actual, expected, msg) {
michael@0 59 msg = msg || String(actual) + ' !== ' + String(expected);
michael@0 60 if (actual !== expected) {
michael@0 61 do_throw(msg);
michael@0 62 }
michael@0 63 };
michael@0 64
michael@0 65 exports.throws = function (fn) {
michael@0 66 try {
michael@0 67 fn();
michael@0 68 do_throw('Expected an error to be thrown, but it wasn\'t.');
michael@0 69 }
michael@0 70 catch (e) {
michael@0 71 }
michael@0 72 };
michael@0 73
michael@0 74 });
michael@0 75 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 76 /*
michael@0 77 * Copyright 2011 Mozilla Foundation and contributors
michael@0 78 * Licensed under the New BSD license. See LICENSE or:
michael@0 79 * http://opensource.org/licenses/BSD-3-Clause
michael@0 80 */
michael@0 81 define('test/source-map/util', ['require', 'exports', 'module' , 'lib/source-map/util'], function(require, exports, module) {
michael@0 82
michael@0 83 var util = require('source-map/util');
michael@0 84
michael@0 85 // This is a test mapping which maps functions from two different files
michael@0 86 // (one.js and two.js) to a minified generated source.
michael@0 87 //
michael@0 88 // Here is one.js:
michael@0 89 //
michael@0 90 // ONE.foo = function (bar) {
michael@0 91 // return baz(bar);
michael@0 92 // };
michael@0 93 //
michael@0 94 // Here is two.js:
michael@0 95 //
michael@0 96 // TWO.inc = function (n) {
michael@0 97 // return n + 1;
michael@0 98 // };
michael@0 99 //
michael@0 100 // And here is the generated code (min.js):
michael@0 101 //
michael@0 102 // ONE.foo=function(a){return baz(a);};
michael@0 103 // TWO.inc=function(a){return a+1;};
michael@0 104 exports.testGeneratedCode = " ONE.foo=function(a){return baz(a);};\n"+
michael@0 105 " TWO.inc=function(a){return a+1;};";
michael@0 106 exports.testMap = {
michael@0 107 version: 3,
michael@0 108 file: 'min.js',
michael@0 109 names: ['bar', 'baz', 'n'],
michael@0 110 sources: ['one.js', 'two.js'],
michael@0 111 sourceRoot: '/the/root',
michael@0 112 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
michael@0 113 };
michael@0 114 exports.testMapWithSourcesContent = {
michael@0 115 version: 3,
michael@0 116 file: 'min.js',
michael@0 117 names: ['bar', 'baz', 'n'],
michael@0 118 sources: ['one.js', 'two.js'],
michael@0 119 sourcesContent: [
michael@0 120 ' ONE.foo = function (bar) {\n' +
michael@0 121 ' return baz(bar);\n' +
michael@0 122 ' };',
michael@0 123 ' TWO.inc = function (n) {\n' +
michael@0 124 ' return n + 1;\n' +
michael@0 125 ' };'
michael@0 126 ],
michael@0 127 sourceRoot: '/the/root',
michael@0 128 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
michael@0 129 };
michael@0 130 exports.emptyMap = {
michael@0 131 version: 3,
michael@0 132 file: 'min.js',
michael@0 133 names: [],
michael@0 134 sources: [],
michael@0 135 mappings: ''
michael@0 136 };
michael@0 137
michael@0 138
michael@0 139 function assertMapping(generatedLine, generatedColumn, originalSource,
michael@0 140 originalLine, originalColumn, name, map, assert,
michael@0 141 dontTestGenerated, dontTestOriginal) {
michael@0 142 if (!dontTestOriginal) {
michael@0 143 var origMapping = map.originalPositionFor({
michael@0 144 line: generatedLine,
michael@0 145 column: generatedColumn
michael@0 146 });
michael@0 147 assert.equal(origMapping.name, name,
michael@0 148 'Incorrect name, expected ' + JSON.stringify(name)
michael@0 149 + ', got ' + JSON.stringify(origMapping.name));
michael@0 150 assert.equal(origMapping.line, originalLine,
michael@0 151 'Incorrect line, expected ' + JSON.stringify(originalLine)
michael@0 152 + ', got ' + JSON.stringify(origMapping.line));
michael@0 153 assert.equal(origMapping.column, originalColumn,
michael@0 154 'Incorrect column, expected ' + JSON.stringify(originalColumn)
michael@0 155 + ', got ' + JSON.stringify(origMapping.column));
michael@0 156
michael@0 157 var expectedSource;
michael@0 158
michael@0 159 if (originalSource && map.sourceRoot && originalSource.indexOf(map.sourceRoot) === 0) {
michael@0 160 expectedSource = originalSource;
michael@0 161 } else if (originalSource) {
michael@0 162 expectedSource = map.sourceRoot
michael@0 163 ? util.join(map.sourceRoot, originalSource)
michael@0 164 : originalSource;
michael@0 165 } else {
michael@0 166 expectedSource = null;
michael@0 167 }
michael@0 168
michael@0 169 assert.equal(origMapping.source, expectedSource,
michael@0 170 'Incorrect source, expected ' + JSON.stringify(expectedSource)
michael@0 171 + ', got ' + JSON.stringify(origMapping.source));
michael@0 172 }
michael@0 173
michael@0 174 if (!dontTestGenerated) {
michael@0 175 var genMapping = map.generatedPositionFor({
michael@0 176 source: originalSource,
michael@0 177 line: originalLine,
michael@0 178 column: originalColumn
michael@0 179 });
michael@0 180 assert.equal(genMapping.line, generatedLine,
michael@0 181 'Incorrect line, expected ' + JSON.stringify(generatedLine)
michael@0 182 + ', got ' + JSON.stringify(genMapping.line));
michael@0 183 assert.equal(genMapping.column, generatedColumn,
michael@0 184 'Incorrect column, expected ' + JSON.stringify(generatedColumn)
michael@0 185 + ', got ' + JSON.stringify(genMapping.column));
michael@0 186 }
michael@0 187 }
michael@0 188 exports.assertMapping = assertMapping;
michael@0 189
michael@0 190 function assertEqualMaps(assert, actualMap, expectedMap) {
michael@0 191 assert.equal(actualMap.version, expectedMap.version, "version mismatch");
michael@0 192 assert.equal(actualMap.file, expectedMap.file, "file mismatch");
michael@0 193 assert.equal(actualMap.names.length,
michael@0 194 expectedMap.names.length,
michael@0 195 "names length mismatch: " +
michael@0 196 actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
michael@0 197 for (var i = 0; i < actualMap.names.length; i++) {
michael@0 198 assert.equal(actualMap.names[i],
michael@0 199 expectedMap.names[i],
michael@0 200 "names[" + i + "] mismatch: " +
michael@0 201 actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
michael@0 202 }
michael@0 203 assert.equal(actualMap.sources.length,
michael@0 204 expectedMap.sources.length,
michael@0 205 "sources length mismatch: " +
michael@0 206 actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
michael@0 207 for (var i = 0; i < actualMap.sources.length; i++) {
michael@0 208 assert.equal(actualMap.sources[i],
michael@0 209 expectedMap.sources[i],
michael@0 210 "sources[" + i + "] length mismatch: " +
michael@0 211 actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
michael@0 212 }
michael@0 213 assert.equal(actualMap.sourceRoot,
michael@0 214 expectedMap.sourceRoot,
michael@0 215 "sourceRoot mismatch: " +
michael@0 216 actualMap.sourceRoot + " != " + expectedMap.sourceRoot);
michael@0 217 assert.equal(actualMap.mappings, expectedMap.mappings,
michael@0 218 "mappings mismatch:\nActual: " + actualMap.mappings + "\nExpected: " + expectedMap.mappings);
michael@0 219 if (actualMap.sourcesContent) {
michael@0 220 assert.equal(actualMap.sourcesContent.length,
michael@0 221 expectedMap.sourcesContent.length,
michael@0 222 "sourcesContent length mismatch");
michael@0 223 for (var i = 0; i < actualMap.sourcesContent.length; i++) {
michael@0 224 assert.equal(actualMap.sourcesContent[i],
michael@0 225 expectedMap.sourcesContent[i],
michael@0 226 "sourcesContent[" + i + "] mismatch");
michael@0 227 }
michael@0 228 }
michael@0 229 }
michael@0 230 exports.assertEqualMaps = assertEqualMaps;
michael@0 231
michael@0 232 });
michael@0 233 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 234 /*
michael@0 235 * Copyright 2011 Mozilla Foundation and contributors
michael@0 236 * Licensed under the New BSD license. See LICENSE or:
michael@0 237 * http://opensource.org/licenses/BSD-3-Clause
michael@0 238 */
michael@0 239 define('lib/source-map/util', ['require', 'exports', 'module' , ], function(require, exports, module) {
michael@0 240
michael@0 241 /**
michael@0 242 * This is a helper function for getting values from parameter/options
michael@0 243 * objects.
michael@0 244 *
michael@0 245 * @param args The object we are extracting values from
michael@0 246 * @param name The name of the property we are getting.
michael@0 247 * @param defaultValue An optional value to return if the property is missing
michael@0 248 * from the object. If this is not specified and the property is missing, an
michael@0 249 * error will be thrown.
michael@0 250 */
michael@0 251 function getArg(aArgs, aName, aDefaultValue) {
michael@0 252 if (aName in aArgs) {
michael@0 253 return aArgs[aName];
michael@0 254 } else if (arguments.length === 3) {
michael@0 255 return aDefaultValue;
michael@0 256 } else {
michael@0 257 throw new Error('"' + aName + '" is a required argument.');
michael@0 258 }
michael@0 259 }
michael@0 260 exports.getArg = getArg;
michael@0 261
michael@0 262 var urlRegexp = /([\w+\-.]+):\/\/((\w+:\w+)@)?([\w.]+)?(:(\d+))?(\S+)?/;
michael@0 263 var dataUrlRegexp = /^data:.+\,.+/;
michael@0 264
michael@0 265 function urlParse(aUrl) {
michael@0 266 var match = aUrl.match(urlRegexp);
michael@0 267 if (!match) {
michael@0 268 return null;
michael@0 269 }
michael@0 270 return {
michael@0 271 scheme: match[1],
michael@0 272 auth: match[3],
michael@0 273 host: match[4],
michael@0 274 port: match[6],
michael@0 275 path: match[7]
michael@0 276 };
michael@0 277 }
michael@0 278 exports.urlParse = urlParse;
michael@0 279
michael@0 280 function urlGenerate(aParsedUrl) {
michael@0 281 var url = aParsedUrl.scheme + "://";
michael@0 282 if (aParsedUrl.auth) {
michael@0 283 url += aParsedUrl.auth + "@"
michael@0 284 }
michael@0 285 if (aParsedUrl.host) {
michael@0 286 url += aParsedUrl.host;
michael@0 287 }
michael@0 288 if (aParsedUrl.port) {
michael@0 289 url += ":" + aParsedUrl.port
michael@0 290 }
michael@0 291 if (aParsedUrl.path) {
michael@0 292 url += aParsedUrl.path;
michael@0 293 }
michael@0 294 return url;
michael@0 295 }
michael@0 296 exports.urlGenerate = urlGenerate;
michael@0 297
michael@0 298 function join(aRoot, aPath) {
michael@0 299 var url;
michael@0 300
michael@0 301 if (aPath.match(urlRegexp) || aPath.match(dataUrlRegexp)) {
michael@0 302 return aPath;
michael@0 303 }
michael@0 304
michael@0 305 if (aPath.charAt(0) === '/' && (url = urlParse(aRoot))) {
michael@0 306 url.path = aPath;
michael@0 307 return urlGenerate(url);
michael@0 308 }
michael@0 309
michael@0 310 return aRoot.replace(/\/$/, '') + '/' + aPath;
michael@0 311 }
michael@0 312 exports.join = join;
michael@0 313
michael@0 314 /**
michael@0 315 * Because behavior goes wacky when you set `__proto__` on objects, we
michael@0 316 * have to prefix all the strings in our set with an arbitrary character.
michael@0 317 *
michael@0 318 * See https://github.com/mozilla/source-map/pull/31 and
michael@0 319 * https://github.com/mozilla/source-map/issues/30
michael@0 320 *
michael@0 321 * @param String aStr
michael@0 322 */
michael@0 323 function toSetString(aStr) {
michael@0 324 return '$' + aStr;
michael@0 325 }
michael@0 326 exports.toSetString = toSetString;
michael@0 327
michael@0 328 function fromSetString(aStr) {
michael@0 329 return aStr.substr(1);
michael@0 330 }
michael@0 331 exports.fromSetString = fromSetString;
michael@0 332
michael@0 333 function relative(aRoot, aPath) {
michael@0 334 aRoot = aRoot.replace(/\/$/, '');
michael@0 335
michael@0 336 var url = urlParse(aRoot);
michael@0 337 if (aPath.charAt(0) == "/" && url && url.path == "/") {
michael@0 338 return aPath.slice(1);
michael@0 339 }
michael@0 340
michael@0 341 return aPath.indexOf(aRoot + '/') === 0
michael@0 342 ? aPath.substr(aRoot.length + 1)
michael@0 343 : aPath;
michael@0 344 }
michael@0 345 exports.relative = relative;
michael@0 346
michael@0 347 function strcmp(aStr1, aStr2) {
michael@0 348 var s1 = aStr1 || "";
michael@0 349 var s2 = aStr2 || "";
michael@0 350 return (s1 > s2) - (s1 < s2);
michael@0 351 }
michael@0 352
michael@0 353 /**
michael@0 354 * Comparator between two mappings where the original positions are compared.
michael@0 355 *
michael@0 356 * Optionally pass in `true` as `onlyCompareGenerated` to consider two
michael@0 357 * mappings with the same original source/line/column, but different generated
michael@0 358 * line and column the same. Useful when searching for a mapping with a
michael@0 359 * stubbed out mapping.
michael@0 360 */
michael@0 361 function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {
michael@0 362 var cmp;
michael@0 363
michael@0 364 cmp = strcmp(mappingA.source, mappingB.source);
michael@0 365 if (cmp) {
michael@0 366 return cmp;
michael@0 367 }
michael@0 368
michael@0 369 cmp = mappingA.originalLine - mappingB.originalLine;
michael@0 370 if (cmp) {
michael@0 371 return cmp;
michael@0 372 }
michael@0 373
michael@0 374 cmp = mappingA.originalColumn - mappingB.originalColumn;
michael@0 375 if (cmp || onlyCompareOriginal) {
michael@0 376 return cmp;
michael@0 377 }
michael@0 378
michael@0 379 cmp = strcmp(mappingA.name, mappingB.name);
michael@0 380 if (cmp) {
michael@0 381 return cmp;
michael@0 382 }
michael@0 383
michael@0 384 cmp = mappingA.generatedLine - mappingB.generatedLine;
michael@0 385 if (cmp) {
michael@0 386 return cmp;
michael@0 387 }
michael@0 388
michael@0 389 return mappingA.generatedColumn - mappingB.generatedColumn;
michael@0 390 };
michael@0 391 exports.compareByOriginalPositions = compareByOriginalPositions;
michael@0 392
michael@0 393 /**
michael@0 394 * Comparator between two mappings where the generated positions are
michael@0 395 * compared.
michael@0 396 *
michael@0 397 * Optionally pass in `true` as `onlyCompareGenerated` to consider two
michael@0 398 * mappings with the same generated line and column, but different
michael@0 399 * source/name/original line and column the same. Useful when searching for a
michael@0 400 * mapping with a stubbed out mapping.
michael@0 401 */
michael@0 402 function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) {
michael@0 403 var cmp;
michael@0 404
michael@0 405 cmp = mappingA.generatedLine - mappingB.generatedLine;
michael@0 406 if (cmp) {
michael@0 407 return cmp;
michael@0 408 }
michael@0 409
michael@0 410 cmp = mappingA.generatedColumn - mappingB.generatedColumn;
michael@0 411 if (cmp || onlyCompareGenerated) {
michael@0 412 return cmp;
michael@0 413 }
michael@0 414
michael@0 415 cmp = strcmp(mappingA.source, mappingB.source);
michael@0 416 if (cmp) {
michael@0 417 return cmp;
michael@0 418 }
michael@0 419
michael@0 420 cmp = mappingA.originalLine - mappingB.originalLine;
michael@0 421 if (cmp) {
michael@0 422 return cmp;
michael@0 423 }
michael@0 424
michael@0 425 cmp = mappingA.originalColumn - mappingB.originalColumn;
michael@0 426 if (cmp) {
michael@0 427 return cmp;
michael@0 428 }
michael@0 429
michael@0 430 return strcmp(mappingA.name, mappingB.name);
michael@0 431 };
michael@0 432 exports.compareByGeneratedPositions = compareByGeneratedPositions;
michael@0 433
michael@0 434 });
michael@0 435 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 436 /*
michael@0 437 * Copyright 2011 Mozilla Foundation and contributors
michael@0 438 * Licensed under the New BSD license. See LICENSE or:
michael@0 439 * http://opensource.org/licenses/BSD-3-Clause
michael@0 440 */
michael@0 441 function runSourceMapTests(modName, do_throw) {
michael@0 442 let mod = require(modName);
michael@0 443 let assert = require('test/source-map/assert');
michael@0 444 let util = require('test/source-map/util');
michael@0 445
michael@0 446 assert.init(do_throw);
michael@0 447
michael@0 448 for (let k in mod) {
michael@0 449 if (/^test/.test(k)) {
michael@0 450 mod[k](assert, util);
michael@0 451 }
michael@0 452 }
michael@0 453
michael@0 454 }
michael@0 455 this.runSourceMapTests = runSourceMapTests;

mercurial