Fixes a bug with Object.getClass() behaviour. Improves JUnit emulation

This commit is contained in:
Alexey Andreev 2013-12-03 19:07:53 +04:00
parent 167e1a52c8
commit b3fe2f6a29
6 changed files with 86 additions and 13 deletions

View File

@ -15,7 +15,7 @@ import org.teavm.model.ValueType;
* *
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public class TArrayNativeGenerator implements Generator, DependencyPlugin { public class ArrayNativeGenerator implements Generator, DependencyPlugin {
@Override @Override
public void methodAchieved(DependencyChecker checker, MethodReference method) { public void methodAchieved(DependencyChecker checker, MethodReference method) {
if (method.getName().equals("getLength")) { if (method.getName().equals("getLength")) {
@ -34,7 +34,7 @@ public class TArrayNativeGenerator implements Generator, DependencyPlugin {
private void generateGetLength(GeneratorContext context, SourceWriter writer) { private void generateGetLength(GeneratorContext context, SourceWriter writer) {
String array = context.getParameterName(1); String array = context.getParameterName(1);
writer.append("if (" + array + " === null || " + array + " .$cls.$meta.item === undefined) {") writer.append("if (" + array + " === null || " + array + " .$class.$meta.item === undefined) {")
.newLine().indent(); .newLine().indent();
String clsName = "java.lang.IllegalArgumentException"; String clsName = "java.lang.IllegalArgumentException";
MethodReference cons = new MethodReference(clsName, new MethodDescriptor("<init>", ValueType.VOID)); MethodReference cons = new MethodReference(clsName, new MethodDescriptor("<init>", ValueType.VOID));

View File

@ -10,7 +10,7 @@ import org.teavm.javascript.ni.GeneratedBy;
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public final class TArray extends TObject { public final class TArray extends TObject {
@GeneratedBy(TArrayNativeGenerator.class) @GeneratedBy(ArrayNativeGenerator.class)
@PluggableDependency(TArrayNativeGenerator.class) @PluggableDependency(ArrayNativeGenerator.class)
public static native int getLength(TObject array) throws TIllegalArgumentException; public static native int getLength(TObject array) throws TIllegalArgumentException;
} }

View File

@ -99,8 +99,12 @@ public class ClasslibTestGenerator {
out.println(" border: 2px solid black;"); out.println(" border: 2px solid black;");
out.println(" margin: 2em 1em 2em 1em;"); out.println(" margin: 2em 1em 2em 1em;");
out.println(" }"); out.println(" }");
out.println(" table td {"); out.println(" table td, table th {");
out.println(" border: 1px solid gray;"); out.println(" border: 1px solid gray;");
out.println(" padding: 0.1em 0.5em 0.2em 0.5em;");
out.println(" }");
out.println(" table thead, table tfoot {");
out.println(" border: 2px solid black;");
out.println(" }"); out.println(" }");
out.println(" </style>"); out.println(" </style>");
out.println(" </head>"); out.println(" </head>");

View File

@ -1,4 +1,7 @@
currentTestReportBody = null; currentTestReportBody = null;
currentTimeSpent = 0;
totalTimeSpent = 0;
currentMethodCount = 0;
runTestCase = function(instance, methodName, realMethodName, expectedExceptions) { runTestCase = function(instance, methodName, realMethodName, expectedExceptions) {
var row = document.createElement("tr"); var row = document.createElement("tr");
@ -10,23 +13,37 @@ runTestCase = function(instance, methodName, realMethodName, expectedExceptions)
row.appendChild(statusCell); row.appendChild(statusCell);
var exceptionCell = document.createElement("td"); var exceptionCell = document.createElement("td");
row.appendChild(exceptionCell); row.appendChild(exceptionCell);
var timeCell = document.createElement("td");
row.appendChild(timeCell);
var startTime = new Date().getTime();
var endTime;
try { try {
instance[realMethodName](); instance[realMethodName]();
endTime = new Date().getTime();
if (expectedExceptions.length > 0) { if (expectedExceptions.length > 0) {
statusCell.appendChild(document.createTextNode("expected exception not thrown")); statusCell.appendChild(document.createTextNode("expected exception not thrown"));
statusCell.style.color = 'yellow';
} else { } else {
statusCell.appendChild(document.createTextNode("ok")); statusCell.appendChild(document.createTextNode("ok"));
statusCell.style.color = 'green';
} }
} catch (e) { } catch (e) {
endTime = new Date().getTime();
if (isExpectedException(e, expectedExceptions)) { if (isExpectedException(e, expectedExceptions)) {
statusCell.appendChild(document.createTextNode("ok")); statusCell.appendChild(document.createTextNode("ok"));
statusCell.style.color = 'green';
} else { } else {
statusCell.appendChild(document.createTextNode("unexpected exception")); statusCell.appendChild(document.createTextNode("unexpected exception"));
var exceptionText = document.createElement("pre"); var exceptionText = document.createElement("pre");
exceptionText.appendChild(document.createTextNode(e.stack)); exceptionText.appendChild(document.createTextNode(e.stack));
exceptionCell.appendChild(exceptionText); exceptionCell.appendChild(exceptionText);
statusCell.style.color = 'red';
} }
} }
++currentMethodCount;
var timeSpent = (endTime - startTime) / 1000;
currentTimeSpent += timeSpent;
timeCell.appendChild(document.createTextNode(timeSpent.toFixed(3)));
} }
isExpectedException = function(e, expectedExceptions) { isExpectedException = function(e, expectedExceptions) {
@ -41,14 +58,53 @@ isExpectedException = function(e, expectedExceptions) {
} }
testClass = function(className, classTests) { testClass = function(className, classTests) {
currentTimeSpent = 0;
currentMethodCount = 0;
var table = document.createElement("table"); var table = document.createElement("table");
document.body.appendChild(table); document.body.appendChild(table);
var caption = document.createElement("caption"); var caption = document.createElement("caption");
table.appendChild(caption); table.appendChild(caption);
caption.appendChild(document.createTextNode(className)); caption.appendChild(document.createTextNode(className));
var head = document.createElement("thead");
table.appendChild(head);
var headRow = document.createElement("tr");
head.appendChild(headRow);
var headCell = document.createElement("th");
headRow.appendChild(headCell);
headCell.appendChild(document.createTextNode("Method"));
headCell = document.createElement("th");
headRow.appendChild(headCell);
headCell.appendChild(document.createTextNode("Result"));
headCell = document.createElement("th");
headRow.appendChild(headCell);
headCell.appendChild(document.createTextNode("Exception"));
headCell = document.createElement("th");
headRow.appendChild(headCell);
headCell.appendChild(document.createTextNode("Time spent, s"));
var tbody = document.createElement("tbody"); var tbody = document.createElement("tbody");
table.appendChild(tbody); table.appendChild(tbody);
currentTestReportBody = tbody; currentTestReportBody = tbody;
classTests(); classTests();
var foot = document.createElement("tfoot");
table.appendChild(foot);
var footRow = document.createElement("tr");
foot.appendChild(footRow);
var footName = document.createElement("td");
footRow.appendChild(footName);
footName.appendChild(document.createTextNode("---"));
var footMethods = document.createElement("td");
footRow.appendChild(footMethods);
footMethods.appendChild(document.createTextNode(currentMethodCount));
var footSpace = document.createElement("td");
footRow.appendChild(footSpace);
footSpace.appendChild(document.createTextNode("---"));
var footTime = document.createElement("td");
footRow.appendChild(footTime);
footTime.appendChild(document.createTextNode(currentTimeSpent.toFixed(3)));
totalTimeSpent += currentTimeSpent;
currentTestReportBody = null; currentTestReportBody = null;
} }

View File

@ -119,6 +119,8 @@ public class Renderer implements ExprVisitor, StatementVisitor {
.append(cls.getParentName() != null ? naming.getNameFor(cls.getParentName()) : .append(cls.getParentName() != null ? naming.getNameFor(cls.getParentName()) :
"Object").append("();").newLine(); "Object").append("();").newLine();
writer.appendClass(cls.getName()).append(".$meta = { "); writer.appendClass(cls.getName()).append(".$meta = { ");
writer.append("name : \"").append(cls.getName()).append("\", ");
writer.append("primitive : false, ");
writer.append("supertypes : ["); writer.append("supertypes : [");
boolean first = true; boolean first = true;
if (cls.getParentName() != null) { if (cls.getParentName() != null) {

View File

@ -47,7 +47,7 @@ $rt_arraycls = function(cls) {
if (cls.$array == undefined) { if (cls.$array == undefined) {
var arraycls = function(data) { var arraycls = function(data) {
this.data = data; this.data = data;
this.$cls = arraycls; this.$class = arraycls;
}; };
arraycls.prototype = new ($rt_objcls())(); arraycls.prototype = new ($rt_objcls())();
arraycls.$meta = { item : cls, supertypes : [$rt_objcls()] }; arraycls.$meta = { item : cls, supertypes : [$rt_objcls()] };
@ -66,13 +66,8 @@ $rt_booleanclsCache = null;
$rt_booleancls = function() { $rt_booleancls = function() {
if ($rt_booleanclsCache == null) { if ($rt_booleanclsCache == null) {
$rt_booleanclsCache = $rt_createcls(); $rt_booleanclsCache = $rt_createcls();
} $rt_booleanclsCache.primitive = true;
return $rt_booleanclsCache; $rt_booleanclsCache.name = "boolean";
}
$rt_booleanclsCache = null;
$rt_booleancls = function() {
if ($rt_booleanclsCache == null) {
$rt_booleanclsCache = $rt_createcls();
} }
return $rt_booleanclsCache; return $rt_booleanclsCache;
} }
@ -80,6 +75,8 @@ $rt_charclsCache = null;
$rt_charcls = function() { $rt_charcls = function() {
if ($rt_charclsCache == null) { if ($rt_charclsCache == null) {
$rt_charclsCache = $rt_createcls(); $rt_charclsCache = $rt_createcls();
$rt_charclsCache.primitive = true;
$rt_charclsCache.name = "char";
} }
return $rt_charclsCache; return $rt_charclsCache;
} }
@ -87,6 +84,8 @@ $rt_byteclsCache = null;
$rt_bytecls = function() { $rt_bytecls = function() {
if ($rt_byteclsCache == null) { if ($rt_byteclsCache == null) {
$rt_byteclsCache = $rt_createcls(); $rt_byteclsCache = $rt_createcls();
$rt_byteclsCache.primitive = true;
$rt_byteclsCache.name = "byte";
} }
return $rt_byteclsCache; return $rt_byteclsCache;
} }
@ -94,6 +93,8 @@ $rt_shortclsCache = null;
$rt_shortcls = function() { $rt_shortcls = function() {
if ($rt_shortclsCache == null) { if ($rt_shortclsCache == null) {
$rt_shortclsCache = $rt_createcls(); $rt_shortclsCache = $rt_createcls();
$rt_shortclsCache.primitive = true;
$rt_shortclsCache.name = "short";
} }
return $rt_shortclsCache; return $rt_shortclsCache;
} }
@ -101,6 +102,8 @@ $rt_intclsCache = null;
$rt_intcls = function() { $rt_intcls = function() {
if ($rt_intclsCache == null) { if ($rt_intclsCache == null) {
$rt_intclsCache = $rt_createcls(); $rt_intclsCache = $rt_createcls();
$rt_intclsCache.primitive = true;
$rt_intclsCache.name = "int";
} }
return $rt_intclsCache; return $rt_intclsCache;
} }
@ -108,6 +111,8 @@ $rt_longclsCache = null;
$rt_longcls = function() { $rt_longcls = function() {
if ($rt_longclsCache == null) { if ($rt_longclsCache == null) {
$rt_longclsCache = $rt_createcls(); $rt_longclsCache = $rt_createcls();
$rt_longclsCache.primitive = true;
$rt_longclsCache.name = "long";
} }
return $rt_longclsCache; return $rt_longclsCache;
} }
@ -115,6 +120,8 @@ $rt_floatclsCache = null;
$rt_floatcls = function() { $rt_floatcls = function() {
if ($rt_floatclsCache == null) { if ($rt_floatclsCache == null) {
$rt_floatclsCache = $rt_createcls(); $rt_floatclsCache = $rt_createcls();
$rt_floatclsCache.primitive = true;
$rt_floatclsCache.name = "float";
} }
return $rt_floatclsCache; return $rt_floatclsCache;
} }
@ -122,6 +129,8 @@ $rt_doubleclsCache = null;
$rt_doublecls = function() { $rt_doublecls = function() {
if ($rt_doubleclsCache == null) { if ($rt_doubleclsCache == null) {
$rt_doubleclsCache = $rt_createcls(); $rt_doubleclsCache = $rt_createcls();
$rt_doubleclsCache.primitive = true;
$rt_doubleclsCache.name = "double";
} }
return $rt_doubleclsCache; return $rt_doubleclsCache;
} }
@ -129,6 +138,8 @@ $rt_voidclsCache = null;
$rt_voidcls = function() { $rt_voidcls = function() {
if ($rt_voidclsCache == null) { if ($rt_voidclsCache == null) {
$rt_voidclsCache = $rt_createcls(); $rt_voidclsCache = $rt_createcls();
$rt_voidclsCache.primitive = true;
$rt_voidclsCache.name = "void";
} }
return $rt_voidclsCache; return $rt_voidclsCache;
} }