mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 00:04:10 -08:00
Adds generator of HTML page, running junit test cases
This commit is contained in:
parent
50a8c85cd1
commit
958592a4b0
|
@ -16,5 +16,10 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,30 @@
|
|||
package org.teavm.classlib.java.lang;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
class TObjectTests {
|
||||
@Test
|
||||
public void objectCreated() {
|
||||
Object a = new Object();
|
||||
assertNotNull(a);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void differentInstancesNotEqual() {
|
||||
Object a = new Object();
|
||||
Object b = new Object();
|
||||
assertNotEquals(a, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameInstancesAreEqual() {
|
||||
Object a = new Object();
|
||||
Object b = a;
|
||||
assertEquals(a, b);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.teavm.classlib.java.lang.annotation;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
public @interface TAnnotation {
|
||||
}
|
|
@ -1,12 +1,15 @@
|
|||
package org.teavm.classlibgen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.teavm.codegen.DefaultAliasProvider;
|
||||
import org.teavm.codegen.DefaultNamingStrategy;
|
||||
import org.teavm.codegen.SourceWriter;
|
||||
import org.teavm.javascript.Decompiler;
|
||||
import org.teavm.javascript.Renderer;
|
||||
import org.teavm.javascript.ast.ClassNode;
|
||||
import org.teavm.model.ClassHolder;
|
||||
import org.teavm.model.*;
|
||||
import org.teavm.model.resource.ClasspathClassHolderSource;
|
||||
|
||||
/**
|
||||
|
@ -14,16 +17,77 @@ import org.teavm.model.resource.ClasspathClassHolderSource;
|
|||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||
*/
|
||||
public class ClasslibTestGenerator {
|
||||
public static void main(String[] args) {
|
||||
ClasspathClassHolderSource source = new ClasspathClassHolderSource();
|
||||
Decompiler decompiler = new Decompiler(source);
|
||||
ClassHolder cls = source.getClassHolder("java.lang.Object");
|
||||
ClassNode clsNode = decompiler.decompile(cls);
|
||||
DefaultAliasProvider aliasProvider = new DefaultAliasProvider();
|
||||
DefaultNamingStrategy naming = new DefaultNamingStrategy(aliasProvider, source);
|
||||
SourceWriter writer = new SourceWriter(naming);
|
||||
Renderer renderer = new Renderer(writer, source);
|
||||
renderer.render(clsNode);
|
||||
private static ClasspathClassHolderSource classSource;
|
||||
private static Decompiler decompiler;
|
||||
private static DefaultAliasProvider aliasProvider;
|
||||
private static DefaultNamingStrategy naming;
|
||||
private static SourceWriter writer;
|
||||
private static Renderer renderer;
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
classSource = new ClasspathClassHolderSource();
|
||||
decompiler = new Decompiler(classSource);
|
||||
aliasProvider = new DefaultAliasProvider();
|
||||
naming = new DefaultNamingStrategy(aliasProvider, classSource);
|
||||
writer = new SourceWriter(naming);
|
||||
renderer = new Renderer(writer, classSource);
|
||||
decompileClass("java.lang.Object");
|
||||
decompileClass("java.lang.ObjectTests");
|
||||
decompileClass("java.lang.Class");
|
||||
decompileClass("java.lang.annotation.Annotation");
|
||||
decompileClass("org.junit.Assert");
|
||||
decompileClass("org.junit.Test");
|
||||
renderHead();
|
||||
ClassLoader classLoader = ClasslibTestGenerator.class.getClassLoader();
|
||||
try (InputStream input = classLoader.getResourceAsStream(
|
||||
"org/teavm/classlib/junit-support.js")) {
|
||||
System.out.println(IOUtils.toString(input));
|
||||
}
|
||||
try (InputStream input = classLoader.getResourceAsStream(
|
||||
"org/teavm/javascript/runtime.js")) {
|
||||
System.out.println(IOUtils.toString(input));
|
||||
}
|
||||
renderClassTest(classSource.getClassHolder("java.lang.ObjectTests"));
|
||||
System.out.println(writer);
|
||||
renderFoot();
|
||||
}
|
||||
|
||||
private static void decompileClass(String className) {
|
||||
ClassHolder cls = classSource.getClassHolder(className);
|
||||
ClassNode clsNode = decompiler.decompile(cls);
|
||||
renderer.render(clsNode);
|
||||
}
|
||||
|
||||
private static void renderHead() {
|
||||
System.out.println("<!DOCTYPE html>");
|
||||
System.out.println("<html>");
|
||||
System.out.println(" <head>");
|
||||
System.out.println(" <title>TeaVM JUnit tests</title>");
|
||||
System.out.println(" <meta http-equiv=\"Content-Type\" " +
|
||||
"content=\"text/html;charset=UTF-8\"/>");
|
||||
System.out.println(" <title>TeaVM JUnit tests</title>");
|
||||
System.out.println(" </head>");
|
||||
System.out.println(" <body>");
|
||||
System.out.println(" <script type=\"text/javascript\">");
|
||||
}
|
||||
|
||||
private static void renderFoot() {
|
||||
System.out.println(" </script>");
|
||||
System.out.println(" </body>");
|
||||
System.out.println("</html>");
|
||||
}
|
||||
|
||||
private static void renderClassTest(ClassHolder cls) {
|
||||
writer.append("testClass(\"" + cls.getName() + "\", function() {").newLine().indent();
|
||||
MethodReference cons = new MethodReference(cls.getName(),
|
||||
new MethodDescriptor("<init>", ValueType.VOID));
|
||||
for (MethodHolder method : cls.getMethods()) {
|
||||
if (method.getAnnotations().get("org.junit.Test") != null) {
|
||||
MethodReference ref = new MethodReference(cls.getName(), method.getDescriptor());
|
||||
writer.append("runTestCase(").appendClass(cls.getName()).append(".").appendMethod(cons)
|
||||
.append("(), \"" + method.getName() + "\", \"").appendMethod(ref).append("\");").newLine();
|
||||
}
|
||||
}
|
||||
writer.outdent().append("})").newLine();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
JUnitAssertionFailure = function() {}
|
||||
JUnitAssertionFailure.prototype = new Error();
|
||||
|
||||
currentTestReportBody = null;
|
||||
|
||||
runTestCase = function(instance, methodName, realMethodName) {
|
||||
var row = document.createElement("tr");
|
||||
currentTestReportBody.appendChild(row);
|
||||
var nameCell = document.createElement("td");
|
||||
row.appendChild(nameCell);
|
||||
nameCell.appendChild(document.createTextNode(methodName));
|
||||
var statusCell = document.createElement("td");
|
||||
row.appendChild(statusCell);
|
||||
var exceptionCell = document.createElement("td");
|
||||
row.appendChild(exceptionCell);
|
||||
try {
|
||||
instance[realMethodName]();
|
||||
statusCell.appendChild(document.createTextNode("ok"));
|
||||
} catch (e) {
|
||||
if (e instanceof JUnitAssertionFailure) {
|
||||
statusCell.appendChild(document.createTextNode("assertion failed"));
|
||||
exceptionCell.appendChild(document.createTextNode(e.stack));
|
||||
} else {
|
||||
statusCell.appendChild(document.createTextNode("unexpected exception"));
|
||||
exceptionCell.appendChild(document.createTextNode(e.stack));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
testClass = function(className, classTests) {
|
||||
var table = document.createElement("table");
|
||||
document.body.appendChild(table);
|
||||
var caption = document.createElement("caption");
|
||||
table.appendChild(caption);
|
||||
caption.appendChild(document.createTextNode(className));
|
||||
var tbody = document.createElement("tbody");
|
||||
table.appendChild(tbody);
|
||||
currentTestReportBody = tbody;
|
||||
classTests();
|
||||
currentTestReportBody = null;
|
||||
}
|
|
@ -153,7 +153,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
|
|||
}
|
||||
renderWorkingMethod(method);
|
||||
int startParam = 0;
|
||||
if (method.getModifiers().contains(ElementModifier.STATIC)) {
|
||||
if (method.getModifiers().contains(NodeModifier.STATIC)) {
|
||||
startParam = 1;
|
||||
}
|
||||
writer.appendClass(ref.getClassName()).append('.');
|
||||
|
@ -186,7 +186,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
|
|||
MethodReference ref = method.getReference();
|
||||
writer.append("function ").appendClass(ref.getClassName()).append('_').appendMethod(ref).append('(');
|
||||
int startParam = 0;
|
||||
if (method.getModifiers().contains(ElementModifier.STATIC)) {
|
||||
if (method.getModifiers().contains(NodeModifier.STATIC)) {
|
||||
startParam = 1;
|
||||
}
|
||||
for (int i = startParam; i <= ref.parameterCount(); ++i) {
|
||||
|
|
|
@ -302,11 +302,11 @@ public class StatementGenerator implements InstructionVisitor {
|
|||
insn.getAlternative());
|
||||
break;
|
||||
case NOT_NULL:
|
||||
branch(Expr.binary(BinaryOperation.STRICT_EQUALS, Expr.var(insn.getOperand().getIndex()),
|
||||
branch(Expr.binary(BinaryOperation.STRICT_NOT_EQUALS, Expr.var(insn.getOperand().getIndex()),
|
||||
Expr.constant(null)), insn.getConsequent(), insn.getAlternative());
|
||||
break;
|
||||
case NULL:
|
||||
branch(Expr.binary(BinaryOperation.STRICT_NOT_EQUALS, Expr.var(insn.getOperand().getIndex()),
|
||||
branch(Expr.binary(BinaryOperation.STRICT_EQUALS, Expr.var(insn.getOperand().getIndex()),
|
||||
Expr.constant(null)), insn.getConsequent(), insn.getAlternative());
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -81,7 +81,10 @@ public class ClasspathResourceMapper implements Mapper<String, ClassHolder> {
|
|||
String packageName = name.substring(0, index);
|
||||
ClassHolder classHolder = innerMapper.map(transformation.packagePrefix + "." + packageName +
|
||||
"." + transformation.classPrefix + className);
|
||||
return renamer.rename(classHolder);
|
||||
if (classHolder != null) {
|
||||
classHolder = renamer.rename(classHolder);
|
||||
}
|
||||
return classHolder;
|
||||
}
|
||||
}
|
||||
return innerMapper.map(name);
|
||||
|
|
Loading…
Reference in New Issue
Block a user