qunit.js 44 KB

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