Debian Codegraph @

test/com/google/debugging/sourcemap/SourceMapGeneratorV3Test.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.debugging.sourcemap;
18
19 import com.google.common.collect.Lists;
20 import com.google.common.collect.Maps;
21 import com.google.javascript.jscomp.SourceMap;
22 import com.google.javascript.jscomp.SourceMap.Format;
23
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28
29
30 /**
31 * @author [email protected] (John Lenz)
32 */
33 public class SourceMapGeneratorV3Test extends SourceMapTestCase {
34
35 public SourceMapGeneratorV3Test() {
36 }
37
38 @Override
39 protected SourceMapConsumer getSourceMapConsumer() {
40 return new SourceMapConsumerV3();
41 }
42
43 @Override
44 protected Format getSourceMapFormat() {
45 return SourceMap.Format.V3;
46 }
47
48 public void testBasicMapping1() throws Exception {
49 compileAndCheck("function __BASIC__() { }");
50 }
51
52 public void testBasicMappingGoldenOutput() throws Exception {
53 // Empty source map test
54 checkSourceMap("function __BASIC__() { }",
55
56 "{\n" +
57 "\"version\":3,\n" +
58 "\"file\":\"testcode\",\n" +
59 "\"lineCount\":1,\n" +
60 "\"mappings\":\"AAAAA,QAASA,UAAS,EAAG;\",\n" +
61 "\"sources\":[\"testcode\"],\n" +
62 "\"names\":[\"__BASIC__\"]\n" +
63 "}\n");
64 }
65
66 public void testBasicMapping2() throws Exception {
67 compileAndCheck("function __BASIC__(__PARAM1__) {}");
68 }
69
70 public void testLiteralMappings() throws Exception {
71 compileAndCheck("function __BASIC__(__PARAM1__, __PARAM2__) { " +
72 "var __VAR__ = '__STR__'; }");
73 }
74
75 public void testLiteralMappingsGoldenOutput() throws Exception {
76 // Empty source map test
77 checkSourceMap("function __BASIC__(__PARAM1__, __PARAM2__) { " +
78 "var __VAR__ = '__STR__'; }",
79
80 "{\n" +
81 "\"version\":3,\n" +
82 "\"file\":\"testcode\",\n" +
83 "\"lineCount\":1,\n" +
84 "\"mappings\":\"AAAAA,QAASA,UAAS,CAACC,UAAD,CAAaC,UAAb," +
85 "CAAyB,CAAE,IAAIC,QAAU,SAAhB;\",\n" +
86 "\"sources\":[\"testcode\"],\n" +
87 "\"names\":[\"__BASIC__\",\"__PARAM1__\",\"__PARAM2__\"," +
88 "\"__VAR__\"]\n" +
89 "}\n");
90 }
91
92 public void testMultilineMapping() throws Exception {
93 compileAndCheck("function __BASIC__(__PARAM1__, __PARAM2__) {\n" +
94 "var __VAR__ = '__STR__';\n" +
95 "var __ANO__ = \"__STR2__\";\n" +
96 "}");
97 }
98
99 public void testMultilineMapping2() throws Exception {
100 compileAndCheck("function __BASIC__(__PARAM1__, __PARAM2__) {\n" +
101 "var __VAR__ = 1;\n" +
102 "var __ANO__ = 2;\n" +
103 "}");
104 }
105
106 public void testMultiFunctionMapping() throws Exception {
107 compileAndCheck("function __BASIC__(__PARAM1__, __PARAM2__) {\n" +
108 "var __VAR__ = '__STR__';\n" +
109 "var __ANO__ = \"__STR2__\";\n" +
110 "}\n" +
111
112 "function __BASIC2__(__PARAM3__, __PARAM4__) {\n" +
113 "var __VAR2__ = '__STR2__';\n" +
114 "var __ANO2__ = \"__STR3__\";\n" +
115 "}\n");
116 }
117
118 public void testGoldenOutput0() throws Exception {
119 // Empty source map test
120 checkSourceMap("",
121
122 "{\n" +
123 "\"version\":3,\n" +
124 "\"file\":\"testcode\",\n" +
125 "\"lineCount\":1,\n" +
126 "\"mappings\":\";\",\n" +
127 "\"sources\":[],\n" +
128 "\"names\":[]\n" +
129 "}\n");
130 }
131
132 public void testGoldenOutput0a() throws Exception {
133 // Empty source map test
134 checkSourceMap("a;",
135
136 "{\n" +
137 "\"version\":3,\n" +
138 "\"file\":\"testcode\",\n" +
139 "\"lineCount\":1,\n" +
140 "\"mappings\":\"AAAAA;\",\n" +
141 "\"sources\":[\"testcode\"],\n" +
142 "\"names\":[\"a\"]\n" +
143 "}\n");
144 }
145
146 public void testGoldenOutput1() throws Exception {
147 detailLevel = SourceMap.DetailLevel.ALL;
148
149 checkSourceMap("function f(foo, bar) { foo = foo + bar + 2; return foo; }",
150
151 "{\n" +
152 "\"version\":3,\n" +
153 "\"file\":\"testcode\",\n" +
154 "\"lineCount\":1,\n" +
155 "\"mappings\":\"AAAAA,QAASA,EAAC,CAACC,GAAD,CAAMC,GAAN," +
156 "CAAW,CAAED,GAAA,CAAMA,GAAN,CAAYC,GAAZ,CAAkB,CAAG," +
157 "OAAOD,IAA9B;\",\n" +
158 "\"sources\":[\"testcode\"],\n" +
159 "\"names\":[\"f\",\"foo\",\"bar\"]\n" +
160 "}\n");
161
162 detailLevel = SourceMap.DetailLevel.SYMBOLS;
163
164 checkSourceMap("function f(foo, bar) { foo = foo + bar + 2; return foo; }",
165
166 "{\n" +
167 "\"version\":3,\n" +
168 "\"file\":\"testcode\",\n" +
169 "\"lineCount\":1,\n" +
170 "\"mappings\":\"AAAAA,QAASA,EAATA,CAAWC,GAAXD,CAAgBE," +
171 "GAAhBF,EAAuBC,GAAvBD,CAA6BC,GAA7BD,CAAmCE,GAAnCF," +
172 "SAAmDC,IAAnDD;\",\n" +
173 "\"sources\":[\"testcode\"],\n" +
174 "\"names\":[\"f\",\"foo\",\"bar\"]\n" +
175 "}\n");
176 }
177
178 public void testGoldenOutput2() throws Exception {
179 checkSourceMap("function f(foo, bar) {\r\n\n\n\nfoo = foo + bar + foo;" +
180 "\nreturn foo;\n}",
181
182 "{\n" +
183 "\"version\":3,\n" +
184 "\"file\":\"testcode\",\n" +
185 "\"lineCount\":1,\n" +
186 "\"mappings\":\"AAAAA,QAASA,EAAC,CAACC,GAAD,CAAMC,GAAN," +
187 "CAAW,CAIrBD,GAAA,CAAMA,GAAN,CAAYC,GAAZ,CAAkBD," +
188 "GAClB,OAAOA,IALc;\",\n" +
189 "\"sources\":[\"testcode\"],\n" +
190 "\"names\":[\"f\",\"foo\",\"bar\"]\n" +
191 "}\n");
192 }
193
194 public void testGoldenOutput3() throws Exception {
195 checkSourceMap("c:\\myfile.js",
196 "foo;",
197
198 "{\n" +
199 "\"version\":3,\n" +
200 "\"file\":\"testcode\",\n" +
201 "\"lineCount\":1,\n" +
202 "\"mappings\":\"AAAAA;\",\n" +
203 "\"sources\":[\"c:\\\\myfile.js\"],\n" +
204 "\"names\":[\"foo\"]\n" +
205 "}\n");
206 }
207
208 public void testGoldenOutput4() throws Exception {
209 checkSourceMap("c:\\myfile.js",
210 "foo; boo; goo;",
211
212 "{\n" +
213 "\"version\":3,\n" +
214 "\"file\":\"testcode\",\n" +
215 "\"lineCount\":1,\n" +
216 "\"mappings\":\"AAAAA,GAAOC,IAAOC;\",\n" +
217 "\"sources\":[\"c:\\\\myfile.js\"],\n" +
218 "\"names\":[\"foo\",\"boo\",\"goo\"]\n" +
219 "}\n");
220 }
221
222 public void testGoldenOutput5() throws Exception {
223 detailLevel = SourceMap.DetailLevel.ALL;
224
225 checkSourceMap(
226 "c:\\myfile.js",
227 "/** @preserve\n" +
228 " * this is a test.\n" +
229 " */\n" +
230 "var foo=a + 'this is a really long line that will force the"
231 + " mapping to span multiple lines 123456789 123456789"
232 + " 123456789 123456789 123456789 123456789 123456789"
233 + " 123456789 123456789 123456789 123456789 123456789"
234 + " 123456789 123456789 123456789 123456789 123456789"
235 + " 123456789 123456789 123456789 123456789 123456789"
236 + " 123456789 123456789 123456789 123456789 123456789"
237 + " 123456789 123456789 123456789 123456789 123456789"
238 + " 123456789 123456789 123456789 123456789 123456789"
239 + " 123456789 123456789 123456789 123456789 123456789"
240 + " 123456789 123456789 123456789 123456789 123456789"
241 + " 123456789 123456789 123456789 123456789 123456789"
242 + " 123456789 123456789 123456789 123456789 123456789"
243 + " 123456789 123456789 123456789 123456789 123456789"
244 + " 123456789 123456789 123456789 123456789 123456789"
245 + " 123456789 123456789 123456789 123456789 123456789"
246 + " 123456789 123456789 123456789 123456789 123456789"
247 + " 123456789 123456789 123456789 123456789 123456789"
248 + " 123456789 123456789 123456789 123456789 123456789"
249 + " 123456789 123456789 123456789 123456789 123456789"
250 + " 123456789 123456789 123456789 123456789 123456789"
251 + " 123456789 123456789 123456789 123456789 123456789"
252 + " 123456789 123456789 123456789 123456789 123456789"
253 + " 123456789 123456789 123456789 123456789 123456789"
254 + " 123456789 123456789 123456789 123456789 123456789"
255 + " 123456789 123456789 123456789 123456789 123456789"
256 + "' + c + d + e;",
257
258 "{\n" +
259 "\"version\":3,\n" +
260 "\"file\":\"testcode\",\n" +
261 "\"lineCount\":6,\n" +
262 "\"mappings\":\"A;;;;AAGA,IAAIA,IAAIC,CAAJD,CAAQ,mxCAARA;AAA8xCE," +
263 "CAA9xCF,CAAkyCG,CAAlyCH,CAAsyCI;\",\n" +
264 "\"sources\":[\"c:\\\\myfile.js\"],\n" +
265 "\"names\":[\"foo\",\"a\",\"c\",\"d\",\"e\"]\n" +
266 "}\n");
267
268 detailLevel = SourceMap.DetailLevel.SYMBOLS;
269
270 checkSourceMap("c:\\myfile.js",
271 "/** @preserve\n" +
272 " * this is a test.\n" +
273 " */\n" +
274 "var foo=a + 'this is a really long line that will force the"
275 + " mapping to span multiple lines 123456789 123456789"
276 + " 123456789 123456789 123456789 123456789 123456789"
277 + " 123456789 123456789 123456789 123456789 123456789"
278 + " 123456789 123456789 123456789 123456789 123456789"
279 + " 123456789 123456789 123456789 123456789 123456789"
280 + " 123456789 123456789 123456789 123456789 123456789"
281 + " 123456789 123456789 123456789 123456789 123456789"
282 + " 123456789 123456789 123456789 123456789 123456789"
283 + " 123456789 123456789 123456789 123456789 123456789"
284 + " 123456789 123456789 123456789 123456789 123456789"
285 + " 123456789 123456789 123456789 123456789 123456789"
286 + " 123456789 123456789 123456789 123456789 123456789"
287 + " 123456789 123456789 123456789 123456789 123456789"
288 + " 123456789 123456789 123456789 123456789 123456789"
289 + " 123456789 123456789 123456789 123456789 123456789"
290 + " 123456789 123456789 123456789 123456789 123456789"
291 + " 123456789 123456789 123456789 123456789 123456789"
292 + " 123456789 123456789 123456789 123456789 123456789"
293 + " 123456789 123456789 123456789 123456789 123456789"
294 + " 123456789 123456789 123456789 123456789 123456789"
295 + " 123456789 123456789 123456789 123456789 123456789"
296 + " 123456789 123456789 123456789 123456789 123456789"
297 + " 123456789 123456789 123456789 123456789 123456789"
298 + " 123456789 123456789 123456789 123456789 123456789"
299 + " 123456789 123456789 123456789 123456789 123456789"
300 + "' + c + d + e;",
301
302 "{\n" +
303 "\"version\":3,\n" +
304 "\"file\":\"testcode\",\n" +
305 "\"lineCount\":6,\n" +
306 "\"mappings\":\"A;;;;IAGIA,IAAIC,CAAJD;AAA8xCE,CAA9xCF,CAAkyCG," +
307 "CAAlyCH,CAAsyCI;\",\n" +
308 "\"sources\":[\"c:\\\\myfile.js\"],\n" +
309 "\"names\":[\"foo\",\"a\",\"c\",\"d\",\"e\"]\n" +
310 "}\n");
311 }
312
313 public void testBasicDeterminism() throws Exception {
314 RunResult result1 = compile("file1", "foo;", "file2", "bar;");
315 RunResult result2 = compile("file2", "foo;", "file1", "bar;");
316
317 String map1 = getSourceMap(result1);
318 String map2 = getSourceMap(result2);
319
320 // Assert that the files section of the maps are the same. The actual
321 // entries will differ, so we cannot do a simple full comparison.
322
323 // Line 5 has the file information.
324 String files1 = map1.split("\n")[4];
325 String files2 = map2.split("\n")[4];
326
327 assertEquals(files1, files2);
328 }
329
330 public void testWriteMetaMap() throws IOException {
331 StringWriter out = new StringWriter();
332 String name = "./app.js";
333 List<SourceMapSection> appSections = Lists.newArrayList(
334 SourceMapSection.forURL("src1", 0, 0),
335 SourceMapSection.forURL("src2", 100, 10),
336 SourceMapSection.forURL("src3", 150, 5));
337
338 SourceMapGeneratorV3 generator = new SourceMapGeneratorV3();
339 generator.appendIndexMapTo(out, name, appSections);
340
341 assertEquals(
342 "{\n" +
343 "\"version\":3,\n" +
344 "\"file\":\"./app.js\",\n" +
345 "\"sections\":[\n" +
346 "{\n" +
347 "\"offset\":{\n" +
348 "\"line\":0,\n" +
349 "\"column\":0\n" +
350 "},\n" +
351 "\"url\":\"src1\"\n" +
352 "},\n" +
353 "{\n" +
354 "\"offset\":{\n" +
355 "\"line\":100,\n" +
356 "\"column\":10\n" +
357 "},\n" +
358 "\"url\":\"src2\"\n" +
359 "},\n" +
360 "{\n" +
361 "\"offset\":{\n" +
362 "\"line\":150,\n" +
363 "\"column\":5\n" +
364 "},\n" +
365 "\"url\":\"src3\"\n" +
366 "}\n" +
367 "]\n" +
368 "}\n",
369 out.toString());
370 }
371
372 private String getEmptyMapFor(String name) throws IOException {
373 StringWriter out = new StringWriter();
374 SourceMapGeneratorV3 generator = new SourceMapGeneratorV3();
375 generator.appendTo(out, name);
376 return out.toString();
377 }
378
379 public void testWriteMetaMap2() throws IOException {
380 StringWriter out = new StringWriter();
381 String name = "./app.js";
382 List<SourceMapSection> appSections = Lists.newArrayList(
383 // Map and URLs can be mixed.
384 SourceMapSection.forMap(getEmptyMapFor("./part.js"), 0, 0),
385 SourceMapSection.forURL("src2", 100, 10));
386
387 SourceMapGeneratorV3 generator = new SourceMapGeneratorV3();
388 generator.appendIndexMapTo(out, name, appSections);
389
390 assertEquals(
391 "{\n" +
392 "\"version\":3,\n" +
393 "\"file\":\"./app.js\",\n" +
394 "\"sections\":[\n" +
395 "{\n" +
396 "\"offset\":{\n" +
397 "\"line\":0,\n" +
398 "\"column\":0\n" +
399 "},\n" +
400 "\"map\":{\n" +
401 "\"version\":3,\n" +
402 "\"file\":\"./part.js\",\n" +
403 "\"lineCount\":1,\n" +
404 "\"mappings\":\";\",\n" +
405 "\"sources\":[],\n" +
406 "\"names\":[]\n" +
407 "}\n" +
408 "\n" +
409 "},\n" +
410 "{\n" +
411 "\"offset\":{\n" +
412 "\"line\":100,\n" +
413 "\"column\":10\n" +
414 "},\n" +
415 "\"url\":\"src2\"\n" +
416 "}\n" +
417 "]\n" +
418 "}\n",
419 out.toString());
420 }
421
422 public void testParseSourceMetaMap() throws Exception {
423 final String INPUT1 = "file1";
424 final String INPUT2 = "file2";
425 LinkedHashMap<String, String> inputs = Maps.newLinkedHashMap();
426 inputs.put(INPUT1, "var __FOO__ = 1;");
427 inputs.put(INPUT2, "var __BAR__ = 2;");
428 RunResult result1 = compile(inputs.get(INPUT1), INPUT1);
429 RunResult result2 = compile(inputs.get(INPUT2), INPUT2);
430
431 final String MAP1 = "map1";
432 final String MAP2 = "map2";
433 final LinkedHashMap<String, String> maps = Maps.newLinkedHashMap();
434 maps.put(MAP1, result1.sourceMapFileContent);
435 maps.put(MAP2, result2.sourceMapFileContent);
436
437 List<SourceMapSection> sections = Lists.newArrayList();
438
439 StringBuilder output = new StringBuilder();
440 FilePosition offset = appendAndCount(output, result1.generatedSource);
441 sections.add(SourceMapSection.forURL(MAP1, 0, 0));
442 output.append(result2.generatedSource);
443 sections.add(
444 SourceMapSection.forURL(MAP2, offset.getLine(), offset.getColumn()));
445
446 SourceMapGeneratorV3 generator = new SourceMapGeneratorV3();
447 StringBuilder mapContents = new StringBuilder();
448 generator.appendIndexMapTo(mapContents, "out.js", sections);
449
450 check(inputs, output.toString(), mapContents.toString(),
451 new SourceMapSupplier() {
452 @Override
453 public String getSourceMap(String url){
454 return maps.get(url);
455 }});
456 }
457
458 public void testSourceMapMerging() throws Exception {
459 final String INPUT1 = "file1";
460 final String INPUT2 = "file2";
461 LinkedHashMap<String, String> inputs = Maps.newLinkedHashMap();
462 inputs.put(INPUT1, "var __FOO__ = 1;");
463 inputs.put(INPUT2, "var __BAR__ = 2;");
464 RunResult result1 = compile(inputs.get(INPUT1), INPUT1);
465 RunResult result2 = compile(inputs.get(INPUT2), INPUT2);
466
467 StringBuilder output = new StringBuilder();
468 FilePosition offset = appendAndCount(output, result1.generatedSource);
469 output.append(result2.generatedSource);
470
471 SourceMapGeneratorV3 generator = new SourceMapGeneratorV3();
472
473 generator.mergeMapSection(0, 0, result1.sourceMapFileContent);
474 generator.mergeMapSection(offset.getLine(), offset.getColumn(),
475 result2.sourceMapFileContent);
476
477 StringBuilder mapContents = new StringBuilder();
478 generator.appendTo(mapContents, "out.js");
479
480 check(inputs, output.toString(), mapContents.toString());
481 }
482
483 FilePosition count(String js) {
484 int line = 0, column = 0;
485 for (int i = 0; i < js.length(); i++) {
486 if (js.charAt(i) == '\n') {
487 line++;
488 column = 0;
489 } else {
490 column++;
491 }
492 }
493 return new FilePosition(line, column);
494 }
495
496 FilePosition appendAndCount(Appendable out, String js) throws IOException {
497 out.append(js);
498 return count(js);
499 }
500 }