| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 77 |
test("Bar.prototype={'a':function(){}," + |
| 78 |
"'b':function(){}};b.b()", |
| 79 |
"Bar.prototype={'b':function(){}," + |
| 80 |
"'a':function(){}};b.a()"); |
| 81 |
|
| 82 |
|
| 83 |
test("Bar.prototype={'_getFoo':function(){}," + |
| 84 |
"'getBar':function(){}};b._getFoo()", |
| 85 |
"Bar.prototype={'_getFoo':function(){}," + |
| 86 |
"'a':function(){}};b._getFoo()"); |
| 87 |
|
| 88 |
|
| 89 |
test("Bar.prototype={'toString':function(){}," + |
| 90 |
"'getBar':function(){}};b.toString()", |
| 91 |
"Bar.prototype={'toString':function(){}," + |
| 92 |
"'a':function(){}};b.toString()"); |
| 93 |
|
| 94 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 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 |
|
| 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 |
|
| 132 |
|
| 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 |
|
| 146 |
|
| 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 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
test("x.prototype.myprop=function(){};y={myprop:0};z.myprop", |
| 161 |
"x.prototype.myprop=function(){};y={myprop:0};z.myprop"); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 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 |
} |