Debian Codegraph @

test/com/google/javascript/jscomp/CombinedCompilerPassTest.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.Lists;
20 import com.google.common.collect.Sets;
21 import com.google.javascript.jscomp.NodeTraversal.Callback;
22 import com.google.javascript.jscomp.NodeTraversal.ScopedCallback;
23 import com.google.javascript.rhino.Node;
24 import com.google.javascript.rhino.Token;
25
26 import junit.framework.TestCase;
27
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.Set;
31
32 /**
33 */
34 public class CombinedCompilerPassTest extends TestCase {
35
36 private Compiler compiler;
37
38 /**
39 * Returns a Node tree with the post-order traversal a b c d e f g h i j k l m
40 * and the in-order traversal m d a b c h e f g l i j k:
41 *
42 * m
43 * ,---------|---------.
44 * d h l
45 * ,--|--. ,--|--. ,--|--.
46 * a b c e f g i j k
47 *
48 */
49 private static Node createPostOrderAlphabet() {
50 Node a = Node.newString("a");
51 Node b = Node.newString("b");
52 Node c = Node.newString("c");
53 Node d = Node.newString("d");
54 Node e = Node.newString("e");
55 Node f = Node.newString("f");
56 Node g = Node.newString("g");
57 Node h = Node.newString("h");
58 Node i = Node.newString("i");
59 Node j = Node.newString("j");
60 Node k = Node.newString("k");
61 Node l = Node.newString("l");
62 Node m = Node.newString("m");
63
64 d.addChildToBack(a);
65 d.addChildToBack(b);
66 d.addChildToBack(c);
67
68 h.addChildrenToBack(e);
69 h.addChildrenToBack(f);
70 h.addChildrenToBack(g);
71
72 l.addChildToBack(i);
73 l.addChildToBack(j);
74 l.addChildToBack(k);
75
76 m.addChildToBack(d);
77 m.addChildToBack(h);
78 m.addChildToBack(l);
79
80 return m;
81 }
82
83 @Override
84 public void setUp() throws Exception {
85 super.setUp();
86 compiler = new Compiler();
87 }
88
89 /**
90 * Concatenates contents of string nodes encountered in pre-order
91 * and post-order traversals. Abbreviates traversals by ignoring subtrees
92 * rooted with specified strings.
93 */
94 private static class ConcatTraversal implements Callback {
95 private StringBuilder visited = new StringBuilder();
96 private StringBuilder shouldTraversed = new StringBuilder();
97 private Set<String> ignoring = Sets.newHashSet();
98
99 ConcatTraversal ignore(String s) {
100 ignoring.add(s);
101 return this;
102 }
103
104 @Override
105 public void visit(NodeTraversal t, Node n, Node parent) {
106 assertEquals(Token.STRING, n.getType());
107 visited.append(n.getString());
108 }
109
110 @Override
111 public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
112 assertEquals(Token.STRING, n.getType());
113 shouldTraversed.append(n.getString());
114 return !ignoring.contains(n.getString());
115 }
116
117 /** Returns strings concatenated during post-order traversal. */
118 String getVisited() {
119 return visited.toString();
120 }
121
122 /** Returns strings concatenated during pre-order traversal. */
123 String getShouldTraversed() {
124 return shouldTraversed.toString();
125 }
126
127 Collection<String> getIgnoring() {
128 return ignoring;
129 }
130 }
131
132 /**
133 * Collection of data for a traversal test. Contains the traversal callback
134 * and the expected pre- and post-order traversal results.
135 */
136 private static class TestHelper {
137 private ConcatTraversal traversal;
138 private String expectedVisited;
139 private String shouldTraverseExpected;
140
141 TestHelper(ConcatTraversal traversal, String expectedVisited,
142 String shouldTraverseExpected) {
143 this.traversal = traversal;
144 this.expectedVisited = expectedVisited;
145 this.shouldTraverseExpected = shouldTraverseExpected;
146 }
147
148 ConcatTraversal getTraversal() {
149 return traversal;
150 }
151
152 void checkResults() {
153 assertEquals("ConcatTraversal ignoring " +
154 traversal.getIgnoring().toString() +
155 " has unexpected visiting order",
156 expectedVisited, traversal.getVisited());
157
158 assertEquals("ConcatTraversal ignoring " +
159 traversal.getIgnoring().toString() +
160 " has unexpected traversal order",
161 shouldTraverseExpected, traversal.getShouldTraversed());
162 }
163 }
164
165 private static List<TestHelper> createStringTests() {
166 List<TestHelper> tests = Lists.newArrayList();
167
168 tests.add(new TestHelper(
169 new ConcatTraversal(), "abcdefghijklm", "mdabchefglijk"));
170
171 tests.add(new TestHelper(
172 new ConcatTraversal().ignore("d"), "efghijklm", "mdhefglijk"));
173
174 tests.add(new TestHelper(
175 new ConcatTraversal().ignore("f"), "abcdeghijklm", "mdabchefglijk"));
176
177 tests.add(new TestHelper(new ConcatTraversal().ignore("m"), "", "m"));
178
179 return tests;
180 }
181
182 public void testIndividualPasses() {
183 for (TestHelper test : createStringTests()) {
184 CombinedCompilerPass pass =
185 new CombinedCompilerPass(compiler, test.getTraversal());
186 pass.process(null, createPostOrderAlphabet());
187 test.checkResults();
188 }
189 }
190
191 public void testCombinedPasses() {
192 List<TestHelper> tests = createStringTests();
193 Callback[] callbacks = new Callback[tests.size()];
194 int i = 0;
195 for (TestHelper test : tests) {
196 callbacks[i++] = test.getTraversal();
197 }
198 CombinedCompilerPass pass =
199 new CombinedCompilerPass(compiler, callbacks);
200 pass.process(null, createPostOrderAlphabet());
201 for (TestHelper test : tests) {
202 test.checkResults();
203 }
204 }
205
206 /**
207 * Records the scopes visited during an AST traversal. Abbreviates traversals
208 * by ignoring subtrees rooted with specified NAME nodes.
209 */
210 private static class ScopeRecordingCallback implements ScopedCallback {
211
212 Set<Node> visitedScopes = Sets.newHashSet();
213 Set<String> ignoring = Sets.newHashSet();
214
215 void ignore(String name) {
216 ignoring.add(name);
217 }
218
219 @Override
220 public void enterScope(NodeTraversal t) {
221 visitedScopes.add(t.getScopeRoot());
222 }
223
224 @Override
225 public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
226 return !n.isName() || !ignoring.contains(n.getString());
227 }
228
229 Set<Node> getVisitedScopes() {
230 return visitedScopes;
231 }
232
233 @Override
234 public void exitScope(NodeTraversal t) {
235 }
236
237 @Override
238 public void visit(NodeTraversal t, Node n, Node parent) {
239 }
240
241 }
242
243 public void testScopes() {
244 Node root =
245 compiler.parseTestCode("var y = function() { var x = function() { };}");
246
247 ScopeRecordingCallback c1 = new ScopeRecordingCallback();
248 c1.ignore("y");
249 ScopeRecordingCallback c2 = new ScopeRecordingCallback();
250 c2.ignore("x");
251 ScopeRecordingCallback c3 = new ScopeRecordingCallback();
252
253 CombinedCompilerPass pass = new CombinedCompilerPass(compiler, c1, c2, c3);
254 pass.process(null, root);
255
256 assertEquals(1, c1.getVisitedScopes().size());
257 assertEquals(2, c2.getVisitedScopes().size());
258 assertEquals(3, c3.getVisitedScopes().size());
259 }
260 }