13 asyncTimeout: 2 // seconds for async timeout
17 $('#userAgent').html(navigator.userAgent);
21 function synchronize(callback) {
22 _config.queue[_config.queue.length] = callback;
23 if(!_config.blocking) {
29 while(_config.queue.length && !_config.blocking) {
30 var call = _config.queue[0];
31 _config.queue = _config.queue.slice(1);
36 function stop(allowFailure) {
37 _config.blocking = true;
38 var handler = allowFailure ? start : function() {
39 ok( false, "Test timed out" );
42 _config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
46 clearTimeout(_config.timeout);
47 _config.blocking = false;
52 _config.blocking = false;
53 var time = new Date();
54 _config.fixture = document.getElementById('main').innerHTML;
55 synchronize(function() {
56 time = new Date() - time;
57 $("<div>").html(['<p class="result">Tests completed in ',
58 time, ' milliseconds.<br/>',
59 _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
62 $("#banner").addClass(_config.stats.bad ? "fail" : "pass");
66 function test(name, callback) {
67 if(_config.currentModule)
68 name = _config.currentModule + " module: " + name;
69 synchronize(function() {
74 if( typeof console != "undefined" && console.error && console.warn ) {
75 console.error("Test " + name + " died, exception and test follows");
77 console.warn(callback.toString());
79 _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
82 synchronize(function() {
85 if(_config.expected && _config.expected != _config.Test.length) {
86 _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
88 _config.expected = null;
90 var good = 0, bad = 0;
91 var ol = document.createElement("ol");
92 ol.style.display = "none";
93 var li = "", state = "pass";
94 for ( var i = 0; i < _config.Test.length; i++ ) {
95 var li = document.createElement("li");
96 li.className = _config.Test[i][0] ? "pass" : "fail";
97 li.innerHTML = _config.Test[i][1];
101 if ( !_config.Test[i][0] ) {
108 var li = document.createElement("li");
109 li.className = state;
111 var b = document.createElement("b");
112 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
113 b.onclick = function(){
114 var n = this.nextSibling;
115 if ( jQuery.css( n, "display" ) == "none" )
116 n.style.display = "block";
118 n.style.display = "none";
121 li.appendChild( ol );
123 document.getElementById("tests").appendChild( li );
127 // call on start of module test to prepend name to all tests
128 function module(moduleName) {
129 _config.currentModule = moduleName;
133 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
135 function expect(asserts) {
136 _config.expected = asserts;
140 * Resets the test setup. Useful for tests that modify the DOM.
143 document.getElementById('main').innerHTML = _config.fixture;
148 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
150 function ok(a, msg) {
151 _config.Test.push( [ !!a, msg ] );
155 * Asserts that two arrays are the same
157 function isSet(a, b, msg) {
159 if ( a && b && a.length == b.length ) {
166 _config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
168 _config.Test.push( [ ret, msg ] );
172 * Returns an array of elements with the given IDs, eg.
173 * @example q("main", "foo", "bar")
174 * @result [<div id="main">, <span id="foo">, <input id="bar">]
178 for ( var i = 0; i < arguments.length; i++ )
179 r.push( document.getElementById( arguments[i] ) );
184 * Asserts that a select matches the given IDs
185 * @example t("Check for something", "//[a]", ["foo", "baar"]);
186 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
189 var f = jQuery.find(b);
191 for ( var i = 0; i < f.length; i++ )
192 s += (s && ",") + '"' + f[i].id + '"';
193 isSet(f, q.apply(q,c), a + " (" + b + ")");
197 * Add random number to url to stop IE from caching
199 * @example url("data/test.html")
200 * @result "data/test.html?10538358428943"
202 * @example url("data/test.php?foo=bar")
203 * @result "data/test.php?foo=bar&10538358345554"
205 function url(value) {
206 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);