1 28 29 package org.jruby.ast.visitor.rewriter; 30 31 import java.io.BufferedReader ; 32 import java.io.FileOutputStream ; 33 import java.io.FileReader ; 34 import java.io.IOException ; 35 import java.io.StringWriter ; 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 visitNode(Node n) { 56 StringWriter out = new StringWriter (); 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 (), ""); 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 { 112 String fileName = "outputTest"; 113 try { 114 String testString = "test"; 115 FileOutputStream stream = new FileOutputStream (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 reader = new BufferedReader (new FileReader (fileName)); 122 assertEquals(reader.readLine(), testString); 123 } finally { 124 new java.io.File (fileName).delete(); 125 } 126 } 127 } 128 129 130 131 | Popular Tags |