Search in sources :

Example 46 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class IntegrationTest method testSubstituteEs6Syntax.

@Test
public void testSubstituteEs6Syntax() {
    CompilerOptions options = createCompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setLanguageIn(LanguageMode.ECMASCRIPT_NEXT_IN);
    options.setLanguageOut(LanguageMode.ECMASCRIPT_NEXT);
    externs = ImmutableList.of(SourceFile.fromCode("testExterns.js", new TestExternsBuilder().addConsole().build()));
    // This is a regression test to confirm both that SubstituteEs6Syntax switches to object
    // literal shorthand here and that it correctly reports that it has added that feature.
    // IntegrationTestCase runs `ValidityCheck` to report an error if a feature usage gets added to
    // the AST without that addition being recorded.
    test(options, // 
    "console.log({console: console});", "console.log({console});");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 47 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class TypedAstIntegrationTest method removesRegExpCallsIfUnsafelyReferenced.

@Test
public void removesRegExpCallsIfUnsafelyReferenced() throws IOException {
    precompileLibrary(extern(new TestExternsBuilder().addRegExp().addConsole().build()), code(// 
    "(/abc/gi).exec('');", "console.log(RegExp.$1);"));
    CompilerOptions options = new CompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    Compiler compiler = compileTypedAstShards(options);
    Node expectedRoot = parseExpectedCode("(/abc/gi).exec(''); console.log(RegExp.$1);");
    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 48 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class TypedAstIntegrationTest method disambiguatesAndDeletesMethodsAcrossLibraries.

@Test
public void disambiguatesAndDeletesMethodsAcrossLibraries() throws IOException {
    SourceFile lib1 = code("class Lib1 { m() { return 'lib1'; } n() { return 'delete me'; } }");
    SourceFile lib2 = code("class Lib2 { m() { return 'delete me'; } n() { return 'lib2'; } }");
    precompileLibrary(lib1);
    precompileLibrary(lib2);
    precompileLibrary(extern(new TestExternsBuilder().addAlert().build()), typeSummary(lib1), typeSummary(lib2), code("alert(new Lib1().m()); alert(new Lib2().n());"));
    CompilerOptions options = new CompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setDisambiguateProperties(true);
    Compiler compiler = compileTypedAstShards(options);
    Node expectedRoot = parseExpectedCode("", "", "alert('lib1'); alert('lib2')");
    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 49 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class CompilerTest method testExternsFileAsEntryPoint3.

@Test
public void testExternsFileAsEntryPoint3() throws Exception {
    // Test code reference to an extern that doesn't exist,
    // but 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(nonexistentExtern);"));
    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).hasSize(1);
    assertThat(result.errors.get(0).getType()).isEqualTo(VarCheck.UNDEFINED_VAR_ERROR);
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 50 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class CompilerTest method testExternsFileAsEntryPoint2.

@Test
public void testExternsFileAsEntryPoint2() throws Exception {
    // Test code reference to an extern that doesn't exist,
    // but the extern is still the sole entry point.
    List<SourceFile> inputs = ImmutableList.of(SourceFile.fromCode("/externs.js", "/** @fileoverview @externs */ /** @const {number} */ var bar = 1;"), SourceFile.fromCode("/foo.js", "console.log(nonexistentExtern);"));
    List<ModuleIdentifier> entryPoints = ImmutableList.of(ModuleIdentifier.forFile("/externs.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()).isEmpty();
}
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