Debian Codegraph @

test/com/google/javascript/jscomp/deps/SortedDependenciesTest.java

raw symbols SCIP
1 /*
2 * Copyright 2010 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.deps;
18
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Lists;
21 import com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException;
22
23 import junit.framework.TestCase;
24
25 import java.util.List;
26
27 /**
28 * Tests for {@link SortedDependencies}
29 * @author [email protected] (Nick Santos)
30 */
31 public class SortedDependenciesTest extends TestCase {
32
33 public void testSort() throws Exception {
34 SimpleDependencyInfo a = new SimpleDependencyInfo(
35 "a", "a", provides(), requires("b", "c"));
36 SimpleDependencyInfo b = new SimpleDependencyInfo(
37 "b", "b", provides("b"), requires("d"));
38 SimpleDependencyInfo c = new SimpleDependencyInfo(
39 "c", "c", provides("c"), requires("d"));
40 SimpleDependencyInfo d = new SimpleDependencyInfo(
41 "d", "d", provides("d"), requires());
42 SimpleDependencyInfo e = new SimpleDependencyInfo(
43 "e", "e", provides("e"), requires());
44
45 assertSortedInputs(
46 ImmutableList.of(d, b, c, a),
47 ImmutableList.of(a, b, c, d));
48 assertSortedInputs(
49 ImmutableList.of(d, b, c, a),
50 ImmutableList.of(d, b, c, a));
51 assertSortedInputs(
52 ImmutableList.of(d, c, b, a),
53 ImmutableList.of(d, c, b, a));
54 assertSortedInputs(
55 ImmutableList.of(d, b, c, a),
56 ImmutableList.of(d, a, b, c));
57
58 assertSortedDeps(
59 ImmutableList.of(d, b, c, a),
60 ImmutableList.of(d, b, c, a),
61 ImmutableList.of(a));
62 assertSortedDeps(
63 ImmutableList.of(d, c),
64 ImmutableList.of(d, c, b, a),
65 ImmutableList.of(c));
66 assertSortedDeps(
67 ImmutableList.of(d),
68 ImmutableList.of(d, c, b, a),
69 ImmutableList.of(d));
70
71 try {
72 assertSortedDeps(
73 ImmutableList.<SimpleDependencyInfo>of(),
74 ImmutableList.of(a, b, c, d),
75 ImmutableList.of(e));
76 fail("Expected an exception");
77 } catch (IllegalArgumentException expected) {}
78 }
79
80 public void testSort2() throws Exception {
81 SimpleDependencyInfo ab = new SimpleDependencyInfo(
82 "ab", "ab", provides("a", "b"), requires("d", "f"));
83 SimpleDependencyInfo c = new SimpleDependencyInfo(
84 "c", "c", provides("c"), requires("h"));
85 SimpleDependencyInfo d = new SimpleDependencyInfo(
86 "d", "d", provides("d"), requires("e", "f"));
87 SimpleDependencyInfo ef = new SimpleDependencyInfo(
88 "ef", "ef", provides("e", "f"), requires("g", "c"));
89 SimpleDependencyInfo g = new SimpleDependencyInfo(
90 "g", "g", provides("g"), requires());
91 SimpleDependencyInfo hi = new SimpleDependencyInfo(
92 "hi", "hi", provides("h", "i"), requires());
93
94 assertSortedInputs(
95 ImmutableList.of(g, hi, c, ef, d, ab),
96 ImmutableList.of(ab, c, d, ef, g, hi));
97
98 assertSortedDeps(
99 ImmutableList.of(g),
100 ImmutableList.of(ab, c, d, ef, g, hi),
101 ImmutableList.of(g));
102 assertSortedDeps(
103 ImmutableList.of(g, hi, c, ef, d),
104 ImmutableList.of(ab, c, d, ef, g, hi),
105 ImmutableList.of(d, hi));
106 }
107
108 public void testSort3() {
109 SimpleDependencyInfo a = new SimpleDependencyInfo(
110 "a", "a", provides("a"), requires("c"));
111 SimpleDependencyInfo b = new SimpleDependencyInfo(
112 "b", "b", provides("b"), requires("a"));
113 SimpleDependencyInfo c = new SimpleDependencyInfo(
114 "c", "c", provides("c"), requires("b"));
115
116 try {
117 new SortedDependencies<SimpleDependencyInfo>(
118 Lists.newArrayList(a, b, c));
119 fail("expected exception");
120 } catch (CircularDependencyException e) {
121 assertEquals("a -> a", e.getMessage());
122 }
123 }
124
125 public void testSort4() throws Exception {
126 // Check the degenerate case.
127 SimpleDependencyInfo a = new SimpleDependencyInfo(
128 "a", "a", provides("a"), requires("a"));
129 assertSortedDeps(
130 ImmutableList.of(a),
131 ImmutableList.of(a),
132 ImmutableList.of(a));
133 }
134
135 public void testSort5() throws Exception {
136 SimpleDependencyInfo a = new SimpleDependencyInfo(
137 "a", "a", provides("a"), requires());
138 SimpleDependencyInfo b = new SimpleDependencyInfo(
139 "b", "b", provides("b"), requires());
140 SimpleDependencyInfo c = new SimpleDependencyInfo(
141 "c", "c", provides("c"), requires());
142
143 assertSortedInputs(
144 ImmutableList.of(a, b, c),
145 ImmutableList.of(a, b, c));
146 assertSortedInputs(
147 ImmutableList.of(c, b, a),
148 ImmutableList.of(c, b, a));
149 }
150
151 private void assertSortedInputs(
152 List<SimpleDependencyInfo> expected,
153 List<SimpleDependencyInfo> shuffled) throws Exception {
154 SortedDependencies<SimpleDependencyInfo> sorted =
155 new SortedDependencies<SimpleDependencyInfo>(shuffled);
156 assertEquals(expected, sorted.getSortedList());
157 }
158
159 private void assertSortedDeps(
160 List<SimpleDependencyInfo> expected,
161 List<SimpleDependencyInfo> shuffled,
162 List<SimpleDependencyInfo> roots) throws Exception {
163 SortedDependencies<SimpleDependencyInfo> sorted =
164 new SortedDependencies<SimpleDependencyInfo>(shuffled);
165 assertEquals(expected, sorted.getSortedDependenciesOf(roots));
166 }
167
168 private List<String> requires(String ... strings) {
169 return Lists.newArrayList(strings);
170 }
171
172 private List<String> provides(String ... strings) {
173 return Lists.newArrayList(strings);
174 }
175 }