| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.collect.ImmutableSet; |
| 20 |
|
| 21 |
import java.util.*; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class AliasStringsTest extends CompilerTestCase { |
| 28 |
|
| 29 |
private static final String EXTERNS = "alert"; |
| 30 |
private static final Set<String> ALL_STRINGS = null; |
| 31 |
|
| 32 |
private Set<String> strings = ALL_STRINGS; |
| 33 |
private JSModuleGraph moduleGraph = null; |
| 34 |
private boolean hashReduction = false; |
| 35 |
|
| 36 |
public AliasStringsTest() { |
| 37 |
super(EXTERNS); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public void setUp() { |
| 42 |
super.enableLineNumberCheck(false); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public CompilerPass getProcessor(Compiler compiler) { |
| 47 |
AliasStrings pass = |
| 48 |
new AliasStrings(compiler, moduleGraph, strings, "(?i)secret", false); |
| 49 |
if (hashReduction) |
| 50 |
pass.unitTestHashReductionMask = 0; |
| 51 |
return pass; |
| 52 |
} |
| 53 |
|
| 54 |
public void testAssignment() { |
| 55 |
strings = ImmutableSet.of("none", "width", "overimaginative"); |
| 56 |
|
| 57 |
|
| 58 |
testSame("var foo='foo'"); |
| 59 |
testSame("a='titanium',b='titanium',c='titanium',d='titanium'"); |
| 60 |
|
| 61 |
|
| 62 |
testSame("myStr='width'"); |
| 63 |
testSame("Bar.prototype.start='none'"); |
| 64 |
|
| 65 |
test("a='overimaginative';b='overimaginative'", |
| 66 |
"var $$S_overimaginative='overimaginative';" + |
| 67 |
"a=$$S_overimaginative;b=$$S_overimaginative"); |
| 68 |
|
| 69 |
testSame("var width=1234"); |
| 70 |
testSame("width=1234;width=10000;width=9900;width=17;"); |
| 71 |
} |
| 72 |
|
| 73 |
public void testSeveral() { |
| 74 |
strings = ImmutableSet.of("", "px", "none", "width"); |
| 75 |
|
| 76 |
test("function f() {" + |
| 77 |
"var styles=['width',100,'px','display','none'].join('')}", |
| 78 |
"var $$S_='';" + |
| 79 |
"var $$S_none='none';" + |
| 80 |
"var $$S_px='px';" + |
| 81 |
"var $$S_width='width';" + |
| 82 |
"function f() {var styles=[$$S_width,100,$$S_px,'display'," + |
| 83 |
"$$S_none].join($$S_)}"); |
| 84 |
} |
| 85 |
|
| 86 |
public void testSortedOutput() { |
| 87 |
strings = ImmutableSet.of("aba", "aaa", "aca", "bca", "bba"); |
| 88 |
test("function f() {return ['aba', 'aaa', 'aca', 'bca', 'bba']}", |
| 89 |
"var $$S_aaa='aaa';" + |
| 90 |
"var $$S_aba='aba';" + |
| 91 |
"var $$S_aca='aca';" + |
| 92 |
"var $$S_bba='bba';" + |
| 93 |
"var $$S_bca='bca';" + |
| 94 |
"function f() {" + |
| 95 |
" return [$$S_aba, $$S_aaa, $$S_aca, $$S_bca, $$S_bba]}"); |
| 96 |
} |
| 97 |
|
| 98 |
public void testObjectLiterals() { |
| 99 |
strings = ImmutableSet.of("px", "!@#$%^&*()"); |
| 100 |
|
| 101 |
test("var foo={px:435}", "var foo={px:435}"); |
| 102 |
|
| 103 |
|
| 104 |
test("var foo={'px':435}", "var foo={'px':435}"); |
| 105 |
test("bar=function f(){return {'px':435}}", |
| 106 |
"bar=function f(){return {'px':435}}"); |
| 107 |
|
| 108 |
test("function f() {var foo={bar:'!@#$%^&*()'}}", |
| 109 |
"var $$S_$21$40$23$24$25$5e$26$2a$28$29='!@#$%^&*()';" + |
| 110 |
"function f() {var foo={bar:$$S_$21$40$23$24$25$5e$26$2a$28$29}}"); |
| 111 |
|
| 112 |
test("function f() {var foo={px:435,foo:'px',bar:'baz'}}", |
| 113 |
"var $$S_px='px';" + |
| 114 |
"function f() {var foo={px:435,foo:$$S_px,bar:'baz'}}"); |
| 115 |
} |
| 116 |
|
| 117 |
public void testGetProp() { |
| 118 |
strings = ImmutableSet.of("px", "width"); |
| 119 |
|
| 120 |
testSame("function f(){element.style.px=1234}"); |
| 121 |
|
| 122 |
test("function f(){shape.width.units='px'}", |
| 123 |
"var $$S_px='px';function f(){shape.width.units=$$S_px}"); |
| 124 |
|
| 125 |
test("function f(){shape['width'].units='pt'}", |
| 126 |
"var $$S_width='width';" + |
| 127 |
"function f(){shape[$$S_width].units='pt'}"); |
| 128 |
} |
| 129 |
|
| 130 |
public void testFunctionCalls() { |
| 131 |
strings = ImmutableSet.of("", ",", "overimaginative"); |
| 132 |
|
| 133 |
|
| 134 |
testSame("alert('')"); |
| 135 |
testSame("var a=[1,2,3];a.join(',')"); |
| 136 |
|
| 137 |
test("f('overimaginative', 'overimaginative')", |
| 138 |
"var $$S_overimaginative='overimaginative';" + |
| 139 |
"f($$S_overimaginative,$$S_overimaginative)"); |
| 140 |
} |
| 141 |
|
| 142 |
public void testRegularExpressions() { |
| 143 |
strings = ImmutableSet.of("px"); |
| 144 |
|
| 145 |
testSame("/px/.match('10px')"); |
| 146 |
} |
| 147 |
|
| 148 |
public void testBlackList() { |
| 149 |
test("(function (){var f=\'sec ret\';g=\"TOPseCreT\"})", |
| 150 |
"var $$S_sec$20ret='sec ret';" + |
| 151 |
"(function (){var f=$$S_sec$20ret;g=\"TOPseCreT\"})"); |
| 152 |
} |
| 153 |
|
| 154 |
public void testLongStableAlias() { |
| 155 |
strings = ALL_STRINGS; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
test("a='Antidisestablishmentarianism';" + |
| 160 |
"b='Antidisestablishmentarianism';", |
| 161 |
"var $$S_Antidisestablishment_506eaf9c=" + |
| 162 |
" 'Antidisestablishmentarianism';" + |
| 163 |
"a=$$S_Antidisestablishment_506eaf9c;" + |
| 164 |
"b=$$S_Antidisestablishment_506eaf9c"); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
test("a='AntidisestablishmentarianIsm';" + |
| 169 |
"b='AntidisestablishmentarianIsm';", |
| 170 |
"var $$S_Antidisestablishment_6823e97c=" + |
| 171 |
" 'AntidisestablishmentarianIsm';" + |
| 172 |
"a=$$S_Antidisestablishment_6823e97c;" + |
| 173 |
"b=$$S_Antidisestablishment_6823e97c"); |
| 174 |
|
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
public void testLongStableAliasHashCollision() { |
| 179 |
strings = ALL_STRINGS; |
| 180 |
hashReduction = true; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
test("f('Antidisestablishmentarianism');" + |
| 186 |
"f('Antidisestablishmentarianism');" + |
| 187 |
"f('Antidisestablishmentarianismo');" + |
| 188 |
"f('Antidisestablishmentarianismo');", |
| 189 |
|
| 190 |
"var $$S_Antidisestablishment_0=" + |
| 191 |
" 'Antidisestablishmentarianism';" + |
| 192 |
"var $$S_Antidisestablishment_0_1=" + |
| 193 |
" 'Antidisestablishmentarianismo';" + |
| 194 |
|
| 195 |
"f($$S_Antidisestablishment_0);" + |
| 196 |
"f($$S_Antidisestablishment_0);" + |
| 197 |
"f($$S_Antidisestablishment_0_1);" + |
| 198 |
"f($$S_Antidisestablishment_0_1);"); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
public void testStringsThatAreGlobalVarValues() { |
| 203 |
strings = ALL_STRINGS; |
| 204 |
|
| 205 |
testSame("var foo='foo'; var bar='';"); |
| 206 |
|
| 207 |
|
| 208 |
testSame("var foo=['foo','bar'];"); |
| 209 |
|
| 210 |
|
| 211 |
testSame("var foo=['foo',['bar']];"); |
| 212 |
|
| 213 |
|
| 214 |
test("var foo=['foo', 'bar'];function bar() {return 'foo';}", |
| 215 |
"var $$S_foo='foo';" + |
| 216 |
"var foo=[$$S_foo, 'bar'];function bar() {return $$S_foo;}"); |
| 217 |
|
| 218 |
|
| 219 |
testSame("var foo={'foo': 'bar'};"); |
| 220 |
|
| 221 |
|
| 222 |
testSame("var foo={'foo': {'bar': 'baz'}};"); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
test("var foo={'foo': 'bar'};function bar() {return 'foo';}", |
| 227 |
"var $$S_foo='foo';var foo={'foo': 'bar'};" + |
| 228 |
"function bar() {return $$S_foo;}"); |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
test("var foo={'foo': 'foo'};function bar() {return 'foo';}", |
| 233 |
"var $$S_foo='foo';" + |
| 234 |
"var foo={'foo': $$S_foo};function bar() {return $$S_foo;}"); |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
public void testStringsInModules() { |
| 239 |
strings = ALL_STRINGS; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
JSModule[] modules = |
| 246 |
createModuleBush( |
| 247 |
|
| 248 |
"function f(a) { alert('f:' + a); }" + |
| 249 |
"function g() { alert('ciao'); }", |
| 250 |
|
| 251 |
"f('-------hi-------');" + |
| 252 |
"f('bye');" + |
| 253 |
"function h(a) { alert('h:' + a); }", |
| 254 |
|
| 255 |
"f('-------hi-------');" + |
| 256 |
"h('ciao' + '------adios------');" + |
| 257 |
"(function() { alert('zzz'); })();", |
| 258 |
|
| 259 |
"f('-------hi-------'); alert('------adios------');" + |
| 260 |
"h('-----peaches-----'); h('-----peaches-----');"); |
| 261 |
|
| 262 |
moduleGraph = new JSModuleGraph(modules); |
| 263 |
|
| 264 |
test(modules, |
| 265 |
new String[] { |
| 266 |
|
| 267 |
"var $$S_ciao = 'ciao';" + |
| 268 |
"var $$S_f$3a = 'f:';" + |
| 269 |
"function f(a) { alert($$S_f$3a + a); }" + |
| 270 |
"function g() { alert($$S_ciao); }", |
| 271 |
|
| 272 |
"var $$S_$2d$2d$2d$2d$2d$2d$2dhi$2d$2d$2d$2d$2d$2d$2d" + |
| 273 |
" = '-------hi-------';" + |
| 274 |
"var $$S_$2d$2d$2d$2d$2d$2d_adios$2d$2d$2d$2d$2d$2d" + |
| 275 |
" = '------adios------'; " + |
| 276 |
"var $$S_h$3a = 'h:';" + |
| 277 |
"f($$S_$2d$2d$2d$2d$2d$2d$2dhi$2d$2d$2d$2d$2d$2d$2d);" + |
| 278 |
"f('bye');" + |
| 279 |
"function h(a) { alert($$S_h$3a + a); }", |
| 280 |
|
| 281 |
"var $$S_zzz = 'zzz';" + |
| 282 |
"f($$S_$2d$2d$2d$2d$2d$2d$2dhi$2d$2d$2d$2d$2d$2d$2d);" + |
| 283 |
"h($$S_ciao + $$S_$2d$2d$2d$2d$2d$2d_adios$2d$2d$2d$2d$2d$2d);" + |
| 284 |
"(function() { alert($$S_zzz) })();", |
| 285 |
|
| 286 |
"var $$S_$2d$2d$2d$2d$2dpeaches$2d$2d$2d$2d$2d" + |
| 287 |
" = '-----peaches-----';" + |
| 288 |
"f($$S_$2d$2d$2d$2d$2d$2d$2dhi$2d$2d$2d$2d$2d$2d$2d);" + |
| 289 |
"alert($$S_$2d$2d$2d$2d$2d$2d_adios$2d$2d$2d$2d$2d$2d);" + |
| 290 |
"h($$S_$2d$2d$2d$2d$2dpeaches$2d$2d$2d$2d$2d);" + |
| 291 |
"h($$S_$2d$2d$2d$2d$2dpeaches$2d$2d$2d$2d$2d);", |
| 292 |
}); |
| 293 |
moduleGraph = null; |
| 294 |
} |
| 295 |
|
| 296 |
public void testStringsInModules2() { |
| 297 |
strings = ALL_STRINGS; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
JSModule[] modules = |
| 304 |
createModuleBush( |
| 305 |
|
| 306 |
"function g() { alert('ciao'); }", |
| 307 |
|
| 308 |
"function h(a) { alert('h:' + a); }", |
| 309 |
|
| 310 |
"h('ciao' + 'adios');", |
| 311 |
|
| 312 |
"g();"); |
| 313 |
|
| 314 |
moduleGraph = new JSModuleGraph(modules); |
| 315 |
|
| 316 |
test(modules, |
| 317 |
new String[] { |
| 318 |
|
| 319 |
"var $$S_ciao = 'ciao';" + |
| 320 |
"function g() { alert($$S_ciao); }", |
| 321 |
|
| 322 |
"var $$S_h$3a = 'h:';" + |
| 323 |
"function h(a) { alert($$S_h$3a + a); }", |
| 324 |
|
| 325 |
"h($$S_ciao + 'adios');", |
| 326 |
|
| 327 |
"g();", |
| 328 |
}); |
| 329 |
moduleGraph = null; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
public void testEmptyModules() { |
| 334 |
JSModule[] modules = |
| 335 |
createModuleStar( |
| 336 |
|
| 337 |
"", |
| 338 |
|
| 339 |
"function foo() { f('good') }", |
| 340 |
|
| 341 |
"function foo() { f('good') }"); |
| 342 |
|
| 343 |
moduleGraph = new JSModuleGraph(modules); |
| 344 |
test(modules, |
| 345 |
new String[] { |
| 346 |
|
| 347 |
"var $$S_good='good'", |
| 348 |
|
| 349 |
"function foo() {f($$S_good)}", |
| 350 |
|
| 351 |
"function foo() {f($$S_good)}",}); |
| 352 |
|
| 353 |
moduleGraph = null; |
| 354 |
} |
| 355 |
} |