| 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.ImmutableSet; |
| 21 |
|
| 22 |
import junit.framework.TestCase; |
| 23 |
import java.util.Arrays; |
| 24 |
import java.util.List; |
| 25 |
import java.util.ArrayList; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class JSModuleTest extends TestCase { |
| 32 |
private JSModule mod1; |
| 33 |
private JSModule mod2; |
| 34 |
private JSModule mod3; |
| 35 |
private JSModule mod4; |
| 36 |
private JSModule mod5; |
| 37 |
|
| 38 |
@Override |
| 39 |
protected void setUp() { |
| 40 |
List<JSModule> modulesInDepOrder = new ArrayList<JSModule>(); |
| 41 |
|
| 42 |
mod1 = new JSModule("mod1"); |
| 43 |
modulesInDepOrder.add(mod1); |
| 44 |
|
| 45 |
mod2 = new JSModule("mod2"); |
| 46 |
mod2.addDependency(mod1); |
| 47 |
modulesInDepOrder.add(mod2); |
| 48 |
|
| 49 |
mod3 = new JSModule("mod3"); |
| 50 |
mod3.addDependency(mod1); |
| 51 |
modulesInDepOrder.add(mod3); |
| 52 |
|
| 53 |
mod4 = new JSModule("mod4"); |
| 54 |
mod4.addDependency(mod2); |
| 55 |
mod4.addDependency(mod3); |
| 56 |
modulesInDepOrder.add(mod4); |
| 57 |
|
| 58 |
mod5 = new JSModule("mod5"); |
| 59 |
mod5.addDependency(mod1); |
| 60 |
modulesInDepOrder.add(mod5); |
| 61 |
} |
| 62 |
|
| 63 |
public void testDependencies() { |
| 64 |
assertEquals(ImmutableSet.of(), mod1.getAllDependencies()); |
| 65 |
assertEquals(ImmutableSet.of(mod1), mod2.getAllDependencies()); |
| 66 |
assertEquals(ImmutableSet.of(mod1), mod3.getAllDependencies()); |
| 67 |
assertEquals(ImmutableSet.of(mod1, mod2, mod3), mod4.getAllDependencies()); |
| 68 |
|
| 69 |
assertEquals(ImmutableSet.of(mod1), mod1.getThisAndAllDependencies()); |
| 70 |
assertEquals(ImmutableSet.of(mod1, mod2), mod2.getThisAndAllDependencies()); |
| 71 |
assertEquals(ImmutableSet.of(mod1, mod3), mod3.getThisAndAllDependencies()); |
| 72 |
assertEquals(ImmutableSet.of(mod1, mod2, mod3, mod4), |
| 73 |
mod4.getThisAndAllDependencies()); |
| 74 |
} |
| 75 |
|
| 76 |
public void testSortInputs() throws Exception { |
| 77 |
CompilerInput a = new CompilerInput( |
| 78 |
SourceFile.fromCode("a.js", |
| 79 |
"goog.require('b');goog.require('c')")); |
| 80 |
CompilerInput b = new CompilerInput( |
| 81 |
SourceFile.fromCode("b.js", |
| 82 |
"goog.provide('b');goog.require('d')")); |
| 83 |
CompilerInput c = new CompilerInput( |
| 84 |
SourceFile.fromCode("c.js", |
| 85 |
"goog.provide('c');goog.require('d')")); |
| 86 |
CompilerInput d = new CompilerInput( |
| 87 |
SourceFile.fromCode("d.js", |
| 88 |
"goog.provide('d')")); |
| 89 |
|
| 90 |
|
| 91 |
CompilerInput e = new CompilerInput( |
| 92 |
SourceFile.fromCode("e.js", |
| 93 |
"goog.provide('e')")); |
| 94 |
CompilerInput f = new CompilerInput( |
| 95 |
SourceFile.fromCode("f.js", |
| 96 |
"goog.provide('f')")); |
| 97 |
|
| 98 |
assertSortedInputs( |
| 99 |
ImmutableList.of(d, b, c, a), |
| 100 |
ImmutableList.of(a, b, c, d)); |
| 101 |
assertSortedInputs( |
| 102 |
ImmutableList.of(d, b, c, a), |
| 103 |
ImmutableList.of(d, b, c, a)); |
| 104 |
assertSortedInputs( |
| 105 |
ImmutableList.of(d, c, b, a), |
| 106 |
ImmutableList.of(d, c, b, a)); |
| 107 |
assertSortedInputs( |
| 108 |
ImmutableList.of(d, b, c, a), |
| 109 |
ImmutableList.of(d, a, b, c)); |
| 110 |
|
| 111 |
assertSortedInputs( |
| 112 |
ImmutableList.of(d, b, c, a, e, f), |
| 113 |
ImmutableList.of(a, b, c, d, e, f)); |
| 114 |
assertSortedInputs( |
| 115 |
ImmutableList.of(e, f, d, b, c, a), |
| 116 |
ImmutableList.of(e, f, a, b, c, d)); |
| 117 |
assertSortedInputs( |
| 118 |
ImmutableList.of(e, d, b, c, a, f), |
| 119 |
ImmutableList.of(a, b, c, e, d, f)); |
| 120 |
assertSortedInputs( |
| 121 |
ImmutableList.of(e, f, d, b, c, a), |
| 122 |
ImmutableList.of(e, a, f, b, c, d)); |
| 123 |
} |
| 124 |
|
| 125 |
private void assertSortedInputs( |
| 126 |
List<CompilerInput> expected, List<CompilerInput> shuffled) |
| 127 |
throws Exception { |
| 128 |
JSModule mod = new JSModule("mod"); |
| 129 |
for (CompilerInput input : shuffled) { |
| 130 |
input.setModule(null); |
| 131 |
mod.add(input); |
| 132 |
} |
| 133 |
Compiler compiler = new Compiler(System.err); |
| 134 |
compiler.initCompilerOptionsIfTesting(); |
| 135 |
mod.sortInputsByDeps(compiler); |
| 136 |
|
| 137 |
assertEquals(expected, mod.getInputs()); |
| 138 |
} |
| 139 |
|
| 140 |
public void testSortJsModules() throws Exception { |
| 141 |
|
| 142 |
assertEquals(ImmutableList.of(mod1, mod2, mod3, mod4), |
| 143 |
Arrays.asList(JSModule.sortJsModules( |
| 144 |
ImmutableList.of(mod1, mod2, mod3, mod4)))); |
| 145 |
assertEquals(ImmutableList.of(mod1, mod3, mod2, mod4), |
| 146 |
Arrays.asList(JSModule.sortJsModules( |
| 147 |
ImmutableList.of(mod1, mod3, mod2, mod4)))); |
| 148 |
|
| 149 |
|
| 150 |
assertEquals(ImmutableList.of(mod1, mod3, mod2, mod4), |
| 151 |
Arrays.asList(JSModule.sortJsModules( |
| 152 |
ImmutableList.of(mod4, mod3, mod2, mod1)))); |
| 153 |
assertEquals(ImmutableList.of(mod1, mod3, mod2, mod4), |
| 154 |
Arrays.asList(JSModule.sortJsModules( |
| 155 |
ImmutableList.of(mod3, mod1, mod2, mod4)))); |
| 156 |
|
| 157 |
|
| 158 |
assertEquals(ImmutableList.of(mod1, mod3, mod2, mod4), |
| 159 |
Arrays.asList(JSModule.sortJsModules( |
| 160 |
ImmutableList.of(mod4, mod3, mod1, mod2)))); |
| 161 |
} |
| 162 |
} |