use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class GuardedCallbackTest method ifGuardWithOptChainTest.
@Test
public void ifGuardWithOptChainTest() {
final String externs = new TestExternsBuilder().addConsole().addPromise().build();
test(externs(externs), srcs(lines(//
"", "if (Promise?.allSettled) {", " Promise.allSettled([]).then(() => console.log('done'));", "}", "")), expected(lines(//
"", "if (GUARDED_NAME?.GUARDED_PROP) {", " GUARDED_NAME.GUARDED_PROP([]).then(() => console.log('done'));", "}", "")));
test(externs(externs), srcs(lines(//
"", "if (Promise?.allSettled([])) {", " Promise.allSettled([]).then(() => console.log('done'));", "}", "")), expected(lines(//
"", "if (GUARDED_NAME?.allSettled([])) {", " GUARDED_NAME.allSettled([]).then(() => console.log('done'));", "}", "")));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class J2clIntegrationTest method testInlineClassStaticGetterSetter.
@Test
public void testInlineClassStaticGetterSetter() {
externs = ImmutableList.of(new TestExternsBuilder().addFunction().addConsole().buildExternsFile("externs.js"));
test(createCompilerOptions(), lines("var A = class {", " static $clinit() {", " A.$x = 2;", " }", " static get x() {", " return A.$clinit(), A.$x;", " }", " static set x(value) {", " A.$clinit(), A.$x = value;", " }", "};", "A.x = 3;", "console.log(A.x);"), lines(//
"var a;", "a = 2;", "a = 3;", "console.log(a);"));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class TypedAstIntegrationTest method removesRegExpCallsIfSafe.
@Test
public void removesRegExpCallsIfSafe() throws IOException {
precompileLibrary(extern(new TestExternsBuilder().addRegExp().build()), code("(/abc/gi).exec('')"));
CompilerOptions options = new CompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
Compiler compiler = compileTypedAstShards(options);
Node expectedRoot = parseExpectedCode("");
assertNode(compiler.getRoot().getSecondChild()).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class TypedAstIntegrationTest method lateFulfilledGlobalVariableIsRenamed.
@Test
public void lateFulfilledGlobalVariableIsRenamed() throws IOException {
SourceFile lib1 = code(lines("function lib1() {", " if (typeof lib2Var !== 'undefined') {", " alert(lib2Var);", " }", "}"));
precompileLibrary(extern(new TestExternsBuilder().addAlert().build()), lib1);
precompileLibrary(typeSummary(lib1), code("var lib2Var = 10; lib1();"));
CompilerOptions options = new CompilerOptions();
options.setVariableRenaming(VariableRenamingPolicy.ALL);
options.setGeneratePseudoNames(true);
Compiler compiler = compileTypedAstShards(options);
String[] expected = new String[] { lines("function $lib1$$() {", " if (typeof $lib2Var$$ !== 'undefined') {", " alert($lib2Var$$);", " }", "}"), "var $lib2Var$$ = 10; $lib1$$();" };
Node expectedRoot = parseExpectedCode(expected);
assertNode(compiler.getRoot().getSecondChild()).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class TypedAstIntegrationTest method simpleAlertCall.
@Test
public void simpleAlertCall() throws IOException {
precompileLibrary(extern(new TestExternsBuilder().addAlert().build()), code("alert(10);"));
CompilerOptions options = new CompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
Compiler compiler = compileTypedAstShards(options);
Node expectedRoot = parseExpectedCode("alert(10);");
assertNode(compiler.getRoot().getSecondChild()).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
}
Aggregations