-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
IntegrationTestCase.java
479 lines (444 loc) · 17.4 KB
/
IntegrationTestCase.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
/*
* Copyright 2012 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.javascript.jscomp.testing.JSErrorSubject.assertError;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.CompilerTestCase.NoninjectingCompiler;
import com.google.javascript.rhino.Node;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
/**
* Framework for end-to-end test cases.
*
* @author nicksantos@google.com (Nick Santos)
*/
abstract class IntegrationTestCase extends TestCase {
protected static final Joiner LINE_JOINER = Joiner.on('\n');
protected static final Joiner EMPTY_JOINER = Joiner.on("");
/** Externs for the test */
protected static final ImmutableList<SourceFile> DEFAULT_EXTERNS =
ImmutableList.of(
SourceFile.fromCode(
"externs",
LINE_JOINER.join(
"var arguments;",
"var undefined;",
"/**",
" * @const",
" */",
"var Math = {};",
"/**",
" * @param {?} n1",
" * @param {?} n2",
" * @return {number}",
" * @nosideeffects",
" */",
"Math.pow = function(n1, n2) {};",
"var isNaN;",
"var Infinity;",
"/**",
" * @interface",
" * @template VALUE",
" */",
"function Iterable() {}",
"/**",
" * @interface",
" * @template VALUE",
" */",
"function Iterator() {}",
"/**",
" * @param {VALUE=} value",
" * @return {!IIterableResult<VALUE>}",
" */",
"Iterator.prototype.next;",
"/** @interface @extends {Iterator<T>} @extends {Iterable<T>} @template T*/",
"var IteratorIterable = function() {};",
"/** @interface */",
"function IArrayLike() {};",
// TODO(sdh): See if we can remove IIterableResult and Set once polyfills are
// split
"/**",
" * @record",
" * @template VALUE",
" */",
"function IIterableResult() {};",
"/** @type {boolean} */",
"IIterableResult.prototype.done;",
"/** @type {VALUE} */",
"IIterableResult.prototype.value;",
"/**",
" * @constructor",
" * @extends {Array<string>}",
" */",
"var ITemplateArray = function() {};",
"/** @constructor */",
"var Map;",
"/** @constructor */",
"var Set;",
"/** @constructor */",
"function ObjectPropertyDescriptor() {};",
"",
"/** @constructor */ function Window() {}",
"/** @type {string} */ Window.prototype.name;",
"/** @type {string} */ Window.prototype.offsetWidth;",
"/** @type {Window} */ var window;",
"",
"/** @nosideeffects */ function noSideEffects() {}",
"",
"/**",
" * @constructor",
" * @nosideeffects",
" */",
"function Widget() {}",
"/** @modifies {this} */ Widget.prototype.go = function() {};",
"/** @return {string} */ var widgetToken = function() {};",
"",
"function alert(message) {}",
"",
"/**",
" * @constructor",
" * @implements {IArrayLike}",
" * @implements {Iterable<T>}",
" * @return {!Array<?>}",
" * @param {...*} var_args",
" * @template T",
" */",
"function Array(var_args) {}",
"",
"/** @type {number} */",
"Array.prototype.length;",
"",
"/** @return {IteratorIterable<T>} */",
"Array.prototype.values;",
"",
"/**",
" * @constructor",
" * @return {number}",
" * @param {*=} opt_n",
" */",
"function Number(opt_n) {}",
"",
"/**",
" * @constructor",
" * @implements {Iterable<string>}",
" * @param {*=} opt_str",
" * @return {string}",
" */",
"function String(opt_str) {}",
"",
"/**",
" * @constructor",
" * @return {boolean}",
" * @param {*=} opt_b",
" */",
"function Boolean(opt_b) {}",
"",
"/**",
" * @constructor",
" * @return {!TypeError}",
" * @param {*=} opt_message",
" * @param {*=} opt_file",
" * @param {*=} opt_line",
" */",
"function TypeError(opt_message, opt_file, opt_line) {}",
"/**",
" * @constructor",
" * @param {*=} opt_message",
" * @param {*=} opt_file",
" * @param {*=} opt_line",
" * @return {!Error}",
" * @nosideeffects",
" */",
"function Error(opt_message, opt_file, opt_line) {}",
"/**",
" * @constructor",
" * @param {*=} opt_value",
" * @return {!Object}",
" */",
"function Object(opt_value) {}",
"Object.seal;",
"Object.defineProperties;",
"",
"Object.prototype;",
"",
"/** @type {!Function} */",
"Object.prototype.constructor;",
"Object.create = function(obj) {};",
"/** @type {function(!Object, ?Object)} */",
"Object.setPrototypeOf;",
"",
"/** @typedef {?} */",
"var symbol;",
"",
"/**",
" * @param {string} s",
" * @return {symbol}",
" */",
"function Symbol(s) {}",
"",
"/**",
" * @param {...*} var_args",
" * @constructor",
" */",
"function Function(var_args) {}",
"/**",
" * @param {*} context",
" * @param {!IArrayLike} args",
" */",
"Function.prototype.apply = function (context, args) {};",
"/** @param {...*} var_args */",
"Function.prototype.call = function (var_args) {};",
"",
"/**",
" * @constructor",
" * @template T",
" */",
"function Arguments() {}",
"",
"/**",
" * @interface",
" * @extends {IteratorIterable<VALUE>}",
" * @template VALUE",
" */",
"function Generator() {}",
"/**",
" * @param {?=} opt_value",
" * @return {!IIterableResult<VALUE>}",
" * @override",
" */",
"Generator.prototype.next = function(opt_value) {};",
"/**",
" * @param {VALUE} value",
" * @return {!IIterableResult<VALUE>}",
" */",
"Generator.prototype.return = function(value) {};",
"/**",
" * @param {?} exception",
" * @return {!IIterableResult<VALUE>}",
" */",
"Generator.prototype.throw = function(exception) {};")));
protected List<SourceFile> externs = DEFAULT_EXTERNS;
// The most recently used compiler.
protected Compiler lastCompiler;
protected boolean normalizeResults = false;
protected boolean useNoninjectingCompiler = false;
protected String inputFileNamePrefix;
protected String inputFileNameSuffix;
@Override
public void setUp() {
externs = DEFAULT_EXTERNS;
lastCompiler = null;
normalizeResults = false;
useNoninjectingCompiler = false;
inputFileNamePrefix = "i";
inputFileNameSuffix = ".js";
}
protected void testSame(CompilerOptions options, String original) {
testSame(options, new String[] { original });
}
protected void testSame(CompilerOptions options, String[] original) {
test(options, original, original);
}
/**
* Asserts that when compiling with the given compiler options,
* {@code original} is transformed into {@code compiled}.
*/
protected void test(CompilerOptions options,
String original, String compiled) {
test(options, new String[] { original }, new String[] { compiled });
}
/**
* Asserts that when compiling with the given compiler options,
* {@code original} is transformed into {@code compiled}.
*/
protected void test(CompilerOptions options,
String[] original, String[] compiled) {
Compiler compiler = compile(options, original);
assertEquals("Expected no warnings or errors\n" +
"Errors: \n" + Joiner.on("\n").join(compiler.getErrors()) + "\n" +
"Warnings: \n" + Joiner.on("\n").join(compiler.getWarnings()),
0, compiler.getErrors().length + compiler.getWarnings().length);
Node root = compiler.getJsRoot();
Node expectedRoot = parseExpectedCode(compiled, options, normalizeResults);
String explanation = expectedRoot.checkTreeEquals(root);
assertNull("\n"
+ "Expected: " + compiler.toSource(expectedRoot) + "\n"
+ "Result: " + compiler.toSource(root) + "\n"
+ explanation,
explanation);
}
/**
* Asserts that when compiling with the given compiler options,
* there is an error or warning.
*/
protected void test(CompilerOptions options,
String original, DiagnosticType warning) {
test(options, new String[] { original }, warning);
}
protected void test(CompilerOptions options,
String original, String compiled, DiagnosticType warning) {
test(options, new String[] { original }, new String[] { compiled },
warning);
}
protected void test(CompilerOptions options,
String[] original, DiagnosticType warning) {
test(options, original, null, warning);
}
/**
* Asserts that when compiling with the given compiler options,
* there is an error or warning.
*/
protected void test(CompilerOptions options,
String[] original, String[] compiled, DiagnosticType warning) {
Compiler compiler = compile(options, original);
checkUnexpectedErrorsOrWarnings(compiler, 1);
assertEquals("Expected exactly one warning or error",
1, compiler.getErrors().length + compiler.getWarnings().length);
if (compiler.getErrors().length > 0) {
assertError(compiler.getErrors()[0]).hasType(warning);
} else {
assertError(compiler.getWarnings()[0]).hasType(warning);
}
if (compiled != null) {
Node root = compiler.getRoot().getLastChild();
Node expectedRoot = parseExpectedCode(compiled, options, normalizeResults);
String explanation = expectedRoot.checkTreeEquals(root);
assertNull("\nExpected: " + compiler.toSource(expectedRoot) +
"\nResult: " + compiler.toSource(root) +
"\n" + explanation, explanation);
}
}
/** Asserts that when compiling with the given compiler options, there is an error or warning. */
protected void test(
CompilerOptions options, String[] original, String[] compiled, DiagnosticType[] warnings) {
Compiler compiler = compile(options, original);
checkUnexpectedErrorsOrWarnings(compiler, warnings.length);
if (compiled != null) {
Node root = compiler.getRoot().getLastChild();
Node expectedRoot = parseExpectedCode(compiled, options, normalizeResults);
String explanation = expectedRoot.checkTreeEquals(root);
assertNull(
"\nExpected: "
+ compiler.toSource(expectedRoot)
+ "\nResult: "
+ compiler.toSource(root)
+ "\n"
+ explanation,
explanation);
}
}
/**
* Asserts that there is at least one parse error.
*/
protected void testParseError(CompilerOptions options, String original) {
testParseError(options, original, null);
}
/**
* Asserts that there is at least one parse error.
*/
protected void testParseError(CompilerOptions options,
String original, String compiled) {
Compiler compiler = compile(options, original);
for (JSError error : compiler.getErrors()) {
if (!error.getType().equals(RhinoErrorReporter.PARSE_ERROR)) {
fail("Found unexpected error type " + error.getType() + ":\n" + error);
}
}
assertEquals("Unexpected warnings: " +
Joiner.on("\n").join(compiler.getWarnings()),
0, compiler.getWarnings().length);
if (compiled != null) {
Node root = compiler.getRoot().getLastChild();
Node expectedRoot = parseExpectedCode(
new String[] {compiled}, options, normalizeResults);
String explanation = expectedRoot.checkTreeEquals(root);
assertNull("\nExpected: " + compiler.toSource(expectedRoot) +
"\nResult: " + compiler.toSource(root) +
"\n" + explanation, explanation);
}
}
protected void checkUnexpectedErrorsOrWarnings(
Compiler compiler, int expected) {
int actual = compiler.getErrors().length + compiler.getWarnings().length;
if (actual != expected) {
String msg = "";
for (JSError err : compiler.getErrors()) {
msg += "Error:" + err + "\n";
}
for (JSError err : compiler.getWarnings()) {
msg += "Warning:" + err + "\n";
}
assertEquals("Unexpected warnings or errors.\n " + msg,
expected, actual);
}
}
protected Compiler compile(CompilerOptions options, String original) {
return compile(options, new String[] { original });
}
protected Compiler compile(CompilerOptions options, String[] original) {
Compiler compiler =
useNoninjectingCompiler
? new NoninjectingCompiler(new BlackHoleErrorManager())
: new Compiler(new BlackHoleErrorManager());
lastCompiler = compiler;
compiler.compileModules(
externs,
ImmutableList.copyOf(
CompilerTestCase.createModuleChain(
ImmutableList.copyOf(original), inputFileNamePrefix, inputFileNameSuffix)),
options);
return compiler;
}
/**
* Parse the expected code to compare against.
* We want to run this with similar parsing options, but don't
* want to run the commonjs preprocessing passes (so that we can use this
* to test the commonjs code).
*/
protected Node parseExpectedCode(
String[] original, CompilerOptions options, boolean normalize) {
boolean oldProcessCommonJsModules = options.processCommonJSModules;
options.processCommonJSModules = false;
Node expectedRoot = parse(original, options, normalize);
options.processCommonJSModules = oldProcessCommonJsModules;
return expectedRoot;
}
protected Node parse(
String[] original, CompilerOptions options, boolean normalize) {
Compiler compiler = new Compiler();
List<SourceFile> inputs = new ArrayList<>();
for (int i = 0; i < original.length; i++) {
inputs.add(SourceFile.fromCode(inputFileNamePrefix + i + inputFileNameSuffix, original[i]));
}
compiler.init(externs, inputs, options);
checkUnexpectedErrorsOrWarnings(compiler, 0);
Node all = compiler.parseInputs();
checkUnexpectedErrorsOrWarnings(compiler, 0);
Node n = all.getLastChild();
Node externs = all.getFirstChild();
(new CreateSyntheticBlocks(
compiler, "synStart", "synEnd")).process(externs, n);
if (normalize) {
new Normalize(compiler, false)
.process(compiler.getExternsRoot(), compiler.getJsRoot());
}
return n;
}
/** Creates a CompilerOptions object with google coding conventions. */
abstract CompilerOptions createCompilerOptions();
}