Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2005 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 public class RenamePrototypesTest extends CompilerTestCase {
20
21 private static final String EXTERNS = "var js_version;js_version.toString;";
22 private VariableMap prevUsedRenameMap;
23 private RenamePrototypes renamePrototypes;
24
25 public RenamePrototypesTest() {
26 super(EXTERNS);
27 enableNormalize();
28 }
29
30 @Override
31 public CompilerPass getProcessor(Compiler compiler) {
32 return renamePrototypes =
33 new RenamePrototypes(compiler, true, null, prevUsedRenameMap);
34 }
35
36 @Override
37 protected void tearDown() throws Exception {
38 super.tearDown();
39
40 prevUsedRenameMap = null;
41 }
42
43 @Override
44 protected int getNumRepetitions() {
45 // The RenamePrototypes pass should only be run once over a parse tree.
46 return 1;
47 }
48
49 public void testRenamePrototypes1() {
50 test("Bar.prototype={'getFoo':function(){},2:function(){}}",
51 "Bar.prototype={'a':function(){},2:function(){}}");
52 }
53
54 public void testRenamePrototypes2() {
55 // Simple
56 test("Bar.prototype.getFoo=function(){};Bar.getFoo(b);" +
57 "Bar.prototype.getBaz=function(){}",
58 "Bar.prototype.a=function(){};Bar.a(b);" +
59 "Bar.prototype.b=function(){}");
60 test("Bar.prototype['getFoo']=function(){};Bar.getFoo(b);" +
61 "Bar.prototype['getBaz']=function(){}",
62 "Bar.prototype['a']=function(){};Bar.a(b);" +
63 "Bar.prototype['b']=function(){}");
64 test("Bar.prototype={'getFoo':function(){},2:function(){}}",
65 "Bar.prototype={'a':function(){},2:function(){}}");
66 test("Bar.prototype={'getFoo':function(){}," +
67 "'getBar':function(){}};b.getFoo()",
68 "Bar.prototype={'a':function(){}," +
69 "'b':function(){}};b.a()");
70
71 test("Bar.prototype={'B':function(){}," +
72 "'getBar':function(){}};b.getBar()",
73 "Bar.prototype={'b':function(){}," +
74 "'a':function(){}};b.a()");
75
76 // overlap
77 test("Bar.prototype={'a':function(){}," +
78 "'b':function(){}};b.b()",
79 "Bar.prototype={'b':function(){}," +
80 "'a':function(){}};b.a()");
81
82 // don't rename anything with a leading underscore
83 test("Bar.prototype={'_getFoo':function(){}," +
84 "'getBar':function(){}};b._getFoo()",
85 "Bar.prototype={'_getFoo':function(){}," +
86 "'a':function(){}};b._getFoo()");
87
88 // Externed methods
89 test("Bar.prototype={'toString':function(){}," +
90 "'getBar':function(){}};b.toString()",
91 "Bar.prototype={'toString':function(){}," +
92 "'a':function(){}};b.toString()");
93
94 // don't rename a method to an existing (unrenamed) property
95 test("Bar.prototype.foo=function(){}" +
96 ";bar.foo();bar.a",
97 "Bar.prototype.b=function(){}" +
98 ";bar.b();bar.a");
99 }
100
101 public void testRenamePrototypesWithGetOrSet() {
102 // Simple
103 // TODO(johnlenz): Enable these for after Rhino support is added.
104 // test("Bar.prototype={get 'getFoo'(){}}",
105 // "Bar.prototype={get a(){}}");
106 // test("Bar.prototype={get 2(){}}",
107 // "Bar.prototype={get 2(){}}");
108 test("Bar.prototype={get getFoo(){}}",
109 "Bar.prototype={get a(){}}");
110 test("Bar.prototype={get getFoo(){}}; a.getFoo;",
111 "Bar.prototype={get a(){}}; a.a;");
112
113 // TODO(johnlenz): Enable these for after Rhino support is added.
114 // test("Bar.prototype={set 'getFoo'(x){}}",
115 // "Bar.prototype={set a(x){}}");
116 // test("Bar.prototype={set 2(x){}}",
117 // "Bar.prototype={set 2(x){}}");
118 test("Bar.prototype={set getFoo(x){}}",
119 "Bar.prototype={set a(x){}}");
120 test("Bar.prototype={set getFoo(x){}}; a.getFoo;",
121 "Bar.prototype={set a(x){}}; a.a;");
122
123 // overlap
124 test("Bar.prototype={get a(){}," +
125 "get b(){}};b.b()",
126 "Bar.prototype={get b(){}," +
127 "get a(){}};b.a()");
128 }
129
130 /**
131 * Test renaming private properties (end with underscores) and test to make
132 * sure we don't rename other properties.
133 */
134 public void testRenameProperties() {
135 test("var foo; foo.prop_='bar'", "var foo;foo.a='bar'");
136 test("this.prop_='bar'", "this.a='bar'");
137 test("this.prop='bar'", "this.prop='bar'");
138 test("this['prop_']='bar'", "this['a']='bar'");
139 test("this['prop']='bar'", "this['prop']='bar'");
140 test("var foo={prop1_: 'bar',prop2_: 'baz'};",
141 "var foo={a:'bar',b:'baz'}");
142 }
143
144 /**
145 * Tests a potential tricky interaction between prototype renaming and
146 * property renaming.
147 */
148 public void testBoth() {
149 test("Bar.prototype.getFoo_=function(){};Bar.getFoo_(b);" +
150 "Bar.prototype.getBaz_=function(){}",
151 "Bar.prototype.a=function(){};Bar.a(b);" +
152 "Bar.prototype.b=function(){}");
153 }
154
155 public void testPropertyNameThatIsBothObjLitKeyAndPrototypeProperty() {
156 // This test protects against regression of a bug where non-private object
157 // literal keys were getting renamed if they clashed with custom prototype
158 // methods. Now we don't simply don't rename in this situation, since
159 // references like z.myprop are ambiguous.
160 test("x.prototype.myprop=function(){};y={myprop:0};z.myprop",
161 "x.prototype.myprop=function(){};y={myprop:0};z.myprop");
162
163 // This test shows that a property can be renamed if both the prototype
164 // property renaming policy and the objlit key renaming policy agree that
165 // it can be renamed.
166 test("x.prototype.myprop_=function(){};y={myprop_:0};z.myprop_",
167 "x.prototype.a=function(){};y={a:0};z.a");
168 }
169
170 public void testModule() {
171 JSModule[] modules = createModules(
172 "function Bar(){} var foo; Bar.prototype.getFoo_=function(x){};" +
173 "foo.getFoo_(foo);foo.doo_=foo;foo.bloo_=foo;",
174 "function Far(){} var too; Far.prototype.getGoo_=function(x){};" +
175 "too.getGoo_(too);too.troo_=too;too.bloo_=too;");
176
177 test(modules, new String[] {
178 "function Bar(){}var foo; Bar.prototype.a=function(x){};" +
179 "foo.a(foo);foo.d=foo;foo.c=foo;",
180 "function Far(){}var too; Far.prototype.b=function(x){};" +
181 "too.b(too);too.e=too;too.c=too;"
182 });
183 }
184
185 public void testStableSimple1() {
186 testStable(
187 "Bar.prototype.getFoo=function(){};Bar.getFoo(b);" +
188 "Bar.prototype.getBaz=function(){}",
189 "Bar.prototype.a=function(){};Bar.a(b);" +
190 "Bar.prototype.b=function(){}",
191 "Bar.prototype.getBar=function(){};Bar.getBar(b);" +
192 "Bar.prototype.getFoo=function(){};Bar.getFoo(b);" +
193 "Bar.prototype.getBaz=function(){}",
194 "Bar.prototype.c=function(){};Bar.c(b);" +
195 "Bar.prototype.a=function(){};Bar.a(b);" +
196 "Bar.prototype.b=function(){}");
197 }
198
199 public void testStableSimple2() {
200 testStable(
201 "Bar.prototype['getFoo']=function(){};Bar.getFoo(b);" +
202 "Bar.prototype['getBaz']=function(){}",
203 "Bar.prototype['a']=function(){};Bar.a(b);" +
204 "Bar.prototype['b']=function(){}",
205 "Bar.prototype['getFoo']=function(){};Bar.getFoo(b);" +
206 "Bar.prototype['getBar']=function(){};" +
207 "Bar.prototype['getBaz']=function(){}",
208 "Bar.prototype['a']=function(){};Bar.a(b);" +
209 "Bar.prototype['c']=function(){};" +
210 "Bar.prototype['b']=function(){}");
211 }
212
213 public void testStableSimple3() {
214 testStable(
215 "Bar.prototype={'getFoo':function(){}," +
216 "'getBar':function(){}};b.getFoo()",
217 "Bar.prototype={'a':function(){}, 'b':function(){}};b.a()",
218 "Bar.prototype={'getFoo':function(){}," +
219 "'getBaz':function(){},'getBar':function(){}};b.getFoo()",
220 "Bar.prototype={'a':function(){}, " +
221 "'c':function(){}, 'b':function(){}};b.a()");
222 }
223
224 public void testStableOverlap() {
225 testStable(
226 "Bar.prototype={'a':function(){},'b':function(){}};b.b()",
227 "Bar.prototype={'b':function(){},'a':function(){}};b.a()",
228 "Bar.prototype={'a':function(){},'b':function(){}};b.b()",
229 "Bar.prototype={'b':function(){},'a':function(){}};b.a()");
230 }
231
232 public void testStableTrickyExternedMethods() {
233 test("Bar.prototype={'toString':function(){}," +
234 "'getBar':function(){}};b.toString()",
235 "Bar.prototype={'toString':function(){}," +
236 "'a':function(){}};b.toString()");
237 prevUsedRenameMap = renamePrototypes.getPropertyMap();
238 String externs = EXTERNS + "prop.a;";
239 test(externs,
240 "Bar.prototype={'toString':function(){}," +
241 "'getBar':function(){}};b.toString()",
242 "Bar.prototype={'toString':function(){}," +
243 "'b':function(){}};b.toString()", null, null);
244 }
245
246 public void testStable(String input1, String expected1,
247 String input2, String expected2) {
248 test(input1, expected1);
249 prevUsedRenameMap = renamePrototypes.getPropertyMap();
250 test(input2, expected2);
251 }
252 }