Debian Codegraph @

test/com/google/javascript/jscomp/ReplaceIdGeneratorsTest.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.javascript.jscomp;
18
19 import com.google.common.collect.ImmutableSet;
20
21
22 /**
23 * Tests for {@link ReplaceIdGenerators}.
24 *
25 */
26 public class ReplaceIdGeneratorsTest extends CompilerTestCase {
27
28 private boolean generatePseudoNames = false;
29 private ReplaceIdGenerators lastPass = null;
30 private String previousMappings = null;
31
32 @Override
33 protected CompilerPass getProcessor(final Compiler compiler) {
34 lastPass = new ReplaceIdGenerators(
35 compiler,
36 new ImmutableSet.Builder<String>()
37 .add("goog.events.getUniqueId")
38 .add("goog.place.getUniqueId")
39 .build(),
40 generatePseudoNames,
41 previousMappings);
42 return lastPass;
43 }
44
45 @Override
46 protected void setUp() throws Exception {
47 super.setUp();
48 generatePseudoNames = false;
49 previousMappings = null;
50 }
51
52 @Override
53 protected int getNumRepetitions() {
54 return 1;
55 }
56
57 public void testBackwardCompat() {
58 test("foo.bar = goog.events.getUniqueId('foo_bar')",
59 "foo.bar = 'a'",
60 "foo.bar = 'foo_bar$0'");
61 }
62
63 public void testSerialization1() {
64 testMap("var x = goog.events.getUniqueId('xxx');\n" +
65 "var y = goog.events.getUniqueId('yyy');\n",
66
67 "var x = 'a';\n" +
68 "var y = 'b';\n",
69
70 "[goog.events.getUniqueId]\n" +
71 "\n" +
72 "a:testcode:1:32\n" +
73 "b:testcode:2:32\n" +
74 "\n");
75 }
76
77 public void testSerialization2() {
78 testMap("/** @consistentIdGenerator */ id = function() {};" +
79 "f1 = id('f1');" +
80 "f1 = id('f1')",
81
82 "id = function() {};" +
83 "f1 = 'a';" +
84 "f1 = 'a'",
85
86 "[id]\n" +
87 "\n" +
88 "a:f1\n" +
89 "\n");
90 }
91
92 public void testReusePreviousSerialization1() {
93 previousMappings =
94 "[goog.events.getUniqueId]\n" +
95 "\n" +
96 "previous1:testcode:1:32\n" +
97 "previous2:testcode:2:32\n" +
98 "\n" +
99 "[goog.place.getUniqueId]\n" +
100 "\n" +
101 "\n";
102 testMap("var x = goog.events.getUniqueId('xxx');\n" +
103 "var y = goog.events.getUniqueId('yyy');\n",
104
105 "var x = 'previous1';\n" +
106 "var y = 'previous2';\n",
107
108 "[goog.events.getUniqueId]\n" +
109 "\n" +
110 "previous1:testcode:1:32\n" +
111 "previous2:testcode:2:32\n" +
112 "\n");
113 }
114
115 public void testReusePreviousSerialization2() {
116 previousMappings =
117 "[goog.events.getUniqueId]\n" +
118 "\n" +
119 "a:testcode:1:32\n" +
120 "b:testcode:2:32\n" +
121 "\n" +
122 "[goog.place.getUniqueId]\n" +
123 "\n" +
124 "\n";
125 testMap(
126 "var x = goog.events.getUniqueId('xxx');\n" +
127 "\n" + // new line to change location
128 "var y = goog.events.getUniqueId('yyy');\n",
129
130 "var x = 'a';\n" +
131 "var y = 'c';\n",
132
133 "[goog.events.getUniqueId]\n" +
134 "\n" +
135 "a:testcode:1:32\n" +
136 "c:testcode:3:32\n" +
137 "\n");
138 }
139
140 public void testReusePreviousSerializationConsistent1() {
141 previousMappings =
142 "[id]\n" +
143 "\n" +
144 "a:f1\n" +
145 "\n";
146 testMap(
147 "/** @consistentIdGenerator */ id = function() {};" +
148 "f1 = id('f1');" +
149 "f1 = id('f1')",
150
151 "id = function() {};" +
152 "f1 = 'a';" +
153 "f1 = 'a'",
154
155 "[id]\n" +
156 "\n" +
157 "a:f1\n" +
158 "\n");
159 }
160
161 public void testSimple() {
162 test("/** @idGenerator */ foo.getUniqueId = function() {};" +
163 "foo.bar = foo.getUniqueId('foo_bar')",
164
165 "foo.getUniqueId = function() {};" +
166 "foo.bar = 'a'",
167
168 "foo.getUniqueId = function() {};" +
169 "foo.bar = 'foo_bar$0'");
170
171 test("/** @idGenerator */ goog.events.getUniqueId = function() {};" +
172 "foo1 = goog.events.getUniqueId('foo1');" +
173 "foo1 = goog.events.getUniqueId('foo1');",
174
175 "goog.events.getUniqueId = function() {};" +
176 "foo1 = 'a';" +
177 "foo1 = 'b';",
178
179 "goog.events.getUniqueId = function() {};" +
180 "foo1 = 'foo1$0';" +
181 "foo1 = 'foo1$1';");
182 }
183
184 public void testSimpleConsistent() {
185 test("/** @consistentIdGenerator */ id = function() {};" +
186 "foo.bar = id('foo_bar')",
187
188 "id = function() {};" +
189 "foo.bar = 'a'",
190
191 "id = function() {};" +
192 "foo.bar = 'foo_bar$0'");
193
194 test("/** @consistentIdGenerator */ id = function() {};" +
195 "f1 = id('f1');" +
196 "f1 = id('f1')",
197
198 "id = function() {};" +
199 "f1 = 'a';" +
200 "f1 = 'a'",
201
202 "id = function() {};" +
203 "f1 = 'f1$0';" +
204 "f1 = 'f1$0'");
205
206 test("/** @consistentIdGenerator */ id = function() {};" +
207 "f1 = id('f1');" +
208 "f1 = id('f1');" +
209 "f1 = id('f1')",
210
211 "id = function() {};" +
212 "f1 = 'a';" +
213 "f1 = 'a';" +
214 "f1 = 'a'",
215
216 "id = function() {};" +
217 "f1 = 'f1$0';" +
218 "f1 = 'f1$0';" +
219 "f1 = 'f1$0'");
220 }
221
222 public void testSimpleStable() {
223 testNonPseudoSupportingGenerator(
224 "/** @stableIdGenerator */ id = function() {};" +
225 "foo.bar = id('foo_bar')",
226
227 "id = function() {};" +
228 "foo.bar = '125lGg'");
229
230 testNonPseudoSupportingGenerator(
231 "/** @stableIdGenerator */ id = function() {};" +
232 "f1 = id('f1');" +
233 "f1 = id('f1')",
234
235 "id = function() {};" +
236 "f1 = 'AAAMiw';" +
237 "f1 = 'AAAMiw'");
238 }
239
240 public void testVar() {
241 test("/** @consistentIdGenerator */ var id = function() {};" +
242 "foo.bar = id('foo_bar')",
243
244 "var id = function() {};" +
245 "foo.bar = 'a'",
246
247 "var id = function() {};" +
248 "foo.bar = 'foo_bar$0'");
249
250 testNonPseudoSupportingGenerator(
251 "/** @stableIdGenerator */ var id = function() {};" +
252 "foo.bar = id('foo_bar')",
253
254 "var id = function() {};" +
255 "foo.bar = '125lGg'");
256 }
257
258 public void testObjLit() {
259 test("/** @consistentIdGenerator */ get.id = function() {};" +
260 "foo.bar = {a: get.id('foo_bar')}",
261
262 "get.id = function() {};" +
263 "foo.bar = {a: 'a'}",
264
265 "get.id = function() {};" +
266 "foo.bar = {a: 'foo_bar$0'}");
267
268 testNonPseudoSupportingGenerator(
269 "/** @stableIdGenerator */ get.id = function() {};" +
270 "foo.bar = {a: get.id('foo_bar')}",
271
272 "get.id = function() {};" +
273 "foo.bar = {a: '125lGg'}");
274 }
275
276 public void testTwoGenerators() {
277 test("/** @idGenerator */ var id1 = function() {};" +
278 "/** @idGenerator */ var id2 = function() {};" +
279 "f1 = id1('1');" +
280 "f2 = id1('1');" +
281 "f3 = id2('1');" +
282 "f4 = id2('1');",
283
284 "var id1 = function() {};" +
285 "var id2 = function() {};" +
286 "f1 = 'a';" +
287 "f2 = 'b';" +
288 "f3 = 'a';" +
289 "f4 = 'b';",
290
291 "var id1 = function() {};" +
292 "var id2 = function() {};" +
293 "f1 = '1$0';" +
294 "f2 = '1$1';" +
295 "f3 = '1$0';" +
296 "f4 = '1$1';");
297 }
298
299 public void testMixedGenerators() {
300 test("/** @idGenerator */ var id1 = function() {};" +
301 "/** @consistentIdGenerator */ var id2 = function() {};" +
302 "/** @stableIdGenerator */ var id3 = function() {};" +
303 "f1 = id1('1');" +
304 "f2 = id1('1');" +
305 "f3 = id2('1');" +
306 "f4 = id2('1');" +
307 "f5 = id3('1');" +
308 "f6 = id3('1');",
309
310 "var id1 = function() {};" +
311 "var id2 = function() {};" +
312 "var id3 = function() {};" +
313 "f1 = 'a';" +
314 "f2 = 'b';" +
315 "f3 = 'a';" +
316 "f4 = 'a';" +
317 "f5 = 'AAAAMQ';" +
318 "f6 = 'AAAAMQ';",
319
320 "var id1 = function() {};" +
321 "var id2 = function() {};" +
322 "var id3 = function() {};" +
323 "f1 = '1$0';" +
324 "f2 = '1$1';" +
325 "f3 = '1$0';" +
326 "f4 = '1$0';" +
327 "f5 = 'AAAAMQ';" +
328 "f6 = 'AAAAMQ';");
329 }
330
331 public void testLocalCall() {
332 testSame(new String[] {"/** @idGenerator */ var id = function() {}; " +
333 "function Foo() { id('foo'); }"},
334 ReplaceIdGenerators.NON_GLOBAL_ID_GENERATOR_CALL);
335 }
336
337 public void testConditionalCall() {
338 testSame(new String[] {"/** @idGenerator */ var id = function() {}; " +
339 "if(x) id('foo');"},
340 ReplaceIdGenerators.CONDITIONAL_ID_GENERATOR_CALL);
341
342 test("/** @consistentIdGenerator */ var id = function() {};" +
343 "function fb() {foo.bar = id('foo_bar')}",
344
345 "var id = function() {};" +
346 "function fb() {foo.bar = 'a'}",
347
348 "var id = function() {};" +
349 "function fb() {foo.bar = 'foo_bar$0'}");
350
351 testNonPseudoSupportingGenerator(
352 "/** @stableIdGenerator */ var id = function() {};" +
353 "function fb() {foo.bar = id('foo_bar')}",
354
355 "var id = function() {};" +
356 "function fb() {foo.bar = '125lGg'}");
357 }
358
359 public void testConflictingIdGenerator() {
360 testSame(new String[] {"/** @idGenerator \n @consistentIdGenerator \n*/" +
361 "var id = function() {}; "},
362 ReplaceIdGenerators.CONFLICTING_GENERATOR_TYPE);
363
364 testSame(new String[] {"/** @stableIdGenerator \n @idGenerator \n*/" +
365 "var id = function() {}; "},
366 ReplaceIdGenerators.CONFLICTING_GENERATOR_TYPE);
367
368 testSame(new String[] {"/** @stableIdGenerator \n @consistentIdGenerator \n*/" +
369 "var id = function() {}; "},
370 ReplaceIdGenerators.CONFLICTING_GENERATOR_TYPE);
371
372 test("/** @consistentIdGenerator */ var id = function() {};" +
373 "if (x) {foo.bar = id('foo_bar')}",
374
375 "var id = function() {};" +
376 "if (x) {foo.bar = 'a'}",
377
378 "var id = function() {};" +
379 "if (x) {foo.bar = 'foo_bar$0'}");
380 }
381
382 private void testMap(String code, String expected, String expectedMap) {
383 test(code, expected);
384 assertEquals(expectedMap, lastPass.getSerializedIdMappings());
385 }
386
387 private void test(String code, String expected, String expectedPseudo) {
388 generatePseudoNames = false;
389 test(code, expected);
390 generatePseudoNames = true;
391 test(code, expectedPseudo);
392 }
393
394 private void testNonPseudoSupportingGenerator(String code, String expected) {
395 generatePseudoNames = false;
396 test(code, expected);
397 generatePseudoNames = true;
398 test(code, expected);
399 }
400 }