react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / uglify-js / node_modules / source-map / test / source-map / test-source-map-consumer.js
80765 views/* -*- Mode: js; js-indent-level: 2; -*- */1/*2* Copyright 2011 Mozilla Foundation and contributors3* Licensed under the New BSD license. See LICENSE or:4* http://opensource.org/licenses/BSD-3-Clause5*/6if (typeof define !== 'function') {7var define = require('amdefine')(module, require);8}9define(function (require, exports, module) {1011var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;12var SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;1314exports['test that we can instantiate with a string or an object'] = function (assert, util) {15assert.doesNotThrow(function () {16var map = new SourceMapConsumer(util.testMap);17});18assert.doesNotThrow(function () {19var map = new SourceMapConsumer(JSON.stringify(util.testMap));20});21};2223exports['test that the `sources` field has the original sources'] = function (assert, util) {24var map = new SourceMapConsumer(util.testMap);25var sources = map.sources;2627assert.equal(sources[0], '/the/root/one.js');28assert.equal(sources[1], '/the/root/two.js');29assert.equal(sources.length, 2);30};3132exports['test that the source root is reflected in a mapping\'s source field'] = function (assert, util) {33var map = new SourceMapConsumer(util.testMap);34var mapping;3536mapping = map.originalPositionFor({37line: 2,38column: 139});40assert.equal(mapping.source, '/the/root/two.js');4142mapping = map.originalPositionFor({43line: 1,44column: 145});46assert.equal(mapping.source, '/the/root/one.js');47};4849exports['test mapping tokens back exactly'] = function (assert, util) {50var map = new SourceMapConsumer(util.testMap);5152util.assertMapping(1, 1, '/the/root/one.js', 1, 1, null, map, assert);53util.assertMapping(1, 5, '/the/root/one.js', 1, 5, null, map, assert);54util.assertMapping(1, 9, '/the/root/one.js', 1, 11, null, map, assert);55util.assertMapping(1, 18, '/the/root/one.js', 1, 21, 'bar', map, assert);56util.assertMapping(1, 21, '/the/root/one.js', 2, 3, null, map, assert);57util.assertMapping(1, 28, '/the/root/one.js', 2, 10, 'baz', map, assert);58util.assertMapping(1, 32, '/the/root/one.js', 2, 14, 'bar', map, assert);5960util.assertMapping(2, 1, '/the/root/two.js', 1, 1, null, map, assert);61util.assertMapping(2, 5, '/the/root/two.js', 1, 5, null, map, assert);62util.assertMapping(2, 9, '/the/root/two.js', 1, 11, null, map, assert);63util.assertMapping(2, 18, '/the/root/two.js', 1, 21, 'n', map, assert);64util.assertMapping(2, 21, '/the/root/two.js', 2, 3, null, map, assert);65util.assertMapping(2, 28, '/the/root/two.js', 2, 10, 'n', map, assert);66};6768exports['test mapping tokens fuzzy'] = function (assert, util) {69var map = new SourceMapConsumer(util.testMap);7071// Finding original positions72util.assertMapping(1, 20, '/the/root/one.js', 1, 21, 'bar', map, assert, true);73util.assertMapping(1, 30, '/the/root/one.js', 2, 10, 'baz', map, assert, true);74util.assertMapping(2, 12, '/the/root/two.js', 1, 11, null, map, assert, true);7576// Finding generated positions77util.assertMapping(1, 18, '/the/root/one.js', 1, 22, 'bar', map, assert, null, true);78util.assertMapping(1, 28, '/the/root/one.js', 2, 13, 'baz', map, assert, null, true);79util.assertMapping(2, 9, '/the/root/two.js', 1, 16, null, map, assert, null, true);80};8182exports['test mappings and end of lines'] = function (assert, util) {83var smg = new SourceMapGenerator({84file: 'foo.js'85});86smg.addMapping({87original: { line: 1, column: 1 },88generated: { line: 1, column: 1 },89source: 'bar.js'90});91smg.addMapping({92original: { line: 2, column: 2 },93generated: { line: 2, column: 2 },94source: 'bar.js'95});9697var map = SourceMapConsumer.fromSourceMap(smg);9899// When finding original positions, mappings end at the end of the line.100util.assertMapping(2, 1, null, null, null, null, map, assert, true)101102// When finding generated positions, mappings do not end at the end of the line.103util.assertMapping(1, 1, 'bar.js', 2, 1, null, map, assert, null, true);104};105106exports['test creating source map consumers with )]}\' prefix'] = function (assert, util) {107assert.doesNotThrow(function () {108var map = new SourceMapConsumer(")]}'" + JSON.stringify(util.testMap));109});110};111112exports['test eachMapping'] = function (assert, util) {113var map = new SourceMapConsumer(util.testMap);114var previousLine = -Infinity;115var previousColumn = -Infinity;116map.eachMapping(function (mapping) {117assert.ok(mapping.generatedLine >= previousLine);118119if (mapping.source) {120assert.equal(mapping.source.indexOf(util.testMap.sourceRoot), 0);121}122123if (mapping.generatedLine === previousLine) {124assert.ok(mapping.generatedColumn >= previousColumn);125previousColumn = mapping.generatedColumn;126}127else {128previousLine = mapping.generatedLine;129previousColumn = -Infinity;130}131});132};133134exports['test iterating over mappings in a different order'] = function (assert, util) {135var map = new SourceMapConsumer(util.testMap);136var previousLine = -Infinity;137var previousColumn = -Infinity;138var previousSource = "";139map.eachMapping(function (mapping) {140assert.ok(mapping.source >= previousSource);141142if (mapping.source === previousSource) {143assert.ok(mapping.originalLine >= previousLine);144145if (mapping.originalLine === previousLine) {146assert.ok(mapping.originalColumn >= previousColumn);147previousColumn = mapping.originalColumn;148}149else {150previousLine = mapping.originalLine;151previousColumn = -Infinity;152}153}154else {155previousSource = mapping.source;156previousLine = -Infinity;157previousColumn = -Infinity;158}159}, null, SourceMapConsumer.ORIGINAL_ORDER);160};161162exports['test that we can set the context for `this` in eachMapping'] = function (assert, util) {163var map = new SourceMapConsumer(util.testMap);164var context = {};165map.eachMapping(function () {166assert.equal(this, context);167}, context);168};169170exports['test that the `sourcesContent` field has the original sources'] = function (assert, util) {171var map = new SourceMapConsumer(util.testMapWithSourcesContent);172var sourcesContent = map.sourcesContent;173174assert.equal(sourcesContent[0], ' ONE.foo = function (bar) {\n return baz(bar);\n };');175assert.equal(sourcesContent[1], ' TWO.inc = function (n) {\n return n + 1;\n };');176assert.equal(sourcesContent.length, 2);177};178179exports['test that we can get the original sources for the sources'] = function (assert, util) {180var map = new SourceMapConsumer(util.testMapWithSourcesContent);181var sources = map.sources;182183assert.equal(map.sourceContentFor(sources[0]), ' ONE.foo = function (bar) {\n return baz(bar);\n };');184assert.equal(map.sourceContentFor(sources[1]), ' TWO.inc = function (n) {\n return n + 1;\n };');185assert.equal(map.sourceContentFor("one.js"), ' ONE.foo = function (bar) {\n return baz(bar);\n };');186assert.equal(map.sourceContentFor("two.js"), ' TWO.inc = function (n) {\n return n + 1;\n };');187assert.throws(function () {188map.sourceContentFor("");189}, Error);190assert.throws(function () {191map.sourceContentFor("/the/root/three.js");192}, Error);193assert.throws(function () {194map.sourceContentFor("three.js");195}, Error);196};197198exports['test sourceRoot + generatedPositionFor'] = function (assert, util) {199var map = new SourceMapGenerator({200sourceRoot: 'foo/bar',201file: 'baz.js'202});203map.addMapping({204original: { line: 1, column: 1 },205generated: { line: 2, column: 2 },206source: 'bang.coffee'207});208map.addMapping({209original: { line: 5, column: 5 },210generated: { line: 6, column: 6 },211source: 'bang.coffee'212});213map = new SourceMapConsumer(map.toString());214215// Should handle without sourceRoot.216var pos = map.generatedPositionFor({217line: 1,218column: 1,219source: 'bang.coffee'220});221222assert.equal(pos.line, 2);223assert.equal(pos.column, 2);224225// Should handle with sourceRoot.226var pos = map.generatedPositionFor({227line: 1,228column: 1,229source: 'foo/bar/bang.coffee'230});231232assert.equal(pos.line, 2);233assert.equal(pos.column, 2);234};235236exports['test sourceRoot + originalPositionFor'] = function (assert, util) {237var map = new SourceMapGenerator({238sourceRoot: 'foo/bar',239file: 'baz.js'240});241map.addMapping({242original: { line: 1, column: 1 },243generated: { line: 2, column: 2 },244source: 'bang.coffee'245});246map = new SourceMapConsumer(map.toString());247248var pos = map.originalPositionFor({249line: 2,250column: 2,251});252253// Should always have the prepended source root254assert.equal(pos.source, 'foo/bar/bang.coffee');255assert.equal(pos.line, 1);256assert.equal(pos.column, 1);257};258259exports['test github issue #56'] = function (assert, util) {260var map = new SourceMapGenerator({261sourceRoot: 'http://',262file: 'www.example.com/foo.js'263});264map.addMapping({265original: { line: 1, column: 1 },266generated: { line: 2, column: 2 },267source: 'www.example.com/original.js'268});269map = new SourceMapConsumer(map.toString());270271var sources = map.sources;272assert.equal(sources.length, 1);273assert.equal(sources[0], 'http://www.example.com/original.js');274};275276exports['test github issue #43'] = function (assert, util) {277var map = new SourceMapGenerator({278sourceRoot: 'http://example.com',279file: 'foo.js'280});281map.addMapping({282original: { line: 1, column: 1 },283generated: { line: 2, column: 2 },284source: 'http://cdn.example.com/original.js'285});286map = new SourceMapConsumer(map.toString());287288var sources = map.sources;289assert.equal(sources.length, 1,290'Should only be one source.');291assert.equal(sources[0], 'http://cdn.example.com/original.js',292'Should not be joined with the sourceRoot.');293};294295exports['test absolute path, but same host sources'] = function (assert, util) {296var map = new SourceMapGenerator({297sourceRoot: 'http://example.com/foo/bar',298file: 'foo.js'299});300map.addMapping({301original: { line: 1, column: 1 },302generated: { line: 2, column: 2 },303source: '/original.js'304});305map = new SourceMapConsumer(map.toString());306307var sources = map.sources;308assert.equal(sources.length, 1,309'Should only be one source.');310assert.equal(sources[0], 'http://example.com/original.js',311'Source should be relative the host of the source root.');312};313314exports['test github issue #64'] = function (assert, util) {315var map = new SourceMapConsumer({316"version": 3,317"file": "foo.js",318"sourceRoot": "http://example.com/",319"sources": ["/a"],320"names": [],321"mappings": "AACA",322"sourcesContent": ["foo"]323});324325assert.equal(map.sourceContentFor("a"), "foo");326assert.equal(map.sourceContentFor("/a"), "foo");327};328329exports['test bug 885597'] = function (assert, util) {330var map = new SourceMapConsumer({331"version": 3,332"file": "foo.js",333"sourceRoot": "file:///Users/AlGore/Invented/The/Internet/",334"sources": ["/a"],335"names": [],336"mappings": "AACA",337"sourcesContent": ["foo"]338});339340var s = map.sources[0];341assert.equal(map.sourceContentFor(s), "foo");342};343344exports['test github issue #72, duplicate sources'] = function (assert, util) {345var map = new SourceMapConsumer({346"version": 3,347"file": "foo.js",348"sources": ["source1.js", "source1.js", "source3.js"],349"names": [],350"mappings": ";EAAC;;IAEE;;MEEE",351"sourceRoot": "http://example.com"352});353354var pos = map.originalPositionFor({355line: 2,356column: 2357});358assert.equal(pos.source, 'http://example.com/source1.js');359assert.equal(pos.line, 1);360assert.equal(pos.column, 1);361362var pos = map.originalPositionFor({363line: 4,364column: 4365});366assert.equal(pos.source, 'http://example.com/source1.js');367assert.equal(pos.line, 3);368assert.equal(pos.column, 3);369370var pos = map.originalPositionFor({371line: 6,372column: 6373});374assert.equal(pos.source, 'http://example.com/source3.js');375assert.equal(pos.line, 5);376assert.equal(pos.column, 5);377};378379exports['test github issue #72, duplicate names'] = function (assert, util) {380var map = new SourceMapConsumer({381"version": 3,382"file": "foo.js",383"sources": ["source.js"],384"names": ["name1", "name1", "name3"],385"mappings": ";EAACA;;IAEEA;;MAEEE",386"sourceRoot": "http://example.com"387});388389var pos = map.originalPositionFor({390line: 2,391column: 2392});393assert.equal(pos.name, 'name1');394assert.equal(pos.line, 1);395assert.equal(pos.column, 1);396397var pos = map.originalPositionFor({398line: 4,399column: 4400});401assert.equal(pos.name, 'name1');402assert.equal(pos.line, 3);403assert.equal(pos.column, 3);404405var pos = map.originalPositionFor({406line: 6,407column: 6408});409assert.equal(pos.name, 'name3');410assert.equal(pos.line, 5);411assert.equal(pos.column, 5);412};413414exports['test SourceMapConsumer.fromSourceMap'] = function (assert, util) {415var smg = new SourceMapGenerator({416sourceRoot: 'http://example.com/',417file: 'foo.js'418});419smg.addMapping({420original: { line: 1, column: 1 },421generated: { line: 2, column: 2 },422source: 'bar.js'423});424smg.addMapping({425original: { line: 2, column: 2 },426generated: { line: 4, column: 4 },427source: 'baz.js',428name: 'dirtMcGirt'429});430smg.setSourceContent('baz.js', 'baz.js content');431432var smc = SourceMapConsumer.fromSourceMap(smg);433assert.equal(smc.file, 'foo.js');434assert.equal(smc.sourceRoot, 'http://example.com/');435assert.equal(smc.sources.length, 2);436assert.equal(smc.sources[0], 'http://example.com/bar.js');437assert.equal(smc.sources[1], 'http://example.com/baz.js');438assert.equal(smc.sourceContentFor('baz.js'), 'baz.js content');439440var pos = smc.originalPositionFor({441line: 2,442column: 2443});444assert.equal(pos.line, 1);445assert.equal(pos.column, 1);446assert.equal(pos.source, 'http://example.com/bar.js');447assert.equal(pos.name, null);448449pos = smc.generatedPositionFor({450line: 1,451column: 1,452source: 'http://example.com/bar.js'453});454assert.equal(pos.line, 2);455assert.equal(pos.column, 2);456457pos = smc.originalPositionFor({458line: 4,459column: 4460});461assert.equal(pos.line, 2);462assert.equal(pos.column, 2);463assert.equal(pos.source, 'http://example.com/baz.js');464assert.equal(pos.name, 'dirtMcGirt');465466pos = smc.generatedPositionFor({467line: 2,468column: 2,469source: 'http://example.com/baz.js'470});471assert.equal(pos.line, 4);472assert.equal(pos.column, 4);473};474});475476477