Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
/* -*- Mode: js; js-indent-level: 2; -*- */
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
6
*/
7
if (typeof define !== 'function') {
8
var define = require('amdefine')(module, require);
9
}
10
define(function (require, exports, module) {
11
12
var util = require('../../lib/source-map/util');
13
14
// This is a test mapping which maps functions from two different files
15
// (one.js and two.js) to a minified generated source.
16
//
17
// Here is one.js:
18
//
19
// ONE.foo = function (bar) {
20
// return baz(bar);
21
// };
22
//
23
// Here is two.js:
24
//
25
// TWO.inc = function (n) {
26
// return n + 1;
27
// };
28
//
29
// And here is the generated code (min.js):
30
//
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;};";
35
exports.testMap = {
36
version: 3,
37
file: 'min.js',
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'
42
};
43
exports.testMapWithSourcesContent = {
44
version: 3,
45
file: 'min.js',
46
names: ['bar', 'baz', 'n'],
47
sources: ['one.js', 'two.js'],
48
sourcesContent: [
49
' ONE.foo = function (bar) {\n' +
50
' return baz(bar);\n' +
51
' };',
52
' TWO.inc = function (n) {\n' +
53
' return n + 1;\n' +
54
' };'
55
],
56
sourceRoot: '/the/root',
57
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
58
};
59
exports.emptyMap = {
60
version: 3,
61
file: 'min.js',
62
names: [],
63
sources: [],
64
mappings: ''
65
};
66
67
68
function assertMapping(generatedLine, generatedColumn, originalSource,
69
originalLine, originalColumn, name, map, assert,
70
dontTestGenerated, dontTestOriginal) {
71
if (!dontTestOriginal) {
72
var origMapping = map.originalPositionFor({
73
line: generatedLine,
74
column: generatedColumn
75
});
76
assert.equal(origMapping.name, name,
77
'Incorrect name, expected ' + JSON.stringify(name)
78
+ ', got ' + JSON.stringify(origMapping.name));
79
assert.equal(origMapping.line, originalLine,
80
'Incorrect line, expected ' + JSON.stringify(originalLine)
81
+ ', got ' + JSON.stringify(origMapping.line));
82
assert.equal(origMapping.column, originalColumn,
83
'Incorrect column, expected ' + JSON.stringify(originalColumn)
84
+ ', got ' + JSON.stringify(origMapping.column));
85
86
var expectedSource;
87
88
if (originalSource && map.sourceRoot && originalSource.indexOf(map.sourceRoot) === 0) {
89
expectedSource = originalSource;
90
} else if (originalSource) {
91
expectedSource = map.sourceRoot
92
? util.join(map.sourceRoot, originalSource)
93
: originalSource;
94
} else {
95
expectedSource = null;
96
}
97
98
assert.equal(origMapping.source, expectedSource,
99
'Incorrect source, expected ' + JSON.stringify(expectedSource)
100
+ ', got ' + JSON.stringify(origMapping.source));
101
}
102
103
if (!dontTestGenerated) {
104
var genMapping = map.generatedPositionFor({
105
source: originalSource,
106
line: originalLine,
107
column: originalColumn
108
});
109
assert.equal(genMapping.line, generatedLine,
110
'Incorrect line, expected ' + JSON.stringify(generatedLine)
111
+ ', got ' + JSON.stringify(genMapping.line));
112
assert.equal(genMapping.column, generatedColumn,
113
'Incorrect column, expected ' + JSON.stringify(generatedColumn)
114
+ ', got ' + JSON.stringify(genMapping.column));
115
}
116
}
117
exports.assertMapping = assertMapping;
118
119
function assertEqualMaps(assert, actualMap, expectedMap) {
120
assert.equal(actualMap.version, expectedMap.version, "version mismatch");
121
assert.equal(actualMap.file, expectedMap.file, "file mismatch");
122
assert.equal(actualMap.names.length,
123
expectedMap.names.length,
124
"names length mismatch: " +
125
actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
126
for (var i = 0; i < actualMap.names.length; i++) {
127
assert.equal(actualMap.names[i],
128
expectedMap.names[i],
129
"names[" + i + "] mismatch: " +
130
actualMap.names.join(", ") + " != " + expectedMap.names.join(", "));
131
}
132
assert.equal(actualMap.sources.length,
133
expectedMap.sources.length,
134
"sources length mismatch: " +
135
actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
136
for (var i = 0; i < actualMap.sources.length; i++) {
137
assert.equal(actualMap.sources[i],
138
expectedMap.sources[i],
139
"sources[" + i + "] length mismatch: " +
140
actualMap.sources.join(", ") + " != " + expectedMap.sources.join(", "));
141
}
142
assert.equal(actualMap.sourceRoot,
143
expectedMap.sourceRoot,
144
"sourceRoot mismatch: " +
145
actualMap.sourceRoot + " != " + expectedMap.sourceRoot);
146
assert.equal(actualMap.mappings, expectedMap.mappings,
147
"mappings mismatch:\nActual: " + actualMap.mappings + "\nExpected: " + expectedMap.mappings);
148
if (actualMap.sourcesContent) {
149
assert.equal(actualMap.sourcesContent.length,
150
expectedMap.sourcesContent.length,
151
"sourcesContent length mismatch");
152
for (var i = 0; i < actualMap.sourcesContent.length; i++) {
153
assert.equal(actualMap.sourcesContent[i],
154
expectedMap.sourcesContent[i],
155
"sourcesContent[" + i + "] mismatch");
156
}
157
}
158
}
159
exports.assertEqualMaps = assertEqualMaps;
160
161
});
162
163