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 6173 2009-02-02 20:09:32Z jeresig $
15 // Tests for equality any JavaScript type and structure without unexpected results.
16 // Discussions and reference: http://philrathe.com/articles/equiv
17 // Test suites: http://philrathe.com/tests/equiv
18 // Author: Philippe Rathé <prathe@gmail.com>
19 var equiv = function () {
21 var innerEquiv; // the real equiv function
22 var callers = []; // stack to decide between skip/abort functions
24 // Determine what is o.
26 if (typeof o === "string") {
29 } else if (typeof o === "boolean") {
32 } else if (typeof o === "number") {
40 } else if (typeof o === "undefined") {
43 // consider: typeof null === object
44 } else if (o === null) {
47 // consider: typeof [] === object
48 } else if (o instanceof Array) {
51 // consider: typeof new Date() === object
52 } else if (o instanceof Date) {
55 // consider: /./ instanceof Object;
56 // /./ instanceof RegExp;
57 // typeof /./ === "function"; // => false in IE and Opera,
58 // true in FF and Safari
59 } else if (o instanceof RegExp) {
62 } else if (typeof o === "object") {
65 } else if (o instanceof Function) {
70 // Call the o related callback with the given arguments.
71 function bindCallbacks(o, callbacks, args) {
74 if (hoozit(callbacks[prop]) === "function") {
75 return callbacks[prop].apply(callbacks, args);
77 return callbacks[prop]; // or undefined
82 var callbacks = function () {
84 // for string, boolean, number and null
85 function useStrictEquality(b, a) {
90 "string": useStrictEquality,
91 "boolean": useStrictEquality,
92 "number": useStrictEquality,
93 "null": useStrictEquality,
94 "undefined": useStrictEquality,
100 "date": function (b, a) {
101 return hoozit(b) === "date" && a.valueOf() === b.valueOf();
104 "regexp": function (b, a) {
105 return hoozit(b) === "regexp" &&
106 a.source === b.source && // the regex itself
107 a.global === b.global && // and its modifers (gmi) ...
108 a.ignoreCase === b.ignoreCase &&
109 a.multiline === b.multiline;
112 // - skip when the property is a method of an instance (OOP)
113 // - abort otherwise,
114 // initial === would have catch identical references anyway
115 "function": function () {
116 var caller = callers[callers.length - 1];
117 return caller !== Object &&
118 typeof caller !== "undefined";
121 "array": function (b, a) {
125 // b could be an object literal here
126 if ( ! (hoozit(b) === "array")) {
131 if (len !== b.length) { // safe and faster
134 for (i = 0; i < len; i++) {
135 if( ! innerEquiv(a[i], b[i])) {
142 "object": function (b, a) {
144 var eq = true; // unless we can proove it
145 var aProperties = [], bProperties = []; // collection of strings
147 // comparing constructors is more strict than using instanceof
148 if ( a.constructor !== b.constructor) {
152 // stack constructor before traversing properties
153 callers.push(a.constructor);
155 for (i in a) { // be strict: don't ensures hasOwnProperty and go deep
157 aProperties.push(i); // collect a's properties
159 if ( ! innerEquiv(a[i], b[i])) {
164 callers.pop(); // unstack, we are done
167 bProperties.push(i); // collect b's properties
170 // Ensures identical properties name
171 return eq && innerEquiv(aProperties.sort(), bProperties.sort());
176 innerEquiv = function () { // can take multiple arguments
177 var args = Array.prototype.slice.apply(arguments);
178 if (args.length < 2) {
179 return true; // end transition
182 return (function (a, b) {
184 return true; // catch the most you can
186 } else if (typeof a !== typeof b || a === null || b === null || typeof a === "undefined" || typeof b === "undefined") {
187 return false; // don't lose time with error prone cases
190 return bindCallbacks(a, callbacks, [b, a]);
193 // apply transition with (1..n) arguments
194 })(args[0], args[1]) && arguments.callee.apply(this, args.splice(1, args.length -1));
200 var GETParams = $.map( location.search.slice(1).split('&'), decodeURIComponent ),
201 ngindex = $.inArray("noglobals", GETParams),
202 noglobals = ngindex !== -1;
205 GETParams.splice( ngindex, 1 );
213 // block until document ready
215 //restrict modules/tests by get parameters
217 isLocal: !!(window.location.protocol == 'file:')
220 // public API as global methods
230 isLocal: config.isLocal,
231 same: function(a, b, message) {
232 push(equiv(a, b), a, b, message);
237 done: function(failures, total){},
238 log: function(result, message){}
240 // legacy methods below
243 compare: function() {
244 throw "compare is deprecated - use same() instead";
246 compare2: function() {
247 throw "compare2 is deprecated - use same() instead";
249 serialArray: function() {
250 throw "serialArray is deprecated - use jsDump.parse() instead";
255 triggerEvent: triggerEvent
258 $(window).load(function() {
259 $('#userAgent').html(navigator.userAgent);
260 var head = $('<div class="testrunner-toolbar"><label for="filter-pass">Hide passed tests</label></div>').insertAfter("#userAgent");
261 $('<input type="checkbox" id="filter-pass" />').attr("disabled", true).prependTo(head).click(function() {
262 $('li.pass')[this.checked ? 'hide' : 'show']();
264 $('<input type="checkbox" id="filter-missing">').attr("disabled", true).appendTo(head).click(function() {
265 $("li.fail:contains('missing test - untested code is broken code')").parent('ol').parent('li.fail')[this.checked ? 'hide' : 'show']();
267 $("#filter-missing").after('<label for="filter-missing">Hide missing tests (untested code is broken code)</label>');
271 function synchronize(callback) {
272 config.queue.push(callback);
273 if(!config.blocking) {
279 while(config.queue.length && !config.blocking) {
280 config.queue.shift()();
284 function stop(timeout) {
285 config.blocking = true;
287 config.timeout = setTimeout(function() {
288 QUnit.ok( false, "Test timed out" );
293 // A slight delay, to avoid any current callbacks
294 setTimeout(function() {
296 clearTimeout(config.timeout);
297 config.blocking = false;
302 function validTest( name ) {
303 var i = config.filters.length,
310 var filter = config.filters[i],
311 not = filter.charAt(0) == '!';
313 filter = filter.slice(1);
314 if( name.indexOf(filter) != -1 )
323 config.blocking = false;
324 var started = +new Date;
325 config.fixture = document.getElementById('main').innerHTML;
326 config.ajaxSettings = $.ajaxSettings;
327 synchronize(function() {
328 $('<p id="testresult" class="result"/>').html(['Tests completed in ',
329 +new Date - started, ' milliseconds.<br/>',
330 '<span class="bad">', config.stats.bad, '</span> tests of <span class="all">', config.stats.all, '</span> failed.']
333 $("#banner").addClass(config.stats.bad ? "fail" : "pass");
334 QUnit.done( config.stats.bad, config.stats.all );
340 function saveGlobal(){
344 for( var key in window )
347 function checkPollution( name ){
351 if( pollution.length > old.length ){
352 ok( false, "Introduced global variable(s): " + diff(old, pollution).join(", ") );
357 function diff( clean, dirty ){
358 return $.grep( dirty, function(name){
359 return $.inArray( name, clean ) == -1;
363 function test(name, callback) {
364 if(config.currentModule)
365 name = config.currentModule + " module: " + name;
366 var lifecycle = $.extend({
367 setup: function() {},
368 teardown: function() {}
369 }, config.moduleLifecycle);
371 if ( !validTest(name) )
374 synchronize(function() {
375 config.assertions = [];
376 config.expected = null;
382 QUnit.ok( false, "Setup failed on " + name + ": " + e.message );
385 synchronize(function() {
389 if( typeof console != "undefined" && console.error && console.warn ) {
390 console.error("Test " + name + " died, exception and test follows");
392 console.warn(callback.toString());
394 QUnit.ok( false, "Died on test #" + (config.assertions.length + 1) + ": " + e.message );
395 // else next test will carry the responsibility
399 synchronize(function() {
402 lifecycle.teardown();
404 QUnit.ok( false, "Teardown failed on " + name + ": " + e.message );
407 synchronize(function() {
411 if( typeof console != "undefined" && console.error && console.warn ) {
412 console.error("reset() failed, following Test " + name + ", exception and reset fn follows");
414 console.warn(reset.toString());
418 if(config.expected && config.expected != config.assertions.length) {
419 QUnit.ok( false, "Expected " + config.expected + " assertions, but " + config.assertions.length + " were run" );
422 var good = 0, bad = 0;
423 var ol = $("<ol/>").hide();
424 config.stats.all += config.assertions.length;
425 for ( var i = 0; i < config.assertions.length; i++ ) {
426 var assertion = config.assertions[i];
427 $("<li/>").addClass(assertion.result ? "pass" : "fail").text(assertion.message || "(no message)").appendTo(ol);
428 assertion.result ? good++ : bad++;
430 config.stats.bad += bad;
432 var b = $("<strong/>").html(name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + config.assertions.length + ")</b>")
434 $(this).next().toggle();
436 .dblclick(function(event) {
437 var target = $(event.target).filter("strong").clone();
438 if ( target.length ) {
439 target.children().remove();
440 location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent($.trim(target.text()));
444 $("<li/>").addClass(bad ? "fail" : "pass").append(b).append(ol).appendTo("#tests");
447 $("#filter-pass").attr("disabled", null);
448 $("#filter-missing").attr("disabled", null);
453 // call on start of module test to prepend name to all tests
454 function module(name, lifecycle) {
455 config.currentModule = name;
456 config.moduleLifecycle = lifecycle;
460 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
462 function expect(asserts) {
463 config.expected = asserts;
467 * Resets the test setup. Useful for tests that modify the DOM.
470 $("#main").html( config.fixture );
472 $.ajaxSettings = $.extend({}, config.ajaxSettings);
477 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
479 function ok(a, msg) {
482 config.assertions.push({
489 * Asserts that two arrays are the same
491 function isSet(a, b, msg) {
492 function serialArray( a ) {
496 for ( var i = 0; i < a.length; i++ ) {
497 var str = a[i].nodeName;
499 str = str.toLowerCase();
501 str += "#" + a[i].id;
507 return "[ " + r.join(", ") + " ]";
510 if ( a && b && a.length != undefined && a.length == b.length ) {
511 for ( var i = 0; i < a.length; i++ )
516 QUnit.ok( ret, !ret ? (msg + " expected: " + serialArray(b) + " result: " + serialArray(a)) : msg );
520 * Asserts that two objects are equivalent
522 function isObj(a, b, msg) {
536 QUnit.ok( ret, msg );
540 * Returns an array of elements with the given IDs, eg.
541 * @example q("main", "foo", "bar")
542 * @result [<div id="main">, <span id="foo">, <input id="bar">]
546 for ( var i = 0; i < arguments.length; i++ )
547 r.push( document.getElementById( arguments[i] ) );
552 * Asserts that a select matches the given IDs
553 * @example t("Check for something", "//[a]", ["foo", "baar"]);
554 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
559 for ( var i = 0; i < f.length; i++ )
560 s += (s && ",") + '"' + f[i].id + '"';
561 isSet(f, q.apply(q,c), a + " (" + b + ")");
565 * Add random number to url to stop IE from caching
567 * @example url("data/test.html")
568 * @result "data/test.html?10538358428943"
570 * @example url("data/test.php?foo=bar")
571 * @result "data/test.php?foo=bar&10538358345554"
573 function url(value) {
574 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
578 * Checks that the first two arguments are equal, with an optional message.
579 * Prints out both actual and expected values.
581 * Prefered to ok( actual == expected, message )
583 * @example equals( $.format("Received {0} bytes.", 2), "Received 2 bytes." );
585 * @param Object actual
586 * @param Object expected
587 * @param String message (optional)
589 function equals(actual, expected, message) {
590 push(expected == actual, actual, expected, message);
593 function push(result, actual, expected, message) {
594 message = message || (result ? "okay" : "failed");
595 QUnit.ok( result, result ? message + ": " + expected : message + ", expected: " + jsDump.parse(expected) + " result: " + jsDump.parse(actual) );
599 * Trigger an event on an element.
601 * @example triggerEvent( document.body, "click" );
603 * @param DOMElement elem
606 function triggerEvent( elem, type, event ) {
607 if ( $.browser.mozilla || $.browser.opera ) {
608 event = document.createEvent("MouseEvents");
609 event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
610 0, 0, 0, 0, 0, false, false, false, false, 0, null);
611 elem.dispatchEvent( event );
612 } else if ( $.browser.msie ) {
613 elem.fireEvent("on"+type);
621 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
622 * Licensed under BSD (http://www.opensource.org/licenses/bsd-license.php)
624 * @projectDescription Advanced and extensible data dumping for Javascript.
626 * @author Ariel Flesler
627 * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
630 function quote( str ){
631 return '"' + str.toString().replace(/"/g, '\\"') + '"';
633 function literal( o ){
636 function join( pre, arr, post ){
637 var s = jsDump.separator(),
638 base = jsDump.indent();
639 inner = jsDump.indent(1);
641 arr = arr.join( ',' + s + inner );
644 return [ pre, inner + arr, base + post ].join(s);
646 function array( arr ){
647 var i = arr.length, ret = Array(i);
650 ret[i] = this.parse( arr[i] );
652 return join( '[', ret, ']' );
655 var reName = /^function (\w+)/;
657 var jsDump = window.jsDump = {
658 parse:function( obj, type ){//type is used mostly internally, you can fix a (custom)type in advance
659 var parser = this.parsers[ type || this.typeOf(obj) ];
660 type = typeof parser;
662 return type == 'function' ? parser.call( this, obj ) :
663 type == 'string' ? parser :
666 typeOf:function( obj ){
667 var type = typeof obj,
668 f = 'function';//we'll use it 3 times, save it
669 return type != 'object' && type != f ? type :
671 obj.exec ? 'regexp' :// some browsers (FF) consider regexps functions
672 obj.getHours ? 'date' :
673 obj.scrollBy ? 'window' :
674 obj.nodeName == '#document' ? 'document' :
675 obj.nodeName ? 'node' :
676 obj.item ? 'nodelist' : // Safari reports nodelists as functions
677 obj.callee ? 'arguments' :
678 obj.call || obj.constructor != Array && //an array would also fall on this hack
679 (obj+'').indexOf(f) != -1 ? f : //IE reports functions like alert, as objects
680 'length' in obj ? 'array' :
683 separator:function(){
684 return this.multiline ? this.HTML ? '<br />' : '\n' : this.HTML ? ' ' : ' ';
686 indent:function( extra ){// extra can be a number, shortcut for increasing-calling-decreasing
687 if( !this.multiline )
689 var chr = this.indentChar;
691 chr = chr.replace(/\t/g,' ').replace(/ /g,' ');
692 return Array( this._depth_ + (extra||0) ).join(chr);
695 this._depth_ += a || 1;
698 this._depth_ -= a || 1;
700 setParser:function( name, parser ){
701 this.parsers[name] = parser;
703 // The next 3 are exposed so you can use them
709 // This is the list of parsers, to modify them, use jsDump.setParser
712 document: '[Document]',
713 error:'[ERROR]', //when no parser is found, shouldn't happen
714 unknown: '[Unknown]',
716 undefined:'undefined',
717 'function':function( fn ){
718 var ret = 'function',
719 name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE
724 ret = [ ret, this.parse( fn, 'functionArgs' ), '){'].join('');
725 return join( ret, this.parse(fn,'functionCode'), '}' );
730 object:function( map ){
733 for( var key in map )
734 ret.push( this.parse(key,'key') + ': ' + this.parse(map[key]) );
736 return join( '{', ret, '}' );
738 node:function( node ){
739 var open = this.HTML ? '<' : '<',
740 close = this.HTML ? '>' : '>';
742 var tag = node.nodeName.toLowerCase(),
745 for( var a in this.DOMAttrs ){
746 var val = node[this.DOMAttrs[a]];
748 ret += ' ' + a + '=' + this.parse( val, 'attribute' );
750 return ret + close + open + '/' + tag + close;
752 functionArgs:function( fn ){//function calls it internally, it's the arguments part of the function
758 args[l] = String.fromCharCode(97+l);//97 is 'a'
759 return ' ' + args.join(', ') + ' ';
761 key:quote, //object calls it internally, the key part of an item in a map
762 functionCode:'[code]', //function calls it internally, it's the content of the function
763 attribute:quote, //node calls it internally, it's an html attribute value
766 regexp:literal, //regex
770 DOMAttrs:{//attributes to dump from nodes, name=>realName
775 HTML:false,//if true, entities are escaped ( <, >, \t, space and \n )
776 indentChar:' ',//indentation unit
777 multiline:true //if true, items in a collection, are separated by a \n, else just a space.