Test framework for instructions
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package org.jetbrains.jet;
|
||||
|
||||
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FilenameFilter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public abstract class JetTestCaseBase extends LightDaemonAnalyzerTestCase {
|
||||
|
||||
private String dataPath;
|
||||
protected final String name;
|
||||
|
||||
public JetTestCaseBase(String dataPath, String name) {
|
||||
this.dataPath = dataPath;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return getTestDataPathBase();
|
||||
}
|
||||
|
||||
protected static String getTestDataPathBase() {
|
||||
return getHomeDirectory() + "/idea/testData";
|
||||
}
|
||||
|
||||
private static String getHomeDirectory() {
|
||||
return new File(PathManager.getResourceRoot(JetTestCaseBase.class, "/org/jetbrains/jet/JetTestCaseBase.class")).getParentFile().getParentFile().getParent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "test" + name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
doTest(getTestFilePath(), false, false);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String getTestFilePath() {
|
||||
return dataPath + name + ".jet";
|
||||
}
|
||||
|
||||
public interface NamedTestFactory {
|
||||
@NotNull Test createTest(@NotNull String dataPath, @NotNull String name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, @NotNull NamedTestFactory factory) {
|
||||
TestSuite suite = new TestSuite(dataPath);
|
||||
final String extension = ".jet";
|
||||
FilenameFilter extensionFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.endsWith(extension);
|
||||
}
|
||||
};
|
||||
File dir = new File(baseDataDir + dataPath);
|
||||
FileFilter dirFilter = new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
return pathname.isDirectory();
|
||||
}
|
||||
};
|
||||
if (recursive) {
|
||||
File[] files = dir.listFiles(dirFilter);
|
||||
assert files != null : dir;
|
||||
List<File> subdirs = Arrays.asList(files);
|
||||
Collections.sort(subdirs);
|
||||
for (File subdir : subdirs) {
|
||||
suite.addTest(suiteForDirectory(baseDataDir, dataPath + "/" + subdir.getName(), recursive, factory));
|
||||
}
|
||||
}
|
||||
List<File> files = Arrays.asList(dir.listFiles(extensionFilter));
|
||||
Collections.sort(files);
|
||||
for (File file : files) {
|
||||
String fileName = file.getName();
|
||||
assert fileName != null;
|
||||
suite.addTest(factory.createTest(dataPath, fileName.substring(0, fileName.length() - extension.length())));
|
||||
}
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* @author max
|
||||
*/
|
||||
package org.jetbrains.jet.cfg;
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import junit.framework.AssertionFailedError;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.JetTestCaseBase;
|
||||
import org.jetbrains.jet.lang.ErrorHandler;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.Instruction;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTrace;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.Pseudocode;
|
||||
import org.jetbrains.jet.lang.psi.JetElement;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetNamedDeclaration;
|
||||
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JetControlFlowTest extends JetTestCaseBase {
|
||||
static {
|
||||
System.setProperty("idea.platform.prefix", "Idea");
|
||||
}
|
||||
|
||||
public JetControlFlowTest(String dataPath, String name) {
|
||||
super(dataPath, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
configureByFile(getTestFilePath());
|
||||
JetFile file = (JetFile) getFile();
|
||||
|
||||
AnalyzingUtils.analyzeNamespace(file.getRootNamespace(), ErrorHandler.THROW_EXCEPTION, new JetControlFlowDataTraceFactory() {
|
||||
@NotNull
|
||||
@Override
|
||||
public JetControlFlowDataTrace createTrace(JetElement element) {
|
||||
return new JetControlFlowDataTrace() {
|
||||
private final Map<JetElement, Pseudocode> data = new LinkedHashMap<JetElement, Pseudocode>();
|
||||
|
||||
@Override
|
||||
public void recordControlFlowData(@NotNull JetElement element, @NotNull Pseudocode pseudocode) {
|
||||
data.put(element, pseudocode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
try {
|
||||
processCFData(name);
|
||||
} catch (AssertionFailedError e) {
|
||||
dumpDot(name, data.values());
|
||||
throw e;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void processCFData(String name) throws IOException {
|
||||
Collection<Pseudocode> pseudocodes = data.values();
|
||||
for (Pseudocode pseudocode : pseudocodes) {
|
||||
pseudocode.postProcess();
|
||||
}
|
||||
|
||||
StringBuilder instructionDump = new StringBuilder();
|
||||
for (Pseudocode pseudocode : pseudocodes) {
|
||||
pseudocode.dumpInstructions(instructionDump);
|
||||
instructionDump.append("=====================\n");
|
||||
}
|
||||
|
||||
String expectedInstructionsFileName = getTestDataPath() + "/" + getTestFilePath().replace(".jet", ".instructions");
|
||||
File expectedInstructionsFile = new File(expectedInstructionsFileName);
|
||||
if (!expectedInstructionsFile.exists()) {
|
||||
FileUtil.writeToFile(expectedInstructionsFile, instructionDump.toString());
|
||||
fail("No expected instructions for " + name + " generated result is written into " + expectedInstructionsFileName);
|
||||
}
|
||||
String expectedInstructions = FileUtil.loadFile(expectedInstructionsFile);
|
||||
|
||||
assertEquals(expectedInstructions, instructionDump.toString());
|
||||
|
||||
// StringBuilder graphDump = new StringBuilder();
|
||||
// for (Pseudocode pseudocode : pseudocodes) {
|
||||
// topOrderDump(pseudocode.)
|
||||
// }
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void dumpDot(String name, Collection<Pseudocode> pseudocodes) throws FileNotFoundException {
|
||||
String graphFileName = getTestDataPath() + "/" + getTestFilePath().replace(".jet", ".dot");
|
||||
File target = new File(graphFileName);
|
||||
|
||||
PrintStream out = new PrintStream(target);
|
||||
|
||||
out.println("digraph " + name + " {");
|
||||
int[] count = new int[1];
|
||||
Map<Instruction, String> nodeToName = new HashMap<Instruction, String>();
|
||||
for (Pseudocode pseudocode : pseudocodes) {
|
||||
pseudocode.dumpNodes(out, count, nodeToName);
|
||||
}
|
||||
int i = 0;
|
||||
for (Pseudocode pseudocode : pseudocodes) {
|
||||
String label;
|
||||
JetElement correspondingElement = pseudocode.getCorrespondingElement();
|
||||
if (correspondingElement instanceof JetNamedDeclaration) {
|
||||
JetNamedDeclaration namedDeclaration = (JetNamedDeclaration) correspondingElement;
|
||||
label = namedDeclaration.getName();
|
||||
}
|
||||
else {
|
||||
label = "anonymous_" + i;
|
||||
}
|
||||
out.println("subgraph cluster_" + i + " {\n" +
|
||||
"label=\"" + label + "\";\n" +
|
||||
"color=blue;\n");
|
||||
pseudocode.dumpEdges(out, count, nodeToName);
|
||||
out.println("}");
|
||||
i++;
|
||||
}
|
||||
out.println("}");
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static TestSuite suite() {
|
||||
TestSuite suite = new TestSuite();
|
||||
suite.addTest(JetTestCaseBase.suiteForDirectory(getTestDataPathBase(), "/cfg/", true, new JetTestCaseBase.NamedTestFactory() {
|
||||
@NotNull
|
||||
@Override
|
||||
public Test createTest(@NotNull String dataPath, @NotNull String name) {
|
||||
return new JetControlFlowTest(dataPath, name);
|
||||
}
|
||||
}));
|
||||
return suite;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +1,28 @@
|
||||
package org.jetbrains.jet.checkers;
|
||||
|
||||
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import org.jetbrains.jet.parsing.JetParsingTest;
|
||||
|
||||
import java.io.File;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.JetTestCaseBase;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class JetPsiCheckerTest extends LightDaemonAnalyzerTestCase {
|
||||
public class JetPsiCheckerTest extends JetTestCaseBase {
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return getHomeDirectory() + "/idea/testData";
|
||||
public JetPsiCheckerTest(String dataPath, String name) {
|
||||
super(dataPath, name);
|
||||
}
|
||||
|
||||
private static String getHomeDirectory() {
|
||||
return new File(PathManager.getResourceRoot(JetParsingTest.class, "/org/jetbrains/jet/parsing/JetParsingTest.class")).getParentFile().getParentFile().getParent();
|
||||
}
|
||||
|
||||
public void testProperties() throws Exception {
|
||||
doTest("/checker/Properties.jet", true, true);
|
||||
}
|
||||
|
||||
public void testBinaryCallsOnNullableValues() throws Exception {
|
||||
doTest("/checker/BinaryCallsOnNullableValues.jet", true, true);
|
||||
}
|
||||
|
||||
public void testQualifiedThis() throws Exception {
|
||||
doTest("/checker/QualifiedThis.jet", true, true);
|
||||
}
|
||||
|
||||
public void testBreakContinue() throws Exception {
|
||||
doTest("/checker/BreakContinue.jet", true, true);
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite();
|
||||
suite.addTest(JetTestCaseBase.suiteForDirectory(getTestDataPathBase(), "/checker/", true, new JetTestCaseBase.NamedTestFactory() {
|
||||
@NotNull
|
||||
@Override
|
||||
public Test createTest(@NotNull String dataPath, @NotNull String name) {
|
||||
return new JetPsiCheckerTest(dataPath, name);
|
||||
}
|
||||
}));
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,20 +9,18 @@ import com.intellij.psi.PsiErrorElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.testFramework.ParsingTestCase;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.JetTestCaseBase;
|
||||
import org.jetbrains.jet.lang.psi.JetElement;
|
||||
import org.jetbrains.jet.lang.psi.JetVisitor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class JetParsingTest extends ParsingTestCase {
|
||||
static {
|
||||
@@ -101,40 +99,16 @@ public class JetParsingTest extends ParsingTestCase {
|
||||
|
||||
public static TestSuite suite() {
|
||||
TestSuite suite = new TestSuite();
|
||||
suite.addTest(suiteForDirectory("/", false));
|
||||
suite.addTest(suiteForDirectory("examples", true));
|
||||
return suite;
|
||||
}
|
||||
|
||||
private static TestSuite suiteForDirectory(final String dataPath, boolean recursive) {
|
||||
TestSuite suite = new TestSuite(dataPath);
|
||||
final String extension = ".jet";
|
||||
FilenameFilter extensionFilter = new FilenameFilter() {
|
||||
JetTestCaseBase.NamedTestFactory factory = new JetTestCaseBase.NamedTestFactory() {
|
||||
@NotNull
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.endsWith(extension);
|
||||
public Test createTest(@NotNull String dataPath, @NotNull String name) {
|
||||
return new JetParsingTest(dataPath, name);
|
||||
}
|
||||
};
|
||||
File dir = new File(getTestDataDir() + "/psi/" + dataPath);
|
||||
FileFilter dirFilter = new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
return pathname.isDirectory();
|
||||
}
|
||||
};
|
||||
if (recursive) {
|
||||
List<File> subdirs = Arrays.asList(dir.listFiles(dirFilter));
|
||||
Collections.sort(subdirs);
|
||||
for (File subdir : subdirs) {
|
||||
suite.addTest(suiteForDirectory(dataPath + "/" + subdir.getName(), recursive));
|
||||
}
|
||||
}
|
||||
List<File> files = Arrays.asList(dir.listFiles(extensionFilter));
|
||||
Collections.sort(files);
|
||||
for (File file : files) {
|
||||
String fileName = file.getName();
|
||||
suite.addTest(new JetParsingTest(dataPath, fileName.substring(0, fileName.length() - extension.length())));
|
||||
}
|
||||
String prefix = JetParsingTest.getTestDataDir() + "/psi/";
|
||||
suite.addTest(JetTestCaseBase.suiteForDirectory(prefix, "/", false, factory));
|
||||
suite.addTest(JetTestCaseBase.suiteForDirectory(prefix, "examples", true, factory));
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user