Debian Codegraph @

test/com/google/javascript/jscomp/ProcessCommonJSModulesTest.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import java.util.List;
20
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.collect.Lists;
23
24 /**
25 * Unit tests for {@link ProcessCommonJSModules}
26 */
27 public class ProcessCommonJSModulesTest extends CompilerTestCase {
28
29 public ProcessCommonJSModulesTest() {
30 }
31
32 @Override
33 protected CompilerPass getProcessor(Compiler compiler) {
34 return new ProcessCommonJSModules(compiler, "foo/bar/", false);
35 }
36
37 @Override
38 protected int getNumRepetitions() {
39 return 1;
40 }
41
42 public void testWithoutExports() {
43 setFilename("test");
44 test(
45 "var name = require('name');" +
46 "name()",
47 "goog.provide('module$test');" +
48 "var module$test = {};" +
49 "goog.require('module$name');" +
50 "var name$$module$test = module$name;" +
51 "name$$module$test();");
52 setFilename("test/sub");
53 test(
54 "var name = require('mod/name');" +
55 "(function() { name(); })();",
56 "goog.provide('module$test$sub');" +
57 "var module$test$sub = {};" +
58 "goog.require('module$mod$name');" +
59 "var name$$module$test$sub = module$mod$name;" +
60 "(function() { name$$module$test$sub(); })();");
61 }
62
63 public void testExports() {
64 setFilename("test");
65 test(
66 "var name = require('name');" +
67 "exports.foo = 1;",
68 "goog.provide('module$test');" +
69 "var module$test = {};" +
70 "goog.require('module$name');" +
71 "var name$$module$test = module$name;" +
72 "module$test.foo = 1;");
73 test(
74 "var name = require('name');" +
75 "module.exports = function() {};",
76 "goog.provide('module$test');" +
77 "var module$test = {};" +
78 "goog.require('module$name');" +
79 "var name$$module$test = module$name;" +
80 "module$test.module$exports = function() {};" +
81 "if(module$test.module$exports)" +
82 "module$test=module$test.module$exports");
83 }
84
85 public void testVarRenaming() {
86 setFilename("test");
87 test(
88 "var a = 1, b = 2;" +
89 "(function() { var a; b = 4})()",
90 "goog.provide('module$test');" +
91 "var module$test = {};" +
92 "var a$$module$test = 1, b$$module$test = 2;" +
93 "(function() { var a; b$$module$test = 4})();");
94 }
95
96 public void testDash() {
97 setFilename("test-test");
98 test(
99 "var name = require('name'); exports.foo = 1;",
100 "goog.provide('module$test_test');" +
101 "var module$test_test = {};" +
102 "goog.require('module$name');" +
103 "var name$$module$test_test = module$name;" +
104 "module$test_test.foo = 1;");
105 }
106
107 public void testModuleName() {
108 assertEquals("module$foo$baz",
109 ProcessCommonJSModules.toModuleName("./baz.js", "foo/bar.js"));
110 assertEquals("module$foo$baz_bar",
111 ProcessCommonJSModules.toModuleName("./baz-bar.js", "foo/bar.js"));
112 assertEquals("module$baz",
113 ProcessCommonJSModules.toModuleName("../baz.js", "foo/bar.js"));
114 assertEquals("module$baz",
115 ProcessCommonJSModules.toModuleName("../../baz.js", "foo/bar/abc.js"));
116 assertEquals("module$baz", ProcessCommonJSModules.toModuleName(
117 "../../../baz.js", "foo/bar/abc/xyz.js"));
118 setFilename("foo/bar");
119 test(
120 "var name = require('name');",
121 "goog.provide('module$foo$bar'); var module$foo$bar = {};" +
122 "goog.require('module$name');" +
123 "var name$$module$foo$bar = module$name;");
124 test(
125 "var name = require('./name');",
126 "goog.provide('module$foo$bar');" +
127 "var module$foo$bar = {};" +
128 "goog.require('module$foo$name');" +
129 "var name$$module$foo$bar = module$foo$name;");
130
131 }
132
133 public void testGuessModuleName() {
134 ProcessCommonJSModules pass = new ProcessCommonJSModules(null, "foo");
135 assertEquals("module$baz",
136 pass.guessCJSModuleName("foo/baz.js"));
137 assertEquals("module$baz",
138 pass.guessCJSModuleName("foo\\baz.js"));
139 assertEquals("module$bar$baz",
140 pass.guessCJSModuleName("foo\\bar\\baz.js"));
141 }
142
143 public void testSortInputs() throws Exception {
144 SourceFile a = SourceFile.fromCode("a.js",
145 "require('b');require('c')");
146 SourceFile b = SourceFile.fromCode("b.js",
147 "require('d')");
148 SourceFile c = SourceFile.fromCode("c.js",
149 "require('d')");
150 SourceFile d = SourceFile.fromCode("d.js", "1;");
151
152 assertSortedInputs(
153 ImmutableList.of(d, b, c, a),
154 ImmutableList.of(a, b, c, d));
155 assertSortedInputs(
156 ImmutableList.of(d, b, c, a),
157 ImmutableList.of(d, b, c, a));
158 assertSortedInputs(
159 ImmutableList.of(d, c, b, a),
160 ImmutableList.of(d, c, b, a));
161 assertSortedInputs(
162 ImmutableList.of(d, b, c, a),
163 ImmutableList.of(d, a, b, c));
164 }
165
166 private void assertSortedInputs(
167 List<SourceFile> expected, List<SourceFile> shuffled)
168 throws Exception {
169 Compiler compiler = new Compiler(System.err);
170 compiler.initCompilerOptionsIfTesting();
171 compiler.getOptions().setProcessCommonJSModules(true);
172 compiler.getOptions().dependencyOptions.setEntryPoints(
173 Lists.newArrayList(ProcessCommonJSModules.toModuleName("a")));
174 compiler.compile(Lists.newArrayList(SourceFile.fromCode("externs.js", "")),
175 shuffled, compiler.getOptions());
176
177 List<SourceFile> result = Lists.newArrayList();
178 for (JSModule m : compiler.getModuleGraph().getAllModules()) {
179 for (CompilerInput i : m.getInputs()) {
180 result.add(i.getSourceFile());
181 }
182 }
183
184 assertEquals(expected, result);
185 }
186 }