| 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.ImmutableMap; |
| 20 |
import com.google.common.collect.ImmutableSet; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import com.google.javascript.jscomp.ReplaceStrings.Result; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
|
| 25 |
import java.util.Collections; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Set; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class ReplaceStringsTest extends CompilerTestCase { |
| 34 |
private ReplaceStrings pass; |
| 35 |
private Set<String> reserved; |
| 36 |
private VariableMap previous; |
| 37 |
|
| 38 |
private final static String EXTERNS = |
| 39 |
"var goog = {};\n" + |
| 40 |
"goog.debug = {};\n" + |
| 41 |
"/** @constructor */\n" + |
| 42 |
"goog.debug.Trace = function() {};\n" + |
| 43 |
"goog.debug.Trace.startTracer = function (var_args) {};\n" + |
| 44 |
"/** @constructor */\n" + |
| 45 |
"goog.debug.Logger = function() {};\n" + |
| 46 |
"goog.debug.Logger.prototype.info = function(msg, opt_ex) {};\n" + |
| 47 |
"/**\n" + |
| 48 |
" * @param {string} name\n" + |
| 49 |
" * @return {!goog.debug.Logger}\n" + |
| 50 |
" */\n" + |
| 51 |
"goog.debug.Logger.getLogger = function(name){};\n"; |
| 52 |
|
| 53 |
public ReplaceStringsTest() { |
| 54 |
super(EXTERNS, true); |
| 55 |
enableNormalize(); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
protected CompilerOptions getOptions() { |
| 60 |
CompilerOptions options = super.getOptions(); |
| 61 |
options.setWarningLevel( |
| 62 |
DiagnosticGroups.MISSING_PROPERTIES, CheckLevel.OFF); |
| 63 |
return options; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
protected void setUp() throws Exception { |
| 68 |
super.setUp(); |
| 69 |
super.enableLineNumberCheck(false); |
| 70 |
super.enableTypeCheck(CheckLevel.OFF); |
| 71 |
reserved = Collections.emptySet(); |
| 72 |
previous = null; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public CompilerPass getProcessor(final Compiler compiler) { |
| 77 |
List<String> names = Lists.newArrayList( |
| 78 |
"Error(?)", |
| 79 |
"goog.debug.Trace.startTracer(*)", |
| 80 |
"goog.debug.Logger.getLogger(?)", |
| 81 |
"goog.debug.Logger.prototype.info(?)" |
| 82 |
); |
| 83 |
pass = new ReplaceStrings(compiler, "`", names, reserved, previous); |
| 84 |
|
| 85 |
return new CompilerPass() { |
| 86 |
@Override |
| 87 |
public void process(Node externs, Node js) { |
| 88 |
new CollapseProperties(compiler, true, true).process(externs, js); |
| 89 |
pass.process(externs, js); |
| 90 |
} |
| 91 |
}; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public int getNumRepetitions() { |
| 96 |
|
| 97 |
|
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
public void testStable1() { |
| 102 |
previous = VariableMap.fromMap(ImmutableMap.of("previous","xyz")); |
| 103 |
testDebugStrings( |
| 104 |
"Error('xyz');", |
| 105 |
"Error('previous');", |
| 106 |
(new String[] { "previous", "xyz" })); |
| 107 |
reserved = ImmutableSet.of("a", "b", "previous"); |
| 108 |
testDebugStrings( |
| 109 |
"Error('xyz');", |
| 110 |
"Error('c');", |
| 111 |
(new String[] { "c", "xyz" })); |
| 112 |
} |
| 113 |
|
| 114 |
public void testStable2() { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
previous = VariableMap.fromMap(ImmutableMap.of("a","unused")); |
| 121 |
testDebugStrings( |
| 122 |
"Error('xyz');", |
| 123 |
"Error('b');", |
| 124 |
(new String[] { "b", "xyz" })); |
| 125 |
} |
| 126 |
|
| 127 |
public void testThrowError1() { |
| 128 |
testDebugStrings( |
| 129 |
"throw Error('xyz');", |
| 130 |
"throw Error('a');", |
| 131 |
(new String[] { "a", "xyz" })); |
| 132 |
previous = VariableMap.fromMap(ImmutableMap.of("previous","xyz")); |
| 133 |
testDebugStrings( |
| 134 |
"throw Error('xyz');", |
| 135 |
"throw Error('previous');", |
| 136 |
(new String[] { "previous", "xyz" })); |
| 137 |
} |
| 138 |
|
| 139 |
public void testThrowError2() { |
| 140 |
testDebugStrings( |
| 141 |
"throw Error('x' +\n 'yz');", |
| 142 |
"throw Error('a');", |
| 143 |
(new String[] { "a", "xyz" })); |
| 144 |
} |
| 145 |
|
| 146 |
public void testThrowError3() { |
| 147 |
testDebugStrings( |
| 148 |
"throw Error('Unhandled mail' + ' search type ' + type);", |
| 149 |
"throw Error('a' + '`' + type);", |
| 150 |
(new String[] { "a", "Unhandled mail search type `" })); |
| 151 |
} |
| 152 |
|
| 153 |
public void testThrowError4() { |
| 154 |
testDebugStrings( |
| 155 |
"/** @constructor */\n" + |
| 156 |
"var A = function() {};\n" + |
| 157 |
"A.prototype.m = function(child) {\n" + |
| 158 |
" if (this.haveChild(child)) {\n" + |
| 159 |
" throw Error('Node: ' + this.getDataPath() +\n" + |
| 160 |
" ' already has a child named ' + child);\n" + |
| 161 |
" } else if (child.parentNode) {\n" + |
| 162 |
" throw Error('Node: ' + child.getDataPath() +\n" + |
| 163 |
" ' already has a parent');\n" + |
| 164 |
" }\n" + |
| 165 |
" child.parentNode = this;\n" + |
| 166 |
"};", |
| 167 |
|
| 168 |
"var A = function(){};\n" + |
| 169 |
"A.prototype.m = function(child) {\n" + |
| 170 |
" if (this.haveChild(child)) {\n" + |
| 171 |
" throw Error('a' + '`' + this.getDataPath() + '`' + child);\n" + |
| 172 |
" } else if (child.parentNode) {\n" + |
| 173 |
" throw Error('b' + '`' + child.getDataPath());\n" + |
| 174 |
" }\n" + |
| 175 |
" child.parentNode = this;\n" + |
| 176 |
"};", |
| 177 |
(new String[] { |
| 178 |
"a", |
| 179 |
"Node: ` already has a child named `", |
| 180 |
"b", |
| 181 |
"Node: ` already has a parent", |
| 182 |
})); |
| 183 |
} |
| 184 |
|
| 185 |
public void testThrowNonStringError() { |
| 186 |
|
| 187 |
|
| 188 |
testDebugStrings( |
| 189 |
"throw Error(x('abc'));", |
| 190 |
"throw Error(x('abc'));", |
| 191 |
(new String[] { })); |
| 192 |
} |
| 193 |
|
| 194 |
public void testThrowConstStringError() { |
| 195 |
testDebugStrings( |
| 196 |
"var AA = 'uvw', AB = 'xyz'; throw Error(AB);", |
| 197 |
"var AA = 'uvw', AB = 'xyz'; throw Error('a');", |
| 198 |
(new String [] { "a", "xyz" })); |
| 199 |
} |
| 200 |
|
| 201 |
public void testThrowNewError1() { |
| 202 |
testDebugStrings( |
| 203 |
"throw new Error('abc');", |
| 204 |
"throw new Error('a');", |
| 205 |
(new String[] { "a", "abc" })); |
| 206 |
} |
| 207 |
|
| 208 |
public void testThrowNewError2() { |
| 209 |
testDebugStrings( |
| 210 |
"throw new Error();", |
| 211 |
"throw new Error();", |
| 212 |
new String[] {}); |
| 213 |
} |
| 214 |
|
| 215 |
public void testStartTracer1() { |
| 216 |
testDebugStrings( |
| 217 |
"goog.debug.Trace.startTracer('HistoryManager.updateHistory');", |
| 218 |
"goog.debug.Trace.startTracer('a');", |
| 219 |
(new String[] { "a", "HistoryManager.updateHistory" })); |
| 220 |
} |
| 221 |
|
| 222 |
public void testStartTracer2() { |
| 223 |
testDebugStrings( |
| 224 |
"goog$debug$Trace.startTracer('HistoryManager', 'updateHistory');", |
| 225 |
"goog$debug$Trace.startTracer('a', 'b');", |
| 226 |
(new String[] { |
| 227 |
"a", "HistoryManager", |
| 228 |
"b", "updateHistory" })); |
| 229 |
} |
| 230 |
|
| 231 |
public void testStartTracer3() { |
| 232 |
testDebugStrings( |
| 233 |
"goog$debug$Trace.startTracer('ThreadlistView',\n" + |
| 234 |
" 'Updating ' + array.length + ' rows');", |
| 235 |
"goog$debug$Trace.startTracer('a', 'b' + '`' + array.length);", |
| 236 |
new String[] { "a", "ThreadlistView", "b", "Updating ` rows" }); |
| 237 |
} |
| 238 |
|
| 239 |
public void testStartTracer4() { |
| 240 |
testDebugStrings( |
| 241 |
"goog.debug.Trace.startTracer(s, 'HistoryManager.updateHistory');", |
| 242 |
"goog.debug.Trace.startTracer(s, 'a');", |
| 243 |
(new String[] { "a", "HistoryManager.updateHistory" })); |
| 244 |
} |
| 245 |
|
| 246 |
public void testLoggerInitialization() { |
| 247 |
testDebugStrings( |
| 248 |
"goog$debug$Logger$getLogger('my.app.Application');", |
| 249 |
"goog$debug$Logger$getLogger('a');", |
| 250 |
(new String[] { "a", "my.app.Application" })); |
| 251 |
} |
| 252 |
|
| 253 |
public void testLoggerOnObject1() { |
| 254 |
testDebugStrings( |
| 255 |
"var x = {};" + |
| 256 |
"x.logger_ = goog.debug.Logger.getLogger('foo');" + |
| 257 |
"x.logger_.info('Some message');", |
| 258 |
"var x$logger_ = goog.debug.Logger.getLogger('a');" + |
| 259 |
"x$logger_.info('b');", |
| 260 |
new String[] { |
| 261 |
"a", "foo", |
| 262 |
"b", "Some message"}); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
public void testLoggerOnObject2() { |
| 267 |
test( |
| 268 |
"var x = {};" + |
| 269 |
"x.info = function(a) {};" + |
| 270 |
"x.info('Some message');", |
| 271 |
"var x$info = function(a) {};" + |
| 272 |
"x$info('Some message');"); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
public void testLoggerOnObject3a() { |
| 277 |
testSame( |
| 278 |
"/** @constructor */\n" + |
| 279 |
"var x = function() {};\n" + |
| 280 |
"x.prototype.info = function(a) {};" + |
| 281 |
"(new x).info('Some message');"); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
public void testLoggerOnObject3b() { |
| 286 |
testSame( |
| 287 |
"/** @constructor */\n" + |
| 288 |
"var x = function() {};\n" + |
| 289 |
"x.prototype.info = function(a) {};" + |
| 290 |
"var y = (new x); this.info('Some message');"); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
public void testLoggerOnObject4() { |
| 295 |
testSame("(new x).info('Some message');"); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
public void testLoggerOnObject5() { |
| 300 |
testSame("my$Thing.logger_.info('Some message');"); |
| 301 |
} |
| 302 |
|
| 303 |
public void testLoggerOnVar() { |
| 304 |
testDebugStrings( |
| 305 |
"var logger = goog.debug.Logger.getLogger('foo');" + |
| 306 |
"logger.info('Some message');", |
| 307 |
"var logger = goog.debug.Logger.getLogger('a');" + |
| 308 |
"logger.info('b');", |
| 309 |
new String[] { |
| 310 |
"a", "foo", |
| 311 |
"b", "Some message"}); |
| 312 |
} |
| 313 |
|
| 314 |
public void testLoggerOnThis() { |
| 315 |
testDebugStrings( |
| 316 |
"function f() {" + |
| 317 |
" this.logger_ = goog.debug.Logger.getLogger('foo');" + |
| 318 |
" this.logger_.info('Some message');" + |
| 319 |
"}", |
| 320 |
"function f() {" + |
| 321 |
" this.logger_ = goog.debug.Logger.getLogger('a');" + |
| 322 |
" this.logger_.info('b');" + |
| 323 |
"}", |
| 324 |
new String[] { |
| 325 |
"a", "foo", |
| 326 |
"b", "Some message"}); |
| 327 |
} |
| 328 |
|
| 329 |
public void testRepeatedErrorString1() { |
| 330 |
testDebugStrings( |
| 331 |
"Error('abc');Error('def');Error('abc');", |
| 332 |
"Error('a');Error('b');Error('a');", |
| 333 |
(new String[] { "a", "abc", "b", "def" })); |
| 334 |
} |
| 335 |
|
| 336 |
public void testRepeatedErrorString2() { |
| 337 |
testDebugStrings( |
| 338 |
"Error('a:' + u + ', b:' + v); Error('a:' + x + ', b:' + y);", |
| 339 |
"Error('a' + '`' + u + '`' + v); Error('a' + '`' + x + '`' + y);", |
| 340 |
(new String[] { "a", "a:`, b:`" })); |
| 341 |
} |
| 342 |
|
| 343 |
public void testRepeatedErrorString3() { |
| 344 |
testDebugStrings( |
| 345 |
"var AB = 'b'; throw Error(AB); throw Error(AB);", |
| 346 |
"var AB = 'b'; throw Error('a'); throw Error('a');", |
| 347 |
(new String[] { "a", "b" })); |
| 348 |
} |
| 349 |
|
| 350 |
public void testRepeatedTracerString() { |
| 351 |
testDebugStrings( |
| 352 |
"goog$debug$Trace.startTracer('A', 'B', 'A');", |
| 353 |
"goog$debug$Trace.startTracer('a', 'b', 'a');", |
| 354 |
(new String[] { "a", "A", "b", "B" })); |
| 355 |
} |
| 356 |
|
| 357 |
public void testRepeatedLoggerString() { |
| 358 |
testDebugStrings( |
| 359 |
"goog$debug$Logger$getLogger('goog.net.XhrTransport');" + |
| 360 |
"goog$debug$Logger$getLogger('my.app.Application');" + |
| 361 |
"goog$debug$Logger$getLogger('my.app.Application');", |
| 362 |
"goog$debug$Logger$getLogger('a');" + |
| 363 |
"goog$debug$Logger$getLogger('b');" + |
| 364 |
"goog$debug$Logger$getLogger('b');", |
| 365 |
new String[] { |
| 366 |
"a", "goog.net.XhrTransport","b", "my.app.Application" }); |
| 367 |
} |
| 368 |
|
| 369 |
public void testRepeatedStringsWithDifferentMethods() { |
| 370 |
test( |
| 371 |
"throw Error('A');" |
| 372 |
+ "goog$debug$Trace.startTracer('B', 'A');" |
| 373 |
+ "goog$debug$Logger$getLogger('C');" |
| 374 |
+ "goog$debug$Logger$getLogger('B');" |
| 375 |
+ "goog$debug$Logger$getLogger('A');" |
| 376 |
+ "throw Error('D');" |
| 377 |
+ "throw Error('C');" |
| 378 |
+ "throw Error('B');" |
| 379 |
+ "throw Error('A');", |
| 380 |
"throw Error('a');" |
| 381 |
+ "goog$debug$Trace.startTracer('b', 'a');" |
| 382 |
+ "goog$debug$Logger$getLogger('c');" |
| 383 |
+ "goog$debug$Logger$getLogger('b');" |
| 384 |
+ "goog$debug$Logger$getLogger('a');" |
| 385 |
+ "throw Error('d');" |
| 386 |
+ "throw Error('c');" |
| 387 |
+ "throw Error('b');" |
| 388 |
+ "throw Error('a');"); |
| 389 |
} |
| 390 |
|
| 391 |
public void testReserved() { |
| 392 |
testDebugStrings( |
| 393 |
"throw Error('xyz');", |
| 394 |
"throw Error('a');", |
| 395 |
(new String[] { "a", "xyz" })); |
| 396 |
reserved = ImmutableSet.of("a", "b", "c"); |
| 397 |
testDebugStrings( |
| 398 |
"throw Error('xyz');", |
| 399 |
"throw Error('d');", |
| 400 |
(new String[] { "d", "xyz" })); |
| 401 |
} |
| 402 |
|
| 403 |
private void testDebugStrings(String js, String expected, |
| 404 |
String[] substitutedStrings) { |
| 405 |
|
| 406 |
test(js, expected); |
| 407 |
|
| 408 |
List<Result> results = pass.getResult(); |
| 409 |
assertTrue(substitutedStrings.length % 2 == 0); |
| 410 |
assertEquals(substitutedStrings.length/2, results.size()); |
| 411 |
|
| 412 |
|
| 413 |
for (int i = 0; i < substitutedStrings.length; i += 2) { |
| 414 |
Result result = results.get(i/2); |
| 415 |
String original = substitutedStrings[i + 1]; |
| 416 |
assertEquals(original, result.original); |
| 417 |
|
| 418 |
String replacement = substitutedStrings[i]; |
| 419 |
assertEquals(replacement, result.replacement); |
| 420 |
} |
| 421 |
} |
| 422 |
} |