1 28 29 package org.jruby.ast.visitor.rewriter; 30 31 import java.io.BufferedReader ; 32 import java.io.FileNotFoundException ; 33 import java.io.FileReader ; 34 import java.io.StringReader ; 35 import java.io.StringWriter ; 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.regex.Matcher ; 39 import java.util.regex.Pattern ; 40 41 import junit.framework.Test; 42 import junit.framework.TestSuite; 43 44 import org.jruby.common.NullWarnings; 45 import org.jruby.lexer.yacc.LexerSource; 46 import org.jruby.parser.DefaultRubyParser; 47 import org.jruby.parser.RubyParserConfiguration; 48 import org.jruby.parser.RubyParserPool; 49 50 public class SourceRewriteTester extends TestSuite { 51 52 private static final String testRegexp = "^##!(.*)\\s*(\\w*)*$"; 53 private static final String resultRegexp = "^##=.*$"; 54 55 private static class MatcherState { 57 public static final int SKIP = 0; 58 public static final int IN_SOURCE = 1; 59 public static final int IN_EXPECTED_RESULT= 2; 60 } 61 62 public static String generateSource(String original) { 63 if(original.equals("")) 64 return original; 65 DefaultRubyParser parser = RubyParserPool.getInstance().borrowParser(); 66 parser.setWarnings(new NullWarnings()); 67 68 LexerSource lexerSource = new LexerSource("", new StringReader (original)); 69 StringWriter outputWriter = new StringWriter (); 70 ReWriteVisitor visitor = new ReWriteVisitor(outputWriter, original); 71 parser.parse(new RubyParserConfiguration(), lexerSource).getAST().accept(visitor); 72 visitor.flushStream(); 73 RubyParserPool.getInstance().returnParser(parser); 74 return outputWriter.getBuffer().toString(); 75 } 76 77 private static BufferedReader createReader(String file) throws FileNotFoundException { 78 return new BufferedReader (new FileReader (file)); 79 } 80 81 public static Test suite() throws Exception { 82 83 BufferedReader in = createReader("test/org/jruby/ast/visitor/rewriter/TestReWriteVisitorSource.txt"); 84 85 ArrayList testCases = createTests(in); 86 87 return createSuite(testCases); 88 } 89 90 private static TestSuite createSuite(ArrayList testCases) { 91 TestSuite suite = new TestSuite(); 92 Iterator it = testCases.iterator(); 93 while(it.hasNext()) { 94 SourceTestCase subject = (SourceTestCase)it.next(); 95 subject.setGeneratedSource(generateSource(subject.getSource())); 96 suite.addTest(subject); 97 } 98 return suite; 99 } 100 101 102 private static boolean lineMatchesBeginOfTest(String line) { 103 return createMatcherFromString(testRegexp, line).find(); 104 } 105 106 private static Matcher createMatcherFromString(String pattern, String line) { 107 return Pattern.compile(pattern).matcher(line); 108 } 109 110 private static String getNameOfTest(String line) { 111 Matcher matcherBeginOfTest = createMatcherFromString(testRegexp, line); 112 if(matcherBeginOfTest.find()) 113 return matcherBeginOfTest.group(1); 114 else 115 return "Not Named"; 116 } 117 118 private static boolean lineMatchesBeginOfResult(String line) { 119 return createMatcherFromString(resultRegexp, line).find(); 120 } 121 122 private static ArrayList createTests(BufferedReader inputReader) throws Exception { 123 124 String line; 125 SourceTestCase tc = null; 126 int matcherState = MatcherState.SKIP; 127 ArrayList testCases = new ArrayList (); 128 129 while ((line = inputReader.readLine()) != null){ 130 131 if(lineMatchesBeginOfTest(line)) { 132 matcherState = MatcherState.IN_SOURCE; 133 tc = new SourceTestCase(getNameOfTest(line), true); 134 testCases.add(tc); 135 continue; 136 } else if (lineMatchesBeginOfResult(line)) { 137 matcherState = MatcherState.IN_EXPECTED_RESULT; 138 continue; 139 } 140 141 switch(matcherState) { 142 case MatcherState.IN_SOURCE: 143 tc.appendLineToSource(line); 144 break; 145 case MatcherState.IN_EXPECTED_RESULT: 146 tc.appendLineToExpectedResult(line); 147 break; 148 case MatcherState.SKIP: 149 break; 150 } 151 } 152 return testCases; 153 } 154 } 155 156 157 158 | Popular Tags |