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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/sourcemap/tests/unit/test_source_node.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,373 @@
     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-node", ["require", "exports", "module"], function (require, exports, module) {
    1.19 +
    1.20 +  var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;
    1.21 +  var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;
    1.22 +  var SourceNode = require('source-map/source-node').SourceNode;
    1.23 +
    1.24 +  exports['test .add()'] = function (assert, util) {
    1.25 +    var node = new SourceNode(null, null, null);
    1.26 +
    1.27 +    // Adding a string works.
    1.28 +    node.add('function noop() {}');
    1.29 +
    1.30 +    // Adding another source node works.
    1.31 +    node.add(new SourceNode(null, null, null));
    1.32 +
    1.33 +    // Adding an array works.
    1.34 +    node.add(['function foo() {',
    1.35 +              new SourceNode(null, null, null,
    1.36 +                             'return 10;'),
    1.37 +              '}']);
    1.38 +
    1.39 +    // Adding other stuff doesn't.
    1.40 +    assert.throws(function () {
    1.41 +      node.add({});
    1.42 +    });
    1.43 +    assert.throws(function () {
    1.44 +      node.add(function () {});
    1.45 +    });
    1.46 +  };
    1.47 +
    1.48 +  exports['test .prepend()'] = function (assert, util) {
    1.49 +    var node = new SourceNode(null, null, null);
    1.50 +
    1.51 +    // Prepending a string works.
    1.52 +    node.prepend('function noop() {}');
    1.53 +    assert.equal(node.children[0], 'function noop() {}');
    1.54 +    assert.equal(node.children.length, 1);
    1.55 +
    1.56 +    // Prepending another source node works.
    1.57 +    node.prepend(new SourceNode(null, null, null));
    1.58 +    assert.equal(node.children[0], '');
    1.59 +    assert.equal(node.children[1], 'function noop() {}');
    1.60 +    assert.equal(node.children.length, 2);
    1.61 +
    1.62 +    // Prepending an array works.
    1.63 +    node.prepend(['function foo() {',
    1.64 +              new SourceNode(null, null, null,
    1.65 +                             'return 10;'),
    1.66 +              '}']);
    1.67 +    assert.equal(node.children[0], 'function foo() {');
    1.68 +    assert.equal(node.children[1], 'return 10;');
    1.69 +    assert.equal(node.children[2], '}');
    1.70 +    assert.equal(node.children[3], '');
    1.71 +    assert.equal(node.children[4], 'function noop() {}');
    1.72 +    assert.equal(node.children.length, 5);
    1.73 +
    1.74 +    // Prepending other stuff doesn't.
    1.75 +    assert.throws(function () {
    1.76 +      node.prepend({});
    1.77 +    });
    1.78 +    assert.throws(function () {
    1.79 +      node.prepend(function () {});
    1.80 +    });
    1.81 +  };
    1.82 +
    1.83 +  exports['test .toString()'] = function (assert, util) {
    1.84 +    assert.equal((new SourceNode(null, null, null,
    1.85 +                                 ['function foo() {',
    1.86 +                                  new SourceNode(null, null, null, 'return 10;'),
    1.87 +                                  '}'])).toString(),
    1.88 +                 'function foo() {return 10;}');
    1.89 +  };
    1.90 +
    1.91 +  exports['test .join()'] = function (assert, util) {
    1.92 +    assert.equal((new SourceNode(null, null, null,
    1.93 +                                 ['a', 'b', 'c', 'd'])).join(', ').toString(),
    1.94 +                 'a, b, c, d');
    1.95 +  };
    1.96 +
    1.97 +  exports['test .walk()'] = function (assert, util) {
    1.98 +    var node = new SourceNode(null, null, null,
    1.99 +                              ['(function () {\n',
   1.100 +                               '  ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n',
   1.101 +                               '  ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
   1.102 +                               '}());']);
   1.103 +    var expected = [
   1.104 +      { str: '(function () {\n', source: null,   line: null, column: null },
   1.105 +      { str: '  ',               source: null,   line: null, column: null },
   1.106 +      { str: 'someCall()',       source: 'a.js', line: 1,    column: 0    },
   1.107 +      { str: ';\n',              source: null,   line: null, column: null },
   1.108 +      { str: '  ',               source: null,   line: null, column: null },
   1.109 +      { str: 'if (foo) bar()',   source: 'b.js', line: 2,    column: 0    },
   1.110 +      { str: ';\n',              source: null,   line: null, column: null },
   1.111 +      { str: '}());',            source: null,   line: null, column: null },
   1.112 +    ];
   1.113 +    var i = 0;
   1.114 +    node.walk(function (chunk, loc) {
   1.115 +      assert.equal(expected[i].str, chunk);
   1.116 +      assert.equal(expected[i].source, loc.source);
   1.117 +      assert.equal(expected[i].line, loc.line);
   1.118 +      assert.equal(expected[i].column, loc.column);
   1.119 +      i++;
   1.120 +    });
   1.121 +  };
   1.122 +
   1.123 +  exports['test .replaceRight'] = function (assert, util) {
   1.124 +    var node;
   1.125 +
   1.126 +    // Not nested
   1.127 +    node = new SourceNode(null, null, null, 'hello world');
   1.128 +    node.replaceRight(/world/, 'universe');
   1.129 +    assert.equal(node.toString(), 'hello universe');
   1.130 +
   1.131 +    // Nested
   1.132 +    node = new SourceNode(null, null, null,
   1.133 +                          [new SourceNode(null, null, null, 'hey sexy mama, '),
   1.134 +                           new SourceNode(null, null, null, 'want to kill all humans?')]);
   1.135 +    node.replaceRight(/kill all humans/, 'watch Futurama');
   1.136 +    assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');
   1.137 +  };
   1.138 +
   1.139 +  exports['test .toStringWithSourceMap()'] = function (assert, util) {
   1.140 +    var node = new SourceNode(null, null, null,
   1.141 +                              ['(function () {\n',
   1.142 +                               '  ',
   1.143 +                                 new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),
   1.144 +                                 new SourceNode(1, 8, 'a.js', '()'),
   1.145 +                                 ';\n',
   1.146 +                               '  ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
   1.147 +                               '}());']);
   1.148 +    var map = node.toStringWithSourceMap({
   1.149 +      file: 'foo.js'
   1.150 +    }).map;
   1.151 +
   1.152 +    assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
   1.153 +    map = new SourceMapConsumer(map.toString());
   1.154 +
   1.155 +    var actual;
   1.156 +
   1.157 +    actual = map.originalPositionFor({
   1.158 +      line: 1,
   1.159 +      column: 4
   1.160 +    });
   1.161 +    assert.equal(actual.source, null);
   1.162 +    assert.equal(actual.line, null);
   1.163 +    assert.equal(actual.column, null);
   1.164 +
   1.165 +    actual = map.originalPositionFor({
   1.166 +      line: 2,
   1.167 +      column: 2
   1.168 +    });
   1.169 +    assert.equal(actual.source, 'a.js');
   1.170 +    assert.equal(actual.line, 1);
   1.171 +    assert.equal(actual.column, 0);
   1.172 +    assert.equal(actual.name, 'originalCall');
   1.173 +
   1.174 +    actual = map.originalPositionFor({
   1.175 +      line: 3,
   1.176 +      column: 2
   1.177 +    });
   1.178 +    assert.equal(actual.source, 'b.js');
   1.179 +    assert.equal(actual.line, 2);
   1.180 +    assert.equal(actual.column, 0);
   1.181 +
   1.182 +    actual = map.originalPositionFor({
   1.183 +      line: 3,
   1.184 +      column: 16
   1.185 +    });
   1.186 +    assert.equal(actual.source, null);
   1.187 +    assert.equal(actual.line, null);
   1.188 +    assert.equal(actual.column, null);
   1.189 +
   1.190 +    actual = map.originalPositionFor({
   1.191 +      line: 4,
   1.192 +      column: 2
   1.193 +    });
   1.194 +    assert.equal(actual.source, null);
   1.195 +    assert.equal(actual.line, null);
   1.196 +    assert.equal(actual.column, null);
   1.197 +  };
   1.198 +
   1.199 +  exports['test .fromStringWithSourceMap()'] = function (assert, util) {
   1.200 +    var node = SourceNode.fromStringWithSourceMap(
   1.201 +                              util.testGeneratedCode,
   1.202 +                              new SourceMapConsumer(util.testMap));
   1.203 +
   1.204 +    var result = node.toStringWithSourceMap({
   1.205 +      file: 'min.js'
   1.206 +    });
   1.207 +    var map = result.map;
   1.208 +    var code = result.code;
   1.209 +
   1.210 +    assert.equal(code, util.testGeneratedCode);
   1.211 +    assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
   1.212 +    map = map.toJSON();
   1.213 +    assert.equal(map.version, util.testMap.version);
   1.214 +    assert.equal(map.file, util.testMap.file);
   1.215 +    assert.equal(map.mappings, util.testMap.mappings);
   1.216 +  };
   1.217 +
   1.218 +  exports['test .fromStringWithSourceMap() empty map'] = function (assert, util) {
   1.219 +    var node = SourceNode.fromStringWithSourceMap(
   1.220 +                              util.testGeneratedCode,
   1.221 +                              new SourceMapConsumer(util.emptyMap));
   1.222 +    var result = node.toStringWithSourceMap({
   1.223 +      file: 'min.js'
   1.224 +    });
   1.225 +    var map = result.map;
   1.226 +    var code = result.code;
   1.227 +
   1.228 +    assert.equal(code, util.testGeneratedCode);
   1.229 +    assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
   1.230 +    map = map.toJSON();
   1.231 +    assert.equal(map.version, util.emptyMap.version);
   1.232 +    assert.equal(map.file, util.emptyMap.file);
   1.233 +    assert.equal(map.mappings.length, util.emptyMap.mappings.length);
   1.234 +    assert.equal(map.mappings, util.emptyMap.mappings);
   1.235 +  };
   1.236 +
   1.237 +  exports['test .fromStringWithSourceMap() complex version'] = function (assert, util) {
   1.238 +    var input = new SourceNode(null, null, null, [
   1.239 +      "(function() {\n",
   1.240 +        "  var Test = {};\n",
   1.241 +        "  ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };\n"),
   1.242 +        "  ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), "\n",
   1.243 +        "}());\n",
   1.244 +        "/* Generated Source */"]);
   1.245 +    input = input.toStringWithSourceMap({
   1.246 +      file: 'foo.js'
   1.247 +    });
   1.248 +
   1.249 +    var node = SourceNode.fromStringWithSourceMap(
   1.250 +                              input.code,
   1.251 +                              new SourceMapConsumer(input.map.toString()));
   1.252 +
   1.253 +    var result = node.toStringWithSourceMap({
   1.254 +      file: 'foo.js'
   1.255 +    });
   1.256 +    var map = result.map;
   1.257 +    var code = result.code;
   1.258 +
   1.259 +    assert.equal(code, input.code);
   1.260 +    assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
   1.261 +    map = map.toJSON();
   1.262 +    var inputMap = input.map.toJSON();
   1.263 +    util.assertEqualMaps(assert, map, inputMap);
   1.264 +  };
   1.265 +
   1.266 +  exports['test .fromStringWithSourceMap() merging duplicate mappings'] = function (assert, util) {
   1.267 +    var input = new SourceNode(null, null, null, [
   1.268 +      new SourceNode(1, 0, "a.js", "(function"),
   1.269 +      new SourceNode(1, 0, "a.js", "() {\n"),
   1.270 +      "  ",
   1.271 +      new SourceNode(1, 0, "a.js", "var Test = "),
   1.272 +      new SourceNode(1, 0, "b.js", "{};\n"),
   1.273 +      new SourceNode(2, 0, "b.js", "Test"),
   1.274 +      new SourceNode(2, 0, "b.js", ".A", "A"),
   1.275 +      new SourceNode(2, 20, "b.js", " = { value: 1234 };\n", "A"),
   1.276 +      "}());\n",
   1.277 +      "/* Generated Source */"
   1.278 +    ]);
   1.279 +    input = input.toStringWithSourceMap({
   1.280 +      file: 'foo.js'
   1.281 +    });
   1.282 +
   1.283 +    var correctMap = new SourceMapGenerator({
   1.284 +      file: 'foo.js'
   1.285 +    });
   1.286 +    correctMap.addMapping({
   1.287 +      generated: { line: 1, column: 0 },
   1.288 +      source: 'a.js',
   1.289 +      original: { line: 1, column: 0 }
   1.290 +    });
   1.291 +    correctMap.addMapping({
   1.292 +      generated: { line: 2, column: 0 }
   1.293 +    });
   1.294 +    correctMap.addMapping({
   1.295 +      generated: { line: 2, column: 2 },
   1.296 +      source: 'a.js',
   1.297 +      original: { line: 1, column: 0 }
   1.298 +    });
   1.299 +    correctMap.addMapping({
   1.300 +      generated: { line: 2, column: 13 },
   1.301 +      source: 'b.js',
   1.302 +      original: { line: 1, column: 0 }
   1.303 +    });
   1.304 +    correctMap.addMapping({
   1.305 +      generated: { line: 3, column: 0 },
   1.306 +      source: 'b.js',
   1.307 +      original: { line: 2, column: 0 }
   1.308 +    });
   1.309 +    correctMap.addMapping({
   1.310 +      generated: { line: 3, column: 4 },
   1.311 +      source: 'b.js',
   1.312 +      name: 'A',
   1.313 +      original: { line: 2, column: 0 }
   1.314 +    });
   1.315 +    correctMap.addMapping({
   1.316 +      generated: { line: 3, column: 6 },
   1.317 +      source: 'b.js',
   1.318 +      name: 'A',
   1.319 +      original: { line: 2, column: 20 }
   1.320 +    });
   1.321 +    correctMap.addMapping({
   1.322 +      generated: { line: 4, column: 0 }
   1.323 +    });
   1.324 +
   1.325 +    var inputMap = input.map.toJSON();
   1.326 +    correctMap = correctMap.toJSON();
   1.327 +    util.assertEqualMaps(assert, correctMap, inputMap);
   1.328 +  };
   1.329 +
   1.330 +  exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {
   1.331 +    var aNode = new SourceNode(1, 1, 'a.js', 'a');
   1.332 +    aNode.setSourceContent('a.js', 'someContent');
   1.333 +    var node = new SourceNode(null, null, null,
   1.334 +                              ['(function () {\n',
   1.335 +                               '  ', aNode,
   1.336 +                               '  ', new SourceNode(1, 1, 'b.js', 'b'),
   1.337 +                               '}());']);
   1.338 +    node.setSourceContent('b.js', 'otherContent');
   1.339 +    var map = node.toStringWithSourceMap({
   1.340 +      file: 'foo.js'
   1.341 +    }).map;
   1.342 +
   1.343 +    assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
   1.344 +    map = new SourceMapConsumer(map.toString());
   1.345 +
   1.346 +    assert.equal(map.sources.length, 2);
   1.347 +    assert.equal(map.sources[0], 'a.js');
   1.348 +    assert.equal(map.sources[1], 'b.js');
   1.349 +    assert.equal(map.sourcesContent.length, 2);
   1.350 +    assert.equal(map.sourcesContent[0], 'someContent');
   1.351 +    assert.equal(map.sourcesContent[1], 'otherContent');
   1.352 +  };
   1.353 +
   1.354 +  exports['test walkSourceContents'] = function (assert, util) {
   1.355 +    var aNode = new SourceNode(1, 1, 'a.js', 'a');
   1.356 +    aNode.setSourceContent('a.js', 'someContent');
   1.357 +    var node = new SourceNode(null, null, null,
   1.358 +                              ['(function () {\n',
   1.359 +                               '  ', aNode,
   1.360 +                               '  ', new SourceNode(1, 1, 'b.js', 'b'),
   1.361 +                               '}());']);
   1.362 +    node.setSourceContent('b.js', 'otherContent');
   1.363 +    var results = [];
   1.364 +    node.walkSourceContents(function (sourceFile, sourceContent) {
   1.365 +      results.push([sourceFile, sourceContent]);
   1.366 +    });
   1.367 +    assert.equal(results.length, 2);
   1.368 +    assert.equal(results[0][0], 'a.js');
   1.369 +    assert.equal(results[0][1], 'someContent');
   1.370 +    assert.equal(results[1][0], 'b.js');
   1.371 +    assert.equal(results[1][1], 'otherContent');
   1.372 +  };
   1.373 +});
   1.374 +function run_test() {
   1.375 +  runSourceMapTests('test/source-map/test-source-node', do_throw);
   1.376 +}

mercurial