Search in sources :

Example 91 with TestExternsBuilder

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);");
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 92 with TestExternsBuilder

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))")));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 93 with TestExternsBuilder

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)"));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 94 with TestExternsBuilder

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);"));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 95 with TestExternsBuilder

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'));", "}", "")));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Aggregations

TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)107 Test (org.junit.Test)106 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)58 Node (com.google.javascript.rhino.Node)17 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)17 Compiler (com.google.javascript.jscomp.Compiler)8 SourceFile (com.google.javascript.jscomp.SourceFile)7 Color (com.google.javascript.jscomp.colors.Color)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 CodeSubTree (com.google.javascript.jscomp.testing.CodeSubTree)2 ImmutableList (com.google.common.collect.ImmutableList)1 NoninjectingCompiler (com.google.javascript.jscomp.testing.NoninjectingCompiler)1 ClosureReverseAbstractInterpreter (com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter)1 SemanticReverseAbstractInterpreter (com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 File (java.io.File)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1