use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class CompilerTest method testExternsFileAsEntryPoint4.
@Test
public void testExternsFileAsEntryPoint4() throws Exception {
// Test that has a code reference to an extern that does exist,
// and the extern and source files are both entry points
List<SourceFile> inputs = ImmutableList.of(SourceFile.fromCode("/externs.js", "/** @fileoverview @externs */ /** @const {number} */ var bar = 1;"), SourceFile.fromCode("/foo.js", "console.log(bar);"));
List<ModuleIdentifier> entryPoints = ImmutableList.of(ModuleIdentifier.forFile("/externs.js"), ModuleIdentifier.forFile("/foo.js"));
CompilerOptions options = createNewFlagBasedOptions();
options.setDependencyOptions(DependencyOptions.pruneForEntryPoints(entryPoints));
List<SourceFile> externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("default_externs.js"));
Compiler compiler = new Compiler();
compiler.compile(externs, inputs, options);
Result result = compiler.getResult();
assertThat(result.errors).isEmpty();
assertThat(compiler.toSource()).isEqualTo("console.log(bar);");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class GuardedCallbackTest method optChainTest.
@Test
public void optChainTest() {
final String externs = new TestExternsBuilder().addConsole().addPromise().build();
testSame(externs(externs), // Nothing guarded
srcs("console.log(Promise.allSettled([Promise.resolve(), x.allSettled]))"));
test(externs(externs), srcs("console.log(Promise?.allSettled([Promise.resolve(), x.allSettled]))"), expected("console.log(GUARDED_NAME?.allSettled([GUARDED_NAME.resolve(), x.allSettled]))"));
test(externs(externs), srcs("console.log(Promise.allSettled?.([Promise.resolve(), x.allSettled]))"), expected("console.log(Promise.GUARDED_PROP?.([Promise.resolve(), x.GUARDED_PROP]))"));
test(externs(externs), srcs("console.log(Promise?.allSettled?.([Promise.resolve(), x.allSettled]))"), expected("console.log(GUARDED_NAME?.GUARDED_PROP?.([GUARDED_NAME.resolve(), x.GUARDED_PROP]))"));
test(externs(externs), srcs(lines("console.log(", " Promise?.resolve(Promise)", " .then(x.finally)", " .finally?.(x.finally))")), expected(lines("console.log(", " GUARDED_NAME?.resolve(GUARDED_NAME)", " .then(x.finally)", " .GUARDED_PROP?.(x.GUARDED_PROP))")));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class GuardedCallbackTest method guardedTest.
@Test
public void guardedTest() {
final String externs = new TestExternsBuilder().addConsole().addPromise().build();
test(externs(externs), srcs("console.log(Promise && Promise.allSettled && Promise.allSettled);"), expected("console.log(GUARDED_NAME && GUARDED_NAME.GUARDED_PROP && GUARDED_NAME.GUARDED_PROP)"));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class GuardedCallbackTest method unguardedTest.
@Test
public void unguardedTest() {
final String externs = new TestExternsBuilder().addConsole().addPromise().build();
testSame(externs(externs), srcs("console.log(Promise.allSettled);"));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class GuardedCallbackTest method ifGuardTest.
@Test
public void ifGuardTest() {
final String externs = new TestExternsBuilder().addConsole().addPromise().build();
test(externs(externs), srcs(lines(//
"", "if (Promise && Promise.allSettled) {", " Promise.allSettled([]).then(() => console.log('done'));", "}", "")), expected(lines(//
"", "if (GUARDED_NAME && GUARDED_NAME.GUARDED_PROP) {", " GUARDED_NAME.GUARDED_PROP([]).then(() => console.log('done'));", "}", "")));
}
Aggregations