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