KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileNotFoundException JavaDoc;
33 import java.io.FileReader JavaDoc;
34 import java.io.StringReader JavaDoc;
35 import java.io.StringWriter JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.regex.Matcher JavaDoc;
39 import java.util.regex.Pattern JavaDoc;
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 JavaDoc testRegexp = "^##!(.*)\\s*(\\w*)*$";
53     private static final String JavaDoc resultRegexp = "^##=.*$";
54     
55     // replace with an enum as soon as we can use 1.5
56
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 JavaDoc generateSource(String JavaDoc 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 JavaDoc(original));
69         StringWriter JavaDoc outputWriter = new StringWriter JavaDoc();
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 JavaDoc createReader(String JavaDoc file) throws FileNotFoundException JavaDoc {
78         return new BufferedReader JavaDoc(new FileReader JavaDoc(file));
79     }
80     
81     public static Test suite() throws Exception JavaDoc {
82         
83         BufferedReader JavaDoc in = createReader("test/org/jruby/ast/visitor/rewriter/TestReWriteVisitorSource.txt");
84         
85         ArrayList JavaDoc testCases = createTests(in);
86
87         return createSuite(testCases);
88     }
89
90     private static TestSuite createSuite(ArrayList JavaDoc testCases) {
91         TestSuite suite = new TestSuite();
92         Iterator JavaDoc 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 JavaDoc line) {
103         return createMatcherFromString(testRegexp, line).find();
104     }
105
106     private static Matcher JavaDoc createMatcherFromString(String JavaDoc pattern, String JavaDoc line) {
107         return Pattern.compile(pattern).matcher(line);
108     }
109     
110     private static String JavaDoc getNameOfTest(String JavaDoc line) {
111         Matcher JavaDoc 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 JavaDoc line) {
119         return createMatcherFromString(resultRegexp, line).find();
120     }
121
122     private static ArrayList JavaDoc createTests(BufferedReader JavaDoc inputReader) throws Exception JavaDoc {
123         
124         String JavaDoc line;
125         SourceTestCase tc = null;
126         int matcherState = MatcherState.SKIP;
127         ArrayList JavaDoc testCases = new ArrayList JavaDoc();
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