Skip to content
Browse files

Tests :)

  • Loading branch information...
1 parent ba679c0 commit 08d6e4b238f4a823313528f8718fd068e0a0479e @cramforce committed Dec 3, 2011
View
2 src/com/google/javascript/jscomp/CommandLineRunner.java
@@ -317,7 +317,7 @@
@Option(name = "--transform_amd_modules",
usage = "Transform AMD to Common JS modules.")
- private boolean transform_amd_modules;
+ private boolean transform_amd_modules = false;
@Option(name = "--process_closure_primitives",
handler = BooleanOptionHandler.class,
View
12 src/com/google/javascript/jscomp/Compiler.java
@@ -1190,14 +1190,14 @@ Node parseInputs() {
externsRoot.addChildToBack(n);
}
+ // Modules inferred in ProcessCommonJS pass.
+ if (options.transformAMDToCJSModules || options.processCommonJSModules) {
+ processAMDAndCommonJSModules();
+ }
+
// Check if the sources need to be re-ordered.
if (options.manageClosureDependencies) {
- // Modules inferred in ProcessCommonJS pass.
- if (options.transformAMDToCJSModules || options.processCommonJSModules) {
- processAMDAndCommonJSModules();
- }
-
for (CompilerInput input : inputs) {
input.setCompiler(this);
@@ -1320,7 +1320,7 @@ private void processAMDAndCommonJSModules() {
// Common JS modules override standard initialization.
if (cjsModules.size() > 0) {
this.modules = cjsModules;
- this.moduleGraph = null;
+ this.moduleGraph = new JSModuleGraph(this.modules);
}
}
View
40 test/com/google/javascript/jscomp/CommandLineRunnerTest.java
@@ -103,6 +103,7 @@
public void setUp() throws Exception {
super.setUp();
externs = DEFAULT_EXTERNS;
+ filename = null;
lastCompiler = null;
lastArg = null;
outReader = new ByteArrayOutputStream();
@@ -895,6 +896,28 @@ public void testNoSrCFilesWithManifest() throws IOException {
"Manifest files cannot be generated when the input is from stdin.");
}
+ public void testTransformAMD() {
+ args.add("--transform_amd_modules");
+ test("define({})", "exports = {}");
+ }
+
+ public void testProcessCJS() {
+ args.add("--process_cjs_modules");
+ args.add("--common_js_entry_module=foo/bar");
+ setFilename("foo/bar.js");
+ test("exports.test = 1",
+ "var module$foo$bar={test:1}; module$foo$bar.module$exports && (module$foo$bar=module$foo$bar.module$exports)");
+ }
+
+ public void testTransformAMDAndProcessCJS() {
+ args.add("--transform_amd_modules");
+ args.add("--process_cjs_modules");
+ args.add("--common_js_entry_module=foo/bar");
+ setFilename("foo/bar.js");
+ test("define({foo: 1})",
+ "var module$foo$bar={}, module$foo$bar={foo:1}; module$foo$bar.module$exports && (module$foo$bar=module$foo$bar.module$exports)");
+ }
+
/* Helper functions */
private void testSame(String original) {
@@ -1008,6 +1031,19 @@ private CommandLineRunner createCommandLineRunner(String[] original) {
new PrintStream(errReader));
}
+ private String filename = null;
+
+ private void setFilename(String filename) {
+ this.filename = filename;
+ }
+
+ private String getFilename(int i) {
+ if (filename == null) {
+ return "input" + i;
+ }
+ return filename;
+ }
+
private Compiler compile(String[] original) {
CommandLineRunner runner = createCommandLineRunner(original);
assertTrue(runner.shouldRunCompiler());
@@ -1017,7 +1053,7 @@ private Compiler compile(String[] original) {
if (useModules == ModulePattern.NONE) {
List<JSSourceFile> inputs = Lists.newArrayList();
for (int i = 0; i < original.length; i++) {
- inputs.add(JSSourceFile.fromCode("input" + i, original[i]));
+ inputs.add(JSSourceFile.fromCode(getFilename(i), original[i]));
}
inputsSupplier = Suppliers.ofInstance(inputs);
} else if (useModules == ModulePattern.STAR) {
@@ -1054,7 +1090,7 @@ private Node parse(String[] original) {
Compiler compiler = runner.createCompiler();
List<JSSourceFile> inputs = Lists.newArrayList();
for (int i = 0; i < original.length; i++) {
- inputs.add(JSSourceFile.fromCode("input" + i, original[i]));
+ inputs.add(JSSourceFile.fromCode(getFilename(i), original[i]));
}
CompilerOptions options = new CompilerOptions();
// ECMASCRIPT5 is the most forgiving.
View
27 test/com/google/javascript/jscomp/CompilerTest.java
@@ -16,6 +16,11 @@
package com.google.javascript.jscomp;
+import java.util.List;
+
+import com.google.common.collect.Lists;
+import com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException;
+import com.google.javascript.jscomp.deps.SortedDependencies.MissingProvideException;
import com.google.javascript.rhino.Node;
import junit.framework.TestCase;
@@ -102,4 +107,26 @@ public void testLocalUndefined() throws Exception {
"(function (undefined) { alert(undefined); })();");
compiler.compile(externs, input, options);
}
+
+ public void testCommonJSProvidesAndRequire() throws CircularDependencyException, MissingProvideException {
+ JSSourceFile[] inputs = {
+ JSSourceFile.fromCode("gin.js", "require('tonic')"),
+ JSSourceFile.fromCode("tonic.js", ""),
+ JSSourceFile.fromCode("mix.js", "require('gin'); require('tonic');")};
+ CompilerOptions options = new CompilerOptions();
+ options.ideMode = true;
+ List<String> entryPoints = Lists.newArrayList("module$mix");
+ options.setManageClosureDependencies(entryPoints);
+ options.closurePass = true;
+ options.processCommonJSModules = true;
+ Compiler compiler = new Compiler();
+ compiler.init(new JSSourceFile[0], inputs, options);
+ compiler.parseInputs();
+ JSModuleGraph graph = compiler.getModuleGraph();
+ assertEquals(graph.getModuleCount(), 3);
+ List<CompilerInput> result = graph.manageDependencies(entryPoints, compiler.getInputsForTesting());
+ assertEquals("tonic.js", result.get(0).getName());
+ assertEquals("gin.js", result.get(1).getName());
+ assertEquals("mix.js", result.get(2).getName());
+ }
}

0 comments on commit 08d6e4b

Please sign in to comment.
Something went wrong with that request. Please try again.