mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-31 12:24:10 -08:00
JUnit tests now compiled each in a separate file
This commit is contained in:
parent
03018a8f40
commit
d100654b93
|
@ -26,6 +26,7 @@ public class ClasslibTestGenerator {
|
||||||
private static Renderer renderer;
|
private static Renderer renderer;
|
||||||
private static List<MethodReference> testMethods = new ArrayList<>();
|
private static List<MethodReference> testMethods = new ArrayList<>();
|
||||||
private static Map<String, List<MethodReference>> groupedMethods = new HashMap<>();
|
private static Map<String, List<MethodReference>> groupedMethods = new HashMap<>();
|
||||||
|
private static Map<MethodReference, String> fileNames = new HashMap<>();
|
||||||
private static String[] testClasses = { "java.lang.ObjectTests", "java.lang.SystemTests",
|
private static String[] testClasses = { "java.lang.ObjectTests", "java.lang.SystemTests",
|
||||||
"java.lang.StringBuilderTests", "java.lang.ClassTests", "java.lang.StringTests",
|
"java.lang.StringBuilderTests", "java.lang.ClassTests", "java.lang.StringTests",
|
||||||
"java.lang.VMTests" };
|
"java.lang.VMTests" };
|
||||||
|
@ -33,9 +34,11 @@ public class ClasslibTestGenerator {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
outputDir = new File(args[0]);
|
outputDir = new File(args[0]);
|
||||||
outputDir.mkdirs();
|
outputDir.mkdirs();
|
||||||
|
new File(outputDir, "tests").mkdirs();
|
||||||
resourceToFile("org/teavm/javascript/runtime.js", "runtime.js");
|
resourceToFile("org/teavm/javascript/runtime.js", "runtime.js");
|
||||||
resourceToFile("org/teavm/classlib/junit-support.js", "junit-support.js");
|
resourceToFile("org/teavm/classlib/junit-support.js", "junit-support.js");
|
||||||
resourceToFile("org/teavm/classlib/junit.css", "junit.css");
|
resourceToFile("org/teavm/classlib/junit.css", "junit.css");
|
||||||
|
resourceToFile("org/teavm/classlib/junit.html", "junit.html");
|
||||||
classSource = new ClasspathClassHolderSource();
|
classSource = new ClasspathClassHolderSource();
|
||||||
for (int i = 0; i < testClasses.length; ++i) {
|
for (int i = 0; i < testClasses.length; ++i) {
|
||||||
testClasses[i] = "org.teavm.classlib." + testClasses[i];
|
testClasses[i] = "org.teavm.classlib." + testClasses[i];
|
||||||
|
@ -44,21 +47,56 @@ public class ClasslibTestGenerator {
|
||||||
ClassHolder classHolder = classSource.getClassHolder(testClass);
|
ClassHolder classHolder = classSource.getClassHolder(testClass);
|
||||||
findTests(classHolder);
|
findTests(classHolder);
|
||||||
}
|
}
|
||||||
writer.append("runTests = function() {").newLine().indent();
|
|
||||||
writer.append("document.getElementById(\"start-button\").style.display = 'none';").newLine();
|
File allTestsFile = new File(outputDir, "tests/all.js");
|
||||||
for (String testClass : testClasses) {
|
try (Writer allTestsWriter = new OutputStreamWriter(new FileOutputStream(allTestsFile), "UTF-8")) {
|
||||||
renderClassTest(classSource.getClassHolder(testClass));
|
allTestsWriter.write("doRunTests = function() {\n");
|
||||||
|
allTestsWriter.write(" new JUnitServer(document.body).runAllTests([");
|
||||||
|
boolean first = true;
|
||||||
|
for (String testClass : testClasses) {
|
||||||
|
if (!first) {
|
||||||
|
allTestsWriter.append(",");
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
allTestsWriter.append("\n { name : \"").append(testClass).append("\", methods : [");
|
||||||
|
boolean firstMethod = true;
|
||||||
|
for (MethodReference methodRef : groupedMethods.get(testClass)) {
|
||||||
|
String scriptName = "tests/" + fileNames.size() + ".js";
|
||||||
|
fileNames.put(methodRef, scriptName);
|
||||||
|
if (!firstMethod) {
|
||||||
|
allTestsWriter.append(",");
|
||||||
|
}
|
||||||
|
firstMethod = false;
|
||||||
|
allTestsWriter.append("\n { name : \"" + methodRef.getName() + "\", script : \"" +
|
||||||
|
scriptName + "\", expected : [");
|
||||||
|
MethodHolder methodHolder = classSource.getClassHolder(testClass).getMethod(
|
||||||
|
methodRef.getDescriptor());
|
||||||
|
AnnotationHolder annot = methodHolder.getAnnotations().get("org.junit.Test");
|
||||||
|
AnnotationValue expectedAnnot = annot.getValues().get("expected");
|
||||||
|
if (expectedAnnot != null) {
|
||||||
|
String className = ((ValueType.Object)expectedAnnot.getJavaClass()).getClassName();
|
||||||
|
allTestsWriter.append("\"" + className + "\"");
|
||||||
|
}
|
||||||
|
allTestsWriter.append("] }");
|
||||||
|
}
|
||||||
|
allTestsWriter.append("] }");
|
||||||
|
}
|
||||||
|
allTestsWriter.write("], function() {}); }");
|
||||||
|
}
|
||||||
|
for (MethodReference method : testMethods) {
|
||||||
|
System.out.println("Building test for " + method);
|
||||||
|
decompileClassesForTest(method, fileNames.get(method));
|
||||||
}
|
}
|
||||||
writer.outdent().append("}").newLine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void decompileClassesForTest(MethodReference methodRef, String targetName) throws IOException {
|
private static void decompileClassesForTest(MethodReference methodRef, String targetName) throws IOException {
|
||||||
|
classSource = new ClasspathClassHolderSource();
|
||||||
decompiler = new Decompiler(classSource);
|
decompiler = new Decompiler(classSource);
|
||||||
aliasProvider = new MinifyingAliasProvider();
|
aliasProvider = new DefaultAliasProvider();
|
||||||
naming = new DefaultNamingStrategy(aliasProvider, classSource);
|
naming = new DefaultNamingStrategy(aliasProvider, classSource);
|
||||||
naming.setMinifying(true);
|
naming.setMinifying(false);
|
||||||
SourceWriterBuilder builder = new SourceWriterBuilder(naming);
|
SourceWriterBuilder builder = new SourceWriterBuilder(naming);
|
||||||
builder.setMinified(true);
|
builder.setMinified(false);
|
||||||
writer = builder.build();
|
writer = builder.build();
|
||||||
renderer = new Renderer(writer, classSource);
|
renderer = new Renderer(writer, classSource);
|
||||||
renderer.renderRuntime();
|
renderer.renderRuntime();
|
||||||
|
@ -67,12 +105,21 @@ public class ClasslibTestGenerator {
|
||||||
new MethodDescriptor("<init>", ValueType.VOID));
|
new MethodDescriptor("<init>", ValueType.VOID));
|
||||||
dependencyChecker.addEntryPoint(cons);
|
dependencyChecker.addEntryPoint(cons);
|
||||||
dependencyChecker.addEntryPoint(methodRef);
|
dependencyChecker.addEntryPoint(methodRef);
|
||||||
|
dependencyChecker.attachMethodGraph(new MethodReference("java.lang.Class", new MethodDescriptor("createNew",
|
||||||
|
ValueType.object("java.lang.Class"))));
|
||||||
|
dependencyChecker.attachMethodGraph(new MethodReference("java.lang.String", new MethodDescriptor("<init>",
|
||||||
|
ValueType.arrayOf(ValueType.CHARACTER), ValueType.VOID)));
|
||||||
dependencyChecker.checkDependencies();
|
dependencyChecker.checkDependencies();
|
||||||
ListableClassHolderSource classSet = dependencyChecker.cutUnachievableClasses();
|
ListableClassHolderSource classSet = dependencyChecker.cutUnachievableClasses();
|
||||||
ClassSetOptimizer optimizer = new ClassSetOptimizer();
|
ClassSetOptimizer optimizer = new ClassSetOptimizer();
|
||||||
optimizer.optimizeAll(classSet);
|
optimizer.optimizeAll(classSet);
|
||||||
renderer.renderRuntime();
|
renderer.renderRuntime();
|
||||||
decompileClasses(classSet.getClassNames());
|
decompileClasses(classSet.getClassNames());
|
||||||
|
writer.append("JUnitClient.run(function() {").softNewLine().indent();
|
||||||
|
writer.append("var testObj = ").appendClass(methodRef.getClassName()).append(".")
|
||||||
|
.appendMethod(cons).append("();").softNewLine();
|
||||||
|
writer.append("testObj.").appendMethod(methodRef).append("();").softNewLine();
|
||||||
|
writer.outdent().append("});").newLine();
|
||||||
try (Writer out = new OutputStreamWriter(new FileOutputStream(new File(outputDir, targetName)), "UTF-8")) {
|
try (Writer out = new OutputStreamWriter(new FileOutputStream(new File(outputDir, targetName)), "UTF-8")) {
|
||||||
out.write(writer.toString());
|
out.write(writer.toString());
|
||||||
}
|
}
|
||||||
|
@ -85,27 +132,6 @@ public class ClasslibTestGenerator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void renderClassTest(ClassHolder cls) {
|
|
||||||
List<MethodReference> methods = groupedMethods.get(cls.getName());
|
|
||||||
writer.append("testClass(\"" + cls.getName() + "\", function() {").newLine().indent();
|
|
||||||
MethodReference cons = new MethodReference(cls.getName(), new MethodDescriptor("<init>", ValueType.VOID));
|
|
||||||
for (MethodReference method : methods) {
|
|
||||||
writer.append("runTestCase(").appendClass(cls.getName()).append(".").appendMethod(cons)
|
|
||||||
.append("(), \"" + method.getDescriptor().getName() + "\", \"").appendMethod(method)
|
|
||||||
.append("\", [");
|
|
||||||
MethodHolder methodHolder = classSource.getClassHolder(method.getClassName()).getMethod(
|
|
||||||
method.getDescriptor());
|
|
||||||
AnnotationHolder annot = methodHolder.getAnnotations().get("org.junit.Test");
|
|
||||||
AnnotationValue expectedAnnot = annot.getValues().get("expected");
|
|
||||||
if (expectedAnnot != null) {
|
|
||||||
String className = ((ValueType.Object)expectedAnnot.getJavaClass()).getClassName();
|
|
||||||
writer.appendClass(className);
|
|
||||||
}
|
|
||||||
writer.append("]);").newLine();
|
|
||||||
}
|
|
||||||
writer.outdent().append("})").newLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void findTests(ClassHolder cls) {
|
private static void findTests(ClassHolder cls) {
|
||||||
for (MethodHolder method : cls.getMethods()) {
|
for (MethodHolder method : cls.getMethods()) {
|
||||||
if (method.getAnnotations().get("org.junit.Test") != null) {
|
if (method.getAnnotations().get("org.junit.Test") != null) {
|
||||||
|
|
|
@ -102,10 +102,26 @@ JUnitServer.prototype.runTest = function(test, callback) {
|
||||||
this.timeSpent = 0;
|
this.timeSpent = 0;
|
||||||
this.methodCount = 0;
|
this.methodCount = 0;
|
||||||
this.createTable(test.name);
|
this.createTable(test.name);
|
||||||
|
var self = this;
|
||||||
this.runMethodFromList(test.methods, 0, function() {
|
this.runMethodFromList(test.methods, 0, function() {
|
||||||
|
self.createFooter();
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
JUnitServer.prototype.runAllTests = function(tests, callback) {
|
||||||
|
this.runTestFromList(tests, 0, callback);
|
||||||
|
}
|
||||||
|
JUnitServer.prototype.runTestFromList = function(tests, index, callback) {
|
||||||
|
if (index < tests.length) {
|
||||||
|
var test = tests[index];
|
||||||
|
var self = this;
|
||||||
|
this.runTest(test, function() {
|
||||||
|
self.runTestFromList(tests, index + 1, callback);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
JUnitServer.prototype.runMethodFromList = function(methods, index, callback) {
|
JUnitServer.prototype.runMethodFromList = function(methods, index, callback) {
|
||||||
if (index < methods.length) {
|
if (index < methods.length) {
|
||||||
var method = methods[index];
|
var method = methods[index];
|
||||||
|
|
|
@ -5,7 +5,8 @@
|
||||||
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
|
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
|
||||||
<title>TeaVM JUnit tests</title>
|
<title>TeaVM JUnit tests</title>
|
||||||
<link rel="stylesheet" href="junit.css" type="text/css"/>
|
<link rel="stylesheet" href="junit.css" type="text/css"/>
|
||||||
<script type="text/javascript" src="tests.js"></script>
|
<script type="text/javascript" src="junit-support.js"></script>
|
||||||
|
<script type="text/javascript" src="tests/all.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
|
@ -121,7 +121,7 @@ public class DependencyChecker {
|
||||||
exceptionOccured.set(null);
|
exceptionOccured.set(null);
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
if (executor.getActiveCount() == 0 || executor.awaitTermination(10, TimeUnit.MILLISECONDS)) {
|
if (executor.getActiveCount() == 0 || executor.awaitTermination(2, TimeUnit.MILLISECONDS)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user