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.

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

mercurial