Search in sources :

Example 31 with TestExternsBuilder

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

the class ES2022IntegrationTest method publicClassFields_supportedInOptimizationsMode3.

@Test
public void publicClassFields_supportedInOptimizationsMode3() {
    CompilerOptions options = fullyOptimizedCompilerOptions();
    externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs"));
    test(options, lines("/** @unrestricted */", // 
    "class MyClass {", "  f1 = 1;", "  ['a'] = 'hi';", "  ['f3'] = function() { return this.f1; };", "}", "console.log(new MyClass().f1);", "console.log(new MyClass()['a']);", "console.log(new MyClass()['f3']());"), lines(// 
    "class a {", "  b = 1;", "  a = 'hi';", "  f3 = function() { return this.b; };", "}", "console.log(1);", "console.log(new a().a);", "console.log(new a().f3());"));
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 32 with TestExternsBuilder

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

the class IntegrationTest method testIsolatePolyfills_transpileOnlyMode.

@Test
public void testIsolatePolyfills_transpileOnlyMode() {
    CompilerOptions options = createCompilerOptions();
    options.setLanguageIn(LanguageMode.STABLE_IN);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    options.setRewritePolyfills(true);
    options.setIsolatePolyfills(true);
    options.setSkipNonTranspilationPasses(true);
    externs = ImmutableList.of(SourceFile.fromCode("testExterns.js", new TestExternsBuilder().addAlert().build()));
    // Polyfill isolation currently doesn't work with transpileOnly mode. It depends on runtime
    // library injection not being disabled.
    assertThrows(Exception.class, () -> compile(options, "alert('hi'.startsWith('h'));"));
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 33 with TestExternsBuilder

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

the class SerializeTypedAstPassTest method testAst_typeSummaryFiles_areNotSerialized_orSearchedForTypes.

@Test
public void testAst_typeSummaryFiles_areNotSerialized_orSearchedForTypes() {
    enableCreateModuleMap();
    // SerializeTypedAstPass will drop the type summary files, which live on the externs root, to
    // avoid serializing them.
    allowExternsChanges();
    Externs closureExterns = externs(new TestExternsBuilder().addClosureExterns().build());
    SourceFile mandatorySource = SourceFile.fromCode("mandatory.js", "/* mandatory source */");
    SerializationResult typeSummaryResult = compile(closureExterns, srcs(mandatorySource, SourceFile.fromCode("foo.i.js", lines(// 
    "/** @fileoverview @typeSummary */", "", "goog.loadModule(function(exports) {", "  goog.module('a.Foo');", "  class Foo { }", "  exports = Foo;", "});"))));
    SerializationResult emptyResult = compile(// 
    closureExterns, srcs(mandatorySource));
    assertThat(typeSummaryResult.ast).isEqualTo(emptyResult.ast);
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) SourceFile(com.google.javascript.jscomp.SourceFile) Test(org.junit.Test)

Example 34 with TestExternsBuilder

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

the class TypeCheckTest method testValueTypeBuiltInPrototypePropertyType.

@Test
public void testValueTypeBuiltInPrototypePropertyType() {
    Node node = parseAndTypeCheck(new TestExternsBuilder().addString().build(), "\"x\".charAt(0)");
    assertTypeEquals(getNativeStringType(), node.getFirstFirstChild().getJSType());
}
Also used : Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 35 with TestExternsBuilder

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

the class TypeCheckTest method testClosureTypesMultipleWarnings.

private void testClosureTypesMultipleWarnings(String js, List<String> descriptions) {
    compiler.initOptions(compiler.getOptions());
    Node jsRoot = IR.root(compiler.parseTestCode(js));
    Node externs = IR.root(compiler.parseTestCode(new TestExternsBuilder().addString().addClosureExterns().addExtra(CLOSURE_DEFS).build()));
    IR.root(externs, jsRoot);
    assertWithMessage("parsing error: " + Joiner.on(", ").join(compiler.getErrors())).that(compiler.getErrorCount()).isEqualTo(0);
    new GatherModuleMetadata(compiler, false, ResolutionMode.BROWSER).process(externs, jsRoot);
    // For processing goog.forwardDeclare for forward typedefs.
    new ProcessClosurePrimitives(compiler).process(externs, jsRoot);
    new TypeCheck(compiler, new ClosureReverseAbstractInterpreter(registry).append(new SemanticReverseAbstractInterpreter(registry)).getFirst(), registry).processForTesting(externs, jsRoot);
    assertWithMessage("unexpected error(s) : " + Joiner.on(", ").join(compiler.getErrors())).that(compiler.getErrorCount()).isEqualTo(0);
    if (descriptions == null) {
        assertWithMessage("unexpected warning(s) : " + Joiner.on(", ").join(compiler.getWarnings())).that(compiler.getWarningCount()).isEqualTo(0);
    } else {
        assertWithMessage("unexpected warning(s) : " + Joiner.on(", ").join(compiler.getWarnings())).that(compiler.getWarningCount()).isEqualTo(descriptions.size());
        Set<String> actualWarningDescriptions = new HashSet<>();
        for (int i = 0; i < descriptions.size(); i++) {
            actualWarningDescriptions.add(compiler.getWarnings().get(i).getDescription());
        }
        assertThat(actualWarningDescriptions).isEqualTo(new HashSet<>(descriptions));
    }
}
Also used : SemanticReverseAbstractInterpreter(com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) ClosureReverseAbstractInterpreter(com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter) HashSet(java.util.HashSet)

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