| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 56 |
|
| 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 |
|
| 64 |
|
| 65 |
public void disable_testSplitInplaceCall() { |
| 66 |
splitFunctions("(function() { print('hi') })()", |
| 67 |
"(function " + HOLDER + "() {})()", |
| 68 |
"(function() { print('hi') })"); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 77 |
"var foo = function " + HOLDER + "() {}; foo();" + |
| 78 |
"var bar = function " + HOLDER + "() {}; bar(1,2)", |
| 79 |
|
| 80 |
"(function(x) { foo(1) })", |
| 81 |
|
| 82 |
"(function(x,y) { bar(1,2) })"); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 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 |
} |