13 asyncTimeout: 2 // seconds for async timeout
17 $('#userAgent').html(navigator.userAgent);
19 $("h1").append(" - Disabled for Safari");
24 function synchronize(callback) {
25 _config.queue[_config.queue.length] = callback;
26 if(!_config.blocking) {
32 while(_config.queue.length && !_config.blocking) {
33 var call = _config.queue[0];
34 _config.queue = _config.queue.slice(1);
39 function stop(allowFailure) {
40 _config.blocking = true;
41 var handler = allowFailure ? start : function() {
42 ok( false, "Test timed out" );
45 _config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
49 clearTimeout(_config.timeout);
50 _config.blocking = false;
55 _config.blocking = false;
56 var time = new Date();
57 _config.fixture = document.getElementById('main').innerHTML;
58 synchronize(function() {
59 time = new Date() - time;
60 $("<div>").html(['<p class="result">Tests completed in ',
61 time, ' milliseconds.<br/>',
62 _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
65 $("#banner").addClass(_config.stats.bad ? "fail" : "pass");
69 function test(name, callback, nowait) {
70 // safari seems to have some memory problems, so we need to slow it down
71 if($.browser.safari && !nowait) {
74 setTimeout(start, 250);
78 if(_config.currentModule)
79 name = _config.currentModule + " module: " + name;
81 synchronize(function() {
86 if( typeof console != "undefined" && console.error && console.warn ) {
87 console.error("Test " + name + " died, exception and test follows");
89 console.warn(callback.toString());
91 _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
94 synchronize(function() {
97 // don't output pause tests
100 if(_config.expected && _config.expected != _config.Test.length) {
101 _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
103 _config.expected = null;
105 var good = 0, bad = 0;
106 var ol = document.createElement("ol");
107 ol.style.display = "none";
108 var li = "", state = "pass";
109 for ( var i = 0; i < _config.Test.length; i++ ) {
110 var li = document.createElement("li");
111 li.className = _config.Test[i][0] ? "pass" : "fail";
112 li.innerHTML = _config.Test[i][1];
113 ol.appendChild( li );
116 if ( !_config.Test[i][0] ) {
123 var li = document.createElement("li");
124 li.className = state;
126 var b = document.createElement("b");
127 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
128 b.onclick = function(){
129 var n = this.nextSibling;
130 if ( jQuery.css( n, "display" ) == "none" )
131 n.style.display = "block";
133 n.style.display = "none";
136 li.appendChild( ol );
138 document.getElementById("tests").appendChild( li );
142 // call on start of module test to prepend name to all tests
143 function module(moduleName) {
144 _config.currentModule = moduleName;
148 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
150 function expect(asserts) {
151 _config.expected = asserts;
155 * Resets the test setup. Useful for tests that modify the DOM.
158 document.getElementById('main').innerHTML = _config.fixture;
163 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
165 function ok(a, msg) {
166 _config.Test.push( [ !!a, msg ] );
170 * Asserts that two arrays are the same
172 function isSet(a, b, msg) {
174 if ( a && b && a.length == b.length ) {
175 for ( var i = 0; i < a.length; i++ )
181 _config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] );
183 _config.Test.push( [ ret, msg ] );
186 function serialArray( a ) {
188 for ( var i = 0; i < a.length; i++ ) {
189 var str = a[i].nodeName;
191 str = str.toLowerCase();
193 str += "#" + a[i].id;
199 return "[ " + r.join(", ") + " ]"
203 * Returns an array of elements with the given IDs, eg.
204 * @example q("main", "foo", "bar")
205 * @result [<div id="main">, <span id="foo">, <input id="bar">]
209 for ( var i = 0; i < arguments.length; i++ )
210 r.push( document.getElementById( arguments[i] ) );
215 * Asserts that a select matches the given IDs
216 * @example t("Check for something", "//[a]", ["foo", "baar"]);
217 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
222 for ( var i = 0; i < f.length; i++ )
223 s += (s && ",") + '"' + f[i].id + '"';
224 isSet(f, q.apply(q,c), a + " (" + b + ")");
228 * Add random number to url to stop IE from caching
230 * @example url("data/test.html")
231 * @result "data/test.html?10538358428943"
233 * @example url("data/test.php?foo=bar")
234 * @result "data/test.php?foo=bar&10538358345554"
236 function url(value) {
237 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
241 * Checks that the first two arguments are equal, with an optional message.
242 * Prints out both expected and actual values on failure.
244 * Prefered to ok( expected == actual, message )
246 * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) );
248 * @param Object expected
249 * @param Object actual
250 * @param String message (optional)
252 function equals(expected, actual, message) {
253 var result = expected == actual;
254 message = message || result ? "okay" : "failed";
255 _config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] );