142
St
startsInvasion / files
startsInvasion / libs / node_modules / bean / support / syn / resources / qunit / qunit.js
29.4 KB
1/* @documentjs-ignore 2 * QUnit - A JavaScript Unit Testing Framework 3 * 4 * http://docs.jquery.com/QUnit 5 * 6 * Copyright (c) 2009 John Resig, J�rn Zaefferer 7 * Dual licensed under the MIT (MIT-LICENSE.txt) 8 * and GPL (GPL-LICENSE.txt) licenses. 9 */ 10 11(function(window) { 12 13var QUnit = { 14 15 // Initialize the configuration options 16 init: function() { 17 extend(config, { 18 stats: { all: 0, bad: 0 }, 19 moduleStats: { all: 0, bad: 0 }, 20 started: +new Date, 21 updateRate: 1000, 22 blocking: false, 23 autostart: true, 24 autorun: false, 25 assertions: [], 26 filters: [], 27 queue: [] 28 }); 29 30 var tests = id("qunit-tests"), 31 banner = id("qunit-banner"), 32 result = id("qunit-testresult"); 33 34 if ( tests ) { 35 tests.innerHTML = ""; 36 } 37 38 if ( banner ) { 39 banner.className = ""; 40 } 41 42 if ( result ) { 43 result.parentNode.removeChild( result ); 44 } 45 }, 46 47 // call on start of module test to prepend name to all tests 48 module: function(name, testEnvironment) { 49 config.currentModule = name; 50 51 synchronize(function() { 52 if ( config.currentModule ) { 53 QUnit.moduleDone( config.currentModule, config.moduleStats.bad, config.moduleStats.all ); 54 } 55 56 config.currentModule = name; 57 config.moduleTestEnvironment = testEnvironment; 58 config.moduleStats = { all: 0, bad: 0 }; 59 60 QUnit.moduleStart( name, testEnvironment ); 61 }, true); 62 }, 63 64 asyncTest: function(testName, expected, callback) { 65 if ( arguments.length === 2 ) { 66 callback = expected; 67 expected = 0; 68 } 69 70 QUnit.test(testName, expected, callback, true); 71 }, 72 73 test: function(testName, expected, callback, async) { 74 var name = testName, testEnvironment, testEnvironmentArg; 75 76 if ( arguments.length === 2 ) { 77 callback = expected; 78 expected = null; 79 } 80 // is 2nd argument a testEnvironment? 81 if ( expected && typeof expected === 'object') { 82 testEnvironmentArg = expected; 83 expected = null; 84 } 85 86 if ( config.currentModule ) { 87 name = config.currentModule + " module: " + name; 88 } 89 90 if ( !validTest(name) ) { 91 return; 92 } 93 94 synchronize(function() { 95 96 testEnvironment = extend({ 97 setup: function() {}, 98 teardown: function() {} 99 }, config.moduleTestEnvironment); 100 if (testEnvironmentArg) { 101 extend(testEnvironment,testEnvironmentArg); 102 } 103 104 QUnit.testStart( testName, testEnvironment ); 105 106 // allow utility functions to access the current test environment 107 QUnit.current_testEnvironment = testEnvironment; 108 109 config.assertions = []; 110 config.expected = expected; 111 112 var tests = id("qunit-tests"); 113 if (tests) { 114 var b = document.createElement("strong"); 115 b.innerHTML = "Running " + name; 116 var li = document.createElement("li"); 117 li.appendChild( b ); 118 li.id = "current-test-output"; 119 tests.appendChild( li ) 120 } 121 122 try { 123 if ( !config.pollution ) { 124 saveGlobal(); 125 } 126 127 testEnvironment.setup.call(testEnvironment); 128 } catch(e) { 129 QUnit.ok( false, "Setup failed on " + name + ": " + e.message ); 130 } 131 }, true); 132 133 synchronize(function() { 134 if ( async ) { 135 QUnit.stop(); 136 } 137 138 try { 139 callback.call(testEnvironment); 140 } catch(e) { 141 fail("Test " + name + " died, exception and test follows", e, callback); 142 QUnit.ok( false, "Died on test #" + (config.assertions.length + 1) + ": " + e.message ); 143 // else next test will carry the responsibility 144 saveGlobal(); 145 146 // Restart the tests if they're blocking 147 if ( config.blocking ) { 148 start(); 149 } 150 } 151 }, true); 152 153 synchronize(function() { 154 try { 155 checkPollution(); 156 testEnvironment.teardown.call(testEnvironment); 157 } catch(e) { 158 QUnit.ok( false, "Teardown failed on " + name + ": " + e.message ); 159 } 160 }, true); 161 162 synchronize(function() { 163 try { 164 QUnit.reset(); 165 } catch(e) { 166 fail("reset() failed, following Test " + name + ", exception and reset fn follows", e, reset); 167 } 168 169 if ( config.expected && config.expected != config.assertions.length ) { 170 QUnit.ok( false, "Expected " + config.expected + " assertions, but " + config.assertions.length + " were run" ); 171 } 172 173 var good = 0, bad = 0, 174 tests = id("qunit-tests"); 175 176 config.stats.all += config.assertions.length; 177 config.moduleStats.all += config.assertions.length; 178 179 if ( tests ) { 180 var ol = document.createElement("ol"); 181 ol.style.display = "none"; 182 183 for ( var i = 0; i < config.assertions.length; i++ ) { 184 var assertion = config.assertions[i]; 185 186 var li = document.createElement("li"); 187 li.className = assertion.result ? "pass" : "fail"; 188 li.appendChild(document.createTextNode(assertion.message || "(no message)")); 189 ol.appendChild( li ); 190 191 if ( assertion.result ) { 192 good++; 193 } else { 194 bad++; 195 config.stats.bad++; 196 config.moduleStats.bad++; 197 } 198 } 199 200 var b = document.createElement("strong"); 201 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + config.assertions.length + ")</b>"; 202 203 addEvent(b, "click", function() { 204 var next = b.nextSibling, display = next.style.display; 205 next.style.display = display === "none" ? "block" : "none"; 206 }); 207 208 addEvent(b, "dblclick", function(e) { 209 var target = e && e.target ? e.target : window.event.srcElement; 210 if ( target.nodeName.toLowerCase() === "strong" ) { 211 var text = "", node = target.firstChild; 212 213 while ( node.nodeType === 3 ) { 214 text += node.nodeValue; 215 node = node.nextSibling; 216 } 217 218 text = text.replace(/(^\s*|\s*$)/g, ""); 219 220 if ( window.location ) { 221 window.location.href = window.location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent(text); 222 } 223 } 224 }); 225 226 var li = id("current-test-output"); 227 li.id = ""; 228 li.className = bad ? "fail" : "pass"; 229 li.removeChild( li.firstChild ); 230 li.appendChild( b ); 231 li.appendChild( ol ); 232 233 if ( bad ) { 234 var toolbar = id("qunit-testrunner-toolbar"); 235 if ( toolbar ) { 236 toolbar.style.display = "block"; 237 id("qunit-filter-pass").disabled = null; 238 id("qunit-filter-missing").disabled = null; 239 } 240 } 241 242 } else { 243 for ( var i = 0; i < config.assertions.length; i++ ) { 244 if ( !config.assertions[i].result ) { 245 bad++; 246 config.stats.bad++; 247 config.moduleStats.bad++; 248 } 249 } 250 } 251 252 QUnit.testDone( testName, bad, config.assertions.length ); 253 254 if ( !window.setTimeout && !config.queue.length ) { 255 done(); 256 } 257 }, true); 258 259 if ( window.setTimeout && !config.doneTimer ) { 260 config.doneTimer = window.setTimeout(function(){ 261 if ( !config.queue.length ) { 262 done(); 263 } else { 264 synchronize( done ); 265 } 266 }, 13); 267 } 268 }, 269 270 /** 271 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through. 272 */ 273 expect: function(asserts) { 274 config.expected = asserts; 275 }, 276 277 /** 278 * Asserts true. 279 * @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" ); 280 */ 281 ok: function(a, msg) { 282 QUnit.log(a, msg); 283 284 config.assertions.push({ 285 result: !!a, 286 message: msg 287 }); 288 }, 289 290 /** 291 * Checks that the first two arguments are equal, with an optional message. 292 * Prints out both actual and expected values. 293 * 294 * Prefered to ok( actual == expected, message ) 295 * 296 * @example equal( format("Received {0} bytes.", 2), "Received 2 bytes." ); 297 * 298 * @param Object actual 299 * @param Object expected 300 * @param String message (optional) 301 */ 302 equal: function(actual, expected, message) { 303 push(expected == actual, actual, expected, message); 304 }, 305 306 notEqual: function(actual, expected, message) { 307 push(expected != actual, actual, expected, message); 308 }, 309 310 deepEqual: function(actual, expected, message) { 311 push(QUnit.equiv(actual, expected), actual, expected, message); 312 }, 313 314 notDeepEqual: function(actual, expected, message) { 315 push(!QUnit.equiv(actual, expected), actual, expected, message); 316 }, 317 318 strictEqual: function(actual, expected, message) { 319 push(expected === actual, actual, expected, message); 320 }, 321 322 notStrictEqual: function(actual, expected, message) { 323 push(expected !== actual, actual, expected, message); 324 }, 325 326 start: function() { 327 // A slight delay, to avoid any current callbacks 328 if ( window.setTimeout ) { 329 window.setTimeout(function() { 330 if ( config.timeout ) { 331 clearTimeout(config.timeout); 332 } 333 334 config.blocking = false; 335 process(); 336 }, 13); 337 } else { 338 config.blocking = false; 339 process(); 340 } 341 }, 342 343 stop: function(timeout) { 344 config.blocking = true; 345 346 if ( timeout && window.setTimeout ) { 347 config.timeout = window.setTimeout(function() { 348 QUnit.ok( false, "Test timed out" ); 349 QUnit.start(); 350 }, timeout); 351 } 352 }, 353 354 /** 355 * Resets the test setup. Useful for tests that modify the DOM. 356 */ 357 reset: function() { 358 if ( window.jQuery ) { 359 jQuery("#main").html( config.fixture ); 360 jQuery.event.global = {}; 361 jQuery.ajaxSettings = extend({}, config.ajaxSettings); 362 } 363 }, 364 restart : function(){ 365 this.init(); 366 for(var i =0; i < config.cachelist.length; i++){ 367 synchronize(config.cachelist[i]); 368 } 369 if (window.setTimeout && !config.doneTimer) { 370 config.doneTimer = window.setTimeout(function(){ 371 if (!config.queue.length) { 372 done(); 373 } 374 else { 375 synchronize(done); 376 } 377 }, 13); 378 } 379 }, 380 /** 381 * Trigger an event on an element. 382 * 383 * @example triggerEvent( document.body, "click" ); 384 * 385 * @param DOMElement elem 386 * @param String type 387 */ 388 triggerEvent: function( elem, type, event ) { 389 if ( document.createEvent ) { 390 event = document.createEvent("MouseEvents"); 391 event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView, 392 0, 0, 0, 0, 0, false, false, false, false, 0, null); 393 elem.dispatchEvent( event ); 394 395 } else if ( elem.fireEvent ) { 396 elem.fireEvent("on"+type); 397 } 398 }, 399 400 // Safe object type checking 401 is: function( type, obj ) { 402 return Object.prototype.toString.call( obj ) === "[object "+ type +"]"; 403 }, 404 405 // Logging callbacks 406 done: function(failures, total) {}, 407 log: function(result, message) {}, 408 testStart: function(name, testEnvironment) {}, 409 testDone: function(name, failures, total) {}, 410 moduleStart: function(name, testEnvironment) {}, 411 moduleDone: function(name, failures, total) {} 412}; 413 414// Backwards compatibility, deprecated 415QUnit.equals = QUnit.equal; 416QUnit.same = QUnit.deepEqual; 417 418// Maintain internal state 419var config = { 420 // The queue of tests to run 421 queue: [], 422 423 // block until document ready 424 blocking: true, 425 //a list of everything to run 426 cachelist : [] 427}; 428 429// Load paramaters 430(function() { 431 var location = window.location || { search: "", protocol: "file:" }, 432 GETParams = location.search.slice(1).split('&'); 433 434 for ( var i = 0; i < GETParams.length; i++ ) { 435 GETParams[i] = decodeURIComponent( GETParams[i] ); 436 if ( GETParams[i] === "noglobals" ) { 437 GETParams.splice( i, 1 ); 438 i--; 439 config.noglobals = true; 440 } else if ( GETParams[i].search('=') > -1 ) { 441 GETParams.splice( i, 1 ); 442 i--; 443 } 444 } 445 446 // restrict modules/tests by get parameters 447 config.filters = GETParams; 448 449 // Figure out if we're running the tests from a server or not 450 QUnit.isLocal = !!(location.protocol === 'file:'); 451})(); 452 453// Expose the API as global variables, unless an 'exports' 454// object exists, in that case we assume we're in CommonJS 455if ( typeof exports === "undefined" || typeof require === "undefined" ) { 456 extend(window, QUnit); 457 window.QUnit = QUnit; 458} else { 459 extend(exports, QUnit); 460 exports.QUnit = QUnit; 461} 462 463QUnit.config = config; 464 465if ( typeof document === "undefined" || document.readyState === "complete" ) { 466 config.autorun = true; 467} 468 469addEvent(window, "load", function() { 470 // Initialize the config, saving the execution queue 471 var oldconfig = extend({}, config); 472 QUnit.init(); 473 extend(config, oldconfig); 474 475 config.blocking = false; 476 477 var userAgent = id("qunit-userAgent"); 478 if ( userAgent ) { 479 userAgent.innerHTML = navigator.userAgent; 480 } 481 482 var toolbar = id("qunit-testrunner-toolbar"); 483 if ( toolbar ) { 484 toolbar.style.display = "none"; 485 486 var filter = document.createElement("input"); 487 filter.type = "checkbox"; 488 filter.id = "qunit-filter-pass"; 489 filter.disabled = true; 490 addEvent( filter, "click", function() { 491 var li = document.getElementsByTagName("li"); 492 for ( var i = 0; i < li.length; i++ ) { 493 if ( li[i].className.indexOf("pass") > -1 ) { 494 li[i].style.display = filter.checked ? "none" : ""; 495 } 496 } 497 }); 498 toolbar.appendChild( filter ); 499 500 var label = document.createElement("label"); 501 label.setAttribute("for", "qunit-filter-pass"); 502 label.innerHTML = "Hide passed tests"; 503 toolbar.appendChild( label ); 504 505 var missing = document.createElement("input"); 506 missing.type = "checkbox"; 507 missing.id = "qunit-filter-missing"; 508 missing.disabled = true; 509 addEvent( missing, "click", function() { 510 var li = document.getElementsByTagName("li"); 511 for ( var i = 0; i < li.length; i++ ) { 512 if ( li[i].className.indexOf("fail") > -1 && li[i].innerHTML.indexOf('missing test - untested code is broken code') > - 1 ) { 513 li[i].parentNode.parentNode.style.display = missing.checked ? "none" : "block"; 514 } 515 } 516 }); 517 toolbar.appendChild( missing ); 518 519 label = document.createElement("label"); 520 label.setAttribute("for", "qunit-filter-missing"); 521 label.innerHTML = "Hide missing tests (untested code is broken code)"; 522 toolbar.appendChild( label ); 523 } 524 525 var main = id('main'); 526 if ( main ) { 527 config.fixture = main.innerHTML; 528 } 529 530 if ( window.jQuery ) { 531 config.ajaxSettings = window.jQuery.ajaxSettings; 532 } 533 534 if (config.autostart) { 535 QUnit.start(); 536 } 537}); 538 539function done() { 540 if ( config.doneTimer && window.clearTimeout ) { 541 window.clearTimeout( config.doneTimer ); 542 config.doneTimer = null; 543 } 544 545 if ( config.queue.length ) { 546 config.doneTimer = window.setTimeout(function(){ 547 if ( !config.queue.length ) { 548 done(); 549 } else { 550 synchronize( done ); 551 } 552 }, 13); 553 554 return; 555 } 556 557 config.autorun = true; 558 559 // Log the last module results 560 if ( config.currentModule ) { 561 QUnit.moduleDone( config.currentModule, config.moduleStats.bad, config.moduleStats.all ); 562 } 563 564 var banner = id("qunit-banner"), 565 tests = id("qunit-tests"), 566 html = ['Tests completed in ', 567 +new Date - config.started, ' milliseconds.<br/>', 568 '<span class="passed">', config.stats.all - config.stats.bad, '</span> tests of <span class="total">', config.stats.all, '</span> passed, <span class="failed">', config.stats.bad,'</span> failed.'].join(''); 569 570 if ( banner ) { 571 banner.className = (config.stats.bad ? "qunit-fail" : "qunit-pass"); 572 } 573 574 if ( tests ) { 575 var result = id("qunit-testresult"); 576 577 if ( !result ) { 578 result = document.createElement("p"); 579 result.id = "qunit-testresult"; 580 result.className = "result"; 581 tests.parentNode.insertBefore( result, tests.nextSibling ); 582 } 583 584 result.innerHTML = html; 585 } 586 587 QUnit.done( config.stats.bad, config.stats.all ); 588} 589 590function validTest( name ) { 591 var i = config.filters.length, 592 run = false; 593 594 if ( !i ) { 595 return true; 596 } 597 598 while ( i-- ) { 599 var filter = config.filters[i], 600 not = filter.charAt(0) == '!'; 601 602 if ( not ) { 603 filter = filter.slice(1); 604 } 605 606 if ( name.indexOf(filter) !== -1 ) { 607 return !not; 608 } 609 610 if ( not ) { 611 run = true; 612 } 613 } 614 615 return run; 616} 617 618function push(result, actual, expected, message) { 619 message = message || (result ? "okay" : "failed"); 620 QUnit.ok( result, message + ", expected: " + QUnit.jsDump.parse(expected) + " result: " + QUnit.jsDump.parse(actual) ); 621} 622 623function synchronize( callback , save) { 624 config.queue.push( callback ); 625 if(save){ 626 config.cachelist.push( callback ) 627 } 628 if ( config.autorun && !config.blocking ) { 629 process(); 630 } 631} 632function process() { 633 var start = (new Date()).getTime(); 634 635 while ( config.queue.length && !config.blocking ) { 636 if ( config.updateRate <= 0 || (((new Date()).getTime() - start) < config.updateRate) ) { 637 config.queue.shift()(); 638 639 } else { 640 setTimeout( process, 13 ); 641 break; 642 } 643 } 644} 645 646function saveGlobal() { 647 config.pollution = []; 648 649 if ( config.noglobals ) { 650 for ( var key in window ) { 651 config.pollution.push( key ); 652 } 653 } 654} 655 656function checkPollution( name ) { 657 var old = config.pollution; 658 saveGlobal(); 659 660 var newGlobals = diff( old, config.pollution ); 661 if ( newGlobals.length > 0 ) { 662 ok( false, "Introduced global variable(s): " + newGlobals.join(", ") ); 663 config.expected++; 664 } 665 666 var deletedGlobals = diff( config.pollution, old ); 667 if ( deletedGlobals.length > 0 ) { 668 ok( false, "Deleted global variable(s): " + deletedGlobals.join(", ") ); 669 config.expected++; 670 } 671} 672 673// returns a new Array with the elements that are in a but not in b 674function diff( a, b ) { 675 var result = a.slice(); 676 for ( var i = 0; i < result.length; i++ ) { 677 for ( var j = 0; j < b.length; j++ ) { 678 if ( result[i] === b[j] ) { 679 result.splice(i, 1); 680 i--; 681 break; 682 } 683 } 684 } 685 return result; 686} 687 688function fail(message, exception, callback) { 689 if ( typeof console !== "undefined" && console.error && console.warn ) { 690 console.error(message); 691 console.error(exception); 692 console.warn(callback.toString()); 693 694 } else if ( window.opera && opera.postError ) { 695 opera.postError(message, exception, callback.toString); 696 } 697} 698 699function extend(a, b) { 700 for ( var prop in b ) { 701 a[prop] = b[prop]; 702 } 703 704 return a; 705} 706 707function addEvent(elem, type, fn) { 708 if ( elem.addEventListener ) { 709 elem.addEventListener( type, fn, false ); 710 } else if ( elem.attachEvent ) { 711 elem.attachEvent( "on" + type, fn ); 712 } else { 713 fn(); 714 } 715} 716 717function id(name) { 718 return !!(typeof document !== "undefined" && document && document.getElementById) && 719 document.getElementById( name ); 720} 721 722// Test for equality any JavaScript type. 723// Discussions and reference: http://philrathe.com/articles/equiv 724// Test suites: http://philrathe.com/tests/equiv 725// Author: Philippe Rathé <prathe@gmail.com> 726QUnit.equiv = function () { 727 728 var innerEquiv; // the real equiv function 729 var callers = []; // stack to decide between skip/abort functions 730 var parents = []; // stack to avoiding loops from circular referencing 731 732 733 // Determine what is o. 734 function hoozit(o) { 735 if (QUnit.is("String", o)) { 736 return "string"; 737 738 } else if (QUnit.is("Boolean", o)) { 739 return "boolean"; 740 741 } else if (QUnit.is("Number", o)) { 742 743 if (isNaN(o)) { 744 return "nan"; 745 } else { 746 return "number"; 747 } 748 749 } else if (typeof o === "undefined") { 750 return "undefined"; 751 752 // consider: typeof null === object 753 } else if (o === null) { 754 return "null"; 755 756 // consider: typeof [] === object 757 } else if (QUnit.is( "Array", o)) { 758 return "array"; 759 760 // consider: typeof new Date() === object 761 } else if (QUnit.is( "Date", o)) { 762 return "date"; 763 764 // consider: /./ instanceof Object; 765 // /./ instanceof RegExp; 766 // typeof /./ === "function"; // => false in IE and Opera, 767 // true in FF and Safari 768 } else if (QUnit.is( "RegExp", o)) { 769 return "regexp"; 770 771 } else if (typeof o === "object") { 772 return "object"; 773 774 } else if (QUnit.is( "Function", o)) { 775 return "function"; 776 } else { 777 return undefined; 778 } 779 } 780 781 // Call the o related callback with the given arguments. 782 function bindCallbacks(o, callbacks, args) { 783 var prop = hoozit(o); 784 if (prop) { 785 if (hoozit(callbacks[prop]) === "function") { 786 return callbacks[prop].apply(callbacks, args); 787 } else { 788 return callbacks[prop]; // or undefined 789 } 790 } 791 } 792 793 var callbacks = function () { 794 795 // for string, boolean, number and null 796 function useStrictEquality(b, a) { 797 if (b instanceof a.constructor || a instanceof b.constructor) { 798 // to catch short annotaion VS 'new' annotation of a declaration 799 // e.g. var i = 1; 800 // var j = new Number(1); 801 return a == b; 802 } else { 803 return a === b; 804 } 805 } 806 807 return { 808 "string": useStrictEquality, 809 "boolean": useStrictEquality, 810 "number": useStrictEquality, 811 "null": useStrictEquality, 812 "undefined": useStrictEquality, 813 814 "nan": function (b) { 815 return isNaN(b); 816 }, 817 818 "date": function (b, a) { 819 return hoozit(b) === "date" && a.valueOf() === b.valueOf(); 820 }, 821 822 "regexp": function (b, a) { 823 return hoozit(b) === "regexp" && 824 a.source === b.source && // the regex itself 825 a.global === b.global && // and its modifers (gmi) ... 826 a.ignoreCase === b.ignoreCase && 827 a.multiline === b.multiline; 828 }, 829 830 // - skip when the property is a method of an instance (OOP) 831 // - abort otherwise, 832 // initial === would have catch identical references anyway 833 "function": function () { 834 var caller = callers[callers.length - 1]; 835 return caller !== Object && 836 typeof caller !== "undefined"; 837 }, 838 839 "array": function (b, a) { 840 var i, j, loop; 841 var len; 842 843 // b could be an object literal here 844 if ( ! (hoozit(b) === "array")) { 845 return false; 846 } 847 848 len = a.length; 849 if (len !== b.length) { // safe and faster 850 return false; 851 } 852 853 //track reference to avoid circular references 854 parents.push(a); 855 for (i = 0; i < len; i++) { 856 loop = false; 857 for(j=0;j<parents.length;j++){ 858 if(parents[j] === a[i]){ 859 loop = true;//dont rewalk array 860 } 861 } 862 if (!loop && ! innerEquiv(a[i], b[i])) { 863 parents.pop(); 864 return false; 865 } 866 } 867 parents.pop(); 868 return true; 869 }, 870 871 "object": function (b, a) { 872 var i, j, loop; 873 var eq = true; // unless we can proove it 874 var aProperties = [], bProperties = []; // collection of strings 875 876 // comparing constructors is more strict than using instanceof 877 if ( a.constructor !== b.constructor) { 878 return false; 879 } 880 881 // stack constructor before traversing properties 882 callers.push(a.constructor); 883 //track reference to avoid circular references 884 parents.push(a); 885 886 for (i in a) { // be strict: don't ensures hasOwnProperty and go deep 887 loop = false; 888 for(j=0;j<parents.length;j++){ 889 if(parents[j] === a[i]) 890 loop = true; //don't go down the same path twice 891 } 892 aProperties.push(i); // collect a's properties 893 894 if (!loop && ! innerEquiv(a[i], b[i])) { 895 eq = false; 896 break; 897 } 898 } 899 900 callers.pop(); // unstack, we are done 901 parents.pop(); 902 903 for (i in b) { 904 bProperties.push(i); // collect b's properties 905 } 906 907 // Ensures identical properties name 908 return eq && innerEquiv(aProperties.sort(), bProperties.sort()); 909 } 910 }; 911 }(); 912 913 innerEquiv = function () { // can take multiple arguments 914 var args = Array.prototype.slice.apply(arguments); 915 if (args.length < 2) { 916 return true; // end transition 917 } 918 919 return (function (a, b) { 920 if (a === b) { 921 return true; // catch the most you can 922 } else if (a === null || b === null || typeof a === "undefined" || typeof b === "undefined" || hoozit(a) !== hoozit(b)) { 923 return false; // don't lose time with error prone cases 924 } else { 925 return bindCallbacks(a, callbacks, [b, a]); 926 } 927 928 // apply transition with (1..n) arguments 929 })(args[0], args[1]) && arguments.callee.apply(this, args.splice(1, args.length -1)); 930 }; 931 932 return innerEquiv; 933 934}(); 935 936/** 937 * jsDump 938 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com 939 * Licensed under BSD (http://www.opensource.org/licenses/bsd-license.php) 940 * Date: 5/15/2008 941 * @projectDescription Advanced and extensible data dumping for Javascript. 942 * @version 1.0.0 943 * @author Ariel Flesler 944 * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html} 945 */ 946QUnit.jsDump = (function() { 947 function quote( str ) { 948 return '"' + str.toString().replace(/"/g, '\\"') + '"'; 949 }; 950 function literal( o ) { 951 return o + ''; 952 }; 953 function join( pre, arr, post ) { 954 var s = jsDump.separator(), 955 base = jsDump.indent(), 956 inner = jsDump.indent(1); 957 if ( arr.join ) 958 arr = arr.join( ',' + s + inner ); 959 if ( !arr ) 960 return pre + post; 961 return [ pre, inner + arr, base + post ].join(s); 962 }; 963 function array( arr ) { 964 var i = arr.length, ret = Array(i); 965 this.up(); 966 while ( i-- ) 967 ret[i] = this.parse( arr[i] ); 968 this.down(); 969 return join( '[', ret, ']' ); 970 }; 971 972 var reName = /^function (\w+)/; 973 974 var jsDump = { 975 parse:function( obj, type ) { //type is used mostly internally, you can fix a (custom)type in advance 976 var parser = this.parsers[ type || this.typeOf(obj) ]; 977 type = typeof parser; 978 979 return type == 'function' ? parser.call( this, obj ) : 980 type == 'string' ? parser : 981 this.parsers.error; 982 }, 983 typeOf:function( obj ) { 984 var type; 985 if ( obj === null ) { 986 type = "null"; 987 } else if (typeof obj === "undefined") { 988 type = "undefined"; 989 } else if (QUnit.is("RegExp", obj)) { 990 type = "regexp"; 991 } else if (QUnit.is("Date", obj)) { 992 type = "date"; 993 } else if (QUnit.is("Function", obj)) { 994 type = "function"; 995 } else if (obj.setInterval && obj.document && !obj.nodeType) { 996 type = "window"; 997 } else if (obj.nodeType === 9) { 998 type = "document"; 999 } else if (obj.nodeType) { 1000 type = "node"; 1001 } else if (typeof obj === "object" && typeof obj.length === "number" && obj.length >= 0) { 1002 type = "array"; 1003 } else { 1004 type = typeof obj; 1005 } 1006 return type; 1007 }, 1008 separator:function() { 1009 return this.multiline ? this.HTML ? '<br />' : '\n' : this.HTML ? ' ' : ' '; 1010 }, 1011 indent:function( extra ) {// extra can be a number, shortcut for increasing-calling-decreasing 1012 if ( !this.multiline ) 1013 return ''; 1014 var chr = this.indentChar; 1015 if ( this.HTML ) 1016 chr = chr.replace(/\t/g,' ').replace(/ /g,' '); 1017 return Array( this._depth_ + (extra||0) ).join(chr); 1018 }, 1019 up:function( a ) { 1020 this._depth_ += a || 1; 1021 }, 1022 down:function( a ) { 1023 this._depth_ -= a || 1; 1024 }, 1025 setParser:function( name, parser ) { 1026 this.parsers[name] = parser; 1027 }, 1028 // The next 3 are exposed so you can use them 1029 quote:quote, 1030 literal:literal, 1031 join:join, 1032 // 1033 _depth_: 1, 1034 // This is the list of parsers, to modify them, use jsDump.setParser 1035 parsers:{ 1036 window: '[Window]', 1037 document: '[Document]', 1038 error:'[ERROR]', //when no parser is found, shouldn't happen 1039 unknown: '[Unknown]', 1040 'null':'null', 1041 undefined:'undefined', 1042 'function':function( fn ) { 1043 var ret = 'function', 1044 name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE 1045 if ( name ) 1046 ret += ' ' + name; 1047 ret += '('; 1048 1049 ret = [ ret, this.parse( fn, 'functionArgs' ), '){'].join(''); 1050 return join( ret, this.parse(fn,'functionCode'), '}' ); 1051 }, 1052 array: array, 1053 nodelist: array, 1054 arguments: array, 1055 object:function( map ) { 1056 var ret = [ ]; 1057 this.up(); 1058 for ( var key in map ) 1059 ret.push( this.parse(key,'key') + ': ' + this.parse(map[key]) ); 1060 this.down(); 1061 return join( '{', ret, '}' ); 1062 }, 1063 node:function( node ) { 1064 var open = this.HTML ? '<' : '<', 1065 close = this.HTML ? '>' : '>'; 1066 1067 var tag = node.nodeName.toLowerCase(), 1068 ret = open + tag; 1069 1070 for ( var a in this.DOMAttrs ) { 1071 var val = node[this.DOMAttrs[a]]; 1072 if ( val ) 1073 ret += ' ' + a + '=' + this.parse( val, 'attribute' ); 1074 } 1075 return ret + close + open + '/' + tag + close; 1076 }, 1077 functionArgs:function( fn ) {//function calls it internally, it's the arguments part of the function 1078 var l = fn.length; 1079 if ( !l ) return ''; 1080 1081 var args = Array(l); 1082 while ( l-- ) 1083 args[l] = String.fromCharCode(97+l);//97 is 'a' 1084 return ' ' + args.join(', ') + ' '; 1085 }, 1086 key:quote, //object calls it internally, the key part of an item in a map 1087 functionCode:'[code]', //function calls it internally, it's the content of the function 1088 attribute:quote, //node calls it internally, it's an html attribute value 1089 string:quote, 1090 date:quote, 1091 regexp:literal, //regex 1092 number:literal, 1093 'boolean':literal 1094 }, 1095 DOMAttrs:{//attributes to dump from nodes, name=>realName 1096 id:'id', 1097 name:'name', 1098 'class':'className' 1099 }, 1100 HTML:false,//if true, entities are escaped ( <, >, \t, space and \n ) 1101 indentChar:' ',//indentation unit 1102 multiline:false //if true, items in a collection, are separated by a \n, else just a space. 1103 }; 1104 1105 return jsDump; 1106})(); 1107 1108})(this);