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 != undefined && 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 ] );
187 * Asserts that two objects are equivalent
189 function isObj(a, b, msg) {
203 _config.Test.push( [ ret, msg ] );
206 function serialArray( a ) {
210 for ( var i = 0; i < a.length; i++ ) {
211 var str = a[i].nodeName;
213 str = str.toLowerCase();
215 str += "#" + a[i].id;
221 return "[ " + r.join(", ") + " ]"
225 * Returns an array of elements with the given IDs, eg.
226 * @example q("main", "foo", "bar")
227 * @result [<div id="main">, <span id="foo">, <input id="bar">]
231 for ( var i = 0; i < arguments.length; i++ )
232 r.push( document.getElementById( arguments[i] ) );
237 * Asserts that a select matches the given IDs
238 * @example t("Check for something", "//[a]", ["foo", "baar"]);
239 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
244 for ( var i = 0; i < f.length; i++ )
245 s += (s && ",") + '"' + f[i].id + '"';
246 isSet(f, q.apply(q,c), a + " (" + b + ")");
250 * Add random number to url to stop IE from caching
252 * @example url("data/test.html")
253 * @result "data/test.html?10538358428943"
255 * @example url("data/test.php?foo=bar")
256 * @result "data/test.php?foo=bar&10538358345554"
258 function url(value) {
259 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
263 * Checks that the first two arguments are equal, with an optional message.
264 * Prints out both expected and actual values on failure.
266 * Prefered to ok( expected == actual, message )
268 * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) );
270 * @param Object expected
271 * @param Object actual
272 * @param String message (optional)
274 function equals(expected, actual, message) {
275 var result = expected == actual;
276 message = message || (result ? "okay" : "failed");
277 _config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] );
281 * Trigger an event on an element.
283 * @example triggerEvent( document.body, "click" );
285 * @param DOMElement elem
288 function triggerEvent( elem, type, event ) {
289 if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
290 event = document.createEvent("MouseEvents");
291 event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
292 0, 0, 0, 0, 0, false, false, false, false, 0, null);
293 elem.dispatchEvent( event );
294 } else if ( jQuery.browser.msie ) {
295 elem.fireEvent("on"+type);