Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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.debugging.sourcemap.SourceMapGeneratorV2.LineMapEncoder;
20 import com.google.javascript.jscomp.SourceMap;
21 import com.google.javascript.jscomp.SourceMap.Format;
22
23 import java.io.IOException;
24
25 /**
26 * Tests for {@link SourceMap}.
27 *
28 */
29 public class SourceMapGeneratorV2Test extends SourceMapTestCase {
30
31 public SourceMapGeneratorV2Test() {
32 disableColumnValidation();
33 }
34
35 @Override
36 protected SourceMapConsumer getSourceMapConsumer() {
37 return new SourceMapConsumerV2();
38 }
39
40 @Override
41 protected Format getSourceMapFormat() {
42 return SourceMap.Format.V2;
43 }
44
45 @Override
46 public void setUp() {
47 detailLevel = SourceMap.DetailLevel.ALL;
48 }
49
50 public void testBasicMapping() throws Exception {
51 compileAndCheck("function __BASIC__() { }");
52 }
53
54 public void testBasicMappingGoldenOutput() throws Exception {
55 // Empty source map test
56 checkSourceMap("function __BASIC__() { }",
57
58 //"/** Source Map **/\n" +
59 "{\n" +
60 "\"version\":2,\n" +
61 "\"file\":\"testcode\",\n" +
62 "\"lineCount\":1,\n" +
63 "\"lineMaps\":[\"cAkBEBEB\"],\n" +
64 "\"mappings\":[[0,1,0,0],\n" +
65 "[0,1,9,0],\n" +
66 "[0,1,18],\n" +
67 "[0,1,21],\n" +
68 "],\n" +
69 "\"sources\":[\"testcode\"],\n" +
70 "\"names\":[\"__BASIC__\"]\n" +
71 "}\n");
72 }
73
74 public void testLiteralMappings() throws Exception {
75 compileAndCheck("function __BASIC__(__PARAM1__, __PARAM2__) { " +
76 "var __VAR__ = '__STR__'; }");
77 }
78
79 public void testLiteralMappingsGoldenOutput() throws Exception {
80 // Empty source map test
81 checkSourceMap("function __BASIC__(__PARAM1__, __PARAM2__) { " +
82 "var __VAR__ = '__STR__'; }",
83
84 //"/** Source Map **/\n" +
85 "{\n" +
86 "\"version\":2,\n" +
87 "\"file\":\"testcode\",\n" +
88 "\"lineCount\":1,\n" +
89 "\"lineMaps\":[\"cAkBABkBA/kCA+ADMBcBgBA9\"],\n" +
90 "\"mappings\":[[0,1,0,0],\n" +
91 "[0,1,9,0],\n" +
92 "[0,1,18],\n" +
93 "[0,1,19,1],\n" +
94 "[0,1,31,2],\n" +
95 "[0,1,43],\n" +
96 "[0,1,45],\n" +
97 "[0,1,49,3],\n" +
98 "[0,1,59],\n" +
99 "],\n" +
100 "\"sources\":[\"testcode\"],\n" +
101 "\"names\":[" +
102 "\"__BASIC__\",\"__PARAM1__\",\"__PARAM2__\"," +
103 "\"__VAR__\"]\n" +
104 "}\n");
105 }
106
107 public void testMultilineMapping() throws Exception {
108 compileAndCheck("function __BASIC__(__PARAM1__, __PARAM2__) {\n" +
109 "var __VAR__ = '__STR__';\n" +
110 "var __ANO__ = \"__STR2__\";\n" +
111 "}");
112 }
113
114 public void testMultiFunctionMapping() throws Exception {
115 compileAndCheck("function __BASIC__(__PARAM1__, __PARAM2__) {\n" +
116 "var __VAR__ = '__STR__';\n" +
117 "var __ANO__ = \"__STR2__\";\n" +
118 "}\n\n" +
119
120 "function __BASIC2__(__PARAM3__, __PARAM4__) {\n" +
121 "var __VAR2__ = '__STR2__';\n" +
122 "var __ANO2__ = \"__STR3__\";\n" +
123 "}\n\n");
124 }
125
126 public void testGoldenOutput0() throws Exception {
127 // Empty source map test
128 checkSourceMap("",
129
130 "{\n" +
131 "\"version\":2,\n" +
132 "\"file\":\"testcode\",\n" +
133 "\"lineCount\":1,\n" +
134 "\"lineMaps\":[\"\"],\n" +
135 "\"mappings\":[],\n" +
136 "\"sources\":[\"testcode\"],\n" +
137 "\"names\":[]\n" +
138 "}\n");
139 }
140
141 public void testGoldenOutput1() throws Exception {
142 detailLevel = SourceMap.DetailLevel.ALL;
143
144 checkSourceMap(
145 "function f(foo, bar) { foo = foo + bar + 2; return foo; }",
146
147 "{\n" +
148 "\"version\":2,\n" +
149 "\"file\":\"testcode\",\n" +
150 "\"lineCount\":1,\n" +
151 "\"lineMaps\":[\"cAEBABIBA/ICA+ADICA/ICA+IDA9AEYBMBA5\"],\n" +
152 "\"mappings\":[[0,1,0,0],\n" +
153 "[0,1,9,0],\n" +
154 "[0,1,10],\n" +
155 "[0,1,11,1],\n" +
156 "[0,1,16,2],\n" +
157 "[0,1,21],\n" +
158 "[0,1,23],\n" +
159 "[0,1,23,1],\n" +
160 "[0,1,29,1],\n" +
161 "[0,1,35,2],\n" +
162 "[0,1,41],\n" +
163 "[0,1,44],\n" +
164 "[0,1,51,1],\n" +
165 "],\n" +
166 "\"sources\":[\"testcode\"],\n" +
167 "\"names\":[\"f\",\"foo\",\"bar\"]\n" +
168 "}\n");
169
170 detailLevel = SourceMap.DetailLevel.SYMBOLS;
171
172 checkSourceMap("function f(foo, bar) { foo = foo + bar + 2; return foo; }",
173
174 "{\n" +
175 "\"version\":2,\n" +
176 "\"file\":\"testcode\",\n" +
177 "\"lineCount\":1,\n" +
178 "\"lineMaps\":[\"cAEBA/ICA+IDE9IEA8IFA7IGg6MHA5\"],\n" +
179 "\"mappings\":[[0,1,0,0],\n" +
180 "[0,1,9,0],\n" +
181 "[0,1,11,1],\n" +
182 "[0,1,16,2],\n" +
183 "[0,1,23,1],\n" +
184 "[0,1,29,1],\n" +
185 "[0,1,35,2],\n" +
186 "[0,1,51,1],\n" +
187 "],\n" +
188 "\"sources\":[\"testcode\"],\n" +
189 "\"names\":[\"f\",\"foo\",\"bar\"]\n" +
190 "}\n");
191 }
192
193 public void testGoldenOutput2() throws Exception {
194 checkSourceMap("function f(foo, bar) {\r\n\n\n\nfoo = foo + bar + foo;" +
195 "\nreturn foo;\n}",
196
197 "{\n" +
198 "\"version\":2,\n" +
199 "\"file\":\"testcode\",\n" +
200 "\"lineCount\":1,\n" +
201 "\"lineMaps\":[" +
202 "\"cAEBABIBA/ICA+ADICA/ICA+IDA9IEYBMBA5\"],\n" +
203 "\"mappings\":[[0,1,0,0],\n" +
204 "[0,1,9,0],\n" +
205 "[0,1,10],\n" +
206 "[0,1,11,1],\n" +
207 "[0,1,16,2],\n" +
208 "[0,1,21],\n" +
209 "[0,5,0],\n" +
210 "[0,5,0,1],\n" +
211 "[0,5,6,1],\n" +
212 "[0,5,12,2],\n" +
213 "[0,5,18,1],\n" +
214 "[0,6,0],\n" +
215 "[0,6,7,1],\n" +
216 "],\n" +
217 "\"sources\":[\"testcode\"],\n" +
218 "\"names\":[\"f\",\"foo\",\"bar\"]\n" +
219 "}\n");
220 }
221
222 public void testGoldenOutput3() throws Exception {
223 checkSourceMap("c:\\myfile.js",
224 "foo;",
225
226 "{\n" +
227 "\"version\":2,\n" +
228 "\"file\":\"testcode\",\n" +
229 "\"lineCount\":1,\n" +
230 "\"lineMaps\":[\"IA\"],\n" +
231 "\"mappings\":[[0,1,0,0],\n" +
232 "],\n" +
233 "\"sources\":[\"c:\\\\myfile.js\"],\n" +
234 "\"names\":[\"foo\"]\n" +
235 "}\n");
236 }
237
238 public void testGoldenOutput4() throws Exception {
239 checkSourceMap("c:\\myfile.js",
240 "foo; boo; goo;",
241
242 "{\n" +
243 "\"version\":2,\n" +
244 "\"file\":\"testcode\",\n" +
245 "\"lineCount\":1,\n" +
246 "\"lineMaps\":[\"IAMBMB\"],\n" +
247 "\"mappings\":[[0,1,0,0],\n" +
248 "[0,1,7,1],\n" +
249 "[0,1,14,2],\n" +
250 "],\n" +
251 "\"sources\":[\"c:\\\\myfile.js\"],\n" +
252 "\"names\":[\"foo\",\"boo\",\"goo\"]\n" +
253 "}\n");
254 }
255
256 public void testGoldenOutput5() throws Exception {
257 detailLevel = SourceMap.DetailLevel.ALL;
258
259 checkSourceMap("c:\\myfile.js",
260 "/** @preserve\n" +
261 " * this is a test.\n" +
262 " */\n" +
263 "var foo=a + 'this is a really long line that will force the"
264 + " mapping to span multiple lines 123456789 123456789"
265 + " 123456789 123456789 123456789 123456789 123456789"
266 + " 123456789 123456789 123456789 123456789 123456789"
267 + " 123456789 123456789 123456789 123456789 123456789"
268 + " 123456789 123456789 123456789 123456789 123456789"
269 + " 123456789 123456789 123456789 123456789 123456789"
270 + " 123456789 123456789 123456789 123456789 123456789"
271 + " 123456789 123456789 123456789 123456789 123456789"
272 + " 123456789 123456789 123456789 123456789 123456789"
273 + " 123456789 123456789 123456789 123456789 123456789"
274 + " 123456789 123456789 123456789 123456789 123456789"
275 + " 123456789 123456789 123456789 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 + "' + c + d + e;",
290
291 "{\n" +
292 "\"version\":2,\n" +
293 "\"file\":\"testcode\",\n" +
294 "\"lineCount\":6,\n" +
295 "\"lineMaps\":[\"\",\n" +
296 "\"\",\n" +
297 "\"\",\n" +
298 "\"\",\n" +
299 "\"MAMBABA/!!AUSC\",\n" +
300 "\"AEA9AEA8AF\"],\n" +
301 "\"mappings\":[[0,4,0],\n" +
302 "[0,4,4,0],\n" +
303 "[0,4,8,1],\n" +
304 "[0,4,12],\n" +
305 "[0,4,1314,2],\n" +
306 "[0,4,1318,3],\n" +
307 "[0,4,1322,4],\n" +
308 "],\n" +
309 "\"sources\":[\"c:\\\\myfile.js\"],\n" +
310 "\"names\":[\"foo\",\"a\",\"c\",\"d\",\"e\"]\n" +
311 "}\n");
312
313 detailLevel = SourceMap.DetailLevel.SYMBOLS;
314
315 checkSourceMap("c:\\myfile.js",
316 "/** @preserve\n" +
317 " * this is a test.\n" +
318 " */\n" +
319 "var foo=a + 'this is a really long line that will force the"
320 + " mapping to span multiple lines 123456789 123456789"
321 + " 123456789 123456789 123456789 123456789 123456789"
322 + " 123456789 123456789 123456789 123456789 123456789"
323 + " 123456789 123456789 123456789 123456789 123456789"
324 + " 123456789 123456789 123456789 123456789 123456789"
325 + " 123456789 123456789 123456789 123456789 123456789"
326 + " 123456789 123456789 123456789 123456789 123456789"
327 + " 123456789 123456789 123456789 123456789 123456789"
328 + " 123456789 123456789 123456789 123456789 123456789"
329 + " 123456789 123456789 123456789 123456789 123456789"
330 + " 123456789 123456789 123456789 123456789 123456789"
331 + " 123456789 123456789 123456789 123456789 123456789"
332 + " 123456789 123456789 123456789 123456789 123456789"
333 + " 123456789 123456789 123456789 123456789 123456789"
334 + " 123456789 123456789 123456789 123456789 123456789"
335 + " 123456789 123456789 123456789 123456789 123456789"
336 + " 123456789 123456789 123456789 123456789 123456789"
337 + " 123456789 123456789 123456789 123456789 123456789"
338 + " 123456789 123456789 123456789 123456789 123456789"
339 + " 123456789 123456789 123456789 123456789 123456789"
340 + " 123456789 123456789 123456789 123456789 123456789"
341 + " 123456789 123456789 123456789 123456789 123456789"
342 + " 123456789 123456789 123456789 123456789 123456789"
343 + " 123456789 123456789 123456789 123456789 123456789"
344 + " 123456789 123456789 123456789 123456789 123456789"
345 + "' + c + d + e;",
346
347 "{\n" +
348 "\"version\":2,\n" +
349 "\"file\":\"testcode\",\n" +
350 "\"lineCount\":6,\n" +
351 "\"lineMaps\":[\"\",\n" +
352 "\"\",\n" +
353 "\"\",\n" +
354 "\"\",\n" +
355 "\"M/MBAB\",\n" +
356 "\"ACA+ADA9AE\"],\n" +
357 "\"mappings\":[[0,4,4,0],\n" +
358 "[0,4,8,1],\n" +
359 "[0,4,1314,2],\n" +
360 "[0,4,1318,3],\n" +
361 "[0,4,1322,4],\n" +
362 "],\n" +
363 "\"sources\":[\"c:\\\\myfile.js\"],\n" +
364 "\"names\":[\"foo\",\"a\",\"c\",\"d\",\"e\"]\n" +
365 "}\n");
366 }
367
368 public void testBasicDeterminism() throws Exception {
369 RunResult result1 = compile("file1", "foo;", "file2", "bar;");
370 RunResult result2 = compile("file2", "foo;", "file1", "bar;");
371
372 String map1 = getSourceMap(result1);
373 String map2 = getSourceMap(result2);
374
375 // Assert that the files section of the maps are the same. The actual
376 // entries will differ, so we cannot do a simple full comparison.
377
378 // Line 5 has the file information.
379 String files1 = map1.split("\n")[4];
380 String files2 = map2.split("\n")[4];
381
382 assertEquals(files1, files2);
383 }
384
385 private int getRelativeId(int id, int lastId) {
386 int length = LineMapEncoder.getRelativeMappingIdLength(id, lastId);
387 int result = LineMapEncoder.getRelativeMappingId(id, length, lastId);
388 int inverse = SourceMapLineDecoder.getIdFromRelativeId(
389 result, length, lastId);
390 assertEquals(id, inverse);
391 return result;
392 }
393
394 public void testEncodingRelativeId() {
395 assertEquals(0, getRelativeId(0, 0));
396 assertEquals(64 + (-1), getRelativeId(-1, 0));
397 assertEquals(64 + (-32), getRelativeId(0, 32));
398 assertEquals(31, getRelativeId(31, 0));
399 assertEquals(4096 + (-33), getRelativeId(0, 33));
400 assertEquals(32, getRelativeId(32, 0));
401 }
402
403 public void testEncodingIdLength() {
404 assertEquals(1, LineMapEncoder.getRelativeMappingIdLength(0, 0));
405 assertEquals(1, LineMapEncoder.getRelativeMappingIdLength(-1, 0));
406 assertEquals(1, LineMapEncoder.getRelativeMappingIdLength(0, 32));
407 assertEquals(1, LineMapEncoder.getRelativeMappingIdLength(31, 0));
408 assertEquals(2, LineMapEncoder.getRelativeMappingIdLength(0, 33));
409 assertEquals(2, LineMapEncoder.getRelativeMappingIdLength(32, 0));
410
411 assertEquals(2, LineMapEncoder.getRelativeMappingIdLength(2047, 0));
412 assertEquals(3, LineMapEncoder.getRelativeMappingIdLength(2048, 0));
413 assertEquals(2, LineMapEncoder.getRelativeMappingIdLength(0, 2048));
414 assertEquals(3, LineMapEncoder.getRelativeMappingIdLength(0, 2049));
415 }
416
417 private String getEntry(int id, int lastId, int reps) throws IOException {
418 StringBuilder sb = new StringBuilder();
419 LineMapEncoder.encodeEntry(sb, id, lastId, reps);
420 return sb.toString();
421 }
422
423 public void testEncoding() throws IOException {
424 assertEquals("AA", getEntry(0, 0, 1));
425 assertEquals("EA", getEntry(0, 0, 2));
426 assertEquals("8A", getEntry(0, 0, 16));
427 assertEquals("!AQA", getEntry(0, 0, 17));
428 assertEquals("!ARA", getEntry(0, 0, 18));
429 assertEquals("!A+A", getEntry(0, 0, 63));
430 assertEquals("!A/A", getEntry(0, 0, 64));
431 assertEquals("!!ABAA", getEntry(0, 0, 65));
432 assertEquals("!!A//A", getEntry(0, 0, 4096));
433 assertEquals("!!!ABAAA", getEntry(0, 0, 4097));
434
435 assertEquals("Af", getEntry(31, 0, 1));
436 assertEquals("BAg", getEntry(32, 0, 1));
437 assertEquals("AB", getEntry(32, 31, 1));
438
439 assertEquals("!AQf", getEntry(31, 0, 17));
440 assertEquals("!BQAg", getEntry(32, 0, 17));
441 assertEquals("!AQB", getEntry(32, 31, 17));
442
443 assertEquals("!A/B", getEntry(32, 31, 64));
444 assertEquals("!!ABAB", getEntry(32, 31, 65));
445 }
446 }