Search in sources :

Example 96 with TestExternsBuilder

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

Example 97 with TestExternsBuilder

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

Example 98 with TestExternsBuilder

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);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 99 with TestExternsBuilder

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);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 100 with TestExternsBuilder

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);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) 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