Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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.Lists;
21 import com.google.javascript.rhino.Node;
22
23 import junit.framework.TestCase;
24
25 import java.util.List;
26
27 /**
28 * Unit tests for {@link AstParallelizer}.
29 *
30 */
31 public class AstParallelizerTest extends TestCase {
32
33 private static final String HOLDER = AstParallelizer.TEMP_NAME;
34
35 public void testNoSplit() {
36 splitFunctions("", "");
37 splitFunctions("var x", "var x");
38 splitFunctions("var x", "var x");
39 splitFunctions("x()", "x()");
40 }
41
42 public void testSplitNamedFuntion() {
43 splitFunctions("function foo() { foo() } foo()",
44 "function " + HOLDER + "() {} foo()",
45 "function foo() { foo() }");
46 }
47
48
49 public void testSplitNamedFuntionWithArgs() {
50 splitFunctions("function foo(x) { foo(1) } foo(1)",
51 "function " + HOLDER + "() {} foo(1)",
52 "function foo(x) { foo(1) }");
53 }
54
55 // TODO(johnlenz): This test is invalid it relies on allowing
56 // nameless function statements, which does not parse.
57 public void disable_testSplitAnonFuntion() {
58 splitFunctions("var foo = function(x) { foo(1) }; foo(1)",
59 "var foo = function " + HOLDER + "() {}; foo(1)",
60 "(function(x) { foo(1) })");
61 }
62
63 // TODO(johnlenz): This test is invalid it relies on allowing
64 // nameless function statements, which does not parse.
65 public void disable_testSplitInplaceCall() {
66 splitFunctions("(function() { print('hi') })()",
67 "(function " + HOLDER + "() {})()",
68 "(function() { print('hi') })");
69 }
70
71 // TODO(johnlenz): This test is invalid it relies on allowing
72 // nameless function statements, which does not parse.
73 public void disable_testSplitMupltiFuntions() {
74 splitFunctions("var foo = function(x) { foo(1) }; foo();" +
75 "var bar = function(x,y) { bar(1,2) }; bar(1,2)",
76 // Output Root
77 "var foo = function " + HOLDER + "() {}; foo();" +
78 "var bar = function " + HOLDER + "() {}; bar(1,2)",
79 // foo
80 "(function(x) { foo(1) })",
81 // bar
82 "(function(x,y) { bar(1,2) })");
83 }
84
85 // TODO(johnlenz): This test is invalid it relies on allowing
86 // nameless function statements, which does not parse.
87 public void disable_testInnerFunctions() {
88 splitFunctions("var foo = function() {var bar = function() {}}",
89 "var foo = function " + HOLDER + "() {}",
90 "function() {var bar = function() {}}");
91 }
92
93 public void testSplitFileLevel() {
94 splitFiles(new String[] { "var a", "var b", "var c"});
95 splitFiles(new String[] {
96 "var a", "var b", "var c", "var d", "function e() {}"});
97 }
98
99 /**
100 * Splits at function level with {@link AstParallelizer#split()}, verify the
101 * output matches what is expected and then verify
102 * {@link AstParallelizer#join()} can reverse the whole process.
103 */
104 private void splitFunctions(String input, String ... output) {
105 Compiler compiler = new Compiler();
106 Node original = compiler.parseTestCode(input);
107 Node root = original.cloneTree();
108 AstParallelizer parallelizer =
109 AstParallelizer.createNewFunctionLevelAstParallelizer(root, true);
110 List<Node> forest = parallelizer.split();
111 assertEquals(output.length, forest.size());
112 int i = 0;
113 for (Node n : forest) {
114 Node tree = compiler.parseTestCode(output[i++]);
115 assertEquals(compiler.toSource(tree), compiler.toSource(n));
116 }
117
118 parallelizer.join();
119 assertTrue(original.isEquivalentTo(root));
120 }
121
122 private void splitFiles(String[] input) {
123 Compiler compiler = new Compiler();
124 List<SourceFile> files = Lists.newArrayList();
125
126 for (int i = 0; i < input.length; i ++) {
127 files.add(SourceFile.fromCode("file" + i, input[i]));
128 }
129
130 compiler.init(
131 ImmutableList.<SourceFile>of(), files, new CompilerOptions());
132 compiler.parse();
133 Node original = compiler.getRoot();
134 Node root = original.cloneTree();
135
136 AstParallelizer parallelizer =
137 AstParallelizer.createNewFileLevelAstParallelizer(root);
138 List<Node> forest = parallelizer.split();
139 assertEquals(input.length, forest.size());
140 int i = 0;
141 for (Node n : forest) {
142 Node tree = compiler.parseTestCode(input[i++]);
143 assertEquals(compiler.toSource(tree), compiler.toSource(n));
144 }
145
146 parallelizer.join();
147 assertTrue(original.isEquivalentTo(root));
148 }
149 }