13 asyncTimeout: 2 // seconds for async timeout
17 $('#userAgent').html(navigator.userAgent);
19 $("h1").append(" - Slowed down for Safari to prevent crashes");
23 function synchronize(callback) {
24 _config.queue[_config.queue.length] = callback;
25 if(!_config.blocking) {
31 while(_config.queue.length && !_config.blocking) {
32 var call = _config.queue[0];
33 _config.queue = _config.queue.slice(1);
38 function stop(allowFailure) {
39 _config.blocking = true;
40 var handler = allowFailure ? start : function() {
41 ok( false, "Test timed out" );
44 _config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
48 clearTimeout(_config.timeout);
49 _config.blocking = false;
54 _config.blocking = false;
55 var time = new Date();
56 _config.fixture = document.getElementById('main').innerHTML;
57 synchronize(function() {
58 time = new Date() - time;
59 $("<div>").html(['<p class="result">Tests completed in ',
60 time, ' milliseconds.<br/>',
61 _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
64 $("#banner").addClass(_config.stats.bad ? "fail" : "pass");
68 function test(name, callback, nowait) {
69 // safari seems to have some memory problems, so we need to slow it down
70 if($.browser.safari && !nowait) {
73 setTimeout(start, 250);
77 if(_config.currentModule)
78 name = _config.currentModule + " module: " + name;
80 synchronize(function() {
85 if( typeof console != "undefined" && console.error && console.warn ) {
86 console.error("Test " + name + " died, exception and test follows");
88 console.warn(callback.toString());
90 _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
93 synchronize(function() {
96 // don't output pause tests
99 if(_config.expected && _config.expected != _config.Test.length) {
100 _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
102 _config.expected = null;
104 var good = 0, bad = 0;
105 var ol = document.createElement("ol");
106 ol.style.display = "none";
107 var li = "", state = "pass";
108 for ( var i = 0; i < _config.Test.length; i++ ) {
109 var li = document.createElement("li");
110 li.className = _config.Test[i][0] ? "pass" : "fail";
111 li.innerHTML = _config.Test[i][1];
112 ol.appendChild( li );
115 if ( !_config.Test[i][0] ) {
122 var li = document.createElement("li");
123 li.className = state;
125 var b = document.createElement("b");
126 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
127 b.onclick = function(){
128 var n = this.nextSibling;
129 if ( jQuery.css( n, "display" ) == "none" )
130 n.style.display = "block";
132 n.style.display = "none";
135 li.appendChild( ol );
137 document.getElementById("tests").appendChild( li );
141 // call on start of module test to prepend name to all tests
142 function module(moduleName) {
143 _config.currentModule = moduleName;
147 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
149 function expect(asserts) {
150 _config.expected = asserts;
154 * Resets the test setup. Useful for tests that modify the DOM.
157 document.getElementById('main').innerHTML = _config.fixture;
162 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
164 function ok(a, msg) {
165 _config.Test.push( [ !!a, msg ] );
169 * Asserts that two arrays are the same
171 function isSet(a, b, msg) {
173 if ( a && b && a.length == b.length ) {
180 _config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
182 _config.Test.push( [ ret, msg ] );
186 * Returns an array of elements with the given IDs, eg.
187 * @example q("main", "foo", "bar")
188 * @result [<div id="main">, <span id="foo">, <input id="bar">]
192 for ( var i = 0; i < arguments.length; i++ )
193 r.push( document.getElementById( arguments[i] ) );
198 * Asserts that a select matches the given IDs
199 * @example t("Check for something", "//[a]", ["foo", "baar"]);
200 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
203 var f = jQuery.find(b);
205 for ( var i = 0; i < f.length; i++ )
206 s += (s && ",") + '"' + f[i].id + '"';
207 isSet(f, q.apply(q,c), a + " (" + b + ")");
211 * Add random number to url to stop IE from caching
213 * @example url("data/test.html")
214 * @result "data/test.html?10538358428943"
216 * @example url("data/test.php?foo=bar")
217 * @result "data/test.php?foo=bar&10538358345554"
219 function url(value) {
220 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);