/* [The "BSD licence"] Copyright (c) 2007-2008 Leon, Jen-Yuan Su All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ group junit; classHeader(header,junitFileName) ::= <<
import junit.framework.TestCase; import java.io.*; import java.lang.reflect.*; import org.antlr.runtime.*; import org.antlr.runtime.tree.*; public class extends TestCase { String stdout; String stderr;<\n><\n> >> testTreeRuleMethod(methodName,testTreeRuleName,testRuleName,testInput,isFile,tokenType,expecting) ::= << public void () throws Exception { // test input: Object retval = execTreeParser(, , , ); Object actual = examineParserExecResult(, retval); Object expecting = ; assertEquals("testing rule "+, expecting, actual); }<\n><\n> >> testTreeRuleMethod2(methodName,testTreeRuleName,testRuleName,testInput,isFile,returnType,expecting) ::= << public void () throws Exception { // test input: retval = ()execTreeParser(, , , ); assertTrue("testing rule "+, ); }<\n><\n> >> testRuleMethod(methodName,testRuleName,testInput,isFile,tokenType,expecting) ::= << public void () throws Exception { // test input: Object retval = execParser(, , ); Object actual = examineParserExecResult(, retval); Object expecting = ; assertEquals("testing rule "+, expecting, actual); }<\n><\n> >> testRuleMethod2(methodName,testRuleName,testInput,isFile,returnType,expecting) ::= << public void () throws Exception { // test input: retval = ()execParser(, , ); assertTrue("testing rule "+, ); }<\n><\n> >> execParser(lexerName,parserName,parserPath) ::= << // Invoke target parser.rule public Object execParser(String testRuleName, String testInput, boolean isFile) throws Exception { CharStream input; /** Set up ANTLR input stream based on input source, file or String */ if ( isFile==true ) { input = new ANTLRFileStream(testInput); } else { input = new ANTLRStringStream(testInput); } try { lexer = new (input); CommonTokenStream tokens = new CommonTokenStream(lexer); parser = new (tokens); /** Use Reflection to get rule method from parser */ Method ruleName = Class.forName("").getMethod(testRuleName); /** Start of I/O Redirecting */ PipedInputStream pipedIn = new PipedInputStream(); PipedOutputStream pipedOut = new PipedOutputStream(); PipedInputStream pipedErrIn = new PipedInputStream(); PipedOutputStream pipedErrOut = new PipedOutputStream(); try { pipedOut.connect(pipedIn); pipedErrOut.connect(pipedErrIn); } catch(IOException e) { System.err.println("connection failed..."); System.exit(1); } PrintStream console = System.out; PrintStream consoleErr = System.err; PrintStream ps = new PrintStream(pipedOut); PrintStream ps2 = new PrintStream(pipedErrOut); System.setOut(ps); System.setErr(ps2); /** End of redirecting */ /** Invoke grammar rule, and store if there is a return value */ Object ruleReturn = ruleName.invoke(parser); String astString = null; /** If rule has return value, determine if it's an AST */ if ( ruleReturn!=null ) { /** If return object is instanceof AST, get the toStringTree */ if ( ruleReturn.toString().indexOf(testRuleName+"_return")>0 ) { try { // NullPointerException may happen here... Class _return = Class.forName(""+"$"+testRuleName+"_return"); Method[] methods = _return.getDeclaredMethods(); for(Method method : methods) { if ( method.getName().equals("getTree") ) { Method returnName = _return.getMethod("getTree"); CommonTree tree = (CommonTree) returnName.invoke(ruleReturn); astString = tree.toStringTree(); } } } catch(Exception e) { System.err.println(e); } } } org.antlr.gunit.gUnitExecuter.StreamVacuum stdoutVacuum = new org.antlr.gunit.gUnitExecuter.StreamVacuum(pipedIn); org.antlr.gunit.gUnitExecuter.StreamVacuum stderrVacuum = new org.antlr.gunit.gUnitExecuter.StreamVacuum(pipedErrIn); ps.close(); ps2.close(); System.setOut(console); // Reset standard output System.setErr(consoleErr); // Reset standard err out this.stdout = null; this.stderr = null; stdoutVacuum.start(); stderrVacuum.start(); stdoutVacuum.join(); stderrVacuum.join(); // retVal could be actual return object from rule, stderr or stdout if ( stderrVacuum.toString().length()>0 ) { this.stderr = stderrVacuum.toString(); return this.stderr; } if ( stdoutVacuum.toString().length()>0 ) { this.stdout = stdoutVacuum.toString(); } if ( astString!=null ) { // Return toStringTree of AST return astString; } if ( ruleReturn!=null ) { return ruleReturn; } if ( stderrVacuum.toString().length()==0 && stdoutVacuum.toString().length()==0 ) { return null; } } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); } catch (SecurityException e) { e.printStackTrace(); System.exit(1); } catch (NoSuchMethodException e) { e.printStackTrace(); System.exit(1); } catch (IllegalAccessException e) { e.printStackTrace(); System.exit(1); } catch (InvocationTargetException e) { return e.getCause().toString(); } catch (InterruptedException e) { e.printStackTrace(); System.exit(1); } catch (Exception e) { e.printStackTrace(); System.exit(1); } return stdout; }<\n><\n> >> execTreeParser(treeParserName,lexerName,parserName,parserPath,treeParserPath) ::= << // Invoke target parser.rule public Object execTreeParser(String testTreeRuleName, String testRuleName, String testInput, boolean isFile) throws Exception { CharStream input; if ( isFile==true ) { input = new ANTLRFileStream(testInput); } else { input = new ANTLRStringStream(testInput); } try { lexer = new (input); CommonTokenStream tokens = new CommonTokenStream(lexer); parser = new (tokens); /** Use Reflection to get rule method from parser */ Method ruleName = Class.forName("").getMethod(testRuleName); /** Start of I/O Redirecting */ PipedInputStream pipedIn = new PipedInputStream(); PipedOutputStream pipedOut = new PipedOutputStream(); PipedInputStream pipedErrIn = new PipedInputStream(); PipedOutputStream pipedErrOut = new PipedOutputStream(); try { pipedOut.connect(pipedIn); pipedErrOut.connect(pipedErrIn); } catch(IOException e) { System.err.println("connection failed..."); System.exit(1); } PrintStream console = System.out; PrintStream consoleErr = System.err; PrintStream ps = new PrintStream(pipedOut); PrintStream ps2 = new PrintStream(pipedErrOut); System.setOut(ps); System.setErr(ps2); /** End of redirecting */ /** Invoke grammar rule, and get the return value */ Object ruleReturn = ruleName.invoke(parser); Class _return = Class.forName(""+"$"+testRuleName+"_return"); Method returnName = _return.getMethod("getTree"); CommonTree tree = (CommonTree) returnName.invoke(ruleReturn); // Walk resulting tree; create tree nodes stream first CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree); // AST nodes have payload that point into token stream nodes.setTokenStream(tokens); // Create a tree walker attached to the nodes stream treeParser = new (nodes); /** Invoke the tree rule, and store the return value if there is */ Method treeRuleName = Class.forName("").getMethod(testTreeRuleName); Object treeRuleReturn = treeRuleName.invoke(treeParser); String astString = null; /** If tree rule has return value, determine if it's an AST */ if ( treeRuleReturn!=null ) { /** If return object is instanceof AST, get the toStringTree */ if ( treeRuleReturn.toString().indexOf(testTreeRuleName+"_return")>0 ) { try { // NullPointerException may happen here... Class _treeReturn = Class.forName(""+"$"+testTreeRuleName+"_return"); Method[] methods = _treeReturn.getDeclaredMethods(); for(Method method : methods) { if ( method.getName().equals("getTree") ) { Method treeReturnName = _treeReturn.getMethod("getTree"); CommonTree returnTree = (CommonTree) treeReturnName.invoke(treeRuleReturn); astString = returnTree.toStringTree(); } } } catch(Exception e) { System.err.println(e); } } } org.antlr.gunit.gUnitExecuter.StreamVacuum stdoutVacuum = new org.antlr.gunit.gUnitExecuter.StreamVacuum(pipedIn); org.antlr.gunit.gUnitExecuter.StreamVacuum stderrVacuum = new org.antlr.gunit.gUnitExecuter.StreamVacuum(pipedErrIn); ps.close(); ps2.close(); System.setOut(console); // Reset standard output System.setErr(consoleErr); // Reset standard err out this.stdout = null; this.stderr = null; stdoutVacuum.start(); stderrVacuum.start(); stdoutVacuum.join(); stderrVacuum.join(); // retVal could be actual return object from rule, stderr or stdout if ( stderrVacuum.toString().length()>0 ) { this.stderr = stderrVacuum.toString(); return this.stderr; } if ( stdoutVacuum.toString().length()>0 ) { this.stdout = stdoutVacuum.toString(); } if ( astString!=null ) { // Return toStringTree of AST return astString; } if ( treeRuleReturn!=null ) { return treeRuleReturn; } if ( stderrVacuum.toString().length()==0 && stdoutVacuum.toString().length()==0 ) { return null; } } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); } catch (SecurityException e) { e.printStackTrace(); System.exit(1); } catch (NoSuchMethodException e) { e.printStackTrace(); System.exit(1); } catch (IllegalAccessException e) { e.printStackTrace(); System.exit(1); } catch (InvocationTargetException e) { return e.getCause().toString(); } catch (InterruptedException e) { e.printStackTrace(); System.exit(1); } catch (Exception e) { e.printStackTrace(); System.exit(1); } return stdout; }<\n><\n> >> examineParserExecResult() ::= << // Modify the return value if the expected token type is OK or FAIL public Object examineParserExecResult(int tokenType, Object retVal) { if ( tokenType==27 ) { // expected Token: OK if ( this.stderr==null ) { return "OK"; } else { return "FAIL"; } } else if ( tokenType==28 ) { // expected Token: FAIL if ( this.stderr!=null ) { return "FAIL"; } else { return "OK"; } } else { // return the same object for the other token types return retVal; } } >>