2 * QUnit v1.7.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.
16 fileName = (sourceFromStacktrace( 0 ) || "" ).replace(/(:\d+)+\)?/, "").replace(/.+\//, ""),
17 toString = Object.prototype.toString,
18 hasOwn = Object.prototype.hasOwnProperty,
20 setTimeout: typeof window.setTimeout !== "undefined",
21 sessionStorage: (function() {
22 var x = "qunit-test-string";
24 sessionStorage.setItem( x, x );
25 sessionStorage.removeItem( x );
33 function Test( settings ) {
34 extend( this, settings );
36 this.testNumber = ++Test.count;
44 tests = id( "qunit-tests" );
47 b = document.createElement( "strong" );
48 b.innerHTML = this.name;
50 // `a` initialized at top of scope
51 a = document.createElement( "a" );
52 a.innerHTML = "Rerun";
53 a.href = QUnit.url({ testNumber: this.testNumber });
55 li = document.createElement( "li" );
58 li.className = "running";
59 li.id = this.id = "qunit-test-output" + testId++;
61 tests.appendChild( li );
65 if ( this.module !== config.previousModule ) {
66 if ( config.previousModule ) {
67 runLoggingCallbacks( "moduleDone", QUnit, {
68 name: config.previousModule,
69 failed: config.moduleStats.bad,
70 passed: config.moduleStats.all - config.moduleStats.bad,
71 total: config.moduleStats.all
74 config.previousModule = this.module;
75 config.moduleStats = { all: 0, bad: 0 };
76 runLoggingCallbacks( "moduleStart", QUnit, {
79 } else if ( config.autorun ) {
80 runLoggingCallbacks( "moduleStart", QUnit, {
85 config.current = this;
87 this.testEnvironment = extend({
89 teardown: function() {}
90 }, this.moduleTestEnvironment );
92 runLoggingCallbacks( "testStart", QUnit, {
97 // allow utility functions to access the current test environment
99 QUnit.current_testEnvironment = this.testEnvironment;
101 if ( !config.pollution ) {
104 if ( config.notrycatch ) {
105 this.testEnvironment.setup.call( this.testEnvironment );
109 this.testEnvironment.setup.call( this.testEnvironment );
111 QUnit.pushFailure( "Setup failed on " + this.testName + ": " + e.message, extractStacktrace( e, 1 ) );
115 config.current = this;
117 var running = id( "qunit-testresult" );
120 running.innerHTML = "Running: <br/>" + this.name;
127 if ( config.notrycatch ) {
128 this.callback.call( this.testEnvironment, QUnit.assert );
133 this.callback.call( this.testEnvironment, QUnit.assert );
135 QUnit.pushFailure( "Died on test #" + (this.assertions.length + 1) + " " + this.stack + ": " + e.message, extractStacktrace( e, 0 ) );
136 // else next test will carry the responsibility
139 // Restart the tests if they're blocking
140 if ( config.blocking ) {
145 teardown: function() {
146 config.current = this;
147 if ( config.notrycatch ) {
148 this.testEnvironment.teardown.call( this.testEnvironment );
152 this.testEnvironment.teardown.call( this.testEnvironment );
154 QUnit.pushFailure( "Teardown failed on " + this.testName + ": " + e.message, extractStacktrace( e, 1 ) );
160 config.current = this;
161 if ( config.requireExpects && this.expected == null ) {
162 QUnit.pushFailure( "Expected number of assertions to be defined, but expect() was not called.", this.stack );
163 } else if ( this.expected != null && this.expected != this.assertions.length ) {
164 QUnit.pushFailure( "Expected " + this.expected + " assertions, but " + this.assertions.length + " were run", this.stack );
165 } else if ( this.expected == null && !this.assertions.length ) {
166 QUnit.pushFailure( "Expected at least one assertion, but none were run - call expect(0) to accept zero assertions.", this.stack );
169 var assertion, a, b, i, li, ol,
173 tests = id( "qunit-tests" );
175 config.stats.all += this.assertions.length;
176 config.moduleStats.all += this.assertions.length;
179 ol = document.createElement( "ol" );
181 for ( i = 0; i < this.assertions.length; i++ ) {
182 assertion = this.assertions[i];
184 li = document.createElement( "li" );
185 li.className = assertion.result ? "pass" : "fail";
186 li.innerHTML = assertion.message || ( assertion.result ? "okay" : "failed" );
187 ol.appendChild( li );
189 if ( assertion.result ) {
194 config.moduleStats.bad++;
198 // store result when possible
199 if ( QUnit.config.reorder && defined.sessionStorage ) {
201 sessionStorage.setItem( "qunit-test-" + this.module + "-" + this.testName, bad );
203 sessionStorage.removeItem( "qunit-test-" + this.module + "-" + this.testName );
208 ol.style.display = "none";
211 // `b` initialized at top of scope
212 b = document.createElement( "strong" );
213 b.innerHTML = this.name + " <b class='counts'>(<b class='failed'>" + bad + "</b>, <b class='passed'>" + good + "</b>, " + this.assertions.length + ")</b>";
215 addEvent(b, "click", function() {
216 var next = b.nextSibling.nextSibling,
217 display = next.style.display;
218 next.style.display = display === "none" ? "block" : "none";
221 addEvent(b, "dblclick", function( e ) {
222 var target = e && e.target ? e.target : window.event.srcElement;
223 if ( target.nodeName.toLowerCase() == "span" || target.nodeName.toLowerCase() == "b" ) {
224 target = target.parentNode;
226 if ( window.location && target.nodeName.toLowerCase() === "strong" ) {
227 window.location = QUnit.url({ testNumber: test.testNumber });
231 // `li` initialized at top of scope
233 li.className = bad ? "fail" : "pass";
234 li.removeChild( li.firstChild );
237 li.appendChild ( a );
238 li.appendChild( ol );
241 for ( i = 0; i < this.assertions.length; i++ ) {
242 if ( !this.assertions[i].result ) {
245 config.moduleStats.bad++;
250 runLoggingCallbacks( "testDone", QUnit, {
254 passed: this.assertions.length - bad,
255 total: this.assertions.length
265 synchronize(function() {
269 // each of these can by async
270 synchronize(function() {
273 synchronize(function() {
276 synchronize(function() {
279 synchronize(function() {
284 // `bad` initialized at top of scope
285 // defer when previous test run passed, if storage is available
286 bad = QUnit.config.reorder && defined.sessionStorage &&
287 +sessionStorage.getItem( "qunit-test-" + this.module + "-" + this.testName );
292 synchronize( run, true );
297 // Root QUnit object.
298 // `QUnit` initialized at top of scope
301 // call on start of module test to prepend name to all tests
302 module: function( name, testEnvironment ) {
303 config.currentModule = name;
304 config.currentModuleTestEnviroment = testEnvironment;
307 asyncTest: function( testName, expected, callback ) {
308 if ( arguments.length === 2 ) {
313 QUnit.test( testName, expected, callback, true );
316 test: function( testName, expected, callback, async ) {
318 name = "<span class='test-name'>" + escapeInnerText( testName ) + "</span>";
320 if ( arguments.length === 2 ) {
325 if ( config.currentModule ) {
326 name = "<span class='module-name'>" + config.currentModule + "</span>: " + name;
335 module: config.currentModule,
336 moduleTestEnvironment: config.currentModuleTestEnviroment,
337 stack: sourceFromStacktrace( 2 )
340 if ( !validTest( test ) ) {
347 // Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
348 expect: function( asserts ) {
349 config.current.expected = asserts;
352 start: function( count ) {
353 config.semaphore -= count || 1;
354 // don't start until equal number of stop-calls
355 if ( config.semaphore > 0 ) {
358 // ignore if start is called more often then stop
359 if ( config.semaphore < 0 ) {
360 config.semaphore = 0;
362 // A slight delay, to avoid any current callbacks
363 if ( defined.setTimeout ) {
364 window.setTimeout(function() {
365 if ( config.semaphore > 0 ) {
368 if ( config.timeout ) {
369 clearTimeout( config.timeout );
372 config.blocking = false;
376 config.blocking = false;
381 stop: function( count ) {
382 config.semaphore += count || 1;
383 config.blocking = true;
385 if ( config.testTimeout && defined.setTimeout ) {
386 clearTimeout( config.timeout );
387 config.timeout = window.setTimeout(function() {
388 QUnit.ok( false, "Test timed out" );
389 config.semaphore = 1;
391 }, config.testTimeout );
397 // All of these must call either QUnit.push() or manually do:
398 // - runLoggingCallbacks( "log", .. );
399 // - config.current.assertions.push({ .. });
402 * Asserts rough true-ish result.
403 * @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" );
405 ok: function( result, msg ) {
406 if ( !config.current ) {
407 throw new Error( "ok() assertion outside test context, was " + sourceFromStacktrace(2) );
417 msg = escapeInnerText( msg || (result ? "okay" : "failed" ) );
418 msg = "<span class='test-message'>" + msg + "</span>";
421 source = sourceFromStacktrace( 2 );
423 details.source = source;
424 msg += "<table><tr class='test-source'><th>Source: </th><td><pre>" + escapeInnerText( source ) + "</pre></td></tr></table>";
427 runLoggingCallbacks( "log", QUnit, details );
428 config.current.assertions.push({
435 * Assert that the first two arguments are equal, with an optional message.
436 * Prints out both actual and expected values.
437 * @example equal( format( "Received {0} bytes.", 2), "Received 2 bytes.", "format() replaces {0} with next argument" );
439 equal: function( actual, expected, message ) {
440 QUnit.push( expected == actual, actual, expected, message );
443 notEqual: function( actual, expected, message ) {
444 QUnit.push( expected != actual, actual, expected, message );
447 deepEqual: function( actual, expected, message ) {
448 QUnit.push( QUnit.equiv(actual, expected), actual, expected, message );
451 notDeepEqual: function( actual, expected, message ) {
452 QUnit.push( !QUnit.equiv(actual, expected), actual, expected, message );
455 strictEqual: function( actual, expected, message ) {
456 QUnit.push( expected === actual, actual, expected, message );
459 notStrictEqual: function( actual, expected, message ) {
460 QUnit.push( expected !== actual, actual, expected, message );
463 raises: function( block, expected, message ) {
467 if ( typeof expected === "string" ) {
473 block.call( config.current.testEnvironment );
479 // we don't want to validate thrown error
482 // expected is a regexp
483 } else if ( QUnit.objectType( expected ) === "regexp" ) {
484 ok = expected.test( actual );
485 // expected is a constructor
486 } else if ( actual instanceof expected ) {
488 // expected is a validation function which returns true is validation passed
489 } else if ( expected.call( {}, actual ) === true ) {
494 QUnit.push( ok, actual, null, message );
498 // @deprecated: Kept assertion helpers in root for backwards compatibility
499 extend( QUnit, QUnit.assert );
502 * @deprecated: Kept for backwards compatibility
503 * next step: remove entirely
505 QUnit.equals = function() {
506 QUnit.push( false, false, false, "QUnit.equals has been deprecated since 2009 (e88049a0), use QUnit.equal instead" );
508 QUnit.same = function() {
509 QUnit.push( false, false, false, "QUnit.same has been deprecated since 2009 (e88049a0), use QUnit.deepEqual instead" );
512 // We want access to the constructor's prototype
517 // Make F QUnit's constructor so that we can add to the prototype later
518 QUnit.constructor = F;
522 * Config object: Maintain internal state
523 * Later exposed as QUnit.config
524 * `config` initialized at top of scope
527 // The queue of tests to run
530 // block until document ready
533 // when enabled, show only failing tests
534 // gets persisted through sessionStorage and can be changed in UI via checkbox
537 // by default, run previously failed tests first
538 // very useful in combination with "Hide passed tests" checked
541 // by default, modify document.title when suite is done
544 // when enabled, all tests must call expect()
545 requireExpects: false,
547 urlConfig: [ "noglobals", "notrycatch" ],
549 // logging callback queues
559 // Initialize more QUnit.config and QUnit.urlParams
562 location = window.location || { search: "", protocol: "file:" },
563 params = location.search.slice( 1 ).split( "&" ),
564 length = params.length,
569 for ( i = 0; i < length; i++ ) {
570 current = params[ i ].split( "=" );
571 current[ 0 ] = decodeURIComponent( current[ 0 ] );
572 // allow just a key to turn on a flag, e.g., test.html?noglobals
573 current[ 1 ] = current[ 1 ] ? decodeURIComponent( current[ 1 ] ) : true;
574 urlParams[ current[ 0 ] ] = current[ 1 ];
578 QUnit.urlParams = urlParams;
579 config.filter = urlParams.filter;
580 config.testNumber = parseInt( urlParams.testNumber, 10 ) || null;
582 // Figure out if we're running the tests from a server or not
583 QUnit.isLocal = location.protocol === "file:";
586 // Export global variables, unless an 'exports' object exists,
587 // in that case we assume we're in CommonJS (dealt with on the bottom of the script)
588 if ( typeof exports === "undefined" ) {
589 extend( window, QUnit );
591 // Expose QUnit object
592 window.QUnit = QUnit;
595 // Extend QUnit object,
596 // these after set here because they should not be exposed as global functions
600 // Initialize the configuration options
603 stats: { all: 0, bad: 0 },
604 moduleStats: { all: 0, bad: 0 },
605 started: +new Date(),
615 var tests, banner, result,
616 qunit = id( "qunit" );
620 "<h1 id='qunit-header'>" + escapeInnerText( document.title ) + "</h1>" +
621 "<h2 id='qunit-banner'></h2>" +
622 "<div id='qunit-testrunner-toolbar'></div>" +
623 "<h2 id='qunit-userAgent'></h2>" +
624 "<ol id='qunit-tests'></ol>";
627 tests = id( "qunit-tests" );
628 banner = id( "qunit-banner" );
629 result = id( "qunit-testresult" );
632 tests.innerHTML = "";
636 banner.className = "";
640 result.parentNode.removeChild( result );
644 result = document.createElement( "p" );
645 result.id = "qunit-testresult";
646 result.className = "result";
647 tests.parentNode.insertBefore( result, tests );
648 result.innerHTML = "Running...<br/> ";
652 // Resets the test setup. Useful for tests that modify the DOM.
653 // If jQuery is available, uses jQuery's html(), otherwise just innerHTML.
657 if ( window.jQuery ) {
658 jQuery( "#qunit-fixture" ).html( config.fixture );
660 fixture = id( "qunit-fixture" );
662 fixture.innerHTML = config.fixture;
667 // Trigger an event on an element.
668 // @example triggerEvent( document.body, "click" );
669 triggerEvent: function( elem, type, event ) {
670 if ( document.createEvent ) {
671 event = document.createEvent( "MouseEvents" );
672 event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
673 0, 0, 0, 0, 0, false, false, false, false, 0, null);
675 elem.dispatchEvent( event );
676 } else if ( elem.fireEvent ) {
677 elem.fireEvent( "on" + type );
681 // Safe object type checking
682 is: function( type, obj ) {
683 return QUnit.objectType( obj ) == type;
686 objectType: function( obj ) {
687 if ( typeof obj === "undefined" ) {
689 // consider: typeof null === object
691 if ( obj === null ) {
695 var type = toString.call( obj ).match(/^\[object\s(.*)\]$/)[1] || "";
709 return type.toLowerCase();
711 if ( typeof obj === "object" ) {
717 push: function( result, actual, expected, message ) {
718 if ( !config.current ) {
719 throw new Error( "assertion outside test context, was " + sourceFromStacktrace() );
730 message = escapeInnerText( message ) || ( result ? "okay" : "failed" );
731 message = "<span class='test-message'>" + message + "</span>";
735 expected = escapeInnerText( QUnit.jsDump.parse(expected) );
736 actual = escapeInnerText( QUnit.jsDump.parse(actual) );
737 output += "<table><tr class='test-expected'><th>Expected: </th><td><pre>" + expected + "</pre></td></tr>";
739 if ( actual != expected ) {
740 output += "<tr class='test-actual'><th>Result: </th><td><pre>" + actual + "</pre></td></tr>";
741 output += "<tr class='test-diff'><th>Diff: </th><td><pre>" + QUnit.diff( expected, actual ) + "</pre></td></tr>";
744 source = sourceFromStacktrace();
747 details.source = source;
748 output += "<tr class='test-source'><th>Source: </th><td><pre>" + escapeInnerText( source ) + "</pre></td></tr>";
751 output += "</table>";
754 runLoggingCallbacks( "log", QUnit, details );
756 config.current.assertions.push({
762 pushFailure: function( message, source ) {
769 message = escapeInnerText(message ) || "error";
770 message = "<span class='test-message'>" + message + "</span>";
774 details.source = source;
775 output += "<table><tr class='test-source'><th>Source: </th><td><pre>" + escapeInnerText( source ) + "</pre></td></tr></table>";
778 runLoggingCallbacks( "log", QUnit, details );
780 config.current.assertions.push({
786 url: function( params ) {
787 params = extend( extend( {}, QUnit.urlParams ), params );
791 for ( key in params ) {
792 if ( !hasOwn.call( params, key ) ) {
795 querystring += encodeURIComponent( key ) + "=" +
796 encodeURIComponent( params[ key ] ) + "&";
798 return window.location.pathname + querystring.slice( 0, -1 );
804 // load, equiv, jsDump, diff: Attached later
808 * @deprecated: Created for backwards compatibility with test runner that set the hook function
809 * into QUnit.{hook}, instead of invoking it and passing the hook function.
810 * QUnit.constructor is set to the empty F() above so that we can add to it's prototype here.
811 * Doing this allows us to tell if the following methods have been overwritten on the actual
814 extend( QUnit.constructor.prototype, {
816 // Logging callbacks; all receive a single argument with the listed properties
817 // run test/logs.html for any related changes
818 begin: registerLoggingCallback( "begin" ),
820 // done: { failed, passed, total, runtime }
821 done: registerLoggingCallback( "done" ),
823 // log: { result, actual, expected, message }
824 log: registerLoggingCallback( "log" ),
826 // testStart: { name }
827 testStart: registerLoggingCallback( "testStart" ),
829 // testDone: { name, failed, passed, total }
830 testDone: registerLoggingCallback( "testDone" ),
832 // moduleStart: { name }
833 moduleStart: registerLoggingCallback( "moduleStart" ),
835 // moduleDone: { name, failed, passed, total }
836 moduleDone: registerLoggingCallback( "moduleDone" )
839 if ( typeof document === "undefined" || document.readyState === "complete" ) {
840 config.autorun = true;
843 QUnit.load = function() {
844 runLoggingCallbacks( "begin", QUnit, {} );
846 // Initialize the config, saving the execution queue
847 var banner, filter, i, label, len, main, ol, toolbar, userAgent, val,
849 oldconfig = extend( {}, config );
852 extend(config, oldconfig);
854 config.blocking = false;
856 len = config.urlConfig.length;
858 for ( i = 0; i < len; i++ ) {
859 val = config.urlConfig[i];
860 config[val] = QUnit.urlParams[val];
861 urlConfigHtml += "<label><input name='" + val + "' type='checkbox'" + ( config[val] ? " checked='checked'" : "" ) + ">" + val + "</label>";
864 // `userAgent` initialized at top of scope
865 userAgent = id( "qunit-userAgent" );
867 userAgent.innerHTML = navigator.userAgent;
870 // `banner` initialized at top of scope
871 banner = id( "qunit-header" );
873 banner.innerHTML = "<a href='" + QUnit.url({ filter: undefined }) + "'>" + banner.innerHTML + "</a> " + urlConfigHtml;
874 addEvent( banner, "change", function( event ) {
876 params[ event.target.name ] = event.target.checked ? true : undefined;
877 window.location = QUnit.url( params );
881 // `toolbar` initialized at top of scope
882 toolbar = id( "qunit-testrunner-toolbar" );
884 // `filter` initialized at top of scope
885 filter = document.createElement( "input" );
886 filter.type = "checkbox";
887 filter.id = "qunit-filter-pass";
889 addEvent( filter, "click", function() {
891 ol = document.getElementById( "qunit-tests" );
893 if ( filter.checked ) {
894 ol.className = ol.className + " hidepass";
896 tmp = " " + ol.className.replace( /[\n\t\r]/g, " " ) + " ";
897 ol.className = tmp.replace( / hidepass /, " " );
899 if ( defined.sessionStorage ) {
900 if (filter.checked) {
901 sessionStorage.setItem( "qunit-filter-passed-tests", "true" );
903 sessionStorage.removeItem( "qunit-filter-passed-tests" );
908 if ( config.hidepassed || defined.sessionStorage && sessionStorage.getItem( "qunit-filter-passed-tests" ) ) {
909 filter.checked = true;
910 // `ol` initialized at top of scope
911 ol = document.getElementById( "qunit-tests" );
912 ol.className = ol.className + " hidepass";
914 toolbar.appendChild( filter );
916 // `label` initialized at top of scope
917 label = document.createElement( "label" );
918 label.setAttribute( "for", "qunit-filter-pass" );
919 label.innerHTML = "Hide passed tests";
920 toolbar.appendChild( label );
923 // `main` initialized at top of scope
924 main = id( "qunit-fixture" );
926 config.fixture = main.innerHTML;
929 if ( config.autostart ) {
934 addEvent( window, "load", QUnit.load );
936 // addEvent(window, "error" ) gives us a useless event object
937 window.onerror = function( message, file, line ) {
938 if ( QUnit.config.current ) {
939 QUnit.pushFailure( message, file + ":" + line );
941 QUnit.test( "global failure", function() {
942 QUnit.pushFailure( message, file + ":" + line );
948 config.autorun = true;
950 // Log the last module results
951 if ( config.currentModule ) {
952 runLoggingCallbacks( "moduleDone", QUnit, {
953 name: config.currentModule,
954 failed: config.moduleStats.bad,
955 passed: config.moduleStats.all - config.moduleStats.bad,
956 total: config.moduleStats.all
961 banner = id( "qunit-banner" ),
962 tests = id( "qunit-tests" ),
963 runtime = +new Date() - config.started,
964 passed = config.stats.all - config.stats.bad,
966 "Tests completed in ",
968 " milliseconds.<br/>",
969 "<span class='passed'>",
971 "</span> tests of <span class='total'>",
973 "</span> passed, <span class='failed'>",
979 banner.className = ( config.stats.bad ? "qunit-fail" : "qunit-pass" );
983 id( "qunit-testresult" ).innerHTML = html;
986 if ( config.altertitle && typeof document !== "undefined" && document.title ) {
987 // show ✖ for good, ✔ for bad suite result in title
988 // use escape sequences in case file gets loaded with non-utf-8-charset
990 ( config.stats.bad ? "\u2716" : "\u2714" ),
991 document.title.replace( /^[\u2714\u2716] /i, "" )
995 // clear own sessionStorage items if all tests passed
996 if ( config.reorder && defined.sessionStorage && config.stats.bad === 0 ) {
997 // `key` & `i` initialized at top of scope
998 for ( i = 0; i < sessionStorage.length; i++ ) {
999 key = sessionStorage.key( i++ );
1000 if ( key.indexOf( "qunit-test-" ) === 0 ) {
1001 sessionStorage.removeItem( key );
1006 runLoggingCallbacks( "done", QUnit, {
1007 failed: config.stats.bad,
1009 total: config.stats.all,
1014 function validTest( test ) {
1016 filter = config.filter && config.filter.toLowerCase(),
1017 fullName = (test.module + ": " + test.testName).toLowerCase();
1019 if ( config.testNumber ) {
1020 return test.testNumber === config.testNumber;
1027 include = filter.charAt( 0 ) !== "!";
1029 filter = filter.slice( 1 );
1032 // If the filter matches, we need to honour include
1033 if ( fullName.indexOf( filter ) !== -1 ) {
1037 // Otherwise, do the opposite
1041 // so far supports only Firefox, Chrome and Opera (buggy), Safari (for real exceptions)
1042 // Later Safari and IE10 are supposed to support error.stack as well
1043 // See also https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error/Stack
1044 function extractStacktrace( e, offset ) {
1045 offset = offset === undefined ? 3 : offset;
1047 var stack, include, i, regex;
1049 if ( e.stacktrace ) {
1051 return e.stacktrace.split( "\n" )[ offset + 3 ];
1052 } else if ( e.stack ) {
1054 stack = e.stack.split( "\n" );
1055 if (/^error$/i.test( stack[0] ) ) {
1060 for ( i = offset; i < stack.length; i++ ) {
1061 if ( stack[ i ].indexOf( fileName ) != -1 ) {
1064 include.push( stack[ i ] );
1066 if ( include.length ) {
1067 return include.join( "\n" );
1070 return stack[ offset ];
1071 } else if ( e.sourceURL ) {
1072 // Safari, PhantomJS
1073 // hopefully one day Safari provides actual stacktraces
1074 // exclude useless self-reference for generated Error objects
1075 if ( /qunit.js$/.test( e.sourceURL ) ) {
1078 // for actual exceptions, this is useful
1079 return e.sourceURL + ":" + e.line;
1082 function sourceFromStacktrace( offset ) {
1086 return extractStacktrace( e, offset );
1090 function escapeInnerText( s ) {
1095 return s.replace( /[\&<>]/g, function( s ) {
1097 case "&": return "&";
1098 case "<": return "<";
1099 case ">": return ">";
1105 function synchronize( callback, last ) {
1106 config.queue.push( callback );
1108 if ( config.autorun && !config.blocking ) {
1113 function process( last ) {
1117 var start = new Date().getTime();
1118 config.depth = config.depth ? config.depth + 1 : 1;
1120 while ( config.queue.length && !config.blocking ) {
1121 if ( !defined.setTimeout || config.updateRate <= 0 || ( ( new Date().getTime() - start ) < config.updateRate ) ) {
1122 config.queue.shift()();
1124 window.setTimeout( next, 13 );
1129 if ( last && !config.blocking && !config.queue.length && config.depth === 0 ) {
1134 function saveGlobal() {
1135 config.pollution = [];
1137 if ( config.noglobals ) {
1138 for ( var key in window ) {
1139 // in Opera sometimes DOM element ids show up here, ignore them
1140 if ( !hasOwn.call( window, key ) || /^qunit-test-output/.test( key ) ) {
1143 config.pollution.push( key );
1148 function checkPollution( name ) {
1151 old = config.pollution;
1155 newGlobals = diff( config.pollution, old );
1156 if ( newGlobals.length > 0 ) {
1157 QUnit.pushFailure( "Introduced global variable(s): " + newGlobals.join(", ") );
1160 deletedGlobals = diff( old, config.pollution );
1161 if ( deletedGlobals.length > 0 ) {
1162 QUnit.pushFailure( "Deleted global variable(s): " + deletedGlobals.join(", ") );
1166 // returns a new Array with the elements that are in a but not in b
1167 function diff( a, b ) {
1171 for ( i = 0; i < result.length; i++ ) {
1172 for ( j = 0; j < b.length; j++ ) {
1173 if ( result[i] === b[j] ) {
1174 result.splice( i, 1 );
1183 function extend( a, b ) {
1184 for ( var prop in b ) {
1185 if ( b[ prop ] === undefined ) {
1188 // Avoid "Member not found" error in IE8 caused by setting window.constructor
1189 } else if ( prop !== "constructor" || a !== window ) {
1190 a[ prop ] = b[ prop ];
1197 function addEvent( elem, type, fn ) {
1198 if ( elem.addEventListener ) {
1199 elem.addEventListener( type, fn, false );
1200 } else if ( elem.attachEvent ) {
1201 elem.attachEvent( "on" + type, fn );
1207 function id( name ) {
1208 return !!( typeof document !== "undefined" && document && document.getElementById ) &&
1209 document.getElementById( name );
1212 function registerLoggingCallback( key ) {
1213 return function( callback ) {
1214 config[key].push( callback );
1218 // Supports deprecated method of completely overwriting logging callbacks
1219 function runLoggingCallbacks( key, scope, args ) {
1222 if ( QUnit.hasOwnProperty( key ) ) {
1223 QUnit[ key ].call(scope, args );
1225 callbacks = config[ key ];
1226 for ( i = 0; i < callbacks.length; i++ ) {
1227 callbacks[ i ].call( scope, args );
1232 // Test for equality any JavaScript type.
1233 // Author: Philippe Rathé <prathe@gmail.com>
1234 QUnit.equiv = (function() {
1236 // Call the o related callback with the given arguments.
1237 function bindCallbacks( o, callbacks, args ) {
1238 var prop = QUnit.objectType( o );
1240 if ( QUnit.objectType( callbacks[ prop ] ) === "function" ) {
1241 return callbacks[ prop ].apply( callbacks, args );
1243 return callbacks[ prop ]; // or undefined
1248 // the real equiv function
1250 // stack to decide between skip/abort functions
1252 // stack to avoiding loops from circular referencing
1255 getProto = Object.getPrototypeOf || function ( obj ) {
1256 return obj.__proto__;
1258 callbacks = (function () {
1260 // for string, boolean, number and null
1261 function useStrictEquality( b, a ) {
1262 if ( b instanceof a.constructor || a instanceof b.constructor ) {
1263 // to catch short annotaion VS 'new' annotation of a
1266 // var j = new Number(1);
1274 "string": useStrictEquality,
1275 "boolean": useStrictEquality,
1276 "number": useStrictEquality,
1277 "null": useStrictEquality,
1278 "undefined": useStrictEquality,
1280 "nan": function( b ) {
1284 "date": function( b, a ) {
1285 return QUnit.objectType( b ) === "date" && a.valueOf() === b.valueOf();
1288 "regexp": function( b, a ) {
1289 return QUnit.objectType( b ) === "regexp" &&
1291 a.source === b.source &&
1293 a.global === b.global &&
1295 a.ignoreCase === b.ignoreCase &&
1296 a.multiline === b.multiline;
1299 // - skip when the property is a method of an instance (OOP)
1300 // - abort otherwise,
1301 // initial === would have catch identical references anyway
1302 "function": function() {
1303 var caller = callers[callers.length - 1];
1304 return caller !== Object && typeof caller !== "undefined";
1307 "array": function( b, a ) {
1308 var i, j, len, loop;
1310 // b could be an object literal here
1311 if ( QUnit.objectType( b ) !== "array" ) {
1316 if ( len !== b.length ) {
1321 // track reference to avoid circular references
1323 for ( i = 0; i < len; i++ ) {
1325 for ( j = 0; j < parents.length; j++ ) {
1326 if ( parents[j] === a[i] ) {
1327 loop = true;// dont rewalk array
1330 if ( !loop && !innerEquiv(a[i], b[i]) ) {
1339 "object": function( b, a ) {
1346 // comparing constructors is more strict than using
1348 if ( a.constructor !== b.constructor ) {
1349 // Allow objects with no prototype to be equivalent to
1350 // objects with Object as their constructor.
1351 if ( !(( getProto(a) === null && getProto(b) === Object.prototype ) ||
1352 ( getProto(b) === null && getProto(a) === Object.prototype ) ) ) {
1357 // stack constructor before traversing properties
1358 callers.push( a.constructor );
1359 // track reference to avoid circular references
1362 for ( i in a ) { // be strict: don't ensures hasOwnProperty
1365 for ( j = 0; j < parents.length; j++ ) {
1366 if ( parents[j] === a[i] ) {
1367 // don't go down the same path twice
1371 aProperties.push(i); // collect a's properties
1373 if (!loop && !innerEquiv( a[i], b[i] ) ) {
1379 callers.pop(); // unstack, we are done
1383 bProperties.push( i ); // collect b's properties
1386 // Ensures identical properties name
1387 return eq && innerEquiv( aProperties.sort(), bProperties.sort() );
1392 innerEquiv = function() { // can take multiple arguments
1393 var args = [].slice.apply( arguments );
1394 if ( args.length < 2 ) {
1395 return true; // end transition
1398 return (function( a, b ) {
1400 return true; // catch the most you can
1401 } else if ( a === null || b === null || typeof a === "undefined" ||
1402 typeof b === "undefined" ||
1403 QUnit.objectType(a) !== QUnit.objectType(b) ) {
1404 return false; // don't lose time with error prone cases
1406 return bindCallbacks(a, callbacks, [ b, a ]);
1409 // apply transition with (1..n) arguments
1410 }( args[0], args[1] ) && arguments.callee.apply( this, args.splice(1, args.length - 1 )) );
1417 * jsDump Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com |
1418 * http://flesler.blogspot.com Licensed under BSD
1419 * (http://www.opensource.org/licenses/bsd-license.php) Date: 5/15/2008
1421 * @projectDescription Advanced and extensible data dumping for Javascript.
1423 * @author Ariel Flesler
1424 * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
1426 QUnit.jsDump = (function() {
1427 function quote( str ) {
1428 return '"' + str.toString().replace( /"/g, '\\"' ) + '"';
1430 function literal( o ) {
1433 function join( pre, arr, post ) {
1434 var s = jsDump.separator(),
1435 base = jsDump.indent(),
1436 inner = jsDump.indent(1);
1438 arr = arr.join( "," + s + inner );
1443 return [ pre, inner + arr, base + post ].join(s);
1445 function array( arr, stack ) {
1446 var i = arr.length, ret = new Array(i);
1449 ret[i] = this.parse( arr[i] , undefined , stack);
1452 return join( "[", ret, "]" );
1455 var reName = /^function (\w+)/,
1457 parse: function( obj, type, stack ) { //type is used mostly internally, you can fix a (custom)type in advance
1458 stack = stack || [ ];
1460 parser = this.parsers[ type || this.typeOf(obj) ];
1462 type = typeof parser;
1463 inStack = inArray( obj, stack );
1465 if ( inStack != -1 ) {
1466 return "recursion(" + (inStack - stack.length) + ")";
1469 if ( type == "function" ) {
1471 res = parser.call( this, obj, stack );
1476 return ( type == "string" ) ? parser : this.parsers.error;
1478 typeOf: function( obj ) {
1480 if ( obj === null ) {
1482 } else if ( typeof obj === "undefined" ) {
1484 } else if ( QUnit.is( "regexp", obj) ) {
1486 } else if ( QUnit.is( "date", obj) ) {
1488 } else if ( QUnit.is( "function", obj) ) {
1490 } else if ( typeof obj.setInterval !== undefined && typeof obj.document !== "undefined" && typeof obj.nodeType === "undefined" ) {
1492 } else if ( obj.nodeType === 9 ) {
1494 } else if ( obj.nodeType ) {
1498 toString.call( obj ) === "[object Array]" ||
1500 ( typeof obj.length === "number" && typeof obj.item !== "undefined" && ( obj.length ? obj.item(0) === obj[0] : ( obj.item( 0 ) === null && typeof obj[0] === "undefined" ) ) )
1508 separator: function() {
1509 return this.multiline ? this.HTML ? "<br />" : "\n" : this.HTML ? " " : " ";
1511 indent: function( extra ) {// extra can be a number, shortcut for increasing-calling-decreasing
1512 if ( !this.multiline ) {
1515 var chr = this.indentChar;
1517 chr = chr.replace( /\t/g, " " ).replace( / /g, " " );
1519 return new Array( this._depth_ + (extra||0) ).join(chr);
1522 this._depth_ += a || 1;
1524 down: function( a ) {
1525 this._depth_ -= a || 1;
1527 setParser: function( name, parser ) {
1528 this.parsers[name] = parser;
1530 // The next 3 are exposed so you can use them
1536 // This is the list of parsers, to modify them, use jsDump.setParser
1539 document: "[Document]",
1540 error: "[ERROR]", //when no parser is found, shouldn"t happen
1541 unknown: "[Unknown]",
1543 "undefined": "undefined",
1544 "function": function( fn ) {
1545 var ret = "function",
1546 name = "name" in fn ? fn.name : (reName.exec(fn) || [])[1];//functions never have name in IE
1553 ret = [ ret, QUnit.jsDump.parse( fn, "functionArgs" ), "){" ].join( "" );
1554 return join( ret, QUnit.jsDump.parse(fn,"functionCode" ), "}" );
1559 object: function( map, stack ) {
1560 var ret = [ ], keys, key, val, i;
1562 if ( Object.keys ) {
1563 keys = Object.keys( map );
1566 for ( key in map ) {
1571 for ( i = 0; i < keys.length; i++ ) {
1574 ret.push( QUnit.jsDump.parse( key, "key" ) + ": " + QUnit.jsDump.parse( val, undefined, stack ) );
1576 QUnit.jsDump.down();
1577 return join( "{", ret, "}" );
1579 node: function( node ) {
1581 open = QUnit.jsDump.HTML ? "<" : "<",
1582 close = QUnit.jsDump.HTML ? ">" : ">",
1583 tag = node.nodeName.toLowerCase(),
1586 for ( a in QUnit.jsDump.DOMAttrs ) {
1587 val = node[ QUnit.jsDump.DOMAttrs[a] ];
1589 ret += " " + a + "=" + QUnit.jsDump.parse( val, "attribute" );
1592 return ret + close + open + "/" + tag + close;
1594 functionArgs: function( fn ) {//function calls it internally, it's the arguments part of the function
1602 args = new Array(l);
1604 args[l] = String.fromCharCode(97+l);//97 is 'a'
1606 return " " + args.join( ", " ) + " ";
1608 key: quote, //object calls it internally, the key part of an item in a map
1609 functionCode: "[code]", //function calls it internally, it's the content of the function
1610 attribute: quote, //node calls it internally, it's an html attribute value
1613 regexp: literal, //regex
1618 //attributes to dump from nodes, name=>realName
1621 "class": "className"
1623 HTML: false,//if true, entities are escaped ( <, >, \t, space and \n )
1624 indentChar: " ",//indentation unit
1625 multiline: true //if true, items in a collection, are separated by a \n, else just a space.
1632 function getText( elems ) {
1636 for ( i = 0; elems[i]; i++ ) {
1639 // Get the text from text nodes and CDATA nodes
1640 if ( elem.nodeType === 3 || elem.nodeType === 4 ) {
1641 ret += elem.nodeValue;
1643 // Traverse everything else, except comment nodes
1644 } else if ( elem.nodeType !== 8 ) {
1645 ret += getText( elem.childNodes );
1653 function inArray( elem, array ) {
1654 if ( array.indexOf ) {
1655 return array.indexOf( elem );
1658 for ( var i = 0, length = array.length; i < length; i++ ) {
1659 if ( array[ i ] === elem ) {
1668 * Javascript Diff Algorithm
1669 * By John Resig (http://ejohn.org/)
1670 * Modified by Chu Alan "sprite"
1672 * Released under the MIT license.
1675 * http://ejohn.org/projects/javascript-diff-algorithm/
1677 * Usage: QUnit.diff(expected, actual)
1679 * 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"
1681 QUnit.diff = (function() {
1682 function diff( o, n ) {
1687 for ( i = 0; i < n.length; i++ ) {
1688 if ( ns[ n[i] ] == null ) {
1694 ns[ n[i] ].rows.push( i );
1697 for ( i = 0; i < o.length; i++ ) {
1698 if ( os[ o[i] ] == null ) {
1704 os[ o[i] ].rows.push( i );
1708 if ( !hasOwn.call( ns, i ) ) {
1711 if ( ns[i].rows.length == 1 && typeof os[i] != "undefined" && os[i].rows.length == 1 ) {
1712 n[ ns[i].rows[0] ] = {
1713 text: n[ ns[i].rows[0] ],
1716 o[ os[i].rows[0] ] = {
1717 text: o[ os[i].rows[0] ],
1723 for ( i = 0; i < n.length - 1; i++ ) {
1724 if ( n[i].text != null && n[ i + 1 ].text == null && n[i].row + 1 < o.length && o[ n[i].row + 1 ].text == null &&
1725 n[ i + 1 ] == o[ n[i].row + 1 ] ) {
1731 o[ n[i].row + 1 ] = {
1732 text: o[ n[i].row + 1 ],
1738 for ( i = n.length - 1; i > 0; i-- ) {
1739 if ( n[i].text != null && n[ i - 1 ].text == null && n[i].row > 0 && o[ n[i].row - 1 ].text == null &&
1740 n[ i - 1 ] == o[ n[i].row - 1 ]) {
1746 o[ n[i].row - 1 ] = {
1747 text: o[ n[i].row - 1 ],
1759 return function( o, n ) {
1760 o = o.replace( /\s+$/, "" );
1761 n = n.replace( /\s+$/, "" );
1765 out = diff( o === "" ? [] : o.split(/\s+/), n === "" ? [] : n.split(/\s+/) ),
1766 oSpace = o.match(/\s+/g),
1767 nSpace = n.match(/\s+/g);
1769 if ( oSpace == null ) {
1776 if ( nSpace == null ) {
1783 if ( out.n.length === 0 ) {
1784 for ( i = 0; i < out.o.length; i++ ) {
1785 str += "<del>" + out.o[i] + oSpace[i] + "</del>";
1789 if ( out.n[0].text == null ) {
1790 for ( n = 0; n < out.o.length && out.o[n].text == null; n++ ) {
1791 str += "<del>" + out.o[n] + oSpace[n] + "</del>";
1795 for ( i = 0; i < out.n.length; i++ ) {
1796 if (out.n[i].text == null) {
1797 str += "<ins>" + out.n[i] + nSpace[i] + "</ins>";
1800 // `pre` initialized at top of scope
1803 for ( n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++ ) {
1804 pre += "<del>" + out.o[n] + oSpace[n] + "</del>";
1806 str += " " + out.n[i].text + nSpace[i] + pre;
1815 // for CommonJS enviroments, export everything
1816 if ( typeof exports !== "undefined" ) {
1817 extend(exports, QUnit);
1820 // get at whatever the global object is, like window in browsers
1821 }( (function() {return this;}.call()) ));