mocha.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672
  1. ;(function(){
  2. // CommonJS require()
  3. function require(p){
  4. var path = require.resolve(p)
  5. , mod = require.modules[path];
  6. if (!mod) throw new Error('failed to require "' + p + '"');
  7. if (!mod.exports) {
  8. mod.exports = {};
  9. mod.call(mod.exports, mod, mod.exports, require.relative(path));
  10. }
  11. return mod.exports;
  12. }
  13. require.modules = {};
  14. require.resolve = function (path){
  15. var orig = path
  16. , reg = path + '.js'
  17. , index = path + '/index.js';
  18. return require.modules[reg] && reg
  19. || require.modules[index] && index
  20. || orig;
  21. };
  22. require.register = function (path, fn){
  23. require.modules[path] = fn;
  24. };
  25. require.relative = function (parent) {
  26. return function(p){
  27. if ('.' != p[0]) return require(p);
  28. var path = parent.split('/')
  29. , segs = p.split('/');
  30. path.pop();
  31. for (var i = 0; i < segs.length; i++) {
  32. var seg = segs[i];
  33. if ('..' == seg) path.pop();
  34. else if ('.' != seg) path.push(seg);
  35. }
  36. return require(path.join('/'));
  37. };
  38. };
  39. require.register("browser/debug.js", function(module, exports, require){
  40. module.exports = function(type){
  41. return function(){
  42. }
  43. };
  44. }); // module: browser/debug.js
  45. require.register("browser/events.js", function(module, exports, require){
  46. /**
  47. * Module exports.
  48. */
  49. exports.EventEmitter = EventEmitter;
  50. /**
  51. * Check if `obj` is an array.
  52. */
  53. function isArray(obj) {
  54. return '[object Array]' == {}.toString.call(obj);
  55. }
  56. /**
  57. * Event emitter constructor.
  58. *
  59. * @api public.
  60. */
  61. function EventEmitter(){};
  62. /**
  63. * Adds a listener.
  64. *
  65. * @api public
  66. */
  67. EventEmitter.prototype.on = function (name, fn) {
  68. if (!this.$events) {
  69. this.$events = {};
  70. }
  71. if (!this.$events[name]) {
  72. this.$events[name] = fn;
  73. } else if (isArray(this.$events[name])) {
  74. this.$events[name].push(fn);
  75. } else {
  76. this.$events[name] = [this.$events[name], fn];
  77. }
  78. return this;
  79. };
  80. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  81. /**
  82. * Adds a volatile listener.
  83. *
  84. * @api public
  85. */
  86. EventEmitter.prototype.once = function (name, fn) {
  87. var self = this;
  88. function on () {
  89. self.removeListener(name, on);
  90. fn.apply(this, arguments);
  91. };
  92. on.listener = fn;
  93. this.on(name, on);
  94. return this;
  95. };
  96. /**
  97. * Removes a listener.
  98. *
  99. * @api public
  100. */
  101. EventEmitter.prototype.removeListener = function (name, fn) {
  102. if (this.$events && this.$events[name]) {
  103. var list = this.$events[name];
  104. if (isArray(list)) {
  105. var pos = -1;
  106. for (var i = 0, l = list.length; i < l; i++) {
  107. if (list[i] === fn || (list[i].listener && list[i].listener === fn)) {
  108. pos = i;
  109. break;
  110. }
  111. }
  112. if (pos < 0) {
  113. return this;
  114. }
  115. list.splice(pos, 1);
  116. if (!list.length) {
  117. delete this.$events[name];
  118. }
  119. } else if (list === fn || (list.listener && list.listener === fn)) {
  120. delete this.$events[name];
  121. }
  122. }
  123. return this;
  124. };
  125. /**
  126. * Removes all listeners for an event.
  127. *
  128. * @api public
  129. */
  130. EventEmitter.prototype.removeAllListeners = function (name) {
  131. if (name === undefined) {
  132. this.$events = {};
  133. return this;
  134. }
  135. if (this.$events && this.$events[name]) {
  136. this.$events[name] = null;
  137. }
  138. return this;
  139. };
  140. /**
  141. * Gets all listeners for a certain event.
  142. *
  143. * @api publci
  144. */
  145. EventEmitter.prototype.listeners = function (name) {
  146. if (!this.$events) {
  147. this.$events = {};
  148. }
  149. if (!this.$events[name]) {
  150. this.$events[name] = [];
  151. }
  152. if (!isArray(this.$events[name])) {
  153. this.$events[name] = [this.$events[name]];
  154. }
  155. return this.$events[name];
  156. };
  157. /**
  158. * Emits an event.
  159. *
  160. * @api public
  161. */
  162. EventEmitter.prototype.emit = function (name) {
  163. if (!this.$events) {
  164. return false;
  165. }
  166. var handler = this.$events[name];
  167. if (!handler) {
  168. return false;
  169. }
  170. var args = [].slice.call(arguments, 1);
  171. if ('function' == typeof handler) {
  172. handler.apply(this, args);
  173. } else if (isArray(handler)) {
  174. var listeners = handler.slice();
  175. for (var i = 0, l = listeners.length; i < l; i++) {
  176. listeners[i].apply(this, args);
  177. }
  178. } else {
  179. return false;
  180. }
  181. return true;
  182. };
  183. }); // module: browser/events.js
  184. require.register("browser/fs.js", function(module, exports, require){
  185. }); // module: browser/fs.js
  186. require.register("browser/progress.js", function(module, exports, require){
  187. /**
  188. * Expose `Progress`.
  189. */
  190. module.exports = Progress;
  191. /**
  192. * Initialize a new `Progress` indicator.
  193. */
  194. function Progress() {
  195. this.percent = 0;
  196. this.size(0);
  197. this.fontSize(12);
  198. this.font('helvetica, arial, sans-serif');
  199. }
  200. /**
  201. * Set progress size to `n`.
  202. *
  203. * @param {Number} n
  204. * @return {Progress} for chaining
  205. * @api public
  206. */
  207. Progress.prototype.size = function(n){
  208. this._size = n;
  209. return this;
  210. };
  211. /**
  212. * Set text to `str`.
  213. *
  214. * @param {String} str
  215. * @return {Progress} for chaining
  216. * @api public
  217. */
  218. Progress.prototype.text = function(str){
  219. this._text = str;
  220. return this;
  221. };
  222. /**
  223. * Set font size to `n`.
  224. *
  225. * @param {Number} n
  226. * @return {Progress} for chaining
  227. * @api public
  228. */
  229. Progress.prototype.fontSize = function(n){
  230. this._fontSize = n;
  231. return this;
  232. };
  233. /**
  234. * Set font `family`.
  235. *
  236. * @param {String} family
  237. * @return {Progress} for chaining
  238. */
  239. Progress.prototype.font = function(family){
  240. this._font = family;
  241. return this;
  242. };
  243. /**
  244. * Update percentage to `n`.
  245. *
  246. * @param {Number} n
  247. * @return {Progress} for chaining
  248. */
  249. Progress.prototype.update = function(n){
  250. this.percent = n;
  251. return this;
  252. };
  253. /**
  254. * Draw on `ctx`.
  255. *
  256. * @param {CanvasRenderingContext2d} ctx
  257. * @return {Progress} for chaining
  258. */
  259. Progress.prototype.draw = function(ctx){
  260. var percent = Math.min(this.percent, 100)
  261. , size = this._size
  262. , half = size / 2
  263. , x = half
  264. , y = half
  265. , rad = half - 1
  266. , fontSize = this._fontSize;
  267. ctx.font = fontSize + 'px ' + this._font;
  268. var angle = Math.PI * 2 * (percent / 100);
  269. ctx.clearRect(0, 0, size, size);
  270. // outer circle
  271. ctx.strokeStyle = '#9f9f9f';
  272. ctx.beginPath();
  273. ctx.arc(x, y, rad, 0, angle, false);
  274. ctx.stroke();
  275. // inner circle
  276. ctx.strokeStyle = '#eee';
  277. ctx.beginPath();
  278. ctx.arc(x, y, rad - 1, 0, angle, true);
  279. ctx.stroke();
  280. // text
  281. var text = this._text || (percent | 0) + '%'
  282. , w = ctx.measureText(text).width;
  283. ctx.fillText(
  284. text
  285. , x - w / 2 + 1
  286. , y + fontSize / 2 - 1);
  287. return this;
  288. };
  289. }); // module: browser/progress.js
  290. require.register("browser/tty.js", function(module, exports, require){
  291. exports.isatty = function(){
  292. return true;
  293. };
  294. exports.getWindowSize = function(){
  295. return [window.innerHeight, window.innerWidth];
  296. };
  297. }); // module: browser/tty.js
  298. require.register("hook.js", function(module, exports, require){
  299. /**
  300. * Module dependencies.
  301. */
  302. var Runnable = require('./runnable');
  303. /**
  304. * Expose `Hook`.
  305. */
  306. module.exports = Hook;
  307. /**
  308. * Initialize a new `Hook` with the given `title` and callback `fn`.
  309. *
  310. * @param {String} title
  311. * @param {Function} fn
  312. * @api private
  313. */
  314. function Hook(title, fn) {
  315. Runnable.call(this, title, fn);
  316. }
  317. /**
  318. * Inherit from `Runnable.prototype`.
  319. */
  320. Hook.prototype = new Runnable;
  321. Hook.prototype.constructor = Hook;
  322. }); // module: hook.js
  323. require.register("interfaces/bdd.js", function(module, exports, require){
  324. /**
  325. * Module dependencies.
  326. */
  327. var Suite = require('../suite')
  328. , Test = require('../test');
  329. /**
  330. * BDD-style interface:
  331. *
  332. * describe('Array', function(){
  333. * describe('#indexOf()', function(){
  334. * it('should return -1 when not present', function(){
  335. *
  336. * });
  337. *
  338. * it('should return the index when present', function(){
  339. *
  340. * });
  341. * });
  342. * });
  343. *
  344. */
  345. module.exports = function(suite){
  346. var suites = [suite];
  347. suite.on('pre-require', function(context){
  348. /**
  349. * Execute before running tests.
  350. */
  351. context.before = function(fn){
  352. suites[0].beforeAll(fn);
  353. };
  354. /**
  355. * Execute after running tests.
  356. */
  357. context.after = function(fn){
  358. suites[0].afterAll(fn);
  359. };
  360. /**
  361. * Execute before each test case.
  362. */
  363. context.beforeEach = function(fn){
  364. suites[0].beforeEach(fn);
  365. };
  366. /**
  367. * Execute after each test case.
  368. */
  369. context.afterEach = function(fn){
  370. suites[0].afterEach(fn);
  371. };
  372. /**
  373. * Describe a "suite" with the given `title`
  374. * and callback `fn` containing nested suites
  375. * and/or tests.
  376. */
  377. context.describe = function(title, fn){
  378. var suite = Suite.create(suites[0], title);
  379. suites.unshift(suite);
  380. fn();
  381. suites.shift();
  382. };
  383. /**
  384. * Describe a specification or test-case
  385. * with the given `title` and callback `fn`
  386. * acting as a thunk.
  387. */
  388. context.it = function(title, fn){
  389. suites[0].addTest(new Test(title, fn));
  390. };
  391. });
  392. };
  393. }); // module: interfaces/bdd.js
  394. require.register("interfaces/exports.js", function(module, exports, require){
  395. /**
  396. * Module dependencies.
  397. */
  398. var Suite = require('../suite')
  399. , Test = require('../test');
  400. /**
  401. * TDD-style interface:
  402. *
  403. * exports.Array = {
  404. * '#indexOf()': {
  405. * 'should return -1 when the value is not present': function(){
  406. *
  407. * },
  408. *
  409. * 'should return the correct index when the value is present': function(){
  410. *
  411. * }
  412. * }
  413. * };
  414. *
  415. */
  416. module.exports = function(suite){
  417. var suites = [suite];
  418. suite.on('require', visit);
  419. function visit(obj) {
  420. var suite;
  421. for (var key in obj) {
  422. if ('function' == typeof obj[key]) {
  423. var fn = obj[key];
  424. switch (key) {
  425. case 'before':
  426. suites[0].beforeAll(fn);
  427. break;
  428. case 'after':
  429. suites[0].afterAll(fn);
  430. break;
  431. case 'beforeEach':
  432. suites[0].beforeEach(fn);
  433. break;
  434. case 'afterEach':
  435. suites[0].afterEach(fn);
  436. break;
  437. default:
  438. suites[0].addTest(new Test(key, fn));
  439. }
  440. } else {
  441. var suite = Suite.create(suites[0], key);
  442. suites.unshift(suite);
  443. visit(obj[key]);
  444. suites.shift();
  445. }
  446. }
  447. }
  448. };
  449. }); // module: interfaces/exports.js
  450. require.register("interfaces/index.js", function(module, exports, require){
  451. exports.bdd = require('./bdd');
  452. exports.tdd = require('./tdd');
  453. exports.exports = require('./exports');
  454. }); // module: interfaces/index.js
  455. require.register("interfaces/tdd.js", function(module, exports, require){
  456. /**
  457. * Module dependencies.
  458. */
  459. var Suite = require('../suite')
  460. , Test = require('../test');
  461. /**
  462. * TDD-style interface:
  463. *
  464. * suite('Array', function(){
  465. * suite('#indexOf()', function(){
  466. * suiteSetup(function(){
  467. *
  468. * });
  469. *
  470. * test('should return -1 when not present', function(){
  471. *
  472. * });
  473. *
  474. * test('should return the index when present', function(){
  475. *
  476. * });
  477. *
  478. * suiteTeardown(function(){
  479. *
  480. * });
  481. * });
  482. * });
  483. *
  484. */
  485. module.exports = function(suite){
  486. var suites = [suite];
  487. suite.on('pre-require', function(context){
  488. /**
  489. * Execute before each test case.
  490. */
  491. context.setup = function(fn){
  492. suites[0].beforeEach(fn);
  493. };
  494. /**
  495. * Execute after each test case.
  496. */
  497. context.teardown = function(fn){
  498. suites[0].afterEach(fn);
  499. };
  500. /**
  501. * Execute before the suite.
  502. */
  503. context.suiteSetup = function(fn){
  504. suites[0].beforeAll(fn);
  505. };
  506. /**
  507. * Execute after the suite.
  508. */
  509. context.suiteTeardown = function(fn){
  510. suites[0].afterAll(fn);
  511. };
  512. /**
  513. * Describe a "suite" with the given `title`
  514. * and callback `fn` containing nested suites
  515. * and/or tests.
  516. */
  517. context.suite = function(title, fn){
  518. var suite = Suite.create(suites[0], title);
  519. suites.unshift(suite);
  520. fn();
  521. suites.shift();
  522. };
  523. /**
  524. * Describe a specification or test-case
  525. * with the given `title` and callback `fn`
  526. * acting as a thunk.
  527. */
  528. context.test = function(title, fn){
  529. suites[0].addTest(new Test(title, fn));
  530. };
  531. });
  532. };
  533. }); // module: interfaces/tdd.js
  534. require.register("mocha.js", function(module, exports, require){
  535. /*!
  536. * mocha
  537. * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
  538. * MIT Licensed
  539. */
  540. /**
  541. * Library version.
  542. */
  543. exports.version = '0.3.6';
  544. exports.interfaces = require('./interfaces');
  545. exports.reporters = require('./reporters');
  546. exports.Runnable = require('./runnable');
  547. exports.Runner = require('./runner');
  548. exports.Suite = require('./suite');
  549. exports.Hook = require('./hook');
  550. exports.Test = require('./test');
  551. exports.watch = require('./watch');
  552. }); // module: mocha.js
  553. require.register("reporters/base.js", function(module, exports, require){
  554. /**
  555. * Module dependencies.
  556. */
  557. var tty = require('browser/tty');
  558. /**
  559. * Check if both stdio streams are associated with a tty.
  560. */
  561. var isatty = tty.isatty(1) && tty.isatty(2);
  562. /**
  563. * Expose `Base`.
  564. */
  565. exports = module.exports = Base;
  566. /**
  567. * Enable coloring by default.
  568. */
  569. exports.useColors = isatty;
  570. /**
  571. * Default color map.
  572. */
  573. exports.colors = {
  574. 'pass': 90
  575. , 'fail': 31
  576. , 'bright pass': 92
  577. , 'bright fail': 91
  578. , 'bright yellow': 93
  579. , 'pending': 36
  580. , 'suite': 0
  581. , 'error title': 0
  582. , 'error message': 31
  583. , 'error stack': 90
  584. , 'checkmark': 32
  585. , 'fast': 90
  586. , 'medium': 33
  587. , 'slow': 31
  588. , 'green': 32
  589. , 'light': 90
  590. };
  591. /**
  592. * Color `str` with the given `type`,
  593. * allowing colors to be disabled,
  594. * as well as user-defined color
  595. * schemes.
  596. *
  597. * @param {String} type
  598. * @param {String} str
  599. * @return {String}
  600. * @api private
  601. */
  602. var color = exports.color = function(type, str) {
  603. if (!exports.useColors) return str;
  604. return '\033[' + exports.colors[type] + 'm' + str + '\033[0m';
  605. };
  606. /**
  607. * Expose term window size, with some
  608. * defaults for when stderr is not a tty.
  609. */
  610. exports.window = {
  611. width: isatty
  612. ? process.stdout.getWindowSize
  613. ? process.stdout.getWindowSize(1)[0]
  614. : tty.getWindowSize()[1]
  615. : 75
  616. };
  617. /**
  618. * Expose some basic cursor interactions
  619. * that are common among reporters.
  620. */
  621. exports.cursor = {
  622. hide: function(){
  623. process.stdout.write('\033[?25l');
  624. },
  625. show: function(){
  626. process.stdout.write('\033[?25h');
  627. }
  628. };
  629. /**
  630. * A test is considered slow if it
  631. * exceeds the following value in milliseconds.
  632. */
  633. exports.slow = 75;
  634. /**
  635. * Outut the given `failures` as a list.
  636. *
  637. * @param {Array} failures
  638. * @api public
  639. */
  640. exports.list = function(failures){
  641. console.error();
  642. failures.forEach(function(test, i){
  643. // format
  644. var fmt = color('error title', ' %s) %s:\n')
  645. + color('error message', ' %s')
  646. + color('error stack', '\n%s\n');
  647. // msg
  648. var err = test.err
  649. , stack = err.stack
  650. , message = err.message || ''
  651. , index = stack.indexOf(message) + message.length
  652. , msg = stack.slice(0, index);
  653. // indent stack trace without msg
  654. stack = stack.slice(index + 1)
  655. .replace(/^/gm, ' ');
  656. console.error(fmt, i, test.fullTitle(), msg, stack);
  657. });
  658. };
  659. /**
  660. * Initialize a new `Base` reporter.
  661. *
  662. * All other reporters generally
  663. * inherit from this reporter, providing
  664. * stats such as test duration, number
  665. * of tests passed / failed etc.
  666. *
  667. * @param {Runner} runner
  668. * @api public
  669. */
  670. function Base(runner) {
  671. var self = this
  672. , stats = this.stats = { suites: 0, tests: 0, passes: 0, failures: 0 }
  673. , failures = this.failures = [];
  674. if (!runner) return;
  675. this.runner = runner;
  676. runner.on('start', function(){
  677. stats.start = new Date;
  678. });
  679. runner.on('suite', function(suite){
  680. stats.suites = stats.suites || 0;
  681. stats.suites++;
  682. });
  683. runner.on('test end', function(test){
  684. stats.tests = stats.tests || 0;
  685. stats.tests++;
  686. });
  687. runner.on('pass', function(test){
  688. stats.passes = stats.passes || 0;
  689. var medium = exports.slow / 2;
  690. test.speed = test.duration > exports.slow
  691. ? 'slow'
  692. : test.duration > medium
  693. ? 'medium'
  694. : 'fast';
  695. stats.passes++;
  696. });
  697. runner.on('fail', function(test, err){
  698. stats.failures = stats.failures || 0;
  699. stats.failures++;
  700. test.err = err;
  701. failures.push(test);
  702. });
  703. runner.on('end', function(){
  704. stats.end = new Date;
  705. stats.duration = new Date - stats.start;
  706. });
  707. }
  708. /**
  709. * Output common epilogue used by many of
  710. * the bundled reporters.
  711. *
  712. * @api public
  713. */
  714. Base.prototype.epilogue = function(){
  715. var stats = this.stats
  716. , fmt;
  717. console.log();
  718. // failure
  719. if (stats.failures) {
  720. fmt = color('bright fail', ' ✖')
  721. + color('fail', ' %d of %d tests failed')
  722. + color('light', ':')
  723. console.error(fmt, stats.failures, this.runner.total);
  724. Base.list(this.failures);
  725. console.error();
  726. return;
  727. }
  728. // pass
  729. fmt = color('bright pass', ' ✔')
  730. + color('green', ' %d tests complete')
  731. + color('light', ' (%dms)');
  732. console.log(fmt, stats.tests || 0, stats.duration);
  733. console.log();
  734. };
  735. }); // module: reporters/base.js
  736. require.register("reporters/doc.js", function(module, exports, require){
  737. /**
  738. * Module dependencies.
  739. */
  740. var Base = require('./base')
  741. , utils = require('../utils');
  742. /**
  743. * Expose `Doc`.
  744. */
  745. exports = module.exports = Doc;
  746. /**
  747. * Initialize a new `Doc` reporter.
  748. *
  749. * @param {Runner} runner
  750. * @api public
  751. */
  752. function Doc(runner) {
  753. Base.call(this, runner);
  754. var self = this
  755. , stats = this.stats
  756. , total = runner.total
  757. , indents = 2;
  758. function indent() {
  759. return Array(indents).join(' ');
  760. }
  761. runner.on('suite', function(suite){
  762. if (suite.root) return;
  763. ++indents;
  764. console.log('%s<section class="suite">', indent());
  765. ++indents;
  766. console.log('%s<h1>%s</h1>', indent(), suite.title);
  767. console.log('%s<dl>', indent());
  768. });
  769. runner.on('suite end', function(suite){
  770. if (suite.root) return;
  771. console.log('%s</dl>', indent());
  772. --indents;
  773. console.log('%s</section>', indent());
  774. --indents;
  775. });
  776. runner.on('pass', function(test){
  777. console.log('%s <dt>%s</dt>', indent(), test.title);
  778. var code = utils.escape(clean(test.fn.toString()));
  779. console.log('%s <dd><pre><code>%s</code></pre></dd>', indent(), code);
  780. });
  781. }
  782. /**
  783. * Strip the function definition from `str`,
  784. * and re-indent for pre whitespace.
  785. */
  786. function clean(str) {
  787. str = str
  788. .replace(/^function *\(.*\) *{/, '')
  789. .replace(/\s+\}$/, '');
  790. var spaces = str.match(/^\n?( *)/)[1].length
  791. , re = new RegExp('^ {' + spaces + '}', 'gm');
  792. str = str.replace(re, '');
  793. return str;
  794. }
  795. }); // module: reporters/doc.js
  796. require.register("reporters/dot.js", function(module, exports, require){
  797. /**
  798. * Module dependencies.
  799. */
  800. var Base = require('./base')
  801. , color = Base.color;
  802. /**
  803. * Expose `Dot`.
  804. */
  805. exports = module.exports = Dot;
  806. /**
  807. * Initialize a new `Dot` matrix test reporter.
  808. *
  809. * @param {Runner} runner
  810. * @api public
  811. */
  812. function Dot(runner) {
  813. Base.call(this, runner);
  814. var self = this
  815. , stats = this.stats
  816. , width = Base.window.width * .75 | 0
  817. , n = 0;
  818. runner.on('start', function(){
  819. process.stdout.write('\n ');
  820. });
  821. runner.on('pending', function(test){
  822. process.stdout.write(color('pending', '.'));
  823. });
  824. runner.on('pass', function(test){
  825. if (++n % width == 0) process.stdout.write('\n ');
  826. if ('slow' == test.speed) {
  827. process.stdout.write(color('bright yellow', '.'));
  828. } else {
  829. process.stdout.write(color(test.speed, '.'));
  830. }
  831. });
  832. runner.on('fail', function(test, err){
  833. if (++n % width == 0) process.stdout.write('\n ');
  834. process.stdout.write(color('fail', '.'));
  835. });
  836. runner.on('end', function(){
  837. console.log();
  838. self.epilogue();
  839. });
  840. }
  841. /**
  842. * Inherit from `Base.prototype`.
  843. */
  844. Dot.prototype = new Base;
  845. Dot.prototype.constructor = Dot;
  846. }); // module: reporters/dot.js
  847. require.register("reporters/html.js", function(module, exports, require){
  848. /**
  849. * Module dependencies.
  850. */
  851. var Base = require('./base')
  852. , utils = require('../utils')
  853. , Progress = require('../browser/progress');
  854. /**
  855. * Expose `Doc`.
  856. */
  857. exports = module.exports = HTML;
  858. /**
  859. * Stats template.
  860. */
  861. var statsTemplate = '<ul id="stats">'
  862. + '<li class="progress"><canvas width="50" height="50"></canvas></li>'
  863. + '<li class="passes">passes: <em>0</em></li>'
  864. + '<li class="failures">failures: <em>0</em></li>'
  865. + '<li class="duration">duration: <em>0</em>s</li>'
  866. + '</ul>';
  867. /**
  868. * Initialize a new `Doc` reporter.
  869. *
  870. * @param {Runner} runner
  871. * @api public
  872. */
  873. function HTML(runner) {
  874. Base.call(this, runner);
  875. // TODO: clean up
  876. var self = this
  877. , stats = this.stats
  878. , total = runner.total
  879. , root = $('#mocha')
  880. , stack = [root]
  881. , stat = $(statsTemplate).appendTo(root)
  882. , canvas = stat.find('canvas').get(0)
  883. , ctx = canvas.getContext('2d')
  884. , progress = new Progress;
  885. if (!root.length) return error('#mocha div missing, add it to your document');
  886. progress.size(50);
  887. runner.on('suite', function(suite){
  888. if (suite.root) return;
  889. // suite
  890. var el = $('<div class="suite"><h1>' + suite.title + '</h1></div>');
  891. // container
  892. stack[0].append(el);
  893. stack.unshift($('<div>'));
  894. el.append(stack[0]);
  895. });
  896. runner.on('suite end', function(suite){
  897. if (suite.root) return;
  898. stack.shift();
  899. });
  900. runner.on('test end', function(test){
  901. // TODO: add to stats
  902. var percent = stats.tests / total * 100 | 0;
  903. // update progress bar
  904. progress.update(percent).draw(ctx);
  905. // update stats
  906. var ms = new Date - stats.start;
  907. stat.find('.passes em').text(stats.passes);
  908. stat.find('.failures em').text(stats.failures);
  909. stat.find('.duration em').text((ms / 1000).toFixed(2));
  910. // test
  911. if (test.passed) {
  912. var el = $('<div class="test pass"><h2>' + test.title + '</h2></div>')
  913. } else if (test.pending) {
  914. var el = $('<div class="test pass pending"><h2>' + test.title + '</h2></div>')
  915. } else {
  916. var el = $('<div class="test fail"><h2>' + test.title + '</h2></div>');
  917. var str = test.err.stack || test.err;
  918. var err = $('<pre class="error">' + str + '</pre>');
  919. el.append(err);
  920. }
  921. // toggle code
  922. el.find('h2').toggle(function(){
  923. pre.slideDown('fast');
  924. }, function(){
  925. pre.slideUp('fast');
  926. });
  927. // code
  928. // TODO: defer
  929. if (!test.pending) {
  930. var code = utils.escape(clean(test.fn.toString()));
  931. var pre = $('<pre><code>' + code + '</code></pre>');
  932. pre.appendTo(el).hide();
  933. }
  934. stack[0].append(el);
  935. });
  936. }
  937. function error(msg) {
  938. $('<div id="error">' + msg + '</div>').appendTo('body');
  939. }
  940. /**
  941. * Strip the function definition from `str`,
  942. * and re-indent for pre whitespace.
  943. */
  944. function clean(str) {
  945. str = str
  946. .replace(/^function *\(.*\) *{/, '')
  947. .replace(/\s+\}$/, '');
  948. var spaces = str.match(/^\n?( *)/)[1].length
  949. , re = new RegExp('^ {' + spaces + '}', 'gm');
  950. str = str
  951. .replace(re, '')
  952. .replace(/^\s+/, '');
  953. return str;
  954. }
  955. }); // module: reporters/html.js
  956. require.register("reporters/index.js", function(module, exports, require){
  957. exports.Base = require('./base');
  958. exports.Dot = require('./dot');
  959. exports.Doc = require('./doc');
  960. exports.TAP = require('./tap');
  961. exports.JSON = require('./json');
  962. exports.HTML = require('./html');
  963. exports.List = require('./list');
  964. exports.Spec = require('./spec');
  965. exports.Progress = require('./progress');
  966. exports.Landing = require('./landing');
  967. exports.JSONStream = require('./json-stream');
  968. }); // module: reporters/index.js
  969. require.register("reporters/json-stream.js", function(module, exports, require){
  970. /**
  971. * Module dependencies.
  972. */
  973. var Base = require('./base')
  974. , color = Base.color;
  975. /**
  976. * Expose `List`.
  977. */
  978. exports = module.exports = List;
  979. /**
  980. * Initialize a new `List` test reporter.
  981. *
  982. * @param {Runner} runner
  983. * @api public
  984. */
  985. function List(runner) {
  986. Base.call(this, runner);
  987. var self = this
  988. , stats = this.stats
  989. , total = runner.total;
  990. runner.on('start', function(){
  991. console.log(JSON.stringify(['start', { total: total }]));
  992. });
  993. runner.on('pass', function(test){
  994. console.log(JSON.stringify(['pass', clean(test)]));
  995. });
  996. runner.on('fail', function(test, err){
  997. console.log(JSON.stringify(['fail', clean(test)]));
  998. });
  999. runner.on('end', function(){
  1000. process.stdout.write(JSON.stringify(['end', self.stats]));
  1001. });
  1002. }
  1003. /**
  1004. * Return a plain-object representation of `test`
  1005. * free of cyclic properties etc.
  1006. *
  1007. * @param {Object} test
  1008. * @return {Object}
  1009. * @api private
  1010. */
  1011. function clean(test) {
  1012. return {
  1013. title: test.title
  1014. , fullTitle: test.fullTitle()
  1015. , duration: test.duration
  1016. }
  1017. }
  1018. }); // module: reporters/json-stream.js
  1019. require.register("reporters/json.js", function(module, exports, require){
  1020. /**
  1021. * Module dependencies.
  1022. */
  1023. var Base = require('./base')
  1024. , cursor = Base.cursor
  1025. , color = Base.color;
  1026. /**
  1027. * Expose `JSON`.
  1028. */
  1029. exports = module.exports = JSONReporter;
  1030. /**
  1031. * Initialize a new `JSON` reporter.
  1032. *
  1033. * @param {Runner} runner
  1034. * @api public
  1035. */
  1036. function JSONReporter(runner) {
  1037. var self = this;
  1038. Base.call(this, runner);
  1039. var tests = []
  1040. , failures = []
  1041. , passes = [];
  1042. runner.on('test end', function(test){
  1043. tests.push(test);
  1044. });
  1045. runner.on('pass', function(test){
  1046. passes.push(test);
  1047. });
  1048. runner.on('fail', function(test){
  1049. failures.push(test);
  1050. });
  1051. runner.on('end', function(){
  1052. var obj = {
  1053. stats: self.stats
  1054. , tests: tests.map(clean)
  1055. , failures: failures.map(clean)
  1056. , passes: passes.map(clean)
  1057. };
  1058. process.stdout.write(JSON.stringify(obj));
  1059. });
  1060. }
  1061. /**
  1062. * Return a plain-object representation of `test`
  1063. * free of cyclic properties etc.
  1064. *
  1065. * @param {Object} test
  1066. * @return {Object}
  1067. * @api private
  1068. */
  1069. function clean(test) {
  1070. return {
  1071. title: test.title
  1072. , fullTitle: test.fullTitle()
  1073. , duration: test.duration
  1074. }
  1075. }
  1076. }); // module: reporters/json.js
  1077. require.register("reporters/landing.js", function(module, exports, require){
  1078. /**
  1079. * Module dependencies.
  1080. */
  1081. var Base = require('./base')
  1082. , cursor = Base.cursor
  1083. , color = Base.color;
  1084. /**
  1085. * Expose `Landing`.
  1086. */
  1087. exports = module.exports = Landing;
  1088. /**
  1089. * Airplane color.
  1090. */
  1091. Base.colors.plane = 0;
  1092. /**
  1093. * Airplane crash color.
  1094. */
  1095. Base.colors['plane crash'] = 31;
  1096. /**
  1097. * Runway color.
  1098. */
  1099. Base.colors.runway = 90;
  1100. /**
  1101. * Initialize a new `Landing` reporter.
  1102. *
  1103. * @param {Runner} runner
  1104. * @api public
  1105. */
  1106. function Landing(runner) {
  1107. Base.call(this, runner);
  1108. var self = this
  1109. , stats = this.stats
  1110. , width = Base.window.width * .75 | 0
  1111. , total = runner.total
  1112. , stream = process.stdout
  1113. , plane = color('plane', '✈')
  1114. , crashed = -1
  1115. , n = 0;
  1116. function runway() {
  1117. var buf = Array(width).join('-');
  1118. return ' ' + color('runway', buf);
  1119. }
  1120. runner.on('start', function(){
  1121. stream.write('\n ');
  1122. cursor.hide();
  1123. });
  1124. runner.on('test end', function(test){
  1125. // check if the plane crashed
  1126. var col = -1 == crashed
  1127. ? width * ++n / total | 0
  1128. : crashed;
  1129. // show the crash
  1130. if (test.failed) {
  1131. plane = color('plane crash', '✈');
  1132. crashed = col;
  1133. }
  1134. // render landing strip
  1135. stream.write('\033[4F\n\n');
  1136. stream.write(runway());
  1137. stream.write('\n ');
  1138. stream.write(color('runway', Array(col).join('⋅')));
  1139. stream.write(plane)
  1140. stream.write(color('runway', Array(width - col).join('⋅') + '\n'));
  1141. stream.write(runway());
  1142. stream.write('\033[0m');
  1143. });
  1144. runner.on('end', function(){
  1145. cursor.show();
  1146. console.log();
  1147. self.epilogue();
  1148. });
  1149. }
  1150. /**
  1151. * Inherit from `Base.prototype`.
  1152. */
  1153. Landing.prototype = new Base;
  1154. Landing.prototype.constructor = Landing;
  1155. }); // module: reporters/landing.js
  1156. require.register("reporters/list.js", function(module, exports, require){
  1157. /**
  1158. * Module dependencies.
  1159. */
  1160. var Base = require('./base')
  1161. , color = Base.color;
  1162. /**
  1163. * Expose `List`.
  1164. */
  1165. exports = module.exports = List;
  1166. /**
  1167. * Initialize a new `List` test reporter.
  1168. *
  1169. * @param {Runner} runner
  1170. * @api public
  1171. */
  1172. function List(runner) {
  1173. Base.call(this, runner);
  1174. var self = this
  1175. , stats = this.stats
  1176. , n = 0;
  1177. runner.on('start', function(){
  1178. console.log();
  1179. });
  1180. runner.on('test', function(test){
  1181. process.stdout.write(color('pass', ' ' + test.fullTitle() + ': '));
  1182. });
  1183. runner.on('pending', function(test){
  1184. var fmt = color('checkmark', ' -')
  1185. + color('pending', ' %s');
  1186. console.log(fmt, test.fullTitle());
  1187. });
  1188. runner.on('pass', function(test){
  1189. var fmt = color('checkmark', ' ✓')
  1190. + color('pass', ' %s: ')
  1191. + color(test.speed, '%dms');
  1192. console.log('\r' + fmt, test.fullTitle(), test.duration);
  1193. });
  1194. runner.on('fail', function(test, err){
  1195. console.log('\r' + color('fail', ' %d) %s'), n++, test.fullTitle());
  1196. });
  1197. runner.on('end', self.epilogue.bind(self));
  1198. }
  1199. /**
  1200. * Inherit from `Base.prototype`.
  1201. */
  1202. List.prototype = new Base;
  1203. List.prototype.constructor = List;
  1204. }); // module: reporters/list.js
  1205. require.register("reporters/progress.js", function(module, exports, require){
  1206. /**
  1207. * Module dependencies.
  1208. */
  1209. var Base = require('./base')
  1210. , cursor = Base.cursor
  1211. , color = Base.color;
  1212. /**
  1213. * Expose `Progress`.
  1214. */
  1215. exports = module.exports = Progress;
  1216. /**
  1217. * General progress bar color.
  1218. */
  1219. Base.colors.progress = 90;
  1220. /**
  1221. * Initialize a new `Progress` bar test reporter.
  1222. *
  1223. * @param {Runner} runner
  1224. * @param {Object} options
  1225. * @api public
  1226. */
  1227. function Progress(runner, options) {
  1228. Base.call(this, runner);
  1229. var self = this
  1230. , options = options || {}
  1231. , stats = this.stats
  1232. , width = Base.window.width * .50 | 0
  1233. , total = runner.total
  1234. , complete = 0
  1235. , max = Math.max;
  1236. // default chars
  1237. options.open = options.open || '[';
  1238. options.complete = options.complete || '▬';
  1239. options.incomplete = options.incomplete || '⋅';
  1240. options.close = options.close || ']';
  1241. options.verbose = false;
  1242. // tests started
  1243. runner.on('start', function(){
  1244. console.log();
  1245. cursor.hide();
  1246. });
  1247. // tests complete
  1248. runner.on('test end', function(){
  1249. var incomplete = total - complete
  1250. , percent = complete++ / total
  1251. , n = width * percent | 0
  1252. , i = width - n;
  1253. process.stdout.write('\r\033[J');
  1254. process.stdout.write(color('progress', ' ' + options.open));
  1255. process.stdout.write(Array(n).join(options.complete));
  1256. process.stdout.write(Array(i).join(options.incomplete));
  1257. process.stdout.write(color('progress', options.close));
  1258. if (options.verbose) {
  1259. process.stdout.write(color('progress', ' ' + complete + ' of ' + total));
  1260. }
  1261. });
  1262. // tests are complete, output some stats
  1263. // and the failures if any
  1264. runner.on('end', function(){
  1265. cursor.show();
  1266. console.log();
  1267. self.epilogue();
  1268. });
  1269. }
  1270. /**
  1271. * Inherit from `Base.prototype`.
  1272. */
  1273. Progress.prototype = new Base;
  1274. Progress.prototype.constructor = Progress;
  1275. }); // module: reporters/progress.js
  1276. require.register("reporters/spec.js", function(module, exports, require){
  1277. /**
  1278. * Module dependencies.
  1279. */
  1280. var Base = require('./base')
  1281. , color = Base.color;
  1282. /**
  1283. * Expose `Spec`.
  1284. */
  1285. exports = module.exports = Spec;
  1286. /**
  1287. * Initialize a new `Spec` test reporter.
  1288. *
  1289. * @param {Runner} runner
  1290. * @api public
  1291. */
  1292. function Spec(runner) {
  1293. Base.call(this, runner);
  1294. var self = this
  1295. , stats = this.stats
  1296. , indents = 0
  1297. , n = 0;
  1298. function indent() {
  1299. return Array(indents).join(' ')
  1300. }
  1301. runner.on('start', function(){
  1302. console.log();
  1303. });
  1304. runner.on('suite', function(suite){
  1305. ++indents;
  1306. console.log(color('suite', '%s%s'), indent(), suite.title);
  1307. });
  1308. runner.on('suite end', function(suite){
  1309. --indents;
  1310. if (1 == indents) console.log();
  1311. });
  1312. runner.on('test', function(test){
  1313. process.stdout.write(indent() + color('pass', ' ◦ ' + test.title + ': '));
  1314. });
  1315. runner.on('pending', function(test){
  1316. var fmt = indent() + color('pending', ' - %s');
  1317. console.log(fmt, test.title);
  1318. });
  1319. runner.on('pass', function(test){
  1320. if ('fast' == test.speed) {
  1321. var fmt = indent()
  1322. + color('checkmark', ' ✓')
  1323. + color('pass', ' %s ');
  1324. console.log('\r' + fmt, test.title);
  1325. } else {
  1326. var fmt = indent()
  1327. + color('checkmark', ' ✓')
  1328. + color('pass', ' %s ')
  1329. + color(test.speed, '(%dms)');
  1330. console.log('\r' + fmt, test.title, test.duration);
  1331. }
  1332. });
  1333. runner.on('fail', function(test, err){
  1334. console.log('\r' + indent() + color('fail', ' %d) %s'), n++, test.title);
  1335. });
  1336. runner.on('end', self.epilogue.bind(self));
  1337. }
  1338. /**
  1339. * Inherit from `Base.prototype`.
  1340. */
  1341. Spec.prototype = new Base;
  1342. Spec.prototype.constructor = Spec;
  1343. }); // module: reporters/spec.js
  1344. require.register("reporters/tap.js", function(module, exports, require){
  1345. /**
  1346. * Module dependencies.
  1347. */
  1348. var Base = require('./base')
  1349. , cursor = Base.cursor
  1350. , color = Base.color;
  1351. /**
  1352. * Expose `TAP`.
  1353. */
  1354. exports = module.exports = TAP;
  1355. /**
  1356. * Initialize a new `TAP` reporter.
  1357. *
  1358. * @param {Runner} runner
  1359. * @api public
  1360. */
  1361. function TAP(runner) {
  1362. Base.call(this, runner);
  1363. var self = this
  1364. , stats = this.stats
  1365. , total = runner.total
  1366. , n = 1;
  1367. runner.on('start', function(){
  1368. console.log('%d..%d', 1, total);
  1369. });
  1370. runner.on('test end', function(){
  1371. ++n;
  1372. });
  1373. runner.on('pass', function(test){
  1374. console.log('ok %d %s', n, test.fullTitle());
  1375. });
  1376. runner.on('fail', function(test, err){
  1377. console.log('not ok %d %s', n, test.fullTitle());
  1378. console.log(err.stack.replace(/^/gm, ' '));
  1379. });
  1380. }
  1381. }); // module: reporters/tap.js
  1382. require.register("runnable.js", function(module, exports, require){
  1383. /**
  1384. * Module dependencies.
  1385. */
  1386. var EventEmitter = require('browser/events').EventEmitter
  1387. , debug = require('browser/debug')('runnable');
  1388. /**
  1389. * Expose `Runnable`.
  1390. */
  1391. module.exports = Runnable;
  1392. /**
  1393. * Initialize a new `Runnable` with the given `title` and callback `fn`.
  1394. *
  1395. * @param {String} title
  1396. * @param {Function} fn
  1397. * @api private
  1398. */
  1399. function Runnable(title, fn) {
  1400. this.title = title;
  1401. this.fn = fn;
  1402. this.async = fn && fn.length;
  1403. this.sync = ! this.async;
  1404. this._timeout = 2000;
  1405. }
  1406. /**
  1407. * Inherit from `EventEmitter.prototype`.
  1408. */
  1409. Runnable.prototype = new EventEmitter;
  1410. Runnable.prototype.constructor = Runnable;
  1411. /**
  1412. * Set & get timeout `ms`.
  1413. *
  1414. * @param {Number} ms
  1415. * @return {Runnable|Number} ms or self
  1416. * @api private
  1417. */
  1418. Runnable.prototype.timeout = function(ms){
  1419. if (0 == arguments.length) return this._timeout;
  1420. debug('timeout %d', ms);
  1421. this._timeout = ms;
  1422. return this;
  1423. };
  1424. /**
  1425. * Return the full title generated by recursively
  1426. * concatenating the parent's full title.
  1427. *
  1428. * @return {String}
  1429. * @api public
  1430. */
  1431. Runnable.prototype.fullTitle = function(){
  1432. return this.parent.fullTitle() + ' ' + this.title;
  1433. };
  1434. /**
  1435. * Clear the timeout.
  1436. *
  1437. * @api private
  1438. */
  1439. Runnable.prototype.clearTimeout = function(){
  1440. clearTimeout(this.timer);
  1441. };
  1442. /**
  1443. * Run the test and invoke `fn(err)`.
  1444. *
  1445. * @param {Function} fn
  1446. * @api private
  1447. */
  1448. Runnable.prototype.run = function(fn){
  1449. var self = this
  1450. , ms = this.timeout()
  1451. , start = new Date
  1452. , finished
  1453. , emitted;
  1454. // timeout
  1455. if (this.async) {
  1456. this.timer = setTimeout(function(){
  1457. done(new Error('timeout of ' + ms + 'ms exceeded'));
  1458. }, ms);
  1459. }
  1460. // called multiple times
  1461. function multiple() {
  1462. if (emitted) return;
  1463. emitted = true;
  1464. self.emit('error', new Error('done() called multiple times'));
  1465. }
  1466. // finished
  1467. function done(err) {
  1468. if (finished) return multiple();
  1469. self.clearTimeout();
  1470. self.duration = new Date - start;
  1471. finished = true;
  1472. fn(err);
  1473. }
  1474. // async
  1475. if (this.async) {
  1476. try {
  1477. this.fn(function(err){
  1478. if (err instanceof Error) return done(err);
  1479. done();
  1480. });
  1481. } catch (err) {
  1482. done(err);
  1483. }
  1484. return;
  1485. }
  1486. // sync
  1487. try {
  1488. if (!this.pending) this.fn();
  1489. this.duration = new Date - start;
  1490. fn();
  1491. } catch (err) {
  1492. fn(err);
  1493. }
  1494. };
  1495. }); // module: runnable.js
  1496. require.register("runner.js", function(module, exports, require){
  1497. /**
  1498. * Module dependencies.
  1499. */
  1500. var EventEmitter = require('browser/events').EventEmitter
  1501. , debug = require('browser/debug')('runner')
  1502. , Test = require('./test')
  1503. , noop = function(){};
  1504. /**
  1505. * Expose `Runner`.
  1506. */
  1507. module.exports = Runner;
  1508. /**
  1509. * Initialize a `Runner` for the given `suite`.
  1510. *
  1511. * Events:
  1512. *
  1513. * - `start` execution started
  1514. * - `end` execution complete
  1515. * - `suite` (suite) test suite execution started
  1516. * - `suite end` (suite) all tests (and sub-suites) have finished
  1517. * - `test` (test) test execution started
  1518. * - `test end` (test) test completed
  1519. * - `hook` (hook) hook execution started
  1520. * - `hook end` (hook) hook complete
  1521. * - `pass` (test) test passed
  1522. * - `fail` (test, err) test failed
  1523. *
  1524. * @api public
  1525. */
  1526. function Runner(suite) {
  1527. var self = this;
  1528. this._globals = [];
  1529. this.suite = suite;
  1530. this.total = suite.total();
  1531. this.failures = 0;
  1532. this.on('test end', function(test){ self.checkGlobals(test); });
  1533. this.on('hook end', function(hook){ self.checkGlobals(hook); });
  1534. this.grep(/.*/);
  1535. this.globals(Object.keys(global).concat(['errno']));
  1536. }
  1537. /**
  1538. * Inherit from `EventEmitter.prototype`.
  1539. */
  1540. Runner.prototype = new EventEmitter;
  1541. Runner.prototype.constructor = Runner;
  1542. /**
  1543. * Run tests with full titles matching `re`.
  1544. *
  1545. * @param {RegExp} re
  1546. * @return {Runner} for chaining
  1547. * @api public
  1548. */
  1549. Runner.prototype.grep = function(re){
  1550. debug('grep %s', re);
  1551. this._grep = re;
  1552. return this;
  1553. };
  1554. /**
  1555. * Allow the given `arr` of globals.
  1556. *
  1557. * @param {Array} arr
  1558. * @return {Runner} for chaining
  1559. * @api public
  1560. */
  1561. Runner.prototype.globals = function(arr){
  1562. debug('globals %j', arr);
  1563. arr.forEach(function(arr){
  1564. this._globals.push(arr);
  1565. }, this);
  1566. return this;
  1567. };
  1568. /**
  1569. * Check for global variable leaks.
  1570. *
  1571. * @api private
  1572. */
  1573. Runner.prototype.checkGlobals = function(test){
  1574. if (this.ignoreLeaks) return;
  1575. var leaks = Object.keys(global).filter(function(key){
  1576. return !~this._globals.indexOf(key);
  1577. }, this);
  1578. this._globals = this._globals.concat(leaks);
  1579. if (leaks.length > 1) {
  1580. this.fail(test, new Error('global leaks detected: ' + leaks.join(', ') + ''));
  1581. } else if (leaks.length) {
  1582. this.fail(test, new Error('global leak detected: ' + leaks[0]));
  1583. }
  1584. };
  1585. /**
  1586. * Fail the given `test`.
  1587. *
  1588. * @param {Test} test
  1589. * @param {Error} err
  1590. * @api private
  1591. */
  1592. Runner.prototype.fail = function(test, err){
  1593. ++this.failures;
  1594. test.failed = true;
  1595. this.emit('fail', test, err);
  1596. };
  1597. /**
  1598. * Fail the given `hook` with `err`.
  1599. *
  1600. * Hook failures (currently) hard-end due
  1601. * to that fact that a failing hook will
  1602. * surely cause subsequent tests to fail,
  1603. * causing jumbled reporting.
  1604. *
  1605. * @param {Hook} hook
  1606. * @param {Error} err
  1607. * @api private
  1608. */
  1609. Runner.prototype.failHook = function(hook, err){
  1610. ++this.failures;
  1611. this.fail(hook, err);
  1612. this.emit('end');
  1613. };
  1614. /**
  1615. * Run hook `name` callbacks and then invoke `fn()`.
  1616. *
  1617. * @param {String} name
  1618. * @param {Function} function
  1619. * @api private
  1620. */
  1621. Runner.prototype.hook = function(name, fn){
  1622. var suite = this.suite
  1623. , hooks = suite['_' + name]
  1624. , ms = suite._timeout
  1625. , self = this
  1626. , timer;
  1627. function next(i) {
  1628. var hook = hooks[i];
  1629. if (!hook) return fn();
  1630. self.currentRunnable = hook;
  1631. self.emit('hook', hook);
  1632. hook.on('error', function(err){
  1633. self.failHook(hook, err);
  1634. });
  1635. hook.run(function(err){
  1636. hook.removeAllListeners('error');
  1637. if (err) return self.failHook(hook, err);
  1638. self.emit('hook end', hook);
  1639. next(++i);
  1640. });
  1641. }
  1642. process.nextTick(function(){
  1643. next(0);
  1644. });
  1645. };
  1646. /**
  1647. * Run hook `name` for the given array of `suites`
  1648. * in order, and callback `fn(err)`.
  1649. *
  1650. * @param {String} name
  1651. * @param {Array} suites
  1652. * @param {Function} fn
  1653. * @api private
  1654. */
  1655. Runner.prototype.hooks = function(name, suites, fn){
  1656. var self = this
  1657. , orig = this.suite;
  1658. function next(suite) {
  1659. self.suite = suite;
  1660. if (!suite) {
  1661. self.suite = orig;
  1662. return fn();
  1663. }
  1664. self.hook(name, function(err){
  1665. if (err) {
  1666. self.suite = orig;
  1667. return fn(err);
  1668. }
  1669. next(suites.pop());
  1670. });
  1671. }
  1672. next(suites.pop());
  1673. };
  1674. /**
  1675. * Run hooks from the top level down.
  1676. *
  1677. * @param {String} name
  1678. * @param {Function} fn
  1679. * @api private
  1680. */
  1681. Runner.prototype.hookUp = function(name, fn){
  1682. var suites = [this.suite].concat(this.parents()).reverse();
  1683. this.hooks(name, suites, fn);
  1684. };
  1685. /**
  1686. * Run hooks from the bottom up.
  1687. *
  1688. * @param {String} name
  1689. * @param {Function} fn
  1690. * @api private
  1691. */
  1692. Runner.prototype.hookDown = function(name, fn){
  1693. var suites = [this.suite].concat(this.parents());
  1694. this.hooks(name, suites, fn);
  1695. };
  1696. /**
  1697. * Return an array of parent Suites from
  1698. * closest to furthest.
  1699. *
  1700. * @return {Array}
  1701. * @api private
  1702. */
  1703. Runner.prototype.parents = function(){
  1704. var suite = this.suite
  1705. , suites = [];
  1706. while (suite = suite.parent) suites.push(suite);
  1707. return suites;
  1708. };
  1709. /**
  1710. * Run the current test and callback `fn(err)`.
  1711. *
  1712. * @param {Function} fn
  1713. * @api private
  1714. */
  1715. Runner.prototype.runTest = function(fn){
  1716. var test = this.test
  1717. , self = this;
  1718. try {
  1719. test.on('error', function(err){
  1720. self.fail(test, err);
  1721. });
  1722. test.run(fn);
  1723. } catch (err) {
  1724. fn(err);
  1725. }
  1726. };
  1727. /**
  1728. * Run tests in the given `suite` and invoke
  1729. * the callback `fn()` when complete.
  1730. *
  1731. * @param {Suite} suite
  1732. * @param {Function} fn
  1733. * @api private
  1734. */
  1735. Runner.prototype.runTests = function(suite, fn){
  1736. var self = this
  1737. , tests = suite.tests
  1738. , test;
  1739. function next(err) {
  1740. // next test
  1741. test = tests.shift();
  1742. // all done
  1743. if (!test) return fn();
  1744. // grep
  1745. if (!self._grep.test(test.fullTitle())) return next();
  1746. // pending
  1747. if (test.pending) {
  1748. self.emit('pending', test);
  1749. self.emit('test end', test);
  1750. return next();
  1751. }
  1752. // execute test and hook(s)
  1753. self.emit('test', self.test = self.currentRunnable = test);
  1754. self.hookDown('beforeEach', function(){
  1755. self.runTest(function(err){
  1756. if (err) {
  1757. self.fail(test, err);
  1758. self.emit('test end', test);
  1759. return self.hookUp('afterEach', next);
  1760. }
  1761. self.emit('pass', test);
  1762. test.passed = true;
  1763. self.emit('test end', test);
  1764. self.hookUp('afterEach', next);
  1765. });
  1766. });
  1767. }
  1768. next();
  1769. };
  1770. /**
  1771. * Run the given `suite` and invoke the
  1772. * callback `fn()` when complete.
  1773. *
  1774. * @param {Suite} suite
  1775. * @param {Function} fn
  1776. * @api private
  1777. */
  1778. Runner.prototype.runSuite = function(suite, fn){
  1779. var self = this
  1780. , i = 0;
  1781. debug('run suite %s', suite.fullTitle());
  1782. this.emit('suite', this.suite = suite);
  1783. function next() {
  1784. var curr = suite.suites[i++];
  1785. if (!curr) return done();
  1786. self.runSuite(curr, next);
  1787. }
  1788. function done() {
  1789. self.suite = suite;
  1790. self.hook('afterAll', function(){
  1791. self.emit('suite end', suite);
  1792. fn();
  1793. });
  1794. }
  1795. this.hook('beforeAll', function(){
  1796. self.runTests(suite, next);
  1797. });
  1798. };
  1799. /**
  1800. * Run the root suite and invoke `fn(failures)`
  1801. * on completion.
  1802. *
  1803. * @param {Function} fn
  1804. * @return {Runner} for chaining
  1805. * @api public
  1806. */
  1807. Runner.prototype.run = function(fn){
  1808. var self = this
  1809. , fn = fn || function(){};
  1810. debug('start');
  1811. // callback
  1812. self.on('end', function(){
  1813. debug('end');
  1814. process.removeListener('uncaughtException', uncaught);
  1815. fn(self.failures);
  1816. });
  1817. // run suites
  1818. this.emit('start');
  1819. this.runSuite(this.suite, function(){
  1820. debug('finished running');
  1821. self.emit('end');
  1822. });
  1823. // uncaught exception
  1824. function uncaught(err){
  1825. debug('uncaught exception');
  1826. self.currentRunnable.clearTimeout();
  1827. self.fail(self.currentRunnable, err);
  1828. self.emit('test end', self.test);
  1829. self.emit('end');
  1830. }
  1831. process.on('uncaughtException', uncaught);
  1832. return this;
  1833. };
  1834. }); // module: runner.js
  1835. require.register("suite.js", function(module, exports, require){
  1836. /**
  1837. * Module dependencies.
  1838. */
  1839. var EventEmitter = require('browser/events').EventEmitter
  1840. , debug = require('browser/debug')('suite')
  1841. , Hook = require('./hook');
  1842. /**
  1843. * Expose `Suite`.
  1844. */
  1845. exports = module.exports = Suite;
  1846. /**
  1847. * Create a new `Suite` with the given `title`
  1848. * and parent `Suite`. When a suite with the
  1849. * same title is already present, that suite
  1850. * is returned to provide nicer reporter
  1851. * and more flexible meta-testing.
  1852. *
  1853. * @param {Suite} parent
  1854. * @param {String} title
  1855. * @return {Suite}
  1856. * @api public
  1857. */
  1858. exports.create = function(parent, title){
  1859. var suite = new Suite(title);
  1860. suite.parent = parent;
  1861. title = suite.fullTitle();
  1862. parent.addSuite(suite);
  1863. return suite;
  1864. };
  1865. /**
  1866. * Initialize a new `Suite` with the given `title`.
  1867. *
  1868. * @param {String} title
  1869. * @api private
  1870. */
  1871. function Suite(title) {
  1872. this.title = title;
  1873. this.suites = [];
  1874. this.tests = [];
  1875. this._beforeEach = [];
  1876. this._beforeAll = [];
  1877. this._afterEach = [];
  1878. this._afterAll = [];
  1879. this.root = !title;
  1880. this._timeout = 2000;
  1881. }
  1882. /**
  1883. * Inherit from `EventEmitter.prototype`.
  1884. */
  1885. Suite.prototype = new EventEmitter;
  1886. Suite.prototype.constructor = Suite;
  1887. /**
  1888. * Return a clone of this `Suite`.
  1889. *
  1890. * @return {Suite}
  1891. * @api private
  1892. */
  1893. Suite.prototype.clone = function(){
  1894. var suite = new Suite(this.title);
  1895. debug('clone');
  1896. suite.timeout(this.timeout());
  1897. return suite;
  1898. };
  1899. /**
  1900. * Set timeout `ms` or short-hand such as "2s".
  1901. *
  1902. * @param {Number|String} ms
  1903. * @return {Suite|Number} for chaining
  1904. * @api private
  1905. */
  1906. Suite.prototype.timeout = function(ms){
  1907. if (0 == arguments.length) return this._timeout;
  1908. if (String(ms).match(/s$/)) ms = parseFloat(ms) * 1000;
  1909. debug('timeout %d', ms);
  1910. this._timeout = parseInt(ms, 10);
  1911. return this;
  1912. };
  1913. /**
  1914. * Run `fn(test[, done])` before running tests.
  1915. *
  1916. * @param {Function} fn
  1917. * @return {Suite} for chaining
  1918. * @api private
  1919. */
  1920. Suite.prototype.beforeAll = function(fn){
  1921. var hook = new Hook('"before all" hook', fn);
  1922. hook.parent = this;
  1923. hook.timeout(this.timeout());
  1924. this._beforeAll.push(hook);
  1925. this.emit('beforeAll', hook);
  1926. return this;
  1927. };
  1928. /**
  1929. * Run `fn(test[, done])` after running tests.
  1930. *
  1931. * @param {Function} fn
  1932. * @return {Suite} for chaining
  1933. * @api private
  1934. */
  1935. Suite.prototype.afterAll = function(fn){
  1936. var hook = new Hook('"after all" hook', fn);
  1937. hook.parent = this;
  1938. hook.timeout(this.timeout());
  1939. this._afterAll.push(hook);
  1940. this.emit('afterAll', hook);
  1941. return this;
  1942. };
  1943. /**
  1944. * Run `fn(test[, done])` before each test case.
  1945. *
  1946. * @param {Function} fn
  1947. * @return {Suite} for chaining
  1948. * @api private
  1949. */
  1950. Suite.prototype.beforeEach = function(fn){
  1951. var hook = new Hook('"before each" hook', fn);
  1952. hook.parent = this;
  1953. hook.timeout(this.timeout());
  1954. this._beforeEach.push(hook);
  1955. this.emit('beforeEach', hook);
  1956. return this;
  1957. };
  1958. /**
  1959. * Run `fn(test[, done])` after each test case.
  1960. *
  1961. * @param {Function} fn
  1962. * @return {Suite} for chaining
  1963. * @api private
  1964. */
  1965. Suite.prototype.afterEach = function(fn){
  1966. var hook = new Hook('"after each" hook', fn);
  1967. hook.parent = this;
  1968. hook.timeout(this.timeout());
  1969. this._afterEach.push(hook);
  1970. this.emit('afterEach', hook);
  1971. return this;
  1972. };
  1973. /**
  1974. * Add a test `suite`.
  1975. *
  1976. * @param {Suite} suite
  1977. * @return {Suite} for chaining
  1978. * @api private
  1979. */
  1980. Suite.prototype.addSuite = function(suite){
  1981. suite.parent = this;
  1982. suite.timeout(this.timeout());
  1983. this.suites.push(suite);
  1984. this.emit('suite', suite);
  1985. return this;
  1986. };
  1987. /**
  1988. * Add a `test` to this suite.
  1989. *
  1990. * @param {Test} test
  1991. * @return {Suite} for chaining
  1992. * @api private
  1993. */
  1994. Suite.prototype.addTest = function(test){
  1995. test.parent = this;
  1996. test.timeout(this.timeout());
  1997. this.tests.push(test);
  1998. this.emit('test', test);
  1999. return this;
  2000. };
  2001. /**
  2002. * Return the full title generated by recursively
  2003. * concatenating the parent's full title.
  2004. *
  2005. * @return {String}
  2006. * @api public
  2007. */
  2008. Suite.prototype.fullTitle = function(){
  2009. if (this.parent) {
  2010. var full = this.parent.fullTitle();
  2011. if (full) return full + ' ' + this.title;
  2012. }
  2013. return this.title;
  2014. };
  2015. /**
  2016. * Return the total number of tests.
  2017. *
  2018. * @return {Number}
  2019. * @api public
  2020. */
  2021. Suite.prototype.total = function(){
  2022. return this.suites.reduce(function(sum, suite){
  2023. return sum + suite.total();
  2024. }, 0) + this.tests.length;
  2025. };
  2026. }); // module: suite.js
  2027. require.register("test.js", function(module, exports, require){
  2028. /**
  2029. * Module dependencies.
  2030. */
  2031. var Runnable = require('./runnable');
  2032. /**
  2033. * Expose `Test`.
  2034. */
  2035. module.exports = Test;
  2036. /**
  2037. * Initialize a new `Test` with the given `title` and callback `fn`.
  2038. *
  2039. * @param {String} title
  2040. * @param {Function} fn
  2041. * @api private
  2042. */
  2043. function Test(title, fn) {
  2044. Runnable.call(this, title, fn);
  2045. this.pending = !fn;
  2046. }
  2047. /**
  2048. * Inherit from `Runnable.prototype`.
  2049. */
  2050. Test.prototype = new Runnable;
  2051. Test.prototype.constructor = Test;
  2052. }); // module: test.js
  2053. require.register("utils.js", function(module, exports, require){
  2054. /**
  2055. * Escape special characters in the given string of html.
  2056. *
  2057. * @param {String} html
  2058. * @return {String}
  2059. * @api private
  2060. */
  2061. exports.escape = function(html) {
  2062. return String(html)
  2063. .replace(/&/g, '&amp;')
  2064. .replace(/"/g, '&quot;')
  2065. .replace(/</g, '&lt;')
  2066. .replace(/>/g, '&gt;');
  2067. };
  2068. }); // module: utils.js
  2069. require.register("watch.js", function(module, exports, require){
  2070. /**
  2071. * Module dependencies.
  2072. */
  2073. var fs = require('browser/fs')
  2074. , debug = require('browser/debug')('watch');
  2075. module.exports = function(paths, fn){
  2076. var options = { interval: 100 };
  2077. paths.forEach(function(path){
  2078. debug('watch %s', path);
  2079. fs.watchFile(path, options, function(curr, prev){
  2080. if (prev.mtime < curr.mtime) fn(path);
  2081. });
  2082. });
  2083. };
  2084. }); // module: watch.js
  2085. /**
  2086. * Node shims.
  2087. *
  2088. * These are meant only to allow
  2089. * mocha.js to run untouched, not
  2090. * to allow running node code in
  2091. * the browser.
  2092. */
  2093. process = {};
  2094. process.exit = function(status){};
  2095. process.stdout = {};
  2096. global = this;
  2097. process.nextTick = function(fn){ fn(); };
  2098. process.removeListener = function(ev){
  2099. if ('uncaughtException' == ev) {
  2100. window.onerror = null;
  2101. }
  2102. };
  2103. process.on = function(ev, fn){
  2104. if ('uncaughtException' == ev) {
  2105. window.onerror = fn;
  2106. }
  2107. };
  2108. mocha = require('mocha');
  2109. // boot
  2110. ;(function(){
  2111. var suite = new mocha.Suite;
  2112. var Reporter = mocha.reporters.HTML;
  2113. function parse(qs) {
  2114. return qs
  2115. .replace('?', '')
  2116. .split('&')
  2117. .reduce(function(obj, pair){
  2118. var i = pair.indexOf('=')
  2119. , key = pair.slice(0, i)
  2120. , val = pair.slice(++i);
  2121. obj[key] = decodeURIComponent(val);
  2122. return obj;
  2123. }, {});
  2124. }
  2125. mocha.setup = function(ui){
  2126. ui = mocha.interfaces[ui];
  2127. if (!ui) throw new Error('invalid mocha interface "' + ui + '"');
  2128. ui(suite);
  2129. suite.emit('pre-require', global);
  2130. };
  2131. mocha.run = function(){
  2132. suite.emit('run');
  2133. var runner = new mocha.Runner(suite);
  2134. var reporter = new Reporter(runner);
  2135. var query = parse(window.location.search || "");
  2136. if (query.grep) runner.grep(new RegExp(query.grep));
  2137. return runner.run();
  2138. };
  2139. })();
  2140. })();