Skip to content

Commit

Permalink
Sanitize test code base (#3061)
Browse files Browse the repository at this point in the history
* sanitize test code base and factorize common code

* fix failing tests

* fix failing tests

* fix failing tests
  • Loading branch information
ericvergnaud authored Jan 29, 2021
1 parent 62a0b02 commit e50ecf4
Show file tree
Hide file tree
Showing 25 changed files with 779 additions and 4,508 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ public BaseRuntimeTest(RuntimeTestDescriptor descriptor, RuntimeTestSupport dele
this.delegate = delegate;
}

public static void mkdir(String dir) {
File f = new File(dir);
f.mkdirs();
}

@Before
public void setUp() throws Exception {
// From http://junit.sourceforge.net/javadoc/org/junit/Assume.html
Expand Down Expand Up @@ -159,7 +154,7 @@ public void testOne() throws Exception {
}

public void testParser(RuntimeTestDescriptor descriptor) throws Exception {
mkdir(delegate.getTmpDir());
RuntimeTestUtils.mkdir(delegate.getTempParserDirPath());

Pair<String, String> pair = descriptor.getGrammar();

Expand All @@ -176,7 +171,7 @@ public void testParser(RuntimeTestDescriptor descriptor) throws Exception {
g.registerRenderer(String.class, new StringRenderer());
g.importTemplates(targetTemplates);
ST grammarST = new ST(g, spair.b);
writeFile(delegate.getTmpDir(), spair.a+".g4", grammarST.render());
writeFile(delegate.getTempParserDirPath(), spair.a+".g4", grammarST.render());
}
}

Expand All @@ -201,7 +196,7 @@ public void testParser(RuntimeTestDescriptor descriptor) throws Exception {
}

public void testLexer(RuntimeTestDescriptor descriptor) throws Exception {
mkdir(delegate.getTmpDir());
RuntimeTestUtils.mkdir(delegate.getTempParserDirPath());

Pair<String, String> pair = descriptor.getGrammar();

Expand All @@ -218,7 +213,7 @@ public void testLexer(RuntimeTestDescriptor descriptor) throws Exception {
g.registerRenderer(String.class, new StringRenderer());
g.importTemplates(targetTemplates);
ST grammarST = new ST(g, spair.b);
writeFile(delegate.getTmpDir(), spair.a+".g4", grammarST.render());
writeFile(delegate.getTempParserDirPath(), spair.a+".g4", grammarST.render());
}
}

Expand All @@ -242,7 +237,7 @@ public static ErrorQueue antlrOnString(String workdir,
boolean defaultListener,
String... extraOptions)
{
mkdir(workdir);
RuntimeTestUtils.mkdir(workdir);
writeFile(workdir, grammarFileName, grammarStr);
return antlrOnString(workdir, targetName, grammarFileName, defaultListener, extraOptions);
}
Expand Down
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);
}
}
}
}

}
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");
}
}
Loading

0 comments on commit e50ecf4

Please sign in to comment.