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());"));
}
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'));"));
}
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);
}
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());
}
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));
}
}
Aggregations