toolkit/devtools/sourcemap/tests/unit/test_source_node.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-node", ["require", "exports", "module"], function (require, exports, module) {
michael@0 16
michael@0 17 var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;
michael@0 18 var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;
michael@0 19 var SourceNode = require('source-map/source-node').SourceNode;
michael@0 20
michael@0 21 exports['test .add()'] = function (assert, util) {
michael@0 22 var node = new SourceNode(null, null, null);
michael@0 23
michael@0 24 // Adding a string works.
michael@0 25 node.add('function noop() {}');
michael@0 26
michael@0 27 // Adding another source node works.
michael@0 28 node.add(new SourceNode(null, null, null));
michael@0 29
michael@0 30 // Adding an array works.
michael@0 31 node.add(['function foo() {',
michael@0 32 new SourceNode(null, null, null,
michael@0 33 'return 10;'),
michael@0 34 '}']);
michael@0 35
michael@0 36 // Adding other stuff doesn't.
michael@0 37 assert.throws(function () {
michael@0 38 node.add({});
michael@0 39 });
michael@0 40 assert.throws(function () {
michael@0 41 node.add(function () {});
michael@0 42 });
michael@0 43 };
michael@0 44
michael@0 45 exports['test .prepend()'] = function (assert, util) {
michael@0 46 var node = new SourceNode(null, null, null);
michael@0 47
michael@0 48 // Prepending a string works.
michael@0 49 node.prepend('function noop() {}');
michael@0 50 assert.equal(node.children[0], 'function noop() {}');
michael@0 51 assert.equal(node.children.length, 1);
michael@0 52
michael@0 53 // Prepending another source node works.
michael@0 54 node.prepend(new SourceNode(null, null, null));
michael@0 55 assert.equal(node.children[0], '');
michael@0 56 assert.equal(node.children[1], 'function noop() {}');
michael@0 57 assert.equal(node.children.length, 2);
michael@0 58
michael@0 59 // Prepending an array works.
michael@0 60 node.prepend(['function foo() {',
michael@0 61 new SourceNode(null, null, null,
michael@0 62 'return 10;'),
michael@0 63 '}']);
michael@0 64 assert.equal(node.children[0], 'function foo() {');
michael@0 65 assert.equal(node.children[1], 'return 10;');
michael@0 66 assert.equal(node.children[2], '}');
michael@0 67 assert.equal(node.children[3], '');
michael@0 68 assert.equal(node.children[4], 'function noop() {}');
michael@0 69 assert.equal(node.children.length, 5);
michael@0 70
michael@0 71 // Prepending other stuff doesn't.
michael@0 72 assert.throws(function () {
michael@0 73 node.prepend({});
michael@0 74 });
michael@0 75 assert.throws(function () {
michael@0 76 node.prepend(function () {});
michael@0 77 });
michael@0 78 };
michael@0 79
michael@0 80 exports['test .toString()'] = function (assert, util) {
michael@0 81 assert.equal((new SourceNode(null, null, null,
michael@0 82 ['function foo() {',
michael@0 83 new SourceNode(null, null, null, 'return 10;'),
michael@0 84 '}'])).toString(),
michael@0 85 'function foo() {return 10;}');
michael@0 86 };
michael@0 87
michael@0 88 exports['test .join()'] = function (assert, util) {
michael@0 89 assert.equal((new SourceNode(null, null, null,
michael@0 90 ['a', 'b', 'c', 'd'])).join(', ').toString(),
michael@0 91 'a, b, c, d');
michael@0 92 };
michael@0 93
michael@0 94 exports['test .walk()'] = function (assert, util) {
michael@0 95 var node = new SourceNode(null, null, null,
michael@0 96 ['(function () {\n',
michael@0 97 ' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n',
michael@0 98 ' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
michael@0 99 '}());']);
michael@0 100 var expected = [
michael@0 101 { str: '(function () {\n', source: null, line: null, column: null },
michael@0 102 { str: ' ', source: null, line: null, column: null },
michael@0 103 { str: 'someCall()', source: 'a.js', line: 1, column: 0 },
michael@0 104 { str: ';\n', source: null, line: null, column: null },
michael@0 105 { str: ' ', source: null, line: null, column: null },
michael@0 106 { str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 },
michael@0 107 { str: ';\n', source: null, line: null, column: null },
michael@0 108 { str: '}());', source: null, line: null, column: null },
michael@0 109 ];
michael@0 110 var i = 0;
michael@0 111 node.walk(function (chunk, loc) {
michael@0 112 assert.equal(expected[i].str, chunk);
michael@0 113 assert.equal(expected[i].source, loc.source);
michael@0 114 assert.equal(expected[i].line, loc.line);
michael@0 115 assert.equal(expected[i].column, loc.column);
michael@0 116 i++;
michael@0 117 });
michael@0 118 };
michael@0 119
michael@0 120 exports['test .replaceRight'] = function (assert, util) {
michael@0 121 var node;
michael@0 122
michael@0 123 // Not nested
michael@0 124 node = new SourceNode(null, null, null, 'hello world');
michael@0 125 node.replaceRight(/world/, 'universe');
michael@0 126 assert.equal(node.toString(), 'hello universe');
michael@0 127
michael@0 128 // Nested
michael@0 129 node = new SourceNode(null, null, null,
michael@0 130 [new SourceNode(null, null, null, 'hey sexy mama, '),
michael@0 131 new SourceNode(null, null, null, 'want to kill all humans?')]);
michael@0 132 node.replaceRight(/kill all humans/, 'watch Futurama');
michael@0 133 assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');
michael@0 134 };
michael@0 135
michael@0 136 exports['test .toStringWithSourceMap()'] = function (assert, util) {
michael@0 137 var node = new SourceNode(null, null, null,
michael@0 138 ['(function () {\n',
michael@0 139 ' ',
michael@0 140 new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),
michael@0 141 new SourceNode(1, 8, 'a.js', '()'),
michael@0 142 ';\n',
michael@0 143 ' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
michael@0 144 '}());']);
michael@0 145 var map = node.toStringWithSourceMap({
michael@0 146 file: 'foo.js'
michael@0 147 }).map;
michael@0 148
michael@0 149 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
michael@0 150 map = new SourceMapConsumer(map.toString());
michael@0 151
michael@0 152 var actual;
michael@0 153
michael@0 154 actual = map.originalPositionFor({
michael@0 155 line: 1,
michael@0 156 column: 4
michael@0 157 });
michael@0 158 assert.equal(actual.source, null);
michael@0 159 assert.equal(actual.line, null);
michael@0 160 assert.equal(actual.column, null);
michael@0 161
michael@0 162 actual = map.originalPositionFor({
michael@0 163 line: 2,
michael@0 164 column: 2
michael@0 165 });
michael@0 166 assert.equal(actual.source, 'a.js');
michael@0 167 assert.equal(actual.line, 1);
michael@0 168 assert.equal(actual.column, 0);
michael@0 169 assert.equal(actual.name, 'originalCall');
michael@0 170
michael@0 171 actual = map.originalPositionFor({
michael@0 172 line: 3,
michael@0 173 column: 2
michael@0 174 });
michael@0 175 assert.equal(actual.source, 'b.js');
michael@0 176 assert.equal(actual.line, 2);
michael@0 177 assert.equal(actual.column, 0);
michael@0 178
michael@0 179 actual = map.originalPositionFor({
michael@0 180 line: 3,
michael@0 181 column: 16
michael@0 182 });
michael@0 183 assert.equal(actual.source, null);
michael@0 184 assert.equal(actual.line, null);
michael@0 185 assert.equal(actual.column, null);
michael@0 186
michael@0 187 actual = map.originalPositionFor({
michael@0 188 line: 4,
michael@0 189 column: 2
michael@0 190 });
michael@0 191 assert.equal(actual.source, null);
michael@0 192 assert.equal(actual.line, null);
michael@0 193 assert.equal(actual.column, null);
michael@0 194 };
michael@0 195
michael@0 196 exports['test .fromStringWithSourceMap()'] = function (assert, util) {
michael@0 197 var node = SourceNode.fromStringWithSourceMap(
michael@0 198 util.testGeneratedCode,
michael@0 199 new SourceMapConsumer(util.testMap));
michael@0 200
michael@0 201 var result = node.toStringWithSourceMap({
michael@0 202 file: 'min.js'
michael@0 203 });
michael@0 204 var map = result.map;
michael@0 205 var code = result.code;
michael@0 206
michael@0 207 assert.equal(code, util.testGeneratedCode);
michael@0 208 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
michael@0 209 map = map.toJSON();
michael@0 210 assert.equal(map.version, util.testMap.version);
michael@0 211 assert.equal(map.file, util.testMap.file);
michael@0 212 assert.equal(map.mappings, util.testMap.mappings);
michael@0 213 };
michael@0 214
michael@0 215 exports['test .fromStringWithSourceMap() empty map'] = function (assert, util) {
michael@0 216 var node = SourceNode.fromStringWithSourceMap(
michael@0 217 util.testGeneratedCode,
michael@0 218 new SourceMapConsumer(util.emptyMap));
michael@0 219 var result = node.toStringWithSourceMap({
michael@0 220 file: 'min.js'
michael@0 221 });
michael@0 222 var map = result.map;
michael@0 223 var code = result.code;
michael@0 224
michael@0 225 assert.equal(code, util.testGeneratedCode);
michael@0 226 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
michael@0 227 map = map.toJSON();
michael@0 228 assert.equal(map.version, util.emptyMap.version);
michael@0 229 assert.equal(map.file, util.emptyMap.file);
michael@0 230 assert.equal(map.mappings.length, util.emptyMap.mappings.length);
michael@0 231 assert.equal(map.mappings, util.emptyMap.mappings);
michael@0 232 };
michael@0 233
michael@0 234 exports['test .fromStringWithSourceMap() complex version'] = function (assert, util) {
michael@0 235 var input = new SourceNode(null, null, null, [
michael@0 236 "(function() {\n",
michael@0 237 " var Test = {};\n",
michael@0 238 " ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };\n"),
michael@0 239 " ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), "\n",
michael@0 240 "}());\n",
michael@0 241 "/* Generated Source */"]);
michael@0 242 input = input.toStringWithSourceMap({
michael@0 243 file: 'foo.js'
michael@0 244 });
michael@0 245
michael@0 246 var node = SourceNode.fromStringWithSourceMap(
michael@0 247 input.code,
michael@0 248 new SourceMapConsumer(input.map.toString()));
michael@0 249
michael@0 250 var result = node.toStringWithSourceMap({
michael@0 251 file: 'foo.js'
michael@0 252 });
michael@0 253 var map = result.map;
michael@0 254 var code = result.code;
michael@0 255
michael@0 256 assert.equal(code, input.code);
michael@0 257 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
michael@0 258 map = map.toJSON();
michael@0 259 var inputMap = input.map.toJSON();
michael@0 260 util.assertEqualMaps(assert, map, inputMap);
michael@0 261 };
michael@0 262
michael@0 263 exports['test .fromStringWithSourceMap() merging duplicate mappings'] = function (assert, util) {
michael@0 264 var input = new SourceNode(null, null, null, [
michael@0 265 new SourceNode(1, 0, "a.js", "(function"),
michael@0 266 new SourceNode(1, 0, "a.js", "() {\n"),
michael@0 267 " ",
michael@0 268 new SourceNode(1, 0, "a.js", "var Test = "),
michael@0 269 new SourceNode(1, 0, "b.js", "{};\n"),
michael@0 270 new SourceNode(2, 0, "b.js", "Test"),
michael@0 271 new SourceNode(2, 0, "b.js", ".A", "A"),
michael@0 272 new SourceNode(2, 20, "b.js", " = { value: 1234 };\n", "A"),
michael@0 273 "}());\n",
michael@0 274 "/* Generated Source */"
michael@0 275 ]);
michael@0 276 input = input.toStringWithSourceMap({
michael@0 277 file: 'foo.js'
michael@0 278 });
michael@0 279
michael@0 280 var correctMap = new SourceMapGenerator({
michael@0 281 file: 'foo.js'
michael@0 282 });
michael@0 283 correctMap.addMapping({
michael@0 284 generated: { line: 1, column: 0 },
michael@0 285 source: 'a.js',
michael@0 286 original: { line: 1, column: 0 }
michael@0 287 });
michael@0 288 correctMap.addMapping({
michael@0 289 generated: { line: 2, column: 0 }
michael@0 290 });
michael@0 291 correctMap.addMapping({
michael@0 292 generated: { line: 2, column: 2 },
michael@0 293 source: 'a.js',
michael@0 294 original: { line: 1, column: 0 }
michael@0 295 });
michael@0 296 correctMap.addMapping({
michael@0 297 generated: { line: 2, column: 13 },
michael@0 298 source: 'b.js',
michael@0 299 original: { line: 1, column: 0 }
michael@0 300 });
michael@0 301 correctMap.addMapping({
michael@0 302 generated: { line: 3, column: 0 },
michael@0 303 source: 'b.js',
michael@0 304 original: { line: 2, column: 0 }
michael@0 305 });
michael@0 306 correctMap.addMapping({
michael@0 307 generated: { line: 3, column: 4 },
michael@0 308 source: 'b.js',
michael@0 309 name: 'A',
michael@0 310 original: { line: 2, column: 0 }
michael@0 311 });
michael@0 312 correctMap.addMapping({
michael@0 313 generated: { line: 3, column: 6 },
michael@0 314 source: 'b.js',
michael@0 315 name: 'A',
michael@0 316 original: { line: 2, column: 20 }
michael@0 317 });
michael@0 318 correctMap.addMapping({
michael@0 319 generated: { line: 4, column: 0 }
michael@0 320 });
michael@0 321
michael@0 322 var inputMap = input.map.toJSON();
michael@0 323 correctMap = correctMap.toJSON();
michael@0 324 util.assertEqualMaps(assert, correctMap, inputMap);
michael@0 325 };
michael@0 326
michael@0 327 exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {
michael@0 328 var aNode = new SourceNode(1, 1, 'a.js', 'a');
michael@0 329 aNode.setSourceContent('a.js', 'someContent');
michael@0 330 var node = new SourceNode(null, null, null,
michael@0 331 ['(function () {\n',
michael@0 332 ' ', aNode,
michael@0 333 ' ', new SourceNode(1, 1, 'b.js', 'b'),
michael@0 334 '}());']);
michael@0 335 node.setSourceContent('b.js', 'otherContent');
michael@0 336 var map = node.toStringWithSourceMap({
michael@0 337 file: 'foo.js'
michael@0 338 }).map;
michael@0 339
michael@0 340 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
michael@0 341 map = new SourceMapConsumer(map.toString());
michael@0 342
michael@0 343 assert.equal(map.sources.length, 2);
michael@0 344 assert.equal(map.sources[0], 'a.js');
michael@0 345 assert.equal(map.sources[1], 'b.js');
michael@0 346 assert.equal(map.sourcesContent.length, 2);
michael@0 347 assert.equal(map.sourcesContent[0], 'someContent');
michael@0 348 assert.equal(map.sourcesContent[1], 'otherContent');
michael@0 349 };
michael@0 350
michael@0 351 exports['test walkSourceContents'] = function (assert, util) {
michael@0 352 var aNode = new SourceNode(1, 1, 'a.js', 'a');
michael@0 353 aNode.setSourceContent('a.js', 'someContent');
michael@0 354 var node = new SourceNode(null, null, null,
michael@0 355 ['(function () {\n',
michael@0 356 ' ', aNode,
michael@0 357 ' ', new SourceNode(1, 1, 'b.js', 'b'),
michael@0 358 '}());']);
michael@0 359 node.setSourceContent('b.js', 'otherContent');
michael@0 360 var results = [];
michael@0 361 node.walkSourceContents(function (sourceFile, sourceContent) {
michael@0 362 results.push([sourceFile, sourceContent]);
michael@0 363 });
michael@0 364 assert.equal(results.length, 2);
michael@0 365 assert.equal(results[0][0], 'a.js');
michael@0 366 assert.equal(results[0][1], 'someContent');
michael@0 367 assert.equal(results[1][0], 'b.js');
michael@0 368 assert.equal(results[1][1], 'otherContent');
michael@0 369 };
michael@0 370 });
michael@0 371 function run_test() {
michael@0 372 runSourceMapTests('test/source-map/test-source-node', do_throw);
michael@0 373 }

mercurial