|
1 /* |
|
2 * WARNING! |
|
3 * |
|
4 * Do not edit this file directly, it is built from the sources at |
|
5 * https://github.com/mozilla/source-map/ |
|
6 */ |
|
7 |
|
8 Components.utils.import('resource://test/Utils.jsm'); |
|
9 /* -*- Mode: js; js-indent-level: 2; -*- */ |
|
10 /* |
|
11 * Copyright 2011 Mozilla Foundation and contributors |
|
12 * Licensed under the New BSD license. See LICENSE or: |
|
13 * http://opensource.org/licenses/BSD-3-Clause |
|
14 */ |
|
15 define("test/source-map/test-source-node", ["require", "exports", "module"], function (require, exports, module) { |
|
16 |
|
17 var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator; |
|
18 var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer; |
|
19 var SourceNode = require('source-map/source-node').SourceNode; |
|
20 |
|
21 exports['test .add()'] = function (assert, util) { |
|
22 var node = new SourceNode(null, null, null); |
|
23 |
|
24 // Adding a string works. |
|
25 node.add('function noop() {}'); |
|
26 |
|
27 // Adding another source node works. |
|
28 node.add(new SourceNode(null, null, null)); |
|
29 |
|
30 // Adding an array works. |
|
31 node.add(['function foo() {', |
|
32 new SourceNode(null, null, null, |
|
33 'return 10;'), |
|
34 '}']); |
|
35 |
|
36 // Adding other stuff doesn't. |
|
37 assert.throws(function () { |
|
38 node.add({}); |
|
39 }); |
|
40 assert.throws(function () { |
|
41 node.add(function () {}); |
|
42 }); |
|
43 }; |
|
44 |
|
45 exports['test .prepend()'] = function (assert, util) { |
|
46 var node = new SourceNode(null, null, null); |
|
47 |
|
48 // Prepending a string works. |
|
49 node.prepend('function noop() {}'); |
|
50 assert.equal(node.children[0], 'function noop() {}'); |
|
51 assert.equal(node.children.length, 1); |
|
52 |
|
53 // Prepending another source node works. |
|
54 node.prepend(new SourceNode(null, null, null)); |
|
55 assert.equal(node.children[0], ''); |
|
56 assert.equal(node.children[1], 'function noop() {}'); |
|
57 assert.equal(node.children.length, 2); |
|
58 |
|
59 // Prepending an array works. |
|
60 node.prepend(['function foo() {', |
|
61 new SourceNode(null, null, null, |
|
62 'return 10;'), |
|
63 '}']); |
|
64 assert.equal(node.children[0], 'function foo() {'); |
|
65 assert.equal(node.children[1], 'return 10;'); |
|
66 assert.equal(node.children[2], '}'); |
|
67 assert.equal(node.children[3], ''); |
|
68 assert.equal(node.children[4], 'function noop() {}'); |
|
69 assert.equal(node.children.length, 5); |
|
70 |
|
71 // Prepending other stuff doesn't. |
|
72 assert.throws(function () { |
|
73 node.prepend({}); |
|
74 }); |
|
75 assert.throws(function () { |
|
76 node.prepend(function () {}); |
|
77 }); |
|
78 }; |
|
79 |
|
80 exports['test .toString()'] = function (assert, util) { |
|
81 assert.equal((new SourceNode(null, null, null, |
|
82 ['function foo() {', |
|
83 new SourceNode(null, null, null, 'return 10;'), |
|
84 '}'])).toString(), |
|
85 'function foo() {return 10;}'); |
|
86 }; |
|
87 |
|
88 exports['test .join()'] = function (assert, util) { |
|
89 assert.equal((new SourceNode(null, null, null, |
|
90 ['a', 'b', 'c', 'd'])).join(', ').toString(), |
|
91 'a, b, c, d'); |
|
92 }; |
|
93 |
|
94 exports['test .walk()'] = function (assert, util) { |
|
95 var node = new SourceNode(null, null, null, |
|
96 ['(function () {\n', |
|
97 ' ', new SourceNode(1, 0, 'a.js', ['someCall()']), ';\n', |
|
98 ' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n', |
|
99 '}());']); |
|
100 var expected = [ |
|
101 { str: '(function () {\n', source: null, line: null, column: null }, |
|
102 { str: ' ', source: null, line: null, column: null }, |
|
103 { str: 'someCall()', source: 'a.js', line: 1, column: 0 }, |
|
104 { str: ';\n', source: null, line: null, column: null }, |
|
105 { str: ' ', source: null, line: null, column: null }, |
|
106 { str: 'if (foo) bar()', source: 'b.js', line: 2, column: 0 }, |
|
107 { str: ';\n', source: null, line: null, column: null }, |
|
108 { str: '}());', source: null, line: null, column: null }, |
|
109 ]; |
|
110 var i = 0; |
|
111 node.walk(function (chunk, loc) { |
|
112 assert.equal(expected[i].str, chunk); |
|
113 assert.equal(expected[i].source, loc.source); |
|
114 assert.equal(expected[i].line, loc.line); |
|
115 assert.equal(expected[i].column, loc.column); |
|
116 i++; |
|
117 }); |
|
118 }; |
|
119 |
|
120 exports['test .replaceRight'] = function (assert, util) { |
|
121 var node; |
|
122 |
|
123 // Not nested |
|
124 node = new SourceNode(null, null, null, 'hello world'); |
|
125 node.replaceRight(/world/, 'universe'); |
|
126 assert.equal(node.toString(), 'hello universe'); |
|
127 |
|
128 // Nested |
|
129 node = new SourceNode(null, null, null, |
|
130 [new SourceNode(null, null, null, 'hey sexy mama, '), |
|
131 new SourceNode(null, null, null, 'want to kill all humans?')]); |
|
132 node.replaceRight(/kill all humans/, 'watch Futurama'); |
|
133 assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?'); |
|
134 }; |
|
135 |
|
136 exports['test .toStringWithSourceMap()'] = function (assert, util) { |
|
137 var node = new SourceNode(null, null, null, |
|
138 ['(function () {\n', |
|
139 ' ', |
|
140 new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'), |
|
141 new SourceNode(1, 8, 'a.js', '()'), |
|
142 ';\n', |
|
143 ' ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n', |
|
144 '}());']); |
|
145 var map = node.toStringWithSourceMap({ |
|
146 file: 'foo.js' |
|
147 }).map; |
|
148 |
|
149 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); |
|
150 map = new SourceMapConsumer(map.toString()); |
|
151 |
|
152 var actual; |
|
153 |
|
154 actual = map.originalPositionFor({ |
|
155 line: 1, |
|
156 column: 4 |
|
157 }); |
|
158 assert.equal(actual.source, null); |
|
159 assert.equal(actual.line, null); |
|
160 assert.equal(actual.column, null); |
|
161 |
|
162 actual = map.originalPositionFor({ |
|
163 line: 2, |
|
164 column: 2 |
|
165 }); |
|
166 assert.equal(actual.source, 'a.js'); |
|
167 assert.equal(actual.line, 1); |
|
168 assert.equal(actual.column, 0); |
|
169 assert.equal(actual.name, 'originalCall'); |
|
170 |
|
171 actual = map.originalPositionFor({ |
|
172 line: 3, |
|
173 column: 2 |
|
174 }); |
|
175 assert.equal(actual.source, 'b.js'); |
|
176 assert.equal(actual.line, 2); |
|
177 assert.equal(actual.column, 0); |
|
178 |
|
179 actual = map.originalPositionFor({ |
|
180 line: 3, |
|
181 column: 16 |
|
182 }); |
|
183 assert.equal(actual.source, null); |
|
184 assert.equal(actual.line, null); |
|
185 assert.equal(actual.column, null); |
|
186 |
|
187 actual = map.originalPositionFor({ |
|
188 line: 4, |
|
189 column: 2 |
|
190 }); |
|
191 assert.equal(actual.source, null); |
|
192 assert.equal(actual.line, null); |
|
193 assert.equal(actual.column, null); |
|
194 }; |
|
195 |
|
196 exports['test .fromStringWithSourceMap()'] = function (assert, util) { |
|
197 var node = SourceNode.fromStringWithSourceMap( |
|
198 util.testGeneratedCode, |
|
199 new SourceMapConsumer(util.testMap)); |
|
200 |
|
201 var result = node.toStringWithSourceMap({ |
|
202 file: 'min.js' |
|
203 }); |
|
204 var map = result.map; |
|
205 var code = result.code; |
|
206 |
|
207 assert.equal(code, util.testGeneratedCode); |
|
208 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); |
|
209 map = map.toJSON(); |
|
210 assert.equal(map.version, util.testMap.version); |
|
211 assert.equal(map.file, util.testMap.file); |
|
212 assert.equal(map.mappings, util.testMap.mappings); |
|
213 }; |
|
214 |
|
215 exports['test .fromStringWithSourceMap() empty map'] = function (assert, util) { |
|
216 var node = SourceNode.fromStringWithSourceMap( |
|
217 util.testGeneratedCode, |
|
218 new SourceMapConsumer(util.emptyMap)); |
|
219 var result = node.toStringWithSourceMap({ |
|
220 file: 'min.js' |
|
221 }); |
|
222 var map = result.map; |
|
223 var code = result.code; |
|
224 |
|
225 assert.equal(code, util.testGeneratedCode); |
|
226 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); |
|
227 map = map.toJSON(); |
|
228 assert.equal(map.version, util.emptyMap.version); |
|
229 assert.equal(map.file, util.emptyMap.file); |
|
230 assert.equal(map.mappings.length, util.emptyMap.mappings.length); |
|
231 assert.equal(map.mappings, util.emptyMap.mappings); |
|
232 }; |
|
233 |
|
234 exports['test .fromStringWithSourceMap() complex version'] = function (assert, util) { |
|
235 var input = new SourceNode(null, null, null, [ |
|
236 "(function() {\n", |
|
237 " var Test = {};\n", |
|
238 " ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };\n"), |
|
239 " ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), "\n", |
|
240 "}());\n", |
|
241 "/* Generated Source */"]); |
|
242 input = input.toStringWithSourceMap({ |
|
243 file: 'foo.js' |
|
244 }); |
|
245 |
|
246 var node = SourceNode.fromStringWithSourceMap( |
|
247 input.code, |
|
248 new SourceMapConsumer(input.map.toString())); |
|
249 |
|
250 var result = node.toStringWithSourceMap({ |
|
251 file: 'foo.js' |
|
252 }); |
|
253 var map = result.map; |
|
254 var code = result.code; |
|
255 |
|
256 assert.equal(code, input.code); |
|
257 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); |
|
258 map = map.toJSON(); |
|
259 var inputMap = input.map.toJSON(); |
|
260 util.assertEqualMaps(assert, map, inputMap); |
|
261 }; |
|
262 |
|
263 exports['test .fromStringWithSourceMap() merging duplicate mappings'] = function (assert, util) { |
|
264 var input = new SourceNode(null, null, null, [ |
|
265 new SourceNode(1, 0, "a.js", "(function"), |
|
266 new SourceNode(1, 0, "a.js", "() {\n"), |
|
267 " ", |
|
268 new SourceNode(1, 0, "a.js", "var Test = "), |
|
269 new SourceNode(1, 0, "b.js", "{};\n"), |
|
270 new SourceNode(2, 0, "b.js", "Test"), |
|
271 new SourceNode(2, 0, "b.js", ".A", "A"), |
|
272 new SourceNode(2, 20, "b.js", " = { value: 1234 };\n", "A"), |
|
273 "}());\n", |
|
274 "/* Generated Source */" |
|
275 ]); |
|
276 input = input.toStringWithSourceMap({ |
|
277 file: 'foo.js' |
|
278 }); |
|
279 |
|
280 var correctMap = new SourceMapGenerator({ |
|
281 file: 'foo.js' |
|
282 }); |
|
283 correctMap.addMapping({ |
|
284 generated: { line: 1, column: 0 }, |
|
285 source: 'a.js', |
|
286 original: { line: 1, column: 0 } |
|
287 }); |
|
288 correctMap.addMapping({ |
|
289 generated: { line: 2, column: 0 } |
|
290 }); |
|
291 correctMap.addMapping({ |
|
292 generated: { line: 2, column: 2 }, |
|
293 source: 'a.js', |
|
294 original: { line: 1, column: 0 } |
|
295 }); |
|
296 correctMap.addMapping({ |
|
297 generated: { line: 2, column: 13 }, |
|
298 source: 'b.js', |
|
299 original: { line: 1, column: 0 } |
|
300 }); |
|
301 correctMap.addMapping({ |
|
302 generated: { line: 3, column: 0 }, |
|
303 source: 'b.js', |
|
304 original: { line: 2, column: 0 } |
|
305 }); |
|
306 correctMap.addMapping({ |
|
307 generated: { line: 3, column: 4 }, |
|
308 source: 'b.js', |
|
309 name: 'A', |
|
310 original: { line: 2, column: 0 } |
|
311 }); |
|
312 correctMap.addMapping({ |
|
313 generated: { line: 3, column: 6 }, |
|
314 source: 'b.js', |
|
315 name: 'A', |
|
316 original: { line: 2, column: 20 } |
|
317 }); |
|
318 correctMap.addMapping({ |
|
319 generated: { line: 4, column: 0 } |
|
320 }); |
|
321 |
|
322 var inputMap = input.map.toJSON(); |
|
323 correctMap = correctMap.toJSON(); |
|
324 util.assertEqualMaps(assert, correctMap, inputMap); |
|
325 }; |
|
326 |
|
327 exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) { |
|
328 var aNode = new SourceNode(1, 1, 'a.js', 'a'); |
|
329 aNode.setSourceContent('a.js', 'someContent'); |
|
330 var node = new SourceNode(null, null, null, |
|
331 ['(function () {\n', |
|
332 ' ', aNode, |
|
333 ' ', new SourceNode(1, 1, 'b.js', 'b'), |
|
334 '}());']); |
|
335 node.setSourceContent('b.js', 'otherContent'); |
|
336 var map = node.toStringWithSourceMap({ |
|
337 file: 'foo.js' |
|
338 }).map; |
|
339 |
|
340 assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator'); |
|
341 map = new SourceMapConsumer(map.toString()); |
|
342 |
|
343 assert.equal(map.sources.length, 2); |
|
344 assert.equal(map.sources[0], 'a.js'); |
|
345 assert.equal(map.sources[1], 'b.js'); |
|
346 assert.equal(map.sourcesContent.length, 2); |
|
347 assert.equal(map.sourcesContent[0], 'someContent'); |
|
348 assert.equal(map.sourcesContent[1], 'otherContent'); |
|
349 }; |
|
350 |
|
351 exports['test walkSourceContents'] = function (assert, util) { |
|
352 var aNode = new SourceNode(1, 1, 'a.js', 'a'); |
|
353 aNode.setSourceContent('a.js', 'someContent'); |
|
354 var node = new SourceNode(null, null, null, |
|
355 ['(function () {\n', |
|
356 ' ', aNode, |
|
357 ' ', new SourceNode(1, 1, 'b.js', 'b'), |
|
358 '}());']); |
|
359 node.setSourceContent('b.js', 'otherContent'); |
|
360 var results = []; |
|
361 node.walkSourceContents(function (sourceFile, sourceContent) { |
|
362 results.push([sourceFile, sourceContent]); |
|
363 }); |
|
364 assert.equal(results.length, 2); |
|
365 assert.equal(results[0][0], 'a.js'); |
|
366 assert.equal(results[0][1], 'someContent'); |
|
367 assert.equal(results[1][0], 'b.js'); |
|
368 assert.equal(results[1][1], 'otherContent'); |
|
369 }; |
|
370 }); |
|
371 function run_test() { |
|
372 runSourceMapTests('test/source-map/test-source-node', do_throw); |
|
373 } |