2
* QUnit - jQuery unit testrunner
4
* http://docs.jquery.com/QUnit
6
* Copyright (c) 2008 John Resig, Jörn Zaefferer
7
* Dual licensed under the MIT (MIT-LICENSE.txt)
8
* and GPL (GPL-LICENSE.txt) licenses.
10
* $Id: testrunner.js 5827 2008-08-13 17:14:48Z joern.zaefferer $
25
asyncTimeout: 2 // seconds for async timeout
28
_config.filters = location.search.length > 1 && //restrict modules/tests by get parameters
29
jQuery.map( location.search.slice(1).split('&'), decodeURIComponent );
31
var isLocal = !!(window.location.protocol == 'file:');
34
jQuery('#userAgent').html(navigator.userAgent);
38
function synchronize(callback) {
39
_config.queue[_config.queue.length] = callback;
40
if(!_config.blocking) {
46
while(_config.queue.length && !_config.blocking) {
47
var call = _config.queue[0];
48
_config.queue = _config.queue.slice(1);
53
function stop(allowFailure) {
54
_config.blocking = true;
55
var handler = allowFailure ? start : function() {
56
ok( false, "Test timed out" );
59
// Disabled, caused too many random errors
60
//_config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
63
// A slight delay, to avoid any current callbacks
64
setTimeout(function(){
66
clearTimeout(_config.timeout);
67
_config.blocking = false;
72
function validTest( name ) {
73
var filters = _config.filters;
77
var i = filters.length,
80
var filter = filters[i],
81
not = filter.charAt(0) == '!';
83
filter = filter.slice(1);
84
if( name.indexOf(filter) != -1 )
93
_config.blocking = false;
94
var started = +new Date;
95
_config.fixture = document.getElementById('main').innerHTML;
96
_config.ajaxSettings = jQuery.ajaxSettings;
97
synchronize(function() {
98
jQuery('<p id="testresult" class="result">').html(['Tests completed in ',
99
+new Date - started, ' milliseconds.<br/>',
100
'<span class="bad">', _config.stats.bad, '</span> tests of <span class="all">', _config.stats.all, '</span> failed.</p>']
103
jQuery("#banner").addClass(_config.stats.bad ? "fail" : "pass");
107
function test(name, callback, nowait) {
108
if(_config.currentModule)
109
name = _config.currentModule + " module: " + name;
111
if ( !validTest(name) )
114
synchronize(function() {
119
if( typeof console != "undefined" && console.error && console.warn ) {
120
console.error("Test " + name + " died, exception and test follows");
122
console.warn(callback.toString());
124
_config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e.message ] );
127
synchronize(function() {
131
if( typeof console != "undefined" && console.error && console.warn ) {
132
console.error("reset() failed, following Test " + name + ", exception and reset fn follows");
134
console.warn(reset.toString());
138
// don't output pause tests
141
if(_config.expected && _config.expected != _config.Test.length) {
142
_config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
144
_config.expected = null;
146
var good = 0, bad = 0;
147
var ol = document.createElement("ol");
148
ol.style.display = "none";
149
var li = "", state = "pass";
150
for ( var i = 0; i < _config.Test.length; i++ ) {
151
var li = document.createElement("li");
152
li.className = _config.Test[i][0] ? "pass" : "fail";
153
li.appendChild( document.createTextNode(_config.Test[i][1]) );
154
ol.appendChild( li );
157
if ( !_config.Test[i][0] ) {
164
var li = document.createElement("li");
165
li.className = state;
167
var b = document.createElement("strong");
168
b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
169
b.onclick = function(){
170
var n = this.nextSibling;
171
if ( jQuery.css( n, "display" ) == "none" )
172
n.style.display = "block";
174
n.style.display = "none";
176
jQuery(b).dblclick(function(event) {
177
var target = jQuery(event.target).filter("strong").clone();
178
if ( target.length ) {
179
target.children().remove();
180
location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent(jQuery.trim(target.text()));
184
li.appendChild( ol );
186
document.getElementById("tests").appendChild( li );
190
// call on start of module test to prepend name to all tests
191
function module(moduleName) {
192
_config.currentModule = moduleName;
196
* Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
198
function expect(asserts) {
199
_config.expected = asserts;
203
* Resets the test setup. Useful for tests that modify the DOM.
206
jQuery("#main").html( _config.fixture );
207
jQuery.event.global = {};
208
jQuery.ajaxSettings = jQuery.extend({}, _config.ajaxSettings);
213
* @example ok( jQuery("a").size() > 5, "There must be at least 5 anchors" );
215
function ok(a, msg) {
216
_config.Test.push( [ !!a, msg ] );
220
* Asserts that two arrays are the same
222
function isSet(a, b, msg) {
224
if ( a && b && a.length != undefined && a.length == b.length ) {
225
for ( var i = 0; i < a.length; i++ )
231
_config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] );
233
_config.Test.push( [ ret, msg ] );
237
* Asserts that two objects are equivalent
239
function isObj(a, b, msg) {
253
_config.Test.push( [ ret, msg ] );
256
function serialArray( a ) {
260
for ( var i = 0; i < a.length; i++ ) {
261
var str = a[i].nodeName;
263
str = str.toLowerCase();
265
str += "#" + a[i].id;
271
return "[ " + r.join(", ") + " ]";
274
function flatMap(a, block) {
276
$.each(a, function() {
277
var x = block.apply(this, arguments);
284
function serialObject( a ) {
285
return "{ " + flatMap(a, function(key, value) {
286
return key + ": " + value;
287
}).join(", ") + " }";
290
function compare(a, b, msg) {
292
if ( a && b && a.length != undefined && a.length == b.length ) {
293
for ( var i = 0; i < a.length; i++ )
294
for(var key in a[i]) {
295
if (a[i][key].constructor == Array) {
296
for (var arrayKey in a[i][key]) {
297
if (a[i][key][arrayKey] != b[i][key][arrayKey]) {
301
} else if (a[i][key] != b[i][key]) {
307
ok( ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) );
310
function compare2(a, b, msg) {
314
if (a[key].constructor == Array) {
315
for (var arrayKey in a[key]) {
316
if (a[key][arrayKey] != b[key][arrayKey]) {
320
} else if (a[key] != b[key]) {
325
if (b[key].constructor == Array) {
326
for (var arrayKey in b[key]) {
327
if (a[key][arrayKey] != b[key][arrayKey]) {
331
} else if (a[key] != b[key]) {
337
ok( ret, msg + " expected: " + serialObject(b) + " result: " + serialObject(a) );
341
* Returns an array of elements with the given IDs, eg.
342
* @example q("main", "foo", "bar")
343
* @result [<div id="main">, <span id="foo">, <input id="bar">]
347
for ( var i = 0; i < arguments.length; i++ )
348
r.push( document.getElementById( arguments[i] ) );
353
* Asserts that a select matches the given IDs
354
* @example t("Check for something", "//[a]", ["foo", "baar"]);
355
* @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
360
for ( var i = 0; i < f.length; i++ )
361
s += (s && ",") + '"' + f[i].id + '"';
362
isSet(f, q.apply(q,c), a + " (" + b + ")");
366
* Add random number to url to stop IE from caching
368
* @example url("data/test.html")
369
* @result "data/test.html?10538358428943"
371
* @example url("data/test.php?foo=bar")
372
* @result "data/test.php?foo=bar&10538358345554"
374
function url(value) {
375
return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
379
* Checks that the first two arguments are equal, with an optional message.
380
* Prints out both actual and expected values.
382
* Prefered to ok( actual == expected, message )
384
* @example equals( $.format("Received {0} bytes.", 2), "Received 2 bytes." );
386
* @param Object actual
387
* @param Object expected
388
* @param String message (optional)
390
function equals(actual, expected, message) {
391
var result = expected == actual;
392
message = message || (result ? "okay" : "failed");
393
_config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] );
397
* Trigger an event on an element.
399
* @example triggerEvent( document.body, "click" );
401
* @param DOMElement elem
404
function triggerEvent( elem, type, event ) {
405
if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
406
event = document.createEvent("MouseEvents");
407
event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
408
0, 0, 0, 0, 0, false, false, false, false, 0, null);
409
elem.dispatchEvent( event );
410
} else if ( jQuery.browser.msie ) {
411
elem.fireEvent("on"+type);