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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/sourcemap/tests/unit/Utils.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,455 @@
     1.4 +/* -*- Mode: js; js-indent-level: 2; -*- */
     1.5 +/*
     1.6 + * Copyright 2011 Mozilla Foundation and contributors
     1.7 + * Licensed under the New BSD license. See LICENSE or:
     1.8 + * http://opensource.org/licenses/BSD-3-Clause
     1.9 + */
    1.10 +
    1.11 +/*
    1.12 + * WARNING!
    1.13 + *
    1.14 + * Do not edit this file directly, it is built from the sources at
    1.15 + * https://github.com/mozilla/source-map/
    1.16 + */
    1.17 +
    1.18 +Components.utils.import('resource://gre/modules/devtools/Require.jsm');
    1.19 +Components.utils.import('resource://gre/modules/devtools/SourceMap.jsm');
    1.20 +
    1.21 +this.EXPORTED_SYMBOLS = [ "define", "runSourceMapTests" ];
    1.22 +/* -*- Mode: js; js-indent-level: 2; -*- */
    1.23 +/*
    1.24 + * Copyright 2011 Mozilla Foundation and contributors
    1.25 + * Licensed under the New BSD license. See LICENSE or:
    1.26 + * http://opensource.org/licenses/BSD-3-Clause
    1.27 + */
    1.28 +define('test/source-map/assert', ['exports'], function (exports) {
    1.29 +
    1.30 +  let do_throw = function (msg) {
    1.31 +    throw new Error(msg);
    1.32 +  };
    1.33 +
    1.34 +  exports.init = function (throw_fn) {
    1.35 +    do_throw = throw_fn;
    1.36 +  };
    1.37 +
    1.38 +  exports.doesNotThrow = function (fn) {
    1.39 +    try {
    1.40 +      fn();
    1.41 +    }
    1.42 +    catch (e) {
    1.43 +      do_throw(e.message);
    1.44 +    }
    1.45 +  };
    1.46 +
    1.47 +  exports.equal = function (actual, expected, msg) {
    1.48 +    msg = msg || String(actual) + ' != ' + String(expected);
    1.49 +    if (actual != expected) {
    1.50 +      do_throw(msg);
    1.51 +    }
    1.52 +  };
    1.53 +
    1.54 +  exports.ok = function (val, msg) {
    1.55 +    msg = msg || String(val) + ' is falsey';
    1.56 +    if (!Boolean(val)) {
    1.57 +      do_throw(msg);
    1.58 +    }
    1.59 +  };
    1.60 +
    1.61 +  exports.strictEqual = function (actual, expected, msg) {
    1.62 +    msg = msg || String(actual) + ' !== ' + String(expected);
    1.63 +    if (actual !== expected) {
    1.64 +      do_throw(msg);
    1.65 +    }
    1.66 +  };
    1.67 +
    1.68 +  exports.throws = function (fn) {
    1.69 +    try {
    1.70 +      fn();
    1.71 +      do_throw('Expected an error to be thrown, but it wasn\'t.');
    1.72 +    }
    1.73 +    catch (e) {
    1.74 +    }
    1.75 +  };
    1.76 +
    1.77 +});
    1.78 +/* -*- Mode: js; js-indent-level: 2; -*- */
    1.79 +/*
    1.80 + * Copyright 2011 Mozilla Foundation and contributors
    1.81 + * Licensed under the New BSD license. See LICENSE or:
    1.82 + * http://opensource.org/licenses/BSD-3-Clause
    1.83 + */
    1.84 +define('test/source-map/util', ['require', 'exports', 'module' ,  'lib/source-map/util'], function(require, exports, module) {
    1.85 +
    1.86 +  var util = require('source-map/util');
    1.87 +
    1.88 +  // This is a test mapping which maps functions from two different files
    1.89 +  // (one.js and two.js) to a minified generated source.
    1.90 +  //
    1.91 +  // Here is one.js:
    1.92 +  //
    1.93 +  //   ONE.foo = function (bar) {
    1.94 +  //     return baz(bar);
    1.95 +  //   };
    1.96 +  //
    1.97 +  // Here is two.js:
    1.98 +  //
    1.99 +  //   TWO.inc = function (n) {
   1.100 +  //     return n + 1;
   1.101 +  //   };
   1.102 +  //
   1.103 +  // And here is the generated code (min.js):
   1.104 +  //
   1.105 +  //   ONE.foo=function(a){return baz(a);};
   1.106 +  //   TWO.inc=function(a){return a+1;};
   1.107 +  exports.testGeneratedCode = " ONE.foo=function(a){return baz(a);};\n"+
   1.108 +                              " TWO.inc=function(a){return a+1;};";
   1.109 +  exports.testMap = {
   1.110 +    version: 3,
   1.111 +    file: 'min.js',
   1.112 +    names: ['bar', 'baz', 'n'],
   1.113 +    sources: ['one.js', 'two.js'],
   1.114 +    sourceRoot: '/the/root',
   1.115 +    mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
   1.116 +  };
   1.117 +  exports.testMapWithSourcesContent = {
   1.118 +    version: 3,
   1.119 +    file: 'min.js',
   1.120 +    names: ['bar', 'baz', 'n'],
   1.121 +    sources: ['one.js', 'two.js'],
   1.122 +    sourcesContent: [
   1.123 +      ' ONE.foo = function (bar) {\n' +
   1.124 +      '   return baz(bar);\n' +
   1.125 +      ' };',
   1.126 +      ' TWO.inc = function (n) {\n' +
   1.127 +      '   return n + 1;\n' +
   1.128 +      ' };'
   1.129 +    ],
   1.130 +    sourceRoot: '/the/root',
   1.131 +    mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
   1.132 +  };
   1.133 +  exports.emptyMap = {
   1.134 +    version: 3,
   1.135 +    file: 'min.js',
   1.136 +    names: [],
   1.137 +    sources: [],
   1.138 +    mappings: ''
   1.139 +  };
   1.140 +
   1.141 +
   1.142 +  function assertMapping(generatedLine, generatedColumn, originalSource,
   1.143 +                         originalLine, originalColumn, name, map, assert,
   1.144 +                         dontTestGenerated, dontTestOriginal) {
   1.145 +    if (!dontTestOriginal) {
   1.146 +      var origMapping = map.originalPositionFor({
   1.147 +        line: generatedLine,
   1.148 +        column: generatedColumn
   1.149 +      });
   1.150 +      assert.equal(origMapping.name, name,
   1.151 +                   'Incorrect name, expected ' + JSON.stringify(name)
   1.152 +                   + ', got ' + JSON.stringify(origMapping.name));
   1.153 +      assert.equal(origMapping.line, originalLine,
   1.154 +                   'Incorrect line, expected ' + JSON.stringify(originalLine)
   1.155 +                   + ', got ' + JSON.stringify(origMapping.line));
   1.156 +      assert.equal(origMapping.column, originalColumn,
   1.157 +                   'Incorrect column, expected ' + JSON.stringify(originalColumn)
   1.158 +                   + ', got ' + JSON.stringify(origMapping.column));
   1.159 +
   1.160 +      var expectedSource;
   1.161 +
   1.162 +      if (originalSource && map.sourceRoot && originalSource.indexOf(map.sourceRoot) === 0) {
   1.163 +        expectedSource = originalSource;
   1.164 +      } else if (originalSource) {
   1.165 +        expectedSource = map.sourceRoot
   1.166 +          ? util.join(map.sourceRoot, originalSource)
   1.167 +          : originalSource;
   1.168 +      } else {
   1.169 +        expectedSource = null;
   1.170 +      }
   1.171 +
   1.172 +      assert.equal(origMapping.source, expectedSource,
   1.173 +                   'Incorrect source, expected ' + JSON.stringify(expectedSource)
   1.174 +                   + ', got ' + JSON.stringify(origMapping.source));
   1.175 +    }
   1.176 +
   1.177 +    if (!dontTestGenerated) {
   1.178 +      var genMapping = map.generatedPositionFor({
   1.179 +        source: originalSource,
   1.180 +        line: originalLine,
   1.181 +        column: originalColumn
   1.182 +      });
   1.183 +      assert.equal(genMapping.line, generatedLine,
   1.184 +                   'Incorrect line, expected ' + JSON.stringify(generatedLine)
   1.185 +                   + ', got ' + JSON.stringify(genMapping.line));
   1.186 +      assert.equal(genMapping.column, generatedColumn,
   1.187 +                   'Incorrect column, expected ' + JSON.stringify(generatedColumn)
   1.188 +                   + ', got ' + JSON.stringify(genMapping.column));
   1.189 +    }
   1.190 +  }
   1.191 +  exports.assertMapping = assertMapping;
   1.192 +
   1.193 +  function assertEqualMaps(assert, actualMap, expectedMap) {
   1.194 +    assert.equal(actualMap.version, expectedMap.version, "version mismatch");
   1.195 +    assert.equal(actualMap.file, expectedMap.file, "file mismatch");
   1.196 +    assert.equal(actualMap.names.length,
   1.197 +                 expectedMap.names.length,
   1.198 +                 "names length mismatch: " +
   1.199 +                   actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
   1.200 +    for (var i = 0; i < actualMap.names.length; i++) {
   1.201 +      assert.equal(actualMap.names[i],
   1.202 +                   expectedMap.names[i],
   1.203 +                   "names[" + i + "] mismatch: " +
   1.204 +                     actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
   1.205 +    }
   1.206 +    assert.equal(actualMap.sources.length,
   1.207 +                 expectedMap.sources.length,
   1.208 +                 "sources length mismatch: " +
   1.209 +                   actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
   1.210 +    for (var i = 0; i < actualMap.sources.length; i++) {
   1.211 +      assert.equal(actualMap.sources[i],
   1.212 +                   expectedMap.sources[i],
   1.213 +                   "sources[" + i + "] length mismatch: " +
   1.214 +                   actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
   1.215 +    }
   1.216 +    assert.equal(actualMap.sourceRoot,
   1.217 +                 expectedMap.sourceRoot,
   1.218 +                 "sourceRoot mismatch: " +
   1.219 +                   actualMap.sourceRoot + " != " + expectedMap.sourceRoot);
   1.220 +    assert.equal(actualMap.mappings, expectedMap.mappings,
   1.221 +                 "mappings mismatch:\nActual:   " + actualMap.mappings + "\nExpected: " + expectedMap.mappings);
   1.222 +    if (actualMap.sourcesContent) {
   1.223 +      assert.equal(actualMap.sourcesContent.length,
   1.224 +                   expectedMap.sourcesContent.length,
   1.225 +                   "sourcesContent length mismatch");
   1.226 +      for (var i = 0; i < actualMap.sourcesContent.length; i++) {
   1.227 +        assert.equal(actualMap.sourcesContent[i],
   1.228 +                     expectedMap.sourcesContent[i],
   1.229 +                     "sourcesContent[" + i + "] mismatch");
   1.230 +      }
   1.231 +    }
   1.232 +  }
   1.233 +  exports.assertEqualMaps = assertEqualMaps;
   1.234 +
   1.235 +});
   1.236 +/* -*- Mode: js; js-indent-level: 2; -*- */
   1.237 +/*
   1.238 + * Copyright 2011 Mozilla Foundation and contributors
   1.239 + * Licensed under the New BSD license. See LICENSE or:
   1.240 + * http://opensource.org/licenses/BSD-3-Clause
   1.241 + */
   1.242 +define('lib/source-map/util', ['require', 'exports', 'module' , ], function(require, exports, module) {
   1.243 +
   1.244 +  /**
   1.245 +   * This is a helper function for getting values from parameter/options
   1.246 +   * objects.
   1.247 +   *
   1.248 +   * @param args The object we are extracting values from
   1.249 +   * @param name The name of the property we are getting.
   1.250 +   * @param defaultValue An optional value to return if the property is missing
   1.251 +   * from the object. If this is not specified and the property is missing, an
   1.252 +   * error will be thrown.
   1.253 +   */
   1.254 +  function getArg(aArgs, aName, aDefaultValue) {
   1.255 +    if (aName in aArgs) {
   1.256 +      return aArgs[aName];
   1.257 +    } else if (arguments.length === 3) {
   1.258 +      return aDefaultValue;
   1.259 +    } else {
   1.260 +      throw new Error('"' + aName + '" is a required argument.');
   1.261 +    }
   1.262 +  }
   1.263 +  exports.getArg = getArg;
   1.264 +
   1.265 +  var urlRegexp = /([\w+\-.]+):\/\/((\w+:\w+)@)?([\w.]+)?(:(\d+))?(\S+)?/;
   1.266 +  var dataUrlRegexp = /^data:.+\,.+/;
   1.267 +
   1.268 +  function urlParse(aUrl) {
   1.269 +    var match = aUrl.match(urlRegexp);
   1.270 +    if (!match) {
   1.271 +      return null;
   1.272 +    }
   1.273 +    return {
   1.274 +      scheme: match[1],
   1.275 +      auth: match[3],
   1.276 +      host: match[4],
   1.277 +      port: match[6],
   1.278 +      path: match[7]
   1.279 +    };
   1.280 +  }
   1.281 +  exports.urlParse = urlParse;
   1.282 +
   1.283 +  function urlGenerate(aParsedUrl) {
   1.284 +    var url = aParsedUrl.scheme + "://";
   1.285 +    if (aParsedUrl.auth) {
   1.286 +      url += aParsedUrl.auth + "@"
   1.287 +    }
   1.288 +    if (aParsedUrl.host) {
   1.289 +      url += aParsedUrl.host;
   1.290 +    }
   1.291 +    if (aParsedUrl.port) {
   1.292 +      url += ":" + aParsedUrl.port
   1.293 +    }
   1.294 +    if (aParsedUrl.path) {
   1.295 +      url += aParsedUrl.path;
   1.296 +    }
   1.297 +    return url;
   1.298 +  }
   1.299 +  exports.urlGenerate = urlGenerate;
   1.300 +
   1.301 +  function join(aRoot, aPath) {
   1.302 +    var url;
   1.303 +
   1.304 +    if (aPath.match(urlRegexp) || aPath.match(dataUrlRegexp)) {
   1.305 +      return aPath;
   1.306 +    }
   1.307 +
   1.308 +    if (aPath.charAt(0) === '/' && (url = urlParse(aRoot))) {
   1.309 +      url.path = aPath;
   1.310 +      return urlGenerate(url);
   1.311 +    }
   1.312 +
   1.313 +    return aRoot.replace(/\/$/, '') + '/' + aPath;
   1.314 +  }
   1.315 +  exports.join = join;
   1.316 +
   1.317 +  /**
   1.318 +   * Because behavior goes wacky when you set `__proto__` on objects, we
   1.319 +   * have to prefix all the strings in our set with an arbitrary character.
   1.320 +   *
   1.321 +   * See https://github.com/mozilla/source-map/pull/31 and
   1.322 +   * https://github.com/mozilla/source-map/issues/30
   1.323 +   *
   1.324 +   * @param String aStr
   1.325 +   */
   1.326 +  function toSetString(aStr) {
   1.327 +    return '$' + aStr;
   1.328 +  }
   1.329 +  exports.toSetString = toSetString;
   1.330 +
   1.331 +  function fromSetString(aStr) {
   1.332 +    return aStr.substr(1);
   1.333 +  }
   1.334 +  exports.fromSetString = fromSetString;
   1.335 +
   1.336 +  function relative(aRoot, aPath) {
   1.337 +    aRoot = aRoot.replace(/\/$/, '');
   1.338 +
   1.339 +    var url = urlParse(aRoot);
   1.340 +    if (aPath.charAt(0) == "/" && url && url.path == "/") {
   1.341 +      return aPath.slice(1);
   1.342 +    }
   1.343 +
   1.344 +    return aPath.indexOf(aRoot + '/') === 0
   1.345 +      ? aPath.substr(aRoot.length + 1)
   1.346 +      : aPath;
   1.347 +  }
   1.348 +  exports.relative = relative;
   1.349 +
   1.350 +  function strcmp(aStr1, aStr2) {
   1.351 +    var s1 = aStr1 || "";
   1.352 +    var s2 = aStr2 || "";
   1.353 +    return (s1 > s2) - (s1 < s2);
   1.354 +  }
   1.355 +
   1.356 +  /**
   1.357 +   * Comparator between two mappings where the original positions are compared.
   1.358 +   *
   1.359 +   * Optionally pass in `true` as `onlyCompareGenerated` to consider two
   1.360 +   * mappings with the same original source/line/column, but different generated
   1.361 +   * line and column the same. Useful when searching for a mapping with a
   1.362 +   * stubbed out mapping.
   1.363 +   */
   1.364 +  function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {
   1.365 +    var cmp;
   1.366 +
   1.367 +    cmp = strcmp(mappingA.source, mappingB.source);
   1.368 +    if (cmp) {
   1.369 +      return cmp;
   1.370 +    }
   1.371 +
   1.372 +    cmp = mappingA.originalLine - mappingB.originalLine;
   1.373 +    if (cmp) {
   1.374 +      return cmp;
   1.375 +    }
   1.376 +
   1.377 +    cmp = mappingA.originalColumn - mappingB.originalColumn;
   1.378 +    if (cmp || onlyCompareOriginal) {
   1.379 +      return cmp;
   1.380 +    }
   1.381 +
   1.382 +    cmp = strcmp(mappingA.name, mappingB.name);
   1.383 +    if (cmp) {
   1.384 +      return cmp;
   1.385 +    }
   1.386 +
   1.387 +    cmp = mappingA.generatedLine - mappingB.generatedLine;
   1.388 +    if (cmp) {
   1.389 +      return cmp;
   1.390 +    }
   1.391 +
   1.392 +    return mappingA.generatedColumn - mappingB.generatedColumn;
   1.393 +  };
   1.394 +  exports.compareByOriginalPositions = compareByOriginalPositions;
   1.395 +
   1.396 +  /**
   1.397 +   * Comparator between two mappings where the generated positions are
   1.398 +   * compared.
   1.399 +   *
   1.400 +   * Optionally pass in `true` as `onlyCompareGenerated` to consider two
   1.401 +   * mappings with the same generated line and column, but different
   1.402 +   * source/name/original line and column the same. Useful when searching for a
   1.403 +   * mapping with a stubbed out mapping.
   1.404 +   */
   1.405 +  function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) {
   1.406 +    var cmp;
   1.407 +
   1.408 +    cmp = mappingA.generatedLine - mappingB.generatedLine;
   1.409 +    if (cmp) {
   1.410 +      return cmp;
   1.411 +    }
   1.412 +
   1.413 +    cmp = mappingA.generatedColumn - mappingB.generatedColumn;
   1.414 +    if (cmp || onlyCompareGenerated) {
   1.415 +      return cmp;
   1.416 +    }
   1.417 +
   1.418 +    cmp = strcmp(mappingA.source, mappingB.source);
   1.419 +    if (cmp) {
   1.420 +      return cmp;
   1.421 +    }
   1.422 +
   1.423 +    cmp = mappingA.originalLine - mappingB.originalLine;
   1.424 +    if (cmp) {
   1.425 +      return cmp;
   1.426 +    }
   1.427 +
   1.428 +    cmp = mappingA.originalColumn - mappingB.originalColumn;
   1.429 +    if (cmp) {
   1.430 +      return cmp;
   1.431 +    }
   1.432 +
   1.433 +    return strcmp(mappingA.name, mappingB.name);
   1.434 +  };
   1.435 +  exports.compareByGeneratedPositions = compareByGeneratedPositions;
   1.436 +
   1.437 +});
   1.438 +/* -*- Mode: js; js-indent-level: 2; -*- */
   1.439 +/*
   1.440 + * Copyright 2011 Mozilla Foundation and contributors
   1.441 + * Licensed under the New BSD license. See LICENSE or:
   1.442 + * http://opensource.org/licenses/BSD-3-Clause
   1.443 + */
   1.444 +function runSourceMapTests(modName, do_throw) {
   1.445 +  let mod = require(modName);
   1.446 +  let assert = require('test/source-map/assert');
   1.447 +  let util = require('test/source-map/util');
   1.448 +
   1.449 +  assert.init(do_throw);
   1.450 +
   1.451 +  for (let k in mod) {
   1.452 +    if (/^test/.test(k)) {
   1.453 +      mod[k](assert, util);
   1.454 +    }
   1.455 +  }
   1.456 +
   1.457 +}
   1.458 +this.runSourceMapTests = runSourceMapTests;

mercurial