qunit.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /**
  2. * QUnit v1.3.0pre - A JavaScript Unit Testing Framework
  3. *
  4. * http://docs.jquery.com/QUnit
  5. *
  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.
  9. */
  10. (function(window) {
  11. var defined = {
  12. setTimeout: typeof window.setTimeout !== "undefined",
  13. sessionStorage: (function() {
  14. var x = "qunit-test-string";
  15. try {
  16. sessionStorage.setItem(x, x);
  17. sessionStorage.removeItem(x);
  18. return true;
  19. } catch(e) {
  20. return false;
  21. }
  22. })()
  23. };
  24. var testId = 0,
  25. toString = Object.prototype.toString,
  26. hasOwn = Object.prototype.hasOwnProperty;
  27. var Test = function(name, testName, expected, async, callback) {
  28. this.name = name;
  29. this.testName = testName;
  30. this.expected = expected;
  31. this.async = async;
  32. this.callback = callback;
  33. this.assertions = [];
  34. };
  35. Test.prototype = {
  36. init: function() {
  37. var tests = id("qunit-tests");
  38. if (tests) {
  39. var b = document.createElement("strong");
  40. b.innerHTML = "Running " + this.name;
  41. var li = document.createElement("li");
  42. li.appendChild( b );
  43. li.className = "running";
  44. li.id = this.id = "test-output" + testId++;
  45. tests.appendChild( li );
  46. }
  47. },
  48. setup: function() {
  49. if (this.module != config.previousModule) {
  50. if ( config.previousModule ) {
  51. runLoggingCallbacks('moduleDone', QUnit, {
  52. name: config.previousModule,
  53. failed: config.moduleStats.bad,
  54. passed: config.moduleStats.all - config.moduleStats.bad,
  55. total: config.moduleStats.all
  56. } );
  57. }
  58. config.previousModule = this.module;
  59. config.moduleStats = { all: 0, bad: 0 };
  60. runLoggingCallbacks( 'moduleStart', QUnit, {
  61. name: this.module
  62. } );
  63. } else if (config.autorun) {
  64. runLoggingCallbacks( 'moduleStart', QUnit, {
  65. name: this.module
  66. } );
  67. }
  68. config.current = this;
  69. this.testEnvironment = extend({
  70. setup: function() {},
  71. teardown: function() {}
  72. }, this.moduleTestEnvironment);
  73. runLoggingCallbacks( 'testStart', QUnit, {
  74. name: this.testName,
  75. module: this.module
  76. });
  77. // allow utility functions to access the current test environment
  78. // TODO why??
  79. QUnit.current_testEnvironment = this.testEnvironment;
  80. try {
  81. if ( !config.pollution ) {
  82. saveGlobal();
  83. }
  84. this.testEnvironment.setup.call(this.testEnvironment);
  85. } catch(e) {
  86. QUnit.ok( false, "Setup failed on " + this.testName + ": " + e.message );
  87. }
  88. },
  89. run: function() {
  90. config.current = this;
  91. if ( this.async ) {
  92. QUnit.stop();
  93. }
  94. if ( config.notrycatch ) {
  95. this.callback.call(this.testEnvironment);
  96. return;
  97. }
  98. try {
  99. this.callback.call(this.testEnvironment);
  100. } catch(e) {
  101. fail("Test " + this.testName + " died, exception and test follows", e, this.callback);
  102. QUnit.ok( false, "Died on test #" + (this.assertions.length + 1) + ": " + e.message + " - " + QUnit.jsDump.parse(e) );
  103. // else next test will carry the responsibility
  104. saveGlobal();
  105. // Restart the tests if they're blocking
  106. if ( config.blocking ) {
  107. QUnit.start();
  108. }
  109. }
  110. },
  111. teardown: function() {
  112. config.current = this;
  113. try {
  114. this.testEnvironment.teardown.call(this.testEnvironment);
  115. checkPollution();
  116. } catch(e) {
  117. QUnit.ok( false, "Teardown failed on " + this.testName + ": " + e.message );
  118. }
  119. },
  120. finish: function() {
  121. config.current = this;
  122. if ( this.expected != null && this.expected != this.assertions.length ) {
  123. QUnit.ok( false, "Expected " + this.expected + " assertions, but " + this.assertions.length + " were run" );
  124. }
  125. var good = 0, bad = 0,
  126. tests = id("qunit-tests");
  127. config.stats.all += this.assertions.length;
  128. config.moduleStats.all += this.assertions.length;
  129. if ( tests ) {
  130. var ol = document.createElement("ol");
  131. for ( var i = 0; i < this.assertions.length; i++ ) {
  132. var assertion = this.assertions[i];
  133. var li = document.createElement("li");
  134. li.className = assertion.result ? "pass" : "fail";
  135. li.innerHTML = assertion.message || (assertion.result ? "okay" : "failed");
  136. ol.appendChild( li );
  137. if ( assertion.result ) {
  138. good++;
  139. } else {
  140. bad++;
  141. config.stats.bad++;
  142. config.moduleStats.bad++;
  143. }
  144. }
  145. // store result when possible
  146. if ( QUnit.config.reorder && defined.sessionStorage ) {
  147. if (bad) {
  148. sessionStorage.setItem("qunit-" + this.module + "-" + this.testName, bad);
  149. } else {
  150. sessionStorage.removeItem("qunit-" + this.module + "-" + this.testName);
  151. }
  152. }
  153. if (bad == 0) {
  154. ol.style.display = "none";
  155. }
  156. var b = document.createElement("strong");
  157. b.innerHTML = this.name + " <b class='counts'>(<b class='failed'>" + bad + "</b>, <b class='passed'>" + good + "</b>, " + this.assertions.length + ")</b>";
  158. var a = document.createElement("a");
  159. a.innerHTML = "Rerun";
  160. a.href = QUnit.url({ filter: getText([b]).replace(/\([^)]+\)$/, "").replace(/(^\s*|\s*$)/g, "") });
  161. addEvent(b, "click", function() {
  162. var next = b.nextSibling.nextSibling,
  163. display = next.style.display;
  164. next.style.display = display === "none" ? "block" : "none";
  165. });
  166. addEvent(b, "dblclick", function(e) {
  167. var target = e && e.target ? e.target : window.event.srcElement;
  168. if ( target.nodeName.toLowerCase() == "span" || target.nodeName.toLowerCase() == "b" ) {
  169. target = target.parentNode;
  170. }
  171. if ( window.location && target.nodeName.toLowerCase() === "strong" ) {
  172. window.location = QUnit.url({ filter: getText([target]).replace(/\([^)]+\)$/, "").replace(/(^\s*|\s*$)/g, "") });
  173. }
  174. });
  175. var li = id(this.id);
  176. li.className = bad ? "fail" : "pass";
  177. li.removeChild( li.firstChild );
  178. li.appendChild( b );
  179. li.appendChild( a );
  180. li.appendChild( ol );
  181. } else {
  182. for ( var i = 0; i < this.assertions.length; i++ ) {
  183. if ( !this.assertions[i].result ) {
  184. bad++;
  185. config.stats.bad++;
  186. config.moduleStats.bad++;
  187. }
  188. }
  189. }
  190. try {
  191. QUnit.reset();
  192. } catch(e) {
  193. fail("reset() failed, following Test " + this.testName + ", exception and reset fn follows", e, QUnit.reset);
  194. }
  195. runLoggingCallbacks( 'testDone', QUnit, {
  196. name: this.testName,
  197. module: this.module,
  198. failed: bad,
  199. passed: this.assertions.length - bad,
  200. total: this.assertions.length
  201. } );
  202. },
  203. queue: function() {
  204. var test = this;
  205. synchronize(function() {
  206. test.init();
  207. });
  208. function run() {
  209. // each of these can by async
  210. synchronize(function() {
  211. test.setup();
  212. });
  213. synchronize(function() {
  214. test.run();
  215. });
  216. synchronize(function() {
  217. test.teardown();
  218. });
  219. synchronize(function() {
  220. test.finish();
  221. });
  222. }
  223. // defer when previous test run passed, if storage is available
  224. var bad = QUnit.config.reorder && defined.sessionStorage && +sessionStorage.getItem("qunit-" + this.module + "-" + this.testName);
  225. if (bad) {
  226. run();
  227. } else {
  228. synchronize(run, true);
  229. };
  230. }
  231. };
  232. var QUnit = {
  233. // call on start of module test to prepend name to all tests
  234. module: function(name, testEnvironment) {
  235. config.currentModule = name;
  236. config.currentModuleTestEnviroment = testEnvironment;
  237. },
  238. asyncTest: function(testName, expected, callback) {
  239. if ( arguments.length === 2 ) {
  240. callback = expected;
  241. expected = null;
  242. }
  243. QUnit.test(testName, expected, callback, true);
  244. },
  245. test: function(testName, expected, callback, async) {
  246. var name = '<span class="test-name">' + escapeInnerText(testName) + '</span>';
  247. if ( arguments.length === 2 ) {
  248. callback = expected;
  249. expected = null;
  250. }
  251. if ( config.currentModule ) {
  252. name = '<span class="module-name">' + config.currentModule + "</span>: " + name;
  253. }
  254. if ( !validTest(config.currentModule + ": " + testName) ) {
  255. return;
  256. }
  257. var test = new Test(name, testName, expected, async, callback);
  258. test.module = config.currentModule;
  259. test.moduleTestEnvironment = config.currentModuleTestEnviroment;
  260. test.queue();
  261. },
  262. /**
  263. * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
  264. */
  265. expect: function(asserts) {
  266. config.current.expected = asserts;
  267. },
  268. /**
  269. * Asserts true.
  270. * @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" );
  271. */
  272. ok: function(a, msg) {
  273. if (!config.current) {
  274. throw new Error("ok() assertion outside test context, was " + sourceFromStacktrace(2));
  275. }
  276. a = !!a;
  277. var details = {
  278. result: a,
  279. message: msg
  280. };
  281. msg = escapeInnerText(msg);
  282. runLoggingCallbacks( 'log', QUnit, details );
  283. config.current.assertions.push({
  284. result: a,
  285. message: msg
  286. });
  287. },
  288. /**
  289. * Checks that the first two arguments are equal, with an optional message.
  290. * Prints out both actual and expected values.
  291. *
  292. * Prefered to ok( actual == expected, message )
  293. *
  294. * @example equal( format("Received {0} bytes.", 2), "Received 2 bytes." );
  295. *
  296. * @param Object actual
  297. * @param Object expected
  298. * @param String message (optional)
  299. */
  300. equal: function(actual, expected, message) {
  301. QUnit.push(expected == actual, actual, expected, message);
  302. },
  303. notEqual: function(actual, expected, message) {
  304. QUnit.push(expected != actual, actual, expected, message);
  305. },
  306. deepEqual: function(actual, expected, message) {
  307. QUnit.push(QUnit.equiv(actual, expected), actual, expected, message);
  308. },
  309. notDeepEqual: function(actual, expected, message) {
  310. QUnit.push(!QUnit.equiv(actual, expected), actual, expected, message);
  311. },
  312. strictEqual: function(actual, expected, message) {
  313. QUnit.push(expected === actual, actual, expected, message);
  314. },
  315. notStrictEqual: function(actual, expected, message) {
  316. QUnit.push(expected !== actual, actual, expected, message);
  317. },
  318. raises: function(block, expected, message) {
  319. var actual, ok = false;
  320. if (typeof expected === 'string') {
  321. message = expected;
  322. expected = null;
  323. }
  324. try {
  325. block();
  326. } catch (e) {
  327. actual = e;
  328. }
  329. if (actual) {
  330. // we don't want to validate thrown error
  331. if (!expected) {
  332. ok = true;
  333. // expected is a regexp
  334. } else if (QUnit.objectType(expected) === "regexp") {
  335. ok = expected.test(actual);
  336. // expected is a constructor
  337. } else if (actual instanceof expected) {
  338. ok = true;
  339. // expected is a validation function which returns true is validation passed
  340. } else if (expected.call({}, actual) === true) {
  341. ok = true;
  342. }
  343. }
  344. QUnit.ok(ok, message);
  345. },
  346. start: function(count) {
  347. config.semaphore -= count || 1;
  348. if (config.semaphore > 0) {
  349. // don't start until equal number of stop-calls
  350. return;
  351. }
  352. if (config.semaphore < 0) {
  353. // ignore if start is called more often then stop
  354. config.semaphore = 0;
  355. }
  356. // A slight delay, to avoid any current callbacks
  357. if ( defined.setTimeout ) {
  358. window.setTimeout(function() {
  359. if (config.semaphore > 0) {
  360. return;
  361. }
  362. if ( config.timeout ) {
  363. clearTimeout(config.timeout);
  364. }
  365. config.blocking = false;
  366. process(true);
  367. }, 13);
  368. } else {
  369. config.blocking = false;
  370. process(true);
  371. }
  372. },
  373. stop: function(count) {
  374. config.semaphore += count || 1;
  375. config.blocking = true;
  376. if ( config.testTimeout && defined.setTimeout ) {
  377. clearTimeout(config.timeout);
  378. config.timeout = window.setTimeout(function() {
  379. QUnit.ok( false, "Test timed out" );
  380. config.semaphore = 1;
  381. QUnit.start();
  382. }, config.testTimeout);
  383. }
  384. }
  385. };
  386. //We want access to the constructor's prototype
  387. (function() {
  388. function F(){};
  389. F.prototype = QUnit;
  390. QUnit = new F();
  391. //Make F QUnit's constructor so that we can add to the prototype later
  392. QUnit.constructor = F;
  393. })();
  394. // deprecated; still export them to window to provide clear error messages
  395. // next step: remove entirely
  396. QUnit.equals = function() {
  397. throw new Error("QUnit.equals has been deprecated since 2009 (e88049a0), use QUnit.equal instead");
  398. };
  399. QUnit.same = function() {
  400. throw new Error("QUnit.same has been deprecated since 2009 (e88049a0), use QUnit.deepEqual instead");
  401. };
  402. // Maintain internal state
  403. var config = {
  404. // The queue of tests to run
  405. queue: [],
  406. // block until document ready
  407. blocking: true,
  408. // when enabled, show only failing tests
  409. // gets persisted through sessionStorage and can be changed in UI via checkbox
  410. hidepassed: false,
  411. // by default, run previously failed tests first
  412. // very useful in combination with "Hide passed tests" checked
  413. reorder: true,
  414. // by default, modify document.title when suite is done
  415. altertitle: true,
  416. urlConfig: ['noglobals', 'notrycatch'],
  417. //logging callback queues
  418. begin: [],
  419. done: [],
  420. log: [],
  421. testStart: [],
  422. testDone: [],
  423. moduleStart: [],
  424. moduleDone: []
  425. };
  426. // Load paramaters
  427. (function() {
  428. var location = window.location || { search: "", protocol: "file:" },
  429. params = location.search.slice( 1 ).split( "&" ),
  430. length = params.length,
  431. urlParams = {},
  432. current;
  433. if ( params[ 0 ] ) {
  434. for ( var i = 0; i < length; i++ ) {
  435. current = params[ i ].split( "=" );
  436. current[ 0 ] = decodeURIComponent( current[ 0 ] );
  437. // allow just a key to turn on a flag, e.g., test.html?noglobals
  438. current[ 1 ] = current[ 1 ] ? decodeURIComponent( current[ 1 ] ) : true;
  439. urlParams[ current[ 0 ] ] = current[ 1 ];
  440. }
  441. }
  442. QUnit.urlParams = urlParams;
  443. config.filter = urlParams.filter;
  444. // Figure out if we're running the tests from a server or not
  445. QUnit.isLocal = !!(location.protocol === 'file:');
  446. })();
  447. // Expose the API as global variables, unless an 'exports'
  448. // object exists, in that case we assume we're in CommonJS
  449. if ( typeof exports === "undefined" || typeof require === "undefined" ) {
  450. extend(window, QUnit);
  451. window.QUnit = QUnit;
  452. } else {
  453. module.exports = QUnit;
  454. }
  455. // define these after exposing globals to keep them in these QUnit namespace only
  456. extend(QUnit, {
  457. config: config,
  458. // Initialize the configuration options
  459. init: function() {
  460. extend(config, {
  461. stats: { all: 0, bad: 0 },
  462. moduleStats: { all: 0, bad: 0 },
  463. started: +new Date,
  464. updateRate: 1000,
  465. blocking: false,
  466. autostart: true,
  467. autorun: false,
  468. filter: "",
  469. queue: [],
  470. semaphore: 0
  471. });
  472. var qunit = id( "qunit" );
  473. if ( qunit ) {
  474. qunit.innerHTML =
  475. '<h1 id="qunit-header">' + document.title + '</h1>' +
  476. '<h2 id="qunit-banner"></h2>' +
  477. '<div id="qunit-testrunner-toolbar"></div>' +
  478. '<h2 id="qunit-userAgent"></h2>' +
  479. '<ol id="qunit-tests"></ol>';
  480. }
  481. var tests = id( "qunit-tests" ),
  482. banner = id( "qunit-banner" ),
  483. result = id( "qunit-testresult" );
  484. if ( tests ) {
  485. tests.innerHTML = "";
  486. }
  487. if ( banner ) {
  488. banner.className = "";
  489. }
  490. if ( result ) {
  491. result.parentNode.removeChild( result );
  492. }
  493. if ( tests ) {
  494. result = document.createElement( "p" );
  495. result.id = "qunit-testresult";
  496. result.className = "result";
  497. tests.parentNode.insertBefore( result, tests );
  498. result.innerHTML = 'Running...<br/>&nbsp;';
  499. }
  500. },
  501. /**
  502. * Resets the test setup. Useful for tests that modify the DOM.
  503. *
  504. * If jQuery is available, uses jQuery's replaceWith(), otherwise use replaceChild
  505. */
  506. reset: function() {
  507. if ( window.jQuery ) {
  508. jQuery( "#qunit-fixture" ).replaceWith( config.fixture.cloneNode(true) );
  509. } else {
  510. var main = id( 'qunit-fixture' );
  511. if ( main ) {
  512. main.parentNode.replaceChild(config.fixture.cloneNode(true), main);
  513. }
  514. }
  515. },
  516. /**
  517. * Trigger an event on an element.
  518. *
  519. * @example triggerEvent( document.body, "click" );
  520. *
  521. * @param DOMElement elem
  522. * @param String type
  523. */
  524. triggerEvent: function( elem, type, event ) {
  525. if ( document.createEvent ) {
  526. event = document.createEvent("MouseEvents");
  527. event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
  528. 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  529. elem.dispatchEvent( event );
  530. } else if ( elem.fireEvent ) {
  531. elem.fireEvent("on"+type);
  532. }
  533. },
  534. // Safe object type checking
  535. is: function( type, obj ) {
  536. return QUnit.objectType( obj ) == type;
  537. },
  538. objectType: function( obj ) {
  539. if (typeof obj === "undefined") {
  540. return "undefined";
  541. // consider: typeof null === object
  542. }
  543. if (obj === null) {
  544. return "null";
  545. }
  546. var type = toString.call( obj ).match(/^\[object\s(.*)\]$/)[1] || '';
  547. switch (type) {
  548. case 'Number':
  549. if (isNaN(obj)) {
  550. return "nan";
  551. } else {
  552. return "number";
  553. }
  554. case 'String':
  555. case 'Boolean':
  556. case 'Array':
  557. case 'Date':
  558. case 'RegExp':
  559. case 'Function':
  560. return type.toLowerCase();
  561. }
  562. if (typeof obj === "object") {
  563. return "object";
  564. }
  565. return undefined;
  566. },
  567. push: function(result, actual, expected, message) {
  568. if (!config.current) {
  569. throw new Error("assertion outside test context, was " + sourceFromStacktrace());
  570. }
  571. var details = {
  572. result: result,
  573. message: message,
  574. actual: actual,
  575. expected: expected
  576. };
  577. message = escapeInnerText(message) || (result ? "okay" : "failed");
  578. message = '<span class="test-message">' + message + "</span>";
  579. var output = message;
  580. if (!result) {
  581. expected = escapeInnerText(QUnit.jsDump.parse(expected));
  582. actual = escapeInnerText(QUnit.jsDump.parse(actual));
  583. output += '<table><tr class="test-expected"><th>Expected: </th><td><pre>' + expected + '</pre></td></tr>';
  584. if (actual != expected) {
  585. output += '<tr class="test-actual"><th>Result: </th><td><pre>' + actual + '</pre></td></tr>';
  586. output += '<tr class="test-diff"><th>Diff: </th><td><pre>' + QUnit.diff(expected, actual) +'</pre></td></tr>';
  587. }
  588. var source = sourceFromStacktrace();
  589. if (source) {
  590. details.source = source;
  591. output += '<tr class="test-source"><th>Source: </th><td><pre>' + escapeInnerText(source) + '</pre></td></tr>';
  592. }
  593. output += "</table>";
  594. }
  595. runLoggingCallbacks( 'log', QUnit, details );
  596. config.current.assertions.push({
  597. result: !!result,
  598. message: output
  599. });
  600. },
  601. url: function( params ) {
  602. params = extend( extend( {}, QUnit.urlParams ), params );
  603. var querystring = "?",
  604. key;
  605. for ( key in params ) {
  606. if ( !hasOwn.call( params, key ) ) {
  607. continue;
  608. }
  609. querystring += encodeURIComponent( key ) + "=" +
  610. encodeURIComponent( params[ key ] ) + "&";
  611. }
  612. return window.location.pathname + querystring.slice( 0, -1 );
  613. },
  614. extend: extend,
  615. id: id,
  616. addEvent: addEvent
  617. });
  618. //QUnit.constructor is set to the empty F() above so that we can add to it's prototype later
  619. //Doing this allows us to tell if the following methods have been overwritten on the actual
  620. //QUnit object, which is a deprecated way of using the callbacks.
  621. extend(QUnit.constructor.prototype, {
  622. // Logging callbacks; all receive a single argument with the listed properties
  623. // run test/logs.html for any related changes
  624. begin: registerLoggingCallback('begin'),
  625. // done: { failed, passed, total, runtime }
  626. done: registerLoggingCallback('done'),
  627. // log: { result, actual, expected, message }
  628. log: registerLoggingCallback('log'),
  629. // testStart: { name }
  630. testStart: registerLoggingCallback('testStart'),
  631. // testDone: { name, failed, passed, total }
  632. testDone: registerLoggingCallback('testDone'),
  633. // moduleStart: { name }
  634. moduleStart: registerLoggingCallback('moduleStart'),
  635. // moduleDone: { name, failed, passed, total }
  636. moduleDone: registerLoggingCallback('moduleDone')
  637. });
  638. if ( typeof document === "undefined" || document.readyState === "complete" ) {
  639. config.autorun = true;
  640. }
  641. QUnit.load = function() {
  642. runLoggingCallbacks( 'begin', QUnit, {} );
  643. // Initialize the config, saving the execution queue
  644. var oldconfig = extend({}, config);
  645. QUnit.init();
  646. extend(config, oldconfig);
  647. config.blocking = false;
  648. var urlConfigHtml = '', len = config.urlConfig.length;
  649. for ( var i = 0, val; i < len, val = config.urlConfig[i]; i++ ) {
  650. config[val] = QUnit.urlParams[val];
  651. urlConfigHtml += '<label><input name="' + val + '" type="checkbox"' + ( config[val] ? ' checked="checked"' : '' ) + '>' + val + '</label>';
  652. }
  653. var userAgent = id("qunit-userAgent");
  654. if ( userAgent ) {
  655. userAgent.innerHTML = navigator.userAgent;
  656. }
  657. var banner = id("qunit-header");
  658. if ( banner ) {
  659. banner.innerHTML = '<a href="' + QUnit.url({ filter: undefined }) + '"> ' + banner.innerHTML + '</a> ' + urlConfigHtml;
  660. addEvent( banner, "change", function( event ) {
  661. var params = {};
  662. params[ event.target.name ] = event.target.checked ? true : undefined;
  663. window.location = QUnit.url( params );
  664. });
  665. }
  666. var toolbar = id("qunit-testrunner-toolbar");
  667. if ( toolbar ) {
  668. var filter = document.createElement("input");
  669. filter.type = "checkbox";
  670. filter.id = "qunit-filter-pass";
  671. addEvent( filter, "click", function() {
  672. var ol = document.getElementById("qunit-tests");
  673. if ( filter.checked ) {
  674. ol.className = ol.className + " hidepass";
  675. } else {
  676. var tmp = " " + ol.className.replace( /[\n\t\r]/g, " " ) + " ";
  677. ol.className = tmp.replace(/ hidepass /, " ");
  678. }
  679. if ( defined.sessionStorage ) {
  680. if (filter.checked) {
  681. sessionStorage.setItem("qunit-filter-passed-tests", "true");
  682. } else {
  683. sessionStorage.removeItem("qunit-filter-passed-tests");
  684. }
  685. }
  686. });
  687. if ( config.hidepassed || defined.sessionStorage && sessionStorage.getItem("qunit-filter-passed-tests") ) {
  688. filter.checked = true;
  689. var ol = document.getElementById("qunit-tests");
  690. ol.className = ol.className + " hidepass";
  691. }
  692. toolbar.appendChild( filter );
  693. var label = document.createElement("label");
  694. label.setAttribute("for", "qunit-filter-pass");
  695. label.innerHTML = "Hide passed tests";
  696. toolbar.appendChild( label );
  697. }
  698. var main = id('qunit-fixture');
  699. if ( main ) {
  700. config.fixture = main.cloneNode(true);
  701. }
  702. if (config.autostart) {
  703. QUnit.start();
  704. }
  705. };
  706. addEvent(window, "load", QUnit.load);
  707. // addEvent(window, "error") gives us a useless event object
  708. window.onerror = function( message, file, line ) {
  709. if ( QUnit.config.current ) {
  710. ok( false, message + ", " + file + ":" + line );
  711. } else {
  712. test( "global failure", function() {
  713. ok( false, message + ", " + file + ":" + line );
  714. });
  715. }
  716. };
  717. function done() {
  718. config.autorun = true;
  719. // Log the last module results
  720. if ( config.currentModule ) {
  721. runLoggingCallbacks( 'moduleDone', QUnit, {
  722. name: config.currentModule,
  723. failed: config.moduleStats.bad,
  724. passed: config.moduleStats.all - config.moduleStats.bad,
  725. total: config.moduleStats.all
  726. } );
  727. }
  728. var banner = id("qunit-banner"),
  729. tests = id("qunit-tests"),
  730. runtime = +new Date - config.started,
  731. passed = config.stats.all - config.stats.bad,
  732. html = [
  733. 'Tests completed in ',
  734. runtime,
  735. ' milliseconds.<br/>',
  736. '<span class="passed">',
  737. passed,
  738. '</span> tests of <span class="total">',
  739. config.stats.all,
  740. '</span> passed, <span class="failed">',
  741. config.stats.bad,
  742. '</span> failed.'
  743. ].join('');
  744. if ( banner ) {
  745. banner.className = (config.stats.bad ? "qunit-fail" : "qunit-pass");
  746. }
  747. if ( tests ) {
  748. id( "qunit-testresult" ).innerHTML = html;
  749. }
  750. if ( config.altertitle && typeof document !== "undefined" && document.title ) {
  751. // show ✖ for good, ✔ for bad suite result in title
  752. // use escape sequences in case file gets loaded with non-utf-8-charset
  753. document.title = [
  754. (config.stats.bad ? "\u2716" : "\u2714"),
  755. document.title.replace(/^[\u2714\u2716] /i, "")
  756. ].join(" ");
  757. }
  758. // clear own sessionStorage items if all tests passed
  759. if ( config.reorder && defined.sessionStorage && config.stats.bad === 0 ) {
  760. for (var key in sessionStorage) {
  761. if (sessionStorage.hasOwnProperty(key) && key.indexOf("qunit-") === 0 ) {
  762. sessionStorage.removeItem(key);
  763. }
  764. }
  765. }
  766. runLoggingCallbacks( 'done', QUnit, {
  767. failed: config.stats.bad,
  768. passed: passed,
  769. total: config.stats.all,
  770. runtime: runtime
  771. } );
  772. }
  773. function validTest( name ) {
  774. var filter = config.filter,
  775. run = false;
  776. if ( !filter ) {
  777. return true;
  778. }
  779. var not = filter.charAt( 0 ) === "!";
  780. if ( not ) {
  781. filter = filter.slice( 1 );
  782. }
  783. if ( name.indexOf( filter ) !== -1 ) {
  784. return !not;
  785. }
  786. if ( not ) {
  787. run = true;
  788. }
  789. return run;
  790. }
  791. // so far supports only Firefox, Chrome and Opera (buggy)
  792. // could be extended in the future to use something like https://github.com/csnover/TraceKit
  793. function sourceFromStacktrace(offset) {
  794. offset = offset || 3;
  795. try {
  796. throw new Error();
  797. } catch ( e ) {
  798. if (e.stacktrace) {
  799. // Opera
  800. return e.stacktrace.split("\n")[offset + 3];
  801. } else if (e.stack) {
  802. // Firefox, Chrome
  803. var stack = e.stack.split("\n");
  804. if (/^error$/i.test(stack[0])) {
  805. stack.shift();
  806. }
  807. return stack[offset];
  808. } else if (e.sourceURL) {
  809. // Safari, PhantomJS
  810. // TODO sourceURL points at the 'throw new Error' line above, useless
  811. //return e.sourceURL + ":" + e.line;
  812. }
  813. }
  814. }
  815. function escapeInnerText(s) {
  816. if (!s) {
  817. return "";
  818. }
  819. s = s + "";
  820. return s.replace(/[\&<>]/g, function(s) {
  821. switch(s) {
  822. case "&": return "&amp;";
  823. case "<": return "&lt;";
  824. case ">": return "&gt;";
  825. default: return s;
  826. }
  827. });
  828. }
  829. function synchronize( callback, last ) {
  830. config.queue.push( callback );
  831. if ( config.autorun && !config.blocking ) {
  832. process(last);
  833. }
  834. }
  835. function process( last ) {
  836. var start = new Date().getTime();
  837. config.depth = config.depth ? config.depth + 1 : 1;
  838. while ( config.queue.length && !config.blocking ) {
  839. if ( !defined.setTimeout || config.updateRate <= 0 || ( ( new Date().getTime() - start ) < config.updateRate ) ) {
  840. config.queue.shift()();
  841. } else {
  842. window.setTimeout( function(){
  843. process( last );
  844. }, 13 );
  845. break;
  846. }
  847. }
  848. config.depth--;
  849. if ( last && !config.blocking && !config.queue.length && config.depth === 0 ) {
  850. done();
  851. }
  852. }
  853. function saveGlobal() {
  854. config.pollution = [];
  855. if ( config.noglobals ) {
  856. for ( var key in window ) {
  857. if ( !hasOwn.call( window, key ) ) {
  858. continue;
  859. }
  860. config.pollution.push( key );
  861. }
  862. }
  863. }
  864. function checkPollution( name ) {
  865. var old = config.pollution;
  866. saveGlobal();
  867. var newGlobals = diff( config.pollution, old );
  868. if ( newGlobals.length > 0 ) {
  869. ok( false, "Introduced global variable(s): " + newGlobals.join(", ") );
  870. }
  871. var deletedGlobals = diff( old, config.pollution );
  872. if ( deletedGlobals.length > 0 ) {
  873. ok( false, "Deleted global variable(s): " + deletedGlobals.join(", ") );
  874. }
  875. }
  876. // returns a new Array with the elements that are in a but not in b
  877. function diff( a, b ) {
  878. var result = a.slice();
  879. for ( var i = 0; i < result.length; i++ ) {
  880. for ( var j = 0; j < b.length; j++ ) {
  881. if ( result[i] === b[j] ) {
  882. result.splice(i, 1);
  883. i--;
  884. break;
  885. }
  886. }
  887. }
  888. return result;
  889. }
  890. function fail(message, exception, callback) {
  891. if ( typeof console !== "undefined" && console.error && console.warn ) {
  892. console.error(message);
  893. console.error(exception);
  894. console.error(exception.stack);
  895. console.warn(callback.toString());
  896. } else if ( window.opera && opera.postError ) {
  897. opera.postError(message, exception, callback.toString);
  898. }
  899. }
  900. function extend(a, b) {
  901. for ( var prop in b ) {
  902. if ( b[prop] === undefined ) {
  903. delete a[prop];
  904. // Avoid "Member not found" error in IE8 caused by setting window.constructor
  905. } else if ( prop !== "constructor" || a !== window ) {
  906. a[prop] = b[prop];
  907. }
  908. }
  909. return a;
  910. }
  911. function addEvent(elem, type, fn) {
  912. if ( elem.addEventListener ) {
  913. elem.addEventListener( type, fn, false );
  914. } else if ( elem.attachEvent ) {
  915. elem.attachEvent( "on" + type, fn );
  916. } else {
  917. fn();
  918. }
  919. }
  920. function id(name) {
  921. return !!(typeof document !== "undefined" && document && document.getElementById) &&
  922. document.getElementById( name );
  923. }
  924. function registerLoggingCallback(key){
  925. return function(callback){
  926. config[key].push( callback );
  927. };
  928. }
  929. // Supports deprecated method of completely overwriting logging callbacks
  930. function runLoggingCallbacks(key, scope, args) {
  931. //debugger;
  932. var callbacks;
  933. if ( QUnit.hasOwnProperty(key) ) {
  934. QUnit[key].call(scope, args);
  935. } else {
  936. callbacks = config[key];
  937. for( var i = 0; i < callbacks.length; i++ ) {
  938. callbacks[i].call( scope, args );
  939. }
  940. }
  941. }
  942. // Test for equality any JavaScript type.
  943. // Author: Philippe Rathé <prathe@gmail.com>
  944. QUnit.equiv = function () {
  945. var innerEquiv; // the real equiv function
  946. var callers = []; // stack to decide between skip/abort functions
  947. var parents = []; // stack to avoiding loops from circular referencing
  948. // Call the o related callback with the given arguments.
  949. function bindCallbacks(o, callbacks, args) {
  950. var prop = QUnit.objectType(o);
  951. if (prop) {
  952. if (QUnit.objectType(callbacks[prop]) === "function") {
  953. return callbacks[prop].apply(callbacks, args);
  954. } else {
  955. return callbacks[prop]; // or undefined
  956. }
  957. }
  958. }
  959. var getProto = Object.getPrototypeOf || function (obj) {
  960. return obj.__proto__;
  961. };
  962. var callbacks = function () {
  963. // for string, boolean, number and null
  964. function useStrictEquality(b, a) {
  965. if (b instanceof a.constructor || a instanceof b.constructor) {
  966. // to catch short annotaion VS 'new' annotation of a
  967. // declaration
  968. // e.g. var i = 1;
  969. // var j = new Number(1);
  970. return a == b;
  971. } else {
  972. return a === b;
  973. }
  974. }
  975. return {
  976. "string" : useStrictEquality,
  977. "boolean" : useStrictEquality,
  978. "number" : useStrictEquality,
  979. "null" : useStrictEquality,
  980. "undefined" : useStrictEquality,
  981. "nan" : function(b) {
  982. return isNaN(b);
  983. },
  984. "date" : function(b, a) {
  985. return QUnit.objectType(b) === "date"
  986. && a.valueOf() === b.valueOf();
  987. },
  988. "regexp" : function(b, a) {
  989. return QUnit.objectType(b) === "regexp"
  990. && a.source === b.source && // the regex itself
  991. a.global === b.global && // and its modifers
  992. // (gmi) ...
  993. a.ignoreCase === b.ignoreCase
  994. && a.multiline === b.multiline;
  995. },
  996. // - skip when the property is a method of an instance (OOP)
  997. // - abort otherwise,
  998. // initial === would have catch identical references anyway
  999. "function" : function() {
  1000. var caller = callers[callers.length - 1];
  1001. return caller !== Object && typeof caller !== "undefined";
  1002. },
  1003. "array" : function(b, a) {
  1004. var i, j, loop;
  1005. var len;
  1006. // b could be an object literal here
  1007. if (!(QUnit.objectType(b) === "array")) {
  1008. return false;
  1009. }
  1010. len = a.length;
  1011. if (len !== b.length) { // safe and faster
  1012. return false;
  1013. }
  1014. // track reference to avoid circular references
  1015. parents.push(a);
  1016. for (i = 0; i < len; i++) {
  1017. loop = false;
  1018. for (j = 0; j < parents.length; j++) {
  1019. if (parents[j] === a[i]) {
  1020. loop = true;// dont rewalk array
  1021. }
  1022. }
  1023. if (!loop && !innerEquiv(a[i], b[i])) {
  1024. parents.pop();
  1025. return false;
  1026. }
  1027. }
  1028. parents.pop();
  1029. return true;
  1030. },
  1031. "object" : function(b, a) {
  1032. var i, j, loop;
  1033. var eq = true; // unless we can proove it
  1034. var aProperties = [], bProperties = []; // collection of
  1035. // strings
  1036. // comparing constructors is more strict than using
  1037. // instanceof
  1038. if (a.constructor !== b.constructor) {
  1039. // Allow objects with no prototype to be equivalent to
  1040. // objects with Object as their constructor.
  1041. if (!((getProto(a) === null && getProto(b) === Object.prototype) ||
  1042. (getProto(b) === null && getProto(a) === Object.prototype)))
  1043. {
  1044. return false;
  1045. }
  1046. }
  1047. // stack constructor before traversing properties
  1048. callers.push(a.constructor);
  1049. // track reference to avoid circular references
  1050. parents.push(a);
  1051. for (i in a) { // be strict: don't ensures hasOwnProperty
  1052. // and go deep
  1053. loop = false;
  1054. for (j = 0; j < parents.length; j++) {
  1055. if (parents[j] === a[i])
  1056. loop = true; // don't go down the same path
  1057. // twice
  1058. }
  1059. aProperties.push(i); // collect a's properties
  1060. if (!loop && !innerEquiv(a[i], b[i])) {
  1061. eq = false;
  1062. break;
  1063. }
  1064. }
  1065. callers.pop(); // unstack, we are done
  1066. parents.pop();
  1067. for (i in b) {
  1068. bProperties.push(i); // collect b's properties
  1069. }
  1070. // Ensures identical properties name
  1071. return eq
  1072. && innerEquiv(aProperties.sort(), bProperties
  1073. .sort());
  1074. }
  1075. };
  1076. }();
  1077. innerEquiv = function() { // can take multiple arguments
  1078. var args = Array.prototype.slice.apply(arguments);
  1079. if (args.length < 2) {
  1080. return true; // end transition
  1081. }
  1082. return (function(a, b) {
  1083. if (a === b) {
  1084. return true; // catch the most you can
  1085. } else if (a === null || b === null || typeof a === "undefined"
  1086. || typeof b === "undefined"
  1087. || QUnit.objectType(a) !== QUnit.objectType(b)) {
  1088. return false; // don't lose time with error prone cases
  1089. } else {
  1090. return bindCallbacks(a, callbacks, [ b, a ]);
  1091. }
  1092. // apply transition with (1..n) arguments
  1093. })(args[0], args[1])
  1094. && arguments.callee.apply(this, args.splice(1,
  1095. args.length - 1));
  1096. };
  1097. return innerEquiv;
  1098. }();
  1099. /**
  1100. * jsDump Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com |
  1101. * http://flesler.blogspot.com Licensed under BSD
  1102. * (http://www.opensource.org/licenses/bsd-license.php) Date: 5/15/2008
  1103. *
  1104. * @projectDescription Advanced and extensible data dumping for Javascript.
  1105. * @version 1.0.0
  1106. * @author Ariel Flesler
  1107. * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
  1108. */
  1109. QUnit.jsDump = (function() {
  1110. function quote( str ) {
  1111. return '"' + str.toString().replace(/"/g, '\\"') + '"';
  1112. };
  1113. function literal( o ) {
  1114. return o + '';
  1115. };
  1116. function join( pre, arr, post ) {
  1117. var s = jsDump.separator(),
  1118. base = jsDump.indent(),
  1119. inner = jsDump.indent(1);
  1120. if ( arr.join )
  1121. arr = arr.join( ',' + s + inner );
  1122. if ( !arr )
  1123. return pre + post;
  1124. return [ pre, inner + arr, base + post ].join(s);
  1125. };
  1126. function array( arr, stack ) {
  1127. var i = arr.length, ret = Array(i);
  1128. this.up();
  1129. while ( i-- )
  1130. ret[i] = this.parse( arr[i] , undefined , stack);
  1131. this.down();
  1132. return join( '[', ret, ']' );
  1133. };
  1134. var reName = /^function (\w+)/;
  1135. var jsDump = {
  1136. parse:function( obj, type, stack ) { //type is used mostly internally, you can fix a (custom)type in advance
  1137. stack = stack || [ ];
  1138. var parser = this.parsers[ type || this.typeOf(obj) ];
  1139. type = typeof parser;
  1140. var inStack = inArray(obj, stack);
  1141. if (inStack != -1) {
  1142. return 'recursion('+(inStack - stack.length)+')';
  1143. }
  1144. //else
  1145. if (type == 'function') {
  1146. stack.push(obj);
  1147. var res = parser.call( this, obj, stack );
  1148. stack.pop();
  1149. return res;
  1150. }
  1151. // else
  1152. return (type == 'string') ? parser : this.parsers.error;
  1153. },
  1154. typeOf:function( obj ) {
  1155. var type;
  1156. if ( obj === null ) {
  1157. type = "null";
  1158. } else if (typeof obj === "undefined") {
  1159. type = "undefined";
  1160. } else if (QUnit.is("RegExp", obj)) {
  1161. type = "regexp";
  1162. } else if (QUnit.is("Date", obj)) {
  1163. type = "date";
  1164. } else if (QUnit.is("Function", obj)) {
  1165. type = "function";
  1166. } else if (typeof obj.setInterval !== undefined && typeof obj.document !== "undefined" && typeof obj.nodeType === "undefined") {
  1167. type = "window";
  1168. } else if (obj.nodeType === 9) {
  1169. type = "document";
  1170. } else if (obj.nodeType) {
  1171. type = "node";
  1172. } else if (
  1173. // native arrays
  1174. toString.call( obj ) === "[object Array]" ||
  1175. // NodeList objects
  1176. ( typeof obj.length === "number" && typeof obj.item !== "undefined" && ( obj.length ? obj.item(0) === obj[0] : ( obj.item( 0 ) === null && typeof obj[0] === "undefined" ) ) )
  1177. ) {
  1178. type = "array";
  1179. } else {
  1180. type = typeof obj;
  1181. }
  1182. return type;
  1183. },
  1184. separator:function() {
  1185. return this.multiline ? this.HTML ? '<br />' : '\n' : this.HTML ? '&nbsp;' : ' ';
  1186. },
  1187. indent:function( extra ) {// extra can be a number, shortcut for increasing-calling-decreasing
  1188. if ( !this.multiline )
  1189. return '';
  1190. var chr = this.indentChar;
  1191. if ( this.HTML )
  1192. chr = chr.replace(/\t/g,' ').replace(/ /g,'&nbsp;');
  1193. return Array( this._depth_ + (extra||0) ).join(chr);
  1194. },
  1195. up:function( a ) {
  1196. this._depth_ += a || 1;
  1197. },
  1198. down:function( a ) {
  1199. this._depth_ -= a || 1;
  1200. },
  1201. setParser:function( name, parser ) {
  1202. this.parsers[name] = parser;
  1203. },
  1204. // The next 3 are exposed so you can use them
  1205. quote:quote,
  1206. literal:literal,
  1207. join:join,
  1208. //
  1209. _depth_: 1,
  1210. // This is the list of parsers, to modify them, use jsDump.setParser
  1211. parsers:{
  1212. window: '[Window]',
  1213. document: '[Document]',
  1214. error:'[ERROR]', //when no parser is found, shouldn't happen
  1215. unknown: '[Unknown]',
  1216. 'null':'null',
  1217. 'undefined':'undefined',
  1218. 'function':function( fn ) {
  1219. var ret = 'function',
  1220. name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE
  1221. if ( name )
  1222. ret += ' ' + name;
  1223. ret += '(';
  1224. ret = [ ret, QUnit.jsDump.parse( fn, 'functionArgs' ), '){'].join('');
  1225. return join( ret, QUnit.jsDump.parse(fn,'functionCode'), '}' );
  1226. },
  1227. array: array,
  1228. nodelist: array,
  1229. arguments: array,
  1230. object:function( map, stack ) {
  1231. var ret = [ ];
  1232. QUnit.jsDump.up();
  1233. for ( var key in map ) {
  1234. var val = map[key];
  1235. ret.push( QUnit.jsDump.parse(key,'key') + ': ' + QUnit.jsDump.parse(val, undefined, stack));
  1236. }
  1237. QUnit.jsDump.down();
  1238. return join( '{', ret, '}' );
  1239. },
  1240. node:function( node ) {
  1241. var open = QUnit.jsDump.HTML ? '&lt;' : '<',
  1242. close = QUnit.jsDump.HTML ? '&gt;' : '>';
  1243. var tag = node.nodeName.toLowerCase(),
  1244. ret = open + tag;
  1245. for ( var a in QUnit.jsDump.DOMAttrs ) {
  1246. var val = node[QUnit.jsDump.DOMAttrs[a]];
  1247. if ( val )
  1248. ret += ' ' + a + '=' + QUnit.jsDump.parse( val, 'attribute' );
  1249. }
  1250. return ret + close + open + '/' + tag + close;
  1251. },
  1252. functionArgs:function( fn ) {//function calls it internally, it's the arguments part of the function
  1253. var l = fn.length;
  1254. if ( !l ) return '';
  1255. var args = Array(l);
  1256. while ( l-- )
  1257. args[l] = String.fromCharCode(97+l);//97 is 'a'
  1258. return ' ' + args.join(', ') + ' ';
  1259. },
  1260. key:quote, //object calls it internally, the key part of an item in a map
  1261. functionCode:'[code]', //function calls it internally, it's the content of the function
  1262. attribute:quote, //node calls it internally, it's an html attribute value
  1263. string:quote,
  1264. date:quote,
  1265. regexp:literal, //regex
  1266. number:literal,
  1267. 'boolean':literal
  1268. },
  1269. DOMAttrs:{//attributes to dump from nodes, name=>realName
  1270. id:'id',
  1271. name:'name',
  1272. 'class':'className'
  1273. },
  1274. HTML:false,//if true, entities are escaped ( <, >, \t, space and \n )
  1275. indentChar:' ',//indentation unit
  1276. multiline:true //if true, items in a collection, are separated by a \n, else just a space.
  1277. };
  1278. return jsDump;
  1279. })();
  1280. // from Sizzle.js
  1281. function getText( elems ) {
  1282. var ret = "", elem;
  1283. for ( var i = 0; elems[i]; i++ ) {
  1284. elem = elems[i];
  1285. // Get the text from text nodes and CDATA nodes
  1286. if ( elem.nodeType === 3 || elem.nodeType === 4 ) {
  1287. ret += elem.nodeValue;
  1288. // Traverse everything else, except comment nodes
  1289. } else if ( elem.nodeType !== 8 ) {
  1290. ret += getText( elem.childNodes );
  1291. }
  1292. }
  1293. return ret;
  1294. };
  1295. //from jquery.js
  1296. function inArray( elem, array ) {
  1297. if ( array.indexOf ) {
  1298. return array.indexOf( elem );
  1299. }
  1300. for ( var i = 0, length = array.length; i < length; i++ ) {
  1301. if ( array[ i ] === elem ) {
  1302. return i;
  1303. }
  1304. }
  1305. return -1;
  1306. }
  1307. /*
  1308. * Javascript Diff Algorithm
  1309. * By John Resig (http://ejohn.org/)
  1310. * Modified by Chu Alan "sprite"
  1311. *
  1312. * Released under the MIT license.
  1313. *
  1314. * More Info:
  1315. * http://ejohn.org/projects/javascript-diff-algorithm/
  1316. *
  1317. * Usage: QUnit.diff(expected, actual)
  1318. *
  1319. * 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"
  1320. */
  1321. QUnit.diff = (function() {
  1322. function diff(o, n) {
  1323. var ns = {};
  1324. var os = {};
  1325. for (var i = 0; i < n.length; i++) {
  1326. if (ns[n[i]] == null)
  1327. ns[n[i]] = {
  1328. rows: [],
  1329. o: null
  1330. };
  1331. ns[n[i]].rows.push(i);
  1332. }
  1333. for (var i = 0; i < o.length; i++) {
  1334. if (os[o[i]] == null)
  1335. os[o[i]] = {
  1336. rows: [],
  1337. n: null
  1338. };
  1339. os[o[i]].rows.push(i);
  1340. }
  1341. for (var i in ns) {
  1342. if ( !hasOwn.call( ns, i ) ) {
  1343. continue;
  1344. }
  1345. if (ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1) {
  1346. n[ns[i].rows[0]] = {
  1347. text: n[ns[i].rows[0]],
  1348. row: os[i].rows[0]
  1349. };
  1350. o[os[i].rows[0]] = {
  1351. text: o[os[i].rows[0]],
  1352. row: ns[i].rows[0]
  1353. };
  1354. }
  1355. }
  1356. for (var i = 0; i < n.length - 1; i++) {
  1357. if (n[i].text != null && n[i + 1].text == null && n[i].row + 1 < o.length && o[n[i].row + 1].text == null &&
  1358. n[i + 1] == o[n[i].row + 1]) {
  1359. n[i + 1] = {
  1360. text: n[i + 1],
  1361. row: n[i].row + 1
  1362. };
  1363. o[n[i].row + 1] = {
  1364. text: o[n[i].row + 1],
  1365. row: i + 1
  1366. };
  1367. }
  1368. }
  1369. for (var i = n.length - 1; i > 0; i--) {
  1370. if (n[i].text != null && n[i - 1].text == null && n[i].row > 0 && o[n[i].row - 1].text == null &&
  1371. n[i - 1] == o[n[i].row - 1]) {
  1372. n[i - 1] = {
  1373. text: n[i - 1],
  1374. row: n[i].row - 1
  1375. };
  1376. o[n[i].row - 1] = {
  1377. text: o[n[i].row - 1],
  1378. row: i - 1
  1379. };
  1380. }
  1381. }
  1382. return {
  1383. o: o,
  1384. n: n
  1385. };
  1386. }
  1387. return function(o, n) {
  1388. o = o.replace(/\s+$/, '');
  1389. n = n.replace(/\s+$/, '');
  1390. var out = diff(o == "" ? [] : o.split(/\s+/), n == "" ? [] : n.split(/\s+/));
  1391. var str = "";
  1392. var oSpace = o.match(/\s+/g);
  1393. if (oSpace == null) {
  1394. oSpace = [" "];
  1395. }
  1396. else {
  1397. oSpace.push(" ");
  1398. }
  1399. var nSpace = n.match(/\s+/g);
  1400. if (nSpace == null) {
  1401. nSpace = [" "];
  1402. }
  1403. else {
  1404. nSpace.push(" ");
  1405. }
  1406. if (out.n.length == 0) {
  1407. for (var i = 0; i < out.o.length; i++) {
  1408. str += '<del>' + out.o[i] + oSpace[i] + "</del>";
  1409. }
  1410. }
  1411. else {
  1412. if (out.n[0].text == null) {
  1413. for (n = 0; n < out.o.length && out.o[n].text == null; n++) {
  1414. str += '<del>' + out.o[n] + oSpace[n] + "</del>";
  1415. }
  1416. }
  1417. for (var i = 0; i < out.n.length; i++) {
  1418. if (out.n[i].text == null) {
  1419. str += '<ins>' + out.n[i] + nSpace[i] + "</ins>";
  1420. }
  1421. else {
  1422. var pre = "";
  1423. for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++) {
  1424. pre += '<del>' + out.o[n] + oSpace[n] + "</del>";
  1425. }
  1426. str += " " + out.n[i].text + nSpace[i] + pre;
  1427. }
  1428. }
  1429. }
  1430. return str;
  1431. };
  1432. })();
  1433. // get at whatever the global object is, like window in browsers
  1434. })( (function() {return this}).call() );