| 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.base.Joiner; |
| 20 |
import com.google.common.base.Preconditions; |
| 21 |
import com.google.common.base.Predicates; |
| 22 |
import com.google.common.collect.ImmutableList; |
| 23 |
import com.google.common.collect.Lists; |
| 24 |
import com.google.javascript.jscomp.CompilerOptions.LanguageMode; |
| 25 |
import com.google.javascript.jscomp.type.ReverseAbstractInterpreter; |
| 26 |
import com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter; |
| 27 |
import com.google.javascript.rhino.Node; |
| 28 |
import com.google.javascript.rhino.testing.BaseJSTypeTestCase; |
| 29 |
|
| 30 |
import junit.framework.TestCase; |
| 31 |
|
| 32 |
import java.io.IOException; |
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public abstract class CompilerTestCase extends TestCase { |
| 46 |
|
| 47 |
|
| 48 |
private final List<SourceFile> externsInputs; |
| 49 |
|
| 50 |
|
| 51 |
private final boolean compareAsTree; |
| 52 |
|
| 53 |
|
| 54 |
protected boolean parseTypeInfo; |
| 55 |
|
| 56 |
|
| 57 |
private boolean allowSourcelessWarnings = false; |
| 58 |
|
| 59 |
|
| 60 |
private boolean closurePassEnabled = false; |
| 61 |
|
| 62 |
|
| 63 |
private boolean typeCheckEnabled = false; |
| 64 |
|
| 65 |
|
| 66 |
private CheckLevel typeCheckLevel; |
| 67 |
|
| 68 |
|
| 69 |
private boolean normalizeEnabled = false; |
| 70 |
|
| 71 |
|
| 72 |
private boolean normalizeExpected = false; |
| 73 |
|
| 74 |
|
| 75 |
private boolean checkLineNumbers = true; |
| 76 |
|
| 77 |
|
| 78 |
private boolean expectParseWarningsThisTest = false; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private DiagnosticType expectedSymbolTableError = null; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private boolean markNoSideEffects = false; |
| 90 |
|
| 91 |
|
| 92 |
private Compiler lastCompiler; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
private boolean acceptES5 = true; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private boolean allowExternsChanges = false; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
private boolean astValidationEnabled = true; |
| 108 |
|
| 109 |
private String filename = "testcode"; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
protected CompilerTestCase(String externs, boolean compareAsTree) { |
| 129 |
this.externsInputs = ImmutableList.of( |
| 130 |
SourceFile.fromCode("externs", externs)); |
| 131 |
this.compareAsTree = compareAsTree; |
| 132 |
this.parseTypeInfo = false; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
protected CompilerTestCase(String externs) { |
| 140 |
this(externs, true); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected CompilerTestCase() { |
| 147 |
this("", true); |
| 148 |
} |
| 149 |
|
| 150 |
@Override protected void tearDown() throws Exception { |
| 151 |
super.tearDown(); |
| 152 |
expectParseWarningsThisTest = false; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
protected abstract CompilerPass getProcessor(Compiler compiler); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
protected CompilerOptions getOptions() { |
| 169 |
return getOptions(new CompilerOptions()); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
protected CompilerOptions getOptions(CompilerOptions options) { |
| 177 |
if (this.acceptES5) { |
| 178 |
options.setLanguageIn(LanguageMode.ECMASCRIPT5); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
options.checkSymbols = true; |
| 184 |
|
| 185 |
options.setWarningLevel( |
| 186 |
DiagnosticGroups.MISSING_PROPERTIES, CheckLevel.WARNING); |
| 187 |
options.setWarningLevel( |
| 188 |
DiagnosticGroups.INVALID_CASTS, CheckLevel.WARNING); |
| 189 |
options.setCodingConvention(getCodingConvention()); |
| 190 |
return options; |
| 191 |
} |
| 192 |
|
| 193 |
protected CodingConvention getCodingConvention() { |
| 194 |
return new GoogleCodingConvention(); |
| 195 |
} |
| 196 |
|
| 197 |
public void setFilename(String filename) { |
| 198 |
this.filename = filename; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
protected int getNumRepetitions() { |
| 206 |
|
| 207 |
|
| 208 |
return 2; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
void allowSourcelessWarnings() { |
| 213 |
allowSourcelessWarnings = true; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
Compiler getLastCompiler() { |
| 218 |
return lastCompiler; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
protected void enableEcmaScript5(boolean acceptES5) { |
| 225 |
this.acceptES5 = acceptES5; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
protected void allowExternsChanges(boolean allowExternsChanges) { |
| 232 |
this.allowExternsChanges = allowExternsChanges; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public void enableTypeCheck(CheckLevel level) { |
| 244 |
typeCheckEnabled = true; |
| 245 |
typeCheckLevel = level; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public void enableLineNumberCheck(boolean newVal) { |
| 252 |
checkLineNumbers = newVal; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
void disableTypeCheck() { |
| 261 |
typeCheckEnabled = false; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
void enableClosurePass() { |
| 269 |
closurePassEnabled = true; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
protected void enableNormalize() { |
| 279 |
enableNormalize(true); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
protected void enableNormalize(boolean normalizeExpected) { |
| 291 |
normalizeEnabled = true; |
| 292 |
this.normalizeExpected = normalizeExpected; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
protected void disableNormalize() { |
| 300 |
normalizeEnabled = false; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
void enableMarkNoSideEffects() { |
| 309 |
markNoSideEffects = true; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
protected void enableAstValidation(boolean validate) { |
| 316 |
astValidationEnabled = validate; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
protected void setExpectParseWarningsThisTest() { |
| 321 |
expectParseWarningsThisTest = true; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
private static TypeCheck createTypeCheck(Compiler compiler, |
| 326 |
CheckLevel level) { |
| 327 |
ReverseAbstractInterpreter rai = |
| 328 |
new SemanticReverseAbstractInterpreter(compiler.getCodingConvention(), |
| 329 |
compiler.getTypeRegistry()); |
| 330 |
|
| 331 |
return new TypeCheck(compiler, rai, compiler.getTypeRegistry(), |
| 332 |
level, CheckLevel.OFF); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public void test(String js, String expected) { |
| 342 |
test(js, expected, (DiagnosticType) null); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public void test(String js, String expected, DiagnosticType error) { |
| 354 |
test(js, expected, error, null); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public void test(String js, String expected, DiagnosticType error, |
| 369 |
DiagnosticType warning, String description) { |
| 370 |
test(externsInputs, js, expected, error, warning, description); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
public void test(String js, String expected, |
| 384 |
DiagnosticType error, DiagnosticType warning) { |
| 385 |
test(externsInputs, js, expected, error, warning, null); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public void test(String externs, String js, String expected, |
| 400 |
DiagnosticType error, DiagnosticType warning) { |
| 401 |
test(externs, js, expected, error, warning, null); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public void test(String externs, String js, String expected, |
| 419 |
DiagnosticType error, DiagnosticType warning, |
| 420 |
String description) { |
| 421 |
List<SourceFile> externsInputs = ImmutableList.of( |
| 422 |
SourceFile.fromCode("externs", externs)); |
| 423 |
test(externsInputs, js, expected, error, warning, description); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public void test(List<SourceFile> externs, String js, String expected, |
| 441 |
DiagnosticType error, |
| 442 |
DiagnosticType warning, String description) { |
| 443 |
Compiler compiler = createCompiler(); |
| 444 |
lastCompiler = compiler; |
| 445 |
|
| 446 |
CompilerOptions options = getOptions(); |
| 447 |
|
| 448 |
if (this.acceptES5) { |
| 449 |
options.setLanguageIn(LanguageMode.ECMASCRIPT5); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
options.checkTypes = parseTypeInfo; |
| 454 |
compiler.init(externs, ImmutableList.of( |
| 455 |
SourceFile.fromCode(filename, js)), options); |
| 456 |
|
| 457 |
BaseJSTypeTestCase.addNativeProperties(compiler.getTypeRegistry()); |
| 458 |
|
| 459 |
test(compiler, maybeCreateArray(expected), error, warning, description); |
| 460 |
} |
| 461 |
|
| 462 |
private String[] maybeCreateArray(String expected) { |
| 463 |
if (expected != null) { |
| 464 |
return new String[] { expected }; |
| 465 |
} |
| 466 |
return null; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
public void test(String[] js, String[] expected) { |
| 476 |
test(js, expected, null); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
public void test(String[] js, String[] expected, DiagnosticType error) { |
| 488 |
test(js, expected, error, null); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public void test(String[] js, String[] expected, DiagnosticType error, |
| 502 |
DiagnosticType warning) { |
| 503 |
test(js, expected, error, warning, null); |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public void test(String[] js, String[] expected, DiagnosticType error, |
| 520 |
DiagnosticType warning, String description) { |
| 521 |
Compiler compiler = createCompiler(); |
| 522 |
lastCompiler = compiler; |
| 523 |
|
| 524 |
List<SourceFile> inputs = Lists.newArrayList(); |
| 525 |
for (int i = 0; i < js.length; i++) { |
| 526 |
inputs.add(SourceFile.fromCode("input" + i, js[i])); |
| 527 |
} |
| 528 |
compiler.init(externsInputs, inputs, getOptions()); |
| 529 |
test(compiler, expected, error, warning, description); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
public void test(JSModule[] modules, String[] expected) { |
| 539 |
test(modules, expected, null); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
public void test(JSModule[] modules, String[] expected, |
| 551 |
DiagnosticType error) { |
| 552 |
test(modules, expected, error, null); |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
public void test(JSModule[] modules, String[] expected, |
| 566 |
DiagnosticType error, DiagnosticType warning) { |
| 567 |
Compiler compiler = createCompiler(); |
| 568 |
lastCompiler = compiler; |
| 569 |
|
| 570 |
compiler.initModules( |
| 571 |
externsInputs, Lists.newArrayList(modules), getOptions()); |
| 572 |
test(compiler, expected, error, warning); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public void testSame(String js) { |
| 581 |
test(js, js); |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
public void testSame(String js, DiagnosticType warning) { |
| 592 |
test(js, js, null, warning); |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
public void testSame(String js, DiagnosticType diag, boolean error) { |
| 604 |
if (error) { |
| 605 |
test(js, js, diag); |
| 606 |
} else { |
| 607 |
test(js, js, null, diag); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
public void testSame(String externs, String js, DiagnosticType warning) { |
| 620 |
testSame(externs, js, warning, null); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
public void testSame( |
| 633 |
String externs, String js, DiagnosticType diag, boolean error) { |
| 634 |
if (error) { |
| 635 |
test(externs, js, js, diag, null); |
| 636 |
} else { |
| 637 |
test(externs, js, js, null, diag); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
public void testSame(String externs, String js, DiagnosticType warning, |
| 653 |
String description) { |
| 654 |
List<SourceFile> externsInputs = ImmutableList.of( |
| 655 |
SourceFile.fromCode("externs", externs)); |
| 656 |
test(externsInputs, js, js, null, warning, description); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
public void testSame(String[] js) { |
| 665 |
test(js, js); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
public void testSame(String[] js, DiagnosticType error) { |
| 676 |
test(js, js, error); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
public void testSame( |
| 688 |
String[] js, DiagnosticType error, DiagnosticType warning) { |
| 689 |
test(js, js, error, warning); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
public void testSame(JSModule[] modules) { |
| 698 |
testSame(modules, null); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
public void testSame(JSModule[] modules, DiagnosticType warning) { |
| 708 |
try { |
| 709 |
String[] expected = new String[modules.length]; |
| 710 |
for (int i = 0; i < modules.length; i++) { |
| 711 |
expected[i] = ""; |
| 712 |
for (CompilerInput input : modules[i].getInputs()) { |
| 713 |
expected[i] += input.getSourceFile().getCode(); |
| 714 |
} |
| 715 |
} |
| 716 |
test(modules, expected, null, warning); |
| 717 |
} catch (IOException e) { |
| 718 |
throw new RuntimeException(e); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
protected void test(Compiler compiler, String[] expected, |
| 734 |
DiagnosticType error, DiagnosticType warning) { |
| 735 |
test(compiler, expected, error, warning, null); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
private void test(Compiler compiler, String[] expected, |
| 754 |
DiagnosticType error, DiagnosticType warning, |
| 755 |
String description) { |
| 756 |
CodeChangeHandler recentChange = new CodeChangeHandler(); |
| 757 |
compiler.addChangeHandler(recentChange); |
| 758 |
|
| 759 |
Node root = compiler.parseInputs(); |
| 760 |
assertTrue("Unexpected parse error(s): " + |
| 761 |
Joiner.on("\n").join(compiler.getErrors()), root != null); |
| 762 |
if (!expectParseWarningsThisTest) { |
| 763 |
assertTrue("Unexpected parse warnings(s): " + |
| 764 |
Joiner.on("\n").join(compiler.getWarnings()), |
| 765 |
compiler.getWarnings().length == 0); |
| 766 |
} |
| 767 |
|
| 768 |
if (astValidationEnabled) { |
| 769 |
(new AstValidator()).validateRoot(root); |
| 770 |
} |
| 771 |
Node externsRoot = root.getFirstChild(); |
| 772 |
Node mainRoot = root.getLastChild(); |
| 773 |
|
| 774 |
|
| 775 |
Node rootClone = root.cloneTree(); |
| 776 |
Node externsRootClone = rootClone.getFirstChild(); |
| 777 |
Node mainRootClone = rootClone.getLastChild(); |
| 778 |
|
| 779 |
int numRepetitions = getNumRepetitions(); |
| 780 |
ErrorManager[] errorManagers = new ErrorManager[numRepetitions]; |
| 781 |
int aggregateWarningCount = 0; |
| 782 |
List<JSError> aggregateWarnings = Lists.newArrayList(); |
| 783 |
boolean hasCodeChanged = false; |
| 784 |
|
| 785 |
assertFalse("Code should not change before processing", |
| 786 |
recentChange.hasCodeChanged()); |
| 787 |
|
| 788 |
for (int i = 0; i < numRepetitions; ++i) { |
| 789 |
if (compiler.getErrorCount() == 0) { |
| 790 |
errorManagers[i] = new BlackHoleErrorManager(compiler); |
| 791 |
|
| 792 |
|
| 793 |
if (closurePassEnabled && i == 0) { |
| 794 |
recentChange.reset(); |
| 795 |
new ProcessClosurePrimitives(compiler, null, CheckLevel.ERROR) |
| 796 |
.process(null, mainRoot); |
| 797 |
hasCodeChanged = hasCodeChanged || recentChange.hasCodeChanged(); |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
if (typeCheckEnabled && i == 0) { |
| 805 |
TypeCheck check = createTypeCheck(compiler, typeCheckLevel); |
| 806 |
check.processForTesting(externsRoot, mainRoot); |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
if (normalizeEnabled && i == 0) { |
| 811 |
normalizeActualCode(compiler, externsRoot, mainRoot); |
| 812 |
} |
| 813 |
|
| 814 |
if (markNoSideEffects && i == 0) { |
| 815 |
MarkNoSideEffectCalls mark = new MarkNoSideEffectCalls(compiler); |
| 816 |
mark.process(externsRoot, mainRoot); |
| 817 |
} |
| 818 |
|
| 819 |
recentChange.reset(); |
| 820 |
|
| 821 |
getProcessor(compiler).process(externsRoot, mainRoot); |
| 822 |
if (astValidationEnabled) { |
| 823 |
(new AstValidator()).validateRoot(root); |
| 824 |
} |
| 825 |
if (checkLineNumbers) { |
| 826 |
(new LineNumberCheck(compiler)).process(externsRoot, mainRoot); |
| 827 |
} |
| 828 |
|
| 829 |
hasCodeChanged = hasCodeChanged || recentChange.hasCodeChanged(); |
| 830 |
aggregateWarningCount += errorManagers[i].getWarningCount(); |
| 831 |
aggregateWarnings.addAll(Lists.newArrayList(compiler.getWarnings())); |
| 832 |
|
| 833 |
if (normalizeEnabled) { |
| 834 |
boolean verifyDeclaredConstants = true; |
| 835 |
new Normalize.VerifyConstants(compiler, verifyDeclaredConstants) |
| 836 |
.process(externsRoot, mainRoot); |
| 837 |
} |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
if (error == null) { |
| 842 |
assertEquals( |
| 843 |
"Unexpected error(s): " + Joiner.on("\n").join(compiler.getErrors()), |
| 844 |
0, compiler.getErrorCount()); |
| 845 |
|
| 846 |
|
| 847 |
ErrorManager symbolTableErrorManager = |
| 848 |
new BlackHoleErrorManager(compiler); |
| 849 |
Node expectedRoot = null; |
| 850 |
if (expected != null) { |
| 851 |
expectedRoot = parseExpectedJs(expected); |
| 852 |
expectedRoot.detachFromParent(); |
| 853 |
} |
| 854 |
|
| 855 |
JSError[] stErrors = symbolTableErrorManager.getErrors(); |
| 856 |
if (expectedSymbolTableError != null) { |
| 857 |
assertEquals("There should be one error.", 1, stErrors.length); |
| 858 |
assertEquals(expectedSymbolTableError, stErrors[0].getType()); |
| 859 |
} else { |
| 860 |
assertEquals("Unexpected symbol table error(s): " + |
| 861 |
Joiner.on("\n").join(stErrors), |
| 862 |
0, stErrors.length); |
| 863 |
} |
| 864 |
|
| 865 |
if (warning == null) { |
| 866 |
assertEquals( |
| 867 |
"Unexpected warning(s): " + Joiner.on("\n").join(aggregateWarnings), |
| 868 |
0, aggregateWarningCount); |
| 869 |
} else { |
| 870 |
assertEquals("There should be one warning, repeated " + numRepetitions + |
| 871 |
" time(s).", numRepetitions, aggregateWarningCount); |
| 872 |
for (int i = 0; i < numRepetitions; ++i) { |
| 873 |
JSError[] warnings = errorManagers[i].getWarnings(); |
| 874 |
JSError actual = warnings[0]; |
| 875 |
assertEquals(warning, actual.getType()); |
| 876 |
|
| 877 |
|
| 878 |
if (!allowSourcelessWarnings) { |
| 879 |
assertTrue("Missing source file name in warning", |
| 880 |
actual.sourceName != null && !actual.sourceName.isEmpty()); |
| 881 |
assertTrue("Missing line number in warning", |
| 882 |
-1 != actual.lineNumber); |
| 883 |
assertTrue("Missing char number in warning", |
| 884 |
-1 != actual.getCharno()); |
| 885 |
} |
| 886 |
|
| 887 |
if (description != null) { |
| 888 |
assertEquals(description, actual.description); |
| 889 |
} |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
if (normalizeEnabled) { |
| 896 |
normalizeActualCode(compiler, externsRootClone, mainRootClone); |
| 897 |
} |
| 898 |
|
| 899 |
boolean codeChange = !mainRootClone.isEquivalentTo(mainRoot); |
| 900 |
boolean externsChange = !externsRootClone.isEquivalentTo(externsRoot); |
| 901 |
|
| 902 |
|
| 903 |
if (externsChange && !allowExternsChanges) { |
| 904 |
String explanation = externsRootClone.checkTreeEquals(externsRoot); |
| 905 |
fail("Unexpected changes to externs" + |
| 906 |
"\nExpected: " + compiler.toSource(externsRootClone) + |
| 907 |
"\nResult: " + compiler.toSource(externsRoot) + |
| 908 |
"\n" + explanation); |
| 909 |
} |
| 910 |
|
| 911 |
if (!codeChange && !externsChange) { |
| 912 |
assertFalse( |
| 913 |
"compiler.reportCodeChange() was called " + |
| 914 |
"even though nothing changed", |
| 915 |
hasCodeChanged); |
| 916 |
} else { |
| 917 |
assertTrue("compiler.reportCodeChange() should have been called", |
| 918 |
hasCodeChanged); |
| 919 |
} |
| 920 |
|
| 921 |
if (expected != null) { |
| 922 |
if (compareAsTree) { |
| 923 |
String explanation = expectedRoot.checkTreeEquals(mainRoot); |
| 924 |
assertNull("\nExpected: " + compiler.toSource(expectedRoot) + |
| 925 |
"\nResult: " + compiler.toSource(mainRoot) + |
| 926 |
"\n" + explanation, explanation); |
| 927 |
} else if (expected != null) { |
| 928 |
assertEquals( |
| 929 |
Joiner.on("").join(expected), compiler.toSource(mainRoot)); |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
Node normalizeCheckRootClone = root.cloneTree(); |
| 935 |
Node normalizeCheckExternsRootClone = normalizeCheckRootClone.getFirstChild(); |
| 936 |
Node normalizeCheckMainRootClone = normalizeCheckRootClone.getLastChild(); |
| 937 |
new PrepareAst(compiler).process( |
| 938 |
normalizeCheckExternsRootClone, normalizeCheckMainRootClone); |
| 939 |
String explanation = |
| 940 |
normalizeCheckMainRootClone.checkTreeEquals(mainRoot); |
| 941 |
assertNull("Node structure normalization invalidated.\nExpected: " + |
| 942 |
compiler.toSource(normalizeCheckMainRootClone) + |
| 943 |
"\nResult: " + compiler.toSource(mainRoot) + |
| 944 |
"\n" + explanation, explanation); |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
if (normalizeEnabled) { |
| 952 |
new Normalize(compiler, true).process( |
| 953 |
normalizeCheckExternsRootClone, normalizeCheckMainRootClone); |
| 954 |
explanation = normalizeCheckMainRootClone.checkTreeEquals(mainRoot); |
| 955 |
assertNull("Normalization invalidated.\nExpected: " + |
| 956 |
compiler.toSource(normalizeCheckMainRootClone) + |
| 957 |
"\nResult: " + compiler.toSource(mainRoot) + |
| 958 |
"\n" + explanation, explanation); |
| 959 |
} |
| 960 |
} else { |
| 961 |
String errors = ""; |
| 962 |
for (JSError actualError : compiler.getErrors()) { |
| 963 |
errors += actualError.description + "\n"; |
| 964 |
} |
| 965 |
assertEquals("There should be one error. " + errors, |
| 966 |
1, compiler.getErrorCount()); |
| 967 |
assertEquals(errors, error, compiler.getErrors()[0].getType()); |
| 968 |
|
| 969 |
if (warning != null) { |
| 970 |
String warnings = ""; |
| 971 |
for (JSError actualError : compiler.getWarnings()) { |
| 972 |
warnings += actualError.description + "\n"; |
| 973 |
} |
| 974 |
assertEquals("There should be one warning. " + warnings, |
| 975 |
1, compiler.getWarningCount()); |
| 976 |
assertEquals(warnings, warning, compiler.getWarnings()[0].getType()); |
| 977 |
} |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
private void normalizeActualCode( |
| 982 |
Compiler compiler, Node externsRoot, Node mainRoot) { |
| 983 |
Normalize normalize = new Normalize(compiler, false); |
| 984 |
normalize.process(externsRoot, mainRoot); |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
protected Node parseExpectedJs(String[] expected) { |
| 991 |
Compiler compiler = createCompiler(); |
| 992 |
List<SourceFile> inputs = Lists.newArrayList(); |
| 993 |
for (int i = 0; i < expected.length; i++) { |
| 994 |
inputs.add(SourceFile.fromCode("expected" + i, expected[i])); |
| 995 |
} |
| 996 |
compiler.init(externsInputs, inputs, getOptions()); |
| 997 |
Node root = compiler.parseInputs(); |
| 998 |
assertTrue("Unexpected parse error(s): " + |
| 999 |
Joiner.on("\n").join(compiler.getErrors()), root != null); |
| 1000 |
Node externsRoot = root.getFirstChild(); |
| 1001 |
Node mainRoot = externsRoot.getNext(); |
| 1002 |
|
| 1003 |
if (normalizeEnabled && normalizeExpected && !compiler.hasErrors()) { |
| 1004 |
Normalize normalize = new Normalize(compiler, false); |
| 1005 |
normalize.process(externsRoot, mainRoot); |
| 1006 |
} |
| 1007 |
return mainRoot; |
| 1008 |
} |
| 1009 |
|
| 1010 |
protected Node parseExpectedJs(String expected) { |
| 1011 |
return parseExpectedJs(new String[] {expected}); |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
static JSModule[] createModuleChain(String... inputs) { |
| 1019 |
JSModule[] modules = createModules(inputs); |
| 1020 |
for (int i = 1; i < modules.length; i++) { |
| 1021 |
modules[i].addDependency(modules[i - 1]); |
| 1022 |
} |
| 1023 |
return modules; |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
static JSModule[] createModuleStar(String... inputs) { |
| 1031 |
JSModule[] modules = createModules(inputs); |
| 1032 |
for (int i = 1; i < modules.length; i++) { |
| 1033 |
modules[i].addDependency(modules[0]); |
| 1034 |
} |
| 1035 |
return modules; |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
static JSModule[] createModuleBush(String ... inputs) { |
| 1044 |
Preconditions.checkState(inputs.length > 2); |
| 1045 |
JSModule[] modules = createModules(inputs); |
| 1046 |
for (int i = 1; i < modules.length; i++) { |
| 1047 |
modules[i].addDependency(modules[i == 1 ? 0 : 1]); |
| 1048 |
} |
| 1049 |
return modules; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
static JSModule[] createModuleTree(String ... inputs) { |
| 1058 |
JSModule[] modules = createModules(inputs); |
| 1059 |
for (int i = 1; i < modules.length; i++) { |
| 1060 |
modules[i].addDependency(modules[(i - 1) / 2]); |
| 1061 |
} |
| 1062 |
return modules; |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
static JSModule[] createModules(String... inputs) { |
| 1070 |
JSModule[] modules = new JSModule[inputs.length]; |
| 1071 |
for (int i = 0; i < inputs.length; i++) { |
| 1072 |
JSModule module = modules[i] = new JSModule("m" + i); |
| 1073 |
module.add(SourceFile.fromCode("i" + i, inputs[i])); |
| 1074 |
} |
| 1075 |
return modules; |
| 1076 |
} |
| 1077 |
|
| 1078 |
private static class BlackHoleErrorManager extends BasicErrorManager { |
| 1079 |
private BlackHoleErrorManager(Compiler compiler) { |
| 1080 |
compiler.setErrorManager(this); |
| 1081 |
} |
| 1082 |
|
| 1083 |
@Override |
| 1084 |
public void println(CheckLevel level, JSError error) {} |
| 1085 |
|
| 1086 |
@Override |
| 1087 |
public void printSummary() {} |
| 1088 |
} |
| 1089 |
|
| 1090 |
Compiler createCompiler() { |
| 1091 |
Compiler compiler = new Compiler(); |
| 1092 |
return compiler; |
| 1093 |
} |
| 1094 |
|
| 1095 |
protected void setExpectedSymbolTableError(DiagnosticType type) { |
| 1096 |
this.expectedSymbolTableError = type; |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
protected final Node findQualifiedNameNode(final String name, Node root) { |
| 1101 |
final List<Node> matches = Lists.newArrayList(); |
| 1102 |
NodeUtil.visitPostOrder(root, |
| 1103 |
new NodeUtil.Visitor() { |
| 1104 |
@Override public void visit(Node n) { |
| 1105 |
if (name.equals(n.getQualifiedName())) { |
| 1106 |
matches.add(n); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
}, |
| 1110 |
Predicates.<Node>alwaysTrue()); |
| 1111 |
return matches.get(0); |
| 1112 |
} |
| 1113 |
} |