| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class RemoveUnusedClassPropertiesTest extends CompilerTestCase { |
| 23 |
|
| 24 |
private static final String EXTERNS = |
| 25 |
"var window;\n" + |
| 26 |
"function alert(a) {}\n" + |
| 27 |
"var EXT = {};" + |
| 28 |
"EXT.ext;"; |
| 29 |
|
| 30 |
public RemoveUnusedClassPropertiesTest() { |
| 31 |
super(EXTERNS); |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 36 |
return new RemoveUnusedClassProperties(compiler); |
| 37 |
} |
| 38 |
|
| 39 |
public void testSimple1() { |
| 40 |
|
| 41 |
test("this.a = 2", "2"); |
| 42 |
test("x = (this.a = 2)", "x = 2"); |
| 43 |
testSame("this.a = 2; x = this.a;"); |
| 44 |
} |
| 45 |
|
| 46 |
public void testSimple2() { |
| 47 |
|
| 48 |
|
| 49 |
test("this.a = 2, f()", "2, f()"); |
| 50 |
test("x = (this.a = 2, f())", "x = (2, f())"); |
| 51 |
test("x = (f(), this.a = 2)", "x = (f(), 2)"); |
| 52 |
} |
| 53 |
|
| 54 |
public void testSimple3() { |
| 55 |
|
| 56 |
testSame("y.a = 2"); |
| 57 |
|
| 58 |
test("y.a = 2; this.a = 2", "y.a = 2; 2"); |
| 59 |
|
| 60 |
testSame("y.a = 2; this.a = 1; alert(x.a)"); |
| 61 |
} |
| 62 |
|
| 63 |
public void testObjLit() { |
| 64 |
|
| 65 |
testSame("({a:2})"); |
| 66 |
|
| 67 |
test("({a:0}); this.a = 1;", "({a:0});1"); |
| 68 |
|
| 69 |
testSame("x = ({a:0}); this.a = 1; alert(x.a)"); |
| 70 |
} |
| 71 |
|
| 72 |
public void testExtern() { |
| 73 |
|
| 74 |
testSame("this.ext = 2"); |
| 75 |
} |
| 76 |
|
| 77 |
public void testExport() { |
| 78 |
|
| 79 |
testSame("this.ext = 2; window['export'] = this.ext;"); |
| 80 |
testSame("function f() { this.ext = 2; } window['export'] = this.ext;"); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
public void testAssignOp1() { |
| 85 |
|
| 86 |
|
| 87 |
test("this.x += 2", "2"); |
| 88 |
testSame("x = (this.x += 2)"); |
| 89 |
testSame("this.x += 2; x = this.x;"); |
| 90 |
|
| 91 |
testSame("this.x += 2; x.x;"); |
| 92 |
} |
| 93 |
|
| 94 |
public void testAssignOp2() { |
| 95 |
|
| 96 |
|
| 97 |
test("this.a += 2, f()", "2, f()"); |
| 98 |
test("x = (this.a += 2, f())", "x = (2, f())"); |
| 99 |
testSame("x = (f(), this.a += 2)"); |
| 100 |
} |
| 101 |
|
| 102 |
public void testInc1() { |
| 103 |
|
| 104 |
|
| 105 |
test("this.x++", "0"); |
| 106 |
testSame("x = (this.x++)"); |
| 107 |
testSame("this.x++; x = this.x;"); |
| 108 |
|
| 109 |
test("--this.x", "0"); |
| 110 |
testSame("x = (--this.x)"); |
| 111 |
testSame("--this.x; x = this.x;"); |
| 112 |
} |
| 113 |
|
| 114 |
public void testInc2() { |
| 115 |
|
| 116 |
|
| 117 |
test("this.a++, f()", "0, f()"); |
| 118 |
test("x = (this.a++, f())", "x = (0, f())"); |
| 119 |
testSame("x = (f(), this.a++)"); |
| 120 |
|
| 121 |
test("--this.a, f()", "0, f()"); |
| 122 |
test("x = (--this.a, f())", "x = (0, f())"); |
| 123 |
testSame("x = (f(), --this.a)"); |
| 124 |
} |
| 125 |
|
| 126 |
public void testJSCompiler_renameProperty() { |
| 127 |
|
| 128 |
testSame("this.a = 2; x[JSCompiler_renameProperty('a')]"); |
| 129 |
testSame("this.a = 2; JSCompiler_renameProperty('a')"); |
| 130 |
} |
| 131 |
|
| 132 |
public void testForIn() { |
| 133 |
|
| 134 |
|
| 135 |
test("this.y = 1;for (var a in x) { alert(x[a]) }", |
| 136 |
"1;for (var a in x) { alert(x[a]) }"); |
| 137 |
} |
| 138 |
|
| 139 |
public void testObjectKeys() { |
| 140 |
|
| 141 |
|
| 142 |
test("this.y = 1;alert(Object.keys(this))", |
| 143 |
"1;alert(Object.keys(this))"); |
| 144 |
} |
| 145 |
|
| 146 |
public void testIssue730() { |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
test( |
| 151 |
"function A() {this.foo = 0;}\n" + |
| 152 |
"function B() {this.a = new A();}\n" + |
| 153 |
"B.prototype.dostuff = function() {this.a.foo++;alert('hi');}\n" + |
| 154 |
"new B().dostuff();\n", |
| 155 |
"function A(){0}" + |
| 156 |
"function B(){this.a=new A}" + |
| 157 |
"B.prototype.dostuff=function(){this.a.foo++;alert(\"hi\")};" + |
| 158 |
"new B().dostuff();"); |
| 159 |
} |
| 160 |
} |