test-tap/lib/Test/TAPBrowser.js
Jeremy@marzhillstudios.com 90966cab7d made the test harness a little easier to read
git-svn-id: https://test-tap.googlecode.com/svn/trunk@8 62346678-a08d-11dd-9c66-c9f8973bfffa
2008-12-28 01:00:49 +00:00

104 lines
2.5 KiB
JavaScript

function load(path) {
var url = location.pathname + "#" + encodeURIComponent(path.replace(/^\.\//, ''))
Test.TAP.prototype.diag('loading: '+path+' <a href="'+url+'">(run in a single window)</a>...');
var req = new XMLHttpRequest();
req.open("GET", path, false);
req.send(null);
if (req.readyState == 4) {
var testobj = eval(req.responseText);
return testobj;
}
}
function createScriptTag(library) {
var path = library.replace(/\./g, '/')+'.js';
var script = document.createElement("script");
script.src = lib+'/'+path;
return script;
}
function loadlib(library) {
document.body.appendChild(createScriptTag(library));
}
function loadTest(test) {
var path = testlib+'/'+test;
return load(path);
}
function loadComponents() {
for (c in toLoad) {
var comp = toLoad[c];
loadlib(comp);
}
}
function runtest(t) {
var outtxt = "";
var div = document.createElement("div");
var onclick = function () {
var c = div.className;
if (c.match(/big/)) {
c = c.replace(/big/, "small");
} else if (c.match(/small/)) {
c = c.replace(/small/, "big");
}
div.className = c;
};
div.addEventListener('click', onclick, true);
div.className = 'test small';
document.body.appendChild(div);
var outfunc = function(text) {
if (text) {
outtxt += text;
if (text.match(/(not ok|Test Suite Crashed)/g) ) {
div.innerHTML = "<div class='fail'>" + div.innerHTML + "\n" + text + "</div>"
} else {
div.innerHTML = "<div class='pass'>" + div.innerHTML + "\n" + text + "</div>"
}
}
}
// set globally for synchronous run
Test.TAP.prototype.out = outfunc;
var testobj = loadTest(t);
if (!testobj) {
alert ("Test Object: "+t+" did not load");
throw new ReferenceError("Test Object did now load");
}
// also set to instance for asynchronous output
testobj.out = outfunc
testobj.on_finished = function () {
if (outtxt.match(/(not ok|Test Suite Crashed)/g) ) {
div.className += ' failsuite';
div.className.replace(/small/, 'big');
}
results.push(div);
}
setTimeout(function () {
testobj.run_tests()
}, 0)
}
var test = loc.match(/.+[#?](.*)\??.*/);
loadComponents();
window.onload = function() {
var testlist = [];
if (test) {
runtest(test[1]);
} else {
for (t in tests) {
runtest(tests[t]);
}
}
}