react / wstein / node_modules / browserify / node_modules / browser-pack / node_modules / combine-source-map / node_modules / source-map / dist / test / test_source_node.js
80559 views/*1* WARNING!2*3* Do not edit this file directly, it is built from the sources at4* https://github.com/mozilla/source-map/5*/67Components.utils.import('resource://test/Utils.jsm');8/* -*- Mode: js; js-indent-level: 2; -*- */9/*10* Copyright 2011 Mozilla Foundation and contributors11* Licensed under the New BSD license. See LICENSE or:12* http://opensource.org/licenses/BSD-3-Clause13*/14define("test/source-map/test-source-node", ["require", "exports", "module"], function (require, exports, module) {1516var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;17var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;18var SourceNode = require('source-map/source-node').SourceNode;1920function forEachNewline(fn) {21return function (assert, util) {22['\n', '\r\n'].forEach(fn.bind(null, assert, util));23}24}2526exports['test .add()'] = function (assert, util) {27var node = new SourceNode(null, null, null);2829// Adding a string works.30node.add('function noop() {}');3132// Adding another source node works.33node.add(new SourceNode(null, null, null));3435// Adding an array works.36node.add(['function foo() {',37new SourceNode(null, null, null,38'return 10;'),39'}']);4041// Adding other stuff doesn't.42assert.throws(function () {43node.add({});44});45assert.throws(function () {46node.add(function () {});47});48};4950exports['test .prepend()'] = function (assert, util) {51var node = new SourceNode(null, null, null);5253// Prepending a string works.54node.prepend('function noop() {}');55assert.equal(node.children[0], 'function noop() {}');56assert.equal(node.children.length, 1);5758// Prepending another source node works.59node.prepend(new SourceNode(null, null, null));60assert.equal(node.children[0], '');61assert.equal(node.children[1], 'function noop() {}');62assert.equal(node.children.length, 2);6364// Prepending an array works.65node.prepend(['function foo() {',66new SourceNode(null, null, null,67'return 10;'),68'}']);69assert.equal(node.children[0], 'function foo() {');70assert.equal(node.children[1], 'return 10;');71assert.equal(node.children[2], '}');72assert.equal(node.children[3], '');73assert.equal(node.children[4], 'function noop() {}');74assert.equal(node.children.length, 5);7576// Prepending other stuff doesn't.77assert.throws(function () {78node.prepend({});79});80assert.throws(function () {81node.prepend(function () {});82});83};8485exports['test .toString()'] = function (assert, util) {86assert.equal((new SourceNode(null, null, null,87['function foo() {',88new SourceNode(null, null, null, 'return 10;'),89'}'])).toString(),90'function foo() {return 10;}');91};9293exports['test .join()'] = function (assert, util) {94assert.equal((new SourceNode(null, null, null,95['a', 'b', 'c', 'd'])).join(', ').toString(),96'a, b, c, d');97};9899exports['test .walk()'] = function (assert, util) {100var node = new SourceNode(null, null, null,101['(function () {\n',102' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n',103' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',104'}());']);105var expected = [106{ str: '(function () {\n', source: null, line: null, column: null },107{ str: ' ', source: null, line: null, column: null },108{ str: 'someCall()', source: 'a.js', line: 1, column: 0 },109{ str: ';\n', source: null, line: null, column: null },110{ str: ' ', source: null, line: null, column: null },111{ str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 },112{ str: ';\n', source: null, line: null, column: null },113{ str: '}());', source: null, line: null, column: null },114];115var i = 0;116node.walk(function (chunk, loc) {117assert.equal(expected[i].str, chunk);118assert.equal(expected[i].source, loc.source);119assert.equal(expected[i].line, loc.line);120assert.equal(expected[i].column, loc.column);121i++;122});123};124125exports['test .replaceRight'] = function (assert, util) {126var node;127128// Not nested129node = new SourceNode(null, null, null, 'hello world');130node.replaceRight(/world/, 'universe');131assert.equal(node.toString(), 'hello universe');132133// Nested134node = new SourceNode(null, null, null,135[new SourceNode(null, null, null, 'hey sexy mama, '),136new SourceNode(null, null, null, 'want to kill all humans?')]);137node.replaceRight(/kill all humans/, 'watch Futurama');138assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');139};140141exports['test .toStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {142var node = new SourceNode(null, null, null,143['(function () {' + nl,144' ',145new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),146new SourceNode(1, 8, 'a.js', '()'),147';' + nl,148' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';' + nl,149'}());']);150var result = node.toStringWithSourceMap({151file: 'foo.js'152});153154assert.equal(result.code, [155'(function () {',156' someCall();',157' if (foo) bar();',158'}());'159].join(nl));160161var map = result.map;162var mapWithoutOptions = node.toStringWithSourceMap().map;163164assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');165assert.ok(mapWithoutOptions instanceof SourceMapGenerator, 'mapWithoutOptions instanceof SourceMapGenerator');166assert.ok(!('file' in mapWithoutOptions));167mapWithoutOptions._file = 'foo.js';168util.assertEqualMaps(assert, map.toJSON(), mapWithoutOptions.toJSON());169170map = new SourceMapConsumer(map.toString());171172var actual;173174actual = map.originalPositionFor({175line: 1,176column: 4177});178assert.equal(actual.source, null);179assert.equal(actual.line, null);180assert.equal(actual.column, null);181182actual = map.originalPositionFor({183line: 2,184column: 2185});186assert.equal(actual.source, 'a.js');187assert.equal(actual.line, 1);188assert.equal(actual.column, 0);189assert.equal(actual.name, 'originalCall');190191actual = map.originalPositionFor({192line: 3,193column: 2194});195assert.equal(actual.source, 'b.js');196assert.equal(actual.line, 2);197assert.equal(actual.column, 0);198199actual = map.originalPositionFor({200line: 3,201column: 16202});203assert.equal(actual.source, null);204assert.equal(actual.line, null);205assert.equal(actual.column, null);206207actual = map.originalPositionFor({208line: 4,209column: 2210});211assert.equal(actual.source, null);212assert.equal(actual.line, null);213assert.equal(actual.column, null);214});215216exports['test .fromStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {217var testCode = util.testGeneratedCode.replace(/\n/g, nl);218var node = SourceNode.fromStringWithSourceMap(219testCode,220new SourceMapConsumer(util.testMap));221222var result = node.toStringWithSourceMap({223file: 'min.js'224});225var map = result.map;226var code = result.code;227228assert.equal(code, testCode);229assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');230map = map.toJSON();231assert.equal(map.version, util.testMap.version);232assert.equal(map.file, util.testMap.file);233assert.equal(map.mappings, util.testMap.mappings);234});235236exports['test .fromStringWithSourceMap() empty map'] = forEachNewline(function (assert, util, nl) {237var node = SourceNode.fromStringWithSourceMap(238util.testGeneratedCode.replace(/\n/g, nl),239new SourceMapConsumer(util.emptyMap));240var result = node.toStringWithSourceMap({241file: 'min.js'242});243var map = result.map;244var code = result.code;245246assert.equal(code, util.testGeneratedCode.replace(/\n/g, nl));247assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');248map = map.toJSON();249assert.equal(map.version, util.emptyMap.version);250assert.equal(map.file, util.emptyMap.file);251assert.equal(map.mappings.length, util.emptyMap.mappings.length);252assert.equal(map.mappings, util.emptyMap.mappings);253});254255exports['test .fromStringWithSourceMap() complex version'] = forEachNewline(function (assert, util, nl) {256var input = new SourceNode(null, null, null, [257"(function() {" + nl,258" var Test = {};" + nl,259" ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };" + nl),260" ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), nl,261"}());" + nl,262"/* Generated Source */"]);263input = input.toStringWithSourceMap({264file: 'foo.js'265});266267var node = SourceNode.fromStringWithSourceMap(268input.code,269new SourceMapConsumer(input.map.toString()));270271var result = node.toStringWithSourceMap({272file: 'foo.js'273});274var map = result.map;275var code = result.code;276277assert.equal(code, input.code);278assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');279map = map.toJSON();280var inputMap = input.map.toJSON();281util.assertEqualMaps(assert, map, inputMap);282});283284exports['test .fromStringWithSourceMap() third argument'] = function (assert, util) {285// Assume the following directory structure:286//287// http://foo.org/288// bar.coffee289// app/290// coffee/291// foo.coffee292// coffeeBundle.js # Made from {foo,bar,baz}.coffee293// maps/294// coffeeBundle.js.map295// js/296// foo.js297// public/298// app.js # Made from {foo,coffeeBundle}.js299// app.js.map300//301// http://www.example.com/302// baz.coffee303304var coffeeBundle = new SourceNode(1, 0, 'foo.coffee', 'foo(coffee);\n');305coffeeBundle.setSourceContent('foo.coffee', 'foo coffee');306coffeeBundle.add(new SourceNode(2, 0, '/bar.coffee', 'bar(coffee);\n'));307coffeeBundle.add(new SourceNode(3, 0, 'http://www.example.com/baz.coffee', 'baz(coffee);'));308coffeeBundle = coffeeBundle.toStringWithSourceMap({309file: 'foo.js',310sourceRoot: '..'311});312313var foo = new SourceNode(1, 0, 'foo.js', 'foo(js);');314315var test = function(relativePath, expectedSources) {316var app = new SourceNode();317app.add(SourceNode.fromStringWithSourceMap(318coffeeBundle.code,319new SourceMapConsumer(coffeeBundle.map.toString()),320relativePath));321app.add(foo);322var i = 0;323app.walk(function (chunk, loc) {324assert.equal(loc.source, expectedSources[i]);325i++;326});327app.walkSourceContents(function (sourceFile, sourceContent) {328assert.equal(sourceFile, expectedSources[0]);329assert.equal(sourceContent, 'foo coffee');330})331};332333test('../coffee/maps', [334'../coffee/foo.coffee',335'/bar.coffee',336'http://www.example.com/baz.coffee',337'foo.js'338]);339340// If the third parameter is omitted or set to the current working341// directory we get incorrect source paths:342343test(undefined, [344'../foo.coffee',345'/bar.coffee',346'http://www.example.com/baz.coffee',347'foo.js'348]);349350test('', [351'../foo.coffee',352'/bar.coffee',353'http://www.example.com/baz.coffee',354'foo.js'355]);356357test('.', [358'../foo.coffee',359'/bar.coffee',360'http://www.example.com/baz.coffee',361'foo.js'362]);363364test('./', [365'../foo.coffee',366'/bar.coffee',367'http://www.example.com/baz.coffee',368'foo.js'369]);370};371372exports['test .toStringWithSourceMap() merging duplicate mappings'] = forEachNewline(function (assert, util, nl) {373var input = new SourceNode(null, null, null, [374new SourceNode(1, 0, "a.js", "(function"),375new SourceNode(1, 0, "a.js", "() {" + nl),376" ",377new SourceNode(1, 0, "a.js", "var Test = "),378new SourceNode(1, 0, "b.js", "{};" + nl),379new SourceNode(2, 0, "b.js", "Test"),380new SourceNode(2, 0, "b.js", ".A", "A"),381new SourceNode(2, 20, "b.js", " = { value: ", "A"),382"1234",383new SourceNode(2, 40, "b.js", " };" + nl, "A"),384"}());" + nl,385"/* Generated Source */"386]);387input = input.toStringWithSourceMap({388file: 'foo.js'389});390391assert.equal(input.code, [392"(function() {",393" var Test = {};",394"Test.A = { value: 1234 };",395"}());",396"/* Generated Source */"397].join(nl))398399var correctMap = new SourceMapGenerator({400file: 'foo.js'401});402correctMap.addMapping({403generated: { line: 1, column: 0 },404source: 'a.js',405original: { line: 1, column: 0 }406});407// Here is no need for a empty mapping,408// because mappings ends at eol409correctMap.addMapping({410generated: { line: 2, column: 2 },411source: 'a.js',412original: { line: 1, column: 0 }413});414correctMap.addMapping({415generated: { line: 2, column: 13 },416source: 'b.js',417original: { line: 1, column: 0 }418});419correctMap.addMapping({420generated: { line: 3, column: 0 },421source: 'b.js',422original: { line: 2, column: 0 }423});424correctMap.addMapping({425generated: { line: 3, column: 4 },426source: 'b.js',427name: 'A',428original: { line: 2, column: 0 }429});430correctMap.addMapping({431generated: { line: 3, column: 6 },432source: 'b.js',433name: 'A',434original: { line: 2, column: 20 }435});436// This empty mapping is required,437// because there is a hole in the middle of the line438correctMap.addMapping({439generated: { line: 3, column: 18 }440});441correctMap.addMapping({442generated: { line: 3, column: 22 },443source: 'b.js',444name: 'A',445original: { line: 2, column: 40 }446});447// Here is no need for a empty mapping,448// because mappings ends at eol449450var inputMap = input.map.toJSON();451correctMap = correctMap.toJSON();452util.assertEqualMaps(assert, inputMap, correctMap);453});454455exports['test .toStringWithSourceMap() multi-line SourceNodes'] = forEachNewline(function (assert, util, nl) {456var input = new SourceNode(null, null, null, [457new SourceNode(1, 0, "a.js", "(function() {" + nl + "var nextLine = 1;" + nl + "anotherLine();" + nl),458new SourceNode(2, 2, "b.js", "Test.call(this, 123);" + nl),459new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';" + nl),460new SourceNode(2, 2, "b.js", "anotherLine();" + nl),461"/*" + nl + "Generated" + nl + "Source" + nl + "*/" + nl,462new SourceNode(3, 4, "c.js", "anotherLine();" + nl),463"/*" + nl + "Generated" + nl + "Source" + nl + "*/"464]);465input = input.toStringWithSourceMap({466file: 'foo.js'467});468469assert.equal(input.code, [470"(function() {",471"var nextLine = 1;",472"anotherLine();",473"Test.call(this, 123);",474"this['stuff'] = 'v';",475"anotherLine();",476"/*",477"Generated",478"Source",479"*/",480"anotherLine();",481"/*",482"Generated",483"Source",484"*/"485].join(nl));486487var correctMap = new SourceMapGenerator({488file: 'foo.js'489});490correctMap.addMapping({491generated: { line: 1, column: 0 },492source: 'a.js',493original: { line: 1, column: 0 }494});495correctMap.addMapping({496generated: { line: 2, column: 0 },497source: 'a.js',498original: { line: 1, column: 0 }499});500correctMap.addMapping({501generated: { line: 3, column: 0 },502source: 'a.js',503original: { line: 1, column: 0 }504});505correctMap.addMapping({506generated: { line: 4, column: 0 },507source: 'b.js',508original: { line: 2, column: 2 }509});510correctMap.addMapping({511generated: { line: 5, column: 0 },512source: 'b.js',513original: { line: 2, column: 2 }514});515correctMap.addMapping({516generated: { line: 6, column: 0 },517source: 'b.js',518original: { line: 2, column: 2 }519});520correctMap.addMapping({521generated: { line: 11, column: 0 },522source: 'c.js',523original: { line: 3, column: 4 }524});525526var inputMap = input.map.toJSON();527correctMap = correctMap.toJSON();528util.assertEqualMaps(assert, inputMap, correctMap);529});530531exports['test .toStringWithSourceMap() with empty string'] = function (assert, util) {532var node = new SourceNode(1, 0, 'empty.js', '');533var result = node.toStringWithSourceMap();534assert.equal(result.code, '');535};536537exports['test .toStringWithSourceMap() with consecutive newlines'] = forEachNewline(function (assert, util, nl) {538var input = new SourceNode(null, null, null, [539"/***/" + nl + nl,540new SourceNode(1, 0, "a.js", "'use strict';" + nl),541new SourceNode(2, 0, "a.js", "a();"),542]);543input = input.toStringWithSourceMap({544file: 'foo.js'545});546547assert.equal(input.code, [548"/***/",549"",550"'use strict';",551"a();",552].join(nl));553554var correctMap = new SourceMapGenerator({555file: 'foo.js'556});557correctMap.addMapping({558generated: { line: 3, column: 0 },559source: 'a.js',560original: { line: 1, column: 0 }561});562correctMap.addMapping({563generated: { line: 4, column: 0 },564source: 'a.js',565original: { line: 2, column: 0 }566});567568var inputMap = input.map.toJSON();569correctMap = correctMap.toJSON();570util.assertEqualMaps(assert, inputMap, correctMap);571});572573exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {574var aNode = new SourceNode(1, 1, 'a.js', 'a');575aNode.setSourceContent('a.js', 'someContent');576var node = new SourceNode(null, null, null,577['(function () {\n',578' ', aNode,579' ', new SourceNode(1, 1, 'b.js', 'b'),580'}());']);581node.setSourceContent('b.js', 'otherContent');582var map = node.toStringWithSourceMap({583file: 'foo.js'584}).map;585586assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');587map = new SourceMapConsumer(map.toString());588589assert.equal(map.sources.length, 2);590assert.equal(map.sources[0], 'a.js');591assert.equal(map.sources[1], 'b.js');592assert.equal(map.sourcesContent.length, 2);593assert.equal(map.sourcesContent[0], 'someContent');594assert.equal(map.sourcesContent[1], 'otherContent');595};596597exports['test walkSourceContents'] = function (assert, util) {598var aNode = new SourceNode(1, 1, 'a.js', 'a');599aNode.setSourceContent('a.js', 'someContent');600var node = new SourceNode(null, null, null,601['(function () {\n',602' ', aNode,603' ', new SourceNode(1, 1, 'b.js', 'b'),604'}());']);605node.setSourceContent('b.js', 'otherContent');606var results = [];607node.walkSourceContents(function (sourceFile, sourceContent) {608results.push([sourceFile, sourceContent]);609});610assert.equal(results.length, 2);611assert.equal(results[0][0], 'a.js');612assert.equal(results[0][1], 'someContent');613assert.equal(results[1][0], 'b.js');614assert.equal(results[1][1], 'otherContent');615};616});617function run_test() {618runSourceMapTests('test/source-map/test-source-node', do_throw);619}620621622