michael@0: /* michael@0: * WARNING! michael@0: * michael@0: * Do not edit this file directly, it is built from the sources at michael@0: * https://github.com/mozilla/source-map/ michael@0: */ michael@0: michael@0: Components.utils.import('resource://test/Utils.jsm'); michael@0: /* -*- Mode: js; js-indent-level: 2; -*- */ michael@0: /* michael@0: * Copyright 2011 Mozilla Foundation and contributors michael@0: * Licensed under the New BSD license. See LICENSE or: michael@0: * http://opensource.org/licenses/BSD-3-Clause michael@0: */ michael@0: define("test/source-map/test-source-node", ["require", "exports", "module"], function (require, exports, module) { michael@0: michael@0: var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator; michael@0: var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer; michael@0: var SourceNode = require('source-map/source-node').SourceNode; michael@0: michael@0: exports['test .add()'] = function (assert, util) { michael@0: var node = new SourceNode(null, null, null); michael@0: michael@0: // Adding a string works. michael@0: node.add('function noop() {}'); michael@0: michael@0: // Adding another source node works. michael@0: node.add(new SourceNode(null, null, null)); michael@0: michael@0: // Adding an array works. michael@0: node.add(['function foo() {', michael@0: new SourceNode(null, null, null, michael@0: 'return 10;'), michael@0: '}']); michael@0: michael@0: // Adding other stuff doesn't. michael@0: assert.throws(function () { michael@0: node.add({}); michael@0: }); michael@0: assert.throws(function () { michael@0: node.add(function () {}); michael@0: }); michael@0: }; michael@0: michael@0: exports['test .prepend()'] = function (assert, util) { michael@0: var node = new SourceNode(null, null, null); michael@0: michael@0: // Prepending a string works. michael@0: node.prepend('function noop() {}'); michael@0: assert.equal(node.children[0], 'function noop() {}'); michael@0: assert.equal(node.children.length, 1); michael@0: michael@0: // Prepending another source node works. michael@0: node.prepend(new SourceNode(null, null, null)); michael@0: assert.equal(node.children[0], ''); michael@0: assert.equal(node.children[1], 'function noop() {}'); michael@0: assert.equal(node.children.length, 2); michael@0: michael@0: // Prepending an array works. michael@0: node.prepend(['function foo() {', michael@0: new SourceNode(null, null, null, michael@0: 'return 10;'), michael@0: '}']); michael@0: assert.equal(node.children[0], 'function foo() {'); michael@0: assert.equal(node.children[1], 'return 10;'); michael@0: assert.equal(node.children[2], '}'); michael@0: assert.equal(node.children[3], ''); michael@0: assert.equal(node.children[4], 'function noop() {}'); michael@0: assert.equal(node.children.length, 5); michael@0: michael@0: // Prepending other stuff doesn't. michael@0: assert.throws(function () { michael@0: node.prepend({}); michael@0: }); michael@0: assert.throws(function () { michael@0: node.prepend(function () {}); michael@0: }); michael@0: }; michael@0: michael@0: exports['test .toString()'] = function (assert, util) { michael@0: assert.equal((new SourceNode(null, null, null, michael@0: ['function foo() {', michael@0: new SourceNode(null, null, null, 'return 10;'), michael@0: '}'])).toString(), michael@0: 'function foo() {return 10;}'); michael@0: }; michael@0: michael@0: exports['test .join()'] = function (assert, util) { michael@0: assert.equal((new SourceNode(null, null, null, michael@0: ['a', 'b', 'c', 'd'])).join(', ').toString(), michael@0: 'a, b, c, d'); michael@0: }; michael@0: michael@0: exports['test .walk()'] = function (assert, util) { michael@0: var node = new SourceNode(null, null, null, michael@0: ['(function () {\n', michael@0: ' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n', michael@0: ' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n', michael@0: '}());']); michael@0: var expected = [ michael@0: { str: '(function () {\n', source: null, line: null, column: null }, michael@0: { str: ' ', source: null, line: null, column: null }, michael@0: { str: 'someCall()', source: 'a.js', line: 1, column: 0 }, michael@0: { str: ';\n', source: null, line: null, column: null }, michael@0: { str: ' ', source: null, line: null, column: null }, michael@0: { str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 }, michael@0: { str: ';\n', source: null, line: null, column: null }, michael@0: { str: '}());', source: null, line: null, column: null }, michael@0: ]; michael@0: var i = 0; michael@0: node.walk(function (chunk, loc) { michael@0: assert.equal(expected[i].str, chunk); michael@0: assert.equal(expected[i].source, loc.source); michael@0: assert.equal(expected[i].line, loc.line); michael@0: assert.equal(expected[i].column, loc.column); michael@0: i++; michael@0: }); michael@0: }; michael@0: michael@0: exports['test .replaceRight'] = function (assert, util) { michael@0: var node; michael@0: michael@0: // Not nested michael@0: node = new SourceNode(null, null, null, 'hello world'); michael@0: node.replaceRight(/world/, 'universe'); michael@0: assert.equal(node.toString(), 'hello universe'); michael@0: michael@0: // Nested michael@0: node = new SourceNode(null, null, null, michael@0: [new SourceNode(null, null, null, 'hey sexy mama, '), michael@0: new SourceNode(null, null, null, 'want to kill all humans?')]); michael@0: node.replaceRight(/kill all humans/, 'watch Futurama'); michael@0: assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?'); michael@0: }; michael@0: michael@0: exports['test .toStringWithSourceMap()'] = function (assert, util) { michael@0: var node = new SourceNode(null, null, null, michael@0: ['(function () {\n', michael@0: ' ', michael@0: new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'), michael@0: new SourceNode(1, 8, 'a.js', '()'), michael@0: ';\n', michael@0: ' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n', michael@0: '}());']); michael@0: var map = node.toStringWithSourceMap({ michael@0: file: 'foo.js' michael@0: }).map; michael@0: michael@0: assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); michael@0: map = new SourceMapConsumer(map.toString()); michael@0: michael@0: var actual; michael@0: michael@0: actual = map.originalPositionFor({ michael@0: line: 1, michael@0: column: 4 michael@0: }); michael@0: assert.equal(actual.source, null); michael@0: assert.equal(actual.line, null); michael@0: assert.equal(actual.column, null); michael@0: michael@0: actual = map.originalPositionFor({ michael@0: line: 2, michael@0: column: 2 michael@0: }); michael@0: assert.equal(actual.source, 'a.js'); michael@0: assert.equal(actual.line, 1); michael@0: assert.equal(actual.column, 0); michael@0: assert.equal(actual.name, 'originalCall'); michael@0: michael@0: actual = map.originalPositionFor({ michael@0: line: 3, michael@0: column: 2 michael@0: }); michael@0: assert.equal(actual.source, 'b.js'); michael@0: assert.equal(actual.line, 2); michael@0: assert.equal(actual.column, 0); michael@0: michael@0: actual = map.originalPositionFor({ michael@0: line: 3, michael@0: column: 16 michael@0: }); michael@0: assert.equal(actual.source, null); michael@0: assert.equal(actual.line, null); michael@0: assert.equal(actual.column, null); michael@0: michael@0: actual = map.originalPositionFor({ michael@0: line: 4, michael@0: column: 2 michael@0: }); michael@0: assert.equal(actual.source, null); michael@0: assert.equal(actual.line, null); michael@0: assert.equal(actual.column, null); michael@0: }; michael@0: michael@0: exports['test .fromStringWithSourceMap()'] = function (assert, util) { michael@0: var node = SourceNode.fromStringWithSourceMap( michael@0: util.testGeneratedCode, michael@0: new SourceMapConsumer(util.testMap)); michael@0: michael@0: var result = node.toStringWithSourceMap({ michael@0: file: 'min.js' michael@0: }); michael@0: var map = result.map; michael@0: var code = result.code; michael@0: michael@0: assert.equal(code, util.testGeneratedCode); michael@0: assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); michael@0: map = map.toJSON(); michael@0: assert.equal(map.version, util.testMap.version); michael@0: assert.equal(map.file, util.testMap.file); michael@0: assert.equal(map.mappings, util.testMap.mappings); michael@0: }; michael@0: michael@0: exports['test .fromStringWithSourceMap() empty map'] = function (assert, util) { michael@0: var node = SourceNode.fromStringWithSourceMap( michael@0: util.testGeneratedCode, michael@0: new SourceMapConsumer(util.emptyMap)); michael@0: var result = node.toStringWithSourceMap({ michael@0: file: 'min.js' michael@0: }); michael@0: var map = result.map; michael@0: var code = result.code; michael@0: michael@0: assert.equal(code, util.testGeneratedCode); michael@0: assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); michael@0: map = map.toJSON(); michael@0: assert.equal(map.version, util.emptyMap.version); michael@0: assert.equal(map.file, util.emptyMap.file); michael@0: assert.equal(map.mappings.length, util.emptyMap.mappings.length); michael@0: assert.equal(map.mappings, util.emptyMap.mappings); michael@0: }; michael@0: michael@0: exports['test .fromStringWithSourceMap() complex version'] = function (assert, util) { michael@0: var input = new SourceNode(null, null, null, [ michael@0: "(function() {\n", michael@0: " var Test = {};\n", michael@0: " ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };\n"), michael@0: " ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), "\n", michael@0: "}());\n", michael@0: "/* Generated Source */"]); michael@0: input = input.toStringWithSourceMap({ michael@0: file: 'foo.js' michael@0: }); michael@0: michael@0: var node = SourceNode.fromStringWithSourceMap( michael@0: input.code, michael@0: new SourceMapConsumer(input.map.toString())); michael@0: michael@0: var result = node.toStringWithSourceMap({ michael@0: file: 'foo.js' michael@0: }); michael@0: var map = result.map; michael@0: var code = result.code; michael@0: michael@0: assert.equal(code, input.code); michael@0: assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); michael@0: map = map.toJSON(); michael@0: var inputMap = input.map.toJSON(); michael@0: util.assertEqualMaps(assert, map, inputMap); michael@0: }; michael@0: michael@0: exports['test .fromStringWithSourceMap() merging duplicate mappings'] = function (assert, util) { michael@0: var input = new SourceNode(null, null, null, [ michael@0: new SourceNode(1, 0, "a.js", "(function"), michael@0: new SourceNode(1, 0, "a.js", "() {\n"), michael@0: " ", michael@0: new SourceNode(1, 0, "a.js", "var Test = "), michael@0: new SourceNode(1, 0, "b.js", "{};\n"), michael@0: new SourceNode(2, 0, "b.js", "Test"), michael@0: new SourceNode(2, 0, "b.js", ".A", "A"), michael@0: new SourceNode(2, 20, "b.js", " = { value: 1234 };\n", "A"), michael@0: "}());\n", michael@0: "/* Generated Source */" michael@0: ]); michael@0: input = input.toStringWithSourceMap({ michael@0: file: 'foo.js' michael@0: }); michael@0: michael@0: var correctMap = new SourceMapGenerator({ michael@0: file: 'foo.js' michael@0: }); michael@0: correctMap.addMapping({ michael@0: generated: { line: 1, column: 0 }, michael@0: source: 'a.js', michael@0: original: { line: 1, column: 0 } michael@0: }); michael@0: correctMap.addMapping({ michael@0: generated: { line: 2, column: 0 } michael@0: }); michael@0: correctMap.addMapping({ michael@0: generated: { line: 2, column: 2 }, michael@0: source: 'a.js', michael@0: original: { line: 1, column: 0 } michael@0: }); michael@0: correctMap.addMapping({ michael@0: generated: { line: 2, column: 13 }, michael@0: source: 'b.js', michael@0: original: { line: 1, column: 0 } michael@0: }); michael@0: correctMap.addMapping({ michael@0: generated: { line: 3, column: 0 }, michael@0: source: 'b.js', michael@0: original: { line: 2, column: 0 } michael@0: }); michael@0: correctMap.addMapping({ michael@0: generated: { line: 3, column: 4 }, michael@0: source: 'b.js', michael@0: name: 'A', michael@0: original: { line: 2, column: 0 } michael@0: }); michael@0: correctMap.addMapping({ michael@0: generated: { line: 3, column: 6 }, michael@0: source: 'b.js', michael@0: name: 'A', michael@0: original: { line: 2, column: 20 } michael@0: }); michael@0: correctMap.addMapping({ michael@0: generated: { line: 4, column: 0 } michael@0: }); michael@0: michael@0: var inputMap = input.map.toJSON(); michael@0: correctMap = correctMap.toJSON(); michael@0: util.assertEqualMaps(assert, correctMap, inputMap); michael@0: }; michael@0: michael@0: exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) { michael@0: var aNode = new SourceNode(1, 1, 'a.js', 'a'); michael@0: aNode.setSourceContent('a.js', 'someContent'); michael@0: var node = new SourceNode(null, null, null, michael@0: ['(function () {\n', michael@0: ' ', aNode, michael@0: ' ', new SourceNode(1, 1, 'b.js', 'b'), michael@0: '}());']); michael@0: node.setSourceContent('b.js', 'otherContent'); michael@0: var map = node.toStringWithSourceMap({ michael@0: file: 'foo.js' michael@0: }).map; michael@0: michael@0: assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); michael@0: map = new SourceMapConsumer(map.toString()); michael@0: michael@0: assert.equal(map.sources.length, 2); michael@0: assert.equal(map.sources[0], 'a.js'); michael@0: assert.equal(map.sources[1], 'b.js'); michael@0: assert.equal(map.sourcesContent.length, 2); michael@0: assert.equal(map.sourcesContent[0], 'someContent'); michael@0: assert.equal(map.sourcesContent[1], 'otherContent'); michael@0: }; michael@0: michael@0: exports['test walkSourceContents'] = function (assert, util) { michael@0: var aNode = new SourceNode(1, 1, 'a.js', 'a'); michael@0: aNode.setSourceContent('a.js', 'someContent'); michael@0: var node = new SourceNode(null, null, null, michael@0: ['(function () {\n', michael@0: ' ', aNode, michael@0: ' ', new SourceNode(1, 1, 'b.js', 'b'), michael@0: '}());']); michael@0: node.setSourceContent('b.js', 'otherContent'); michael@0: var results = []; michael@0: node.walkSourceContents(function (sourceFile, sourceContent) { michael@0: results.push([sourceFile, sourceContent]); michael@0: }); michael@0: assert.equal(results.length, 2); michael@0: assert.equal(results[0][0], 'a.js'); michael@0: assert.equal(results[0][1], 'someContent'); michael@0: assert.equal(results[1][0], 'b.js'); michael@0: assert.equal(results[1][1], 'otherContent'); michael@0: }; michael@0: }); michael@0: function run_test() { michael@0: runSourceMapTests('test/source-map/test-source-node', do_throw); michael@0: }