Refactoring swift test framework.
This commit is contained in:
parent
d66f89db52
commit
6acec92866
|
@ -326,7 +326,7 @@ public class BaseGoTest implements RuntimeTestSupport {
|
|||
// writeFile(overall_tmpdir, "input", input);
|
||||
// rawBuildRecognizerTestFile(parserName, lexerName, listenerName,
|
||||
// visitorName, startRuleName, debug);
|
||||
// return execRecognizer();
|
||||
// return execParser();
|
||||
// }
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
package org.antlr.v4.test.runtime.swift;
|
||||
|
||||
import org.antlr.v4.Tool;
|
||||
import org.antlr.v4.test.runtime.ErrorQueue;
|
||||
import org.antlr.v4.test.runtime.RuntimeTestSupport;
|
||||
import org.antlr.v4.test.runtime.StreamVacuum;
|
||||
|
@ -29,16 +28,21 @@ import static org.junit.Assert.assertTrue;
|
|||
public class BaseSwiftTest implements RuntimeTestSupport {
|
||||
|
||||
/**
|
||||
* The base test directory is the directory where generated files get placed
|
||||
* during unit test execution.
|
||||
* Path of the ANTLR runtime.
|
||||
*/
|
||||
private static final String BASE_TEST_DIR;
|
||||
private static String ANTLR_RUNTIME_PATH;
|
||||
|
||||
private static String ANTLR_FRAMEWORK_DIR;
|
||||
private static void exec(String workingDir, String... command) {
|
||||
ProcessBuilder builder = new ProcessBuilder(command);
|
||||
builder.directory(new File(workingDir));
|
||||
try {
|
||||
Process p = builder.start();
|
||||
p.waitFor();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Common routine to setup the ANTLR4 runtime.
|
||||
*/
|
||||
static {
|
||||
String baseTestDir = System.getProperty("antlr-swift-test-dir");
|
||||
if (baseTestDir == null || baseTestDir.isEmpty()) {
|
||||
|
@ -49,84 +53,41 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
throw new UnsupportedOperationException("The specified base test directory does not exist: " + baseTestDir);
|
||||
}
|
||||
|
||||
BASE_TEST_DIR = baseTestDir;
|
||||
|
||||
//add antlr.swift
|
||||
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
final URL swiftRuntime = loader.getResource("Swift/Sources/Antlr4");
|
||||
// build swift runtime
|
||||
final URL swiftRuntime = loader.getResource("Swift");
|
||||
if (swiftRuntime == null) {
|
||||
throw new RuntimeException("Swift runtime file not found at:" + swiftRuntime.getPath());
|
||||
}
|
||||
String swiftRuntimePath = swiftRuntime.getPath();
|
||||
ANTLR_RUNTIME_PATH = swiftRuntime.getPath();
|
||||
exec(ANTLR_RUNTIME_PATH, "swift", "build");
|
||||
|
||||
try {
|
||||
String commandLine = "find " + swiftRuntimePath + "/ -iname *.swift -not -name merge.swift -exec cat {} ;";
|
||||
ProcessBuilder builder = new ProcessBuilder(commandLine.split(" "));
|
||||
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||
Process p = builder.start();
|
||||
StreamVacuum stdoutVacuum = new StreamVacuum(p.getInputStream());
|
||||
stdoutVacuum.start();
|
||||
p.waitFor();
|
||||
stdoutVacuum.join();
|
||||
|
||||
String antlrSwift = stdoutVacuum.toString();
|
||||
//write to Antlr4
|
||||
ANTLR_FRAMEWORK_DIR = new File(BASE_TEST_DIR, "Antlr4").getAbsolutePath();
|
||||
mkdir(ANTLR_FRAMEWORK_DIR);
|
||||
writeFile(ANTLR_FRAMEWORK_DIR, "Antlr4.swift", antlrSwift);
|
||||
//compile Antlr4 module
|
||||
buildAntlr4Framework();
|
||||
String argsString;
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
// shutdown logic
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
public void run() {
|
||||
// shutdown logic
|
||||
eraseAntlrFrameWorkDir();
|
||||
exec(ANTLR_RUNTIME_PATH, "swift", "package", "clean");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void eraseFilesIn(String dirName) {
|
||||
if (dirName == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
File dir = new File(dirName);
|
||||
String[] files = dir.list();
|
||||
if (files != null) for (String file : files) {
|
||||
new File(dirName + "/" + file).delete();
|
||||
}
|
||||
}
|
||||
|
||||
private static void eraseAntlrFrameWorkDir() {
|
||||
File frameworkdir = new File(ANTLR_FRAMEWORK_DIR);
|
||||
if (frameworkdir.exists()) {
|
||||
eraseFilesIn(ANTLR_FRAMEWORK_DIR);
|
||||
frameworkdir.delete();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean buildAntlr4Framework() throws Exception {
|
||||
String argsString = "xcrun -sdk macosx swiftc -emit-library -emit-module Antlr4.swift -module-name Antlr4 -module-link-name Antlr4 -Xlinker -install_name -Xlinker " + ANTLR_FRAMEWORK_DIR + "/libAntlr4.dylib ";
|
||||
return runProcess(argsString, ANTLR_FRAMEWORK_DIR);
|
||||
}
|
||||
|
||||
public String tmpdir = 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 stderrDuringParse;
|
||||
|
||||
/**
|
||||
* Errors found while running antlr
|
||||
*/
|
||||
private StringBuilder antlrToolErrors;
|
||||
|
||||
/**
|
||||
* If error during parser execution, store stderr here; can't return
|
||||
* stdout and stderr. This doesn't trap errors from running antlr.
|
||||
* Source files used in each small swift project.
|
||||
*/
|
||||
protected String stderrDuringParse;
|
||||
private Set<String> sourceFiles = new HashSet<String>();
|
||||
|
||||
@Override
|
||||
public void testSetUp() throws Exception {
|
||||
|
@ -137,21 +98,20 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
tmpdir = prop;
|
||||
}
|
||||
else {
|
||||
tmpdir = new File(System.getProperty("java.io.tmpdir"), getClass().getSimpleName() +
|
||||
"-" + Thread.currentThread().getName() + "-" + System.currentTimeMillis()).getAbsolutePath();
|
||||
String classSimpleName = getClass().getSimpleName();
|
||||
String threadName = Thread.currentThread().getName();
|
||||
String childPath = String.format("%s-%s-%s", classSimpleName, threadName, System.currentTimeMillis());
|
||||
tmpdir = new File(System.getProperty("java.io.tmpdir"), childPath).getAbsolutePath();
|
||||
}
|
||||
antlrToolErrors = new StringBuilder();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testTearDown() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eraseTempDir() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -179,7 +139,7 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
|
||||
@Override
|
||||
public String execLexer(String grammarFileName, String grammarStr, String lexerName, String input, boolean showDFA) {
|
||||
boolean success = rawGenerateRecognizer(grammarFileName,
|
||||
boolean success = generateParser(grammarFileName,
|
||||
grammarStr,
|
||||
null,
|
||||
lexerName);
|
||||
|
@ -188,16 +148,17 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
writeLexerTestFile(lexerName, showDFA);
|
||||
addSourceFiles("main.swift");
|
||||
|
||||
compile();
|
||||
String output = execTest();
|
||||
return output;
|
||||
// compile();
|
||||
// String output = execTest();
|
||||
// return output;
|
||||
return ""; // TODO:
|
||||
}
|
||||
|
||||
private String execTest() {
|
||||
try {
|
||||
String exec = tmpdir + "/" + EXEC_NAME;
|
||||
String[] args =
|
||||
new String[]{exec, "input"};//new File(tmpdir, "input").getAbsolutePath()
|
||||
new String[]{exec, "input"};
|
||||
ProcessBuilder pb = new ProcessBuilder(args);
|
||||
pb.directory(new File(tmpdir));
|
||||
Process p = pb.start();
|
||||
|
@ -224,13 +185,11 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
return null;
|
||||
}
|
||||
|
||||
private Set<String> sourceFiles = new HashSet<String>();
|
||||
|
||||
private void addSourceFiles(String... files) {
|
||||
Collections.addAll(this.sourceFiles, files);
|
||||
}
|
||||
|
||||
public boolean compile() {
|
||||
private boolean compile() {
|
||||
try {
|
||||
return buildProject();
|
||||
} catch (Exception e) {
|
||||
|
@ -244,13 +203,13 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
String fileList = sourceFiles.toString().replace("[", "").replace("]", "")
|
||||
.replace(", ", " ");
|
||||
|
||||
String argsString = "xcrun -sdk macosx swiftc " + fileList + " -o " + EXEC_NAME + " -I " + ANTLR_FRAMEWORK_DIR + " -L " + ANTLR_FRAMEWORK_DIR + " -module-link-name Antlr4 -suppress-warnings";
|
||||
return runProcess(argsString, tmpdir);
|
||||
// String argsString = "xcrun -sdk macosx swiftc " + fileList + " -o " + EXEC_NAME + " -I " + ANTLR_FRAMEWORK_DIR + " -L " + ANTLR_FRAMEWORK_DIR + " -module-link-name Antlr4 -suppress-warnings";
|
||||
// return runProcess(argsString, tmpdir);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean runProcess(String argsString, String execPath) throws IOException, InterruptedException {
|
||||
String[] args = argsString.split(" ");
|
||||
// System.err.println("Starting build " + argsString);//Utils.join(args, " "))
|
||||
Process process = Runtime.getRuntime().exec(args, null, new File(execPath));
|
||||
StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
|
||||
StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
|
||||
|
@ -272,31 +231,31 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
lexerName, startRuleName, input, showDiagnosticErrors, false);
|
||||
}
|
||||
|
||||
protected String execParser(String grammarFileName,
|
||||
private String execParser(String grammarFileName,
|
||||
String grammarStr,
|
||||
String parserName,
|
||||
String lexerName,
|
||||
String startRuleName,
|
||||
String input, boolean debug,boolean profile)
|
||||
{
|
||||
boolean success = rawGenerateRecognizer(grammarFileName,
|
||||
boolean success = generateParser(grammarFileName,
|
||||
grammarStr,
|
||||
parserName,
|
||||
lexerName,
|
||||
"-visitor");
|
||||
assertTrue(success);
|
||||
writeFile(tmpdir, "input", input);
|
||||
return rawExecRecognizer(parserName,
|
||||
return execParser(parserName,
|
||||
lexerName,
|
||||
startRuleName,
|
||||
debug,profile);
|
||||
}
|
||||
|
||||
protected String rawExecRecognizer(String parserName,
|
||||
String lexerName,
|
||||
String parserStartRuleName,
|
||||
boolean debug,
|
||||
boolean profile)
|
||||
private String execParser(String parserName,
|
||||
String lexerName,
|
||||
String parserStartRuleName,
|
||||
boolean debug,
|
||||
boolean profile)
|
||||
{
|
||||
this.stderrDuringParse = null;
|
||||
if ( parserName==null ) {
|
||||
|
@ -311,15 +270,15 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
}
|
||||
|
||||
addSourceFiles("main.swift");
|
||||
return execRecognizer();
|
||||
return execParser();
|
||||
}
|
||||
|
||||
public String execRecognizer() {
|
||||
private String execParser() {
|
||||
compile();
|
||||
return execTest();
|
||||
}
|
||||
|
||||
protected void writeParserTestFile(String parserName,
|
||||
private void writeParserTestFile(String parserName,
|
||||
String lexerName,
|
||||
String parserStartRuleName,
|
||||
boolean debug,
|
||||
|
@ -384,7 +343,7 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
writeFile(tmpdir, "main.swift", outputFileST.render());
|
||||
}
|
||||
|
||||
protected void writeLexerTestFile(String lexerName, boolean showDFA) {
|
||||
private void writeLexerTestFile(String lexerName, boolean showDFA) {
|
||||
ST outputFileST = new ST(
|
||||
"import Antlr4\n" +
|
||||
"import Foundation\n" +
|
||||
|
@ -417,24 +376,12 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
/**
|
||||
* Return true if all is well
|
||||
*/
|
||||
private boolean rawGenerateRecognizer(String grammarFileName,
|
||||
String grammarStr,
|
||||
String parserName,
|
||||
String lexerName,
|
||||
String... extraOptions) {
|
||||
return rawGenerateRecognizer(grammarFileName, grammarStr, parserName, lexerName, false, extraOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if all is well
|
||||
*/
|
||||
private boolean rawGenerateRecognizer(String grammarFileName,
|
||||
String grammarStr,
|
||||
String parserName,
|
||||
String lexerName,
|
||||
boolean defaultListener,
|
||||
String... extraOptions) {
|
||||
ErrorQueue equeue = antlrOnString(getTmpDir(), "Swift", grammarFileName, grammarStr, defaultListener, extraOptions);
|
||||
private boolean generateParser(String grammarFileName,
|
||||
String grammarStr,
|
||||
String parserName,
|
||||
String lexerName,
|
||||
String... extraOptions) {
|
||||
ErrorQueue equeue = antlrOnString(getTmpDir(), "Swift", grammarFileName, grammarStr, false, extraOptions);
|
||||
if (!equeue.errors.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
@ -461,17 +408,4 @@ public class BaseSwiftTest implements RuntimeTestSupport {
|
|||
addSourceFiles(files.toArray(new String[files.size()]));
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static void mkdir(String dir) {
|
||||
File f = new File(dir);
|
||||
f.mkdirs();
|
||||
}
|
||||
|
||||
protected Tool newTool(String[] args) {
|
||||
return new Tool(args);
|
||||
}
|
||||
|
||||
protected Tool newTool() {
|
||||
return new Tool(new String[]{"-o", tmpdir});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue