-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sanitize test code base and factorize common code * fix failing tests * fix failing tests * fix failing tests
- Loading branch information
1 parent
62a0b02
commit e50ecf4
Showing
25 changed files
with
779 additions
and
4,508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package org.antlr.v4.test.runtime; | ||
|
||
import org.antlr.v4.Tool; | ||
import org.antlr.v4.automata.LexerATNFactory; | ||
import org.antlr.v4.automata.ParserATNFactory; | ||
import org.antlr.v4.runtime.atn.ATN; | ||
import org.antlr.v4.runtime.atn.ATNDeserializer; | ||
import org.antlr.v4.runtime.atn.ATNSerializer; | ||
import org.antlr.v4.semantics.SemanticPipeline; | ||
import org.antlr.v4.tool.Grammar; | ||
import org.antlr.v4.tool.LexerGrammar; | ||
import org.junit.rules.TestRule; | ||
import org.junit.rules.TestWatcher; | ||
import org.junit.runner.Description; | ||
|
||
import java.io.File; | ||
import java.util.Locale; | ||
import java.util.logging.Logger; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public abstract class BaseRuntimeTestSupport implements RuntimeTestSupport { | ||
|
||
// -J-Dorg.antlr.v4.test.BaseTest.level=FINE | ||
protected static final Logger logger = Logger.getLogger(BaseRuntimeTestSupport.class.getName()); | ||
|
||
public static final String NEW_LINE = System.getProperty("line.separator"); | ||
public static final String PATH_SEP = System.getProperty("path.separator"); | ||
|
||
private File tempTestDir = null; | ||
|
||
/** If error during parser execution, store stderr here; can't return | ||
* stdout and stderr. This doesn't trap errors from running antlr. | ||
*/ | ||
private String parseErrors; | ||
|
||
/** Errors found while running antlr */ | ||
private StringBuilder antlrToolErrors; | ||
|
||
@org.junit.Rule | ||
public final TestRule testWatcher = new TestWatcher() { | ||
|
||
@Override | ||
protected void succeeded(Description description) { | ||
testSucceeded(description); | ||
} | ||
|
||
}; | ||
|
||
protected void testSucceeded(Description description) { | ||
// remove tmpdir if no error. | ||
eraseTempDir(); | ||
} | ||
|
||
@Override | ||
public File getTempParserDir() { | ||
return getTempTestDir(); | ||
} | ||
|
||
@Override | ||
public String getTempParserDirPath() { | ||
return getTempParserDir() == null ? null : getTempParserDir().getAbsolutePath(); | ||
} | ||
|
||
@Override | ||
public final File getTempTestDir() { | ||
return tempTestDir; | ||
} | ||
|
||
@Override | ||
public final String getTempDirPath() { | ||
return tempTestDir ==null ? null : tempTestDir.getAbsolutePath(); | ||
} | ||
|
||
|
||
public void setParseErrors(String errors) { | ||
this.parseErrors = errors; | ||
} | ||
|
||
public String getParseErrors() { | ||
return parseErrors; | ||
} | ||
|
||
public String getANTLRToolErrors() { | ||
if ( antlrToolErrors.length()==0 ) { | ||
return null; | ||
} | ||
return antlrToolErrors.toString(); | ||
} | ||
|
||
protected abstract String getPropertyPrefix(); | ||
|
||
@Override | ||
public void testSetUp() throws Exception { | ||
createTempDir(); | ||
antlrToolErrors = new StringBuilder(); | ||
} | ||
|
||
private void createTempDir() { | ||
// new output dir for each test | ||
String propName = getPropertyPrefix() + "-test-dir"; | ||
String prop = System.getProperty(propName); | ||
if(prop!=null && prop.length()>0) { | ||
tempTestDir = new File(prop); | ||
} | ||
else { | ||
String dirName = getClass().getSimpleName() + "-" + Thread.currentThread().getName() + "-" + System.currentTimeMillis(); | ||
tempTestDir = new File(System.getProperty("java.io.tmpdir"), dirName); | ||
} | ||
} | ||
|
||
@Override | ||
public void testTearDown() throws Exception { | ||
} | ||
|
||
@Override | ||
public void beforeTest(RuntimeTestDescriptor descriptor) { | ||
} | ||
|
||
@Override | ||
public void afterTest(RuntimeTestDescriptor descriptor) { | ||
} | ||
|
||
public void eraseTempDir() { | ||
if(shouldEraseTempDir()) { | ||
eraseDirectory(getTempTestDir()); | ||
} | ||
} | ||
|
||
protected boolean shouldEraseTempDir() { | ||
if(tempTestDir == null) | ||
return false; | ||
String propName = getPropertyPrefix() + "-erase-test-dir"; | ||
String prop = System.getProperty(propName); | ||
if (prop != null && prop.length() > 0) | ||
return Boolean.getBoolean(prop); | ||
else | ||
return true; | ||
} | ||
|
||
public static void eraseDirectory(File dir) { | ||
if ( dir.exists() ) { | ||
eraseFilesInDir(dir); | ||
dir.delete(); | ||
} | ||
} | ||
|
||
|
||
public static void eraseFilesInDir(File dir) { | ||
String[] files = dir.list(); | ||
for(int i = 0; files!=null && i < files.length; i++) { | ||
new File(dir,files[i]).delete(); | ||
} | ||
} | ||
|
||
private static String detectedOS; | ||
|
||
public static String getOS() { | ||
if (detectedOS == null) { | ||
String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH); | ||
if ((os.indexOf("mac") >= 0) || (os.indexOf("darwin") >= 0)) { | ||
detectedOS = "mac"; | ||
} | ||
else if (os.indexOf("win") >= 0) { | ||
detectedOS = "windows"; | ||
} | ||
else if (os.indexOf("nux") >= 0) { | ||
detectedOS = "linux"; | ||
} | ||
else { | ||
detectedOS = "unknown"; | ||
} | ||
} | ||
return detectedOS; | ||
} | ||
|
||
|
||
public static boolean isWindows() { | ||
return getOS().equalsIgnoreCase("windows"); | ||
} | ||
|
||
protected ATN createATN(Grammar g, boolean useSerializer) { | ||
if ( g.atn==null ) { | ||
semanticProcess(g); | ||
assertEquals(0, g.tool.getNumErrors()); | ||
|
||
ParserATNFactory f = g.isLexer() ? new LexerATNFactory((LexerGrammar) g) : new ParserATNFactory(g); | ||
|
||
g.atn = f.createATN(); | ||
assertEquals(0, g.tool.getNumErrors()); | ||
} | ||
|
||
ATN atn = g.atn; | ||
if ( useSerializer ) { | ||
char[] serialized = ATNSerializer.getSerializedAsChars(atn); | ||
return new ATNDeserializer().deserialize(serialized); | ||
} | ||
|
||
return atn; | ||
} | ||
protected void semanticProcess(Grammar g) { | ||
if ( g.ast!=null && !g.ast.hasErrors ) { | ||
// System.out.println(g.ast.toStringTree()); | ||
Tool antlr = new Tool(); | ||
SemanticPipeline sem = new SemanticPipeline(g); | ||
sem.process(); | ||
if ( g.getImportedGrammars()!=null ) { // process imported grammars (if any) | ||
for (Grammar imp : g.getImportedGrammars()) { | ||
antlr.processNonCombinedGrammar(imp, false); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
runtime-testsuite/test/org/antlr/v4/test/runtime/MockIntTokenStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.antlr.v4.test.runtime; | ||
|
||
import org.antlr.v4.runtime.*; | ||
import org.antlr.v4.runtime.misc.IntegerList; | ||
import org.antlr.v4.runtime.misc.Interval; | ||
|
||
public class MockIntTokenStream implements TokenStream { | ||
|
||
public IntegerList types; | ||
int p=0; | ||
|
||
public MockIntTokenStream(IntegerList types) { this.types = types; } | ||
|
||
@Override | ||
public void consume() { p++; } | ||
|
||
@Override | ||
public int LA(int i) { return LT(i).getType(); } | ||
|
||
@Override | ||
public int mark() { | ||
return index(); | ||
} | ||
|
||
@Override | ||
public int index() { return p; } | ||
|
||
@Override | ||
public void release(int marker) { | ||
seek(marker); | ||
} | ||
|
||
@Override | ||
public void seek(int index) { | ||
p = index; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return types.size(); | ||
} | ||
|
||
@Override | ||
public String getSourceName() { | ||
return UNKNOWN_SOURCE_NAME; | ||
} | ||
|
||
@Override | ||
public Token LT(int i) { | ||
CommonToken t; | ||
int rawIndex = p + i - 1; | ||
if ( rawIndex>=types.size() ) t = new CommonToken(Token.EOF); | ||
else t = new CommonToken(types.get(rawIndex)); | ||
t.setTokenIndex(rawIndex); | ||
return t; | ||
} | ||
|
||
@Override | ||
public Token get(int i) { | ||
return new org.antlr.v4.runtime.CommonToken(types.get(i)); | ||
} | ||
|
||
@Override | ||
public TokenSource getTokenSource() { | ||
return null; | ||
} | ||
|
||
|
||
@Override | ||
public String getText() { | ||
throw new UnsupportedOperationException("can't give strings"); | ||
} | ||
|
||
|
||
@Override | ||
public String getText(Interval interval) { | ||
throw new UnsupportedOperationException("can't give strings"); | ||
} | ||
|
||
|
||
@Override | ||
public String getText(RuleContext ctx) { | ||
throw new UnsupportedOperationException("can't give strings"); | ||
} | ||
|
||
|
||
@Override | ||
public String getText(Token start, Token stop) { | ||
throw new UnsupportedOperationException("can't give strings"); | ||
} | ||
} |
Oops, something went wrong.