Use a TestWatcher to properly clean up after successful tests

This commit is contained in:
Sam Harwell 2012-12-15 18:19:40 -06:00
parent bca2589be1
commit 99e4438256
31 changed files with 84 additions and 35 deletions

View File

@ -29,7 +29,6 @@
*/
package org.antlr.v4.test;
import org.antlr.v4.Tool;
import org.antlr.v4.automata.ATNFactory;
import org.antlr.v4.automata.ATNPrinter;
@ -65,9 +64,6 @@ import org.antlr.v4.tool.DefaultToolListener;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.GrammarSemanticsMessage;
import org.antlr.v4.tool.LexerGrammar;
import org.antlr.v4.tool.Rule;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
@ -77,6 +73,10 @@ import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import org.antlr.v4.tool.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
@ -104,6 +104,7 @@ import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.junit.Assert.*;
public abstract class BaseTest {
// -J-Dorg.antlr.v4.test.BaseTest.level=FINE
@ -121,30 +122,30 @@ public abstract class BaseTest {
public String tmpdir = null;
/** reset during setUp and set to true if we find a problem */
protected boolean lastTestFailed = false;
/** If error during parser execution, store stderr here; can't return
* stdout and stderr. This doesn't trap errors from running antlr.
*/
protected String stderrDuringParse;
@org.junit.Rule
public final TestRule testWatcher = new TestWatcher() {
@Override
protected void succeeded(Description description) {
// remove tmpdir if no error.
eraseTempDir();
}
};
@Before
public void setUp() throws Exception {
lastTestFailed = false; // hope for the best, but set to true in asserts that fail
// new output dir for each test
tmpdir = new File(System.getProperty("java.io.tmpdir"),
getClass().getSimpleName()+"-"+System.currentTimeMillis()).getAbsolutePath();
// tmpdir = "/tmp";
}
@After
public void tearDown() throws Exception {
// remove tmpdir if no error.
if ( !lastTestFailed ) eraseTempDir();
}
protected org.antlr.v4.Tool newTool(String[] args) {
Tool tool = new Tool(args);
return tool;
@ -1089,24 +1090,15 @@ public abstract class BaseTest {
return elements.subList(Token.MIN_USER_TOKEN_TYPE, elements.size());
}
// override to track errors
public void assertNotNullOrEmpty(String message, String text) {
assertNotNull(message, text);
assertFalse(message, text.isEmpty());
}
public void assertEquals(String msg, Object expected, Object actual) { try {Assert.assertEquals(msg,expected,actual);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertEquals(Object expected, Object actual) { try {Assert.assertEquals(expected,actual);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertEquals(String msg, long expected, long actual) { try {Assert.assertEquals(msg,expected,actual);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertEquals(long expected, long actual) { try {Assert.assertEquals(expected,actual);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertTrue(String message, boolean condition) { try {Assert.assertTrue(message,condition);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertTrue(boolean condition) { try {Assert.assertTrue(condition);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertFalse(String message, boolean condition) { try {Assert.assertFalse(message,condition);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertFalse(boolean condition) { try {Assert.assertFalse(condition);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertNotNull(String message, Object object) { try {Assert.assertNotNull(message, object);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertNotNull(Object object) { try {Assert.assertNotNull(object);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertNull(String message, Object object) { try {Assert.assertNull(message, object);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertNull(Object object) { try {Assert.assertNull(object);} catch (Error e) {lastTestFailed=true; throw e;} }
public void assertNotNullOrEmpty(String text) {
assertNotNull(text);
assertFalse(text.isEmpty());
}
public static class IntTokenStream implements TokenStream {
IntegerList types;

View File

@ -36,6 +36,9 @@ import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.LexerGrammar;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestATNConstruction extends BaseTest {
@Test
public void testA() throws Exception {

View File

@ -38,6 +38,8 @@ import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.LexerGrammar;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestATNDeserialization extends BaseTest {
@Test public void testSimpleNoBlock() throws Exception {
Grammar g = new Grammar(

View File

@ -45,6 +45,8 @@ import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.interp.ParserInterpreter;
import org.junit.Test;
import static org.junit.Assert.*;
// NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH
// NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH
// NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH

View File

@ -41,6 +41,8 @@ import org.junit.Test;
import java.util.List;
import static org.junit.Assert.*;
/**
* Lexer rules are little quirky when it comes to wildcards. Problem
* stems from the fact that we want the longest match to win among

View File

@ -49,6 +49,8 @@ import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.interp.ParserInterpreter;
import org.junit.Test;
import static org.junit.Assert.*;
// NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH
// NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH
// NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH

View File

@ -37,6 +37,8 @@ import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.LexerGrammar;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestATNSerialization extends BaseTest {
@Test public void testSimpleNoBlock() throws Exception {
Grammar g = new Grammar(

View File

@ -39,6 +39,8 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class TestActionSplitter extends BaseTest {
static String[] exprs = {
"foo", "['foo'<" + ActionSplitter.TEXT + ">]",

View File

@ -40,6 +40,8 @@ import org.antlr.v4.tool.LexerGrammar;
import org.antlr.v4.tool.interp.LexerInterpreter;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestBufferedTokenStream extends BaseTest {
protected TokenStream createTokenStream(TokenSource src) {

View File

@ -41,6 +41,8 @@ import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.WritableToken;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestCommonTokenStream extends TestBufferedTokenStream {
@Override

View File

@ -35,6 +35,8 @@ import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.GrammarSemanticsMessage;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestCompositeGrammars extends BaseTest {
protected boolean debug = false;

View File

@ -32,6 +32,8 @@ package org.antlr.v4.test;
import org.junit.Test;
import static org.junit.Assert.*;
/*
cover these cases:
dead end

View File

@ -34,8 +34,7 @@ import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.misc.IntervalSet;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class TestIntervalSet extends BaseTest {

View File

@ -32,6 +32,8 @@ package org.antlr.v4.test;
import org.junit.Test;
import static org.junit.Assert.*;
/** */
public class TestLeftRecursion extends BaseTest {
protected boolean debug = false;

View File

@ -32,6 +32,8 @@ package org.antlr.v4.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestLexerErrors extends BaseTest {
// TEST DETECTION
@Test public void testInvalidCharAtStart() throws Exception {

View File

@ -37,6 +37,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static org.junit.Assert.*;
public class TestLexerExec extends BaseTest {
@Test public void testQuoteTranslation() throws Exception {
String grammar =

View File

@ -32,6 +32,8 @@ package org.antlr.v4.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestListeners extends BaseTest {
@Test public void testBasic() throws Exception {
String grammar =

View File

@ -33,6 +33,8 @@ package org.antlr.v4.test;
import org.antlr.v4.automata.ATNSerializer;
import org.junit.Test;
import static org.junit.Assert.*;
/** test runtime parse errors */
public class TestParseErrors extends BaseTest {
@Test public void testTokenMismatch() throws Exception {

View File

@ -32,6 +32,8 @@ package org.antlr.v4.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestParseTrees extends BaseTest {
@Test public void testTokenAndRuleContextString() throws Exception {
String grammar =

View File

@ -32,6 +32,8 @@ package org.antlr.v4.test;
import org.junit.Test;
import static org.junit.Assert.*;
/** Test parser execution.
*
* For the non-greedy stuff, the rule is that .* or any other non-greedy loop

View File

@ -94,6 +94,8 @@ import java.util.logging.Logger;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import static org.junit.Assert.*;
public class TestPerformance extends BaseTest {
/**
* Parse all java files under this package within the JDK_SOURCE_ROOT
@ -787,7 +789,6 @@ public class TestPerformance extends BaseTest {
};
} catch (Exception e) {
e.printStackTrace(System.out);
lastTestFailed = true;
Assert.fail(e.getMessage());
throw new IllegalStateException(e);
}

View File

@ -34,6 +34,8 @@ import org.antlr.v4.parse.ScopeParser;
import org.antlr.v4.tool.ErrorManager;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestScopeParsing extends BaseTest {
String[] argPairs = {
"", "{}",

View File

@ -32,6 +32,8 @@ package org.antlr.v4.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestSemPredEvalLexer extends BaseTest {
@Test public void testDisableRule() throws Exception {

View File

@ -32,6 +32,8 @@ package org.antlr.v4.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestSemPredEvalParser extends BaseTest {
// TEST VALIDATING PREDS

View File

@ -31,6 +31,8 @@ package org.antlr.v4.test;
import org.junit.Test;
import static org.junit.Assert.*;
/** Test the set stuff in lexer and parser */
public class TestSets extends BaseTest {
protected boolean debug = false;

View File

@ -33,6 +33,8 @@ package org.antlr.v4.test;
import org.antlr.v4.tool.LexerGrammar;
import org.junit.Test;
import static org.junit.Assert.*;
/** */
public class TestSymbolIssues extends BaseTest {
static String[] A = {

View File

@ -36,6 +36,8 @@ import org.antlr.v4.tool.LexerGrammar;
import org.antlr.v4.tool.interp.LexerInterpreter;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestTokenStreamRewriter extends BaseTest {
/** Public default constructor used by TestRig */

View File

@ -40,6 +40,8 @@ import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
import static org.junit.Assert.*;
public class TestTokenTypeAssignment extends BaseTest {
@Test

View File

@ -34,6 +34,8 @@ import org.junit.Test;
import java.util.List;
import static org.junit.Assert.*;
/** Test topo sort in GraphNode. */
public class TestTopologicalSort extends BaseTest {
@Test

View File

@ -43,6 +43,8 @@ import org.junit.Test;
import java.io.Reader;
import java.io.StringReader;
import static org.junit.Assert.*;
public class TestUnbufferedCharStream extends BaseTest {
@Test public void testNoChar() throws Exception {
CharStream input = createStream("");

View File

@ -45,6 +45,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
public class TestUnbufferedTokenStream extends BaseTest {
@Test public void testLookahead() throws Exception {
LexerGrammar g = new LexerGrammar(