2 * QUnit v1.9.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.
408 * @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" );
410 ok: function( result
, msg
) {
411 if ( !config
.current
) {
412 throw new Error( "ok() assertion outside test context, was " + sourceFromStacktrace(2) );
422 msg
= escapeInnerText( msg
|| (result
? "okay" : "failed" ) );
423 msg
= "<span class='test-message'>" + msg
+ "</span>";
426 source
= sourceFromStacktrace( 2 );
428 details
.source
= source
;
429 msg
+= "<table><tr class='test-source'><th>Source: </th><td><pre>" + escapeInnerText( source
) + "</pre></td></tr></table>";
432 runLoggingCallbacks( "log", QUnit
, details
);
433 config
.current
.assertions
.push({
440 * Assert that the first two arguments are equal, with an optional message.
441 * Prints out both actual and expected values.
444 * @example equal( format( "Received {0} bytes.", 2), "Received 2 bytes.", "format() replaces {0} with next argument" );
446 equal: function( actual
, expected
, message
) {
447 QUnit
.push( expected
== actual
, actual
, expected
, message
);
454 notEqual: function( actual
, expected
, message
) {
455 QUnit
.push( expected
!= actual
, actual
, expected
, message
);
462 deepEqual: function( actual
, expected
, message
) {
463 QUnit
.push( QUnit
.equiv(actual
, expected
), actual
, expected
, message
);
470 notDeepEqual: function( actual
, expected
, message
) {
471 QUnit
.push( !QUnit
.equiv(actual
, expected
), actual
, expected
, message
);
478 strictEqual: function( actual
, expected
, message
) {
479 QUnit
.push( expected
=== actual
, actual
, expected
, message
);
483 * @name notStrictEqual
486 notStrictEqual: function( actual
, expected
, message
) {
487 QUnit
.push( expected
!== actual
, actual
, expected
, message
);
490 throws: function( block
, expected
, message
) {
494 // 'expected' is optional
495 if ( typeof expected
=== "string" ) {
500 config
.current
.ignoreGlobalErrors
= true;
502 block
.call( config
.current
.testEnvironment
);
506 config
.current
.ignoreGlobalErrors
= false;
509 // we don't want to validate thrown error
512 // expected is a regexp
513 } else if ( QUnit
.objectType( expected
) === "regexp" ) {
514 ok
= expected
.test( actual
);
515 // expected is a constructor
516 } else if ( actual
instanceof expected
) {
518 // expected is a validation function which returns true is validation passed
519 } else if ( expected
.call( {}, actual
) === true ) {
523 QUnit
.push( ok
, actual
, null, message
);
525 QUnit
.pushFailure( message
, null, 'No exception was thrown.' );
531 * @deprecate since 1.8.0
532 * Kept assertion helpers in root for backwards compatibility
534 extend( QUnit
, QUnit
.assert
);
537 * @deprecated since 1.9.0
538 * Kept global "raises()" for backwards compatibility
540 QUnit
.raises
= QUnit
.assert
.throws;
543 * @deprecated since 1.0.0, replaced with error pushes since 1.3.0
544 * Kept to avoid TypeErrors for undefined methods.
546 QUnit
.equals = function() {
547 QUnit
.push( false, false, false, "QUnit.equals has been deprecated since 2009 (e88049a0), use QUnit.equal instead" );
549 QUnit
.same = function() {
550 QUnit
.push( false, false, false, "QUnit.same has been deprecated since 2009 (e88049a0), use QUnit.deepEqual instead" );
553 // We want access to the constructor's prototype
558 // Make F QUnit's constructor so that we can add to the prototype later
559 QUnit
.constructor = F
;
563 * Config object: Maintain internal state
564 * Later exposed as QUnit.config
565 * `config` initialized at top of scope
568 // The queue of tests to run
571 // block until document ready
574 // when enabled, show only failing tests
575 // gets persisted through sessionStorage and can be changed in UI via checkbox
578 // by default, run previously failed tests first
579 // very useful in combination with "Hide passed tests" checked
582 // by default, modify document.title when suite is done
585 // when enabled, all tests must call expect()
586 requireExpects
: false,
588 // add checkboxes that are persisted in the query-string
589 // when enabled, the id is set to `true` as a `QUnit.config` property
593 label
: "Check for Globals",
594 tooltip
: "Enabling this will test if any test introduces new properties on the `window` object. Stored as query-strings."
598 label
: "No try-catch",
599 tooltip
: "Enabling this will run tests outside of a try-catch block. Makes debugging exceptions in IE reasonable. Stored as query-strings."
603 // logging callback queues
613 // Initialize more QUnit.config and QUnit.urlParams
616 location
= window
.location
|| { search
: "", protocol
: "file:" },
617 params
= location
.search
.slice( 1 ).split( "&" ),
618 length
= params
.length
,
623 for ( i
= 0; i
< length
; i
++ ) {
624 current
= params
[ i
].split( "=" );
625 current
[ 0 ] = decodeURIComponent( current
[ 0 ] );
626 // allow just a key to turn on a flag, e.g., test.html?noglobals
627 current
[ 1 ] = current
[ 1 ] ? decodeURIComponent( current
[ 1 ] ) : true;
628 urlParams
[ current
[ 0 ] ] = current
[ 1 ];
632 QUnit
.urlParams
= urlParams
;
634 // String search anywhere in moduleName+testName
635 config
.filter
= urlParams
.filter
;
637 // Exact match of the module name
638 config
.module
= urlParams
.module
;
640 config
.testNumber
= parseInt( urlParams
.testNumber
, 10 ) || null;
642 // Figure out if we're running the tests from a server or not
643 QUnit
.isLocal
= location
.protocol
=== "file:";
646 // Export global variables, unless an 'exports' object exists,
647 // in that case we assume we're in CommonJS (dealt with on the bottom of the script)
648 if ( typeof exports
=== "undefined" ) {
649 extend( window
, QUnit
);
651 // Expose QUnit object
652 window
.QUnit
= QUnit
;
655 // Extend QUnit object,
656 // these after set here because they should not be exposed as global functions
660 // Initialize the configuration options
663 stats
: { all
: 0, bad
: 0 },
664 moduleStats
: { all
: 0, bad
: 0 },
665 started
: +new Date(),
675 var tests
, banner
, result
,
676 qunit
= id( "qunit" );
680 "<h1 id='qunit-header'>" + escapeInnerText( document
.title
) + "</h1>" +
681 "<h2 id='qunit-banner'></h2>" +
682 "<div id='qunit-testrunner-toolbar'></div>" +
683 "<h2 id='qunit-userAgent'></h2>" +
684 "<ol id='qunit-tests'></ol>";
687 tests
= id( "qunit-tests" );
688 banner
= id( "qunit-banner" );
689 result
= id( "qunit-testresult" );
692 tests
.innerHTML
= "";
696 banner
.className
= "";
700 result
.parentNode
.removeChild( result
);
704 result
= document
.createElement( "p" );
705 result
.id
= "qunit-testresult";
706 result
.className
= "result";
707 tests
.parentNode
.insertBefore( result
, tests
);
708 result
.innerHTML
= "Running...<br/> ";
712 // Resets the test setup. Useful for tests that modify the DOM.
713 // If jQuery is available, uses jQuery's html(), otherwise just innerHTML.
717 if ( window
.jQuery
) {
718 jQuery( "#qunit-fixture" ).html( config
.fixture
);
720 fixture
= id( "qunit-fixture" );
722 fixture
.innerHTML
= config
.fixture
;
727 // Trigger an event on an element.
728 // @example triggerEvent( document.body, "click" );
729 triggerEvent: function( elem
, type
, event
) {
730 if ( document
.createEvent
) {
731 event
= document
.createEvent( "MouseEvents" );
732 event
.initMouseEvent(type
, true, true, elem
.ownerDocument
.defaultView
,
733 0, 0, 0, 0, 0, false, false, false, false, 0, null);
735 elem
.dispatchEvent( event
);
736 } else if ( elem
.fireEvent
) {
737 elem
.fireEvent( "on" + type
);
741 // Safe object type checking
742 is: function( type
, obj
) {
743 return QUnit
.objectType( obj
) == type
;
746 objectType: function( obj
) {
747 if ( typeof obj
=== "undefined" ) {
749 // consider: typeof null === object
751 if ( obj
=== null ) {
755 var type
= toString
.call( obj
).match(/^\[object\s(.*)\]$/)[1] || "";
769 return type
.toLowerCase();
771 if ( typeof obj
=== "object" ) {
777 push: function( result
, actual
, expected
, message
) {
778 if ( !config
.current
) {
779 throw new Error( "assertion outside test context, was " + sourceFromStacktrace() );
790 message
= escapeInnerText( message
) || ( result
? "okay" : "failed" );
791 message
= "<span class='test-message'>" + message
+ "</span>";
795 expected
= escapeInnerText( QUnit
.jsDump
.parse(expected
) );
796 actual
= escapeInnerText( QUnit
.jsDump
.parse(actual
) );
797 output
+= "<table><tr class='test-expected'><th>Expected: </th><td><pre>" + expected
+ "</pre></td></tr>";
799 if ( actual
!= expected
) {
800 output
+= "<tr class='test-actual'><th>Result: </th><td><pre>" + actual
+ "</pre></td></tr>";
801 output
+= "<tr class='test-diff'><th>Diff: </th><td><pre>" + QUnit
.diff( expected
, actual
) + "</pre></td></tr>";
804 source
= sourceFromStacktrace();
807 details
.source
= source
;
808 output
+= "<tr class='test-source'><th>Source: </th><td><pre>" + escapeInnerText( source
) + "</pre></td></tr>";
811 output
+= "</table>";
814 runLoggingCallbacks( "log", QUnit
, details
);
816 config
.current
.assertions
.push({
822 pushFailure: function( message
, source
, actual
) {
823 if ( !config
.current
) {
824 throw new Error( "pushFailure() assertion outside test context, was " + sourceFromStacktrace(2) );
833 message
= escapeInnerText( message
) || "error";
834 message
= "<span class='test-message'>" + message
+ "</span>";
840 output
+= "<tr class='test-actual'><th>Result: </th><td><pre>" + escapeInnerText( actual
) + "</pre></td></tr>";
844 details
.source
= source
;
845 output
+= "<tr class='test-source'><th>Source: </th><td><pre>" + escapeInnerText( source
) + "</pre></td></tr>";
848 output
+= "</table>";
850 runLoggingCallbacks( "log", QUnit
, details
);
852 config
.current
.assertions
.push({
858 url: function( params
) {
859 params
= extend( extend( {}, QUnit
.urlParams
), params
);
863 for ( key
in params
) {
864 if ( !hasOwn
.call( params
, key
) ) {
867 querystring
+= encodeURIComponent( key
) + "=" +
868 encodeURIComponent( params
[ key
] ) + "&";
870 return window
.location
.pathname
+ querystring
.slice( 0, -1 );
876 // load, equiv, jsDump, diff: Attached later
880 * @deprecated: Created for backwards compatibility with test runner that set the hook function
881 * into QUnit.{hook}, instead of invoking it and passing the hook function.
882 * QUnit.constructor is set to the empty F() above so that we can add to it's prototype here.
883 * Doing this allows us to tell if the following methods have been overwritten on the actual
886 extend( QUnit
.constructor.prototype, {
888 // Logging callbacks; all receive a single argument with the listed properties
889 // run test/logs.html for any related changes
890 begin
: registerLoggingCallback( "begin" ),
892 // done: { failed, passed, total, runtime }
893 done
: registerLoggingCallback( "done" ),
895 // log: { result, actual, expected, message }
896 log
: registerLoggingCallback( "log" ),
898 // testStart: { name }
899 testStart
: registerLoggingCallback( "testStart" ),
901 // testDone: { name, failed, passed, total }
902 testDone
: registerLoggingCallback( "testDone" ),
904 // moduleStart: { name }
905 moduleStart
: registerLoggingCallback( "moduleStart" ),
907 // moduleDone: { name, failed, passed, total }
908 moduleDone
: registerLoggingCallback( "moduleDone" )
911 if ( typeof document
=== "undefined" || document
.readyState
=== "complete" ) {
912 config
.autorun
= true;
915 QUnit
.load = function() {
916 runLoggingCallbacks( "begin", QUnit
, {} );
918 // Initialize the config, saving the execution queue
919 var banner
, filter
, i
, label
, len
, main
, ol
, toolbar
, userAgent
, val
, urlConfigCheckboxes
,
921 oldconfig
= extend( {}, config
);
924 extend(config
, oldconfig
);
926 config
.blocking
= false;
928 len
= config
.urlConfig
.length
;
930 for ( i
= 0; i
< len
; i
++ ) {
931 val
= config
.urlConfig
[i
];
932 if ( typeof val
=== "string" ) {
936 tooltip
: "[no tooltip available]"
939 config
[ val
.id
] = QUnit
.urlParams
[ val
.id
];
940 urlConfigHtml
+= "<input id='qunit-urlconfig-" + val
.id
+ "' name='" + val
.id
+ "' type='checkbox'" + ( config
[ val
.id
] ? " checked='checked'" : "" ) + " title='" + val
.tooltip
+ "'><label for='qunit-urlconfig-" + val
.id
+ "' title='" + val
.tooltip
+ "'>" + val
.label
+ "</label>";
943 // `userAgent` initialized at top of scope
944 userAgent
= id( "qunit-userAgent" );
946 userAgent
.innerHTML
= navigator
.userAgent
;
949 // `banner` initialized at top of scope
950 banner
= id( "qunit-header" );
952 banner
.innerHTML
= "<a href='" + QUnit
.url({ filter
: undefined, module
: undefined, testNumber
: undefined }) + "'>" + banner
.innerHTML
+ "</a> ";
955 // `toolbar` initialized at top of scope
956 toolbar
= id( "qunit-testrunner-toolbar" );
958 // `filter` initialized at top of scope
959 filter
= document
.createElement( "input" );
960 filter
.type
= "checkbox";
961 filter
.id
= "qunit-filter-pass";
963 addEvent( filter
, "click", function() {
965 ol
= document
.getElementById( "qunit-tests" );
967 if ( filter
.checked
) {
968 ol
.className
= ol
.className
+ " hidepass";
970 tmp
= " " + ol
.className
.replace( /[\n\t\r]/g, " " ) + " ";
971 ol
.className
= tmp
.replace( / hidepass
/, " " );
973 if ( defined
.sessionStorage
) {
974 if (filter
.checked
) {
975 sessionStorage
.setItem( "qunit-filter-passed-tests", "true" );
977 sessionStorage
.removeItem( "qunit-filter-passed-tests" );
982 if ( config
.hidepassed
|| defined
.sessionStorage
&& sessionStorage
.getItem( "qunit-filter-passed-tests" ) ) {
983 filter
.checked
= true;
984 // `ol` initialized at top of scope
985 ol
= document
.getElementById( "qunit-tests" );
986 ol
.className
= ol
.className
+ " hidepass";
988 toolbar
.appendChild( filter
);
990 // `label` initialized at top of scope
991 label
= document
.createElement( "label" );
992 label
.setAttribute( "for", "qunit-filter-pass" );
993 label
.setAttribute( "title", "Only show tests and assertons that fail. Stored in sessionStorage." );
994 label
.innerHTML
= "Hide passed tests";
995 toolbar
.appendChild( label
);
997 urlConfigCheckboxes
= document
.createElement( 'span' );
998 urlConfigCheckboxes
.innerHTML
= urlConfigHtml
;
999 addEvent( urlConfigCheckboxes
, "change", function( event
) {
1001 params
[ event
.target
.name
] = event
.target
.checked
? true : undefined;
1002 window
.location
= QUnit
.url( params
);
1004 toolbar
.appendChild( urlConfigCheckboxes
);
1007 // `main` initialized at top of scope
1008 main
= id( "qunit-fixture" );
1010 config
.fixture
= main
.innerHTML
;
1013 if ( config
.autostart
) {
1018 addEvent( window
, "load", QUnit
.load
);
1020 // `onErrorFnPrev` initialized at top of scope
1021 // Preserve other handlers
1022 onErrorFnPrev
= window
.onerror
;
1024 // Cover uncaught exceptions
1025 // Returning true will surpress the default browser handler,
1026 // returning false will let it run.
1027 window
.onerror = function ( error
, filePath
, linerNr
) {
1029 if ( onErrorFnPrev
) {
1030 ret
= onErrorFnPrev( error
, filePath
, linerNr
);
1033 // Treat return value as window.onerror itself does,
1034 // Only do our handling if not surpressed.
1035 if ( ret
!== true ) {
1036 if ( QUnit
.config
.current
) {
1037 if ( QUnit
.config
.current
.ignoreGlobalErrors
) {
1040 QUnit
.pushFailure( error
, filePath
+ ":" + linerNr
);
1042 QUnit
.test( "global failure", function() {
1043 QUnit
.pushFailure( error
, filePath
+ ":" + linerNr
);
1053 config
.autorun
= true;
1055 // Log the last module results
1056 if ( config
.currentModule
) {
1057 runLoggingCallbacks( "moduleDone", QUnit
, {
1058 name
: config
.currentModule
,
1059 failed
: config
.moduleStats
.bad
,
1060 passed
: config
.moduleStats
.all
- config
.moduleStats
.bad
,
1061 total
: config
.moduleStats
.all
1066 banner
= id( "qunit-banner" ),
1067 tests
= id( "qunit-tests" ),
1068 runtime
= +new Date() - config
.started
,
1069 passed
= config
.stats
.all
- config
.stats
.bad
,
1071 "Tests completed in ",
1073 " milliseconds.<br/>",
1074 "<span class='passed'>",
1076 "</span> tests of <span class='total'>",
1078 "</span> passed, <span class='failed'>",
1084 banner
.className
= ( config
.stats
.bad
? "qunit-fail" : "qunit-pass" );
1088 id( "qunit-testresult" ).innerHTML
= html
;
1091 if ( config
.altertitle
&& typeof document
!== "undefined" && document
.title
) {
1092 // show ✖ for good, ✔ for bad suite result in title
1093 // use escape sequences in case file gets loaded with non-utf-8-charset
1095 ( config
.stats
.bad
? "\u2716" : "\u2714" ),
1096 document
.title
.replace( /^[\u2714\u2716] /i, "" )
1100 // clear own sessionStorage items if all tests passed
1101 if ( config
.reorder
&& defined
.sessionStorage
&& config
.stats
.bad
=== 0 ) {
1102 // `key` & `i` initialized at top of scope
1103 for ( i
= 0; i
< sessionStorage
.length
; i
++ ) {
1104 key
= sessionStorage
.key( i
++ );
1105 if ( key
.indexOf( "qunit-test-" ) === 0 ) {
1106 sessionStorage
.removeItem( key
);
1111 runLoggingCallbacks( "done", QUnit
, {
1112 failed
: config
.stats
.bad
,
1114 total
: config
.stats
.all
,
1119 /** @return Boolean: true if this test should be ran */
1120 function validTest( test
) {
1122 filter
= config
.filter
&& config
.filter
.toLowerCase(),
1123 module
= config
.module
&& config
.module
.toLowerCase(),
1124 fullName
= (test
.module
+ ": " + test
.testName
).toLowerCase();
1126 if ( config
.testNumber
) {
1127 return test
.testNumber
=== config
.testNumber
;
1130 if ( module
&& ( !test
.module
|| test
.module
.toLowerCase() !== module
) ) {
1138 include
= filter
.charAt( 0 ) !== "!";
1140 filter
= filter
.slice( 1 );
1143 // If the filter matches, we need to honour include
1144 if ( fullName
.indexOf( filter
) !== -1 ) {
1148 // Otherwise, do the opposite
1152 // so far supports only Firefox, Chrome and Opera (buggy), Safari (for real exceptions)
1153 // Later Safari and IE10 are supposed to support error.stack as well
1154 // See also https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error/Stack
1155 function extractStacktrace( e
, offset
) {
1156 offset
= offset
=== undefined ? 3 : offset
;
1158 var stack
, include
, i
, regex
;
1160 if ( e
.stacktrace
) {
1162 return e
.stacktrace
.split( "\n" )[ offset
+ 3 ];
1163 } else if ( e
.stack
) {
1165 stack
= e
.stack
.split( "\n" );
1166 if (/^error$/i.test( stack
[0] ) ) {
1171 for ( i
= offset
; i
< stack
.length
; i
++ ) {
1172 if ( stack
[ i
].indexOf( fileName
) != -1 ) {
1175 include
.push( stack
[ i
] );
1177 if ( include
.length
) {
1178 return include
.join( "\n" );
1181 return stack
[ offset
];
1182 } else if ( e
.sourceURL
) {
1183 // Safari, PhantomJS
1184 // hopefully one day Safari provides actual stacktraces
1185 // exclude useless self-reference for generated Error objects
1186 if ( /qunit.js$/.test( e
.sourceURL
) ) {
1189 // for actual exceptions, this is useful
1190 return e
.sourceURL
+ ":" + e
.line
;
1193 function sourceFromStacktrace( offset
) {
1197 return extractStacktrace( e
, offset
);
1201 function escapeInnerText( s
) {
1206 return s
.replace( /[\&<>]/g, function( s
) {
1208 case "&": return "&";
1209 case "<": return "<";
1210 case ">": return ">";
1216 function synchronize( callback
, last
) {
1217 config
.queue
.push( callback
);
1219 if ( config
.autorun
&& !config
.blocking
) {
1224 function process( last
) {
1228 var start
= new Date().getTime();
1229 config
.depth
= config
.depth
? config
.depth
+ 1 : 1;
1231 while ( config
.queue
.length
&& !config
.blocking
) {
1232 if ( !defined
.setTimeout
|| config
.updateRate
<= 0 || ( ( new Date().getTime() - start
) < config
.updateRate
) ) {
1233 config
.queue
.shift()();
1235 window
.setTimeout( next
, 13 );
1240 if ( last
&& !config
.blocking
&& !config
.queue
.length
&& config
.depth
=== 0 ) {
1245 function saveGlobal() {
1246 config
.pollution
= [];
1248 if ( config
.noglobals
) {
1249 for ( var key
in window
) {
1250 // in Opera sometimes DOM element ids show up here, ignore them
1251 if ( !hasOwn
.call( window
, key
) || /^qunit-test-output/.test( key
) ) {
1254 config
.pollution
.push( key
);
1259 function checkPollution( name
) {
1262 old
= config
.pollution
;
1266 newGlobals
= diff( config
.pollution
, old
);
1267 if ( newGlobals
.length
> 0 ) {
1268 QUnit
.pushFailure( "Introduced global variable(s): " + newGlobals
.join(", ") );
1271 deletedGlobals
= diff( old
, config
.pollution
);
1272 if ( deletedGlobals
.length
> 0 ) {
1273 QUnit
.pushFailure( "Deleted global variable(s): " + deletedGlobals
.join(", ") );
1277 // returns a new Array with the elements that are in a but not in b
1278 function diff( a
, b
) {
1282 for ( i
= 0; i
< result
.length
; i
++ ) {
1283 for ( j
= 0; j
< b
.length
; j
++ ) {
1284 if ( result
[i
] === b
[j
] ) {
1285 result
.splice( i
, 1 );
1294 function extend( a
, b
) {
1295 for ( var prop
in b
) {
1296 if ( b
[ prop
] === undefined ) {
1299 // Avoid "Member not found" error in IE8 caused by setting window.constructor
1300 } else if ( prop
!== "constructor" || a
!== window
) {
1301 a
[ prop
] = b
[ prop
];
1308 function addEvent( elem
, type
, fn
) {
1309 if ( elem
.addEventListener
) {
1310 elem
.addEventListener( type
, fn
, false );
1311 } else if ( elem
.attachEvent
) {
1312 elem
.attachEvent( "on" + type
, fn
);
1318 function id( name
) {
1319 return !!( typeof document
!== "undefined" && document
&& document
.getElementById
) &&
1320 document
.getElementById( name
);
1323 function registerLoggingCallback( key
) {
1324 return function( callback
) {
1325 config
[key
].push( callback
);
1329 // Supports deprecated method of completely overwriting logging callbacks
1330 function runLoggingCallbacks( key
, scope
, args
) {
1333 if ( QUnit
.hasOwnProperty( key
) ) {
1334 QUnit
[ key
].call(scope
, args
);
1336 callbacks
= config
[ key
];
1337 for ( i
= 0; i
< callbacks
.length
; i
++ ) {
1338 callbacks
[ i
].call( scope
, args
);
1343 // Test for equality any JavaScript type.
1344 // Author: Philippe Rathé <prathe@gmail.com>
1345 QUnit
.equiv
= (function() {
1347 // Call the o related callback with the given arguments.
1348 function bindCallbacks( o
, callbacks
, args
) {
1349 var prop
= QUnit
.objectType( o
);
1351 if ( QUnit
.objectType( callbacks
[ prop
] ) === "function" ) {
1352 return callbacks
[ prop
].apply( callbacks
, args
);
1354 return callbacks
[ prop
]; // or undefined
1359 // the real equiv function
1361 // stack to decide between skip/abort functions
1363 // stack to avoiding loops from circular referencing
1366 getProto
= Object
.getPrototypeOf
|| function ( obj
) {
1367 return obj
.__proto__
;
1369 callbacks
= (function () {
1371 // for string, boolean, number and null
1372 function useStrictEquality( b
, a
) {
1373 if ( b
instanceof a
.constructor || a
instanceof b
.constructor ) {
1374 // to catch short annotaion VS 'new' annotation of a
1377 // var j = new Number(1);
1385 "string": useStrictEquality
,
1386 "boolean": useStrictEquality
,
1387 "number": useStrictEquality
,
1388 "null": useStrictEquality
,
1389 "undefined": useStrictEquality
,
1391 "nan": function( b
) {
1395 "date": function( b
, a
) {
1396 return QUnit
.objectType( b
) === "date" && a
.valueOf() === b
.valueOf();
1399 "regexp": function( b
, a
) {
1400 return QUnit
.objectType( b
) === "regexp" &&
1402 a
.source
=== b
.source
&&
1404 a
.global
=== b
.global
&&
1406 a
.ignoreCase
=== b
.ignoreCase
&&
1407 a
.multiline
=== b
.multiline
;
1410 // - skip when the property is a method of an instance (OOP)
1411 // - abort otherwise,
1412 // initial === would have catch identical references anyway
1413 "function": function() {
1414 var caller
= callers
[callers
.length
- 1];
1415 return caller
!== Object
&& typeof caller
!== "undefined";
1418 "array": function( b
, a
) {
1419 var i
, j
, len
, loop
;
1421 // b could be an object literal here
1422 if ( QUnit
.objectType( b
) !== "array" ) {
1427 if ( len
!== b
.length
) {
1432 // track reference to avoid circular references
1434 for ( i
= 0; i
< len
; i
++ ) {
1436 for ( j
= 0; j
< parents
.length
; j
++ ) {
1437 if ( parents
[j
] === a
[i
] ) {
1438 loop
= true;// dont rewalk array
1441 if ( !loop
&& !innerEquiv(a
[i
], b
[i
]) ) {
1450 "object": function( b
, a
) {
1457 // comparing constructors is more strict than using
1459 if ( a
.constructor !== b
.constructor ) {
1460 // Allow objects with no prototype to be equivalent to
1461 // objects with Object as their constructor.
1462 if ( !(( getProto(a
) === null && getProto(b
) === Object
.prototype ) ||
1463 ( getProto(b
) === null && getProto(a
) === Object
.prototype ) ) ) {
1468 // stack constructor before traversing properties
1469 callers
.push( a
.constructor );
1470 // track reference to avoid circular references
1473 for ( i
in a
) { // be strict: don't ensures hasOwnProperty
1476 for ( j
= 0; j
< parents
.length
; j
++ ) {
1477 if ( parents
[j
] === a
[i
] ) {
1478 // don't go down the same path twice
1482 aProperties
.push(i
); // collect a's properties
1484 if (!loop
&& !innerEquiv( a
[i
], b
[i
] ) ) {
1490 callers
.pop(); // unstack, we are done
1494 bProperties
.push( i
); // collect b's properties
1497 // Ensures identical properties name
1498 return eq
&& innerEquiv( aProperties
.sort(), bProperties
.sort() );
1503 innerEquiv = function() { // can take multiple arguments
1504 var args
= [].slice
.apply( arguments
);
1505 if ( args
.length
< 2 ) {
1506 return true; // end transition
1509 return (function( a
, b
) {
1511 return true; // catch the most you can
1512 } else if ( a
=== null || b
=== null || typeof a
=== "undefined" ||
1513 typeof b
=== "undefined" ||
1514 QUnit
.objectType(a
) !== QUnit
.objectType(b
) ) {
1515 return false; // don't lose time with error prone cases
1517 return bindCallbacks(a
, callbacks
, [ b
, a
]);
1520 // apply transition with (1..n) arguments
1521 }( args
[0], args
[1] ) && arguments
.callee
.apply( this, args
.splice(1, args
.length
- 1 )) );
1528 * jsDump Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com |
1529 * http://flesler.blogspot.com Licensed under BSD
1530 * (http://www.opensource.org/licenses/bsd-license.php) Date: 5/15/2008
1532 * @projectDescription Advanced and extensible data dumping for Javascript.
1534 * @author Ariel Flesler
1535 * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
1537 QUnit
.jsDump
= (function() {
1538 function quote( str
) {
1539 return '"' + str
.toString().replace( /"/g, '\\"' ) + '"';
1541 function literal( o ) {
1544 function join( pre, arr, post ) {
1545 var s = jsDump.separator(),
1546 base = jsDump.indent(),
1547 inner = jsDump.indent(1);
1549 arr = arr.join( "," + s + inner );
1554 return [ pre, inner + arr, base + post ].join(s);
1556 function array( arr, stack ) {
1557 var i = arr.length, ret = new Array(i);
1560 ret[i] = this.parse( arr[i] , undefined , stack);
1563 return join( "[", ret, "]" );
1566 var reName = /^function (\w+)/,
1568 parse: function( obj, type, stack ) { //type is used mostly internally, you can fix a (custom)type in advance
1569 stack = stack || [ ];
1571 parser = this.parsers[ type || this.typeOf(obj) ];
1573 type = typeof parser;
1574 inStack = inArray( obj, stack );
1576 if ( inStack != -1 ) {
1577 return "recursion(" + (inStack - stack.length) + ")";
1580 if ( type == "function" ) {
1582 res = parser.call( this, obj, stack );
1587 return ( type == "string
" ) ? parser : this.parsers.error;
1589 typeOf: function( obj ) {
1591 if ( obj === null ) {
1593 } else if ( typeof obj === "undefined" ) {
1595 } else if ( QUnit.is( "regexp
", obj) ) {
1597 } else if ( QUnit.is( "date
", obj) ) {
1599 } else if ( QUnit.is( "function", obj) ) {
1601 } else if ( typeof obj.setInterval !== undefined && typeof obj.document !== "undefined" && typeof obj.nodeType === "undefined" ) {
1603 } else if ( obj.nodeType === 9 ) {
1605 } else if ( obj.nodeType ) {
1609 toString.call( obj ) === "[object Array
]" ||
1611 ( typeof obj.length === "number
" && typeof obj.item !== "undefined" && ( obj.length ? obj.item(0) === obj[0] : ( obj.item( 0 ) === null && typeof obj[0] === "undefined" ) ) )
1619 separator: function() {
1620 return this.multiline ? this.HTML ? "<br
/>" : "\n" : this.HTML ? " 
;" : " ";
1622 indent: function( extra ) {// extra can be a number, shortcut for increasing-calling-decreasing
1623 if ( !this.multiline ) {
1626 var chr = this.indentChar;
1628 chr = chr.replace( /\t/g, " " ).replace( / /g, " 
;" );
1630 return new Array( this._depth_ + (extra||0) ).join(chr);
1633 this._depth_ += a || 1;
1635 down: function( a ) {
1636 this._depth_ -= a || 1;
1638 setParser: function( name, parser ) {
1639 this.parsers[name] = parser;
1641 // The next 3 are exposed so you can use them
1647 // This is the list of parsers, to modify them, use jsDump.setParser
1650 document: "[Document
]",
1651 error: "[ERROR
]", //when no parser is found, shouldn"t happen
1652 unknown
: "[Unknown]",
1654 "undefined": "undefined",
1655 "function": function( fn
) {
1656 var ret
= "function",
1657 name
= "name" in fn
? fn
.name
: (reName
.exec(fn
) || [])[1];//functions never have name in IE
1664 ret
= [ ret
, QUnit
.jsDump
.parse( fn
, "functionArgs" ), "){" ].join( "" );
1665 return join( ret
, QUnit
.jsDump
.parse(fn
,"functionCode" ), "}" );
1670 object: function( map
, stack
) {
1671 var ret
= [ ], keys
, key
, val
, i
;
1673 if ( Object
.keys
) {
1674 keys
= Object
.keys( map
);
1677 for ( key
in map
) {
1682 for ( i
= 0; i
< keys
.length
; i
++ ) {
1685 ret
.push( QUnit
.jsDump
.parse( key
, "key" ) + ": " + QUnit
.jsDump
.parse( val
, undefined, stack
) );
1687 QUnit
.jsDump
.down();
1688 return join( "{", ret
, "}" );
1690 node: function( node
) {
1692 open
= QUnit
.jsDump
.HTML
? "<" : "<",
1693 close
= QUnit
.jsDump
.HTML
? ">" : ">",
1694 tag
= node
.nodeName
.toLowerCase(),
1697 for ( a
in QUnit
.jsDump
.DOMAttrs
) {
1698 val
= node
[ QUnit
.jsDump
.DOMAttrs
[a
] ];
1700 ret
+= " " + a
+ "=" + QUnit
.jsDump
.parse( val
, "attribute" );
1703 return ret
+ close
+ open
+ "/" + tag
+ close
;
1705 functionArgs: function( fn
) {//function calls it internally, it's the arguments part of the function
1713 args
= new Array(l
);
1715 args
[l
] = String
.fromCharCode(97+l
);//97 is 'a'
1717 return " " + args
.join( ", " ) + " ";
1719 key
: quote
, //object calls it internally, the key part of an item in a map
1720 functionCode
: "[code]", //function calls it internally, it's the content of the function
1721 attribute
: quote
, //node calls it internally, it's an html attribute value
1724 regexp
: literal
, //regex
1729 //attributes to dump from nodes, name=>realName
1732 "class": "className"
1734 HTML
: false,//if true, entities are escaped ( <, >, \t, space and \n )
1735 indentChar
: " ",//indentation unit
1736 multiline
: true //if true, items in a collection, are separated by a \n, else just a space.
1743 function getText( elems
) {
1747 for ( i
= 0; elems
[i
]; i
++ ) {
1750 // Get the text from text nodes and CDATA nodes
1751 if ( elem
.nodeType
=== 3 || elem
.nodeType
=== 4 ) {
1752 ret
+= elem
.nodeValue
;
1754 // Traverse everything else, except comment nodes
1755 } else if ( elem
.nodeType
!== 8 ) {
1756 ret
+= getText( elem
.childNodes
);
1764 function inArray( elem
, array
) {
1765 if ( array
.indexOf
) {
1766 return array
.indexOf( elem
);
1769 for ( var i
= 0, length
= array
.length
; i
< length
; i
++ ) {
1770 if ( array
[ i
] === elem
) {
1779 * Javascript Diff Algorithm
1780 * By John Resig (http://ejohn.org/)
1781 * Modified by Chu Alan "sprite"
1783 * Released under the MIT license.
1786 * http://ejohn.org/projects/javascript-diff-algorithm/
1788 * Usage: QUnit.diff(expected, actual)
1790 * 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"
1792 QUnit
.diff
= (function() {
1793 function diff( o
, n
) {
1798 for ( i
= 0; i
< n
.length
; i
++ ) {
1799 if ( ns
[ n
[i
] ] == null ) {
1805 ns
[ n
[i
] ].rows
.push( i
);
1808 for ( i
= 0; i
< o
.length
; i
++ ) {
1809 if ( os
[ o
[i
] ] == null ) {
1815 os
[ o
[i
] ].rows
.push( i
);
1819 if ( !hasOwn
.call( ns
, i
) ) {
1822 if ( ns
[i
].rows
.length
== 1 && typeof os
[i
] != "undefined" && os
[i
].rows
.length
== 1 ) {
1823 n
[ ns
[i
].rows
[0] ] = {
1824 text
: n
[ ns
[i
].rows
[0] ],
1827 o
[ os
[i
].rows
[0] ] = {
1828 text
: o
[ os
[i
].rows
[0] ],
1834 for ( i
= 0; i
< n
.length
- 1; i
++ ) {
1835 if ( n
[i
].text
!= null && n
[ i
+ 1 ].text
== null && n
[i
].row
+ 1 < o
.length
&& o
[ n
[i
].row
+ 1 ].text
== null &&
1836 n
[ i
+ 1 ] == o
[ n
[i
].row
+ 1 ] ) {
1842 o
[ n
[i
].row
+ 1 ] = {
1843 text
: o
[ n
[i
].row
+ 1 ],
1849 for ( i
= n
.length
- 1; i
> 0; i
-- ) {
1850 if ( n
[i
].text
!= null && n
[ i
- 1 ].text
== null && n
[i
].row
> 0 && o
[ n
[i
].row
- 1 ].text
== null &&
1851 n
[ i
- 1 ] == o
[ n
[i
].row
- 1 ]) {
1857 o
[ n
[i
].row
- 1 ] = {
1858 text
: o
[ n
[i
].row
- 1 ],
1870 return function( o
, n
) {
1871 o
= o
.replace( /\s+$/, "" );
1872 n
= n
.replace( /\s+$/, "" );
1876 out
= diff( o
=== "" ? [] : o
.split(/\s+/), n
=== "" ? [] : n
.split(/\s+/) ),
1877 oSpace
= o
.match(/\s+/g),
1878 nSpace
= n
.match(/\s+/g);
1880 if ( oSpace
== null ) {
1887 if ( nSpace
== null ) {
1894 if ( out
.n
.length
=== 0 ) {
1895 for ( i
= 0; i
< out
.o
.length
; i
++ ) {
1896 str
+= "<del>" + out
.o
[i
] + oSpace
[i
] + "</del>";
1900 if ( out
.n
[0].text
== null ) {
1901 for ( n
= 0; n
< out
.o
.length
&& out
.o
[n
].text
== null; n
++ ) {
1902 str
+= "<del>" + out
.o
[n
] + oSpace
[n
] + "</del>";
1906 for ( i
= 0; i
< out
.n
.length
; i
++ ) {
1907 if (out
.n
[i
].text
== null) {
1908 str
+= "<ins>" + out
.n
[i
] + nSpace
[i
] + "</ins>";
1911 // `pre` initialized at top of scope
1914 for ( n
= out
.n
[i
].row
+ 1; n
< out
.o
.length
&& out
.o
[n
].text
== null; n
++ ) {
1915 pre
+= "<del>" + out
.o
[n
] + oSpace
[n
] + "</del>";
1917 str
+= " " + out
.n
[i
].text
+ nSpace
[i
] + pre
;
1926 // for CommonJS enviroments, export everything
1927 if ( typeof exports
!== "undefined" ) {
1928 extend(exports
, QUnit
);
1931 // get at whatever the global object is, like window in browsers
1932 }( (function() {return this;}.call()) ));