KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > ast > visitor > rewriter > TestReWriteVisitor


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2006 Mirko Stocker <me@misto.ch>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28
29 package org.jruby.ast.visitor.rewriter;
30
31 import java.io.BufferedReader JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.FileReader JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.StringWriter JavaDoc;
36
37 import junit.framework.TestCase;
38
39 import org.jruby.ast.ArgumentNode;
40 import org.jruby.ast.ArrayNode;
41 import org.jruby.ast.ConstNode;
42 import org.jruby.ast.LocalVarNode;
43 import org.jruby.ast.Node;
44 import org.jruby.ast.OptNNode;
45 import org.jruby.ast.PostExeNode;
46 import org.jruby.ast.RegexpNode;
47 import org.jruby.ast.visitor.rewriter.ReWriteVisitor;
48 import org.jruby.lexer.yacc.SourcePosition;
49 import org.jruby.util.ByteList;
50
51 public class TestReWriteVisitor extends TestCase {
52
53     static final SourcePosition emptyPosition = new SourcePosition("", 0, 0, 0, 0);
54
55     private String JavaDoc visitNode(Node n) {
56         StringWriter JavaDoc out = new StringWriter JavaDoc();
57         ReWriteVisitor visitor = new ReWriteVisitor(out, "");
58         n.accept(visitor);
59         visitor.flushStream();
60         return out.getBuffer().toString();
61     }
62
63     public void testVisitRegexpNode() {
64         RegexpNode n = new RegexpNode(new SourcePosition("", 0, 0, 2, 4), ByteList.create(".*"), 0);
65         assertEquals("/.*/", visitNode(n));
66     }
67
68     public void testGetLocalVarIndex() {
69         assertEquals(ReWriteVisitor.getLocalVarIndex(new LocalVarNode(emptyPosition, 5, "")), 5);
70         assertEquals(ReWriteVisitor.getLocalVarIndex(new LocalVarNode(emptyPosition, 1, "")), 1);
71         assertEquals(ReWriteVisitor.getLocalVarIndex(null), -1);
72     }
73
74     private ReWriteVisitor getVisitor() {
75         return new ReWriteVisitor(new StringWriter JavaDoc(), "");
76     }
77
78     public void testVisitOptNNode() {
79         assertNull(getVisitor().visitOptNNode(new OptNNode(emptyPosition, null)));
80     }
81
82     public void testVisitPostExeNode() {
83         assertNull(getVisitor().visitPostExeNode(new PostExeNode(emptyPosition, null)));
84     }
85
86     public void testUnwrapSingleArrayNode() {
87         ArrayNode arrayNode = new ArrayNode(emptyPosition);
88         ConstNode constNode = new ConstNode(emptyPosition, "const");
89         ConstNode anotherConstNode = new ConstNode(emptyPosition, "const");
90         arrayNode.add(constNode);
91
92         assertEquals(ReWriteVisitor.unwrapSingleArrayNode(arrayNode), constNode);
93         assertEquals(ReWriteVisitor.unwrapSingleArrayNode(constNode), constNode);
94
95         arrayNode.add(anotherConstNode);
96         assertEquals(ReWriteVisitor.unwrapSingleArrayNode(arrayNode), arrayNode);
97     }
98
99     public void testUnescapeChar() {
100         assertEquals(ReWriteVisitor.unescapeChar('\f'), "f");
101         assertEquals(ReWriteVisitor.unescapeChar('\r'), "r");
102         assertEquals(ReWriteVisitor.unescapeChar('\t'), "t");
103         assertEquals(ReWriteVisitor.unescapeChar('n'), null);
104     }
105
106     public void testArgumentNode() {
107         Node node = new ArgumentNode(new SourcePosition(), "name");
108         assertEquals("name", ReWriteVisitor.createCodeFromNode(node, ""));
109     }
110
111     public void testFileOutputStream() throws IOException JavaDoc {
112         String JavaDoc fileName = "outputTest";
113         try {
114             String JavaDoc testString = "test";
115             FileOutputStream JavaDoc stream = new FileOutputStream JavaDoc(fileName);
116             ReWriteVisitor visitor = new ReWriteVisitor(stream, "");
117             ConstNode node = new ConstNode(emptyPosition, testString);
118             node.accept(visitor);
119             visitor.flushStream();
120             stream.close();
121             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
122             assertEquals(reader.readLine(), testString);
123         } finally {
124             new java.io.File JavaDoc(fileName).delete();
125         }
126     }
127 }
128
129
130
131
Popular Tags