Debian Codegraph @

test/com/google/javascript/jscomp/RenameLocalVarsTest.java

raw symbols SCIP
1 /*
2 * Copyright 2007 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 /**
20 * Tests for {@link RenameVars}.
21 * @see RenameVarsTest
22 */
23 public class RenameLocalVarsTest extends CompilerTestCase {
24 private static final String DEFAULT_PREFIX = "";
25
26 private String prefix = DEFAULT_PREFIX;
27
28 @Override
29 protected CompilerPass getProcessor(Compiler compiler) {
30 return new RenameVars(
31 compiler, prefix, true, false, false, false, null, null, null);
32 }
33
34 public void testRenameSimple() {
35 test("function Foo(v1, v2) {return v1;} Foo();",
36 "function Foo(a, b) {return a;} Foo();");
37 }
38
39 public void testRenameGlobals() {
40 testSame("var Foo; var Bar, y; function x() { Bar++; }");
41 }
42
43 public void testRenameLocals() {
44 test("(function (v1, v2) {}); (function (v3, v4) {});",
45 "(function (a, b) {}); (function (a, b) {});");
46 test("function f1(v1, v2) {}; function f2(v3, v4) {};",
47 "function f1(a, b) {}; function f2(a, b) {};");
48
49 }
50
51 public void testRenameLocalsClashingWithGlobals() {
52 test("function a(v1, v2) {return v1;} a();",
53 "function a(b, c) {return b;} a();");
54 }
55
56 public void testRenameNested() {
57 test("function f1(v1, v2) { (function(v3, v4) {}) }",
58 "function f1(a, b) { (function(c, d) {}) }");
59 test("function f1(v1, v2) { function f2(v3, v4) {} }",
60 "function f1(a, b) { function c(d, e) {} }");
61 }
62
63 public void testRenameWithExterns1() {
64 String externs = "var bar; function alert() {}";
65 test(externs,
66 "function foo(bar) { alert(bar); } foo(3)",
67 "function foo(a) { alert(a); } foo(3)", null, null);
68 }
69
70 public void testRenameWithExterns2() {
71 test("var a; function alert() {}",
72 "function foo(bar) { alert(a);alert(bar); } foo(3);",
73 "function foo(b) { alert(a);alert(b); } foo(3);",
74 null, null);
75 }
76
77 public void testDoNotRenameExportedName() {
78 test("_foo()", "_foo()");
79 }
80
81 public void testRenameWithNameOverlap() {
82 test("function local() { var a = 1; var b = 2; b + b; }",
83 "function local() { var b = 1; var a = 2; a + a; }");
84 }
85
86 public void testRenameWithPrefix1() {
87 prefix = "PRE_";
88 test("function Foo(v1, v2) {return v1} Foo();",
89 "function Foo(a, b) {return a} Foo();");
90 prefix = DEFAULT_PREFIX;
91 }
92
93 public void testRenameWithPrefix2() {
94 prefix = "PRE_";
95 test("function Foo(v1, v2) {var v3 = v1 + v2; return v3;} Foo();",
96 "function Foo(a, b) {var c = a + b; return c;} Foo();");
97 prefix = DEFAULT_PREFIX;
98 }
99
100 public void testRenameWithPrefix3() {
101 prefix = "a";
102 test("function Foo() {return 1;}" +
103 "function Bar() {" +
104 " var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z," +
105 " A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ab;" +
106 " Foo();" +
107 "} Bar();",
108
109 "function Foo() {return 1;}" +
110 "function Bar() {" +
111 " var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C," +
112 " D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,$,aa;" +
113 " Foo();" +
114 "} Bar();");
115 prefix = DEFAULT_PREFIX;
116 }
117 }