JavaPatternMatching/test/plugindevelopment/TypeInsertTester.java
2014-03-09 11:44:12 +01:00

57 lines
2.1 KiB
Java

package plugindevelopment;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Vector;
import typinferenz.TypeInsertPoint;
import typinferenz.TypeInsertSet;
import junit.framework.TestCase;
import mycompiler.MyCompiler;
import mycompiler.MyCompilerAPI;
import mycompiler.myparser.JavaParser.yyException;
import mycompiler.mytypereconstruction.TypeinferenceResultSet;
public class TypeInsertTester extends TestCase {
private static final String rootDirectory = System.getProperty("user.dir")+"/test/plugindevelopment/";
public static void test(String sourceFileToInfere, String solutionFile){
String inferedSource = "";
MyCompilerAPI compiler = MyCompiler.getAPI();
try {
compiler.parse(new File(rootDirectory + sourceFileToInfere));
Vector<TypeinferenceResultSet> results = compiler.typeReconstruction();
assertTrue("Es darf nicht mehr als eine Lösungsmöglichkeit geben", results.size()==1);
for(TypeinferenceResultSet result : results){
Vector<TypeInsertSet> points = result.getTypeInsertionPoints();
assertTrue("Es muss genau ein TypeInsertSet vorhanden sein", points.size()==1);
for(TypeInsertSet point : points){
inferedSource = point.insertAllTypes(getFileContent(rootDirectory + sourceFileToInfere));
String solutionSource = getFileContent(rootDirectory + solutionFile);
System.out.println("\nInferierter Source:\n"+inferedSource);
assertTrue("Nicht das erwartete Ergebnis", inferedSource.equals(solutionSource));
}
}
} catch (IOException | yyException e) {
e.printStackTrace();
fail();
}
}
//Source: https://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
private static String getFileContent(String path)throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(encoded)).toString();
}
}