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 exports.testMapWithSourcesContent = {
61 names: ['bar', 'baz', 'n'],
62 sources: ['one.js', 'two.js'],
64 ' ONE.foo = function (bar) {\n' +
65 ' return baz(bar);\n' +
67 ' TWO.inc = function (n) {\n' +
71 sourceRoot: '/the/root',
72 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
74 exports.testMapRelativeSources = {
77 names: ['bar', 'baz', 'n'],
78 sources: ['./one.js', './two.js'],
80 ' ONE.foo = function (bar) {\n' +
81 ' return baz(bar);\n' +
83 ' TWO.inc = function (n) {\n' +
87 sourceRoot: '/the/root',
88 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
99 function assertMapping(generatedLine, generatedColumn, originalSource,
100 originalLine, originalColumn, name, map, assert,
101 dontTestGenerated, dontTestOriginal) {
102 if (!dontTestOriginal) {
103 var origMapping = map.originalPositionFor({
105 column: generatedColumn
107 assert.equal(origMapping.name, name,
108 'Incorrect name, expected ' + JSON.stringify(name)
109 + ', got ' + JSON.stringify(origMapping.name));
110 assert.equal(origMapping.line, originalLine,
111 'Incorrect line, expected ' + JSON.stringify(originalLine)
112 + ', got ' + JSON.stringify(origMapping.line));
113 assert.equal(origMapping.column, originalColumn,
114 'Incorrect column, expected ' + JSON.stringify(originalColumn)
115 + ', got ' + JSON.stringify(origMapping.column));
119 if (originalSource && map.sourceRoot && originalSource.indexOf(map.sourceRoot) === 0) {
120 expectedSource = originalSource;
121 } else if (originalSource) {
122 expectedSource = map.sourceRoot
123 ? util.join(map.sourceRoot, originalSource)
126 expectedSource = null;
129 assert.equal(origMapping.source, expectedSource,
130 'Incorrect source, expected ' + JSON.stringify(expectedSource)
131 + ', got ' + JSON.stringify(origMapping.source));
134 if (!dontTestGenerated) {
135 var genMapping = map.generatedPositionFor({
136 source: originalSource,
138 column: originalColumn
140 assert.equal(genMapping.line, generatedLine,
141 'Incorrect line, expected ' + JSON.stringify(generatedLine)
142 + ', got ' + JSON.stringify(genMapping.line));
143 assert.equal(genMapping.column, generatedColumn,
144 'Incorrect column, expected ' + JSON.stringify(generatedColumn)
145 + ', got ' + JSON.stringify(genMapping.column));
148 exports.assertMapping = assertMapping;
150 function assertEqualMaps(assert, actualMap, expectedMap) {
151 assert.equal(actualMap.version, expectedMap.version, "version mismatch");
152 assert.equal(actualMap.file, expectedMap.file, "file mismatch");
153 assert.equal(actualMap.names.length,
154 expectedMap.names.length,
155 "names length mismatch: " +
156 actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
157 for (var i = 0; i < actualMap.names.length; i++) {
158 assert.equal(actualMap.names[i],
159 expectedMap.names[i],
160 "names[" + i + "] mismatch: " +
161 actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
163 assert.equal(actualMap.sources.length,
164 expectedMap.sources.length,
165 "sources length mismatch: " +
166 actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
167 for (var i = 0; i < actualMap.sources.length; i++) {
168 assert.equal(actualMap.sources[i],
169 expectedMap.sources[i],
170 "sources[" + i + "] length mismatch: " +
171 actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
173 assert.equal(actualMap.sourceRoot,
174 expectedMap.sourceRoot,
175 "sourceRoot mismatch: " +
176 actualMap.sourceRoot + " != " + expectedMap.sourceRoot);
177 assert.equal(actualMap.mappings, expectedMap.mappings,
178 "mappings mismatch:\nActual: " + actualMap.mappings + "\nExpected: " + expectedMap.mappings);
179 if (actualMap.sourcesContent) {
180 assert.equal(actualMap.sourcesContent.length,
181 expectedMap.sourcesContent.length,
182 "sourcesContent length mismatch");
183 for (var i = 0; i < actualMap.sourcesContent.length; i++) {
184 assert.equal(actualMap.sourcesContent[i],
185 expectedMap.sourcesContent[i],
186 "sourcesContent[" + i + "] mismatch");
190 exports.assertEqualMaps = assertEqualMaps;