1 jQuery.noConflict(); // Allow the test to run with other libs or jQuery's.
15 asyncTimeout: 2 // seconds for async timeout
18 _config.filters = location.search.length > 1 && //restrict modules/tests by get parameters
19 jQuery.map( location.search.slice(1).split('&'), decodeURIComponent );
21 var isLocal = !!(window.location.protocol == 'file:');
24 jQuery('#userAgent').html(navigator.userAgent);
28 function synchronize(callback) {
29 _config.queue[_config.queue.length] = callback;
30 if(!_config.blocking) {
36 while(_config.queue.length && !_config.blocking) {
37 var call = _config.queue[0];
38 _config.queue = _config.queue.slice(1);
43 function stop(allowFailure) {
44 _config.blocking = true;
45 var handler = allowFailure ? start : function() {
46 ok( false, "Test timed out" );
49 // Disabled, caused too many random errors
50 //_config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
53 // A slight delay, to avoid any current callbacks
54 setTimeout(function(){
56 clearTimeout(_config.timeout);
57 _config.blocking = false;
62 function validTest( name ) {
63 var filters = _config.filters;
67 var i = filters.length,
70 var filter = filters[i],
71 not = filter.charAt(0) == '!';
73 filter = filter.slice(1);
74 if( name.indexOf(filter) != -1 )
83 _config.blocking = false;
84 var time = new Date();
85 _config.fixture = document.getElementById('main').innerHTML;
86 _config.ajaxSettings = jQuery.ajaxSettings;
87 synchronize(function() {
88 time = new Date() - time;
89 jQuery("<div>").html(['<p class="result">Tests completed in ',
90 time, ' milliseconds.<br/>',
91 _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
94 jQuery("#banner").addClass(_config.stats.bad ? "fail" : "pass");
98 function test(name, callback, nowait) {
99 if(_config.currentModule)
100 name = _config.currentModule + " module: " + name;
102 if ( !validTest(name) )
105 synchronize(function() {
110 if( typeof console != "undefined" && console.error && console.warn ) {
111 console.error("Test " + name + " died, exception and test follows");
113 console.warn(callback.toString());
115 _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e.message ] );
118 synchronize(function() {
121 // don't output pause tests
124 if(_config.expected && _config.expected != _config.Test.length) {
125 _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
127 _config.expected = null;
129 var good = 0, bad = 0;
130 var ol = document.createElement("ol");
131 ol.style.display = "none";
132 var li = "", state = "pass";
133 for ( var i = 0; i < _config.Test.length; i++ ) {
134 var li = document.createElement("li");
135 li.className = _config.Test[i][0] ? "pass" : "fail";
136 li.appendChild( document.createTextNode(_config.Test[i][1]) );
137 ol.appendChild( li );
140 if ( !_config.Test[i][0] ) {
147 var li = document.createElement("li");
148 li.className = state;
150 var b = document.createElement("strong");
151 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
152 b.onclick = function(){
153 var n = this.nextSibling;
154 if ( jQuery.css( n, "display" ) == "none" )
155 n.style.display = "block";
157 n.style.display = "none";
159 jQuery(b).dblclick(function(event) {
160 var target = jQuery(event.target).filter("strong").clone();
161 if ( target.length ) {
162 target.children().remove();
163 location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent(jQuery.trim(target.text()));
167 li.appendChild( ol );
169 document.getElementById("tests").appendChild( li );
173 // call on start of module test to prepend name to all tests
174 function module(moduleName) {
175 _config.currentModule = moduleName;
179 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
181 function expect(asserts) {
182 _config.expected = asserts;
186 * Resets the test setup. Useful for tests that modify the DOM.
189 jQuery("#main").html( _config.fixture );
190 jQuery.event.global = {};
191 jQuery.ajaxSettings = jQuery.extend({}, _config.ajaxSettings);
196 * @example ok( jQuery("a").size() > 5, "There must be at least 5 anchors" );
198 function ok(a, msg) {
199 _config.Test.push( [ !!a, msg ] );
203 * Asserts that two arrays are the same
205 function isSet(a, b, msg) {
207 if ( a && b && a.length != undefined && a.length == b.length ) {
208 for ( var i = 0; i < a.length; i++ )
214 _config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] );
216 _config.Test.push( [ ret, msg ] );
220 * Asserts that two objects are equivalent
222 function isObj(a, b, msg) {
236 _config.Test.push( [ ret, msg ] );
239 function serialArray( a ) {
243 for ( var i = 0; i < a.length; i++ ) {
244 var str = a[i].nodeName;
246 str = str.toLowerCase();
248 str += "#" + a[i].id;
254 return "[ " + r.join(", ") + " ]";
258 * Returns an array of elements with the given IDs, eg.
259 * @example q("main", "foo", "bar")
260 * @result [<div id="main">, <span id="foo">, <input id="bar">]
264 for ( var i = 0; i < arguments.length; i++ )
265 r.push( document.getElementById( arguments[i] ) );
270 * Asserts that a select matches the given IDs
271 * @example t("Check for something", "//[a]", ["foo", "baar"]);
272 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
277 for ( var i = 0; i < f.length; i++ )
278 s += (s && ",") + '"' + f[i].id + '"';
279 isSet(f, q.apply(q,c), a + " (" + b + ")");
283 * Add random number to url to stop IE from caching
285 * @example url("data/test.html")
286 * @result "data/test.html?10538358428943"
288 * @example url("data/test.php?foo=bar")
289 * @result "data/test.php?foo=bar&10538358345554"
291 function url(value) {
292 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
296 * Checks that the first two arguments are equal, with an optional message.
297 * Prints out both expected and actual values on failure.
299 * Prefered to ok( expected == actual, message )
301 * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) );
303 * @param Object actual
304 * @param Object expected
305 * @param String message (optional)
307 function equals(actual, expected, message) {
308 var result = expected == actual;
309 message = message || (result ? "okay" : "failed");
310 _config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] );
314 * Trigger an event on an element.
316 * @example triggerEvent( document.body, "click" );
318 * @param DOMElement elem
321 function triggerEvent( elem, type, event ) {
322 if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
323 event = document.createEvent("MouseEvents");
324 event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
325 0, 0, 0, 0, 0, false, false, false, false, 0, null);
326 elem.dispatchEvent( event );
327 } else if ( jQuery.browser.msie ) {
328 elem.fireEvent("on"+type);