Source Code Cross Referenced for TestTreeWizard.java in  » Parser » antlr-3.0.1 » org » antlr » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » antlr 3.0.1 » org.antlr.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         [The "BSD licence"]
003:         Copyright (c) 2005-2007 Terence Parr
004:         All rights reserved.
005:
006:         Redistribution and use in source and binary forms, with or without
007:         modification, are permitted provided that the following conditions
008:         are met:
009:         1. Redistributions of source code must retain the above copyright
010:            notice, this list of conditions and the following disclaimer.
011:         2. Redistributions in binary form must reproduce the above copyright
012:            notice, this list of conditions and the following disclaimer in the
013:            documentation and/or other materials provided with the distribution.
014:         3. The name of the author may not be used to endorse or promote products
015:            derived from this software without specific prior written permission.
016:
017:         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018:         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020:         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021:         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022:         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023:         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024:         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025:         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026:         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:         */
028:        package org.antlr.test;
029:
030:        import org.antlr.runtime.tree.*;
031:
032:        import java.util.Map;
033:        import java.util.List;
034:        import java.util.ArrayList;
035:        import java.util.HashMap;
036:
037:        public class TestTreeWizard extends BaseTest {
038:            protected static final String[] tokens = new String[] { "", "", "",
039:                    "", "", "A", "B", "C", "D", "E", "ID", "VAR" };
040:            protected static final TreeAdaptor adaptor = new CommonTreeAdaptor();
041:
042:            public void testSingleNode() throws Exception {
043:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
044:                CommonTree t = (CommonTree) wiz.create("ID");
045:                String found = t.toStringTree();
046:                String expecting = "ID";
047:                assertEquals(expecting, found);
048:            }
049:
050:            public void testSingleNodeWithArg() throws Exception {
051:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
052:                CommonTree t = (CommonTree) wiz.create("ID[foo]");
053:                String found = t.toStringTree();
054:                String expecting = "foo";
055:                assertEquals(expecting, found);
056:            }
057:
058:            public void testSingleNodeTree() throws Exception {
059:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
060:                CommonTree t = (CommonTree) wiz.create("(A)");
061:                String found = t.toStringTree();
062:                String expecting = "A";
063:                assertEquals(expecting, found);
064:            }
065:
066:            public void testSingleLevelTree() throws Exception {
067:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
068:                CommonTree t = (CommonTree) wiz.create("(A B C D)");
069:                String found = t.toStringTree();
070:                String expecting = "(A B C D)";
071:                assertEquals(expecting, found);
072:            }
073:
074:            public void testListTree() throws Exception {
075:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
076:                CommonTree t = (CommonTree) wiz.create("(nil A B C)");
077:                String found = t.toStringTree();
078:                String expecting = "A B C";
079:                assertEquals(expecting, found);
080:            }
081:
082:            public void testInvalidListTree() throws Exception {
083:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
084:                CommonTree t = (CommonTree) wiz.create("A B C");
085:                assertTrue(t == null);
086:            }
087:
088:            public void testDoubleLevelTree() throws Exception {
089:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
090:                CommonTree t = (CommonTree) wiz.create("(A (B C) (B D) E)");
091:                String found = t.toStringTree();
092:                String expecting = "(A (B C) (B D) E)";
093:                assertEquals(expecting, found);
094:            }
095:
096:            public void testSingleNodeIndex() throws Exception {
097:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
098:                CommonTree t = (CommonTree) wiz.create("ID");
099:                Map m = wiz.index(t);
100:                String found = m.toString();
101:                String expecting = "{10=[ID]}";
102:                assertEquals(expecting, found);
103:            }
104:
105:            public void testNoRepeatsIndex() throws Exception {
106:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
107:                CommonTree t = (CommonTree) wiz.create("(A B C D)");
108:                Map m = wiz.index(t);
109:                String found = m.toString();
110:                String expecting = "{8=[D], 6=[B], 7=[C], 5=[A]}";
111:                assertEquals(expecting, found);
112:            }
113:
114:            public void testRepeatsIndex() throws Exception {
115:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
116:                CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)");
117:                Map m = wiz.index(t);
118:                String found = m.toString();
119:                String expecting = "{8=[D, D], 6=[B, B, B], 7=[C], 5=[A, A]}";
120:                assertEquals(expecting, found);
121:            }
122:
123:            public void testNoRepeatsVisit() throws Exception {
124:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
125:                CommonTree t = (CommonTree) wiz.create("(A B C D)");
126:                final List elements = new ArrayList();
127:                wiz.visit(t, wiz.getTokenType("B"), new TreeWizard.Visitor() {
128:                    public void visit(Object t) {
129:                        elements.add(t);
130:                    }
131:                });
132:                String found = elements.toString();
133:                String expecting = "[B]";
134:                assertEquals(expecting, found);
135:            }
136:
137:            public void testNoRepeatsVisit2() throws Exception {
138:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
139:                CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)");
140:                final List elements = new ArrayList();
141:                wiz.visit(t, wiz.getTokenType("C"), new TreeWizard.Visitor() {
142:                    public void visit(Object t) {
143:                        elements.add(t);
144:                    }
145:                });
146:                String found = elements.toString();
147:                String expecting = "[C]";
148:                assertEquals(expecting, found);
149:            }
150:
151:            public void testRepeatsVisit() throws Exception {
152:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
153:                CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)");
154:                final List elements = new ArrayList();
155:                wiz.visit(t, wiz.getTokenType("B"), new TreeWizard.Visitor() {
156:                    public void visit(Object t) {
157:                        elements.add(t);
158:                    }
159:                });
160:                String found = elements.toString();
161:                String expecting = "[B, B, B]";
162:                assertEquals(expecting, found);
163:            }
164:
165:            public void testRepeatsVisit2() throws Exception {
166:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
167:                CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)");
168:                final List elements = new ArrayList();
169:                wiz.visit(t, wiz.getTokenType("A"), new TreeWizard.Visitor() {
170:                    public void visit(Object t) {
171:                        elements.add(t);
172:                    }
173:                });
174:                String found = elements.toString();
175:                String expecting = "[A, A]";
176:                assertEquals(expecting, found);
177:            }
178:
179:            public void testRepeatsVisitWithContext() throws Exception {
180:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
181:                CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)");
182:                final List elements = new ArrayList();
183:                wiz.visit(t, wiz.getTokenType("B"),
184:                        new TreeWizard.ContextVisitor() {
185:                            public void visit(Object t, Object parent,
186:                                    int childIndex, Map labels) {
187:                                elements.add(adaptor.getText(t)
188:                                        + "@"
189:                                        + (parent != null ? adaptor
190:                                                .getText(parent) : "nil") + "["
191:                                        + childIndex + "]");
192:                            }
193:                        });
194:                String found = elements.toString();
195:                String expecting = "[B@A[0], B@A[1], B@A[2]]";
196:                assertEquals(expecting, found);
197:            }
198:
199:            public void testRepeatsVisitWithNullParentAndContext()
200:                    throws Exception {
201:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
202:                CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)");
203:                final List elements = new ArrayList();
204:                wiz.visit(t, wiz.getTokenType("A"),
205:                        new TreeWizard.ContextVisitor() {
206:                            public void visit(Object t, Object parent,
207:                                    int childIndex, Map labels) {
208:                                elements.add(adaptor.getText(t)
209:                                        + "@"
210:                                        + (parent != null ? adaptor
211:                                                .getText(parent) : "nil") + "["
212:                                        + childIndex + "]");
213:                            }
214:                        });
215:                String found = elements.toString();
216:                String expecting = "[A@nil[0], A@A[1]]";
217:                assertEquals(expecting, found);
218:            }
219:
220:            public void testVisitPattern() throws Exception {
221:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
222:                CommonTree t = (CommonTree) wiz.create("(A B C (A B) D)");
223:                final List elements = new ArrayList();
224:                wiz.visit(t, "(A B)", new TreeWizard.Visitor() {
225:                    public void visit(Object t) {
226:                        elements.add(t);
227:                    }
228:                });
229:                String found = elements.toString();
230:                String expecting = "[A]"; // shouldn't match overall root, just (A B)
231:                assertEquals(expecting, found);
232:            }
233:
234:            public void testVisitPatternMultiple() throws Exception {
235:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
236:                CommonTree t = (CommonTree) wiz
237:                        .create("(A B C (A B) (D (A B)))");
238:                final List elements = new ArrayList();
239:                wiz.visit(t, "(A B)", new TreeWizard.ContextVisitor() {
240:                    public void visit(Object t, Object parent, int childIndex,
241:                            Map labels) {
242:                        elements.add(adaptor.getText(t)
243:                                + "@"
244:                                + (parent != null ? adaptor.getText(parent)
245:                                        : "nil") + "[" + childIndex + "]");
246:                    }
247:                });
248:                String found = elements.toString();
249:                String expecting = "[A@A[2], A@D[0]]"; // shouldn't match overall root, just (A B)
250:                assertEquals(expecting, found);
251:            }
252:
253:            public void testVisitPatternMultipleWithLabels() throws Exception {
254:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
255:                CommonTree t = (CommonTree) wiz
256:                        .create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))");
257:                final List elements = new ArrayList();
258:                wiz.visit(t, "(%a:A %b:B)", new TreeWizard.ContextVisitor() {
259:                    public void visit(Object t, Object parent, int childIndex,
260:                            Map labels) {
261:                        elements.add(adaptor.getText(t)
262:                                + "@"
263:                                + (parent != null ? adaptor.getText(parent)
264:                                        : "nil") + "[" + childIndex + "]"
265:                                + labels.get("a") + "&" + labels.get("b"));
266:                    }
267:                });
268:                String found = elements.toString();
269:                String expecting = "[foo@A[2]foo&bar, big@D[0]big&dog]";
270:                assertEquals(expecting, found);
271:            }
272:
273:            public void testParse() throws Exception {
274:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
275:                CommonTree t = (CommonTree) wiz.create("(A B C)");
276:                boolean valid = wiz.parse(t, "(A B C)");
277:                assertTrue(valid);
278:            }
279:
280:            public void testParseSingleNode() throws Exception {
281:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
282:                CommonTree t = (CommonTree) wiz.create("A");
283:                boolean valid = wiz.parse(t, "A");
284:                assertTrue(valid);
285:            }
286:
287:            public void testParseFlatTree() throws Exception {
288:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
289:                CommonTree t = (CommonTree) wiz.create("(nil A B C)");
290:                boolean valid = wiz.parse(t, "(nil A B C)");
291:                assertTrue(valid);
292:            }
293:
294:            public void testWildcard() throws Exception {
295:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
296:                CommonTree t = (CommonTree) wiz.create("(A B C)");
297:                boolean valid = wiz.parse(t, "(A . .)");
298:                assertTrue(valid);
299:            }
300:
301:            public void testParseWithText() throws Exception {
302:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
303:                CommonTree t = (CommonTree) wiz.create("(A B[foo] C[bar])");
304:                // C pattern has no text arg so despite [bar] in t, no need
305:                // to match text--check structure only.
306:                boolean valid = wiz.parse(t, "(A B[foo] C)");
307:                assertTrue(valid);
308:            }
309:
310:            public void testParseWithTextFails() throws Exception {
311:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
312:                CommonTree t = (CommonTree) wiz.create("(A B C)");
313:                boolean valid = wiz.parse(t, "(A[foo] B C)");
314:                assertTrue(!valid); // fails
315:            }
316:
317:            public void testParseLabels() throws Exception {
318:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
319:                CommonTree t = (CommonTree) wiz.create("(A B C)");
320:                Map labels = new HashMap();
321:                boolean valid = wiz.parse(t, "(%a:A %b:B %c:C)", labels);
322:                assertTrue(valid);
323:                assertEquals("A", labels.get("a").toString());
324:                assertEquals("B", labels.get("b").toString());
325:                assertEquals("C", labels.get("c").toString());
326:            }
327:
328:            public void testParseWithWildcardLabels() throws Exception {
329:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
330:                CommonTree t = (CommonTree) wiz.create("(A B C)");
331:                Map labels = new HashMap();
332:                boolean valid = wiz.parse(t, "(A %b:. %c:.)", labels);
333:                assertTrue(valid);
334:                assertEquals("B", labels.get("b").toString());
335:                assertEquals("C", labels.get("c").toString());
336:            }
337:
338:            public void testParseLabelsAndTestText() throws Exception {
339:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
340:                CommonTree t = (CommonTree) wiz.create("(A B[foo] C)");
341:                Map labels = new HashMap();
342:                boolean valid = wiz.parse(t, "(%a:A %b:B[foo] %c:C)", labels);
343:                assertTrue(valid);
344:                assertEquals("A", labels.get("a").toString());
345:                assertEquals("foo", labels.get("b").toString());
346:                assertEquals("C", labels.get("c").toString());
347:            }
348:
349:            public void testParseLabelsInNestedTree() throws Exception {
350:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
351:                CommonTree t = (CommonTree) wiz.create("(A (B C) (D E))");
352:                Map labels = new HashMap();
353:                boolean valid = wiz.parse(t, "(%a:A (%b:B %c:C) (%d:D %e:E) )",
354:                        labels);
355:                assertTrue(valid);
356:                assertEquals("A", labels.get("a").toString());
357:                assertEquals("B", labels.get("b").toString());
358:                assertEquals("C", labels.get("c").toString());
359:                assertEquals("D", labels.get("d").toString());
360:                assertEquals("E", labels.get("e").toString());
361:            }
362:
363:            public void testEquals() throws Exception {
364:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
365:                CommonTree t1 = (CommonTree) wiz.create("(A B C)");
366:                CommonTree t2 = (CommonTree) wiz.create("(A B C)");
367:                boolean same = TreeWizard.equals(t1, t2, adaptor);
368:                assertTrue(same);
369:            }
370:
371:            public void testEqualsWithText() throws Exception {
372:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
373:                CommonTree t1 = (CommonTree) wiz.create("(A B[foo] C)");
374:                CommonTree t2 = (CommonTree) wiz.create("(A B[foo] C)");
375:                boolean same = TreeWizard.equals(t1, t2, adaptor);
376:                assertTrue(same);
377:            }
378:
379:            public void testEqualsWithMismatchedText() throws Exception {
380:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
381:                CommonTree t1 = (CommonTree) wiz.create("(A B[foo] C)");
382:                CommonTree t2 = (CommonTree) wiz.create("(A B C)");
383:                boolean same = TreeWizard.equals(t1, t2, adaptor);
384:                assertTrue(!same);
385:            }
386:
387:            public void testFindPattern() throws Exception {
388:                TreeWizard wiz = new TreeWizard(adaptor, tokens);
389:                CommonTree t = (CommonTree) wiz
390:                        .create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))");
391:                final List subtrees = wiz.find(t, "(A B)");
392:                List elements = subtrees;
393:                String found = elements.toString();
394:                String expecting = "[foo, big]";
395:                assertEquals(expecting, found);
396:            }
397:
398:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.