2 * QUnit v1.8.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.
17 fileName
= (sourceFromStacktrace( 0 ) || "" ).replace(/(:\d+)+\)?/, "").replace(/.+\//, ""),
18 toString
= Object
.prototype.toString
,
19 hasOwn
= Object
.prototype.hasOwnProperty
,
21 setTimeout
: typeof window
.setTimeout
!== "undefined",
22 sessionStorage
: (function() {
23 var x
= "qunit-test-string";
25 sessionStorage
.setItem( x
, x
);
26 sessionStorage
.removeItem( x
);
34 function Test( settings
) {
35 extend( this, settings
);
37 this.testNumber
= ++Test
.count
;
45 tests
= id( "qunit-tests" );
48 b
= document
.createElement( "strong" );
49 b
.innerHTML
= this.name
;
51 // `a` initialized at top of scope
52 a
= document
.createElement( "a" );
53 a
.innerHTML
= "Rerun";
54 a
.href
= QUnit
.url({ testNumber
: this.testNumber
});
56 li
= document
.createElement( "li" );
59 li
.className
= "running";
60 li
.id
= this.id
= "qunit-test-output" + testId
++;
62 tests
.appendChild( li
);
66 if ( this.module
!== config
.previousModule
) {
67 if ( config
.previousModule
) {
68 runLoggingCallbacks( "moduleDone", QUnit
, {
69 name
: config
.previousModule
,
70 failed
: config
.moduleStats
.bad
,
71 passed
: config
.moduleStats
.all
- config
.moduleStats
.bad
,
72 total
: config
.moduleStats
.all
75 config
.previousModule
= this.module
;
76 config
.moduleStats
= { all
: 0, bad
: 0 };
77 runLoggingCallbacks( "moduleStart", QUnit
, {
80 } else if ( config
.autorun
) {
81 runLoggingCallbacks( "moduleStart", QUnit
, {
86 config
.current
= this;
88 this.testEnvironment
= extend({
90 teardown: function() {}
91 }, this.moduleTestEnvironment
);
93 runLoggingCallbacks( "testStart", QUnit
, {
98 // allow utility functions to access the current test environment
100 QUnit
.current_testEnvironment
= this.testEnvironment
;
102 if ( !config
.pollution
) {
105 if ( config
.notrycatch
) {
106 this.testEnvironment
.setup
.call( this.testEnvironment
);
110 this.testEnvironment
.setup
.call( this.testEnvironment
);
112 QUnit
.pushFailure( "Setup failed on " + this.testName
+ ": " + e
.message
, extractStacktrace( e
, 1 ) );
116 config
.current
= this;
118 var running
= id( "qunit-testresult" );
121 running
.innerHTML
= "Running: <br/>" + this.name
;
128 if ( config
.notrycatch
) {
129 this.callback
.call( this.testEnvironment
, QUnit
.assert
);
134 this.callback
.call( this.testEnvironment
, QUnit
.assert
);
136 QUnit
.pushFailure( "Died on test #" + (this.assertions
.length
+ 1) + " " + this.stack
+ ": " + e
.message
, extractStacktrace( e
, 0 ) );
137 // else next test will carry the responsibility
140 // Restart the tests if they're blocking
141 if ( config
.blocking
) {
146 teardown: function() {
147 config
.current
= this;
148 if ( config
.notrycatch
) {
149 this.testEnvironment
.teardown
.call( this.testEnvironment
);
153 this.testEnvironment
.teardown
.call( this.testEnvironment
);
155 QUnit
.pushFailure( "Teardown failed on " + this.testName
+ ": " + e
.message
, extractStacktrace( e
, 1 ) );
161 config
.current
= this;
162 if ( config
.requireExpects
&& this.expected
== null ) {
163 QUnit
.pushFailure( "Expected number of assertions to be defined, but expect() was not called.", this.stack
);
164 } else if ( this.expected
!= null && this.expected
!= this.assertions
.length
) {
165 QUnit
.pushFailure( "Expected " + this.expected
+ " assertions, but " + this.assertions
.length
+ " were run", this.stack
);
166 } else if ( this.expected
== null && !this.assertions
.length
) {
167 QUnit
.pushFailure( "Expected at least one assertion, but none were run - call expect(0) to accept zero assertions.", this.stack
);
170 var assertion
, a
, b
, i
, li
, ol
,
174 tests
= id( "qunit-tests" );
176 config
.stats
.all
+= this.assertions
.length
;
177 config
.moduleStats
.all
+= this.assertions
.length
;
180 ol
= document
.createElement( "ol" );
182 for ( i
= 0; i
< this.assertions
.length
; i
++ ) {
183 assertion
= this.assertions
[i
];
185 li
= document
.createElement( "li" );
186 li
.className
= assertion
.result
? "pass" : "fail";
187 li
.innerHTML
= assertion
.message
|| ( assertion
.result
? "okay" : "failed" );
188 ol
.appendChild( li
);
190 if ( assertion
.result
) {
195 config
.moduleStats
.bad
++;
199 // store result when possible
200 if ( QUnit
.config
.reorder
&& defined
.sessionStorage
) {
202 sessionStorage
.setItem( "qunit-test-" + this.module
+ "-" + this.testName
, bad
);
204 sessionStorage
.removeItem( "qunit-test-" + this.module
+ "-" + this.testName
);
209 ol
.style
.display
= "none";
212 // `b` initialized at top of scope
213 b
= document
.createElement( "strong" );
214 b
.innerHTML
= this.name
+ " <b class='counts'>(<b class='failed'>" + bad
+ "</b>, <b class='passed'>" + good
+ "</b>, " + this.assertions
.length
+ ")</b>";
216 addEvent(b
, "click", function() {
217 var next
= b
.nextSibling
.nextSibling
,
218 display
= next
.style
.display
;
219 next
.style
.display
= display
=== "none" ? "block" : "none";
222 addEvent(b
, "dblclick", function( e
) {
223 var target
= e
&& e
.target
? e
.target
: window
.event
.srcElement
;
224 if ( target
.nodeName
.toLowerCase() == "span" || target
.nodeName
.toLowerCase() == "b" ) {
225 target
= target
.parentNode
;
227 if ( window
.location
&& target
.nodeName
.toLowerCase() === "strong" ) {
228 window
.location
= QUnit
.url({ testNumber
: test
.testNumber
});
232 // `li` initialized at top of scope
234 li
.className
= bad
? "fail" : "pass";
235 li
.removeChild( li
.firstChild
);
238 li
.appendChild ( a
);
239 li
.appendChild( ol
);
242 for ( i
= 0; i
< this.assertions
.length
; i
++ ) {
243 if ( !this.assertions
[i
].result
) {
246 config
.moduleStats
.bad
++;
251 runLoggingCallbacks( "testDone", QUnit
, {
255 passed
: this.assertions
.length
- bad
,
256 total
: this.assertions
.length
261 config
.current
= undefined;
268 synchronize(function() {
272 // each of these can by async
273 synchronize(function() {
276 synchronize(function() {
279 synchronize(function() {
282 synchronize(function() {
287 // `bad` initialized at top of scope
288 // defer when previous test run passed, if storage is available
289 bad
= QUnit
.config
.reorder
&& defined
.sessionStorage
&&
290 +sessionStorage
.getItem( "qunit-test-" + this.module
+ "-" + this.testName
);
295 synchronize( run
, true );
300 // Root QUnit object.
301 // `QUnit` initialized at top of scope
304 // call on start of module test to prepend name to all tests
305 module: function( name
, testEnvironment
) {
306 config
.currentModule
= name
;
307 config
.currentModuleTestEnviroment
= testEnvironment
;
310 asyncTest: function( testName
, expected
, callback
) {
311 if ( arguments
.length
=== 2 ) {
316 QUnit
.test( testName
, expected
, callback
, true );
319 test: function( testName
, expected
, callback
, async
) {
321 name
= "<span class='test-name'>" + escapeInnerText( testName
) + "</span>";
323 if ( arguments
.length
=== 2 ) {
328 if ( config
.currentModule
) {
329 name
= "<span class='module-name'>" + config
.currentModule
+ "</span>: " + name
;
338 module
: config
.currentModule
,
339 moduleTestEnvironment
: config
.currentModuleTestEnviroment
,
340 stack
: sourceFromStacktrace( 2 )
343 if ( !validTest( test
) ) {
350 // Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
351 expect: function( asserts
) {
352 config
.current
.expected
= asserts
;
355 start: function( count
) {
356 config
.semaphore
-= count
|| 1;
357 // don't start until equal number of stop-calls
358 if ( config
.semaphore
> 0 ) {
361 // ignore if start is called more often then stop
362 if ( config
.semaphore
< 0 ) {
363 config
.semaphore
= 0;
365 // A slight delay, to avoid any current callbacks
366 if ( defined
.setTimeout
) {
367 window
.setTimeout(function() {
368 if ( config
.semaphore
> 0 ) {
371 if ( config
.timeout
) {
372 clearTimeout( config
.timeout
);
375 config
.blocking
= false;
379 config
.blocking
= false;
384 stop: function( count
) {
385 config
.semaphore
+= count
|| 1;
386 config
.blocking
= true;
388 if ( config
.testTimeout
&& defined
.setTimeout
) {
389 clearTimeout( config
.timeout
);
390 config
.timeout
= window
.setTimeout(function() {
391 QUnit
.ok( false, "Test timed out" );
392 config
.semaphore
= 1;
394 }, config
.testTimeout
);
400 // All of these must call either QUnit.push() or manually do:
401 // - runLoggingCallbacks( "log", .. );
402 // - config.current.assertions.push({ .. });
405 * Asserts rough true-ish result.
406 * @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" );
408 ok: function( result
, msg
) {
409 if ( !config
.current
) {
410 throw new Error( "ok() assertion outside test context, was " + sourceFromStacktrace(2) );
420 msg
= escapeInnerText( msg
|| (result
? "okay" : "failed" ) );
421 msg
= "<span class='test-message'>" + msg
+ "</span>";
424 source
= sourceFromStacktrace( 2 );
426 details
.source
= source
;
427 msg
+= "<table><tr class='test-source'><th>Source: </th><td><pre>" + escapeInnerText( source
) + "</pre></td></tr></table>";
430 runLoggingCallbacks( "log", QUnit
, details
);
431 config
.current
.assertions
.push({
438 * Assert that the first two arguments are equal, with an optional message.
439 * Prints out both actual and expected values.
440 * @example equal( format( "Received {0} bytes.", 2), "Received 2 bytes.", "format() replaces {0} with next argument" );
442 equal: function( actual
, expected
, message
) {
443 QUnit
.push( expected
== actual
, actual
, expected
, message
);
446 notEqual: function( actual
, expected
, message
) {
447 QUnit
.push( expected
!= actual
, actual
, expected
, message
);
450 deepEqual: function( actual
, expected
, message
) {
451 QUnit
.push( QUnit
.equiv(actual
, expected
), actual
, expected
, message
);
454 notDeepEqual: function( actual
, expected
, message
) {
455 QUnit
.push( !QUnit
.equiv(actual
, expected
), actual
, expected
, message
);
458 strictEqual: function( actual
, expected
, message
) {
459 QUnit
.push( expected
=== actual
, actual
, expected
, message
);
462 notStrictEqual: function( actual
, expected
, message
) {
463 QUnit
.push( expected
!== actual
, actual
, expected
, message
);
466 raises: function( block
, expected
, message
) {
470 if ( typeof expected
=== "string" ) {
475 config
.current
.ignoreGlobalErrors
= true;
477 block
.call( config
.current
.testEnvironment
);
481 config
.current
.ignoreGlobalErrors
= false;
484 // we don't want to validate thrown error
487 // expected is a regexp
488 } else if ( QUnit
.objectType( expected
) === "regexp" ) {
489 ok
= expected
.test( actual
);
490 // expected is a constructor
491 } else if ( actual
instanceof expected
) {
493 // expected is a validation function which returns true is validation passed
494 } else if ( expected
.call( {}, actual
) === true ) {
499 QUnit
.push( ok
, actual
, null, message
);
503 // @deprecated: Kept assertion helpers in root for backwards compatibility
504 extend( QUnit
, QUnit
.assert
);
507 * @deprecated: Kept for backwards compatibility
508 * next step: remove entirely
510 QUnit
.equals = function() {
511 QUnit
.push( false, false, false, "QUnit.equals has been deprecated since 2009 (e88049a0), use QUnit.equal instead" );
513 QUnit
.same = function() {
514 QUnit
.push( false, false, false, "QUnit.same has been deprecated since 2009 (e88049a0), use QUnit.deepEqual instead" );
517 // We want access to the constructor's prototype
522 // Make F QUnit's constructor so that we can add to the prototype later
523 QUnit
.constructor = F
;
527 * Config object: Maintain internal state
528 * Later exposed as QUnit.config
529 * `config` initialized at top of scope
532 // The queue of tests to run
535 // block until document ready
538 // when enabled, show only failing tests
539 // gets persisted through sessionStorage and can be changed in UI via checkbox
542 // by default, run previously failed tests first
543 // very useful in combination with "Hide passed tests" checked
546 // by default, modify document.title when suite is done
549 // when enabled, all tests must call expect()
550 requireExpects
: false,
552 urlConfig
: [ "noglobals", "notrycatch" ],
554 // logging callback queues
564 // Initialize more QUnit.config and QUnit.urlParams
567 location
= window
.location
|| { search
: "", protocol
: "file:" },
568 params
= location
.search
.slice( 1 ).split( "&" ),
569 length
= params
.length
,
574 for ( i
= 0; i
< length
; i
++ ) {
575 current
= params
[ i
].split( "=" );
576 current
[ 0 ] = decodeURIComponent( current
[ 0 ] );
577 // allow just a key to turn on a flag, e.g., test.html?noglobals
578 current
[ 1 ] = current
[ 1 ] ? decodeURIComponent( current
[ 1 ] ) : true;
579 urlParams
[ current
[ 0 ] ] = current
[ 1 ];
583 QUnit
.urlParams
= urlParams
;
585 // String search anywhere in moduleName+testName
586 config
.filter
= urlParams
.filter
;
588 // Exact match of the module name
589 config
.module
= urlParams
.module
;
591 config
.testNumber
= parseInt( urlParams
.testNumber
, 10 ) || null;
593 // Figure out if we're running the tests from a server or not
594 QUnit
.isLocal
= location
.protocol
=== "file:";
597 // Export global variables, unless an 'exports' object exists,
598 // in that case we assume we're in CommonJS (dealt with on the bottom of the script)
599 if ( typeof exports
=== "undefined" ) {
600 extend( window
, QUnit
);
602 // Expose QUnit object
603 window
.QUnit
= QUnit
;
606 // Extend QUnit object,
607 // these after set here because they should not be exposed as global functions
611 // Initialize the configuration options
614 stats
: { all
: 0, bad
: 0 },
615 moduleStats
: { all
: 0, bad
: 0 },
616 started
: +new Date(),
626 var tests
, banner
, result
,
627 qunit
= id( "qunit" );
631 "<h1 id='qunit-header'>" + escapeInnerText( document
.title
) + "</h1>" +
632 "<h2 id='qunit-banner'></h2>" +
633 "<div id='qunit-testrunner-toolbar'></div>" +
634 "<h2 id='qunit-userAgent'></h2>" +
635 "<ol id='qunit-tests'></ol>";
638 tests
= id( "qunit-tests" );
639 banner
= id( "qunit-banner" );
640 result
= id( "qunit-testresult" );
643 tests
.innerHTML
= "";
647 banner
.className
= "";
651 result
.parentNode
.removeChild( result
);
655 result
= document
.createElement( "p" );
656 result
.id
= "qunit-testresult";
657 result
.className
= "result";
658 tests
.parentNode
.insertBefore( result
, tests
);
659 result
.innerHTML
= "Running...<br/> ";
663 // Resets the test setup. Useful for tests that modify the DOM.
664 // If jQuery is available, uses jQuery's html(), otherwise just innerHTML.
668 if ( window
.jQuery
) {
669 jQuery( "#qunit-fixture" ).html( config
.fixture
);
671 fixture
= id( "qunit-fixture" );
673 fixture
.innerHTML
= config
.fixture
;
678 // Trigger an event on an element.
679 // @example triggerEvent( document.body, "click" );
680 triggerEvent: function( elem
, type
, event
) {
681 if ( document
.createEvent
) {
682 event
= document
.createEvent( "MouseEvents" );
683 event
.initMouseEvent(type
, true, true, elem
.ownerDocument
.defaultView
,
684 0, 0, 0, 0, 0, false, false, false, false, 0, null);
686 elem
.dispatchEvent( event
);
687 } else if ( elem
.fireEvent
) {
688 elem
.fireEvent( "on" + type
);
692 // Safe object type checking
693 is: function( type
, obj
) {
694 return QUnit
.objectType( obj
) == type
;
697 objectType: function( obj
) {
698 if ( typeof obj
=== "undefined" ) {
700 // consider: typeof null === object
702 if ( obj
=== null ) {
706 var type
= toString
.call( obj
).match(/^\[object\s(.*)\]$/)[1] || "";
720 return type
.toLowerCase();
722 if ( typeof obj
=== "object" ) {
728 push: function( result
, actual
, expected
, message
) {
729 if ( !config
.current
) {
730 throw new Error( "assertion outside test context, was " + sourceFromStacktrace() );
741 message
= escapeInnerText( message
) || ( result
? "okay" : "failed" );
742 message
= "<span class='test-message'>" + message
+ "</span>";
746 expected
= escapeInnerText( QUnit
.jsDump
.parse(expected
) );
747 actual
= escapeInnerText( QUnit
.jsDump
.parse(actual
) );
748 output
+= "<table><tr class='test-expected'><th>Expected: </th><td><pre>" + expected
+ "</pre></td></tr>";
750 if ( actual
!= expected
) {
751 output
+= "<tr class='test-actual'><th>Result: </th><td><pre>" + actual
+ "</pre></td></tr>";
752 output
+= "<tr class='test-diff'><th>Diff: </th><td><pre>" + QUnit
.diff( expected
, actual
) + "</pre></td></tr>";
755 source
= sourceFromStacktrace();
758 details
.source
= source
;
759 output
+= "<tr class='test-source'><th>Source: </th><td><pre>" + escapeInnerText( source
) + "</pre></td></tr>";
762 output
+= "</table>";
765 runLoggingCallbacks( "log", QUnit
, details
);
767 config
.current
.assertions
.push({
773 pushFailure: function( message
, source
) {
774 if ( !config
.current
) {
775 throw new Error( "pushFailure() assertion outside test context, was " + sourceFromStacktrace(2) );
784 message
= escapeInnerText(message
) || "error";
785 message
= "<span class='test-message'>" + message
+ "</span>";
789 details
.source
= source
;
790 output
+= "<table><tr class='test-source'><th>Source: </th><td><pre>" + escapeInnerText( source
) + "</pre></td></tr></table>";
793 runLoggingCallbacks( "log", QUnit
, details
);
795 config
.current
.assertions
.push({
801 url: function( params
) {
802 params
= extend( extend( {}, QUnit
.urlParams
), params
);
806 for ( key
in params
) {
807 if ( !hasOwn
.call( params
, key
) ) {
810 querystring
+= encodeURIComponent( key
) + "=" +
811 encodeURIComponent( params
[ key
] ) + "&";
813 return window
.location
.pathname
+ querystring
.slice( 0, -1 );
819 // load, equiv, jsDump, diff: Attached later
823 * @deprecated: Created for backwards compatibility with test runner that set the hook function
824 * into QUnit.{hook}, instead of invoking it and passing the hook function.
825 * QUnit.constructor is set to the empty F() above so that we can add to it's prototype here.
826 * Doing this allows us to tell if the following methods have been overwritten on the actual
829 extend( QUnit
.constructor.prototype, {
831 // Logging callbacks; all receive a single argument with the listed properties
832 // run test/logs.html for any related changes
833 begin
: registerLoggingCallback( "begin" ),
835 // done: { failed, passed, total, runtime }
836 done
: registerLoggingCallback( "done" ),
838 // log: { result, actual, expected, message }
839 log
: registerLoggingCallback( "log" ),
841 // testStart: { name }
842 testStart
: registerLoggingCallback( "testStart" ),
844 // testDone: { name, failed, passed, total }
845 testDone
: registerLoggingCallback( "testDone" ),
847 // moduleStart: { name }
848 moduleStart
: registerLoggingCallback( "moduleStart" ),
850 // moduleDone: { name, failed, passed, total }
851 moduleDone
: registerLoggingCallback( "moduleDone" )
854 if ( typeof document
=== "undefined" || document
.readyState
=== "complete" ) {
855 config
.autorun
= true;
858 QUnit
.load = function() {
859 runLoggingCallbacks( "begin", QUnit
, {} );
861 // Initialize the config, saving the execution queue
862 var banner
, filter
, i
, label
, len
, main
, ol
, toolbar
, userAgent
, val
,
864 oldconfig
= extend( {}, config
);
867 extend(config
, oldconfig
);
869 config
.blocking
= false;
871 len
= config
.urlConfig
.length
;
873 for ( i
= 0; i
< len
; i
++ ) {
874 val
= config
.urlConfig
[i
];
875 config
[val
] = QUnit
.urlParams
[val
];
876 urlConfigHtml
+= "<label><input name='" + val
+ "' type='checkbox'" + ( config
[val
] ? " checked='checked'" : "" ) + ">" + val
+ "</label>";
879 // `userAgent` initialized at top of scope
880 userAgent
= id( "qunit-userAgent" );
882 userAgent
.innerHTML
= navigator
.userAgent
;
885 // `banner` initialized at top of scope
886 banner
= id( "qunit-header" );
888 banner
.innerHTML
= "<a href='" + QUnit
.url({ filter
: undefined }) + "'>" + banner
.innerHTML
+ "</a> " + urlConfigHtml
;
889 addEvent( banner
, "change", function( event
) {
891 params
[ event
.target
.name
] = event
.target
.checked
? true : undefined;
892 window
.location
= QUnit
.url( params
);
896 // `toolbar` initialized at top of scope
897 toolbar
= id( "qunit-testrunner-toolbar" );
899 // `filter` initialized at top of scope
900 filter
= document
.createElement( "input" );
901 filter
.type
= "checkbox";
902 filter
.id
= "qunit-filter-pass";
904 addEvent( filter
, "click", function() {
906 ol
= document
.getElementById( "qunit-tests" );
908 if ( filter
.checked
) {
909 ol
.className
= ol
.className
+ " hidepass";
911 tmp
= " " + ol
.className
.replace( /[\n\t\r]/g, " " ) + " ";
912 ol
.className
= tmp
.replace( / hidepass
/, " " );
914 if ( defined
.sessionStorage
) {
915 if (filter
.checked
) {
916 sessionStorage
.setItem( "qunit-filter-passed-tests", "true" );
918 sessionStorage
.removeItem( "qunit-filter-passed-tests" );
923 if ( config
.hidepassed
|| defined
.sessionStorage
&& sessionStorage
.getItem( "qunit-filter-passed-tests" ) ) {
924 filter
.checked
= true;
925 // `ol` initialized at top of scope
926 ol
= document
.getElementById( "qunit-tests" );
927 ol
.className
= ol
.className
+ " hidepass";
929 toolbar
.appendChild( filter
);
931 // `label` initialized at top of scope
932 label
= document
.createElement( "label" );
933 label
.setAttribute( "for", "qunit-filter-pass" );
934 label
.innerHTML
= "Hide passed tests";
935 toolbar
.appendChild( label
);
938 // `main` initialized at top of scope
939 main
= id( "qunit-fixture" );
941 config
.fixture
= main
.innerHTML
;
944 if ( config
.autostart
) {
949 addEvent( window
, "load", QUnit
.load
);
951 // `onErrorFnPrev` initialized at top of scope
952 // Preserve other handlers
953 onErrorFnPrev
= window
.onerror
;
955 // Cover uncaught exceptions
956 // Returning true will surpress the default browser handler,
957 // returning false will let it run.
958 window
.onerror = function ( error
, filePath
, linerNr
) {
960 if ( onErrorFnPrev
) {
961 ret
= onErrorFnPrev( error
, filePath
, linerNr
);
964 // Treat return value as window.onerror itself does,
965 // Only do our handling if not surpressed.
966 if ( ret
!== true ) {
967 if ( QUnit
.config
.current
) {
968 if ( QUnit
.config
.current
.ignoreGlobalErrors
) {
971 QUnit
.pushFailure( error
, filePath
+ ":" + linerNr
);
973 QUnit
.test( "global failure", function() {
974 QUnit
.pushFailure( error
, filePath
+ ":" + linerNr
);
984 config
.autorun
= true;
986 // Log the last module results
987 if ( config
.currentModule
) {
988 runLoggingCallbacks( "moduleDone", QUnit
, {
989 name
: config
.currentModule
,
990 failed
: config
.moduleStats
.bad
,
991 passed
: config
.moduleStats
.all
- config
.moduleStats
.bad
,
992 total
: config
.moduleStats
.all
997 banner
= id( "qunit-banner" ),
998 tests
= id( "qunit-tests" ),
999 runtime
= +new Date() - config
.started
,
1000 passed
= config
.stats
.all
- config
.stats
.bad
,
1002 "Tests completed in ",
1004 " milliseconds.<br/>",
1005 "<span class='passed'>",
1007 "</span> tests of <span class='total'>",
1009 "</span> passed, <span class='failed'>",
1015 banner
.className
= ( config
.stats
.bad
? "qunit-fail" : "qunit-pass" );
1019 id( "qunit-testresult" ).innerHTML
= html
;
1022 if ( config
.altertitle
&& typeof document
!== "undefined" && document
.title
) {
1023 // show ✖ for good, ✔ for bad suite result in title
1024 // use escape sequences in case file gets loaded with non-utf-8-charset
1026 ( config
.stats
.bad
? "\u2716" : "\u2714" ),
1027 document
.title
.replace( /^[\u2714\u2716] /i, "" )
1031 // clear own sessionStorage items if all tests passed
1032 if ( config
.reorder
&& defined
.sessionStorage
&& config
.stats
.bad
=== 0 ) {
1033 // `key` & `i` initialized at top of scope
1034 for ( i
= 0; i
< sessionStorage
.length
; i
++ ) {
1035 key
= sessionStorage
.key( i
++ );
1036 if ( key
.indexOf( "qunit-test-" ) === 0 ) {
1037 sessionStorage
.removeItem( key
);
1042 runLoggingCallbacks( "done", QUnit
, {
1043 failed
: config
.stats
.bad
,
1045 total
: config
.stats
.all
,
1050 /** @return Boolean: true if this test should be ran */
1051 function validTest( test
) {
1053 filter
= config
.filter
&& config
.filter
.toLowerCase(),
1054 module
= config
.module
,
1055 fullName
= (test
.module
+ ": " + test
.testName
).toLowerCase();
1057 if ( config
.testNumber
) {
1058 return test
.testNumber
=== config
.testNumber
;
1061 if ( module
&& test
.module
!== module
) {
1069 include
= filter
.charAt( 0 ) !== "!";
1071 filter
= filter
.slice( 1 );
1074 // If the filter matches, we need to honour include
1075 if ( fullName
.indexOf( filter
) !== -1 ) {
1079 // Otherwise, do the opposite
1083 // so far supports only Firefox, Chrome and Opera (buggy), Safari (for real exceptions)
1084 // Later Safari and IE10 are supposed to support error.stack as well
1085 // See also https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error/Stack
1086 function extractStacktrace( e
, offset
) {
1087 offset
= offset
=== undefined ? 3 : offset
;
1089 var stack
, include
, i
, regex
;
1091 if ( e
.stacktrace
) {
1093 return e
.stacktrace
.split( "\n" )[ offset
+ 3 ];
1094 } else if ( e
.stack
) {
1096 stack
= e
.stack
.split( "\n" );
1097 if (/^error$/i.test( stack
[0] ) ) {
1102 for ( i
= offset
; i
< stack
.length
; i
++ ) {
1103 if ( stack
[ i
].indexOf( fileName
) != -1 ) {
1106 include
.push( stack
[ i
] );
1108 if ( include
.length
) {
1109 return include
.join( "\n" );
1112 return stack
[ offset
];
1113 } else if ( e
.sourceURL
) {
1114 // Safari, PhantomJS
1115 // hopefully one day Safari provides actual stacktraces
1116 // exclude useless self-reference for generated Error objects
1117 if ( /qunit.js$/.test( e
.sourceURL
) ) {
1120 // for actual exceptions, this is useful
1121 return e
.sourceURL
+ ":" + e
.line
;
1124 function sourceFromStacktrace( offset
) {
1128 return extractStacktrace( e
, offset
);
1132 function escapeInnerText( s
) {
1137 return s
.replace( /[\&<>]/g, function( s
) {
1139 case "&": return "&";
1140 case "<": return "<";
1141 case ">": return ">";
1147 function synchronize( callback
, last
) {
1148 config
.queue
.push( callback
);
1150 if ( config
.autorun
&& !config
.blocking
) {
1155 function process( last
) {
1159 var start
= new Date().getTime();
1160 config
.depth
= config
.depth
? config
.depth
+ 1 : 1;
1162 while ( config
.queue
.length
&& !config
.blocking
) {
1163 if ( !defined
.setTimeout
|| config
.updateRate
<= 0 || ( ( new Date().getTime() - start
) < config
.updateRate
) ) {
1164 config
.queue
.shift()();
1166 window
.setTimeout( next
, 13 );
1171 if ( last
&& !config
.blocking
&& !config
.queue
.length
&& config
.depth
=== 0 ) {
1176 function saveGlobal() {
1177 config
.pollution
= [];
1179 if ( config
.noglobals
) {
1180 for ( var key
in window
) {
1181 // in Opera sometimes DOM element ids show up here, ignore them
1182 if ( !hasOwn
.call( window
, key
) || /^qunit-test-output/.test( key
) ) {
1185 config
.pollution
.push( key
);
1190 function checkPollution( name
) {
1193 old
= config
.pollution
;
1197 newGlobals
= diff( config
.pollution
, old
);
1198 if ( newGlobals
.length
> 0 ) {
1199 QUnit
.pushFailure( "Introduced global variable(s): " + newGlobals
.join(", ") );
1202 deletedGlobals
= diff( old
, config
.pollution
);
1203 if ( deletedGlobals
.length
> 0 ) {
1204 QUnit
.pushFailure( "Deleted global variable(s): " + deletedGlobals
.join(", ") );
1208 // returns a new Array with the elements that are in a but not in b
1209 function diff( a
, b
) {
1213 for ( i
= 0; i
< result
.length
; i
++ ) {
1214 for ( j
= 0; j
< b
.length
; j
++ ) {
1215 if ( result
[i
] === b
[j
] ) {
1216 result
.splice( i
, 1 );
1225 function extend( a
, b
) {
1226 for ( var prop
in b
) {
1227 if ( b
[ prop
] === undefined ) {
1230 // Avoid "Member not found" error in IE8 caused by setting window.constructor
1231 } else if ( prop
!== "constructor" || a
!== window
) {
1232 a
[ prop
] = b
[ prop
];
1239 function addEvent( elem
, type
, fn
) {
1240 if ( elem
.addEventListener
) {
1241 elem
.addEventListener( type
, fn
, false );
1242 } else if ( elem
.attachEvent
) {
1243 elem
.attachEvent( "on" + type
, fn
);
1249 function id( name
) {
1250 return !!( typeof document
!== "undefined" && document
&& document
.getElementById
) &&
1251 document
.getElementById( name
);
1254 function registerLoggingCallback( key
) {
1255 return function( callback
) {
1256 config
[key
].push( callback
);
1260 // Supports deprecated method of completely overwriting logging callbacks
1261 function runLoggingCallbacks( key
, scope
, args
) {
1264 if ( QUnit
.hasOwnProperty( key
) ) {
1265 QUnit
[ key
].call(scope
, args
);
1267 callbacks
= config
[ key
];
1268 for ( i
= 0; i
< callbacks
.length
; i
++ ) {
1269 callbacks
[ i
].call( scope
, args
);
1274 // Test for equality any JavaScript type.
1275 // Author: Philippe Rathé <prathe@gmail.com>
1276 QUnit
.equiv
= (function() {
1278 // Call the o related callback with the given arguments.
1279 function bindCallbacks( o
, callbacks
, args
) {
1280 var prop
= QUnit
.objectType( o
);
1282 if ( QUnit
.objectType( callbacks
[ prop
] ) === "function" ) {
1283 return callbacks
[ prop
].apply( callbacks
, args
);
1285 return callbacks
[ prop
]; // or undefined
1290 // the real equiv function
1292 // stack to decide between skip/abort functions
1294 // stack to avoiding loops from circular referencing
1297 getProto
= Object
.getPrototypeOf
|| function ( obj
) {
1298 return obj
.__proto__
;
1300 callbacks
= (function () {
1302 // for string, boolean, number and null
1303 function useStrictEquality( b
, a
) {
1304 if ( b
instanceof a
.constructor || a
instanceof b
.constructor ) {
1305 // to catch short annotaion VS 'new' annotation of a
1308 // var j = new Number(1);
1316 "string": useStrictEquality
,
1317 "boolean": useStrictEquality
,
1318 "number": useStrictEquality
,
1319 "null": useStrictEquality
,
1320 "undefined": useStrictEquality
,
1322 "nan": function( b
) {
1326 "date": function( b
, a
) {
1327 return QUnit
.objectType( b
) === "date" && a
.valueOf() === b
.valueOf();
1330 "regexp": function( b
, a
) {
1331 return QUnit
.objectType( b
) === "regexp" &&
1333 a
.source
=== b
.source
&&
1335 a
.global
=== b
.global
&&
1337 a
.ignoreCase
=== b
.ignoreCase
&&
1338 a
.multiline
=== b
.multiline
;
1341 // - skip when the property is a method of an instance (OOP)
1342 // - abort otherwise,
1343 // initial === would have catch identical references anyway
1344 "function": function() {
1345 var caller
= callers
[callers
.length
- 1];
1346 return caller
!== Object
&& typeof caller
!== "undefined";
1349 "array": function( b
, a
) {
1350 var i
, j
, len
, loop
;
1352 // b could be an object literal here
1353 if ( QUnit
.objectType( b
) !== "array" ) {
1358 if ( len
!== b
.length
) {
1363 // track reference to avoid circular references
1365 for ( i
= 0; i
< len
; i
++ ) {
1367 for ( j
= 0; j
< parents
.length
; j
++ ) {
1368 if ( parents
[j
] === a
[i
] ) {
1369 loop
= true;// dont rewalk array
1372 if ( !loop
&& !innerEquiv(a
[i
], b
[i
]) ) {
1381 "object": function( b
, a
) {
1388 // comparing constructors is more strict than using
1390 if ( a
.constructor !== b
.constructor ) {
1391 // Allow objects with no prototype to be equivalent to
1392 // objects with Object as their constructor.
1393 if ( !(( getProto(a
) === null && getProto(b
) === Object
.prototype ) ||
1394 ( getProto(b
) === null && getProto(a
) === Object
.prototype ) ) ) {
1399 // stack constructor before traversing properties
1400 callers
.push( a
.constructor );
1401 // track reference to avoid circular references
1404 for ( i
in a
) { // be strict: don't ensures hasOwnProperty
1407 for ( j
= 0; j
< parents
.length
; j
++ ) {
1408 if ( parents
[j
] === a
[i
] ) {
1409 // don't go down the same path twice
1413 aProperties
.push(i
); // collect a's properties
1415 if (!loop
&& !innerEquiv( a
[i
], b
[i
] ) ) {
1421 callers
.pop(); // unstack, we are done
1425 bProperties
.push( i
); // collect b's properties
1428 // Ensures identical properties name
1429 return eq
&& innerEquiv( aProperties
.sort(), bProperties
.sort() );
1434 innerEquiv = function() { // can take multiple arguments
1435 var args
= [].slice
.apply( arguments
);
1436 if ( args
.length
< 2 ) {
1437 return true; // end transition
1440 return (function( a
, b
) {
1442 return true; // catch the most you can
1443 } else if ( a
=== null || b
=== null || typeof a
=== "undefined" ||
1444 typeof b
=== "undefined" ||
1445 QUnit
.objectType(a
) !== QUnit
.objectType(b
) ) {
1446 return false; // don't lose time with error prone cases
1448 return bindCallbacks(a
, callbacks
, [ b
, a
]);
1451 // apply transition with (1..n) arguments
1452 }( args
[0], args
[1] ) && arguments
.callee
.apply( this, args
.splice(1, args
.length
- 1 )) );
1459 * jsDump Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com |
1460 * http://flesler.blogspot.com Licensed under BSD
1461 * (http://www.opensource.org/licenses/bsd-license.php) Date: 5/15/2008
1463 * @projectDescription Advanced and extensible data dumping for Javascript.
1465 * @author Ariel Flesler
1466 * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
1468 QUnit
.jsDump
= (function() {
1469 function quote( str
) {
1470 return '"' + str
.toString().replace( /"/g, '\\"' ) + '"';
1472 function literal( o ) {
1475 function join( pre, arr, post ) {
1476 var s = jsDump.separator(),
1477 base = jsDump.indent(),
1478 inner = jsDump.indent(1);
1480 arr = arr.join( "," + s + inner );
1485 return [ pre, inner + arr, base + post ].join(s);
1487 function array( arr, stack ) {
1488 var i = arr.length, ret = new Array(i);
1491 ret[i] = this.parse( arr[i] , undefined , stack);
1494 return join( "[", ret, "]" );
1497 var reName = /^function (\w+)/,
1499 parse: function( obj, type, stack ) { //type is used mostly internally, you can fix a (custom)type in advance
1500 stack = stack || [ ];
1502 parser = this.parsers[ type || this.typeOf(obj) ];
1504 type = typeof parser;
1505 inStack = inArray( obj, stack );
1507 if ( inStack != -1 ) {
1508 return "recursion(" + (inStack - stack.length) + ")";
1511 if ( type == "function" ) {
1513 res = parser.call( this, obj, stack );
1518 return ( type == "string
" ) ? parser : this.parsers.error;
1520 typeOf: function( obj ) {
1522 if ( obj === null ) {
1524 } else if ( typeof obj === "undefined" ) {
1526 } else if ( QUnit.is( "regexp
", obj) ) {
1528 } else if ( QUnit.is( "date
", obj) ) {
1530 } else if ( QUnit.is( "function", obj) ) {
1532 } else if ( typeof obj.setInterval !== undefined && typeof obj.document !== "undefined" && typeof obj.nodeType === "undefined" ) {
1534 } else if ( obj.nodeType === 9 ) {
1536 } else if ( obj.nodeType ) {
1540 toString.call( obj ) === "[object Array
]" ||
1542 ( typeof obj.length === "number
" && typeof obj.item !== "undefined" && ( obj.length ? obj.item(0) === obj[0] : ( obj.item( 0 ) === null && typeof obj[0] === "undefined" ) ) )
1550 separator: function() {
1551 return this.multiline ? this.HTML ? "<br
/>" : "\n" : this.HTML ? " 
;" : " ";
1553 indent: function( extra ) {// extra can be a number, shortcut for increasing-calling-decreasing
1554 if ( !this.multiline ) {
1557 var chr = this.indentChar;
1559 chr = chr.replace( /\t/g, " " ).replace( / /g, " 
;" );
1561 return new Array( this._depth_ + (extra||0) ).join(chr);
1564 this._depth_ += a || 1;
1566 down: function( a ) {
1567 this._depth_ -= a || 1;
1569 setParser: function( name, parser ) {
1570 this.parsers[name] = parser;
1572 // The next 3 are exposed so you can use them
1578 // This is the list of parsers, to modify them, use jsDump.setParser
1581 document: "[Document
]",
1582 error: "[ERROR
]", //when no parser is found, shouldn"t happen
1583 unknown
: "[Unknown]",
1585 "undefined": "undefined",
1586 "function": function( fn
) {
1587 var ret
= "function",
1588 name
= "name" in fn
? fn
.name
: (reName
.exec(fn
) || [])[1];//functions never have name in IE
1595 ret
= [ ret
, QUnit
.jsDump
.parse( fn
, "functionArgs" ), "){" ].join( "" );
1596 return join( ret
, QUnit
.jsDump
.parse(fn
,"functionCode" ), "}" );
1601 object: function( map
, stack
) {
1602 var ret
= [ ], keys
, key
, val
, i
;
1604 if ( Object
.keys
) {
1605 keys
= Object
.keys( map
);
1608 for ( key
in map
) {
1613 for ( i
= 0; i
< keys
.length
; i
++ ) {
1616 ret
.push( QUnit
.jsDump
.parse( key
, "key" ) + ": " + QUnit
.jsDump
.parse( val
, undefined, stack
) );
1618 QUnit
.jsDump
.down();
1619 return join( "{", ret
, "}" );
1621 node: function( node
) {
1623 open
= QUnit
.jsDump
.HTML
? "<" : "<",
1624 close
= QUnit
.jsDump
.HTML
? ">" : ">",
1625 tag
= node
.nodeName
.toLowerCase(),
1628 for ( a
in QUnit
.jsDump
.DOMAttrs
) {
1629 val
= node
[ QUnit
.jsDump
.DOMAttrs
[a
] ];
1631 ret
+= " " + a
+ "=" + QUnit
.jsDump
.parse( val
, "attribute" );
1634 return ret
+ close
+ open
+ "/" + tag
+ close
;
1636 functionArgs: function( fn
) {//function calls it internally, it's the arguments part of the function
1644 args
= new Array(l
);
1646 args
[l
] = String
.fromCharCode(97+l
);//97 is 'a'
1648 return " " + args
.join( ", " ) + " ";
1650 key
: quote
, //object calls it internally, the key part of an item in a map
1651 functionCode
: "[code]", //function calls it internally, it's the content of the function
1652 attribute
: quote
, //node calls it internally, it's an html attribute value
1655 regexp
: literal
, //regex
1660 //attributes to dump from nodes, name=>realName
1663 "class": "className"
1665 HTML
: false,//if true, entities are escaped ( <, >, \t, space and \n )
1666 indentChar
: " ",//indentation unit
1667 multiline
: true //if true, items in a collection, are separated by a \n, else just a space.
1674 function getText( elems
) {
1678 for ( i
= 0; elems
[i
]; i
++ ) {
1681 // Get the text from text nodes and CDATA nodes
1682 if ( elem
.nodeType
=== 3 || elem
.nodeType
=== 4 ) {
1683 ret
+= elem
.nodeValue
;
1685 // Traverse everything else, except comment nodes
1686 } else if ( elem
.nodeType
!== 8 ) {
1687 ret
+= getText( elem
.childNodes
);
1695 function inArray( elem
, array
) {
1696 if ( array
.indexOf
) {
1697 return array
.indexOf( elem
);
1700 for ( var i
= 0, length
= array
.length
; i
< length
; i
++ ) {
1701 if ( array
[ i
] === elem
) {
1710 * Javascript Diff Algorithm
1711 * By John Resig (http://ejohn.org/)
1712 * Modified by Chu Alan "sprite"
1714 * Released under the MIT license.
1717 * http://ejohn.org/projects/javascript-diff-algorithm/
1719 * Usage: QUnit.diff(expected, actual)
1721 * 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"
1723 QUnit
.diff
= (function() {
1724 function diff( o
, n
) {
1729 for ( i
= 0; i
< n
.length
; i
++ ) {
1730 if ( ns
[ n
[i
] ] == null ) {
1736 ns
[ n
[i
] ].rows
.push( i
);
1739 for ( i
= 0; i
< o
.length
; i
++ ) {
1740 if ( os
[ o
[i
] ] == null ) {
1746 os
[ o
[i
] ].rows
.push( i
);
1750 if ( !hasOwn
.call( ns
, i
) ) {
1753 if ( ns
[i
].rows
.length
== 1 && typeof os
[i
] != "undefined" && os
[i
].rows
.length
== 1 ) {
1754 n
[ ns
[i
].rows
[0] ] = {
1755 text
: n
[ ns
[i
].rows
[0] ],
1758 o
[ os
[i
].rows
[0] ] = {
1759 text
: o
[ os
[i
].rows
[0] ],
1765 for ( i
= 0; i
< n
.length
- 1; i
++ ) {
1766 if ( n
[i
].text
!= null && n
[ i
+ 1 ].text
== null && n
[i
].row
+ 1 < o
.length
&& o
[ n
[i
].row
+ 1 ].text
== null &&
1767 n
[ i
+ 1 ] == o
[ n
[i
].row
+ 1 ] ) {
1773 o
[ n
[i
].row
+ 1 ] = {
1774 text
: o
[ n
[i
].row
+ 1 ],
1780 for ( i
= n
.length
- 1; i
> 0; i
-- ) {
1781 if ( n
[i
].text
!= null && n
[ i
- 1 ].text
== null && n
[i
].row
> 0 && o
[ n
[i
].row
- 1 ].text
== null &&
1782 n
[ i
- 1 ] == o
[ n
[i
].row
- 1 ]) {
1788 o
[ n
[i
].row
- 1 ] = {
1789 text
: o
[ n
[i
].row
- 1 ],
1801 return function( o
, n
) {
1802 o
= o
.replace( /\s+$/, "" );
1803 n
= n
.replace( /\s+$/, "" );
1807 out
= diff( o
=== "" ? [] : o
.split(/\s+/), n
=== "" ? [] : n
.split(/\s+/) ),
1808 oSpace
= o
.match(/\s+/g),
1809 nSpace
= n
.match(/\s+/g);
1811 if ( oSpace
== null ) {
1818 if ( nSpace
== null ) {
1825 if ( out
.n
.length
=== 0 ) {
1826 for ( i
= 0; i
< out
.o
.length
; i
++ ) {
1827 str
+= "<del>" + out
.o
[i
] + oSpace
[i
] + "</del>";
1831 if ( out
.n
[0].text
== null ) {
1832 for ( n
= 0; n
< out
.o
.length
&& out
.o
[n
].text
== null; n
++ ) {
1833 str
+= "<del>" + out
.o
[n
] + oSpace
[n
] + "</del>";
1837 for ( i
= 0; i
< out
.n
.length
; i
++ ) {
1838 if (out
.n
[i
].text
== null) {
1839 str
+= "<ins>" + out
.n
[i
] + nSpace
[i
] + "</ins>";
1842 // `pre` initialized at top of scope
1845 for ( n
= out
.n
[i
].row
+ 1; n
< out
.o
.length
&& out
.o
[n
].text
== null; n
++ ) {
1846 pre
+= "<del>" + out
.o
[n
] + oSpace
[n
] + "</del>";
1848 str
+= " " + out
.n
[i
].text
+ nSpace
[i
] + pre
;
1857 // for CommonJS enviroments, export everything
1858 if ( typeof exports
!== "undefined" ) {
1859 extend(exports
, QUnit
);
1862 // get at whatever the global object is, like window in browsers
1863 }( (function() {return this;}.call()) ));