qunit.js 42 KB

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