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