| 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 |
|
| 23 |
public class FunctionRewriterTest extends CompilerTestCase { |
| 24 |
|
| 25 |
private static final String RETURNARG_HELPER = |
| 26 |
"function JSCompiler_returnArg(JSCompiler_returnArg_value){" + |
| 27 |
" return function() { return JSCompiler_returnArg_value }" + |
| 28 |
"}"; |
| 29 |
private static final String GET_HELPER = |
| 30 |
"function JSCompiler_get(JSCompiler_get_name){" + |
| 31 |
" return function() { return this[JSCompiler_get_name] }" + |
| 32 |
"}"; |
| 33 |
private static final String SET_HELPER = |
| 34 |
"function JSCompiler_set(JSCompiler_set_name) {" + |
| 35 |
" return function(JSCompiler_set_value){" + |
| 36 |
" this[JSCompiler_set_name]=JSCompiler_set_value" + |
| 37 |
" }" + |
| 38 |
"}"; |
| 39 |
private static final String EMPTY_HELPER = |
| 40 |
"function JSCompiler_emptyFn() {" + |
| 41 |
" return function(){}" + |
| 42 |
"}"; |
| 43 |
private static final String IDENTITY_HELPER = |
| 44 |
"function JSCompiler_identityFn() {" + |
| 45 |
" return function(JSCompiler_identityFn_value) {" + |
| 46 |
" return JSCompiler_identityFn_value" + |
| 47 |
" }" + |
| 48 |
"}"; |
| 49 |
|
| 50 |
@Override |
| 51 |
protected void setUp() { |
| 52 |
super.enableLineNumberCheck(false); |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
protected FunctionRewriter getProcessor(Compiler compiler) { |
| 57 |
return new FunctionRewriter(compiler); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
protected int getNumRepetitions() { |
| 62 |
|
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
public void testReplaceReturnConst1() { |
| 67 |
String source = "a.prototype.foo = function() {return \"foobar\"}"; |
| 68 |
checkCompilesToSame(source, 3); |
| 69 |
checkCompilesTo(source, |
| 70 |
RETURNARG_HELPER, |
| 71 |
"a.prototype.foo = JSCompiler_returnArg(\"foobar\")", |
| 72 |
4); |
| 73 |
} |
| 74 |
|
| 75 |
public void testReplaceReturnConst2() { |
| 76 |
checkCompilesToSame("a.prototype.foo = function() {return foobar}", 10); |
| 77 |
} |
| 78 |
|
| 79 |
public void testReplaceReturnConst3() { |
| 80 |
String source = "a.prototype.foo = function() {return void 0;}"; |
| 81 |
checkCompilesToSame(source, 3); |
| 82 |
checkCompilesTo(source, |
| 83 |
RETURNARG_HELPER, |
| 84 |
"a.prototype.foo = JSCompiler_returnArg(void 0)", |
| 85 |
4); |
| 86 |
} |
| 87 |
|
| 88 |
public void testReplaceGetter1() { |
| 89 |
String source = "a.prototype.foo = function() {return this.foo_}"; |
| 90 |
checkCompilesToSame(source, 3); |
| 91 |
checkCompilesTo(source, |
| 92 |
GET_HELPER, |
| 93 |
"a.prototype.foo = JSCompiler_get(\"foo_\")", |
| 94 |
4); |
| 95 |
} |
| 96 |
|
| 97 |
public void testReplaceGetter2() { |
| 98 |
checkCompilesToSame("a.prototype.foo = function() {return}", 10); |
| 99 |
} |
| 100 |
|
| 101 |
public void testReplaceSetter1() { |
| 102 |
String source = "a.prototype.foo = function(v) {this.foo_ = v}"; |
| 103 |
checkCompilesToSame(source, 4); |
| 104 |
checkCompilesTo(source, |
| 105 |
SET_HELPER, |
| 106 |
"a.prototype.foo = JSCompiler_set(\"foo_\")", |
| 107 |
5); |
| 108 |
} |
| 109 |
|
| 110 |
public void testReplaceSetter2() { |
| 111 |
String source = "a.prototype.foo = function(v, v2) {this.foo_ = v}"; |
| 112 |
checkCompilesToSame(source, 3); |
| 113 |
checkCompilesTo(source, |
| 114 |
SET_HELPER, |
| 115 |
"a.prototype.foo = JSCompiler_set(\"foo_\")", |
| 116 |
4); |
| 117 |
} |
| 118 |
|
| 119 |
public void testReplaceSetter3() { |
| 120 |
checkCompilesToSame("a.prototype.foo = function() {this.foo_ = v}", 10); |
| 121 |
} |
| 122 |
|
| 123 |
public void testReplaceSetter4() { |
| 124 |
checkCompilesToSame( |
| 125 |
"a.prototype.foo = function(v, v2) {this.foo_ = v2}", 10); |
| 126 |
} |
| 127 |
|
| 128 |
public void testReplaceEmptyFunction1() { |
| 129 |
String source = "a.prototype.foo = function() {}"; |
| 130 |
checkCompilesToSame(source, 4); |
| 131 |
checkCompilesTo(source, |
| 132 |
EMPTY_HELPER, |
| 133 |
"a.prototype.foo = JSCompiler_emptyFn()", |
| 134 |
5); |
| 135 |
} |
| 136 |
|
| 137 |
public void testReplaceEmptyFunction2() { |
| 138 |
checkCompilesToSame("function foo() {}", 10); |
| 139 |
} |
| 140 |
|
| 141 |
public void testReplaceEmptyFunction3() { |
| 142 |
String source = "var foo = function() {}"; |
| 143 |
checkCompilesToSame(source, 4); |
| 144 |
checkCompilesTo(source, |
| 145 |
EMPTY_HELPER, |
| 146 |
"var foo = JSCompiler_emptyFn()", |
| 147 |
5); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public void testReplaceIdentityFunction1() { |
| 153 |
String source = "a.prototype.foo = function(a) {return a}"; |
| 154 |
checkCompilesToSame(source, 2); |
| 155 |
checkCompilesTo(source, |
| 156 |
IDENTITY_HELPER, |
| 157 |
"a.prototype.foo = JSCompiler_identityFn()", |
| 158 |
3); |
| 159 |
} |
| 160 |
|
| 161 |
public void testReplaceIdentityFunction2() { |
| 162 |
checkCompilesToSame("a.prototype.foo = function(a) {return a + 1}", 10); |
| 163 |
} |
| 164 |
|
| 165 |
public void testIssue538() { |
| 166 |
checkCompilesToSame( "/** @constructor */\n" + |
| 167 |
"WebInspector.Setting = function() {}\n" + |
| 168 |
"WebInspector.Setting.prototype = {\n" + |
| 169 |
" get name0(){return this._name;},\n" + |
| 170 |
" get name1(){return this._name;},\n" + |
| 171 |
" get name2(){return this._name;},\n" + |
| 172 |
" get name3(){return this._name;},\n" + |
| 173 |
" get name4(){return this._name;},\n" + |
| 174 |
" get name5(){return this._name;},\n" + |
| 175 |
" get name6(){return this._name;},\n" + |
| 176 |
" get name7(){return this._name;},\n" + |
| 177 |
" get name8(){return this._name;},\n" + |
| 178 |
" get name9(){return this._name;},\n" + |
| 179 |
"}", 1); |
| 180 |
} |
| 181 |
|
| 182 |
private void checkCompilesTo(String src, |
| 183 |
String expectedHdr, |
| 184 |
String expectedBody, |
| 185 |
int repetitions) { |
| 186 |
StringBuilder srcBuffer = new StringBuilder(); |
| 187 |
StringBuilder expectedBuffer = new StringBuilder(); |
| 188 |
|
| 189 |
expectedBuffer.append(expectedHdr); |
| 190 |
|
| 191 |
for (int idx = 0; idx < repetitions; idx++) { |
| 192 |
if (idx != 0) { |
| 193 |
srcBuffer.append(";"); |
| 194 |
expectedBuffer.append(";"); |
| 195 |
} |
| 196 |
srcBuffer.append(src); |
| 197 |
expectedBuffer.append(expectedBody); |
| 198 |
} |
| 199 |
test(srcBuffer.toString(), expectedBuffer.toString()); |
| 200 |
} |
| 201 |
|
| 202 |
private void checkCompilesToSame(String src, int repetitions) { |
| 203 |
checkCompilesTo(src, "", src, repetitions); |
| 204 |
} |
| 205 |
} |