Refactoring JUnit emulation

This commit is contained in:
konsoletyper 2013-12-10 22:52:59 +04:00
parent 18fd80be56
commit 231c5a43ee
3 changed files with 93 additions and 15 deletions

View File

@ -2,23 +2,64 @@ currentTestReportBody = null;
currentTimeSpent = 0;
totalTimeSpent = 0;
currentMethodCount = 0;
currentStatusCell = null;
currentExceptionCell = null;
currentTimeCell = null;
currentStartTime = 0;
currentExpectedExceptions = [];
currentFrame = null;
runTestCase = function(instance, methodName, realMethodName, expectedExceptions) {
window.addEventListener("message", function(event) {
endTime = new Date().getTime();
var message = JSON.parse(event.data);
if (message.status == "ok") {
if (currentExpectedExceptions.length > 0) {
currentStatusCell.appendChild(document.createTextNode("expected exception not thrown"));
currentStatusCell.style.color = 'yellow';
} else {
currentStatusCell.appendChild(document.createTextNode("ok"));
currentStatusCell.style.color = 'green';
}
} else if (message.status == "exception") {
if (isExpectedException(e)) {
currentStatusCell.appendChild(document.createTextNode("ok"));
currentStatusCell.style.color = 'green';
} else {
currentStatusCell.appendChild(document.createTextNode("unexpected exception"));
var exceptionText = document.createElement("pre");
exceptionText.appendChild(document.createTextNode(e.stack));
currentExceptionCell.appendChild(exceptionText);
currentStatusCell.style.color = 'red';
}
}
++currentMethodCount;
var timeSpent = (endTime - currentStartTime) / 1000;
currentTimeSpent += timeSpent;
currentTimeCell.appendChild(document.createTextNode(timeSpent.toFixed(3)));
document.body.removeChild(currentFrame);
}, false);
runTestCase = function(methodName, path, expectedExceptions) {
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);
var timeCell = document.createElement("td");
row.appendChild(timeCell);
var startTime = new Date().getTime();
var endTime;
try {
instance[realMethodName]();
currentStatusCell = document.createElement("td");
row.appendChild(currentStatusCell);
currentExceptionCell = document.createElement("td");
row.appendChild(currentExceptionCell);
currentTimeCell = document.createElement("td");
row.appendChild(currentTimeCell);
currentStartTime = new Date().getTime();
currentExpectedExceptions = expectedExceptions;
var frame = document.createElement("iframe");
cirremtFrame = frame;
document.body.appendChild(frame);
var frameDoc = frame.contentWindow.document;
var frameScript = frameDoc.createElement("script");
frameScript.src = path;
frameDoc.body.appendChild(frameScript);
endTime = new Date().getTime();
if (expectedExceptions.length > 0) {
statusCell.appendChild(document.createTextNode("expected exception not thrown"));
@ -46,10 +87,10 @@ runTestCase = function(instance, methodName, realMethodName, expectedExceptions)
timeCell.appendChild(document.createTextNode(timeSpent.toFixed(3)));
}
isExpectedException = function(e, expectedExceptions) {
if (e.$javaException !== undefined) {
for (var i = 0; i < expectedExceptions.length; ++i) {
if (expectedExceptions[i] === e.$javaException.$class) {
isExpectedException = function(e) {
if (e.javaException !== undefined) {
for (var i = 0; i < currentExpectedExceptions.length; ++i) {
if (currentExpectedExceptions[i] === e.javaException) {
return true;
}
}

View File

@ -0,0 +1,19 @@
table {
border-collapse: collapse;
border: 2px solid black;
margin: 2em 1em 2em 1em;
}
table td, table th {
border: 1px solid gray;
padding: 0.1em 0.5em 0.2em 0.5em;
}
table thead, table tfoot {
border: 2px solid black;
}
iframe {
with: 1px;
height: 1px;
padding: 0px;
margin: 0px;
border-style: none;
}

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>TeaVM JUnit tests</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<title>TeaVM JUnit tests</title>
<link rel="stylesheet" href="junit.css" type="text/css"/>
<script type="text/javascript" src="tests.js"></script>
</head>
<body>
<script type="text/javascript">
function runTests() {
document.getElementById("start-button").style.display = "none";
}
</script>
<button id="start-button" onclick="runTests()">Run tests</button>
</body>
</html>