1 /* -*- Mode: js; js-indent-level: 2; -*- */
3 * Copyright 2011 Mozilla Foundation and contributors
4 * Licensed under the New BSD license. See LICENSE or:
5 * http://opensource.org/licenses/BSD-3-Clause
7 if (typeof define !== 'function') {
8 var define = require('amdefine')(module, require);
10 define(function (require, exports, module) {
12 var util = require('../../lib/source-map/util');
14 // This is a test mapping which maps functions from two different files
15 // (one.js and two.js) to a minified generated source.
19 // ONE.foo = function (bar) {
25 // TWO.inc = function (n) {
29 // And here is the generated code (min.js):
31 // ONE.foo=function(a){return baz(a);};
32 // TWO.inc=function(a){return a+1;};
33 exports.testGeneratedCode = " ONE.foo=function(a){return baz(a);};\n"+
34 " TWO.inc=function(a){return a+1;};";
38 names: ['bar', 'baz', 'n'],
39 sources: ['one.js', 'two.js'],
40 sourceRoot: '/the/root',
41 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
43 exports.testMapNoSourceRoot = {
46 names: ['bar', 'baz', 'n'],
47 sources: ['one.js', 'two.js'],
48 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
50 exports.testMapEmptySourceRoot = {
53 names: ['bar', 'baz', 'n'],
54 sources: ['one.js', 'two.js'],
56 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
58 // This mapping is identical to above, but uses the indexed format instead.
59 exports.indexedTestMap = {
74 ' ONE.foo = function (bar) {\n' +
75 ' return baz(bar);\n' +
82 mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID",
84 sourceRoot: "/the/root"
98 ' TWO.inc = function (n) {\n' +
105 mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
107 sourceRoot: "/the/root"
112 exports.indexedTestMapDifferentSourceRoots = {
127 ' ONE.foo = function (bar) {\n' +
128 ' return baz(bar);\n' +
135 mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID",
137 sourceRoot: "/the/root"
151 ' TWO.inc = function (n) {\n' +
158 mappings: "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
160 sourceRoot: "/different/root"
165 exports.testMapWithSourcesContent = {
168 names: ['bar', 'baz', 'n'],
169 sources: ['one.js', 'two.js'],
171 ' ONE.foo = function (bar) {\n' +
172 ' return baz(bar);\n' +
174 ' TWO.inc = function (n) {\n' +
178 sourceRoot: '/the/root',
179 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
181 exports.testMapRelativeSources = {
184 names: ['bar', 'baz', 'n'],
185 sources: ['./one.js', './two.js'],
187 ' ONE.foo = function (bar) {\n' +
188 ' return baz(bar);\n' +
190 ' TWO.inc = function (n) {\n' +
194 sourceRoot: '/the/root',
195 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
206 function assertMapping(generatedLine, generatedColumn, originalSource,
207 originalLine, originalColumn, name, map, assert,
208 dontTestGenerated, dontTestOriginal) {
209 if (!dontTestOriginal) {
210 var origMapping = map.originalPositionFor({
212 column: generatedColumn
214 assert.equal(origMapping.name, name,
215 'Incorrect name, expected ' + JSON.stringify(name)
216 + ', got ' + JSON.stringify(origMapping.name));
217 assert.equal(origMapping.line, originalLine,
218 'Incorrect line, expected ' + JSON.stringify(originalLine)
219 + ', got ' + JSON.stringify(origMapping.line));
220 assert.equal(origMapping.column, originalColumn,
221 'Incorrect column, expected ' + JSON.stringify(originalColumn)
222 + ', got ' + JSON.stringify(origMapping.column));
226 if (originalSource && map.sourceRoot && originalSource.indexOf(map.sourceRoot) === 0) {
227 expectedSource = originalSource;
228 } else if (originalSource) {
229 expectedSource = map.sourceRoot
230 ? util.join(map.sourceRoot, originalSource)
233 expectedSource = null;
236 assert.equal(origMapping.source, expectedSource,
237 'Incorrect source, expected ' + JSON.stringify(expectedSource)
238 + ', got ' + JSON.stringify(origMapping.source));
241 if (!dontTestGenerated) {
242 var genMapping = map.generatedPositionFor({
243 source: originalSource,
245 column: originalColumn
247 assert.equal(genMapping.line, generatedLine,
248 'Incorrect line, expected ' + JSON.stringify(generatedLine)
249 + ', got ' + JSON.stringify(genMapping.line));
250 assert.equal(genMapping.column, generatedColumn,
251 'Incorrect column, expected ' + JSON.stringify(generatedColumn)
252 + ', got ' + JSON.stringify(genMapping.column));
255 exports.assertMapping = assertMapping;
257 function assertEqualMaps(assert, actualMap, expectedMap) {
258 assert.equal(actualMap.version, expectedMap.version, "version mismatch");
259 assert.equal(actualMap.file, expectedMap.file, "file mismatch");
260 assert.equal(actualMap.names.length,
261 expectedMap.names.length,
262 "names length mismatch: " +
263 actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
264 for (var i = 0; i < actualMap.names.length; i++) {
265 assert.equal(actualMap.names[i],
266 expectedMap.names[i],
267 "names[" + i + "] mismatch: " +
268 actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
270 assert.equal(actualMap.sources.length,
271 expectedMap.sources.length,
272 "sources length mismatch: " +
273 actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
274 for (var i = 0; i < actualMap.sources.length; i++) {
275 assert.equal(actualMap.sources[i],
276 expectedMap.sources[i],
277 "sources[" + i + "] length mismatch: " +
278 actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
280 assert.equal(actualMap.sourceRoot,
281 expectedMap.sourceRoot,
282 "sourceRoot mismatch: " +
283 actualMap.sourceRoot + " != " + expectedMap.sourceRoot);
284 assert.equal(actualMap.mappings, expectedMap.mappings,
285 "mappings mismatch:\nActual: " + actualMap.mappings + "\nExpected: " + expectedMap.mappings);
286 if (actualMap.sourcesContent) {
287 assert.equal(actualMap.sourcesContent.length,
288 expectedMap.sourcesContent.length,
289 "sourcesContent length mismatch");
290 for (var i = 0; i < actualMap.sourcesContent.length; i++) {
291 assert.equal(actualMap.sourcesContent[i],
292 expectedMap.sourcesContent[i],
293 "sourcesContent[" + i + "] mismatch");
297 exports.assertEqualMaps = assertEqualMaps;