2 * QUnit v1.5.0 - A JavaScript Unit Testing Framework
4 * http://docs.jquery.com/QUnit
6 * Copyright (c) 2012 John Resig, Jörn Zaefferer
7 * Dual licensed under the MIT (MIT-LICENSE.txt)
8 * or GPL (GPL-LICENSE.txt) licenses.
14 setTimeout
: typeof window
.setTimeout
!== "undefined",
15 sessionStorage
: (function() {
16 var x
= "qunit-test-string";
18 sessionStorage
.setItem(x
, x
);
19 sessionStorage
.removeItem(x
);
28 toString
= Object
.prototype.toString
,
29 hasOwn
= Object
.prototype.hasOwnProperty
;
31 var Test = function(name
, testName
, expected
, async
, callback
) {
33 this.testName
= testName
;
34 this.expected
= expected
;
36 this.callback
= callback
;
41 var tests
= id("qunit-tests");
43 var b
= document
.createElement("strong");
44 b
.innerHTML
= "Running " + this.name
;
45 var li
= document
.createElement("li");
47 li
.className
= "running";
48 li
.id
= this.id
= "test-output" + testId
++;
49 tests
.appendChild( li
);
53 if (this.module
!= config
.previousModule
) {
54 if ( config
.previousModule
) {
55 runLoggingCallbacks('moduleDone', QUnit
, {
56 name
: config
.previousModule
,
57 failed
: config
.moduleStats
.bad
,
58 passed
: config
.moduleStats
.all
- config
.moduleStats
.bad
,
59 total
: config
.moduleStats
.all
62 config
.previousModule
= this.module
;
63 config
.moduleStats
= { all
: 0, bad
: 0 };
64 runLoggingCallbacks( 'moduleStart', QUnit
, {
67 } else if (config
.autorun
) {
68 runLoggingCallbacks( 'moduleStart', QUnit
, {
73 config
.current
= this;
74 this.testEnvironment
= extend({
76 teardown: function() {}
77 }, this.moduleTestEnvironment
);
79 runLoggingCallbacks( 'testStart', QUnit
, {
84 // allow utility functions to access the current test environment
86 QUnit
.current_testEnvironment
= this.testEnvironment
;
88 if ( !config
.pollution
) {
91 if ( config
.notrycatch
) {
92 this.testEnvironment
.setup
.call(this.testEnvironment
);
96 this.testEnvironment
.setup
.call(this.testEnvironment
);
98 QUnit
.pushFailure( "Setup failed on " + this.testName
+ ": " + e
.message
, extractStacktrace( e
, 1 ) );
102 config
.current
= this;
104 var running
= id("qunit-testresult");
107 running
.innerHTML
= "Running: <br/>" + this.name
;
114 if ( config
.notrycatch
) {
115 this.callback
.call(this.testEnvironment
);
119 this.callback
.call(this.testEnvironment
);
121 QUnit
.pushFailure( "Died on test #" + (this.assertions
.length
+ 1) + ": " + e
.message
, extractStacktrace( e
, 1 ) );
122 // else next test will carry the responsibility
125 // Restart the tests if they're blocking
126 if ( config
.blocking
) {
131 teardown: function() {
132 config
.current
= this;
133 if ( config
.notrycatch
) {
134 this.testEnvironment
.teardown
.call(this.testEnvironment
);
138 this.testEnvironment
.teardown
.call(this.testEnvironment
);
140 QUnit
.pushFailure( "Teardown failed on " + this.testName
+ ": " + e
.message
, extractStacktrace( e
, 1 ) );
146 config
.current
= this;
147 if ( this.expected
!= null && this.expected
!= this.assertions
.length
) {
148 QUnit
.pushFailure( "Expected " + this.expected
+ " assertions, but " + this.assertions
.length
+ " were run" );
149 } else if ( this.expected
== null && !this.assertions
.length
) {
150 QUnit
.pushFailure( "Expected at least one assertion, but none were run - call expect(0) to accept zero assertions." );
153 var good
= 0, bad
= 0,
155 tests
= id("qunit-tests");
157 config
.stats
.all
+= this.assertions
.length
;
158 config
.moduleStats
.all
+= this.assertions
.length
;
161 var ol
= document
.createElement("ol");
163 for ( i
= 0; i
< this.assertions
.length
; i
++ ) {
164 var assertion
= this.assertions
[i
];
166 li
= document
.createElement("li");
167 li
.className
= assertion
.result
? "pass" : "fail";
168 li
.innerHTML
= assertion
.message
|| (assertion
.result
? "okay" : "failed");
169 ol
.appendChild( li
);
171 if ( assertion
.result
) {
176 config
.moduleStats
.bad
++;
180 // store result when possible
181 if ( QUnit
.config
.reorder
&& defined
.sessionStorage
) {
183 sessionStorage
.setItem("qunit-test-" + this.module
+ "-" + this.testName
, bad
);
185 sessionStorage
.removeItem("qunit-test-" + this.module
+ "-" + this.testName
);
190 ol
.style
.display
= "none";
193 var b
= document
.createElement("strong");
194 b
.innerHTML
= this.name
+ " <b class='counts'>(<b class='failed'>" + bad
+ "</b>, <b class='passed'>" + good
+ "</b>, " + this.assertions
.length
+ ")</b>";
196 var a
= document
.createElement("a");
197 a
.innerHTML
= "Rerun";
198 a
.href
= QUnit
.url({ filter
: getText([b
]).replace(/\([^)]+\)$/, "").replace(/(^\s*|\s*$)/g, "") });
200 addEvent(b
, "click", function() {
201 var next
= b
.nextSibling
.nextSibling
,
202 display
= next
.style
.display
;
203 next
.style
.display
= display
=== "none" ? "block" : "none";
206 addEvent(b
, "dblclick", function(e
) {
207 var target
= e
&& e
.target
? e
.target
: window
.event
.srcElement
;
208 if ( target
.nodeName
.toLowerCase() == "span" || target
.nodeName
.toLowerCase() == "b" ) {
209 target
= target
.parentNode
;
211 if ( window
.location
&& target
.nodeName
.toLowerCase() === "strong" ) {
212 window
.location
= QUnit
.url({ filter
: getText([target
]).replace(/\([^)]+\)$/, "").replace(/(^\s*|\s*$)/g, "") });
217 li
.className
= bad
? "fail" : "pass";
218 li
.removeChild( li
.firstChild
);
221 li
.appendChild( ol
);
224 for ( i
= 0; i
< this.assertions
.length
; i
++ ) {
225 if ( !this.assertions
[i
].result
) {
228 config
.moduleStats
.bad
++;
235 runLoggingCallbacks( 'testDone', QUnit
, {
239 passed
: this.assertions
.length
- bad
,
240 total
: this.assertions
.length
246 synchronize(function() {
250 // each of these can by async
251 synchronize(function() {
254 synchronize(function() {
257 synchronize(function() {
260 synchronize(function() {
264 // defer when previous test run passed, if storage is available
265 var bad
= QUnit
.config
.reorder
&& defined
.sessionStorage
&& +sessionStorage
.getItem("qunit-test-" + this.module
+ "-" + this.testName
);
269 synchronize(run
, true);
277 // call on start of module test to prepend name to all tests
278 module: function(name
, testEnvironment
) {
279 config
.currentModule
= name
;
280 config
.currentModuleTestEnviroment
= testEnvironment
;
283 asyncTest: function(testName
, expected
, callback
) {
284 if ( arguments
.length
=== 2 ) {
289 QUnit
.test(testName
, expected
, callback
, true);
292 test: function(testName
, expected
, callback
, async
) {
293 var name
= '<span class="test-name">' + escapeInnerText(testName
) + '</span>';
295 if ( arguments
.length
=== 2 ) {
300 if ( config
.currentModule
) {
301 name
= '<span class="module-name">' + config
.currentModule
+ "</span>: " + name
;
304 if ( !validTest(config
.currentModule
+ ": " + testName
) ) {
308 var test
= new Test(name
, testName
, expected
, async
, callback
);
309 test
.module
= config
.currentModule
;
310 test
.moduleTestEnvironment
= config
.currentModuleTestEnviroment
;
314 // Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
315 expect: function(asserts
) {
316 config
.current
.expected
= asserts
;
320 // @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" );
321 ok: function(result
, msg
) {
322 if (!config
.current
) {
323 throw new Error("ok() assertion outside test context, was " + sourceFromStacktrace(2));
330 msg
= escapeInnerText(msg
|| (result
? "okay" : "failed"));
332 var source
= sourceFromStacktrace(2);
334 details
.source
= source
;
335 msg
+= '<table><tr class="test-source"><th>Source: </th><td><pre>' + escapeInnerText(source
) + '</pre></td></tr></table>';
338 runLoggingCallbacks( 'log', QUnit
, details
);
339 config
.current
.assertions
.push({
345 // Checks that the first two arguments are equal, with an optional message. Prints out both actual and expected values.
346 // @example equal( format("Received {0} bytes.", 2), "Received 2 bytes." );
347 equal: function(actual
, expected
, message
) {
348 QUnit
.push(expected
== actual
, actual
, expected
, message
);
351 notEqual: function(actual
, expected
, message
) {
352 QUnit
.push(expected
!= actual
, actual
, expected
, message
);
355 deepEqual: function(actual
, expected
, message
) {
356 QUnit
.push(QUnit
.equiv(actual
, expected
), actual
, expected
, message
);
359 notDeepEqual: function(actual
, expected
, message
) {
360 QUnit
.push(!QUnit
.equiv(actual
, expected
), actual
, expected
, message
);
363 strictEqual: function(actual
, expected
, message
) {
364 QUnit
.push(expected
=== actual
, actual
, expected
, message
);
367 notStrictEqual: function(actual
, expected
, message
) {
368 QUnit
.push(expected
!== actual
, actual
, expected
, message
);
371 raises: function(block
, expected
, message
) {
372 var actual
, ok
= false;
374 if (typeof expected
=== 'string') {
380 block
.call(config
.current
.testEnvironment
);
386 // we don't want to validate thrown error
389 // expected is a regexp
390 } else if (QUnit
.objectType(expected
) === "regexp") {
391 ok
= expected
.test(actual
);
392 // expected is a constructor
393 } else if (actual
instanceof expected
) {
395 // expected is a validation function which returns true is validation passed
396 } else if (expected
.call({}, actual
) === true) {
401 QUnit
.ok(ok
, message
);
404 start: function(count
) {
405 config
.semaphore
-= count
|| 1;
406 if (config
.semaphore
> 0) {
407 // don't start until equal number of stop-calls
410 if (config
.semaphore
< 0) {
411 // ignore if start is called more often then stop
412 config
.semaphore
= 0;
414 // A slight delay, to avoid any current callbacks
415 if ( defined
.setTimeout
) {
416 window
.setTimeout(function() {
417 if (config
.semaphore
> 0) {
420 if ( config
.timeout
) {
421 clearTimeout(config
.timeout
);
424 config
.blocking
= false;
428 config
.blocking
= false;
433 stop: function(count
) {
434 config
.semaphore
+= count
|| 1;
435 config
.blocking
= true;
437 if ( config
.testTimeout
&& defined
.setTimeout
) {
438 clearTimeout(config
.timeout
);
439 config
.timeout
= window
.setTimeout(function() {
440 QUnit
.ok( false, "Test timed out" );
441 config
.semaphore
= 1;
443 }, config
.testTimeout
);
448 //We want access to the constructor's prototype
453 //Make F QUnit's constructor so that we can add to the prototype later
454 QUnit
.constructor = F
;
457 // deprecated; still export them to window to provide clear error messages
458 // next step: remove entirely
459 QUnit
.equals = function() {
460 QUnit
.push(false, false, false, "QUnit.equals has been deprecated since 2009 (e88049a0), use QUnit.equal instead");
462 QUnit
.same = function() {
463 QUnit
.push(false, false, false, "QUnit.same has been deprecated since 2009 (e88049a0), use QUnit.deepEqual instead");
466 // Maintain internal state
468 // The queue of tests to run
471 // block until document ready
474 // when enabled, show only failing tests
475 // gets persisted through sessionStorage and can be changed in UI via checkbox
478 // by default, run previously failed tests first
479 // very useful in combination with "Hide passed tests" checked
482 // by default, modify document.title when suite is done
485 urlConfig
: ['noglobals', 'notrycatch'],
487 //logging callback queues
499 var location
= window
.location
|| { search
: "", protocol
: "file:" },
500 params
= location
.search
.slice( 1 ).split( "&" ),
501 length
= params
.length
,
506 for ( var i
= 0; i
< length
; i
++ ) {
507 current
= params
[ i
].split( "=" );
508 current
[ 0 ] = decodeURIComponent( current
[ 0 ] );
509 // allow just a key to turn on a flag, e.g., test.html?noglobals
510 current
[ 1 ] = current
[ 1 ] ? decodeURIComponent( current
[ 1 ] ) : true;
511 urlParams
[ current
[ 0 ] ] = current
[ 1 ];
515 QUnit
.urlParams
= urlParams
;
516 config
.filter
= urlParams
.filter
;
518 // Figure out if we're running the tests from a server or not
519 QUnit
.isLocal
= location
.protocol
=== 'file:';
522 // Expose the API as global variables, unless an 'exports'
523 // object exists, in that case we assume we're in CommonJS - export everything at the end
524 if ( typeof exports
=== "undefined" || typeof require
=== "undefined" ) {
525 extend(window
, QUnit
);
526 window
.QUnit
= QUnit
;
529 // define these after exposing globals to keep them in these QUnit namespace only
533 // Initialize the configuration options
536 stats
: { all
: 0, bad
: 0 },
537 moduleStats
: { all
: 0, bad
: 0 },
538 started
: +new Date(),
548 var qunit
= id( "qunit" );
551 '<h1 id="qunit-header">' + escapeInnerText( document
.title
) + '</h1>' +
552 '<h2 id="qunit-banner"></h2>' +
553 '<div id="qunit-testrunner-toolbar"></div>' +
554 '<h2 id="qunit-userAgent"></h2>' +
555 '<ol id="qunit-tests"></ol>';
558 var tests
= id( "qunit-tests" ),
559 banner
= id( "qunit-banner" ),
560 result
= id( "qunit-testresult" );
563 tests
.innerHTML
= "";
567 banner
.className
= "";
571 result
.parentNode
.removeChild( result
);
575 result
= document
.createElement( "p" );
576 result
.id
= "qunit-testresult";
577 result
.className
= "result";
578 tests
.parentNode
.insertBefore( result
, tests
);
579 result
.innerHTML
= 'Running...<br/> ';
583 // Resets the test setup. Useful for tests that modify the DOM.
584 // If jQuery is available, uses jQuery's html(), otherwise just innerHTML.
586 if ( window
.jQuery
) {
587 jQuery( "#qunit-fixture" ).html( config
.fixture
);
589 var main
= id( 'qunit-fixture' );
591 main
.innerHTML
= config
.fixture
;
596 // Trigger an event on an element.
597 // @example triggerEvent( document.body, "click" );
598 triggerEvent: function( elem
, type
, event
) {
599 if ( document
.createEvent
) {
600 event
= document
.createEvent("MouseEvents");
601 event
.initMouseEvent(type
, true, true, elem
.ownerDocument
.defaultView
,
602 0, 0, 0, 0, 0, false, false, false, false, 0, null);
603 elem
.dispatchEvent( event
);
605 } else if ( elem
.fireEvent
) {
606 elem
.fireEvent("on"+type
);
610 // Safe object type checking
611 is: function( type
, obj
) {
612 return QUnit
.objectType( obj
) == type
;
615 objectType: function( obj
) {
616 if (typeof obj
=== "undefined") {
619 // consider: typeof null === object
625 var type
= toString
.call( obj
).match(/^\[object\s(.*)\]$/)[1] || '';
639 return type
.toLowerCase();
641 if (typeof obj
=== "object") {
647 push: function(result
, actual
, expected
, message
) {
648 if (!config
.current
) {
649 throw new Error("assertion outside test context, was " + sourceFromStacktrace());
658 message
= escapeInnerText(message
) || (result
? "okay" : "failed");
659 message
= '<span class="test-message">' + message
+ "</span>";
660 var output
= message
;
662 expected
= escapeInnerText(QUnit
.jsDump
.parse(expected
));
663 actual
= escapeInnerText(QUnit
.jsDump
.parse(actual
));
664 output
+= '<table><tr class="test-expected"><th>Expected: </th><td><pre>' + expected
+ '</pre></td></tr>';
665 if (actual
!= expected
) {
666 output
+= '<tr class="test-actual"><th>Result: </th><td><pre>' + actual
+ '</pre></td></tr>';
667 output
+= '<tr class="test-diff"><th>Diff: </th><td><pre>' + QUnit
.diff(expected
, actual
) +'</pre></td></tr>';
669 var source
= sourceFromStacktrace();
671 details
.source
= source
;
672 output
+= '<tr class="test-source"><th>Source: </th><td><pre>' + escapeInnerText(source
) + '</pre></td></tr>';
674 output
+= "</table>";
677 runLoggingCallbacks( 'log', QUnit
, details
);
679 config
.current
.assertions
.push({
685 pushFailure: function(message
, source
) {
690 var output
= escapeInnerText(message
);
692 details
.source
= source
;
693 output
+= '<table><tr class="test-source"><th>Source: </th><td><pre>' + escapeInnerText(source
) + '</pre></td></tr></table>';
695 runLoggingCallbacks( 'log', QUnit
, details
);
696 config
.current
.assertions
.push({
702 url: function( params
) {
703 params
= extend( extend( {}, QUnit
.urlParams
), params
);
704 var querystring
= "?",
706 for ( key
in params
) {
707 if ( !hasOwn
.call( params
, key
) ) {
710 querystring
+= encodeURIComponent( key
) + "=" +
711 encodeURIComponent( params
[ key
] ) + "&";
713 return window
.location
.pathname
+ querystring
.slice( 0, -1 );
721 //QUnit.constructor is set to the empty F() above so that we can add to it's prototype later
722 //Doing this allows us to tell if the following methods have been overwritten on the actual
723 //QUnit object, which is a deprecated way of using the callbacks.
724 extend(QUnit
.constructor.prototype, {
725 // Logging callbacks; all receive a single argument with the listed properties
726 // run test/logs.html for any related changes
727 begin
: registerLoggingCallback('begin'),
728 // done: { failed, passed, total, runtime }
729 done
: registerLoggingCallback('done'),
730 // log: { result, actual, expected, message }
731 log
: registerLoggingCallback('log'),
732 // testStart: { name }
733 testStart
: registerLoggingCallback('testStart'),
734 // testDone: { name, failed, passed, total }
735 testDone
: registerLoggingCallback('testDone'),
736 // moduleStart: { name }
737 moduleStart
: registerLoggingCallback('moduleStart'),
738 // moduleDone: { name, failed, passed, total }
739 moduleDone
: registerLoggingCallback('moduleDone')
742 if ( typeof document
=== "undefined" || document
.readyState
=== "complete" ) {
743 config
.autorun
= true;
746 QUnit
.load = function() {
747 runLoggingCallbacks( 'begin', QUnit
, {} );
749 // Initialize the config, saving the execution queue
750 var oldconfig
= extend({}, config
);
752 extend(config
, oldconfig
);
754 config
.blocking
= false;
756 var urlConfigHtml
= '', len
= config
.urlConfig
.length
;
757 for ( var i
= 0, val
; i
< len
; i
++ ) {
758 val
= config
.urlConfig
[i
];
759 config
[val
] = QUnit
.urlParams
[val
];
760 urlConfigHtml
+= '<label><input name="' + val
+ '" type="checkbox"' + ( config
[val
] ? ' checked="checked"' : '' ) + '>' + val
+ '</label>';
763 var userAgent
= id("qunit-userAgent");
765 userAgent
.innerHTML
= navigator
.userAgent
;
767 var banner
= id("qunit-header");
769 banner
.innerHTML
= '<a href="' + QUnit
.url({ filter
: undefined }) + '"> ' + banner
.innerHTML
+ '</a> ' + urlConfigHtml
;
770 addEvent( banner
, "change", function( event
) {
772 params
[ event
.target
.name
] = event
.target
.checked
? true : undefined;
773 window
.location
= QUnit
.url( params
);
777 var toolbar
= id("qunit-testrunner-toolbar");
779 var filter
= document
.createElement("input");
780 filter
.type
= "checkbox";
781 filter
.id
= "qunit-filter-pass";
782 addEvent( filter
, "click", function() {
783 var ol
= document
.getElementById("qunit-tests");
784 if ( filter
.checked
) {
785 ol
.className
= ol
.className
+ " hidepass";
787 var tmp
= " " + ol
.className
.replace( /[\n\t\r]/g, " " ) + " ";
788 ol
.className
= tmp
.replace(/ hidepass
/, " ");
790 if ( defined
.sessionStorage
) {
791 if (filter
.checked
) {
792 sessionStorage
.setItem("qunit-filter-passed-tests", "true");
794 sessionStorage
.removeItem("qunit-filter-passed-tests");
798 if ( config
.hidepassed
|| defined
.sessionStorage
&& sessionStorage
.getItem("qunit-filter-passed-tests") ) {
799 filter
.checked
= true;
800 var ol
= document
.getElementById("qunit-tests");
801 ol
.className
= ol
.className
+ " hidepass";
803 toolbar
.appendChild( filter
);
805 var label
= document
.createElement("label");
806 label
.setAttribute("for", "qunit-filter-pass");
807 label
.innerHTML
= "Hide passed tests";
808 toolbar
.appendChild( label
);
811 var main
= id('qunit-fixture');
813 config
.fixture
= main
.innerHTML
;
816 if (config
.autostart
) {
821 addEvent(window
, "load", QUnit
.load
);
823 // addEvent(window, "error") gives us a useless event object
824 window
.onerror = function( message
, file
, line
) {
825 if ( QUnit
.config
.current
) {
826 QUnit
.pushFailure( message
, file
+ ":" + line
);
828 QUnit
.test( "global failure", function() {
829 QUnit
.pushFailure( message
, file
+ ":" + line
);
835 config
.autorun
= true;
837 // Log the last module results
838 if ( config
.currentModule
) {
839 runLoggingCallbacks( 'moduleDone', QUnit
, {
840 name
: config
.currentModule
,
841 failed
: config
.moduleStats
.bad
,
842 passed
: config
.moduleStats
.all
- config
.moduleStats
.bad
,
843 total
: config
.moduleStats
.all
847 var banner
= id("qunit-banner"),
848 tests
= id("qunit-tests"),
849 runtime
= +new Date() - config
.started
,
850 passed
= config
.stats
.all
- config
.stats
.bad
,
852 'Tests completed in ',
854 ' milliseconds.<br/>',
855 '<span class="passed">',
857 '</span> tests of <span class="total">',
859 '</span> passed, <span class="failed">',
865 banner
.className
= (config
.stats
.bad
? "qunit-fail" : "qunit-pass");
869 id( "qunit-testresult" ).innerHTML
= html
;
872 if ( config
.altertitle
&& typeof document
!== "undefined" && document
.title
) {
873 // show ✖ for good, ✔ for bad suite result in title
874 // use escape sequences in case file gets loaded with non-utf-8-charset
876 (config
.stats
.bad
? "\u2716" : "\u2714"),
877 document
.title
.replace(/^[\u2714\u2716] /i, "")
881 // clear own sessionStorage items if all tests passed
882 if ( config
.reorder
&& defined
.sessionStorage
&& config
.stats
.bad
=== 0 ) {
884 for ( var i
= 0; i
< sessionStorage
.length
; i
++ ) {
885 key
= sessionStorage
.key( i
++ );
886 if ( key
.indexOf("qunit-test-") === 0 ) {
887 sessionStorage
.removeItem( key
);
892 runLoggingCallbacks( 'done', QUnit
, {
893 failed
: config
.stats
.bad
,
895 total
: config
.stats
.all
,
900 function validTest( name
) {
901 var filter
= config
.filter
,
908 var not
= filter
.charAt( 0 ) === "!";
910 filter
= filter
.slice( 1 );
913 if ( name
.indexOf( filter
) !== -1 ) {
924 // so far supports only Firefox, Chrome and Opera (buggy), Safari (for real exceptions)
925 // Later Safari and IE10 are supposed to support error.stack as well
926 // See also https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error/Stack
927 function extractStacktrace( e
, offset
) {
928 offset
= offset
|| 3;
931 return e
.stacktrace
.split("\n")[offset
+ 3];
932 } else if (e
.stack
) {
934 var stack
= e
.stack
.split("\n");
935 if (/^error$/i.test(stack
[0])) {
938 return stack
[offset
];
939 } else if (e
.sourceURL
) {
941 // hopefully one day Safari provides actual stacktraces
942 // exclude useless self-reference for generated Error objects
943 if ( /qunit.js$/.test( e
.sourceURL
) ) {
946 // for actual exceptions, this is useful
947 return e
.sourceURL
+ ":" + e
.line
;
950 function sourceFromStacktrace(offset
) {
954 return extractStacktrace( e
, offset
);
958 function escapeInnerText(s
) {
963 return s
.replace(/[\&<>]/g, function(s
) {
965 case "&": return "&";
966 case "<": return "<";
967 case ">": return ">";
973 function synchronize( callback
, last
) {
974 config
.queue
.push( callback
);
976 if ( config
.autorun
&& !config
.blocking
) {
981 function process( last
) {
985 var start
= new Date().getTime();
986 config
.depth
= config
.depth
? config
.depth
+ 1 : 1;
988 while ( config
.queue
.length
&& !config
.blocking
) {
989 if ( !defined
.setTimeout
|| config
.updateRate
<= 0 || ( ( new Date().getTime() - start
) < config
.updateRate
) ) {
990 config
.queue
.shift()();
992 window
.setTimeout( next
, 13 );
997 if ( last
&& !config
.blocking
&& !config
.queue
.length
&& config
.depth
=== 0 ) {
1002 function saveGlobal() {
1003 config
.pollution
= [];
1005 if ( config
.noglobals
) {
1006 for ( var key
in window
) {
1007 if ( !hasOwn
.call( window
, key
) ) {
1010 config
.pollution
.push( key
);
1015 function checkPollution( name
) {
1016 var old
= config
.pollution
;
1019 var newGlobals
= diff( config
.pollution
, old
);
1020 if ( newGlobals
.length
> 0 ) {
1021 QUnit
.pushFailure( "Introduced global variable(s): " + newGlobals
.join(", ") );
1024 var deletedGlobals
= diff( old
, config
.pollution
);
1025 if ( deletedGlobals
.length
> 0 ) {
1026 QUnit
.pushFailure( "Deleted global variable(s): " + deletedGlobals
.join(", ") );
1030 // returns a new Array with the elements that are in a but not in b
1031 function diff( a
, b
) {
1032 var result
= a
.slice();
1033 for ( var i
= 0; i
< result
.length
; i
++ ) {
1034 for ( var j
= 0; j
< b
.length
; j
++ ) {
1035 if ( result
[i
] === b
[j
] ) {
1036 result
.splice(i
, 1);
1045 function extend(a
, b
) {
1046 for ( var prop
in b
) {
1047 if ( b
[prop
] === undefined ) {
1050 // Avoid "Member not found" error in IE8 caused by setting window.constructor
1051 } else if ( prop
!== "constructor" || a
!== window
) {
1059 function addEvent(elem
, type
, fn
) {
1060 if ( elem
.addEventListener
) {
1061 elem
.addEventListener( type
, fn
, false );
1062 } else if ( elem
.attachEvent
) {
1063 elem
.attachEvent( "on" + type
, fn
);
1070 return !!(typeof document
!== "undefined" && document
&& document
.getElementById
) &&
1071 document
.getElementById( name
);
1074 function registerLoggingCallback(key
){
1075 return function(callback
){
1076 config
[key
].push( callback
);
1080 // Supports deprecated method of completely overwriting logging callbacks
1081 function runLoggingCallbacks(key
, scope
, args
) {
1084 if ( QUnit
.hasOwnProperty(key
) ) {
1085 QUnit
[key
].call(scope
, args
);
1087 callbacks
= config
[key
];
1088 for( var i
= 0; i
< callbacks
.length
; i
++ ) {
1089 callbacks
[i
].call( scope
, args
);
1094 // Test for equality any JavaScript type.
1095 // Author: Philippe Rathé <prathe@gmail.com>
1096 QUnit
.equiv
= (function() {
1098 var innerEquiv
; // the real equiv function
1099 var callers
= []; // stack to decide between skip/abort functions
1100 var parents
= []; // stack to avoiding loops from circular referencing
1102 // Call the o related callback with the given arguments.
1103 function bindCallbacks(o
, callbacks
, args
) {
1104 var prop
= QUnit
.objectType(o
);
1106 if (QUnit
.objectType(callbacks
[prop
]) === "function") {
1107 return callbacks
[prop
].apply(callbacks
, args
);
1109 return callbacks
[prop
]; // or undefined
1114 var getProto
= Object
.getPrototypeOf
|| function (obj
) {
1115 return obj
.__proto__
;
1118 var callbacks
= (function () {
1120 // for string, boolean, number and null
1121 function useStrictEquality(b
, a
) {
1122 if (b
instanceof a
.constructor || a
instanceof b
.constructor) {
1123 // to catch short annotaion VS 'new' annotation of a
1126 // var j = new Number(1);
1134 "string" : useStrictEquality
,
1135 "boolean" : useStrictEquality
,
1136 "number" : useStrictEquality
,
1137 "null" : useStrictEquality
,
1138 "undefined" : useStrictEquality
,
1140 "nan" : function(b
) {
1144 "date" : function(b
, a
) {
1145 return QUnit
.objectType(b
) === "date" && a
.valueOf() === b
.valueOf();
1148 "regexp" : function(b
, a
) {
1149 return QUnit
.objectType(b
) === "regexp" &&
1151 a
.source
=== b
.source
&&
1153 a
.global
=== b
.global
&&
1155 a
.ignoreCase
=== b
.ignoreCase
&&
1156 a
.multiline
=== b
.multiline
;
1159 // - skip when the property is a method of an instance (OOP)
1160 // - abort otherwise,
1161 // initial === would have catch identical references anyway
1162 "function" : function() {
1163 var caller
= callers
[callers
.length
- 1];
1164 return caller
!== Object
&& typeof caller
!== "undefined";
1167 "array" : function(b
, a
) {
1171 // b could be an object literal here
1172 if (QUnit
.objectType(b
) !== "array") {
1177 if (len
!== b
.length
) { // safe and faster
1181 // track reference to avoid circular references
1183 for (i
= 0; i
< len
; i
++) {
1185 for (j
= 0; j
< parents
.length
; j
++) {
1186 if (parents
[j
] === a
[i
]) {
1187 loop
= true;// dont rewalk array
1190 if (!loop
&& !innerEquiv(a
[i
], b
[i
])) {
1199 "object" : function(b
, a
) {
1201 var eq
= true; // unless we can proove it
1202 var aProperties
= [], bProperties
= []; // collection of
1205 // comparing constructors is more strict than using
1207 if (a
.constructor !== b
.constructor) {
1208 // Allow objects with no prototype to be equivalent to
1209 // objects with Object as their constructor.
1210 if (!((getProto(a
) === null && getProto(b
) === Object
.prototype) ||
1211 (getProto(b
) === null && getProto(a
) === Object
.prototype)))
1217 // stack constructor before traversing properties
1218 callers
.push(a
.constructor);
1219 // track reference to avoid circular references
1222 for (i
in a
) { // be strict: don't ensures hasOwnProperty
1225 for (j
= 0; j
< parents
.length
; j
++) {
1226 if (parents
[j
] === a
[i
]) {
1227 // don't go down the same path twice
1231 aProperties
.push(i
); // collect a's properties
1233 if (!loop
&& !innerEquiv(a
[i
], b
[i
])) {
1239 callers
.pop(); // unstack, we are done
1243 bProperties
.push(i
); // collect b's properties
1246 // Ensures identical properties name
1247 return eq
&& innerEquiv(aProperties
.sort(), bProperties
.sort());
1252 innerEquiv = function() { // can take multiple arguments
1253 var args
= Array
.prototype.slice
.apply(arguments
);
1254 if (args
.length
< 2) {
1255 return true; // end transition
1258 return (function(a
, b
) {
1260 return true; // catch the most you can
1261 } else if (a
=== null || b
=== null || typeof a
=== "undefined" ||
1262 typeof b
=== "undefined" ||
1263 QUnit
.objectType(a
) !== QUnit
.objectType(b
)) {
1264 return false; // don't lose time with error prone cases
1266 return bindCallbacks(a
, callbacks
, [ b
, a
]);
1269 // apply transition with (1..n) arguments
1270 }(args
[0], args
[1]) && arguments
.callee
.apply(this, args
.splice(1, args
.length
- 1)));
1278 * jsDump Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com |
1279 * http://flesler.blogspot.com Licensed under BSD
1280 * (http://www.opensource.org/licenses/bsd-license.php) Date: 5/15/2008
1282 * @projectDescription Advanced and extensible data dumping for Javascript.
1284 * @author Ariel Flesler
1285 * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
1287 QUnit
.jsDump
= (function() {
1288 function quote( str
) {
1289 return '"' + str
.toString().replace(/"/g, '\\"') + '"';
1291 function literal( o ) {
1294 function join( pre, arr, post ) {
1295 var s = jsDump.separator(),
1296 base = jsDump.indent(),
1297 inner = jsDump.indent(1);
1299 arr = arr.join( ',' + s + inner );
1304 return [ pre, inner + arr, base + post ].join(s);
1306 function array( arr, stack ) {
1307 var i = arr.length, ret = new Array(i);
1310 ret[i] = this.parse( arr[i] , undefined , stack);
1313 return join( '[', ret, ']' );
1316 var reName = /^function (\w+)/;
1319 parse: function( obj, type, stack ) { //type is used mostly internally, you can fix a (custom)type in advance
1320 stack = stack || [ ];
1321 var parser = this.parsers[ type || this.typeOf(obj) ];
1322 type = typeof parser;
1323 var inStack = inArray(obj, stack);
1324 if (inStack != -1) {
1325 return 'recursion('+(inStack - stack.length)+')';
1328 if (type == 'function') {
1330 var res = parser.call( this, obj, stack );
1335 return (type == 'string') ? parser : this.parsers.error;
1337 typeOf: function( obj ) {
1339 if ( obj === null ) {
1341 } else if (typeof obj === "undefined") {
1343 } else if (QUnit.is("RegExp
", obj)) {
1345 } else if (QUnit.is("Date
", obj)) {
1347 } else if (QUnit.is("Function
", obj)) {
1349 } else if (typeof obj.setInterval !== undefined && typeof obj.document !== "undefined" && typeof obj.nodeType === "undefined") {
1351 } else if (obj.nodeType === 9) {
1353 } else if (obj.nodeType) {
1357 toString.call( obj ) === "[object Array
]" ||
1359 ( typeof obj.length === "number
" && typeof obj.item !== "undefined" && ( obj.length ? obj.item(0) === obj[0] : ( obj.item( 0 ) === null && typeof obj[0] === "undefined" ) ) )
1367 separator: function() {
1368 return this.multiline ? this.HTML ? '<br />' : '\n' : this.HTML ? ' ' : ' ';
1370 indent: function( extra ) {// extra can be a number, shortcut for increasing-calling-decreasing
1371 if ( !this.multiline ) {
1374 var chr = this.indentChar;
1376 chr = chr.replace(/\t/g,' ').replace(/ /g,' ');
1378 return new Array( this._depth_ + (extra||0) ).join(chr);
1381 this._depth_ += a || 1;
1383 down: function( a ) {
1384 this._depth_ -= a || 1;
1386 setParser: function( name, parser ) {
1387 this.parsers[name] = parser;
1389 // The next 3 are exposed so you can use them
1395 // This is the list of parsers, to modify them, use jsDump.setParser
1398 document: '[Document]',
1399 error: '[ERROR]', //when no parser is found, shouldn't happen
1400 unknown: '[Unknown]',
1402 'undefined': 'undefined',
1403 'function': function( fn ) {
1404 var ret = 'function',
1405 name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE
1411 ret = [ ret, QUnit.jsDump.parse( fn, 'functionArgs' ), '){'].join('');
1412 return join( ret, QUnit.jsDump.parse(fn,'functionCode'), '}' );
1417 object: function( map, stack ) {
1418 var ret = [ ], keys, key, val, i;
1421 keys = Object.keys( map );
1424 for (key in map) { keys.push( key ); }
1427 for (i = 0; i < keys.length; i++) {
1430 ret.push( QUnit.jsDump.parse( key, 'key' ) + ': ' + QUnit.jsDump.parse( val, undefined, stack ) );
1432 QUnit.jsDump.down();
1433 return join( '{', ret, '}' );
1435 node: function( node ) {
1436 var open = QUnit.jsDump.HTML ? '<' : '<',
1437 close = QUnit.jsDump.HTML ? '>' : '>';
1439 var tag = node.nodeName.toLowerCase(),
1442 for ( var a in QUnit.jsDump.DOMAttrs ) {
1443 var val = node[QUnit.jsDump.DOMAttrs[a]];
1445 ret += ' ' + a + '=' + QUnit.jsDump.parse( val, 'attribute' );
1448 return ret + close + open + '/' + tag + close;
1450 functionArgs: function( fn ) {//function calls it internally, it's the arguments part of the function
1456 var args = new Array(l);
1458 args[l] = String.fromCharCode(97+l);//97 is 'a'
1460 return ' ' + args.join(', ') + ' ';
1462 key: quote, //object calls it internally, the key part of an item in a map
1463 functionCode: '[code]', //function calls it internally, it's the content of the function
1464 attribute: quote, //node calls it internally, it's an html attribute value
1467 regexp: literal, //regex
1471 DOMAttrs:{//attributes to dump from nodes, name=>realName
1476 HTML:false,//if true, entities are escaped ( <, >, \t, space and \n )
1477 indentChar:' ',//indentation unit
1478 multiline:true //if true, items in a collection, are separated by a \n, else just a space.
1485 function getText( elems ) {
1488 for ( var i = 0; elems[i]; i++ ) {
1491 // Get the text from text nodes and CDATA nodes
1492 if ( elem.nodeType === 3 || elem.nodeType === 4 ) {
1493 ret += elem.nodeValue;
1495 // Traverse everything else, except comment nodes
1496 } else if ( elem.nodeType !== 8 ) {
1497 ret += getText( elem.childNodes );
1505 function inArray( elem, array ) {
1506 if ( array.indexOf ) {
1507 return array.indexOf( elem );
1510 for ( var i = 0, length = array.length; i < length; i++ ) {
1511 if ( array[ i ] === elem ) {
1520 * Javascript Diff Algorithm
1521 * By John Resig (http://ejohn.org/)
1522 * Modified by Chu Alan "sprite
"
1524 * Released under the MIT license.
1527 * http://ejohn.org/projects/javascript-diff-algorithm/
1529 * Usage: QUnit.diff(expected, actual)
1531 * QUnit.diff("the quick brown fox jumped over
", "the quick fox jumps over
") == "the quick
<del
>brown
</del> fox <del>jumped </del><ins
>jumps
</ins
> over
"
1533 QUnit.diff = (function() {
1534 function diff(o, n) {
1539 for (i = 0; i < n.length; i++) {
1540 if (ns[n[i]] == null) {
1546 ns[n[i]].rows.push(i);
1549 for (i = 0; i < o.length; i++) {
1550 if (os[o[i]] == null) {
1556 os[o[i]].rows.push(i);
1560 if ( !hasOwn.call( ns, i ) ) {
1563 if (ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1) {
1564 n[ns[i].rows[0]] = {
1565 text: n[ns[i].rows[0]],
1568 o[os[i].rows[0]] = {
1569 text: o[os[i].rows[0]],
1575 for (i = 0; i < n.length - 1; i++) {
1576 if (n[i].text != null && n[i + 1].text == null && n[i].row + 1 < o.length && o[n[i].row + 1].text == null &&
1577 n[i + 1] == o[n[i].row + 1]) {
1583 text: o[n[i].row + 1],
1589 for (i = n.length - 1; i > 0; i--) {
1590 if (n[i].text != null && n[i - 1].text == null && n[i].row > 0 && o[n[i].row - 1].text == null &&
1591 n[i - 1] == o[n[i].row - 1]) {
1597 text: o[n[i].row - 1],
1609 return function(o, n) {
1610 o = o.replace(/\s+$/, '');
1611 n = n.replace(/\s+$/, '');
1612 var out = diff(o === "" ? [] : o.split(/\s+/), n === "" ? [] : n.split(/\s+/));
1617 var oSpace = o.match(/\s+/g);
1618 if (oSpace == null) {
1624 var nSpace = n.match(/\s+/g);
1625 if (nSpace == null) {
1632 if (out.n.length === 0) {
1633 for (i = 0; i < out.o.length; i++) {
1634 str += '<del>' + out.o[i] + oSpace[i] + "</del
>";
1638 if (out.n[0].text == null) {
1639 for (n = 0; n < out.o.length && out.o[n].text == null; n++) {
1640 str += '<del>' + out.o[n] + oSpace[n] + "</del
>";
1644 for (i = 0; i < out.n.length; i++) {
1645 if (out.n[i].text == null) {
1646 str += '<ins>' + out.n[i] + nSpace[i] + "</ins
>";
1651 for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++) {
1652 pre += '<del>' + out.o[n] + oSpace[n] + "</del
>";
1654 str += " " + out.n[i].text + nSpace[i] + pre;
1663 // for CommonJS enviroments, export everything
1664 if ( typeof exports !== "undefined" || typeof require !== "undefined" ) {
1665 extend(exports, QUnit);
1668 // get at whatever the global object is, like window in browsers
1669 }( (function() {return this;}.call()) ));