qunit.js 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  1. /**
  2. * QUnit v1.12.0pre - A JavaScript Unit Testing Framework
  3. *
  4. * http://qunitjs.com
  5. *
  6. * Copyright 2012 jQuery Foundation and other contributors
  7. * Released under the MIT license.
  8. * http://jquery.org/license
  9. */
  10. (function( window ) {
  11. var QUnit,
  12. assert,
  13. config,
  14. onErrorFnPrev,
  15. testId = 0,
  16. fileName = (sourceFromStacktrace( 0 ) || "" ).replace(/(:\d+)+\)?/, "").replace(/.+\//, ""),
  17. toString = Object.prototype.toString,
  18. hasOwn = Object.prototype.hasOwnProperty,
  19. // Keep a local reference to Date (GH-283)
  20. Date = window.Date,
  21. defined = {
  22. setTimeout: typeof window.setTimeout !== "undefined",
  23. sessionStorage: (function() {
  24. var x = "qunit-test-string";
  25. try {
  26. sessionStorage.setItem( x, x );
  27. sessionStorage.removeItem( x );
  28. return true;
  29. } catch( e ) {
  30. return false;
  31. }
  32. }())
  33. },
  34. /**
  35. * Provides a normalized error string, correcting an issue
  36. * with IE 7 (and prior) where Error.prototype.toString is
  37. * not properly implemented
  38. *
  39. * Based on http://es5.github.com/#x15.11.4.4
  40. *
  41. * @param {String|Error} error
  42. * @return {String} error message
  43. */
  44. errorString = function( error ) {
  45. var name, message,
  46. errorString = error.toString();
  47. if ( errorString.substring( 0, 7 ) === "[object" ) {
  48. name = error.name ? error.name.toString() : "Error";
  49. message = error.message ? error.message.toString() : "";
  50. if ( name && message ) {
  51. return name + ": " + message;
  52. } else if ( name ) {
  53. return name;
  54. } else if ( message ) {
  55. return message;
  56. } else {
  57. return "Error";
  58. }
  59. } else {
  60. return errorString;
  61. }
  62. },
  63. /**
  64. * Makes a clone of an object using only Array or Object as base,
  65. * and copies over the own enumerable properties.
  66. *
  67. * @param {Object} obj
  68. * @return {Object} New object with only the own properties (recursively).
  69. */
  70. objectValues = function( obj ) {
  71. // Grunt 0.3.x uses an older version of jshint that still has jshint/jshint#392.
  72. /*jshint newcap: false */
  73. var key, val,
  74. vals = QUnit.is( "array", obj ) ? [] : {};
  75. for ( key in obj ) {
  76. if ( hasOwn.call( obj, key ) ) {
  77. val = obj[key];
  78. vals[key] = val === Object(val) ? objectValues(val) : val;
  79. }
  80. }
  81. return vals;
  82. };
  83. function Test( settings ) {
  84. extend( this, settings );
  85. this.assertions = [];
  86. this.testNumber = ++Test.count;
  87. }
  88. Test.count = 0;
  89. Test.prototype = {
  90. init: function() {
  91. var a, b, li,
  92. tests = id( "qunit-tests" );
  93. if ( tests ) {
  94. b = document.createElement( "strong" );
  95. b.innerHTML = this.nameHtml;
  96. // `a` initialized at top of scope
  97. a = document.createElement( "a" );
  98. a.innerHTML = "Rerun";
  99. a.href = QUnit.url({ testNumber: this.testNumber });
  100. li = document.createElement( "li" );
  101. li.appendChild( b );
  102. li.appendChild( a );
  103. li.className = "running";
  104. li.id = this.id = "qunit-test-output" + testId++;
  105. tests.appendChild( li );
  106. }
  107. },
  108. setup: function() {
  109. if ( this.module !== config.previousModule ) {
  110. if ( config.previousModule ) {
  111. runLoggingCallbacks( "moduleDone", QUnit, {
  112. name: config.previousModule,
  113. failed: config.moduleStats.bad,
  114. passed: config.moduleStats.all - config.moduleStats.bad,
  115. total: config.moduleStats.all
  116. });
  117. }
  118. config.previousModule = this.module;
  119. config.moduleStats = { all: 0, bad: 0 };
  120. runLoggingCallbacks( "moduleStart", QUnit, {
  121. name: this.module
  122. });
  123. } else if ( config.autorun ) {
  124. runLoggingCallbacks( "moduleStart", QUnit, {
  125. name: this.module
  126. });
  127. }
  128. config.current = this;
  129. this.testEnvironment = extend({
  130. setup: function() {},
  131. teardown: function() {}
  132. }, this.moduleTestEnvironment );
  133. this.started = +new Date();
  134. runLoggingCallbacks( "testStart", QUnit, {
  135. name: this.testName,
  136. module: this.module
  137. });
  138. // allow utility functions to access the current test environment
  139. // TODO why??
  140. QUnit.current_testEnvironment = this.testEnvironment;
  141. if ( !config.pollution ) {
  142. saveGlobal();
  143. }
  144. if ( config.notrycatch ) {
  145. this.testEnvironment.setup.call( this.testEnvironment );
  146. return;
  147. }
  148. try {
  149. this.testEnvironment.setup.call( this.testEnvironment );
  150. } catch( e ) {
  151. QUnit.pushFailure( "Setup failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 1 ) );
  152. }
  153. },
  154. run: function() {
  155. config.current = this;
  156. var running = id( "qunit-testresult" );
  157. if ( running ) {
  158. running.innerHTML = "Running: <br/>" + this.nameHtml;
  159. }
  160. if ( this.async ) {
  161. QUnit.stop();
  162. }
  163. this.callbackStarted = +new Date();
  164. if ( config.notrycatch ) {
  165. this.callback.call( this.testEnvironment, QUnit.assert );
  166. this.callbackRuntime = +new Date() - this.callbackStarted;
  167. return;
  168. }
  169. try {
  170. this.callback.call( this.testEnvironment, QUnit.assert );
  171. this.callbackRuntime = +new Date() - this.callbackStarted;
  172. } catch( e ) {
  173. this.callbackRuntime = +new Date() - this.callbackStarted;
  174. QUnit.pushFailure( "Died on test #" + (this.assertions.length + 1) + " " + this.stack + ": " + ( e.message || e ), extractStacktrace( e, 0 ) );
  175. // else next test will carry the responsibility
  176. saveGlobal();
  177. // Restart the tests if they're blocking
  178. if ( config.blocking ) {
  179. QUnit.start();
  180. }
  181. }
  182. },
  183. teardown: function() {
  184. config.current = this;
  185. if ( config.notrycatch ) {
  186. if ( typeof this.callbackRuntime === "undefined" ) {
  187. this.callbackRuntime = +new Date() - this.callbackStarted;
  188. }
  189. this.testEnvironment.teardown.call( this.testEnvironment );
  190. return;
  191. } else {
  192. try {
  193. this.testEnvironment.teardown.call( this.testEnvironment );
  194. } catch( e ) {
  195. QUnit.pushFailure( "Teardown failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 1 ) );
  196. }
  197. }
  198. checkPollution();
  199. },
  200. finish: function() {
  201. config.current = this;
  202. if ( config.requireExpects && this.expected === null ) {
  203. QUnit.pushFailure( "Expected number of assertions to be defined, but expect() was not called.", this.stack );
  204. } else if ( this.expected !== null && this.expected !== this.assertions.length ) {
  205. QUnit.pushFailure( "Expected " + this.expected + " assertions, but " + this.assertions.length + " were run", this.stack );
  206. } else if ( this.expected === null && !this.assertions.length ) {
  207. QUnit.pushFailure( "Expected at least one assertion, but none were run - call expect(0) to accept zero assertions.", this.stack );
  208. }
  209. var i, assertion, a, b, time, li, ol,
  210. test = this,
  211. good = 0,
  212. bad = 0,
  213. tests = id( "qunit-tests" );
  214. this.runtime = +new Date() - this.started;
  215. config.stats.all += this.assertions.length;
  216. config.moduleStats.all += this.assertions.length;
  217. if ( tests ) {
  218. ol = document.createElement( "ol" );
  219. ol.className = "qunit-assert-list";
  220. for ( i = 0; i < this.assertions.length; i++ ) {
  221. assertion = this.assertions[i];
  222. li = document.createElement( "li" );
  223. li.className = assertion.result ? "pass" : "fail";
  224. li.innerHTML = assertion.message || ( assertion.result ? "okay" : "failed" );
  225. ol.appendChild( li );
  226. if ( assertion.result ) {
  227. good++;
  228. } else {
  229. bad++;
  230. config.stats.bad++;
  231. config.moduleStats.bad++;
  232. }
  233. }
  234. // store result when possible
  235. if ( QUnit.config.reorder && defined.sessionStorage ) {
  236. if ( bad ) {
  237. sessionStorage.setItem( "qunit-test-" + this.module + "-" + this.testName, bad );
  238. } else {
  239. sessionStorage.removeItem( "qunit-test-" + this.module + "-" + this.testName );
  240. }
  241. }
  242. if ( bad === 0 ) {
  243. addClass( ol, "qunit-collapsed" );
  244. }
  245. // `b` initialized at top of scope
  246. b = document.createElement( "strong" );
  247. b.innerHTML = this.nameHtml + " <b class='counts'>(<b class='failed'>" + bad + "</b>, <b class='passed'>" + good + "</b>, " + this.assertions.length + ")</b>";
  248. addEvent(b, "click", function() {
  249. var next = b.parentNode.lastChild,
  250. collapsed = hasClass( next, "qunit-collapsed" );
  251. ( collapsed ? removeClass : addClass )( next, "qunit-collapsed" );
  252. });
  253. addEvent(b, "dblclick", function( e ) {
  254. var target = e && e.target ? e.target : window.event.srcElement;
  255. if ( target.nodeName.toLowerCase() === "span" || target.nodeName.toLowerCase() === "b" ) {
  256. target = target.parentNode;
  257. }
  258. if ( window.location && target.nodeName.toLowerCase() === "strong" ) {
  259. window.location = QUnit.url({ testNumber: test.testNumber });
  260. }
  261. });
  262. // `time` initialized at top of scope
  263. time = document.createElement( "span" );
  264. time.className = "runtime";
  265. time.innerHTML = this.runtime + " ms";
  266. // `li` initialized at top of scope
  267. li = id( this.id );
  268. li.className = bad ? "fail" : "pass";
  269. li.removeChild( li.firstChild );
  270. a = li.firstChild;
  271. li.appendChild( b );
  272. li.appendChild( a );
  273. li.appendChild( time );
  274. li.appendChild( ol );
  275. } else {
  276. for ( i = 0; i < this.assertions.length; i++ ) {
  277. if ( !this.assertions[i].result ) {
  278. bad++;
  279. config.stats.bad++;
  280. config.moduleStats.bad++;
  281. }
  282. }
  283. }
  284. runLoggingCallbacks( "testDone", QUnit, {
  285. name: this.testName,
  286. module: this.module,
  287. failed: bad,
  288. passed: this.assertions.length - bad,
  289. total: this.assertions.length,
  290. duration: this.runtime
  291. });
  292. QUnit.reset();
  293. config.current = undefined;
  294. },
  295. queue: function() {
  296. var bad,
  297. test = this;
  298. synchronize(function() {
  299. test.init();
  300. });
  301. function run() {
  302. // each of these can by async
  303. synchronize(function() {
  304. test.setup();
  305. });
  306. synchronize(function() {
  307. test.run();
  308. });
  309. synchronize(function() {
  310. test.teardown();
  311. });
  312. synchronize(function() {
  313. test.finish();
  314. });
  315. }
  316. // `bad` initialized at top of scope
  317. // defer when previous test run passed, if storage is available
  318. bad = QUnit.config.reorder && defined.sessionStorage &&
  319. +sessionStorage.getItem( "qunit-test-" + this.module + "-" + this.testName );
  320. if ( bad ) {
  321. run();
  322. } else {
  323. synchronize( run, true );
  324. }
  325. }
  326. };
  327. // Root QUnit object.
  328. // `QUnit` initialized at top of scope
  329. QUnit = {
  330. // call on start of module test to prepend name to all tests
  331. module: function( name, testEnvironment ) {
  332. config.currentModule = name;
  333. config.currentModuleTestEnvironment = testEnvironment;
  334. config.modules[name] = true;
  335. },
  336. asyncTest: function( testName, expected, callback ) {
  337. if ( arguments.length === 2 ) {
  338. callback = expected;
  339. expected = null;
  340. }
  341. QUnit.test( testName, expected, callback, true );
  342. },
  343. test: function( testName, expected, callback, async ) {
  344. var test,
  345. nameHtml = "<span class='test-name'>" + escapeText( testName ) + "</span>";
  346. if ( arguments.length === 2 ) {
  347. callback = expected;
  348. expected = null;
  349. }
  350. if ( config.currentModule ) {
  351. nameHtml = "<span class='module-name'>" + escapeText( config.currentModule ) + "</span>: " + nameHtml;
  352. }
  353. test = new Test({
  354. nameHtml: nameHtml,
  355. testName: testName,
  356. expected: expected,
  357. async: async,
  358. callback: callback,
  359. module: config.currentModule,
  360. moduleTestEnvironment: config.currentModuleTestEnvironment,
  361. stack: sourceFromStacktrace( 2 )
  362. });
  363. if ( !validTest( test ) ) {
  364. return;
  365. }
  366. test.queue();
  367. },
  368. // Specify the number of expected assertions to guarantee that failed test (no assertions are run at all) don't slip through.
  369. expect: function( asserts ) {
  370. if (arguments.length === 1) {
  371. config.current.expected = asserts;
  372. } else {
  373. return config.current.expected;
  374. }
  375. },
  376. start: function( count ) {
  377. // QUnit hasn't been initialized yet.
  378. // Note: RequireJS (et al) may delay onLoad
  379. if ( config.semaphore === undefined ) {
  380. QUnit.begin(function() {
  381. // This is triggered at the top of QUnit.load, push start() to the event loop, to allow QUnit.load to finish first
  382. setTimeout(function() {
  383. QUnit.start( count );
  384. });
  385. });
  386. return;
  387. }
  388. config.semaphore -= count || 1;
  389. // don't start until equal number of stop-calls
  390. if ( config.semaphore > 0 ) {
  391. return;
  392. }
  393. // ignore if start is called more often then stop
  394. if ( config.semaphore < 0 ) {
  395. config.semaphore = 0;
  396. QUnit.pushFailure( "Called start() while already started (QUnit.config.semaphore was 0 already)", null, sourceFromStacktrace(2) );
  397. return;
  398. }
  399. // A slight delay, to avoid any current callbacks
  400. if ( defined.setTimeout ) {
  401. window.setTimeout(function() {
  402. if ( config.semaphore > 0 ) {
  403. return;
  404. }
  405. if ( config.timeout ) {
  406. clearTimeout( config.timeout );
  407. }
  408. config.blocking = false;
  409. process( true );
  410. }, 13);
  411. } else {
  412. config.blocking = false;
  413. process( true );
  414. }
  415. },
  416. stop: function( count ) {
  417. config.semaphore += count || 1;
  418. config.blocking = true;
  419. if ( config.testTimeout && defined.setTimeout ) {
  420. clearTimeout( config.timeout );
  421. config.timeout = window.setTimeout(function() {
  422. QUnit.ok( false, "Test timed out" );
  423. config.semaphore = 1;
  424. QUnit.start();
  425. }, config.testTimeout );
  426. }
  427. }
  428. };
  429. // `assert` initialized at top of scope
  430. // Assert helpers
  431. // All of these must either call QUnit.push() or manually do:
  432. // - runLoggingCallbacks( "log", .. );
  433. // - config.current.assertions.push({ .. });
  434. // We attach it to the QUnit object *after* we expose the public API,
  435. // otherwise `assert` will become a global variable in browsers (#341).
  436. assert = {
  437. /**
  438. * Asserts rough true-ish result.
  439. * @name ok
  440. * @function
  441. * @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" );
  442. */
  443. ok: function( result, msg ) {
  444. if ( !config.current ) {
  445. throw new Error( "ok() assertion outside test context, was " + sourceFromStacktrace(2) );
  446. }
  447. result = !!result;
  448. var source,
  449. details = {
  450. module: config.current.module,
  451. name: config.current.testName,
  452. result: result,
  453. message: msg
  454. };
  455. msg = escapeText( msg || (result ? "okay" : "failed" ) );
  456. msg = "<span class='test-message'>" + msg + "</span>";
  457. if ( !result ) {
  458. source = sourceFromStacktrace( 2 );
  459. if ( source ) {
  460. details.source = source;
  461. msg += "<table><tr class='test-source'><th>Source: </th><td><pre>" + escapeText( source ) + "</pre></td></tr></table>";
  462. }
  463. }
  464. runLoggingCallbacks( "log", QUnit, details );
  465. config.current.assertions.push({
  466. result: result,
  467. message: msg
  468. });
  469. },
  470. /**
  471. * Assert that the first two arguments are equal, with an optional message.
  472. * Prints out both actual and expected values.
  473. * @name equal
  474. * @function
  475. * @example equal( format( "Received {0} bytes.", 2), "Received 2 bytes.", "format() replaces {0} with next argument" );
  476. */
  477. equal: function( actual, expected, message ) {
  478. /*jshint eqeqeq:false */
  479. QUnit.push( expected == actual, actual, expected, message );
  480. },
  481. /**
  482. * @name notEqual
  483. * @function
  484. */
  485. notEqual: function( actual, expected, message ) {
  486. /*jshint eqeqeq:false */
  487. QUnit.push( expected != actual, actual, expected, message );
  488. },
  489. /**
  490. * @name propEqual
  491. * @function
  492. */
  493. propEqual: function( actual, expected, message ) {
  494. actual = objectValues(actual);
  495. expected = objectValues(expected);
  496. QUnit.push( QUnit.equiv(actual, expected), actual, expected, message );
  497. },
  498. /**
  499. * @name notPropEqual
  500. * @function
  501. */
  502. notPropEqual: function( actual, expected, message ) {
  503. actual = objectValues(actual);
  504. expected = objectValues(expected);
  505. QUnit.push( !QUnit.equiv(actual, expected), actual, expected, message );
  506. },
  507. /**
  508. * @name deepEqual
  509. * @function
  510. */
  511. deepEqual: function( actual, expected, message ) {
  512. QUnit.push( QUnit.equiv(actual, expected), actual, expected, message );
  513. },
  514. /**
  515. * @name notDeepEqual
  516. * @function
  517. */
  518. notDeepEqual: function( actual, expected, message ) {
  519. QUnit.push( !QUnit.equiv(actual, expected), actual, expected, message );
  520. },
  521. /**
  522. * @name strictEqual
  523. * @function
  524. */
  525. strictEqual: function( actual, expected, message ) {
  526. QUnit.push( expected === actual, actual, expected, message );
  527. },
  528. /**
  529. * @name notStrictEqual
  530. * @function
  531. */
  532. notStrictEqual: function( actual, expected, message ) {
  533. QUnit.push( expected !== actual, actual, expected, message );
  534. },
  535. "throws": function( block, expected, message ) {
  536. var actual,
  537. expectedOutput = expected,
  538. ok = false;
  539. // 'expected' is optional
  540. if ( typeof expected === "string" ) {
  541. message = expected;
  542. expected = null;
  543. }
  544. config.current.ignoreGlobalErrors = true;
  545. try {
  546. block.call( config.current.testEnvironment );
  547. } catch (e) {
  548. actual = e;
  549. }
  550. config.current.ignoreGlobalErrors = false;
  551. if ( actual ) {
  552. // we don't want to validate thrown error
  553. if ( !expected ) {
  554. ok = true;
  555. expectedOutput = null;
  556. // expected is a regexp
  557. } else if ( QUnit.objectType( expected ) === "regexp" ) {
  558. ok = expected.test( errorString( actual ) );
  559. // expected is a constructor
  560. } else if ( actual instanceof expected ) {
  561. ok = true;
  562. // expected is a validation function which returns true is validation passed
  563. } else if ( expected.call( {}, actual ) === true ) {
  564. expectedOutput = null;
  565. ok = true;
  566. }
  567. QUnit.push( ok, actual, expectedOutput, message );
  568. } else {
  569. QUnit.pushFailure( message, null, 'No exception was thrown.' );
  570. }
  571. }
  572. };
  573. /**
  574. * @deprecated since 1.8.0
  575. * Kept assertion helpers in root for backwards compatibility.
  576. */
  577. extend( QUnit, assert );
  578. /**
  579. * @deprecated since 1.9.0
  580. * Kept root "raises()" for backwards compatibility.
  581. * (Note that we don't introduce assert.raises).
  582. */
  583. QUnit.raises = assert[ "throws" ];
  584. /**
  585. * @deprecated since 1.0.0, replaced with error pushes since 1.3.0
  586. * Kept to avoid TypeErrors for undefined methods.
  587. */
  588. QUnit.equals = function() {
  589. QUnit.push( false, false, false, "QUnit.equals has been deprecated since 2009 (e88049a0), use QUnit.equal instead" );
  590. };
  591. QUnit.same = function() {
  592. QUnit.push( false, false, false, "QUnit.same has been deprecated since 2009 (e88049a0), use QUnit.deepEqual instead" );
  593. };
  594. // We want access to the constructor's prototype
  595. (function() {
  596. function F() {}
  597. F.prototype = QUnit;
  598. QUnit = new F();
  599. // Make F QUnit's constructor so that we can add to the prototype later
  600. QUnit.constructor = F;
  601. }());
  602. /**
  603. * Config object: Maintain internal state
  604. * Later exposed as QUnit.config
  605. * `config` initialized at top of scope
  606. */
  607. config = {
  608. // The queue of tests to run
  609. queue: [],
  610. // block until document ready
  611. blocking: true,
  612. // when enabled, show only failing tests
  613. // gets persisted through sessionStorage and can be changed in UI via checkbox
  614. hidepassed: false,
  615. // by default, run previously failed tests first
  616. // very useful in combination with "Hide passed tests" checked
  617. reorder: true,
  618. // by default, modify document.title when suite is done
  619. altertitle: true,
  620. // when enabled, all tests must call expect()
  621. requireExpects: false,
  622. // add checkboxes that are persisted in the query-string
  623. // when enabled, the id is set to `true` as a `QUnit.config` property
  624. urlConfig: [
  625. {
  626. id: "noglobals",
  627. label: "Check for Globals",
  628. tooltip: "Enabling this will test if any test introduces new properties on the `window` object. Stored as query-strings."
  629. },
  630. {
  631. id: "notrycatch",
  632. label: "No try-catch",
  633. tooltip: "Enabling this will run tests outside of a try-catch block. Makes debugging exceptions in IE reasonable. Stored as query-strings."
  634. }
  635. ],
  636. // Set of all modules.
  637. modules: {},
  638. // logging callback queues
  639. begin: [],
  640. done: [],
  641. log: [],
  642. testStart: [],
  643. testDone: [],
  644. moduleStart: [],
  645. moduleDone: []
  646. };
  647. // Export global variables, unless an 'exports' object exists,
  648. // in that case we assume we're in CommonJS (dealt with on the bottom of the script)
  649. if ( typeof exports === "undefined" ) {
  650. extend( window, QUnit );
  651. // Expose QUnit object
  652. window.QUnit = QUnit;
  653. }
  654. // Initialize more QUnit.config and QUnit.urlParams
  655. (function() {
  656. var i,
  657. location = window.location || { search: "", protocol: "file:" },
  658. params = location.search.slice( 1 ).split( "&" ),
  659. length = params.length,
  660. urlParams = {},
  661. current;
  662. if ( params[ 0 ] ) {
  663. for ( i = 0; i < length; i++ ) {
  664. current = params[ i ].split( "=" );
  665. current[ 0 ] = decodeURIComponent( current[ 0 ] );
  666. // allow just a key to turn on a flag, e.g., test.html?noglobals
  667. current[ 1 ] = current[ 1 ] ? decodeURIComponent( current[ 1 ] ) : true;
  668. urlParams[ current[ 0 ] ] = current[ 1 ];
  669. }
  670. }
  671. QUnit.urlParams = urlParams;
  672. // String search anywhere in moduleName+testName
  673. config.filter = urlParams.filter;
  674. // Exact match of the module name
  675. config.module = urlParams.module;
  676. config.testNumber = parseInt( urlParams.testNumber, 10 ) || null;
  677. // Figure out if we're running the tests from a server or not
  678. QUnit.isLocal = location.protocol === "file:";
  679. }());
  680. // Extend QUnit object,
  681. // these after set here because they should not be exposed as global functions
  682. extend( QUnit, {
  683. assert: assert,
  684. config: config,
  685. // Initialize the configuration options
  686. init: function() {
  687. extend( config, {
  688. stats: { all: 0, bad: 0 },
  689. moduleStats: { all: 0, bad: 0 },
  690. started: +new Date(),
  691. updateRate: 1000,
  692. blocking: false,
  693. autostart: true,
  694. autorun: false,
  695. filter: "",
  696. queue: [],
  697. semaphore: 1
  698. });
  699. var tests, banner, result,
  700. qunit = id( "qunit" );
  701. if ( qunit ) {
  702. qunit.innerHTML =
  703. "<h1 id='qunit-header'>" + escapeText( document.title ) + "</h1>" +
  704. "<h2 id='qunit-banner'></h2>" +
  705. "<div id='qunit-testrunner-toolbar'></div>" +
  706. "<h2 id='qunit-userAgent'></h2>" +
  707. "<ol id='qunit-tests'></ol>";
  708. }
  709. tests = id( "qunit-tests" );
  710. banner = id( "qunit-banner" );
  711. result = id( "qunit-testresult" );
  712. if ( tests ) {
  713. tests.innerHTML = "";
  714. }
  715. if ( banner ) {
  716. banner.className = "";
  717. }
  718. if ( result ) {
  719. result.parentNode.removeChild( result );
  720. }
  721. if ( tests ) {
  722. result = document.createElement( "p" );
  723. result.id = "qunit-testresult";
  724. result.className = "result";
  725. tests.parentNode.insertBefore( result, tests );
  726. result.innerHTML = "Running...<br/>&nbsp;";
  727. }
  728. },
  729. // Resets the test setup. Useful for tests that modify the DOM.
  730. reset: function() {
  731. var fixture = id( "qunit-fixture" );
  732. if ( fixture ) {
  733. fixture.innerHTML = config.fixture;
  734. }
  735. },
  736. // Trigger an event on an element.
  737. // @example triggerEvent( document.body, "click" );
  738. triggerEvent: function( elem, type, event ) {
  739. if ( document.createEvent ) {
  740. event = document.createEvent( "MouseEvents" );
  741. event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
  742. 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  743. elem.dispatchEvent( event );
  744. } else if ( elem.fireEvent ) {
  745. elem.fireEvent( "on" + type );
  746. }
  747. },
  748. // Safe object type checking
  749. is: function( type, obj ) {
  750. return QUnit.objectType( obj ) === type;
  751. },
  752. objectType: function( obj ) {
  753. if ( typeof obj === "undefined" ) {
  754. return "undefined";
  755. // consider: typeof null === object
  756. }
  757. if ( obj === null ) {
  758. return "null";
  759. }
  760. var match = toString.call( obj ).match(/^\[object\s(.*)\]$/),
  761. type = match && match[1] || "";
  762. switch ( type ) {
  763. case "Number":
  764. if ( isNaN(obj) ) {
  765. return "nan";
  766. }
  767. return "number";
  768. case "String":
  769. case "Boolean":
  770. case "Array":
  771. case "Date":
  772. case "RegExp":
  773. case "Function":
  774. return type.toLowerCase();
  775. }
  776. if ( typeof obj === "object" ) {
  777. return "object";
  778. }
  779. return undefined;
  780. },
  781. push: function( result, actual, expected, message ) {
  782. if ( !config.current ) {
  783. throw new Error( "assertion outside test context, was " + sourceFromStacktrace() );
  784. }
  785. var output, source,
  786. details = {
  787. module: config.current.module,
  788. name: config.current.testName,
  789. result: result,
  790. message: message,
  791. actual: actual,
  792. expected: expected
  793. };
  794. message = escapeText( message ) || ( result ? "okay" : "failed" );
  795. message = "<span class='test-message'>" + message + "</span>";
  796. output = message;
  797. if ( !result ) {
  798. expected = escapeText( QUnit.jsDump.parse(expected) );
  799. actual = escapeText( QUnit.jsDump.parse(actual) );
  800. output += "<table><tr class='test-expected'><th>Expected: </th><td><pre>" + expected + "</pre></td></tr>";
  801. if ( actual !== expected ) {
  802. output += "<tr class='test-actual'><th>Result: </th><td><pre>" + actual + "</pre></td></tr>";
  803. output += "<tr class='test-diff'><th>Diff: </th><td><pre>" + QUnit.diff( expected, actual ) + "</pre></td></tr>";
  804. }
  805. source = sourceFromStacktrace();
  806. if ( source ) {
  807. details.source = source;
  808. output += "<tr class='test-source'><th>Source: </th><td><pre>" + escapeText( source ) + "</pre></td></tr>";
  809. }
  810. output += "</table>";
  811. }
  812. runLoggingCallbacks( "log", QUnit, details );
  813. config.current.assertions.push({
  814. result: !!result,
  815. message: output
  816. });
  817. },
  818. pushFailure: function( message, source, actual ) {
  819. if ( !config.current ) {
  820. throw new Error( "pushFailure() assertion outside test context, was " + sourceFromStacktrace(2) );
  821. }
  822. var output,
  823. details = {
  824. module: config.current.module,
  825. name: config.current.testName,
  826. result: false,
  827. message: message
  828. };
  829. message = escapeText( message ) || "error";
  830. message = "<span class='test-message'>" + message + "</span>";
  831. output = message;
  832. output += "<table>";
  833. if ( actual ) {
  834. output += "<tr class='test-actual'><th>Result: </th><td><pre>" + escapeText( actual ) + "</pre></td></tr>";
  835. }
  836. if ( source ) {
  837. details.source = source;
  838. output += "<tr class='test-source'><th>Source: </th><td><pre>" + escapeText( source ) + "</pre></td></tr>";
  839. }
  840. output += "</table>";
  841. runLoggingCallbacks( "log", QUnit, details );
  842. config.current.assertions.push({
  843. result: false,
  844. message: output
  845. });
  846. },
  847. url: function( params ) {
  848. params = extend( extend( {}, QUnit.urlParams ), params );
  849. var key,
  850. querystring = "?";
  851. for ( key in params ) {
  852. if ( !hasOwn.call( params, key ) ) {
  853. continue;
  854. }
  855. querystring += encodeURIComponent( key ) + "=" +
  856. encodeURIComponent( params[ key ] ) + "&";
  857. }
  858. return window.location.protocol + "//" + window.location.host +
  859. window.location.pathname + querystring.slice( 0, -1 );
  860. },
  861. extend: extend,
  862. id: id,
  863. addEvent: addEvent,
  864. addClass: addClass,
  865. hasClass: hasClass,
  866. removeClass: removeClass
  867. // load, equiv, jsDump, diff: Attached later
  868. });
  869. /**
  870. * @deprecated: Created for backwards compatibility with test runner that set the hook function
  871. * into QUnit.{hook}, instead of invoking it and passing the hook function.
  872. * QUnit.constructor is set to the empty F() above so that we can add to it's prototype here.
  873. * Doing this allows us to tell if the following methods have been overwritten on the actual
  874. * QUnit object.
  875. */
  876. extend( QUnit.constructor.prototype, {
  877. // Logging callbacks; all receive a single argument with the listed properties
  878. // run test/logs.html for any related changes
  879. begin: registerLoggingCallback( "begin" ),
  880. // done: { failed, passed, total, runtime }
  881. done: registerLoggingCallback( "done" ),
  882. // log: { result, actual, expected, message }
  883. log: registerLoggingCallback( "log" ),
  884. // testStart: { name }
  885. testStart: registerLoggingCallback( "testStart" ),
  886. // testDone: { name, failed, passed, total, duration }
  887. testDone: registerLoggingCallback( "testDone" ),
  888. // moduleStart: { name }
  889. moduleStart: registerLoggingCallback( "moduleStart" ),
  890. // moduleDone: { name, failed, passed, total }
  891. moduleDone: registerLoggingCallback( "moduleDone" )
  892. });
  893. if ( typeof document === "undefined" || document.readyState === "complete" ) {
  894. config.autorun = true;
  895. }
  896. QUnit.load = function() {
  897. runLoggingCallbacks( "begin", QUnit, {} );
  898. // Initialize the config, saving the execution queue
  899. var banner, filter, i, label, len, main, ol, toolbar, userAgent, val,
  900. urlConfigCheckboxesContainer, urlConfigCheckboxes, moduleFilter,
  901. numModules = 0,
  902. moduleNames = [],
  903. moduleFilterHtml = "",
  904. urlConfigHtml = "",
  905. oldconfig = extend( {}, config );
  906. QUnit.init();
  907. extend(config, oldconfig);
  908. config.blocking = false;
  909. len = config.urlConfig.length;
  910. for ( i = 0; i < len; i++ ) {
  911. val = config.urlConfig[i];
  912. if ( typeof val === "string" ) {
  913. val = {
  914. id: val,
  915. label: val,
  916. tooltip: "[no tooltip available]"
  917. };
  918. }
  919. config[ val.id ] = QUnit.urlParams[ val.id ];
  920. urlConfigHtml += "<input id='qunit-urlconfig-" + escapeText( val.id ) +
  921. "' name='" + escapeText( val.id ) +
  922. "' type='checkbox'" + ( config[ val.id ] ? " checked='checked'" : "" ) +
  923. " title='" + escapeText( val.tooltip ) +
  924. "'><label for='qunit-urlconfig-" + escapeText( val.id ) +
  925. "' title='" + escapeText( val.tooltip ) + "'>" + val.label + "</label>";
  926. }
  927. for ( i in config.modules ) {
  928. if ( config.modules.hasOwnProperty( i ) ) {
  929. moduleNames.push(i);
  930. }
  931. }
  932. numModules = moduleNames.length;
  933. moduleNames.sort( function( a, b ) {
  934. return a.localeCompare( b );
  935. });
  936. moduleFilterHtml += "<label for='qunit-modulefilter'>Module: </label><select id='qunit-modulefilter' name='modulefilter'><option value='' " +
  937. ( config.module === undefined ? "selected='selected'" : "" ) +
  938. ">< All Modules ></option>";
  939. for ( i = 0; i < numModules; i++) {
  940. moduleFilterHtml += "<option value='" + escapeText( encodeURIComponent(moduleNames[i]) ) + "' " +
  941. ( config.module === moduleNames[i] ? "selected='selected'" : "" ) +
  942. ">" + escapeText(moduleNames[i]) + "</option>";
  943. }
  944. moduleFilterHtml += "</select>";
  945. // `userAgent` initialized at top of scope
  946. userAgent = id( "qunit-userAgent" );
  947. if ( userAgent ) {
  948. userAgent.innerHTML = navigator.userAgent;
  949. }
  950. // `banner` initialized at top of scope
  951. banner = id( "qunit-header" );
  952. if ( banner ) {
  953. banner.innerHTML = "<a href='" + QUnit.url({ filter: undefined, module: undefined, testNumber: undefined }) + "'>" + banner.innerHTML + "</a> ";
  954. }
  955. // `toolbar` initialized at top of scope
  956. toolbar = id( "qunit-testrunner-toolbar" );
  957. if ( toolbar ) {
  958. // `filter` initialized at top of scope
  959. filter = document.createElement( "input" );
  960. filter.type = "checkbox";
  961. filter.id = "qunit-filter-pass";
  962. addEvent( filter, "click", function() {
  963. var tmp,
  964. ol = document.getElementById( "qunit-tests" );
  965. if ( filter.checked ) {
  966. ol.className = ol.className + " hidepass";
  967. } else {
  968. tmp = " " + ol.className.replace( /[\n\t\r]/g, " " ) + " ";
  969. ol.className = tmp.replace( / hidepass /, " " );
  970. }
  971. if ( defined.sessionStorage ) {
  972. if (filter.checked) {
  973. sessionStorage.setItem( "qunit-filter-passed-tests", "true" );
  974. } else {
  975. sessionStorage.removeItem( "qunit-filter-passed-tests" );
  976. }
  977. }
  978. });
  979. if ( config.hidepassed || defined.sessionStorage && sessionStorage.getItem( "qunit-filter-passed-tests" ) ) {
  980. filter.checked = true;
  981. // `ol` initialized at top of scope
  982. ol = document.getElementById( "qunit-tests" );
  983. ol.className = ol.className + " hidepass";
  984. }
  985. toolbar.appendChild( filter );
  986. // `label` initialized at top of scope
  987. label = document.createElement( "label" );
  988. label.setAttribute( "for", "qunit-filter-pass" );
  989. label.setAttribute( "title", "Only show tests and assertions that fail. Stored in sessionStorage." );
  990. label.innerHTML = "Hide passed tests";
  991. toolbar.appendChild( label );
  992. urlConfigCheckboxesContainer = document.createElement("span");
  993. urlConfigCheckboxesContainer.innerHTML = urlConfigHtml;
  994. urlConfigCheckboxes = urlConfigCheckboxesContainer.getElementsByTagName("input");
  995. // For oldIE support:
  996. // * Add handlers to the individual elements instead of the container
  997. // * Use "click" instead of "change"
  998. // * Fallback from event.target to event.srcElement
  999. addEvents( urlConfigCheckboxes, "click", function( event ) {
  1000. var params = {},
  1001. target = event.target || event.srcElement;
  1002. params[ target.name ] = target.checked ? true : undefined;
  1003. window.location = QUnit.url( params );
  1004. });
  1005. toolbar.appendChild( urlConfigCheckboxesContainer );
  1006. if (numModules > 1) {
  1007. moduleFilter = document.createElement( 'span' );
  1008. moduleFilter.setAttribute( 'id', 'qunit-modulefilter-container' );
  1009. moduleFilter.innerHTML = moduleFilterHtml;
  1010. addEvent( moduleFilter.lastChild, "change", function() {
  1011. var selectBox = moduleFilter.getElementsByTagName("select")[0],
  1012. selectedModule = decodeURIComponent(selectBox.options[selectBox.selectedIndex].value);
  1013. window.location = QUnit.url( { module: ( selectedModule === "" ) ? undefined : selectedModule } );
  1014. });
  1015. toolbar.appendChild(moduleFilter);
  1016. }
  1017. }
  1018. // `main` initialized at top of scope
  1019. main = id( "qunit-fixture" );
  1020. if ( main ) {
  1021. config.fixture = main.innerHTML;
  1022. }
  1023. if ( config.autostart ) {
  1024. QUnit.start();
  1025. }
  1026. };
  1027. addEvent( window, "load", QUnit.load );
  1028. // `onErrorFnPrev` initialized at top of scope
  1029. // Preserve other handlers
  1030. onErrorFnPrev = window.onerror;
  1031. // Cover uncaught exceptions
  1032. // Returning true will suppress the default browser handler,
  1033. // returning false will let it run.
  1034. window.onerror = function ( error, filePath, linerNr ) {
  1035. var ret = false;
  1036. if ( onErrorFnPrev ) {
  1037. ret = onErrorFnPrev( error, filePath, linerNr );
  1038. }
  1039. // Treat return value as window.onerror itself does,
  1040. // Only do our handling if not suppressed.
  1041. if ( ret !== true ) {
  1042. if ( QUnit.config.current ) {
  1043. if ( QUnit.config.current.ignoreGlobalErrors ) {
  1044. return true;
  1045. }
  1046. QUnit.pushFailure( error, filePath + ":" + linerNr );
  1047. } else {
  1048. QUnit.test( "global failure", extend( function() {
  1049. QUnit.pushFailure( error, filePath + ":" + linerNr );
  1050. }, { validTest: validTest } ) );
  1051. }
  1052. return false;
  1053. }
  1054. return ret;
  1055. };
  1056. function done() {
  1057. config.autorun = true;
  1058. // Log the last module results
  1059. if ( config.currentModule ) {
  1060. runLoggingCallbacks( "moduleDone", QUnit, {
  1061. name: config.currentModule,
  1062. failed: config.moduleStats.bad,
  1063. passed: config.moduleStats.all - config.moduleStats.bad,
  1064. total: config.moduleStats.all
  1065. });
  1066. }
  1067. var i, key,
  1068. banner = id( "qunit-banner" ),
  1069. tests = id( "qunit-tests" ),
  1070. runtime = +new Date() - config.started,
  1071. passed = config.stats.all - config.stats.bad,
  1072. html = [
  1073. "Tests completed in ",
  1074. runtime,
  1075. " milliseconds.<br/>",
  1076. "<span class='passed'>",
  1077. passed,
  1078. "</span> assertions of <span class='total'>",
  1079. config.stats.all,
  1080. "</span> passed, <span class='failed'>",
  1081. config.stats.bad,
  1082. "</span> failed."
  1083. ].join( "" );
  1084. if ( banner ) {
  1085. banner.className = ( config.stats.bad ? "qunit-fail" : "qunit-pass" );
  1086. }
  1087. if ( tests ) {
  1088. id( "qunit-testresult" ).innerHTML = html;
  1089. }
  1090. if ( config.altertitle && typeof document !== "undefined" && document.title ) {
  1091. // show ✖ for good, ✔ for bad suite result in title
  1092. // use escape sequences in case file gets loaded with non-utf-8-charset
  1093. document.title = [
  1094. ( config.stats.bad ? "\u2716" : "\u2714" ),
  1095. document.title.replace( /^[\u2714\u2716] /i, "" )
  1096. ].join( " " );
  1097. }
  1098. // clear own sessionStorage items if all tests passed
  1099. if ( config.reorder && defined.sessionStorage && config.stats.bad === 0 ) {
  1100. // `key` & `i` initialized at top of scope
  1101. for ( i = 0; i < sessionStorage.length; i++ ) {
  1102. key = sessionStorage.key( i++ );
  1103. if ( key.indexOf( "qunit-test-" ) === 0 ) {
  1104. sessionStorage.removeItem( key );
  1105. }
  1106. }
  1107. }
  1108. // scroll back to top to show results
  1109. if ( window.scrollTo ) {
  1110. window.scrollTo(0, 0);
  1111. }
  1112. runLoggingCallbacks( "done", QUnit, {
  1113. failed: config.stats.bad,
  1114. passed: passed,
  1115. total: config.stats.all,
  1116. runtime: runtime
  1117. });
  1118. }
  1119. /** @return Boolean: true if this test should be ran */
  1120. function validTest( test ) {
  1121. var include,
  1122. filter = config.filter && config.filter.toLowerCase(),
  1123. module = config.module && config.module.toLowerCase(),
  1124. fullName = (test.module + ": " + test.testName).toLowerCase();
  1125. // Internally-generated tests are always valid
  1126. if ( test.callback && test.callback.validTest === validTest ) {
  1127. delete test.callback.validTest;
  1128. return true;
  1129. }
  1130. if ( config.testNumber ) {
  1131. return test.testNumber === config.testNumber;
  1132. }
  1133. if ( module && ( !test.module || test.module.toLowerCase() !== module ) ) {
  1134. return false;
  1135. }
  1136. if ( !filter ) {
  1137. return true;
  1138. }
  1139. include = filter.charAt( 0 ) !== "!";
  1140. if ( !include ) {
  1141. filter = filter.slice( 1 );
  1142. }
  1143. // If the filter matches, we need to honour include
  1144. if ( fullName.indexOf( filter ) !== -1 ) {
  1145. return include;
  1146. }
  1147. // Otherwise, do the opposite
  1148. return !include;
  1149. }
  1150. // so far supports only Firefox, Chrome and Opera (buggy), Safari (for real exceptions)
  1151. // Later Safari and IE10 are supposed to support error.stack as well
  1152. // See also https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error/Stack
  1153. function extractStacktrace( e, offset ) {
  1154. offset = offset === undefined ? 3 : offset;
  1155. var stack, include, i;
  1156. if ( e.stacktrace ) {
  1157. // Opera
  1158. return e.stacktrace.split( "\n" )[ offset + 3 ];
  1159. } else if ( e.stack ) {
  1160. // Firefox, Chrome
  1161. stack = e.stack.split( "\n" );
  1162. if (/^error$/i.test( stack[0] ) ) {
  1163. stack.shift();
  1164. }
  1165. if ( fileName ) {
  1166. include = [];
  1167. for ( i = offset; i < stack.length; i++ ) {
  1168. if ( stack[ i ].indexOf( fileName ) !== -1 ) {
  1169. break;
  1170. }
  1171. include.push( stack[ i ] );
  1172. }
  1173. if ( include.length ) {
  1174. return include.join( "\n" );
  1175. }
  1176. }
  1177. return stack[ offset ];
  1178. } else if ( e.sourceURL ) {
  1179. // Safari, PhantomJS
  1180. // hopefully one day Safari provides actual stacktraces
  1181. // exclude useless self-reference for generated Error objects
  1182. if ( /qunit.js$/.test( e.sourceURL ) ) {
  1183. return;
  1184. }
  1185. // for actual exceptions, this is useful
  1186. return e.sourceURL + ":" + e.line;
  1187. }
  1188. }
  1189. function sourceFromStacktrace( offset ) {
  1190. try {
  1191. throw new Error();
  1192. } catch ( e ) {
  1193. return extractStacktrace( e, offset );
  1194. }
  1195. }
  1196. /**
  1197. * Escape text for attribute or text content.
  1198. */
  1199. function escapeText( s ) {
  1200. if ( !s ) {
  1201. return "";
  1202. }
  1203. s = s + "";
  1204. // Both single quotes and double quotes (for attributes)
  1205. return s.replace( /['"<>&]/g, function( s ) {
  1206. switch( s ) {
  1207. case '\'':
  1208. return '&#039;';
  1209. case '"':
  1210. return '&quot;';
  1211. case '<':
  1212. return '&lt;';
  1213. case '>':
  1214. return '&gt;';
  1215. case '&':
  1216. return '&amp;';
  1217. }
  1218. });
  1219. }
  1220. function synchronize( callback, last ) {
  1221. config.queue.push( callback );
  1222. if ( config.autorun && !config.blocking ) {
  1223. process( last );
  1224. }
  1225. }
  1226. function process( last ) {
  1227. function next() {
  1228. process( last );
  1229. }
  1230. var start = new Date().getTime();
  1231. config.depth = config.depth ? config.depth + 1 : 1;
  1232. while ( config.queue.length && !config.blocking ) {
  1233. if ( !defined.setTimeout || config.updateRate <= 0 || ( ( new Date().getTime() - start ) < config.updateRate ) ) {
  1234. config.queue.shift()();
  1235. } else {
  1236. window.setTimeout( next, 13 );
  1237. break;
  1238. }
  1239. }
  1240. config.depth--;
  1241. if ( last && !config.blocking && !config.queue.length && config.depth === 0 ) {
  1242. done();
  1243. }
  1244. }
  1245. function saveGlobal() {
  1246. config.pollution = [];
  1247. if ( config.noglobals ) {
  1248. for ( var key in window ) {
  1249. // in Opera sometimes DOM element ids show up here, ignore them
  1250. if ( !hasOwn.call( window, key ) || /^qunit-test-output/.test( key ) ) {
  1251. continue;
  1252. }
  1253. config.pollution.push( key );
  1254. }
  1255. }
  1256. }
  1257. function checkPollution() {
  1258. var newGlobals,
  1259. deletedGlobals,
  1260. old = config.pollution;
  1261. saveGlobal();
  1262. newGlobals = diff( config.pollution, old );
  1263. if ( newGlobals.length > 0 ) {
  1264. QUnit.pushFailure( "Introduced global variable(s): " + newGlobals.join(", ") );
  1265. }
  1266. deletedGlobals = diff( old, config.pollution );
  1267. if ( deletedGlobals.length > 0 ) {
  1268. QUnit.pushFailure( "Deleted global variable(s): " + deletedGlobals.join(", ") );
  1269. }
  1270. }
  1271. // returns a new Array with the elements that are in a but not in b
  1272. function diff( a, b ) {
  1273. var i, j,
  1274. result = a.slice();
  1275. for ( i = 0; i < result.length; i++ ) {
  1276. for ( j = 0; j < b.length; j++ ) {
  1277. if ( result[i] === b[j] ) {
  1278. result.splice( i, 1 );
  1279. i--;
  1280. break;
  1281. }
  1282. }
  1283. }
  1284. return result;
  1285. }
  1286. function extend( a, b ) {
  1287. for ( var prop in b ) {
  1288. if ( b[ prop ] === undefined ) {
  1289. delete a[ prop ];
  1290. // Avoid "Member not found" error in IE8 caused by setting window.constructor
  1291. } else if ( prop !== "constructor" || a !== window ) {
  1292. a[ prop ] = b[ prop ];
  1293. }
  1294. }
  1295. return a;
  1296. }
  1297. /**
  1298. * @param {HTMLElement} elem
  1299. * @param {string} type
  1300. * @param {Function} fn
  1301. */
  1302. function addEvent( elem, type, fn ) {
  1303. // Standards-based browsers
  1304. if ( elem.addEventListener ) {
  1305. elem.addEventListener( type, fn, false );
  1306. // IE
  1307. } else {
  1308. elem.attachEvent( "on" + type, fn );
  1309. }
  1310. }
  1311. /**
  1312. * @param {Array|NodeList} elems
  1313. * @param {string} type
  1314. * @param {Function} fn
  1315. */
  1316. function addEvents( elems, type, fn ) {
  1317. var i = elems.length;
  1318. while ( i-- ) {
  1319. addEvent( elems[i], type, fn );
  1320. }
  1321. }
  1322. function hasClass( elem, name ) {
  1323. return (" " + elem.className + " ").indexOf(" " + name + " ") > -1;
  1324. }
  1325. function addClass( elem, name ) {
  1326. if ( !hasClass( elem, name ) ) {
  1327. elem.className += (elem.className ? " " : "") + name;
  1328. }
  1329. }
  1330. function removeClass( elem, name ) {
  1331. var set = " " + elem.className + " ";
  1332. // Class name may appear multiple times
  1333. while ( set.indexOf(" " + name + " ") > -1 ) {
  1334. set = set.replace(" " + name + " " , " ");
  1335. }
  1336. // If possible, trim it for prettiness, but not necessarily
  1337. elem.className = window.jQuery ? jQuery.trim( set ) : ( set.trim ? set.trim() : set );
  1338. }
  1339. function id( name ) {
  1340. return !!( typeof document !== "undefined" && document && document.getElementById ) &&
  1341. document.getElementById( name );
  1342. }
  1343. function registerLoggingCallback( key ) {
  1344. return function( callback ) {
  1345. config[key].push( callback );
  1346. };
  1347. }
  1348. // Supports deprecated method of completely overwriting logging callbacks
  1349. function runLoggingCallbacks( key, scope, args ) {
  1350. var i, callbacks;
  1351. if ( QUnit.hasOwnProperty( key ) ) {
  1352. QUnit[ key ].call(scope, args );
  1353. } else {
  1354. callbacks = config[ key ];
  1355. for ( i = 0; i < callbacks.length; i++ ) {
  1356. callbacks[ i ].call( scope, args );
  1357. }
  1358. }
  1359. }
  1360. // Test for equality any JavaScript type.
  1361. // Author: Philippe Rathé <prathe@gmail.com>
  1362. QUnit.equiv = (function() {
  1363. // Call the o related callback with the given arguments.
  1364. function bindCallbacks( o, callbacks, args ) {
  1365. var prop = QUnit.objectType( o );
  1366. if ( prop ) {
  1367. if ( QUnit.objectType( callbacks[ prop ] ) === "function" ) {
  1368. return callbacks[ prop ].apply( callbacks, args );
  1369. } else {
  1370. return callbacks[ prop ]; // or undefined
  1371. }
  1372. }
  1373. }
  1374. // the real equiv function
  1375. var innerEquiv,
  1376. // stack to decide between skip/abort functions
  1377. callers = [],
  1378. // stack to avoiding loops from circular referencing
  1379. parents = [],
  1380. parentsB = [],
  1381. getProto = Object.getPrototypeOf || function ( obj ) {
  1382. return obj.__proto__;
  1383. },
  1384. callbacks = (function () {
  1385. // for string, boolean, number and null
  1386. function useStrictEquality( b, a ) {
  1387. /*jshint eqeqeq:false */
  1388. if ( b instanceof a.constructor || a instanceof b.constructor ) {
  1389. // to catch short annotation VS 'new' annotation of a
  1390. // declaration
  1391. // e.g. var i = 1;
  1392. // var j = new Number(1);
  1393. return a == b;
  1394. } else {
  1395. return a === b;
  1396. }
  1397. }
  1398. return {
  1399. "string": useStrictEquality,
  1400. "boolean": useStrictEquality,
  1401. "number": useStrictEquality,
  1402. "null": useStrictEquality,
  1403. "undefined": useStrictEquality,
  1404. "nan": function( b ) {
  1405. return isNaN( b );
  1406. },
  1407. "date": function( b, a ) {
  1408. return QUnit.objectType( b ) === "date" && a.valueOf() === b.valueOf();
  1409. },
  1410. "regexp": function( b, a ) {
  1411. return QUnit.objectType( b ) === "regexp" &&
  1412. // the regex itself
  1413. a.source === b.source &&
  1414. // and its modifiers
  1415. a.global === b.global &&
  1416. // (gmi) ...
  1417. a.ignoreCase === b.ignoreCase &&
  1418. a.multiline === b.multiline &&
  1419. a.sticky === b.sticky;
  1420. },
  1421. // - skip when the property is a method of an instance (OOP)
  1422. // - abort otherwise,
  1423. // initial === would have catch identical references anyway
  1424. "function": function() {
  1425. var caller = callers[callers.length - 1];
  1426. return caller !== Object && typeof caller !== "undefined";
  1427. },
  1428. "array": function( b, a ) {
  1429. var i, j, len, loop, aCircular, bCircular;
  1430. // b could be an object literal here
  1431. if ( QUnit.objectType( b ) !== "array" ) {
  1432. return false;
  1433. }
  1434. len = a.length;
  1435. if ( len !== b.length ) {
  1436. // safe and faster
  1437. return false;
  1438. }
  1439. // track reference to avoid circular references
  1440. parents.push( a );
  1441. parentsB.push( b );
  1442. for ( i = 0; i < len; i++ ) {
  1443. loop = false;
  1444. for ( j = 0; j < parents.length; j++ ) {
  1445. aCircular = parents[j] === a[i];
  1446. bCircular = parentsB[j] === b[i];
  1447. if ( aCircular || bCircular ) {
  1448. if ( a[i] === b[i] || aCircular && bCircular ) {
  1449. loop = true;
  1450. } else {
  1451. parents.pop();
  1452. parentsB.pop();
  1453. return false;
  1454. }
  1455. }
  1456. }
  1457. if ( !loop && !innerEquiv(a[i], b[i]) ) {
  1458. parents.pop();
  1459. parentsB.pop();
  1460. return false;
  1461. }
  1462. }
  1463. parents.pop();
  1464. parentsB.pop();
  1465. return true;
  1466. },
  1467. "object": function( b, a ) {
  1468. var i, j, loop, aCircular, bCircular,
  1469. // Default to true
  1470. eq = true,
  1471. aProperties = [],
  1472. bProperties = [];
  1473. // comparing constructors is more strict than using
  1474. // instanceof
  1475. if ( a.constructor !== b.constructor ) {
  1476. // Allow objects with no prototype to be equivalent to
  1477. // objects with Object as their constructor.
  1478. if ( !(( getProto(a) === null && getProto(b) === Object.prototype ) ||
  1479. ( getProto(b) === null && getProto(a) === Object.prototype ) ) ) {
  1480. return false;
  1481. }
  1482. }
  1483. // stack constructor before traversing properties
  1484. callers.push( a.constructor );
  1485. // track reference to avoid circular references
  1486. parents.push( a );
  1487. parentsB.push( b );
  1488. // be strict: don't ensures hasOwnProperty and go deep
  1489. for ( i in a ) {
  1490. loop = false;
  1491. for ( j = 0; j < parents.length; j++ ) {
  1492. aCircular = parents[j] === a[i];
  1493. bCircular = parentsB[j] === b[i];
  1494. if ( aCircular || bCircular ) {
  1495. if ( a[i] === b[i] || aCircular && bCircular ) {
  1496. loop = true;
  1497. } else {
  1498. eq = false;
  1499. break;
  1500. }
  1501. }
  1502. }
  1503. aProperties.push(i);
  1504. if ( !loop && !innerEquiv(a[i], b[i]) ) {
  1505. eq = false;
  1506. break;
  1507. }
  1508. }
  1509. parents.pop();
  1510. parentsB.pop();
  1511. callers.pop(); // unstack, we are done
  1512. for ( i in b ) {
  1513. bProperties.push( i ); // collect b's properties
  1514. }
  1515. // Ensures identical properties name
  1516. return eq && innerEquiv( aProperties.sort(), bProperties.sort() );
  1517. }
  1518. };
  1519. }());
  1520. innerEquiv = function() { // can take multiple arguments
  1521. var args = [].slice.apply( arguments );
  1522. if ( args.length < 2 ) {
  1523. return true; // end transition
  1524. }
  1525. return (function( a, b ) {
  1526. if ( a === b ) {
  1527. return true; // catch the most you can
  1528. } else if ( a === null || b === null || typeof a === "undefined" ||
  1529. typeof b === "undefined" ||
  1530. QUnit.objectType(a) !== QUnit.objectType(b) ) {
  1531. return false; // don't lose time with error prone cases
  1532. } else {
  1533. return bindCallbacks(a, callbacks, [ b, a ]);
  1534. }
  1535. // apply transition with (1..n) arguments
  1536. }( args[0], args[1] ) && arguments.callee.apply( this, args.splice(1, args.length - 1 )) );
  1537. };
  1538. return innerEquiv;
  1539. }());
  1540. /**
  1541. * jsDump Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com |
  1542. * http://flesler.blogspot.com Licensed under BSD
  1543. * (http://www.opensource.org/licenses/bsd-license.php) Date: 5/15/2008
  1544. *
  1545. * @projectDescription Advanced and extensible data dumping for Javascript.
  1546. * @version 1.0.0
  1547. * @author Ariel Flesler
  1548. * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
  1549. */
  1550. QUnit.jsDump = (function() {
  1551. function quote( str ) {
  1552. return '"' + str.toString().replace( /"/g, '\\"' ) + '"';
  1553. }
  1554. function literal( o ) {
  1555. return o + "";
  1556. }
  1557. function join( pre, arr, post ) {
  1558. var s = jsDump.separator(),
  1559. base = jsDump.indent(),
  1560. inner = jsDump.indent(1);
  1561. if ( arr.join ) {
  1562. arr = arr.join( "," + s + inner );
  1563. }
  1564. if ( !arr ) {
  1565. return pre + post;
  1566. }
  1567. return [ pre, inner + arr, base + post ].join(s);
  1568. }
  1569. function array( arr, stack ) {
  1570. var i = arr.length, ret = new Array(i);
  1571. this.up();
  1572. while ( i-- ) {
  1573. ret[i] = this.parse( arr[i] , undefined , stack);
  1574. }
  1575. this.down();
  1576. return join( "[", ret, "]" );
  1577. }
  1578. var reName = /^function (\w+)/,
  1579. jsDump = {
  1580. // type is used mostly internally, you can fix a (custom)type in advance
  1581. parse: function( obj, type, stack ) {
  1582. stack = stack || [ ];
  1583. var inStack, res,
  1584. parser = this.parsers[ type || this.typeOf(obj) ];
  1585. type = typeof parser;
  1586. inStack = inArray( obj, stack );
  1587. if ( inStack !== -1 ) {
  1588. return "recursion(" + (inStack - stack.length) + ")";
  1589. }
  1590. if ( type === "function" ) {
  1591. stack.push( obj );
  1592. res = parser.call( this, obj, stack );
  1593. stack.pop();
  1594. return res;
  1595. }
  1596. return ( type === "string" ) ? parser : this.parsers.error;
  1597. },
  1598. typeOf: function( obj ) {
  1599. var type;
  1600. if ( obj === null ) {
  1601. type = "null";
  1602. } else if ( typeof obj === "undefined" ) {
  1603. type = "undefined";
  1604. } else if ( QUnit.is( "regexp", obj) ) {
  1605. type = "regexp";
  1606. } else if ( QUnit.is( "date", obj) ) {
  1607. type = "date";
  1608. } else if ( QUnit.is( "function", obj) ) {
  1609. type = "function";
  1610. } else if ( typeof obj.setInterval !== undefined && typeof obj.document !== "undefined" && typeof obj.nodeType === "undefined" ) {
  1611. type = "window";
  1612. } else if ( obj.nodeType === 9 ) {
  1613. type = "document";
  1614. } else if ( obj.nodeType ) {
  1615. type = "node";
  1616. } else if (
  1617. // native arrays
  1618. toString.call( obj ) === "[object Array]" ||
  1619. // NodeList objects
  1620. ( typeof obj.length === "number" && typeof obj.item !== "undefined" && ( obj.length ? obj.item(0) === obj[0] : ( obj.item( 0 ) === null && typeof obj[0] === "undefined" ) ) )
  1621. ) {
  1622. type = "array";
  1623. } else if ( obj.constructor === Error.prototype.constructor ) {
  1624. type = "error";
  1625. } else {
  1626. type = typeof obj;
  1627. }
  1628. return type;
  1629. },
  1630. separator: function() {
  1631. return this.multiline ? this.HTML ? "<br />" : "\n" : this.HTML ? "&nbsp;" : " ";
  1632. },
  1633. // extra can be a number, shortcut for increasing-calling-decreasing
  1634. indent: function( extra ) {
  1635. if ( !this.multiline ) {
  1636. return "";
  1637. }
  1638. var chr = this.indentChar;
  1639. if ( this.HTML ) {
  1640. chr = chr.replace( /\t/g, " " ).replace( / /g, "&nbsp;" );
  1641. }
  1642. return new Array( this._depth_ + (extra||0) ).join(chr);
  1643. },
  1644. up: function( a ) {
  1645. this._depth_ += a || 1;
  1646. },
  1647. down: function( a ) {
  1648. this._depth_ -= a || 1;
  1649. },
  1650. setParser: function( name, parser ) {
  1651. this.parsers[name] = parser;
  1652. },
  1653. // The next 3 are exposed so you can use them
  1654. quote: quote,
  1655. literal: literal,
  1656. join: join,
  1657. //
  1658. _depth_: 1,
  1659. // This is the list of parsers, to modify them, use jsDump.setParser
  1660. parsers: {
  1661. window: "[Window]",
  1662. document: "[Document]",
  1663. error: function(error) {
  1664. return "Error(\"" + error.message + "\")";
  1665. },
  1666. unknown: "[Unknown]",
  1667. "null": "null",
  1668. "undefined": "undefined",
  1669. "function": function( fn ) {
  1670. var ret = "function",
  1671. // functions never have name in IE
  1672. name = "name" in fn ? fn.name : (reName.exec(fn) || [])[1];
  1673. if ( name ) {
  1674. ret += " " + name;
  1675. }
  1676. ret += "( ";
  1677. ret = [ ret, QUnit.jsDump.parse( fn, "functionArgs" ), "){" ].join( "" );
  1678. return join( ret, QUnit.jsDump.parse(fn,"functionCode" ), "}" );
  1679. },
  1680. array: array,
  1681. nodelist: array,
  1682. "arguments": array,
  1683. object: function( map, stack ) {
  1684. var ret = [ ], keys, key, val, i;
  1685. QUnit.jsDump.up();
  1686. keys = [];
  1687. for ( key in map ) {
  1688. keys.push( key );
  1689. }
  1690. keys.sort();
  1691. for ( i = 0; i < keys.length; i++ ) {
  1692. key = keys[ i ];
  1693. val = map[ key ];
  1694. ret.push( QUnit.jsDump.parse( key, "key" ) + ": " + QUnit.jsDump.parse( val, undefined, stack ) );
  1695. }
  1696. QUnit.jsDump.down();
  1697. return join( "{", ret, "}" );
  1698. },
  1699. node: function( node ) {
  1700. var len, i, val,
  1701. open = QUnit.jsDump.HTML ? "&lt;" : "<",
  1702. close = QUnit.jsDump.HTML ? "&gt;" : ">",
  1703. tag = node.nodeName.toLowerCase(),
  1704. ret = open + tag,
  1705. attrs = node.attributes;
  1706. if ( attrs ) {
  1707. for ( i = 0, len = attrs.length; i < len; i++ ) {
  1708. val = attrs[i].nodeValue;
  1709. // IE6 includes all attributes in .attributes, even ones not explicitly set.
  1710. // Those have values like undefined, null, 0, false, "" or "inherit".
  1711. if ( val && val !== "inherit" ) {
  1712. ret += " " + attrs[i].nodeName + "=" + QUnit.jsDump.parse( val, "attribute" );
  1713. }
  1714. }
  1715. }
  1716. ret += close;
  1717. // Show content of TextNode or CDATASection
  1718. if ( node.nodeType === 3 || node.nodeType === 4 ) {
  1719. ret += node.nodeValue;
  1720. }
  1721. return ret + open + "/" + tag + close;
  1722. },
  1723. // function calls it internally, it's the arguments part of the function
  1724. functionArgs: function( fn ) {
  1725. var args,
  1726. l = fn.length;
  1727. if ( !l ) {
  1728. return "";
  1729. }
  1730. args = new Array(l);
  1731. while ( l-- ) {
  1732. // 97 is 'a'
  1733. args[l] = String.fromCharCode(97+l);
  1734. }
  1735. return " " + args.join( ", " ) + " ";
  1736. },
  1737. // object calls it internally, the key part of an item in a map
  1738. key: quote,
  1739. // function calls it internally, it's the content of the function
  1740. functionCode: "[code]",
  1741. // node calls it internally, it's an html attribute value
  1742. attribute: quote,
  1743. string: quote,
  1744. date: quote,
  1745. regexp: literal,
  1746. number: literal,
  1747. "boolean": literal
  1748. },
  1749. // if true, entities are escaped ( <, >, \t, space and \n )
  1750. HTML: false,
  1751. // indentation unit
  1752. indentChar: " ",
  1753. // if true, items in a collection, are separated by a \n, else just a space.
  1754. multiline: true
  1755. };
  1756. return jsDump;
  1757. }());
  1758. // from jquery.js
  1759. function inArray( elem, array ) {
  1760. if ( array.indexOf ) {
  1761. return array.indexOf( elem );
  1762. }
  1763. for ( var i = 0, length = array.length; i < length; i++ ) {
  1764. if ( array[ i ] === elem ) {
  1765. return i;
  1766. }
  1767. }
  1768. return -1;
  1769. }
  1770. /*
  1771. * Javascript Diff Algorithm
  1772. * By John Resig (http://ejohn.org/)
  1773. * Modified by Chu Alan "sprite"
  1774. *
  1775. * Released under the MIT license.
  1776. *
  1777. * More Info:
  1778. * http://ejohn.org/projects/javascript-diff-algorithm/
  1779. *
  1780. * Usage: QUnit.diff(expected, actual)
  1781. *
  1782. * 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"
  1783. */
  1784. QUnit.diff = (function() {
  1785. /*jshint eqeqeq:false, eqnull:true */
  1786. function diff( o, n ) {
  1787. var i,
  1788. ns = {},
  1789. os = {};
  1790. for ( i = 0; i < n.length; i++ ) {
  1791. if ( !hasOwn.call( ns, n[i] ) ) {
  1792. ns[ n[i] ] = {
  1793. rows: [],
  1794. o: null
  1795. };
  1796. }
  1797. ns[ n[i] ].rows.push( i );
  1798. }
  1799. for ( i = 0; i < o.length; i++ ) {
  1800. if ( !hasOwn.call( os, o[i] ) ) {
  1801. os[ o[i] ] = {
  1802. rows: [],
  1803. n: null
  1804. };
  1805. }
  1806. os[ o[i] ].rows.push( i );
  1807. }
  1808. for ( i in ns ) {
  1809. if ( !hasOwn.call( ns, i ) ) {
  1810. continue;
  1811. }
  1812. if ( ns[i].rows.length === 1 && hasOwn.call( os, i ) && os[i].rows.length === 1 ) {
  1813. n[ ns[i].rows[0] ] = {
  1814. text: n[ ns[i].rows[0] ],
  1815. row: os[i].rows[0]
  1816. };
  1817. o[ os[i].rows[0] ] = {
  1818. text: o[ os[i].rows[0] ],
  1819. row: ns[i].rows[0]
  1820. };
  1821. }
  1822. }
  1823. for ( i = 0; i < n.length - 1; i++ ) {
  1824. if ( n[i].text != null && n[ i + 1 ].text == null && n[i].row + 1 < o.length && o[ n[i].row + 1 ].text == null &&
  1825. n[ i + 1 ] == o[ n[i].row + 1 ] ) {
  1826. n[ i + 1 ] = {
  1827. text: n[ i + 1 ],
  1828. row: n[i].row + 1
  1829. };
  1830. o[ n[i].row + 1 ] = {
  1831. text: o[ n[i].row + 1 ],
  1832. row: i + 1
  1833. };
  1834. }
  1835. }
  1836. for ( i = n.length - 1; i > 0; i-- ) {
  1837. if ( n[i].text != null && n[ i - 1 ].text == null && n[i].row > 0 && o[ n[i].row - 1 ].text == null &&
  1838. n[ i - 1 ] == o[ n[i].row - 1 ]) {
  1839. n[ i - 1 ] = {
  1840. text: n[ i - 1 ],
  1841. row: n[i].row - 1
  1842. };
  1843. o[ n[i].row - 1 ] = {
  1844. text: o[ n[i].row - 1 ],
  1845. row: i - 1
  1846. };
  1847. }
  1848. }
  1849. return {
  1850. o: o,
  1851. n: n
  1852. };
  1853. }
  1854. return function( o, n ) {
  1855. o = o.replace( /\s+$/, "" );
  1856. n = n.replace( /\s+$/, "" );
  1857. var i, pre,
  1858. str = "",
  1859. out = diff( o === "" ? [] : o.split(/\s+/), n === "" ? [] : n.split(/\s+/) ),
  1860. oSpace = o.match(/\s+/g),
  1861. nSpace = n.match(/\s+/g);
  1862. if ( oSpace == null ) {
  1863. oSpace = [ " " ];
  1864. }
  1865. else {
  1866. oSpace.push( " " );
  1867. }
  1868. if ( nSpace == null ) {
  1869. nSpace = [ " " ];
  1870. }
  1871. else {
  1872. nSpace.push( " " );
  1873. }
  1874. if ( out.n.length === 0 ) {
  1875. for ( i = 0; i < out.o.length; i++ ) {
  1876. str += "<del>" + out.o[i] + oSpace[i] + "</del>";
  1877. }
  1878. }
  1879. else {
  1880. if ( out.n[0].text == null ) {
  1881. for ( n = 0; n < out.o.length && out.o[n].text == null; n++ ) {
  1882. str += "<del>" + out.o[n] + oSpace[n] + "</del>";
  1883. }
  1884. }
  1885. for ( i = 0; i < out.n.length; i++ ) {
  1886. if (out.n[i].text == null) {
  1887. str += "<ins>" + out.n[i] + nSpace[i] + "</ins>";
  1888. }
  1889. else {
  1890. // `pre` initialized at top of scope
  1891. pre = "";
  1892. for ( n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++ ) {
  1893. pre += "<del>" + out.o[n] + oSpace[n] + "</del>";
  1894. }
  1895. str += " " + out.n[i].text + nSpace[i] + pre;
  1896. }
  1897. }
  1898. }
  1899. return str;
  1900. };
  1901. }());
  1902. // for CommonJS environments, export everything
  1903. if ( typeof exports !== "undefined" ) {
  1904. extend( exports, QUnit );
  1905. }
  1906. // get at whatever the global object is, like window in browsers
  1907. }( (function() {return this;}.call()) ));