13 asyncTimeout: 2 // seconds for async timeout
16 _config.filters = location.search.length > 1 && //restrict modules/tests by get parameters
17 $.map( location.search.slice(1).split('&'), decodeURIComponent );
19 var isLocal = !!(window.location.protocol == 'file:');
22 $('#userAgent').html(navigator.userAgent);
26 function synchronize(callback) {
27 _config.queue[_config.queue.length] = callback;
28 if(!_config.blocking) {
34 while(_config.queue.length && !_config.blocking) {
35 var call = _config.queue[0];
36 _config.queue = _config.queue.slice(1);
41 function stop(allowFailure) {
42 _config.blocking = true;
43 var handler = allowFailure ? start : function() {
44 ok( false, "Test timed out" );
47 // Disabled, caused too many random errors
48 //_config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
51 // A slight delay, to avoid any current callbacks
52 setTimeout(function(){
54 clearTimeout(_config.timeout);
55 _config.blocking = false;
60 function validTest( name ) {
61 var filters = _config.filters;
65 var i = filters.length,
68 var filter = filters[i],
69 not = filter.charAt(0) == '!';
71 filter = filter.slice(1);
72 if( name.indexOf(filter) != -1 )
81 _config.blocking = false;
82 var time = new Date();
83 _config.fixture = document.getElementById('main').innerHTML;
84 _config.ajaxSettings = $.ajaxSettings;
85 synchronize(function() {
86 time = new Date() - time;
87 $("<div>").html(['<p class="result">Tests completed in ',
88 time, ' milliseconds.<br/>',
89 _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
92 $("#banner").addClass(_config.stats.bad ? "fail" : "pass");
96 function test(name, callback, nowait) {
97 if(_config.currentModule)
98 name = _config.currentModule + " module: " + name;
100 if ( !validTest(name) )
103 synchronize(function() {
108 if( typeof console != "undefined" && console.error && console.warn ) {
109 console.error("Test " + name + " died, exception and test follows");
111 console.warn(callback.toString());
113 _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e.message ] );
116 synchronize(function() {
119 // don't output pause tests
122 if(_config.expected && _config.expected != _config.Test.length) {
123 _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
125 _config.expected = null;
127 var good = 0, bad = 0;
128 var ol = document.createElement("ol");
129 ol.style.display = "none";
130 var li = "", state = "pass";
131 for ( var i = 0; i < _config.Test.length; i++ ) {
132 var li = document.createElement("li");
133 li.className = _config.Test[i][0] ? "pass" : "fail";
134 li.innerHTML = _config.Test[i][1];
135 ol.appendChild( li );
138 if ( !_config.Test[i][0] ) {
145 var li = document.createElement("li");
146 li.className = state;
148 var b = document.createElement("strong");
149 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
150 b.onclick = function(){
151 var n = this.nextSibling;
152 if ( jQuery.css( n, "display" ) == "none" )
153 n.style.display = "block";
155 n.style.display = "none";
157 $(b).dblclick(function(event) {
158 var target = jQuery(event.target).filter("strong").clone();
159 if ( target.length ) {
160 target.children().remove();
161 location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent($.trim(target.text()));
165 li.appendChild( ol );
167 document.getElementById("tests").appendChild( li );
171 // call on start of module test to prepend name to all tests
172 function module(moduleName) {
173 _config.currentModule = moduleName;
177 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
179 function expect(asserts) {
180 _config.expected = asserts;
184 * Resets the test setup. Useful for tests that modify the DOM.
187 $("#main").html( _config.fixture );
189 $.ajaxSettings = $.extend({}, _config.ajaxSettings);
194 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
196 function ok(a, msg) {
197 _config.Test.push( [ !!a, msg ] );
201 * Asserts that two arrays are the same
203 function isSet(a, b, msg) {
205 if ( a && b && a.length != undefined && a.length == b.length ) {
206 for ( var i = 0; i < a.length; i++ )
212 _config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] );
214 _config.Test.push( [ ret, msg ] );
218 * Asserts that two objects are equivalent
220 function isObj(a, b, msg) {
234 _config.Test.push( [ ret, msg ] );
237 function serialArray( a ) {
241 for ( var i = 0; i < a.length; i++ ) {
242 var str = a[i].nodeName;
244 str = str.toLowerCase();
246 str += "#" + a[i].id;
252 return "[ " + r.join(", ") + " ]";
256 * Returns an array of elements with the given IDs, eg.
257 * @example q("main", "foo", "bar")
258 * @result [<div id="main">, <span id="foo">, <input id="bar">]
262 for ( var i = 0; i < arguments.length; i++ )
263 r.push( document.getElementById( arguments[i] ) );
268 * Asserts that a select matches the given IDs
269 * @example t("Check for something", "//[a]", ["foo", "baar"]);
270 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
275 for ( var i = 0; i < f.length; i++ )
276 s += (s && ",") + '"' + f[i].id + '"';
277 isSet(f, q.apply(q,c), a + " (" + b + ")");
281 * Add random number to url to stop IE from caching
283 * @example url("data/test.html")
284 * @result "data/test.html?10538358428943"
286 * @example url("data/test.php?foo=bar")
287 * @result "data/test.php?foo=bar&10538358345554"
289 function url(value) {
290 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
294 * Checks that the first two arguments are equal, with an optional message.
295 * Prints out both expected and actual values on failure.
297 * Prefered to ok( expected == actual, message )
299 * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) );
301 * @param Object actual
302 * @param Object expected
303 * @param String message (optional)
305 function equals(actual, expected, message) {
306 var result = expected == actual;
307 message = message || (result ? "okay" : "failed");
308 _config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] );
312 * Trigger an event on an element.
314 * @example triggerEvent( document.body, "click" );
316 * @param DOMElement elem
319 function triggerEvent( elem, type, event ) {
320 if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
321 event = document.createEvent("MouseEvents");
322 event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
323 0, 0, 0, 0, 0, false, false, false, false, 0, null);
324 elem.dispatchEvent( event );
325 } else if ( jQuery.browser.msie ) {
326 elem.fireEvent("on"+type);