OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 package com.google.dart.compiler.backend.js; | |
6 | |
7 import com.google.dart.compiler.backend.js.FlatteningVisitor.TreeNode; | |
8 import com.google.dart.compiler.backend.js.ast.JsArrayAccess; | |
9 import com.google.dart.compiler.backend.js.ast.JsArrayLiteral; | |
10 import com.google.dart.compiler.backend.js.ast.JsBinaryOperation; | |
11 import com.google.dart.compiler.backend.js.ast.JsBlock; | |
12 import com.google.dart.compiler.backend.js.ast.JsBooleanLiteral; | |
13 import com.google.dart.compiler.backend.js.ast.JsBreak; | |
14 import com.google.dart.compiler.backend.js.ast.JsCase; | |
15 import com.google.dart.compiler.backend.js.ast.JsCatch; | |
16 import com.google.dart.compiler.backend.js.ast.JsConditional; | |
17 import com.google.dart.compiler.backend.js.ast.JsContext; | |
18 import com.google.dart.compiler.backend.js.ast.JsContinue; | |
19 import com.google.dart.compiler.backend.js.ast.JsDebugger; | |
20 import com.google.dart.compiler.backend.js.ast.JsDefault; | |
21 import com.google.dart.compiler.backend.js.ast.JsDoWhile; | |
22 import com.google.dart.compiler.backend.js.ast.JsEmpty; | |
23 import com.google.dart.compiler.backend.js.ast.JsExprStmt; | |
24 import com.google.dart.compiler.backend.js.ast.JsFor; | |
25 import com.google.dart.compiler.backend.js.ast.JsForIn; | |
26 import com.google.dart.compiler.backend.js.ast.JsFunction; | |
27 import com.google.dart.compiler.backend.js.ast.JsIf; | |
28 import com.google.dart.compiler.backend.js.ast.JsInvocation; | |
29 import com.google.dart.compiler.backend.js.ast.JsLabel; | |
30 import com.google.dart.compiler.backend.js.ast.JsName; | |
31 import com.google.dart.compiler.backend.js.ast.JsNameRef; | |
32 import com.google.dart.compiler.backend.js.ast.JsNew; | |
33 import com.google.dart.compiler.backend.js.ast.JsNullLiteral; | |
34 import com.google.dart.compiler.backend.js.ast.JsNumberLiteral; | |
35 import com.google.dart.compiler.backend.js.ast.JsObjectLiteral; | |
36 import com.google.dart.compiler.backend.js.ast.JsParameter; | |
37 import com.google.dart.compiler.backend.js.ast.JsPostfixOperation; | |
38 import com.google.dart.compiler.backend.js.ast.JsPrefixOperation; | |
39 import com.google.dart.compiler.backend.js.ast.JsProgram; | |
40 import com.google.dart.compiler.backend.js.ast.JsPropertyInitializer; | |
41 import com.google.dart.compiler.backend.js.ast.JsRegExp; | |
42 import com.google.dart.compiler.backend.js.ast.JsReturn; | |
43 import com.google.dart.compiler.backend.js.ast.JsStatement; | |
44 import com.google.dart.compiler.backend.js.ast.JsStringLiteral; | |
45 import com.google.dart.compiler.backend.js.ast.JsSwitch; | |
46 import com.google.dart.compiler.backend.js.ast.JsThisRef; | |
47 import com.google.dart.compiler.backend.js.ast.JsThrow; | |
48 import com.google.dart.compiler.backend.js.ast.JsTry; | |
49 import com.google.dart.compiler.backend.js.ast.JsVars; | |
50 import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; | |
51 import com.google.dart.compiler.backend.js.ast.JsVisitable; | |
52 import com.google.dart.compiler.backend.js.ast.JsVisitor; | |
53 import com.google.dart.compiler.backend.js.ast.JsWhile; | |
54 | |
55 import junit.framework.Assert; | |
56 import junit.framework.TestCase; | |
57 | |
58 import java.util.List; | |
59 | |
60 class ComparingVisitor extends JsVisitor { | |
61 | |
62 public static void exec(List<JsStatement> expected, List<JsStatement> actual)
{ | |
63 TreeNode expectedTree = FlatteningVisitor.exec(expected); | |
64 TreeNode actualTree = FlatteningVisitor.exec(actual); | |
65 compare(expectedTree, actualTree); | |
66 } | |
67 | |
68 private static void compare(JsVisitable expected, JsVisitable actual) { | |
69 if (expected == actual) { | |
70 return; | |
71 } | |
72 Assert.assertNotNull(expected); | |
73 Assert.assertNotNull(actual); | |
74 ComparingVisitor visitor = new ComparingVisitor(expected); | |
75 visitor.accept(actual); | |
76 } | |
77 | |
78 private static void compare(TreeNode expected, TreeNode actual) { | |
79 compare(expected.node, actual.node); | |
80 List<TreeNode> expectedChildren = expected.children; | |
81 List<TreeNode> actualChildren = actual.children; | |
82 Assert.assertEquals(expectedChildren.size(), actualChildren.size()); | |
83 for (int i = 0; i < expectedChildren.size(); i++) { | |
84 compare(expectedChildren.get(i), actualChildren.get(i)); | |
85 } | |
86 } | |
87 | |
88 /** | |
89 * We use a raw type here because Sun's javac will barf all over the casts and | |
90 * instanceof tests we do all throughout this file. | |
91 */ | |
92 private final JsVisitable other; | |
93 | |
94 private ComparingVisitor(JsVisitable other) { | |
95 this.other = other; | |
96 } | |
97 | |
98 @Override | |
99 public boolean visit(JsArrayAccess x, JsContext ctx) { | |
100 Assert.assertTrue(other instanceof JsArrayAccess); | |
101 return false; | |
102 } | |
103 | |
104 @Override | |
105 public boolean visit(JsArrayLiteral x, JsContext ctx) { | |
106 Assert.assertTrue(other instanceof JsArrayLiteral); | |
107 return false; | |
108 } | |
109 | |
110 @Override | |
111 public boolean visit(JsBinaryOperation x, JsContext ctx) { | |
112 Assert.assertTrue(other instanceof JsBinaryOperation); | |
113 Assert.assertEquals(((JsBinaryOperation) other).getOperator().getSymbol(), | |
114 x.getOperator().getSymbol()); | |
115 return false; | |
116 } | |
117 | |
118 @Override | |
119 public boolean visit(JsBlock x, JsContext ctx) { | |
120 Assert.assertTrue(other instanceof JsBlock); | |
121 Assert.assertEquals(((JsBlock) other).isGlobalBlock(), x.isGlobalBlock()); | |
122 return false; | |
123 } | |
124 | |
125 @Override | |
126 public boolean visit(JsBooleanLiteral x, JsContext ctx) { | |
127 Assert.assertTrue(other instanceof JsBooleanLiteral); | |
128 Assert.assertEquals(((JsBooleanLiteral) other).getValue(), x.getValue()); | |
129 return false; | |
130 } | |
131 | |
132 @Override | |
133 public boolean visit(JsBreak x, JsContext ctx) { | |
134 Assert.assertTrue(other instanceof JsBreak); | |
135 Assert.assertEquals(((JsBreak) other).getLabel().getIdent(), x.getLabel().ge
tIdent()); | |
136 return false; | |
137 } | |
138 | |
139 @Override | |
140 public boolean visit(JsCase x, JsContext ctx) { | |
141 Assert.assertTrue(other instanceof JsCase); | |
142 return false; | |
143 } | |
144 | |
145 @Override | |
146 public boolean visit(JsCatch x, JsContext ctx) { | |
147 Assert.assertTrue(other instanceof JsCatch); | |
148 Assert.assertEquals(((JsCatch) other).getParameter().getName().getIdent(), | |
149 x.getParameter().getName().getIdent()); | |
150 return false; | |
151 } | |
152 | |
153 @Override | |
154 public boolean visit(JsConditional x, JsContext ctx) { | |
155 Assert.assertTrue(other instanceof JsConditional); | |
156 return false; | |
157 } | |
158 | |
159 @Override | |
160 public boolean visit(JsContinue x, JsContext ctx) { | |
161 Assert.assertTrue(other instanceof JsContinue); | |
162 Assert.assertEquals(((JsContinue) other).getLabel().getIdent(), x.getLabel()
.getIdent()); | |
163 return false; | |
164 } | |
165 | |
166 @Override | |
167 public boolean visit(JsDebugger x, JsContext ctx) { | |
168 Assert.assertTrue(other instanceof JsDebugger); | |
169 return false; | |
170 } | |
171 | |
172 @Override | |
173 public boolean visit(JsDefault x, JsContext ctx) { | |
174 Assert.assertTrue(other instanceof JsDefault); | |
175 return false; | |
176 } | |
177 | |
178 @Override | |
179 public boolean visit(JsDoWhile x, JsContext ctx) { | |
180 Assert.assertTrue(other instanceof JsDoWhile); | |
181 return false; | |
182 } | |
183 | |
184 @Override | |
185 public boolean visit(JsEmpty x, JsContext ctx) { | |
186 Assert.assertTrue(other instanceof JsEmpty); | |
187 return false; | |
188 } | |
189 | |
190 @Override | |
191 public boolean visit(JsExprStmt x, JsContext ctx) { | |
192 Assert.assertTrue(other instanceof JsExprStmt); | |
193 return false; | |
194 } | |
195 | |
196 @Override | |
197 public boolean visit(JsFor x, JsContext ctx) { | |
198 Assert.assertTrue(other instanceof JsFor); | |
199 return false; | |
200 } | |
201 | |
202 @Override | |
203 public boolean visit(JsForIn x, JsContext ctx) { | |
204 Assert.assertTrue(other instanceof JsForIn); | |
205 return false; | |
206 } | |
207 | |
208 @Override | |
209 public boolean visit(JsFunction x, JsContext ctx) { | |
210 Assert.assertTrue(other instanceof JsFunction); | |
211 JsFunction otherFunc = (JsFunction) other; | |
212 JsName otherName = otherFunc.getName(); | |
213 JsName name = x.getName(); | |
214 if (name != otherName) { | |
215 Assert.assertEquals(otherName.getIdent(), name.getIdent()); | |
216 } | |
217 return false; | |
218 } | |
219 | |
220 @Override | |
221 public boolean visit(JsIf x, JsContext ctx) { | |
222 Assert.assertTrue(other instanceof JsIf); | |
223 return false; | |
224 } | |
225 | |
226 @Override | |
227 public boolean visit(JsInvocation x, JsContext ctx) { | |
228 Assert.assertTrue(other instanceof JsInvocation); | |
229 return false; | |
230 } | |
231 | |
232 @Override | |
233 public boolean visit(JsLabel x, JsContext ctx) { | |
234 Assert.assertTrue(other instanceof JsLabel); | |
235 Assert.assertEquals(((JsLabel) other).getName().getIdent(), x.getName().getI
dent()); | |
236 return false; | |
237 } | |
238 | |
239 @Override | |
240 public boolean visit(JsNameRef x, JsContext ctx) { | |
241 Assert.assertTrue(other instanceof JsNameRef); | |
242 Assert.assertEquals(((JsNameRef) other).getIdent(), x.getIdent()); | |
243 return false; | |
244 } | |
245 | |
246 @Override | |
247 public boolean visit(JsNew x, JsContext ctx) { | |
248 Assert.assertTrue(other instanceof JsNew); | |
249 return false; | |
250 } | |
251 | |
252 @Override | |
253 public boolean visit(JsNullLiteral x, JsContext ctx) { | |
254 Assert.assertTrue(other instanceof JsNullLiteral); | |
255 return false; | |
256 } | |
257 | |
258 @Override | |
259 public boolean visit(JsNumberLiteral x, JsContext ctx) { | |
260 Assert.assertTrue(other instanceof JsNumberLiteral); | |
261 Assert.assertEquals(((JsNumberLiteral) other).getValue(), x.getValue()); | |
262 return false; | |
263 } | |
264 | |
265 @Override | |
266 public boolean visit(JsObjectLiteral x, JsContext ctx) { | |
267 Assert.assertTrue(other instanceof JsObjectLiteral); | |
268 return false; | |
269 } | |
270 | |
271 @Override | |
272 public boolean visit(JsParameter x, JsContext ctx) { | |
273 Assert.assertTrue(other instanceof JsParameter); | |
274 Assert.assertEquals(((JsParameter) other).getName().getIdent(), x.getName().
getIdent()); | |
275 return false; | |
276 } | |
277 | |
278 @Override | |
279 public boolean visit(JsPostfixOperation x, JsContext ctx) { | |
280 Assert.assertTrue(other instanceof JsPostfixOperation); | |
281 Assert.assertEquals(((JsPostfixOperation) other).getOperator().getSymbol(), | |
282 x.getOperator().getSymbol()); | |
283 return false; | |
284 } | |
285 | |
286 @Override | |
287 public boolean visit(JsPrefixOperation x, JsContext ctx) { | |
288 Assert.assertTrue(other instanceof JsPrefixOperation); | |
289 Assert.assertEquals(((JsPrefixOperation) other).getOperator().getSymbol(), | |
290 x.getOperator().getSymbol()); | |
291 return false; | |
292 } | |
293 | |
294 @Override | |
295 public boolean visit(JsProgram x, JsContext ctx) { | |
296 Assert.assertTrue(other instanceof JsProgram); | |
297 return false; | |
298 } | |
299 | |
300 @Override | |
301 public boolean visit(JsPropertyInitializer x, JsContext ctx) { | |
302 Assert.assertTrue(other instanceof JsPropertyInitializer); | |
303 return false; | |
304 } | |
305 | |
306 @Override | |
307 public boolean visit(JsRegExp x, JsContext ctx) { | |
308 Assert.assertTrue(other instanceof JsRegExp); | |
309 Assert.assertEquals(((JsRegExp) other).getFlags(), x.getFlags()); | |
310 Assert.assertEquals(((JsRegExp) other).getPattern(), x.getPattern()); | |
311 return false; | |
312 } | |
313 | |
314 @Override | |
315 public boolean visit(JsReturn x, JsContext ctx) { | |
316 Assert.assertTrue(other instanceof JsReturn); | |
317 return false; | |
318 } | |
319 | |
320 @Override | |
321 public boolean visit(JsStringLiteral x, JsContext ctx) { | |
322 Assert.assertTrue(other instanceof JsStringLiteral); | |
323 Assert.assertEquals(((JsStringLiteral) other).getValue(), x.getValue()); | |
324 return false; | |
325 } | |
326 | |
327 @Override | |
328 public boolean visit(JsSwitch x, JsContext ctx) { | |
329 Assert.assertTrue(other instanceof JsSwitch); | |
330 return false; | |
331 } | |
332 | |
333 @Override | |
334 public boolean visit(JsThisRef x, JsContext ctx) { | |
335 Assert.assertTrue(other instanceof JsThisRef); | |
336 return false; | |
337 } | |
338 | |
339 @Override | |
340 public boolean visit(JsThrow x, JsContext ctx) { | |
341 Assert.assertTrue(other instanceof JsThrow); | |
342 return false; | |
343 } | |
344 | |
345 @Override | |
346 public boolean visit(JsTry x, JsContext ctx) { | |
347 Assert.assertTrue(other instanceof JsTry); | |
348 return false; | |
349 } | |
350 | |
351 @Override | |
352 public boolean visit(JsVar x, JsContext ctx) { | |
353 TestCase.assertTrue(other instanceof JsVar); | |
354 TestCase.assertEquals(((JsVar) other).getName().getIdent(), x.getName().getI
dent()); | |
355 return false; | |
356 } | |
357 | |
358 public boolean visit(JsVars x, JsContext ctx) { | |
359 TestCase.assertTrue(other instanceof JsVars); | |
360 return false; | |
361 } | |
362 | |
363 public boolean visit(JsWhile x, JsContext ctx) { | |
364 TestCase.assertTrue(other instanceof JsWhile); | |
365 return false; | |
366 } | |
367 } | |
OLD | NEW |