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
This commit is contained in:
Jeremy@marzhillstudios.com 2008-12-28 01:00:49 +00:00
parent 9337bc047e
commit 90966cab7d
2 changed files with 99 additions and 133 deletions

View File

@ -1,75 +1,39 @@
Test.TAPBrowser = function (path, tests) {
this.test_path = path;
this.parseLocation()
if(this.params.test) {
this.tests = [this.params.test];
} else {
this.tests = tests
}
if(this.params.real_world) {
var world = new Test.RealWorld()
world[this.params.real_world]()
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;
}
}
Test.TAPBrowser.prototype = {
function createScriptTag(library) {
var path = library.replace(/\./g, '/')+'.js';
var script = document.createElement("script");
script.src = lib+'/'+path;
return script;
}
parseLocation: function () {
var info = {};
var hash = new String(location.hash)
if(hash.length > 1) {
var parts = hash.substr(1).split(";")
for(var i = 0; i < parts.length; i++) {
var pair = parts[i].split("=");
info[pair[0]] = pair[1]
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);
}
}
this.params = info;
},
}
loadTest: function (t) {
var path = this.createPath(t);
var url = location.pathname + "#test=" + encodeURIComponent(path.replace(/^\.\//, ''))
Test.TAP.prototype.diag('loading: '+path+' <a href="'+url+'">(run in a single window)</a>...');
var req;
if(navigator.appName == 'Microsoft Internet Explorer') {
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
req = new XMLHttpRequest();
}
if(!req) throw "Can't create XML HTTP Request Object"
req.open("GET", path, false);
req.send(null);
// TODO is this AJAX Impl. robust enough?
if (req.readyState == 4) {
if(window && window.console && console.log) {
console.log("Evaluating test file "+path)
}
var testobj;
try {
var js = req.responseText;
// TODO this needs to be added to every source file instead
testobj = eval(js);
} catch(e) {
throw(e)
}
return testobj;
}
},
createPath: function (t) {
var path = this.test_path +'/'+t;
return path
},
runTest: function (t) {
function runtest(t) {
var outtxt = "";
var div = document.createElement("div");
@ -82,14 +46,7 @@ Test.TAPBrowser.prototype = {
}
div.className = c;
};
if(div.addEventListener) {
div.addEventListener('click', onclick, true);
} else if(div.attachEvent) {
div.attachEvent('onclick', onclick);
} else {
throw "Can't attach event without addEventListener or attachEvent";
}
div.className = 'test small';
document.body.appendChild(div);
@ -97,13 +54,18 @@ Test.TAPBrowser.prototype = {
var outfunc = function(text) {
if (text) {
outtxt += text;
div.innerHTML = div.innerHTML + "\n" + text + "<br />"
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 = this.loadTest(t);
var testobj = loadTest(t);
if (!testobj) {
alert ("Test Object: "+t+" did not load");
throw new ReferenceError("Test Object did now load");
@ -113,25 +75,29 @@ Test.TAPBrowser.prototype = {
testobj.on_finished = function () {
if (outtxt.match(/(not ok|Test Suite Crashed)/g) ) {
div.className += ' fail';
div.className += ' failsuite';
div.className.replace(/small/, 'big');
} else {
div.className += ' pass';
}
results.push(div);
}
setTimeout(function () {
testobj.run_tests();
testobj.run_tests()
}, 0)
},
run: function () {
for (var i = 0; i < this.tests.length; i++) {
var t = this.tests[i]
this.runTest(t)
}
}
}
var test = loc.match(/.+[#?](.*)\??.*/);
loadComponents();
window.onload = function() {
var testlist = [];
if (test) {
runtest(test[1]);
} else {
for (t in tests) {
runtest(tests[t]);
}
}
}

View File

@ -26,7 +26,7 @@
.fail { background-color : red; }
.pass { background-color : green; }
.failsuite {border: 3px solid red;}
</style>
<!--Test Description here -->
<script type="text/javascript">