Search in sources :

Example 51 with TestExternsBuilder

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

the class CompilerTest method testExternsFileAsEntryPoint5.

@Test
public void testExternsFileAsEntryPoint5() throws Exception {
    // Test that has a code reference to an extern that does exist,
    // and only the source source file is an entry point
    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("/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 52 with TestExternsBuilder

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

the class CompilerTest method testEs6ModulePathWithOddCharacters.

@Test
public void testEs6ModulePathWithOddCharacters() throws Exception {
    // Note that this is not yet compatible with transpilation, since the generated goog.provide
    // statements are not valid identifiers.
    List<SourceFile> inputs = ImmutableList.of(SourceFile.fromCode("/index[0].js", "import foo from './foo.js'; foo('hello');"), SourceFile.fromCode("/foo.js", "export default (foo) => { alert(foo); }"));
    List<ModuleIdentifier> entryPoints = ImmutableList.of(ModuleIdentifier.forFile("/index[0].js"));
    CompilerOptions options = createNewFlagBasedOptions();
    options.setDependencyOptions(DependencyOptions.pruneLegacyForEntryPoints(entryPoints));
    List<SourceFile> externs = ImmutableList.of(new TestExternsBuilder().addAlert().buildExternsFile("default_externs.js"));
    Compiler compiler = new Compiler();
    compiler.compile(externs, inputs, options);
    Result result = compiler.getResult();
    assertThat(result.errors).isEmpty();
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 53 with TestExternsBuilder

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

the class CommandLineRunnerTest method testInstrumentCodeProductionCreatesInstrumentationMapping.

@Test
public void testInstrumentCodeProductionCreatesInstrumentationMapping() throws IOException {
    Path tempFolderPath = folder.getRoot().toPath();
    String filePath = tempFolderPath + "\\someFile.txt";
    args.add("--instrument_for_coverage_option=PRODUCTION");
    args.add("--instrument_mapping_report=" + filePath);
    args.add("--production_instrumentation_array_name=ist_arr");
    args.add("--language_out=NO_TRANSPILE");
    args.add("--formatting=PRETTY_PRINT");
    String source = lines("function foo() { ", "   console.log('Hello'); ", "}");
    String expected = lines("var ist_arr = [];", "function foo() {", "  ist_arr.push('C');", "  console.log('Hello');", "}");
    externs = ImmutableList.of(new TestExternsBuilder().addArray().addAlert().addExtra("let ist_arr;").buildExternsFile("externs"));
    test(source, expected);
    File variableMap = new File(filePath);
    List<String> variableMapFile = Files.readLines(variableMap, UTF_8);
    assertThat(variableMapFile).containsExactly(" FileNames:[\"input0\"]", " FunctionNames:[\"foo\"]", " Types:[\"FUNCTION\"]", "C:AAACA").inOrder();
}
Also used : Path(java.nio.file.Path) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) File(java.io.File) Test(org.junit.Test)

Example 54 with TestExternsBuilder

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

the class CompilerTest method testDynamicImportOrdering3.

@Test
public void testDynamicImportOrdering3() throws Exception {
    List<SourceFile> sources = new ArrayList<>();
    sources.add(SourceFile.fromCode("/entry.js", "__webpack_require__(2);"));
    sources.add(SourceFile.fromCode("/a.js", lines("console.log(module.id);", "Promise.all([__webpack_require__.e(0)]).then(function() {", "  return __webpack_require__(3);", "});")));
    sources.add(SourceFile.fromCode("/b.js", "console.log(module.id); module.exports = 'foo';"));
    HashMap<String, String> webpackModulesById = new HashMap<>();
    webpackModulesById.put("1", "/entry.js");
    webpackModulesById.put("2", "/a.js");
    webpackModulesById.put("3", "/b.js");
    CompilerOptions options = new CompilerOptions();
    options.setLanguageIn(LanguageMode.ECMASCRIPT_2015);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    options.setDependencyOptions(DependencyOptions.pruneForEntryPoints(ImmutableList.of(ModuleIdentifier.forFile("/entry.js"))));
    options.setProcessCommonJSModules(true);
    options.setModuleResolutionMode(ResolutionMode.WEBPACK);
    List<SourceFile> externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("default_externs.js"));
    Compiler compiler = new Compiler();
    compiler.initWebpackMap(ImmutableMap.copyOf(webpackModulesById));
    Result result = compiler.compile(externs, ImmutableList.copyOf(sources), options);
    assertThat(result.success).isTrue();
    List<String> orderedInputs = new ArrayList<>();
    for (CompilerInput input : compiler.getInputsInOrder()) {
        orderedInputs.add(input.getName());
    }
    assertThat(orderedInputs).containsExactly("/a.js", "/entry.js", "/b.js").inOrder();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 55 with TestExternsBuilder

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

the class CompilerTest method testDynamicImportOrdering.

@Test
public void testDynamicImportOrdering() throws Exception {
    List<SourceFile> sources = new ArrayList<>();
    sources.add(SourceFile.fromCode("/entry.js", "__webpack_require__(2);"));
    sources.add(SourceFile.fromCode("/a.js", lines("console.log(module.id);", "__webpack_require__.e(0).then(function() { return __webpack_require__(3); });")));
    sources.add(SourceFile.fromCode("/b.js", "console.log(module.id); __webpack_require__(4);"));
    sources.add(SourceFile.fromCode("/c.js", "console.log(module.id);"));
    HashMap<String, String> webpackModulesById = new HashMap<>();
    webpackModulesById.put("1", "/entry.js");
    webpackModulesById.put("2", "/a.js");
    webpackModulesById.put("3", "/b.js");
    webpackModulesById.put("4", "/c.js");
    CompilerOptions options = new CompilerOptions();
    options.setLanguageIn(LanguageMode.ECMASCRIPT_2015);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    options.setLanguageOut(LanguageMode.ECMASCRIPT5);
    options.setDependencyOptions(DependencyOptions.pruneForEntryPoints(ImmutableList.of(ModuleIdentifier.forFile("/entry.js"))));
    options.setProcessCommonJSModules(true);
    options.setModuleResolutionMode(ResolutionMode.WEBPACK);
    List<SourceFile> externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("default_externs.js"));
    Compiler compiler = new Compiler();
    compiler.initWebpackMap(ImmutableMap.copyOf(webpackModulesById));
    Result result = compiler.compile(externs, ImmutableList.copyOf(sources), options);
    assertThat(result.success).isTrue();
    List<String> orderedInputs = new ArrayList<>();
    for (CompilerInput input : compiler.getInputsInOrder()) {
        orderedInputs.add(input.getName());
    }
    assertThat(orderedInputs).containsExactly("/a.js", "/entry.js", "/c.js", "/b.js").inOrder();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) 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