X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=build%2Ftest%2Fdata%2Ftestrunner.js;h=e73fd671d096d67d7dae09b57d9589066cd9b4b3;hb=c6a893b68001be9b9389de5e2c5ea58c1a28c742;hp=ea731274f0fd5a8748e3b0cd7f0253d2e4069ad4;hpb=374dbbe80a3df30f52505a9c3ebd31efef711ec0;p=jquery.git diff --git a/build/test/data/testrunner.js b/build/test/data/testrunner.js index ea73127..e73fd67 100644 --- a/build/test/data/testrunner.js +++ b/build/test/data/testrunner.js @@ -1,57 +1,84 @@ -var asyncTimeout = 2 // seconds for async timeout - -var fixture; -var Test; -var stats = { - all: 0, - bad: 0 +var _config = { + fixture: null, + Test: [], + stats: { + all: 0, + bad: 0 + }, + queue: [], + blocking: true, + timeout: null, + expected: null, + currentModule: null, + asyncTimeout: 2 // seconds for async timeout }; -var queue = []; -var blocking = false; -var timeout; + +$(function() { + $('#userAgent').html(navigator.userAgent); + if($.browser.safari) + $("h1").append(" - Slowed down for Safari to prevent crashes"); + runTest(); +}); function synchronize(callback) { - queue[queue.length] = callback; - if(!blocking) { + _config.queue[_config.queue.length] = callback; + if(!_config.blocking) { process(); } } function process() { - while(queue.length && !blocking) { - var call = queue[0]; - queue = queue.slice(1); + while(_config.queue.length && !_config.blocking) { + var call = _config.queue[0]; + _config.queue = _config.queue.slice(1); call(); } } -function stop() { - blocking = true; - timeout = setTimeout(start, asyncTimeout * 1000); +function stop(allowFailure) { + _config.blocking = true; + var handler = allowFailure ? start : function() { + ok( false, "Test timed out" ); + start(); + }; + _config.timeout = setTimeout(handler, _config.asyncTimeout * 1000); } function start() { - if(timeout) - clearTimeout(timeout); - blocking = false; + if(_config.timeout) + clearTimeout(_config.timeout); + _config.blocking = false; process(); } -function runTest(tests) { - var startTime = new Date(); - fixture = document.getElementById('main').innerHTML; - tests(); +function runTest() { + _config.blocking = false; + var time = new Date(); + _config.fixture = document.getElementById('main').innerHTML; synchronize(function() { - var runTime = new Date() - startTime; - var result = document.createElement("div"); - result.innerHTML = 'Tests completed in ' + runTime + ' milliseconds.
' + - stats.bad + ' tests of ' + stats.all + ' failed.'; - document.getElementsByTagName("body")[0].appendChild(result); + time = new Date() - time; + $("
").html(['

Tests completed in ', + time, ' milliseconds.
', + _config.stats.bad, ' tests of ', _config.stats.all, ' failed.

'] + .join('')) + .appendTo("body"); + $("#banner").addClass(_config.stats.bad ? "fail" : "pass"); }); } -function test(name, callback) { +function test(name, callback, nowait) { + // safari seems to have some memory problems, so we need to slow it down + if($.browser.safari && !nowait) { + test("", function() { + stop(); + setTimeout(start, 250); + }, true); + } + + if(_config.currentModule) + name = _config.currentModule + " module: " + name; + synchronize(function() { - Test = []; + _config.Test = []; try { callback(); } catch(e) { @@ -60,27 +87,35 @@ function test(name, callback) { console.error(e); console.warn(callback.toString()); } - Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] ); + _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] ); } }); synchronize(function() { reset(); + // don't output pause tests + if(nowait) return; + + if(_config.expected && _config.expected != _config.Test.length) { + _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] ); + } + _config.expected = null; + var good = 0, bad = 0; var ol = document.createElement("ol"); - + ol.style.display = "none"; var li = "", state = "pass"; - for ( var i = 0; i < Test.length; i++ ) { + for ( var i = 0; i < _config.Test.length; i++ ) { var li = document.createElement("li"); - li.className = Test[i][0] ? "pass" : "fail"; - li.innerHTML = Test[i][1]; + li.className = _config.Test[i][0] ? "pass" : "fail"; + li.innerHTML = _config.Test[i][1]; ol.appendChild( li ); - stats.all++; - if ( !Test[i][0] ) { + _config.stats.all++; + if ( !_config.Test[i][0] ) { state = "fail"; bad++; - stats.bad++; + _config.stats.bad++; } else good++; } @@ -88,7 +123,7 @@ function test(name, callback) { li.className = state; var b = document.createElement("b"); - b.innerHTML = name + " (" + bad + ", " + good + ", " + Test.length + ")"; + b.innerHTML = name + " (" + bad + ", " + good + ", " + _config.Test.length + ")"; b.onclick = function(){ var n = this.nextSibling; if ( jQuery.css( n, "display" ) == "none" ) @@ -103,11 +138,23 @@ function test(name, callback) { }); } +// call on start of module test to prepend name to all tests +function module(moduleName) { + _config.currentModule = moduleName; +} + +/** + * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through. + */ +function expect(asserts) { + _config.expected = asserts; +} + /** * Resets the test setup. Useful for tests that modify the DOM. */ function reset() { - document.getElementById('main').innerHTML = fixture; + document.getElementById('main').innerHTML = _config.fixture; } /** @@ -115,7 +162,7 @@ function reset() { * @example ok( $("a").size() > 5, "There must be at least 5 anchors" ); */ function ok(a, msg) { - Test.push( [ !!a, msg ] ); + _config.Test.push( [ !!a, msg ] ); } /** @@ -129,9 +176,10 @@ function isSet(a, b, msg) { ret = false; } else ret = false; - if ( !ret && console ) - console.log( msg, a, b ); - Test.push( [ ret, msg ] ); + if ( !ret ) + _config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] ); + else + _config.Test.push( [ ret, msg ] ); } /** @@ -157,4 +205,17 @@ function t(a,b,c) { for ( var i = 0; i < f.length; i++ ) s += (s && ",") + '"' + f[i].id + '"'; isSet(f, q.apply(q,c), a + " (" + b + ")"); +} + +/** + * Add random number to url to stop IE from caching + * + * @example url("data/test.html") + * @result "data/test.html?10538358428943" + * + * @example url("data/test.php?foo=bar") + * @result "data/test.php?foo=bar&10538358345554" + */ +function url(value) { + return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000); } \ No newline at end of file