| 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.ImmutableList; |
| 20 |
import com.google.javascript.rhino.Node; |
| 21 |
|
| 22 |
import java.io.StringReader; |
| 23 |
import java.util.List; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class InstrumentFunctionsTest extends CompilerTestCase { |
| 30 |
private String instrumentationPb; |
| 31 |
|
| 32 |
public InstrumentFunctionsTest() { |
| 33 |
this.instrumentationPb = null; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
protected void setUp() { |
| 38 |
super.enableLineNumberCheck(false); |
| 39 |
this.instrumentationPb = null; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 44 |
return new NameAndInstrumentFunctions(compiler); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
protected int getNumRepetitions() { |
| 49 |
|
| 50 |
return 1; |
| 51 |
} |
| 52 |
|
| 53 |
public void testInstrument() { |
| 54 |
final String kPreamble = |
| 55 |
"var $$toRemoveDefinition1, $$notToRemove;\n" + |
| 56 |
"var $$toRemoveDefinition2, $$toRemoveDefinition3;\n"; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
List<String> initCodeList = ImmutableList.of( |
| 61 |
"var $$Table = [];", |
| 62 |
"function $$TestDefine(id) {", |
| 63 |
" $$Table[id] = 0;", |
| 64 |
"};", |
| 65 |
"function $$TestInstrument(id) {", |
| 66 |
" $$Table[id]++;", |
| 67 |
"};"); |
| 68 |
StringBuilder initCodeBuilder = new StringBuilder(); |
| 69 |
StringBuilder pbBuilder = new StringBuilder(); |
| 70 |
for (String line : initCodeList) { |
| 71 |
initCodeBuilder.append(line).append("\n"); |
| 72 |
pbBuilder.append("init: \"").append(line).append("\"\n"); |
| 73 |
} |
| 74 |
|
| 75 |
pbBuilder.append("report_call: \"$$testInstrument\"") |
| 76 |
.append("report_defined: \"$$testDefine\"") |
| 77 |
.append("declaration_to_remove: \"$$toRemoveDefinition1\"") |
| 78 |
.append("declaration_to_remove: \"$$toRemoveDefinition2\"") |
| 79 |
.append("declaration_to_remove: \"$$toRemoveDefinition3\""); |
| 80 |
|
| 81 |
final String initCode = initCodeBuilder.toString(); |
| 82 |
this.instrumentationPb = pbBuilder.toString(); |
| 83 |
|
| 84 |
|
| 85 |
test("function a(){b}", |
| 86 |
initCode + "$$testDefine(0);" + |
| 87 |
"function a(){$$testInstrument(0);b}"); |
| 88 |
|
| 89 |
|
| 90 |
test(kPreamble + "function a(){b}", |
| 91 |
initCode + |
| 92 |
"$$testDefine(0);" + |
| 93 |
"var $$notToRemove;" + |
| 94 |
"function a(){$$testInstrument(0);b}"); |
| 95 |
|
| 96 |
|
| 97 |
test(kPreamble + "var a = { b: function(){c} }", |
| 98 |
initCode + |
| 99 |
"var $$notToRemove;" + |
| 100 |
"$$testDefine(0);" + |
| 101 |
"var a = { b: function(){$$testInstrument(0);c} }"); |
| 102 |
|
| 103 |
|
| 104 |
test(kPreamble + |
| 105 |
"var a = { b: function(){c}, d: function(){e} }", |
| 106 |
initCode + |
| 107 |
"var $$notToRemove;" + |
| 108 |
"$$testDefine(0);" + |
| 109 |
"$$testDefine(1);" + |
| 110 |
"var a={b:function(){$$testInstrument(0);c}," + |
| 111 |
"d:function(){$$testInstrument(1);e}}"); |
| 112 |
|
| 113 |
|
| 114 |
test(kPreamble + |
| 115 |
"var a = { b: { f: function(){c} }, d: function(){e} }", |
| 116 |
initCode + |
| 117 |
"var $$notToRemove;" + |
| 118 |
"$$testDefine(0);" + |
| 119 |
"$$testDefine(1);" + |
| 120 |
"var a={b:{f:function(){$$testInstrument(0);c}}," + |
| 121 |
"d:function(){$$testInstrument(1);e}}"); |
| 122 |
} |
| 123 |
|
| 124 |
public void testEmpty() { |
| 125 |
this.instrumentationPb = ""; |
| 126 |
test("function a(){b}", "function a(){b}"); |
| 127 |
} |
| 128 |
|
| 129 |
public void testAppNameSetter() { |
| 130 |
this.instrumentationPb = "app_name_setter: \"setAppName\""; |
| 131 |
test("function a(){b}", "setAppName(\"testfile.js\");function a(){b}"); |
| 132 |
} |
| 133 |
|
| 134 |
public void testInit() { |
| 135 |
this.instrumentationPb = "init: \"var foo = 0;\"\n" + |
| 136 |
"init: \"function f(){g();}\"\n"; |
| 137 |
test("function a(){b}", |
| 138 |
"var foo = 0;function f(){g()}function a(){b}"); |
| 139 |
} |
| 140 |
|
| 141 |
public void testDeclare() { |
| 142 |
this.instrumentationPb = "report_defined: \"$$testDefine\""; |
| 143 |
test("function a(){b}", "$$testDefine(0);function a(){b}"); |
| 144 |
} |
| 145 |
|
| 146 |
public void testCall() { |
| 147 |
this.instrumentationPb = "report_call: \"$$testCall\""; |
| 148 |
test("function a(){b}", "function a(){$$testCall(0);b}"); |
| 149 |
} |
| 150 |
|
| 151 |
public void testNested() { |
| 152 |
this.instrumentationPb = "report_call: \"$$testCall\"\n" + |
| 153 |
"report_defined: \"$$testDefine\""; |
| 154 |
test("function a(){ function b(){}}", |
| 155 |
"$$testDefine(1);$$testDefine(0);" + |
| 156 |
"function a(){$$testCall(1);function b(){$$testCall(0)}}"); |
| 157 |
} |
| 158 |
|
| 159 |
public void testExitPaths() { |
| 160 |
this.instrumentationPb = "report_exit: \"$$testExit\""; |
| 161 |
test("function a(){return}", |
| 162 |
"function a(){return $$testExit(0)}"); |
| 163 |
|
| 164 |
test("function b(){return 5}", |
| 165 |
"function b(){return $$testExit(0, 5)}"); |
| 166 |
|
| 167 |
test("function a(){if(2 != 3){return}else{return 5}}", |
| 168 |
"function a(){if(2!=3){return $$testExit(0)}" + |
| 169 |
"else{return $$testExit(0,5)}}"); |
| 170 |
|
| 171 |
test("function a(){if(2 != 3){return}else{return 5}}b()", |
| 172 |
"function a(){if(2!=3){return $$testExit(0)}" + |
| 173 |
"else{return $$testExit(0,5)}}b()"); |
| 174 |
|
| 175 |
test("function a(){if(2 != 3){return}else{return 5}}", |
| 176 |
"function a(){if(2!=3){return $$testExit(0)}" + |
| 177 |
"else{return $$testExit(0,5)}}"); |
| 178 |
} |
| 179 |
|
| 180 |
public void testExitNoReturn() { |
| 181 |
this.instrumentationPb = "report_exit: \"$$testExit\""; |
| 182 |
test("function a(){}", |
| 183 |
"function a(){$$testExit(0);}"); |
| 184 |
|
| 185 |
test("function a(){b()}", |
| 186 |
"function a(){b();$$testExit(0);}"); |
| 187 |
} |
| 188 |
|
| 189 |
public void testPartialExitPaths() { |
| 190 |
this.instrumentationPb = "report_exit: \"$$testExit\""; |
| 191 |
test("function a(){if (2 != 3) {return}}", |
| 192 |
"function a(){if (2 != 3){return $$testExit(0)}$$testExit(0)}"); |
| 193 |
} |
| 194 |
|
| 195 |
public void testExitTry() { |
| 196 |
this.instrumentationPb = "report_exit: \"$$testExit\""; |
| 197 |
test("function a(){try{return}catch(err){}}", |
| 198 |
"function a(){try{return $$testExit(0)}catch(err){}$$testExit(0)}"); |
| 199 |
|
| 200 |
test("function a(){try{}catch(err){return}}", |
| 201 |
"function a(){try{}catch(err){return $$testExit(0)}$$testExit(0)}"); |
| 202 |
|
| 203 |
test("function a(){try{return}finally{}}", |
| 204 |
"function a(){try{return $$testExit(0)}finally{}$$testExit(0)}"); |
| 205 |
|
| 206 |
test("function a(){try{return}catch(err){}finally{}}", |
| 207 |
"function a(){try{return $$testExit(0)}catch(err){}finally{}" + |
| 208 |
"$$testExit(0)}"); |
| 209 |
|
| 210 |
test("function a(){try{return 1}catch(err){return 2}}", |
| 211 |
"function a(){try{return $$testExit(0, 1)}" + |
| 212 |
"catch(err){return $$testExit(0,2)}}"); |
| 213 |
|
| 214 |
test("function a(){try{return 1}catch(err){return 2}finally{}}", |
| 215 |
"function a(){try{return $$testExit(0, 1)}" + |
| 216 |
"catch(err){return $$testExit(0,2)}" + |
| 217 |
"finally{}$$testExit(0)}"); |
| 218 |
|
| 219 |
test("function a(){try{return 1}catch(err){return 2}finally{return}}", |
| 220 |
"function a(){try{return $$testExit(0, 1)}" + |
| 221 |
"catch(err){return $$testExit(0,2)}finally{return $$testExit(0)}}"); |
| 222 |
|
| 223 |
test("function a(){try{}catch(err){}finally{return}}", |
| 224 |
"function a(){try{}catch(err){}finally{return $$testExit(0)}}"); |
| 225 |
} |
| 226 |
|
| 227 |
public void testNestedExit() { |
| 228 |
this.instrumentationPb = "report_exit: \"$$testExit\"\n" + |
| 229 |
"report_defined: \"$$testDefine\""; |
| 230 |
test("function a(){ return function(){ return c;}}", |
| 231 |
"$$testDefine(1);function a(){$$testDefine(0);" + |
| 232 |
"return $$testExit(1, function(){return $$testExit(0, c);});}"); |
| 233 |
} |
| 234 |
|
| 235 |
public void testProtobuffParseFail() { |
| 236 |
this.instrumentationPb = "not an ascii pb\n"; |
| 237 |
test("function a(){b}", "", RhinoErrorReporter.PARSE_ERROR); |
| 238 |
} |
| 239 |
|
| 240 |
public void testInitJsParseFail() { |
| 241 |
this.instrumentationPb = "init: \"= assignWithNoLhs();\""; |
| 242 |
test("function a(){b}", "", RhinoErrorReporter.PARSE_ERROR); |
| 243 |
} |
| 244 |
|
| 245 |
private class NameAndInstrumentFunctions implements CompilerPass { |
| 246 |
private final Compiler compiler; |
| 247 |
NameAndInstrumentFunctions(Compiler compiler) { |
| 248 |
this.compiler = compiler; |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public void process(Node externs, Node root) { |
| 253 |
FunctionNames functionNames = new FunctionNames(compiler); |
| 254 |
functionNames.process(externs, root); |
| 255 |
|
| 256 |
InstrumentFunctions instrumentation = |
| 257 |
new InstrumentFunctions(compiler, functionNames, |
| 258 |
"test init code", "testfile.js", |
| 259 |
new StringReader(instrumentationPb)); |
| 260 |
instrumentation.process(externs, root); |
| 261 |
} |
| 262 |
} |
| 263 |
} |