mirror of
https://github.com/zaphar/test-tap.git
synced 2025-07-21 20:19:49 -04:00
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:
parent
9337bc047e
commit
90966cab7d
@ -1,137 +1,103 @@
|
||||
Test.TAPBrowser = function (path, tests) {
|
||||
this.test_path = path;
|
||||
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");
|
||||
|
||||
this.parseLocation()
|
||||
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);
|
||||
|
||||
if(this.params.test) {
|
||||
this.tests = [this.params.test];
|
||||
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 {
|
||||
this.tests = tests
|
||||
}
|
||||
|
||||
if(this.params.real_world) {
|
||||
var world = new Test.RealWorld()
|
||||
world[this.params.real_world]()
|
||||
for (t in tests) {
|
||||
runtest(tests[t]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Test.TAPBrowser.prototype = {
|
||||
|
||||
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]
|
||||
}
|
||||
}
|
||||
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) {
|
||||
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;
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
var outfunc = function(text) {
|
||||
if (text) {
|
||||
outtxt += text;
|
||||
div.innerHTML = div.innerHTML + "\n" + text + "<br />"
|
||||
}
|
||||
}
|
||||
|
||||
// set globally for synchronous run
|
||||
Test.TAP.prototype.out = outfunc;
|
||||
var testobj = this.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 += ' fail';
|
||||
div.className.replace(/small/, 'big');
|
||||
} else {
|
||||
div.className += ' pass';
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
testobj.run_tests();
|
||||
}, 0)
|
||||
},
|
||||
|
||||
run: function () {
|
||||
for (var i = 0; i < this.tests.length; i++) {
|
||||
var t = this.tests[i]
|
||||
this.runTest(t)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -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">
|
||||
|
Loading…
x
Reference in New Issue
Block a user