-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CompilerTestCase.java
2202 lines (1936 loc) · 73.3 KB
/
CompilerTestCase.java
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2006 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 static com.google.common.base.Preconditions.checkState;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.javascript.jscomp.testing.JSErrorSubject.assertError;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.ForOverride;
import com.google.javascript.jscomp.AbstractCompiler.MostRecentTypechecker;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.deps.ModuleLoader;
import com.google.javascript.jscomp.testing.BlackHoleErrorManager;
import com.google.javascript.jscomp.type.ReverseAbstractInterpreter;
import com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import junit.framework.TestCase;
/**
* <p>Base class for testing JS compiler classes that change
* the node tree of a compiled JS input.</p>
*
* <p>Pulls in shared functionality from different test cases. Also supports
* node tree comparison for input and output (instead of string comparison),
* which makes it easier to write tests b/c you don't have to get the syntax
* exactly correct to the spacing.</p>
*
*/
public abstract class CompilerTestCase extends TestCase {
protected static final Joiner LINE_JOINER = Joiner.on('\n');
// TODO(sdh): Remove this option if there's never a reason to turn it on.
private final boolean emitUseStrict = false;
/** Externs for the test */
private final List<SourceFile> externsInputs;
/** Whether to compare input and output as trees instead of strings */
private boolean compareAsTree;
/** Whether to parse type info from JSDoc comments */
private boolean parseTypeInfo;
/** Whether to take JSDoc into account when comparing ASTs. */
private boolean compareJsDoc;
/** Whether we check warnings without source information. */
private boolean allowSourcelessWarnings;
/** True iff closure pass runs before pass being tested. */
private boolean closurePassEnabled;
/** Whether the closure pass is run on the expected JS. */
private boolean closurePassEnabledForExpected;
/** Whether to rewrite Closure code before the test is run. */
private boolean rewriteClosureCode;
/**
* If true, run type checking together with the pass being tested. A separate
* flag controls whether type checking runs before or after the pass.
*/
private boolean typeCheckEnabled;
/**
* If true, run NTI together with the pass being tested. A separate
* flag controls whether NTI runs before or after the pass.
*/
private boolean newTypeInferenceEnabled;
/**
* If true performs the test using multistage compilation.
*/
private boolean multistageCompilation;
/** Whether to test the compiler pass before the type check. */
private boolean runTypeCheckAfterProcessing;
/** Whether to test the compiler pass before NTI. */
private boolean runNTIAfterProcessing;
/** Whether to scan externs for property names. */
private boolean gatherExternPropertiesEnabled;
/**
* Whether the Normalize pass runs before pass being tested and
* whether the expected JS strings should be normalized.
*/
private boolean normalizeEnabled;
private boolean polymerPass;
/** Whether the tranpilation passes runs before pass being tested. */
private boolean transpileEnabled;
/** Whether we run InferConsts before checking. */
private boolean inferConsts;
/** Whether we run CheckAccessControls after the pass being tested. */
private boolean checkAccessControls;
/** Whether to check that all line number information is preserved. */
private boolean checkLineNumbers;
/** Whether to check that changed scopes are marked as changed */
private boolean checkAstChangeMarking;
/** Whether we expect parse warnings in the current test. */
private boolean expectParseWarningsThisTest;
/**
* An expected symbol table error. Only useful for testing the
* symbol table error-handling.
*/
private DiagnosticType expectedSymbolTableError;
/**
* Whether the MarkNoSideEffectsCalls pass runs before the pass being tested
*/
private boolean markNoSideEffects;
/**
* Whether the PureFunctionIdentifier pass runs before the pass being tested
*/
private boolean computeSideEffects;
/** The most recently used Compiler instance. */
private Compiler lastCompiler;
/**
* Whether to accept ES6, ES5 or ES3 source.
*/
private LanguageMode acceptedLanguage;
private LanguageMode languageOut;
/** How to interpret ES6 module imports */
private ModuleLoader.ResolutionMode moduleResolutionMode;
/**
* Whether externs changes should be allowed for this pass.
*/
private boolean allowExternsChanges;
/**
* Whether the AST should be validated.
*/
private boolean astValidationEnabled;
private String filename = "testcode";
private final Set<DiagnosticType> ignoredWarnings = new HashSet<>();
/** Whether {@link #setUp} has run. */
private boolean setUpRan = false;
protected static final String ACTIVE_X_OBJECT_DEF =
LINE_JOINER.join(
"/**",
" * @param {string} progId",
" * @param {string=} opt_location",
" * @constructor",
" * @see http://msdn.microsoft.com/en-us/library/7sw4ddf8.aspx",
" */",
"function ActiveXObject(progId, opt_location) {}");
/** A minimal set of externs, consisting of only those needed for NTI not to blow up. */
protected static final String MINIMAL_EXTERNS =
LINE_JOINER.join(
"/**",
" * @constructor",
" * @param {*=} opt_value",
" * @return {!Object}",
" */",
"function Object(opt_value) {}",
"/**",
" * @constructor",
" * @param {...*} var_args",
" */",
"function Function(var_args) {}",
"/**",
" * @constructor",
" * @param {*=} arg",
" * @return {string}",
" */",
"function String(arg) {}",
"/**",
" * @interface",
" * @template VALUE",
" */",
"function Iterable() {}",
"/**",
" * @interface",
" * @template T",
" */",
"function Iterator() {}",
"/**",
" * @return {T}",
" */",
"Iterator.prototype.next = function() {}",
"/**",
" * @interface",
" * @template VALUE",
" */",
"function IteratorIterable() {}",
"/**",
" * @interface",
" * @template KEY1, VALUE1",
" */",
"function IObject() {};",
"/**",
" * @record",
" * @extends IObject<number, VALUE2>",
" * @template VALUE2",
" */",
"function IArrayLike() {};",
"/**",
" * @template T",
" * @constructor ",
" * @implements {IArrayLike<T>} ",
" * @implements {Iterable<T>}",
" * @param {...*} var_args",
" * @return {!Array.<?>}",
" */",
"function Array(var_args) {}");
/** A default set of externs for testing. */
protected static final String DEFAULT_EXTERNS =
LINE_JOINER.join(
MINIMAL_EXTERNS,
"/**",
" * @type{number}",
" */",
"IArrayLike.prototype.length;",
"/** @return {string} */",
"Object.prototype.toString = function() {};",
"/**",
" * @param {*} propertyName",
" * @return {boolean}",
" */",
"Object.prototype.hasOwnProperty = function(propertyName) {};",
"/** @type {?Function} */ Object.prototype.constructor;",
"Object.defineProperties = function(obj, descriptors) {};",
"/** @type {!Function} */ Function.prototype.apply;",
"/** @type {!Function} */ Function.prototype.bind;",
"/** @type {!Function} */ Function.prototype.call;",
"/** @type {number} */",
"Function.prototype.length;",
"/** @type {string} */",
"Function.prototype.name;",
"/** @param {number} sliceArg */",
"String.prototype.slice = function(sliceArg) {};",
"/**",
" * @this {?String|string}",
" * @param {?} regex",
" * @param {?} str",
" * @param {string=} opt_flags",
" * @return {string}",
" */",
"String.prototype.replace = function(regex, str, opt_flags) {};",
"/** @type {number} */ String.prototype.length;",
"/**",
" * @constructor",
" * @param {*=} arg",
" * @return {number}",
" */",
"function Number(arg) {}",
"/**",
" * @constructor",
" * @param {*=} arg",
" * @return {boolean}",
" */",
"function Boolean(arg) {}",
"/** @type {number} */ Array.prototype.length;",
"/**",
" * @param {...T} var_args",
" * @return {number} The new length of the array.",
" * @this {{length: number}|!Array.<T>}",
" * @template T",
" * @modifies {this}",
" */",
"Array.prototype.push = function(var_args) {};",
"/**",
" * @this {IArrayLike<T>}",
" * @return {T}",
" * @template T",
" */",
"Array.prototype.shift = function() {};",
"/**",
" * @param {?function(this:S, T, number, !Array<T>): ?} callback",
" * @param {S=} opt_thisobj",
" * @this {?IArrayLike<T>|string}",
" * @template T,S",
" * @return {undefined}",
" */",
"Array.prototype.forEach = function(callback, opt_thisobj) {};",
"/**",
" * @param {?function(this:S, T, number, !Array<T>): ?} callback",
" * @param {S=} opt_thisobj",
" * @return {!Array<T>}",
" * @this {?IArrayLike<T>|string}",
" * @template T,S",
" */",
"Array.prototype.filter = function(callback, opt_thisobj) {};",
"/**",
" * @constructor",
" * @template T",
" * @implements {IArrayLike<T>}",
" */",
"function Arguments() {}",
"/** @type {number} */",
"Arguments.prototype.length;",
"/**",
" * @constructor",
" * @param {*=} opt_pattern",
" * @param {*=} opt_flags",
" * @return {!RegExp}",
" * @nosideeffects",
" */",
"function RegExp(opt_pattern, opt_flags) {}",
"/**",
" * @param {*} str The string to search.",
" * @return {?Array<string>}",
" */",
"RegExp.prototype.exec = function(str) {};",
"/**",
" * @constructor",
" */",
"function ObjectPropertyDescriptor() {}",
"/** @type {*} */",
"ObjectPropertyDescriptor.prototype.value;",
"/**",
" * @param {!Object} obj",
" * @param {string} prop",
" * @return {!ObjectPropertyDescriptor|undefined}",
" * @nosideeffects",
" */",
"Object.getOwnPropertyDescriptor = function(obj, prop) {};",
"/**",
" * @param {!Object} obj",
" * @param {string} prop",
" * @param {!Object} descriptor",
" * @return {!Object}",
" */",
"Object.defineProperty = function(obj, prop, descriptor) {};",
"/** @type {?} */ var unknown;", // For producing unknowns in tests.
"/** @typedef {?} */ var symbol;", // TODO(sdh): remove once primitive 'symbol' supported
"/** @constructor */ function Symbol() {}",
"/** @const {!symbol} */ Symbol.iterator;",
ACTIVE_X_OBJECT_DEF);
/**
* Constructs a test.
*
* @param externs Externs JS as a string
* @param compareAsTree True to compare output & expected as a node tree.
* 99% of the time you want to compare as a tree. There are a few
* special cases where you don't, like if you want to test the code
* printing of "unnatural" syntax trees. For example,
*
* <pre>
* IF
* IF
* STATEMENT
* ELSE
* STATEMENT
* </pre>
*/
protected CompilerTestCase(String externs) {
this.externsInputs = ImmutableList.of(SourceFile.fromCode("externs", externs));
}
/**
* Constructs a test. Uses AST comparison and no externs.
*/
protected CompilerTestCase() {
this("");
}
// Overridden here so that we can easily find all classes that override.
@Override
protected void setUp() throws Exception {
super.setUp();
// TODO(sdh): Initialize *all* the options here, but first we must ensure no subclass
// is changing them in the constructor, rather than in their own setUp method.
this.acceptedLanguage = LanguageMode.ECMASCRIPT_2017;
this.moduleResolutionMode = ModuleLoader.ResolutionMode.BROWSER;
this.allowExternsChanges = false;
this.allowSourcelessWarnings = false;
this.astValidationEnabled = true;
this.checkAccessControls = false;
this.checkAstChangeMarking = true;
this.checkLineNumbers = true;
this.closurePassEnabled = false;
this.closurePassEnabledForExpected = false;
this.compareAsTree = true;
this.compareJsDoc = true;
this.computeSideEffects = false;
this.expectParseWarningsThisTest = false;
this.expectedSymbolTableError = null;
this.gatherExternPropertiesEnabled = false;
this.inferConsts = false;
this.languageOut = LanguageMode.ECMASCRIPT5;
this.markNoSideEffects = false;
this.multistageCompilation = true;
this.newTypeInferenceEnabled = false;
this.normalizeEnabled = false;
this.parseTypeInfo = false;
this.polymerPass = false;
this.rewriteClosureCode = false;
this.runNTIAfterProcessing = false;
this.runTypeCheckAfterProcessing = false;
this.transpileEnabled = false;
this.typeCheckEnabled = false;
this.setUpRan = true;
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
this.setUpRan = false;
}
/**
* Gets the compiler pass instance to use for a test.
*
* @param compiler The compiler
* @return The pass to test
*/
protected abstract CompilerPass getProcessor(Compiler compiler);
/**
* Gets the compiler options to use for this test. Use getProcessor to
* determine what passes should be run.
*/
protected CompilerOptions getOptions() {
return getOptions(new CompilerOptions());
}
/**
* Gets the compiler options to use for this test. Use getProcessor to
* determine what passes should be run.
*/
protected CompilerOptions getOptions(CompilerOptions options) {
options.setLanguageIn(acceptedLanguage);
options.setEmitUseStrict(emitUseStrict);
options.setLanguageOut(languageOut);
options.setModuleResolutionMode(moduleResolutionMode);
// This doesn't affect whether checkSymbols is run--it just affects
// whether variable warnings are filtered.
options.setCheckSymbols(true);
options.setWarningLevel(DiagnosticGroups.MISSING_PROPERTIES, CheckLevel.WARNING);
options.setWarningLevel(DiagnosticGroups.INVALID_CASTS, CheckLevel.WARNING);
if (!ignoredWarnings.isEmpty()) {
options.setWarningLevel(
new DiagnosticGroup(ignoredWarnings.toArray(new DiagnosticType[0])), CheckLevel.OFF);
}
options.setCodingConvention(getCodingConvention());
options.setPolymerVersion(1);
return options;
}
@ForOverride
protected CodingConvention getCodingConvention() {
return new GoogleCodingConvention();
}
/**
* Enables parsing type info from JSDoc comments. This sets the compiler option,
* but does not actually run the type checking pass.
*/
protected final void enableParseTypeInfo() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.parseTypeInfo = true;
}
/** Turns off taking JSDoc into account when comparing ASTs. */
protected final void disableCompareJsDoc() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.compareJsDoc = false;
}
/** Moves OTI type checking to occur after the processor, instead of before. */
protected final void enableRunTypeCheckAfterProcessing() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.runTypeCheckAfterProcessing = true;
}
/** Moves NTI type checking to occur after the processor, instead of before. */
protected final void enableRunNTIAfterProcessing() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.runNTIAfterProcessing = true;
}
// TODO(johnlenz): remove "get" and "set" filename. clients needing this should be
// creating "SourceFile" objects directly.
protected final void setFilename(String filename) {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.filename = filename;
}
public final String getFilename() {
return filename;
}
/**
* Returns the number of times the pass should be run before results are
* verified.
*/
@ForOverride
protected int getNumRepetitions() {
// Since most compiler passes should be idempotent, we run each pass twice
// by default.
return 2;
}
/** Adds the given DiagnosticTypes to the set of warnings to ignore. */
protected final void ignoreWarnings(DiagnosticType... warnings) {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
ignoredWarnings.addAll(Arrays.asList(warnings));
}
/** Adds the given DiagnosticGroups to the set of warnings to ignore. */
protected final void ignoreWarnings(DiagnosticGroup... warnings) {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
for (DiagnosticGroup group : warnings) {
ignoredWarnings.addAll(group.getTypes());
}
}
/** Expect warnings without source information. */
protected final void allowSourcelessWarnings() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
allowSourcelessWarnings = true;
}
/** The most recently used JSComp instance. */
protected Compiler getLastCompiler() {
return lastCompiler;
}
/**
* What language to allow in source parsing. Also sets the output language.
*/
protected final void setAcceptedLanguage(LanguageMode lang) {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
setLanguage(lang, lang);
}
/**
* Sets the input and output language modes..
*/
protected final void setLanguage(LanguageMode langIn, LanguageMode langOut) {
this.acceptedLanguage = langIn;
setLanguageOut(langOut);
}
protected final void setLanguageOut(LanguageMode acceptedLanguage) {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.languageOut = acceptedLanguage;
}
protected final void setModuleResolutionMode(ModuleLoader.ResolutionMode moduleResolutionMode) {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.moduleResolutionMode = moduleResolutionMode;
}
/**
* Whether to run InferConsts before passes
*/
protected final void enableInferConsts() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.inferConsts = true;
}
/**
* Enables running CheckAccessControls after the pass being tested (and checking types).
*/
protected final void enableCheckAccessControls() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.checkAccessControls = true;
}
/**
* Allow externs to change.
*/
protected final void allowExternsChanges() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.allowExternsChanges = true;
}
protected final void enablePolymerPass() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
polymerPass = true;
}
/**
* Perform type checking before running the test pass. This will check
* for type errors and annotate nodes with type information.
*
* @see TypeCheck
*/
protected final void enableTypeCheck() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
typeCheckEnabled = true;
}
// Run the new type inference after the test pass. Useful for testing passes
// that rewrite the AST prior to typechecking, eg, AngularPass or PolymerPass.
protected void enableNewTypeInference() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.newTypeInferenceEnabled = true;
}
/**
* Run using multistage compilation.
*/
protected final void enableMultistageCompilation() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
multistageCompilation = true;
}
/**
* Run using singlestage compilation.
*/
protected final void disableMultistageCompilation() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
multistageCompilation = false;
}
/**
* Disable checking to make sure that line numbers were preserved.
*/
protected final void disableLineNumberCheck() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
checkLineNumbers = false;
}
/**
* @param newVal Whether to validate AST change marking.
*/
protected final void disableValidateAstChangeMarking() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
checkAstChangeMarking = false;
}
/**
* Do not run type checking before running the test pass.
*
* @see TypeCheck
*/
protected final void disableTypeCheck() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
typeCheckEnabled = false;
}
protected final void disableNewTypeInference() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.newTypeInferenceEnabled = false;
}
/**
* Process closure library primitives.
*/
protected final void enableClosurePass() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
closurePassEnabled = true;
}
protected final void enableClosurePassForExpected() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
closurePassEnabledForExpected = true;
}
/**
* Rewrite Closure code before the test is run.
*/
protected final void enableRewriteClosureCode() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
rewriteClosureCode = true;
}
/**
* Don't rewrite Closure code before the test is run.
*/
protected final void disableRewriteClosureCode() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
rewriteClosureCode = false;
}
/**
* Perform AST normalization before running the test pass, and anti-normalize
* after running it.
*
* @see Normalize
*/
protected final void enableNormalize() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.normalizeEnabled = true;
}
/**
* Perform AST transpilation before running the test pass.
*/
protected final void enableTranspile() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
transpileEnabled = true;
}
/**
* Don't perform AST normalization before running the test pass.
* @see Normalize
*/
protected final void disableNormalize() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
normalizeEnabled = false;
}
/**
* Run the MarkSideEffectCalls pass before running the test pass.
*
* @see MarkNoSideEffectCalls
*/
protected final void enableMarkNoSideEffects() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
markNoSideEffects = true;
}
/**
* Run the PureFunctionIdentifier pass before running the test pass.
*
* @see MarkNoSideEffectCalls
*/
protected final void enableComputeSideEffects() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
computeSideEffects = true;
}
/**
* Scan externs for properties that should not be renamed.
*/
protected final void enableGatherExternProperties() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
gatherExternPropertiesEnabled = true;
}
/**
* Disable validating the AST after each run of the pass.
*/
protected final void disableAstValidation() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
astValidationEnabled = false;
}
/**
* Disable comparing the expected output as a tree or string.
*/
protected final void disableCompareAsTree() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
this.compareAsTree = false;
}
/** Whether we should ignore parse warnings for the current test method. */
protected final void setExpectParseWarningsThisTest() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
expectParseWarningsThisTest = true;
}
/** Returns a newly created TypeCheck. */
private static TypeCheck createTypeCheck(Compiler compiler) {
ReverseAbstractInterpreter rai =
new SemanticReverseAbstractInterpreter(compiler.getTypeRegistry());
compiler.setMostRecentTypechecker(MostRecentTypechecker.OTI);
return new TypeCheck(compiler, rai, compiler.getTypeRegistry());
}
protected static void runNewTypeInference(Compiler compiler, Node externs, Node js) {
GlobalTypeInfo gti = compiler.getSymbolTable();
gti.process(externs, js);
NewTypeInference nti = new NewTypeInference(compiler);
nti.process(externs, js);
}
/**
* Verifies that the compiler pass's JS output matches the expected output.
*
* @param js Input
* @param expected Expected JS output
*/
protected void test(String js, String expected) {
test(srcs(js), expected(expected));
}
/**
* Verifies that the compiler generates the given error for the given input.
*
* @param js Input
* @param error Expected error
*/
protected void testError(String js, DiagnosticType error) {
test(srcs(js), error(error));
}
/**
* Verifies that the compiler generates the given error for the given input.
*
* @param js Input
* @param error Expected error
*/
protected void testError(String externs, String js, DiagnosticType error) {
test(externs(externs), srcs(js), error(error));
}
/**
* Verifies that the compiler generates the given error and description for the given input.
*/
protected void testError(String js, DiagnosticType error, String description) {
assertNotNull(error);
test(srcs(js), error(error, description));
}
/**
* Verifies that the compiler generates the given error and description for the given input.
*/
protected void testError(Sources srcs, ErrorDiagnostic error) {
assertNotNull(error);
test(srcs, error);
}
/**
* Verifies that the compiler generates the given error and description for the given input.
*/
protected void testError(Externs externs, Sources srcs, ErrorDiagnostic error) {
assertNotNull(error);
test(externs, srcs, error);
}
/**
* Verifies that the compiler generates the given error for the given input.
*
* @param js Input
* @param error Expected error
*/
protected void testError(String[] js, DiagnosticType error) {
assertNotNull(error);
test(srcs(js), error(error));
}
/**
* Verifies that the compiler generates the given warning for the given input.
*/
protected void testError(List<SourceFile> inputs, DiagnosticType warning) {
assertNotNull(warning);
test(srcs(inputs), error(warning));
}
/**
* Verifies that the compiler generates the given warning for the given input.
*/
protected void testError(List<SourceFile> inputs, DiagnosticType warning, String description) {
assertNotNull(warning);
test(srcs(inputs), error(warning, description));
}
/**
* Verifies that the compiler generates the given warning for the given input.
*
* @param js Input
* @param warning Expected warning
*/
protected void testWarning(String js, DiagnosticType warning) {
assertNotNull(warning);
test(srcs(js), warning(warning));
}
/**
* Verifies that the compiler generates the given warning for the given input.
*
* @param srcs Inputs
* @param warning Expected warning
*/
protected void testWarning(Sources srcs, WarningDiagnostic warning) {
assertNotNull(warning);
test(srcs, warning);
}
/**
* Verifies that the compiler generates the given warning for the given input.
*
* @param externs The externs
* @param srcs The input
* @param warning Expected warning
*/
protected void testWarning(Externs externs, Sources srcs, WarningDiagnostic warning) {
assertNotNull(warning);
test(externs, srcs, warning);
}
/**
* Verifies that the compiler generates the given warning for the given input.
*
* @param js Input
* @param warning Expected warning
*/
protected void testWarning(String externs, String js, DiagnosticType warning) {
assertNotNull(warning);
test(externs(externs), srcs(js), warning(warning));
}
/**
* Verifies that the compiler generates the given warning for the given input.
*/
protected void testWarning(String[] js, DiagnosticType warning) {
assertNotNull(warning);
test(srcs(js), warning(warning));
}
/**
* Verifies that the compiler generates the given warning for the given input.
*/
protected void testWarning(List<SourceFile> inputs, DiagnosticType warning) {
assertNotNull(warning);
test(srcs(inputs), warning(warning));
}
/**
* Verifies that the compiler generates the given warning and description for the given input.
*
* @param js Input
* @param warning Expected warning
*/
protected void testWarning(String js, DiagnosticType warning, String description) {
assertNotNull(warning);
test(srcs(js), warning(warning, description));
}
/**
* Verifies that the compiler generates the given warning for the given input.
*/
protected void testWarning(List<SourceFile> inputs, DiagnosticType warning, String description) {
assertNotNull(warning);
test(srcs(inputs), warning(warning, description));
}
/**
* Verifies that the compiler generates the given warning for the given input.
*/
protected void testWarning(
String externs, String js, DiagnosticType warning, String description) {
assertNotNull(warning);
test(externs(externs), srcs(js), warning(warning, description));
}
/**
* Verifies that the compiler generates no warnings for the given input.
*
* @param js Input
*/
protected void testNoWarning(String js) {
test(srcs(js));
}
/**