Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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 com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Iterables;
21 import com.google.common.collect.Lists;
22
23 import junit.framework.*;
24
25 import java.util.*;
26
27 import org.json.JSONArray;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30
31 /**
32 * Tests for {@link JSModuleGraph}
33 *
34 */
35 public class JSModuleGraphTest extends TestCase {
36
37 private final JSModule A = new JSModule("A");
38 private final JSModule B = new JSModule("B");
39 private final JSModule C = new JSModule("C");
40 private final JSModule D = new JSModule("D");
41 private final JSModule E = new JSModule("E");
42 private final JSModule F = new JSModule("F");
43 private JSModuleGraph graph = null;
44
45 // For resolving dependencies only.
46 private Compiler compiler;
47
48 @Override
49 public void setUp() throws Exception {
50 super.setUp();
51 B.addDependency(A); // __A__
52 C.addDependency(A); // / | \
53 D.addDependency(B); // B C |
54 E.addDependency(B); // / \ /| |
55 E.addDependency(C); // D E | /
56 F.addDependency(A); // \|/
57 F.addDependency(C); // F
58 F.addDependency(E);
59 graph = new JSModuleGraph(new JSModule[] {A, B, C, D, E, F});
60 compiler = new Compiler();
61 }
62
63 public void testModuleDepth() {
64 assertEquals("A should have depth 0", 0, A.getDepth());
65 assertEquals("B should have depth 1", 1, B.getDepth());
66 assertEquals("C should have depth 1", 1, C.getDepth());
67 assertEquals("D should have depth 2", 2, D.getDepth());
68 assertEquals("E should have depth 2", 2, E.getDepth());
69 assertEquals("F should have depth 3", 3, F.getDepth());
70 }
71
72 public void testDeepestCommonDep() {
73 assertDeepestCommonDep(null, A, A);
74 assertDeepestCommonDep(null, A, B);
75 assertDeepestCommonDep(null, A, C);
76 assertDeepestCommonDep(null, A, D);
77 assertDeepestCommonDep(null, A, E);
78 assertDeepestCommonDep(null, A, F);
79 assertDeepestCommonDep(A, B, B);
80 assertDeepestCommonDep(A, B, C);
81 assertDeepestCommonDep(A, B, D);
82 assertDeepestCommonDep(A, B, E);
83 assertDeepestCommonDep(A, B, F);
84 assertDeepestCommonDep(A, C, C);
85 assertDeepestCommonDep(A, C, D);
86 assertDeepestCommonDep(A, C, E);
87 assertDeepestCommonDep(A, C, F);
88 assertDeepestCommonDep(B, D, D);
89 assertDeepestCommonDep(B, D, E);
90 assertDeepestCommonDep(B, D, F);
91 assertDeepestCommonDep(C, E, E);
92 assertDeepestCommonDep(C, E, F);
93 assertDeepestCommonDep(E, F, F);
94 }
95
96 public void testDeepestCommonDepInclusive() {
97 assertDeepestCommonDepInclusive(A, A, A);
98 assertDeepestCommonDepInclusive(A, A, B);
99 assertDeepestCommonDepInclusive(A, A, C);
100 assertDeepestCommonDepInclusive(A, A, D);
101 assertDeepestCommonDepInclusive(A, A, E);
102 assertDeepestCommonDepInclusive(A, A, F);
103 assertDeepestCommonDepInclusive(B, B, B);
104 assertDeepestCommonDepInclusive(A, B, C);
105 assertDeepestCommonDepInclusive(B, B, D);
106 assertDeepestCommonDepInclusive(B, B, E);
107 assertDeepestCommonDepInclusive(B, B, F);
108 assertDeepestCommonDepInclusive(C, C, C);
109 assertDeepestCommonDepInclusive(A, C, D);
110 assertDeepestCommonDepInclusive(C, C, E);
111 assertDeepestCommonDepInclusive(C, C, F);
112 assertDeepestCommonDepInclusive(D, D, D);
113 assertDeepestCommonDepInclusive(B, D, E);
114 assertDeepestCommonDepInclusive(B, D, F);
115 assertDeepestCommonDepInclusive(E, E, E);
116 assertDeepestCommonDepInclusive(E, E, F);
117 assertDeepestCommonDepInclusive(F, F, F);
118 }
119
120 public void testGetTransitiveDepsDeepestFirst() {
121 assertTransitiveDepsDeepestFirst(A);
122 assertTransitiveDepsDeepestFirst(B, A);
123 assertTransitiveDepsDeepestFirst(C, A);
124 assertTransitiveDepsDeepestFirst(D, B, A);
125 assertTransitiveDepsDeepestFirst(E, C, B, A);
126 assertTransitiveDepsDeepestFirst(F, E, C, B, A);
127 }
128
129 public void testCoalesceDuplicateFiles() {
130 A.add(SourceFile.fromCode("a.js", ""));
131
132 B.add(SourceFile.fromCode("a.js", ""));
133 B.add(SourceFile.fromCode("b.js", ""));
134
135 C.add(SourceFile.fromCode("b.js", ""));
136 C.add(SourceFile.fromCode("c.js", ""));
137
138 E.add(SourceFile.fromCode("c.js", ""));
139 E.add(SourceFile.fromCode("d.js", ""));
140
141 graph.coalesceDuplicateFiles();
142
143 assertEquals(2, A.getInputs().size());
144 assertEquals("a.js", A.getInputs().get(0).getName());
145 assertEquals("b.js", A.getInputs().get(1).getName());
146 assertEquals(0, B.getInputs().size());
147 assertEquals(1, C.getInputs().size());
148 assertEquals("c.js", C.getInputs().get(0).getName());
149 assertEquals(1, E.getInputs().size());
150 assertEquals("d.js", E.getInputs().get(0).getName());
151 }
152
153 public void testManageDependencies1() throws Exception {
154 List<CompilerInput> inputs = setUpManageDependenciesTest();
155 List<CompilerInput> results = graph.manageDependencies(
156 ImmutableList.<String>of(), inputs);
157
158 assertInputs(A, "a1", "a3");
159 assertInputs(B, "a2", "b2");
160 assertInputs(C); // no inputs
161 assertInputs(E, "c1", "e1", "e2");
162
163 assertEquals(
164 Lists.newArrayList("a1", "a3", "a2", "b2", "c1", "e1", "e2"),
165 sourceNames(results));
166 }
167
168 public void testManageDependencies2() throws Exception {
169 List<CompilerInput> inputs = setUpManageDependenciesTest();
170 List<CompilerInput> results = graph.manageDependencies(
171 ImmutableList.<String>of("c2"), inputs);
172
173 assertInputs(A, "a1", "a3");
174 assertInputs(B, "a2", "b2");
175 assertInputs(C, "c1", "c2");
176 assertInputs(E, "e1", "e2");
177
178 assertEquals(
179 Lists.newArrayList("a1", "a3", "a2", "b2", "c1", "c2", "e1", "e2"),
180 sourceNames(results));
181 }
182
183 public void testManageDependencies3() throws Exception {
184 List<CompilerInput> inputs = setUpManageDependenciesTest();
185 DependencyOptions depOptions = new DependencyOptions();
186 depOptions.setDependencySorting(true);
187 depOptions.setDependencyPruning(true);
188 depOptions.setMoocherDropping(true);
189 depOptions.setEntryPoints(ImmutableList.<String>of("c2"));
190 List<CompilerInput> results = graph.manageDependencies(
191 depOptions, inputs);
192
193 // Everything gets pushed up into module c, because that's
194 // the only one that has entry points.
195 assertInputs(A);
196 assertInputs(B);
197 assertInputs(C, "a1", "c1", "c2");
198 assertInputs(E);
199
200 assertEquals(
201 Lists.newArrayList("a1", "c1", "c2"),
202 sourceNames(results));
203 }
204
205 public void testManageDependencies4() throws Exception {
206 setUpManageDependenciesTest();
207 DependencyOptions depOptions = new DependencyOptions();
208 depOptions.setDependencySorting(true);
209
210 List<CompilerInput> inputs = Lists.newArrayList();
211
212 // Add the inputs in a random order.
213 inputs.addAll(E.getInputs());
214 inputs.addAll(B.getInputs());
215 inputs.addAll(A.getInputs());
216 inputs.addAll(C.getInputs());
217
218 List<CompilerInput> results = graph.manageDependencies(
219 depOptions, inputs);
220
221 assertInputs(A, "a1", "a2", "a3");
222 assertInputs(B, "b1", "b2");
223 assertInputs(C, "c1", "c2");
224 assertInputs(E, "e1", "e2");
225
226 assertEquals(
227 Lists.newArrayList(
228 "a1", "a2", "a3", "b1", "b2", "c1", "c2", "e1", "e2"),
229 sourceNames(results));
230 }
231
232 public void testNoFiles() throws Exception {
233 DependencyOptions depOptions = new DependencyOptions();
234 depOptions.setDependencySorting(true);
235
236 List<CompilerInput> inputs = Lists.newArrayList();
237 List<CompilerInput> results = graph.manageDependencies(
238 depOptions, inputs);
239 assertTrue(results.isEmpty());
240 }
241
242 public void testToJson() throws JSONException {
243 JSONArray modules = graph.toJson();
244 assertEquals(6, modules.length());
245 for (int i = 0; i < modules.length(); i++) {
246 JSONObject m = modules.getJSONObject(i);
247 assertNotNull(m.getString("name"));
248 assertNotNull(m.getJSONArray("dependencies"));
249 assertNotNull(m.getJSONArray("transitive-dependencies"));
250 assertNotNull(m.getJSONArray("inputs"));
251 }
252 JSONObject m = modules.getJSONObject(3);
253 assertEquals("D", m.getString("name"));
254 assertEquals("[\"B\"]", m.getJSONArray("dependencies").toString());
255 assertEquals(2,
256 m.getJSONArray("transitive-dependencies").length());
257 assertEquals("[]", m.getJSONArray("inputs").toString());
258 }
259
260 private List<CompilerInput> setUpManageDependenciesTest() {
261 List<CompilerInput> inputs = Lists.newArrayList();
262
263 A.add(code("a1", provides("a1"), requires()));
264 A.add(code("a2", provides("a2"), requires("a1")));
265 A.add(code("a3", provides(), requires("a1")));
266
267 B.add(code("b1", provides("b1"), requires("a2")));
268 B.add(code("b2", provides(), requires("a1", "a2")));
269
270 C.add(code("c1", provides("c1"), requires("a1")));
271 C.add(code("c2", provides("c2"), requires("c1")));
272
273 E.add(code("e1", provides(), requires("c1")));
274 E.add(code("e2", provides(), requires("c1")));
275
276 inputs.addAll(A.getInputs());
277 inputs.addAll(B.getInputs());
278 inputs.addAll(C.getInputs());
279 inputs.addAll(E.getInputs());
280
281 for (CompilerInput input : inputs) {
282 input.setCompiler(compiler);
283 }
284 return inputs;
285 }
286
287 private void assertInputs(JSModule module, String ... sourceNames) {
288 assertEquals(
289 Lists.newArrayList(sourceNames),
290 sourceNames(module.getInputs()));
291 }
292
293 private List<String> sourceNames(List<CompilerInput> inputs) {
294 List<String> inputNames = Lists.newArrayList();
295 for (CompilerInput input : inputs) {
296 inputNames.add(input.getName());
297 }
298 return inputNames;
299 }
300
301 private SourceFile code(
302 String sourceName, List<String> provides, List<String> requires) {
303 String text = "";
304 for (String p : provides) {
305 text += "goog.provide('" + p + "');\n";
306 }
307 for (String r : requires) {
308 text += "goog.require('" + r + "');\n";
309 }
310 return SourceFile.fromCode(sourceName, text);
311 }
312
313 private List<String> provides(String ... strings) {
314 return Lists.newArrayList(strings);
315 }
316
317 private List<String> requires(String ... strings) {
318 return Lists.newArrayList(strings);
319 }
320
321 private void assertDeepestCommonDepInclusive(
322 JSModule expected, JSModule m1, JSModule m2) {
323 assertDeepestCommonDepOneWay(expected, m1, m2, true);
324 assertDeepestCommonDepOneWay(expected, m2, m1, true);
325 }
326
327 private void assertDeepestCommonDep(
328 JSModule expected, JSModule m1, JSModule m2) {
329 assertDeepestCommonDepOneWay(expected, m1, m2, false);
330 assertDeepestCommonDepOneWay(expected, m2, m1, false);
331 }
332
333 private void assertDeepestCommonDepOneWay(
334 JSModule expected, JSModule m1, JSModule m2, boolean inclusive) {
335 JSModule actual = inclusive ?
336 graph.getDeepestCommonDependencyInclusive(m1, m2) :
337 graph.getDeepestCommonDependency(m1, m2);
338 if (actual != expected) {
339 fail(String.format(
340 "Deepest common dep of %s and %s should be %s but was %s",
341 m1.getName(), m2.getName(),
342 expected == null ? "null" : expected.getName(),
343 actual == null ? "null" : actual.getName()));
344 }
345 }
346
347 private void assertTransitiveDepsDeepestFirst(JSModule m, JSModule... deps) {
348 Iterable<JSModule> actual = graph.getTransitiveDepsDeepestFirst(m);
349 assertEquals(Arrays.toString(deps),
350 Arrays.toString(Iterables.toArray(actual, JSModule.class)));
351 }
352 }