aboutsummaryrefslogtreecommitdiff
path: root/gunit
diff options
context:
space:
mode:
authorHaibo Huang <hhb@google.com>2018-08-28 13:51:54 -0700
committerHaibo Huang <hhb@google.com>2018-08-28 21:00:58 +0000
commitb7f974f39180d46da516fb24eb2b1ca67d56089c (patch)
tree5f5626f8cde04eb9b6492d7dab0a6f2279d52e6d /gunit
parentbbed35ef4bfde5c3ed2fb5001998b747ce8020a4 (diff)
downloadantlr-b7f974f39180d46da516fb24eb2b1ca67d56089c.tar.gz
Update antlr to 3.5.2
1. Add post_update.sh to preserve build.gradle. 2. Add the patches/fix_infinite_recursion.diff to fix infinite recursion. 3. Run tools/external_updater/updater.sh update antlr (Should we upgrade to v4?) Test: m javac-check RUN_ERROR_PRONE=true Change-Id: Idafdb1cf6cf2310d983be612f36c5e6596a6e23f
Diffstat (limited to 'gunit')
-rw-r--r--gunit/pom.xml60
-rw-r--r--gunit/src/main/antlr3/org/antlr/gunit/swingui/parsers/StGUnit.g16
-rw-r--r--gunit/src/main/java/org/antlr/gunit/AbstractTest.java5
-rw-r--r--gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java34
-rw-r--r--gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java190
-rw-r--r--gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java180
-rw-r--r--gunit/src/main/java/org/antlr/gunit/swingui/TestCaseEditController.java4
-rw-r--r--gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuite.java2
-rw-r--r--gunit/src/main/java/org/antlr/gunit/swingui/runner/ParserLoader.java12
9 files changed, 226 insertions, 277 deletions
diff --git a/gunit/pom.xml b/gunit/pom.xml
index cb35b6b..8a8da9e 100644
--- a/gunit/pom.xml
+++ b/gunit/pom.xml
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
- <groupId>org.antlr</groupId>
<artifactId>gunit</artifactId>
<packaging>jar</packaging>
- <name>ANTLR gUnit v3.4</name>
+ <name>ANTLR 3 gUnit</name>
+ <description>gUnit grammar testing tool for ANTLR 3</description>
+
<!--
Inherit from the ANTLR master pom, which tells us what
@@ -16,11 +17,11 @@
<parent>
<groupId>org.antlr</groupId>
<artifactId>antlr-master</artifactId>
- <version>3.4</version>
+ <version>3.5.2</version>
</parent>
<url>http://www.antlr.org/wiki/display/ANTLR3/gUnit+-+Grammar+Unit+Testing</url>
-
+
<!--
Tell Maven which other artifacts we need in order to
@@ -37,7 +38,6 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <version>4.8.2</version>
<scope>compile</scope>
</dependency>
@@ -48,34 +48,24 @@
<scope>compile</scope>
</dependency>
-
- <dependency>
- <groupId>org.antlr</groupId>
- <artifactId>ST4</artifactId>
- <version>4.0.4</version>
- <scope>compile</scope>
- </dependency>
-
+
<dependency>
<groupId>org.antlr</groupId>
<artifactId>stringtemplate</artifactId>
- <version>3.2.1</version>
<scope>compile</scope>
</dependency>
-
+
</dependencies>
<build>
- <defaultGoal>install</defaultGoal>
-
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr3-maven-plugin</artifactId>
<version>${project.version}</version>
- <configuration></configuration>
+ <configuration />
<executions>
<execution>
<goals>
@@ -85,42 +75,8 @@
</executions>
</plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>jsr14</target>
- <sourceDirectory>src</sourceDirectory>
- </configuration>
- </plugin>
-
- <plugin>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.9</version>
- </plugin>
-
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>findbugs-maven-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <findbugsXmlOutput>true</findbugsXmlOutput>
- <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
- <xmlOutput>true</xmlOutput>
- </configuration>
- </plugin>
-
</plugins>
- <extensions>
- <extension>
- <groupId>org.apache.maven.wagon</groupId>
- <artifactId>wagon-ssh-external</artifactId>
- <version>1.0-beta-2</version>
- </extension>
- </extensions>
-
-
</build>
</project>
diff --git a/gunit/src/main/antlr3/org/antlr/gunit/swingui/parsers/StGUnit.g b/gunit/src/main/antlr3/org/antlr/gunit/swingui/parsers/StGUnit.g
index 1701214..e645f62 100644
--- a/gunit/src/main/antlr3/org/antlr/gunit/swingui/parsers/StGUnit.g
+++ b/gunit/src/main/antlr3/org/antlr/gunit/swingui/parsers/StGUnit.g
@@ -76,17 +76,17 @@ test
;
expect returns [ITestCaseOutput out]
- : OK {$out = adapter.createBoolOutput(true);}
- | FAIL {$out = adapter.createBoolOutput(false);}
- | 'returns' RETVAL {$out = adapter.createReturnOutput($RETVAL.text);}
- | '->' output {$out = adapter.createStdOutput($output.text);}
- | '->' AST {$out = adapter.createAstOutput($AST.text);}
+ : OK {$out = TestSuiteAdapter.createBoolOutput(true);}
+ | FAIL {$out = TestSuiteAdapter.createBoolOutput(false);}
+ | 'returns' RETVAL {$out = TestSuiteAdapter.createReturnOutput($RETVAL.text);}
+ | '->' output {$out = TestSuiteAdapter.createStdOutput($output.text);}
+ | '->' AST {$out = TestSuiteAdapter.createAstOutput($AST.text);}
;
input returns [ITestCaseInput in]
- : STRING {$in = adapter.createStringInput($STRING.text);}
- | ML_STRING {$in = adapter.createMultiInput($ML_STRING.text);}
- | fileInput {$in = adapter.createFileInput($fileInput.path);}
+ : STRING {$in = TestSuiteAdapter.createStringInput($STRING.text);}
+ | ML_STRING {$in = TestSuiteAdapter.createMultiInput($ML_STRING.text);}
+ | fileInput {$in = TestSuiteAdapter.createFileInput($fileInput.path);}
;
output
diff --git a/gunit/src/main/java/org/antlr/gunit/AbstractTest.java b/gunit/src/main/java/org/antlr/gunit/AbstractTest.java
index 158bf04..9d2c8ec 100644
--- a/gunit/src/main/java/org/antlr/gunit/AbstractTest.java
+++ b/gunit/src/main/java/org/antlr/gunit/AbstractTest.java
@@ -63,7 +63,7 @@ public abstract class AbstractTest implements ITestCase {
public String getTestedRuleName() { return this.testedRuleName; }
public int getTestCaseIndex() { return this.testCaseIndex; }
- public void setHeader(String rule, String lexicalRule, String treeRule, int numOfTest, int line) {
+ public void setHeader(String rule, String lexicalRule, String treeRule, int numOfTest, int line, String input) {
StringBuffer buf = new StringBuffer();
buf.append("test" + numOfTest + " (");
if ( treeRule!=null ) {
@@ -73,6 +73,9 @@ public abstract class AbstractTest implements ITestCase {
buf.append(lexicalRule + ", line"+line+")" + " - ");
}
else buf.append(rule + ", line"+line+")" + " - ");
+ buf.append( "\"" );
+ buf.append( input );
+ buf.append( "\"" );
this.header = buf.toString();
}
public void setActual(String actual) { this.actual = actual; }
diff --git a/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java b/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java
index 4713cf7..ed5282e 100644
--- a/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java
+++ b/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java
@@ -73,7 +73,7 @@ public class JUnitCodeGen {
this.testsuiteDir = testsuiteDir;
/** Map the name of rules having return value to its return type */
ruleWithReturn = new HashMap<String, String>();
- Class parserClass = locateParserClass( grammarInfo, classLoader );
+ Class<?> parserClass = locateParserClass( grammarInfo, classLoader );
Method[] methods = parserClass.getDeclaredMethods();
for(Method method : methods) {
if ( !method.getReturnType().getName().equals("void") ) {
@@ -82,7 +82,7 @@ public class JUnitCodeGen {
}
}
- private Class locateParserClass(GrammarInfo grammarInfo, ClassLoader classLoader) throws ClassNotFoundException {
+ private Class<?> locateParserClass(GrammarInfo grammarInfo, ClassLoader classLoader) throws ClassNotFoundException {
String parserClassName = grammarInfo.getGrammarName() + "Parser";
if ( grammarInfo.getGrammarPackage() != null ) {
parserClassName = grammarInfo.getGrammarPackage()+ "." + parserClassName;
@@ -189,13 +189,14 @@ public class JUnitCodeGen {
private void genParserMethods(StringTemplateGroup group, StringBuffer buf) {
for ( gUnitTestSuite ts: grammarInfo.getRuleTestSuites() ) {
int i = 0;
- for ( gUnitTestInput input: ts.testSuites.keySet() ) { // each rule may contain multiple tests
+ for ( Map.Entry<gUnitTestInput, AbstractTest> entry : ts.testSuites.entrySet() ) { // each rule may contain multiple tests
+ gUnitTestInput input = entry.getKey();
i++;
StringTemplate testRuleMethodST;
/** If rule has multiple return values or ast*/
- if ( ts.testSuites.get(input).getType()== gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getRuleName()) ) {
+ if ( entry.getValue().getType()== gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getRuleName()) ) {
testRuleMethodST = group.getInstanceOf("testRuleMethod2");
- String outputString = ts.testSuites.get(input).getText();
+ String outputString = entry.getValue().getText();
testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getRuleName())+i);
testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"');
testRuleMethodST.setAttribute("test", input);
@@ -208,21 +209,21 @@ public class JUnitCodeGen {
if ( ts.isLexicalRule() ) testRuleName = ts.getLexicalRuleName();
else testRuleName = ts.getRuleName();
testRuleMethodST = group.getInstanceOf("testRuleMethod");
- String outputString = ts.testSuites.get(input).getText();
+ String outputString = entry.getValue().getText();
testRuleMethodST.setAttribute("isLexicalRule", ts.isLexicalRule());
testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(testRuleName)+i);
testRuleMethodST.setAttribute("testRuleName", '"'+testRuleName+'"');
testRuleMethodST.setAttribute("test", input);
- testRuleMethodST.setAttribute("tokenType", getTypeString(ts.testSuites.get(input).getType()));
+ testRuleMethodST.setAttribute("tokenType", getTypeString(entry.getValue().getType()));
// normalize whitespace
outputString = normalizeTreeSpec(outputString);
- if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) { // trim ';' at the end of ACTION if there is...
+ if ( entry.getValue().getType()==gUnitParser.ACTION ) { // trim ';' at the end of ACTION if there is...
//testRuleMethodST.setAttribute("expecting", outputString.substring(0, outputString.length()-1));
testRuleMethodST.setAttribute("expecting", outputString);
}
- else if ( ts.testSuites.get(input).getType()==gUnitParser.RETVAL ) { // Expected: RETVAL
+ else if ( entry.getValue().getType()==gUnitParser.RETVAL ) { // Expected: RETVAL
testRuleMethodST.setAttribute("expecting", outputString);
}
else { // Attach "" to expected STRING or AST
@@ -239,13 +240,14 @@ public class JUnitCodeGen {
private void genTreeMethods(StringTemplateGroup group, StringBuffer buf) {
for ( gUnitTestSuite ts: grammarInfo.getRuleTestSuites() ) {
int i = 0;
- for ( gUnitTestInput input: ts.testSuites.keySet() ) { // each rule may contain multiple tests
+ for ( Map.Entry<gUnitTestInput, AbstractTest> entry : ts.testSuites.entrySet() ) { // each rule may contain multiple tests
+ gUnitTestInput input = entry.getKey();
i++;
StringTemplate testRuleMethodST;
/** If rule has multiple return values or ast*/
- if ( ts.testSuites.get(input).getType()== gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getTreeRuleName()) ) {
+ if ( entry.getValue().getType()== gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getTreeRuleName()) ) {
testRuleMethodST = group.getInstanceOf("testTreeRuleMethod2");
- String outputString = ts.testSuites.get(input).getText();
+ String outputString = entry.getValue().getText();
testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getTreeRuleName())+"_walks_"+
changeFirstCapital(ts.getRuleName())+i);
testRuleMethodST.setAttribute("testTreeRuleName", '"'+ts.getTreeRuleName()+'"');
@@ -256,19 +258,19 @@ public class JUnitCodeGen {
}
else {
testRuleMethodST = group.getInstanceOf("testTreeRuleMethod");
- String outputString = ts.testSuites.get(input).getText();
+ String outputString = entry.getValue().getText();
testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getTreeRuleName())+"_walks_"+
changeFirstCapital(ts.getRuleName())+i);
testRuleMethodST.setAttribute("testTreeRuleName", '"'+ts.getTreeRuleName()+'"');
testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"');
testRuleMethodST.setAttribute("test", input);
- testRuleMethodST.setAttribute("tokenType", getTypeString(ts.testSuites.get(input).getType()));
+ testRuleMethodST.setAttribute("tokenType", getTypeString(entry.getValue().getType()));
- if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) { // trim ';' at the end of ACTION if there is...
+ if ( entry.getValue().getType()==gUnitParser.ACTION ) { // trim ';' at the end of ACTION if there is...
//testRuleMethodST.setAttribute("expecting", outputString.substring(0, outputString.length()-1));
testRuleMethodST.setAttribute("expecting", outputString);
}
- else if ( ts.testSuites.get(input).getType()==gUnitParser.RETVAL ) { // Expected: RETVAL
+ else if ( entry.getValue().getType()==gUnitParser.RETVAL ) { // Expected: RETVAL
testRuleMethodST.setAttribute("expecting", outputString);
}
else { // Attach "" to expected STRING or AST
diff --git a/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java b/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java
index bf089c3..2b3b085 100644
--- a/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java
+++ b/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java
@@ -29,37 +29,30 @@ package org.antlr.gunit;
import junit.framework.TestCase;
import org.antlr.runtime.*;
-import org.antlr.runtime.tree.CommonTree;
-import org.antlr.runtime.tree.CommonTreeNodeStream;
-import org.antlr.runtime.tree.TreeAdaptor;
-import org.antlr.runtime.tree.TreeNodeStream;
+import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.StringTemplate;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.PrintStream;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
+import java.io.*;
+import java.lang.reflect.*;
-/** All gUnit-generated JUnit class should extend this class
+/** All gUnit-generated JUnit class should extend this class
* which implements the essential methods for triggering
* ANTLR parser/tree walker
*/
public abstract class gUnitBaseTest extends TestCase {
-
+
public String treeAdaptorPath;
public String packagePath;
public String lexerPath;
public String parserPath;
public String treeParserPath;
-
+
protected String stdout;
protected String stderr;
-
+
private PrintStream console = System.out;
private PrintStream consoleErr = System.err;
-
+
// Invoke target lexer.rule
public String execLexer(String testRuleName, int line, String testInput, boolean isFile) throws Exception {
CharStream input;
@@ -77,20 +70,18 @@ public abstract class gUnitBaseTest extends TestCase {
else {
input = new ANTLRStringStream(testInput);
}
- Class lexer = null;
+ Class<? extends Lexer> lexer;
PrintStream ps = null; // for redirecting stdout later
PrintStream ps2 = null; // for redirecting stderr later
try {
/** Use Reflection to create instances of lexer and parser */
- lexer = Class.forName(lexerPath);
- Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args
- Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
- Object[] lexArgs = new Object[]{input}; // assign value to lexer's args
- Lexer lexObj = (Lexer)lexConstructor.newInstance(lexArgs); // makes new instance of lexer
+ lexer = Class.forName(lexerPath).asSubclass(Lexer.class);
+ Constructor<? extends Lexer> lexConstructor = lexer.getConstructor(CharStream.class);
+ Lexer lexObj = lexConstructor.newInstance(input); // makes new instance of lexer
input.setLine(line);
- Method ruleName = lexer.getMethod("m"+testRuleName, new Class[0]);
-
+ Method ruleName = lexer.getMethod("m"+testRuleName);
+
/** Start of I/O Redirecting */
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
@@ -102,15 +93,15 @@ public abstract class gUnitBaseTest extends TestCase {
/** Invoke lexer rule, and get the current index in CharStream */
ruleName.invoke(lexObj, new Object[0]);
- Method ruleName2 = lexer.getMethod("getCharIndex", new Class[0]);
+ Method ruleName2 = lexer.getMethod("getCharIndex");
int currentIndex = (Integer) ruleName2.invoke(lexObj, new Object[0]);
if ( currentIndex!=input.size() ) {
ps2.println("extra text found, '"+input.substring(currentIndex, input.size()-1)+"'");
}
-
+
this.stdout = null;
this.stderr = null;
-
+
if ( err.toString().length()>0 ) {
this.stderr = err.toString();
return this.stderr;
@@ -122,17 +113,17 @@ public abstract class gUnitBaseTest extends TestCase {
return null;
}
} catch (ClassNotFoundException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (SecurityException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (NoSuchMethodException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (IllegalArgumentException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (InstantiationException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (IllegalAccessException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (InvocationTargetException e) { // This exception could be caused from ANTLR Runtime Exception, e.g. MismatchedTokenException
if ( e.getCause()!=null ) this.stderr = e.getCause().toString();
else this.stderr = e.toString();
@@ -149,9 +140,9 @@ public abstract class gUnitBaseTest extends TestCase {
}
return this.stdout;
}
-
+
// Invoke target parser.rule
-
+
public Object execParser(String testRuleName, int line, String testInput, boolean isFile) throws Exception {
CharStream input;
/** Set up ANTLR input stream based on input source, file or String */
@@ -168,33 +159,28 @@ public abstract class gUnitBaseTest extends TestCase {
else {
input = new ANTLRStringStream(testInput);
}
- Class lexer = null;
- Class parser = null;
+ Class<? extends Lexer> lexer;
+ Class<? extends Parser> parser;
PrintStream ps = null; // for redirecting stdout later
PrintStream ps2 = null; // for redirecting stderr later
ByteArrayOutputStream out = null;
ByteArrayOutputStream err = null;
try {
/** Use Reflection to create instances of lexer and parser */
- lexer = Class.forName(lexerPath);
- Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args
- Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
- Object[] lexArgs = new Object[]{input}; // assign value to lexer's args
- Lexer lexObj = (Lexer)lexConstructor.newInstance(lexArgs); // makes new instance of lexer
+ lexer = Class.forName(lexerPath).asSubclass(Lexer.class);
+ Constructor<? extends Lexer> lexConstructor = lexer.getConstructor(CharStream.class);
+ Lexer lexObj = lexConstructor.newInstance(input); // makes new instance of lexer
input.setLine(line);
CommonTokenStream tokens = new CommonTokenStream(lexObj);
- parser = Class.forName(parserPath);
- Class[] parArgTypes = new Class[]{TokenStream.class}; // assign type to parser's args
- Constructor parConstructor = parser.getConstructor(parArgTypes);
- Object[] parArgs = new Object[]{tokens}; // assign value to parser's args
- Parser parObj = (Parser)parConstructor.newInstance(parArgs); // makes new instance of parser
-
+ parser = Class.forName(parserPath).asSubclass(Parser.class);
+ Constructor<? extends Parser> parConstructor = parser.getConstructor(TokenStream.class);
+ Parser parObj = parConstructor.newInstance(tokens); // makes new instance of parser
+
// set up customized tree adaptor if necessary
if ( treeAdaptorPath!=null ) {
- parArgTypes = new Class[]{TreeAdaptor.class};
- Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", parArgTypes);
- Class _treeAdaptor = Class.forName(treeAdaptorPath);
+ Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", TreeAdaptor.class);
+ Class<? extends TreeAdaptor> _treeAdaptor = Class.forName(treeAdaptorPath).asSubclass(TreeAdaptor.class);
_setTreeAdaptor.invoke(parObj, _treeAdaptor.newInstance());
}
@@ -217,7 +203,7 @@ public abstract class gUnitBaseTest extends TestCase {
if ( ruleReturn!=null ) {
if ( ruleReturn.getClass().toString().indexOf(testRuleName+"_return")>0 ) {
try { // NullPointerException may happen here...
- Class _return = Class.forName(parserPath+"$"+testRuleName+"_return");
+ Class<?> _return = Class.forName(parserPath+"$"+testRuleName+"_return");
Method[] methods = _return.getDeclaredMethods();
for(Method method : methods) {
if ( method.getName().equals("getTree") ) {
@@ -240,13 +226,13 @@ public abstract class gUnitBaseTest extends TestCase {
this.stdout = "";
this.stderr = "";
-
+
/** Invalid input */
if ( tokens.index()!=tokens.size()-1 ) {
//throw new InvalidInputException();
this.stderr += "Stopped parsing at token index "+tokens.index()+": ";
}
-
+
// retVal could be actual return object from rule, stderr or stdout
this.stdout += out.toString();
this.stderr += err.toString();
@@ -267,16 +253,16 @@ public abstract class gUnitBaseTest extends TestCase {
}
}
catch (ClassNotFoundException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
}
catch (SecurityException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
}
catch (NoSuchMethodException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
}
catch (IllegalAccessException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
}
catch (InvocationTargetException e) {
this.stdout = out.toString();
@@ -297,7 +283,7 @@ public abstract class gUnitBaseTest extends TestCase {
}
return this.stdout;
}
-
+
// Invoke target parser.rule
public Object execTreeParser(String testTreeRuleName, String testRuleName, String testInput, boolean isFile) throws Exception {
CharStream input;
@@ -314,37 +300,32 @@ public abstract class gUnitBaseTest extends TestCase {
else {
input = new ANTLRStringStream(testInput);
}
- Class lexer = null;
- Class parser = null;
- Class treeParser = null;
+ Class<? extends Lexer> lexer;
+ Class<? extends Parser> parser;
+ Class<? extends TreeParser> treeParser;
PrintStream ps = null; // for redirecting stdout later
PrintStream ps2 = null; // for redirecting stderr later
try {
/** Use Reflection to create instances of lexer and parser */
- lexer = Class.forName(lexerPath);
- Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args
- Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
- Object[] lexArgs = new Object[]{input}; // assign value to lexer's args
- Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer
-
- CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj);
-
- parser = Class.forName(parserPath);
- Class[] parArgTypes = new Class[]{TokenStream.class}; // assign type to parser's args
- Constructor parConstructor = parser.getConstructor(parArgTypes);
- Object[] parArgs = new Object[]{tokens}; // assign value to parser's args
- Object parObj = parConstructor.newInstance(parArgs); // makes new instance of parser
-
+ lexer = Class.forName(lexerPath).asSubclass(Lexer.class);
+ Constructor<? extends Lexer> lexConstructor = lexer.getConstructor(CharStream.class);
+ Lexer lexObj = lexConstructor.newInstance(input); // makes new instance of lexer
+
+ CommonTokenStream tokens = new CommonTokenStream(lexObj);
+
+ parser = Class.forName(parserPath).asSubclass(Parser.class);
+ Constructor<? extends Parser> parConstructor = parser.getConstructor(TokenStream.class);
+ Parser parObj = parConstructor.newInstance(tokens); // makes new instance of parser
+
// set up customized tree adaptor if necessary
- TreeAdaptor customTreeAdaptor = null;
+ TreeAdaptor customTreeAdaptor = null;
if ( treeAdaptorPath!=null ) {
- parArgTypes = new Class[]{TreeAdaptor.class};
- Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", parArgTypes);
- Class _treeAdaptor = Class.forName(treeAdaptorPath);
- customTreeAdaptor = (TreeAdaptor) _treeAdaptor.newInstance();
+ Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", TreeAdaptor.class);
+ Class<? extends TreeAdaptor> _treeAdaptor = Class.forName(treeAdaptorPath).asSubclass(TreeAdaptor.class);
+ customTreeAdaptor = _treeAdaptor.newInstance();
_setTreeAdaptor.invoke(parObj, customTreeAdaptor);
}
-
+
Method ruleName = parser.getMethod(testRuleName);
/** Start of I/O Redirecting */
@@ -358,8 +339,8 @@ public abstract class gUnitBaseTest extends TestCase {
/** Invoke grammar rule, and get the return value */
Object ruleReturn = ruleName.invoke(parObj);
-
- Class _return = Class.forName(parserPath+"$"+testRuleName+"_return");
+
+ Class<?> _return = Class.forName(parserPath+"$"+testRuleName+"_return");
Method returnName = _return.getMethod("getTree");
CommonTree tree = (CommonTree) returnName.invoke(ruleReturn);
@@ -374,22 +355,20 @@ public abstract class gUnitBaseTest extends TestCase {
// AST nodes have payload that point into token stream
nodes.setTokenStream(tokens);
// Create a tree walker attached to the nodes stream
- treeParser = Class.forName(treeParserPath);
- Class[] treeParArgTypes = new Class[]{TreeNodeStream.class}; // assign type to tree parser's args
- Constructor treeParConstructor = treeParser.getConstructor(treeParArgTypes);
- Object[] treeParArgs = new Object[]{nodes}; // assign value to tree parser's args
- Object treeParObj = treeParConstructor.newInstance(treeParArgs); // makes new instance of tree parser
+ treeParser = Class.forName(treeParserPath).asSubclass(TreeParser.class);
+ Constructor<? extends TreeParser> treeParConstructor = treeParser.getConstructor(TreeNodeStream.class);
+ TreeParser treeParObj = treeParConstructor.newInstance(nodes); // makes new instance of tree parser
// Invoke the tree rule, and store the return value if there is
Method treeRuleName = treeParser.getMethod(testTreeRuleName);
Object treeRuleReturn = treeRuleName.invoke(treeParObj);
-
+
String astString = null;
String stString = null;
/** If tree rule has return value, determine if it contains an AST or a ST */
if ( treeRuleReturn!=null ) {
if ( treeRuleReturn.getClass().toString().indexOf(testTreeRuleName+"_return")>0 ) {
try { // NullPointerException may happen here...
- Class _treeReturn = Class.forName(treeParserPath+"$"+testTreeRuleName+"_return");
+ Class<?> _treeReturn = Class.forName(treeParserPath+"$"+testTreeRuleName+"_return");
Method[] methods = _treeReturn.getDeclaredMethods();
for(Method method : methods) {
if ( method.getName().equals("getTree") ) {
@@ -412,12 +391,12 @@ public abstract class gUnitBaseTest extends TestCase {
this.stdout = null;
this.stderr = null;
-
+
/** Invalid input */
if ( tokens.index()!=tokens.size()-1 ) {
throw new InvalidInputException();
}
-
+
// retVal could be actual return object from rule, stderr or stdout
if ( err.toString().length()>0 ) {
this.stderr = err.toString();
@@ -439,13 +418,13 @@ public abstract class gUnitBaseTest extends TestCase {
return null;
}
} catch (ClassNotFoundException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (SecurityException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (NoSuchMethodException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (IllegalAccessException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException(e);
} catch (InvocationTargetException e) {
if ( e.getCause()!=null ) this.stderr = e.getCause().toString();
else this.stderr = e.toString();
@@ -462,11 +441,13 @@ public abstract class gUnitBaseTest extends TestCase {
}
return stdout;
}
-
+
// Modify the return value if the expected token type is OK or FAIL
- public Object examineExecResult(int tokenType, Object retVal) {
+ public Object examineExecResult(int tokenType, Object retVal) {
+ System.out.println("expect "+(tokenType==gUnitParser.OK?"OK":"FAIL")+
+ "stderr=="+stderr);
if ( tokenType==gUnitParser.OK ) { // expected Token: OK
- if ( this.stderr==null ) {
+ if ( this.stderr==null || this.stderr.length()==0 ) {
return "OK";
}
else {
@@ -474,7 +455,7 @@ public abstract class gUnitBaseTest extends TestCase {
}
}
else if ( tokenType==gUnitParser.FAIL ) { // expected Token: FAIL
- if ( this.stderr!=null ) {
+ if ( this.stderr!=null && this.stderr.length()>0 ) {
return "FAIL";
}
else {
@@ -483,7 +464,12 @@ public abstract class gUnitBaseTest extends TestCase {
}
else { // return the same object for the other token types
return retVal;
- }
+ }
}
-
+
+ protected void handleUnexpectedException(Exception e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
}
diff --git a/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java b/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java
index 8498c65..ab759c8 100644
--- a/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java
+++ b/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java
@@ -47,6 +47,8 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
+import org.antlr.runtime.tree.TreeParser;
public class gUnitExecutor implements ITestSuite {
public GrammarInfo grammarInfo;
@@ -103,7 +105,7 @@ public class gUnitExecutor implements ITestSuite {
return grammarClassLoader;
}
- protected final Class classForName(String name) throws ClassNotFoundException {
+ protected final Class<?> classForName(String name) throws ClassNotFoundException {
return getGrammarClassLoader().loadClass( name );
}
@@ -146,8 +148,7 @@ public class gUnitExecutor implements ITestSuite {
}
}
catch (Exception e) {
- e.printStackTrace();
- System.exit(1);
+ handleUnexpectedException(e);
}
return testResultST.toString();
}
@@ -173,11 +174,12 @@ public class gUnitExecutor implements ITestSuite {
String rule = ts.getRuleName();
String lexicalRule = ts.getLexicalRuleName();
String treeRule = ts.getTreeRuleName();
- for ( gUnitTestInput input: ts.testSuites.keySet() ) { // each rule may contain multiple tests
+ for ( Map.Entry<gUnitTestInput, AbstractTest> entry : ts.testSuites.entrySet() ) { // each rule may contain multiple tests
+ gUnitTestInput input = entry.getKey();
numOfTest++;
// Run parser, and get the return value or stdout or stderr if there is
gUnitTestResult result = null;
- AbstractTest test = ts.testSuites.get(input);
+ AbstractTest test = entry.getValue();
try {
// TODO: create a -debug option to turn on logging, which shows progress of running tests
//System.out.print(numOfTest + ". Running rule: " + rule + "; input: '" + input.testInput + "'");
@@ -186,7 +188,7 @@ public class gUnitExecutor implements ITestSuite {
//System.out.println("; Expecting " + test.getExpected() + "; Success?: " + test.getExpected().equals(test.getResult(result)));
} catch ( InvalidInputException e) {
numOfInvalidInput++;
- test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line);
+ test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line, input.input);
test.setActual(input.input);
invalids.add(test);
continue;
@@ -198,7 +200,7 @@ public class gUnitExecutor implements ITestSuite {
if (actual == null) {
numOfFailure++;
- test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line);
+ test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line, input.input);
test.setActual("null");
failures.add(test);
onFail(test);
@@ -211,14 +213,14 @@ public class gUnitExecutor implements ITestSuite {
// TODO: something with ACTIONS - at least create action test type and throw exception.
else if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) { // expected Token: ACTION
numOfFailure++;
- test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line);
+ test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line, input.input);
test.setActual("\t"+"{ACTION} is not supported in the grammarInfo yet...");
failures.add(test);
onFail(test);
}
else {
numOfFailure++;
- test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line);
+ test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line, input.input);
failures.add(test);
onFail(test);
}
@@ -229,7 +231,7 @@ public class gUnitExecutor implements ITestSuite {
// TODO: throw proper exceptions
protected gUnitTestResult runLexer(String lexerName, String testRuleName, gUnitTestInput testInput) throws Exception {
CharStream input;
- Class lexer = null;
+ Class<? extends Lexer> lexer;
PrintStream ps = null; // for redirecting stdout later
PrintStream ps2 = null; // for redirecting stderr later
try {
@@ -237,13 +239,11 @@ public class gUnitExecutor implements ITestSuite {
input = getANTLRInputStream(testInput);
/** Use Reflection to create instances of lexer and parser */
- lexer = classForName(lexerName);
- Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args
- Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
- Object[] lexArgs = new Object[]{input}; // assign value to lexer's args
- Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer
+ lexer = classForName(lexerName).asSubclass(Lexer.class);
+ Constructor<? extends Lexer> lexConstructor = lexer.getConstructor(CharStream.class);
+ Lexer lexObj = lexConstructor.newInstance(input); // makes new instance of lexer
- Method ruleName = lexer.getMethod("m"+testRuleName, new Class[0]);
+ Method ruleName = lexer.getMethod("m"+testRuleName);
/** Start of I/O Redirecting */
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -255,9 +255,9 @@ public class gUnitExecutor implements ITestSuite {
/** End of redirecting */
/** Invoke lexer rule, and get the current index in CharStream */
- ruleName.invoke(lexObj, new Object[0]);
- Method ruleName2 = lexer.getMethod("getCharIndex", new Class[0]);
- int currentIndex = (Integer) ruleName2.invoke(lexObj, new Object[0]);
+ ruleName.invoke(lexObj);
+ Method ruleName2 = lexer.getMethod("getCharIndex");
+ int currentIndex = (Integer) ruleName2.invoke(lexObj);
if ( currentIndex!=input.size() ) {
ps2.print("extra text found, '"+input.substring(currentIndex, input.size()-1)+"'");
}
@@ -275,17 +275,17 @@ public class gUnitExecutor implements ITestSuite {
} catch (IOException e) {
return getTestExceptionResult(e);
} catch (ClassNotFoundException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (SecurityException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (NoSuchMethodException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (IllegalArgumentException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (InstantiationException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (IllegalAccessException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (InvocationTargetException e) { // This exception could be caused from ANTLR Runtime Exception, e.g. MismatchedTokenException
return getTestExceptionResult(e);
} finally {
@@ -305,8 +305,8 @@ public class gUnitExecutor implements ITestSuite {
// TODO: throw proper exceptions
protected gUnitTestResult runParser(String parserName, String lexerName, String testRuleName, gUnitTestInput testInput) throws Exception {
CharStream input;
- Class lexer = null;
- Class parser = null;
+ Class<? extends Lexer> lexer;
+ Class<? extends Parser> parser;
PrintStream ps = null; // for redirecting stdout later
PrintStream ps2 = null; // for redirecting stderr later
try {
@@ -314,25 +314,20 @@ public class gUnitExecutor implements ITestSuite {
input = getANTLRInputStream(testInput);
/** Use Reflection to create instances of lexer and parser */
- lexer = classForName(lexerName);
- Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args
- Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
- Object[] lexArgs = new Object[]{input}; // assign value to lexer's args
- Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer
+ lexer = classForName(lexerName).asSubclass(Lexer.class);
+ Constructor<? extends Lexer> lexConstructor = lexer.getConstructor(CharStream.class);
+ Lexer lexObj = lexConstructor.newInstance(input); // makes new instance of lexer
- CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj);
+ CommonTokenStream tokens = new CommonTokenStream(lexObj);
- parser = classForName(parserName);
- Class[] parArgTypes = new Class[]{TokenStream.class}; // assign type to parser's args
- Constructor parConstructor = parser.getConstructor(parArgTypes);
- Object[] parArgs = new Object[]{tokens}; // assign value to parser's args
- Object parObj = parConstructor.newInstance(parArgs); // makes new instance of parser
+ parser = classForName(parserName).asSubclass(Parser.class);
+ Constructor<? extends Parser> parConstructor = parser.getConstructor(TokenStream.class);
+ Parser parObj = parConstructor.newInstance(tokens); // makes new instance of parser
// set up customized tree adaptor if necessary
if ( grammarInfo.getAdaptor()!=null ) {
- parArgTypes = new Class[]{TreeAdaptor.class};
- Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", parArgTypes);
- Class _treeAdaptor = classForName(grammarInfo.getAdaptor());
+ Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", TreeAdaptor.class);
+ Class<? extends TreeAdaptor> _treeAdaptor = classForName(grammarInfo.getAdaptor()).asSubclass(TreeAdaptor.class);
_setTreeAdaptor.invoke(parObj, _treeAdaptor.newInstance());
}
@@ -355,7 +350,7 @@ public class gUnitExecutor implements ITestSuite {
if ( ruleReturn!=null ) {
if ( ruleReturn.getClass().toString().indexOf(testRuleName+"_return")>0 ) {
try { // NullPointerException may happen here...
- Class _return = classForName(parserName+"$"+testRuleName+"_return");
+ Class<?> _return = classForName(parserName+"$"+testRuleName+"_return");
Method[] methods = _return.getDeclaredMethods();
for(Method method : methods) {
if ( method.getName().equals("getTree") ) {
@@ -376,11 +371,7 @@ public class gUnitExecutor implements ITestSuite {
}
}
- /** Invalid input */
- if ( tokens.index()!=tokens.size()-1 ) {
- //throw new InvalidInputException();
- ps2.print("Invalid input");
- }
+ checkForValidInput(tokens, ps2);
if ( err.toString().length()>0 ) {
gUnitTestResult testResult = new gUnitTestResult(false, err.toString());
@@ -407,17 +398,17 @@ public class gUnitExecutor implements ITestSuite {
} catch (IOException e) {
return getTestExceptionResult(e);
} catch (ClassNotFoundException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (SecurityException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (NoSuchMethodException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (IllegalArgumentException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (InstantiationException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (IllegalAccessException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (InvocationTargetException e) { // This exception could be caused from ANTLR Runtime Exception, e.g. MismatchedTokenException
return getTestExceptionResult(e);
} finally {
@@ -437,9 +428,9 @@ public class gUnitExecutor implements ITestSuite {
protected gUnitTestResult runTreeParser(String parserName, String lexerName, String testRuleName, String testTreeRuleName, gUnitTestInput testInput) throws Exception {
CharStream input;
String treeParserPath;
- Class lexer = null;
- Class parser = null;
- Class treeParser = null;
+ Class<? extends Lexer> lexer;
+ Class<? extends Parser> parser;
+ Class<? extends TreeParser> treeParser;
PrintStream ps = null; // for redirecting stdout later
PrintStream ps2 = null; // for redirecting stderr later
try {
@@ -455,27 +446,22 @@ public class gUnitExecutor implements ITestSuite {
}
/** Use Reflection to create instances of lexer and parser */
- lexer = classForName(lexerName);
- Class[] lexArgTypes = new Class[]{CharStream.class}; // assign type to lexer's args
- Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
- Object[] lexArgs = new Object[]{input}; // assign value to lexer's args
- Object lexObj = lexConstructor.newInstance(lexArgs); // makes new instance of lexer
+ lexer = classForName(lexerName).asSubclass(Lexer.class);
+ Constructor<? extends Lexer> lexConstructor = lexer.getConstructor(CharStream.class);
+ Lexer lexObj = lexConstructor.newInstance(input); // makes new instance of lexer
- CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj);
+ CommonTokenStream tokens = new CommonTokenStream(lexObj);
- parser = classForName(parserName);
- Class[] parArgTypes = new Class[]{TokenStream.class}; // assign type to parser's args
- Constructor parConstructor = parser.getConstructor(parArgTypes);
- Object[] parArgs = new Object[]{tokens}; // assign value to parser's args
- Object parObj = parConstructor.newInstance(parArgs); // makes new instance of parser
+ parser = classForName(parserName).asSubclass(Parser.class);
+ Constructor<? extends Parser> parConstructor = parser.getConstructor(TokenStream.class);
+ Parser parObj = parConstructor.newInstance(tokens); // makes new instance of parser
// set up customized tree adaptor if necessary
TreeAdaptor customTreeAdaptor = null;
if ( grammarInfo.getAdaptor()!=null ) {
- parArgTypes = new Class[]{TreeAdaptor.class};
- Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", parArgTypes);
- Class _treeAdaptor = classForName(grammarInfo.getAdaptor());
- customTreeAdaptor = (TreeAdaptor) _treeAdaptor.newInstance();
+ Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", TreeAdaptor.class);
+ Class<? extends TreeAdaptor> _treeAdaptor = classForName(grammarInfo.getAdaptor()).asSubclass(TreeAdaptor.class);
+ customTreeAdaptor = _treeAdaptor.newInstance();
_setTreeAdaptor.invoke(parObj, customTreeAdaptor);
}
@@ -493,7 +479,7 @@ public class gUnitExecutor implements ITestSuite {
/** Invoke grammar rule, and get the return value */
Object ruleReturn = ruleName.invoke(parObj);
- Class _return = classForName(parserName+"$"+testRuleName+"_return");
+ Class<?> _return = classForName(parserName+"$"+testRuleName+"_return");
Method returnName = _return.getMethod("getTree");
CommonTree tree = (CommonTree) returnName.invoke(ruleReturn);
@@ -508,11 +494,9 @@ public class gUnitExecutor implements ITestSuite {
// AST nodes have payload that point into token stream
nodes.setTokenStream(tokens);
// Create a tree walker attached to the nodes stream
- treeParser = classForName(treeParserPath);
- Class[] treeParArgTypes = new Class[]{TreeNodeStream.class}; // assign type to tree parser's args
- Constructor treeParConstructor = treeParser.getConstructor(treeParArgTypes);
- Object[] treeParArgs = new Object[]{nodes}; // assign value to tree parser's args
- Object treeParObj = treeParConstructor.newInstance(treeParArgs); // makes new instance of tree parser
+ treeParser = classForName(treeParserPath).asSubclass(TreeParser.class);
+ Constructor<? extends TreeParser> treeParConstructor = treeParser.getConstructor(TreeNodeStream.class);
+ TreeParser treeParObj = treeParConstructor.newInstance(nodes); // makes new instance of tree parser
// Invoke the tree rule, and store the return value if there is
Method treeRuleName = treeParser.getMethod(testTreeRuleName);
Object treeRuleReturn = treeRuleName.invoke(treeParObj);
@@ -523,7 +507,7 @@ public class gUnitExecutor implements ITestSuite {
if ( treeRuleReturn!=null ) {
if ( treeRuleReturn.getClass().toString().indexOf(testTreeRuleName+"_return")>0 ) {
try { // NullPointerException may happen here...
- Class _treeReturn = classForName(treeParserPath+"$"+testTreeRuleName+"_return");
+ Class<?> _treeReturn = classForName(treeParserPath+"$"+testTreeRuleName+"_return");
Method[] methods = _treeReturn.getDeclaredMethods();
for(Method method : methods) {
if ( method.getName().equals("getTree") ) {
@@ -544,11 +528,7 @@ public class gUnitExecutor implements ITestSuite {
}
}
- /** Invalid input */
- if ( tokens.index()!=tokens.size()-1 ) {
- //throw new InvalidInputException();
- ps2.print("Invalid input");
- }
+ checkForValidInput( tokens, ps2 );
if ( err.toString().length()>0 ) {
gUnitTestResult testResult = new gUnitTestResult(false, err.toString());
@@ -576,17 +556,17 @@ public class gUnitExecutor implements ITestSuite {
} catch (IOException e) {
return getTestExceptionResult(e);
} catch (ClassNotFoundException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (SecurityException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (NoSuchMethodException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (IllegalArgumentException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (InstantiationException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (IllegalAccessException e) {
- e.printStackTrace(); System.exit(1);
+ handleUnexpectedException( e );
} catch (InvocationTargetException e) { // note: This exception could be caused from ANTLR Runtime Exception...
return getTestExceptionResult(e);
} finally {
@@ -641,6 +621,23 @@ public class gUnitExecutor implements ITestSuite {
return testResult;
}
+ /**
+ * Verify the input has been properly consumed
+ */
+ protected void checkForValidInput(CommonTokenStream tokens, PrintStream ps2) {
+ if ( tokens.index() != tokens.size() - 1 ) {
+ //At this point we need to check for redundant EOF tokens
+ //which might have been added by the Parser:
+ List<? extends Token> endingTokens = tokens.getTokens(tokens.index(), tokens.size() -1);
+ for (Token endToken : endingTokens) {
+ if (! "<EOF>".equals(endToken.getText())) {
+ //writing to ps2 will mark the test as failed:
+ ps2.print( "Invalid input" );
+ return;
+ }
+ }
+ }
+ }
public void onPass(ITestCase passTest) {
@@ -650,4 +647,9 @@ public class gUnitExecutor implements ITestSuite {
}
+ protected void handleUnexpectedException(Exception e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
}
diff --git a/gunit/src/main/java/org/antlr/gunit/swingui/TestCaseEditController.java b/gunit/src/main/java/org/antlr/gunit/swingui/TestCaseEditController.java
index 30cf0ae..2f15229 100644
--- a/gunit/src/main/java/org/antlr/gunit/swingui/TestCaseEditController.java
+++ b/gunit/src/main/java/org/antlr/gunit/swingui/TestCaseEditController.java
@@ -96,9 +96,9 @@ public class TestCaseEditController implements IController {
/* END OF MODEL*/
- private static final HashMap<Class, String> TypeNameTable;
+ private static final HashMap<Class<?>, String> TypeNameTable;
static {
- TypeNameTable = new HashMap<Class, String> ();
+ TypeNameTable = new HashMap<Class<?>, String> ();
TypeNameTable.put(TestCaseInputString.class, IN_TYPE_STRING);
TypeNameTable.put(TestCaseInputMultiString.class, IN_TYPE_MULTI);
TypeNameTable.put(TestCaseInputFile.class, IN_TYPE_FILE);
diff --git a/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuite.java b/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuite.java
index 06e5227..1264f30 100644
--- a/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuite.java
+++ b/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuite.java
@@ -95,6 +95,6 @@ public class TestSuite {
}
// only for stringtemplate use
- public List getRulesForStringTemplate() {return rules;}
+ public List<Rule> getRulesForStringTemplate() {return rules;}
}
diff --git a/gunit/src/main/java/org/antlr/gunit/swingui/runner/ParserLoader.java b/gunit/src/main/java/org/antlr/gunit/swingui/runner/ParserLoader.java
index 23f5aa0..dcad501 100644
--- a/gunit/src/main/java/org/antlr/gunit/swingui/runner/ParserLoader.java
+++ b/gunit/src/main/java/org/antlr/gunit/swingui/runner/ParserLoader.java
@@ -31,12 +31,12 @@ import java.io.*;
import java.util.HashMap;
/**
- * Class loader for parser & lexer generated by antlr.
+ * Class loader for parser &amp; lexer generated by antlr.
* @author Shaoting
*/
public class ParserLoader extends ClassLoader {
- private HashMap<String, Class> classList;
+ private HashMap<String, Class<?>> classList;
private String grammar;
/**
@@ -51,7 +51,7 @@ public class ParserLoader extends ClassLoader {
// load all the class files in the "classDir" related to the grammarName
File dir = new File(classDir);
if(dir.isDirectory()) {
- classList = new HashMap<String, Class>();
+ classList = new HashMap<String, Class<?>>();
grammar = grammarName;
File[] files = dir.listFiles(new ClassFilenameFilter(grammarName));
for(File f : files) {
@@ -63,7 +63,7 @@ public class ParserLoader extends ClassLoader {
in.close();
// define class
- final Class newClass = defineClass(null, classData, 0, classData.length);
+ final Class<?> newClass = defineClass(null, classData, 0, classData.length);
assert(newClass != null);
resolveClass(newClass);
@@ -86,7 +86,7 @@ public class ParserLoader extends ClassLoader {
@Override
- public synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
+ public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
//System.out.print("loading: " + name);
if(name.startsWith(grammar)) {
if(classList.containsKey(name)) {
@@ -98,7 +98,7 @@ public class ParserLoader extends ClassLoader {
}
} else {
- final Class c = findSystemClass(name);
+ final Class<?> c = findSystemClass(name);
//System.out.println(" .... system found " + c.getName());
return c;
}