mocha.js 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069
  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.charAt(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/diff.js", function(module, exports, require){
  46. /* See LICENSE file for terms of use */
  47. /*
  48. * Text diff implementation.
  49. *
  50. * This library supports the following APIS:
  51. * JsDiff.diffChars: Character by character diff
  52. * JsDiff.diffWords: Word (as defined by \b regex) diff which ignores whitespace
  53. * JsDiff.diffLines: Line based diff
  54. *
  55. * JsDiff.diffCss: Diff targeted at CSS content
  56. *
  57. * These methods are based on the implementation proposed in
  58. * "An O(ND) Difference Algorithm and its Variations" (Myers, 1986).
  59. * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927
  60. */
  61. var JsDiff = (function() {
  62. /*jshint maxparams: 5*/
  63. function clonePath(path) {
  64. return { newPos: path.newPos, components: path.components.slice(0) };
  65. }
  66. function removeEmpty(array) {
  67. var ret = [];
  68. for (var i = 0; i < array.length; i++) {
  69. if (array[i]) {
  70. ret.push(array[i]);
  71. }
  72. }
  73. return ret;
  74. }
  75. function escapeHTML(s) {
  76. var n = s;
  77. n = n.replace(/&/g, '&amp;');
  78. n = n.replace(/</g, '&lt;');
  79. n = n.replace(/>/g, '&gt;');
  80. n = n.replace(/"/g, '&quot;');
  81. return n;
  82. }
  83. var Diff = function(ignoreWhitespace) {
  84. this.ignoreWhitespace = ignoreWhitespace;
  85. };
  86. Diff.prototype = {
  87. diff: function(oldString, newString) {
  88. // Handle the identity case (this is due to unrolling editLength == 0
  89. if (newString === oldString) {
  90. return [{ value: newString }];
  91. }
  92. if (!newString) {
  93. return [{ value: oldString, removed: true }];
  94. }
  95. if (!oldString) {
  96. return [{ value: newString, added: true }];
  97. }
  98. newString = this.tokenize(newString);
  99. oldString = this.tokenize(oldString);
  100. var newLen = newString.length, oldLen = oldString.length;
  101. var maxEditLength = newLen + oldLen;
  102. var bestPath = [{ newPos: -1, components: [] }];
  103. // Seed editLength = 0
  104. var oldPos = this.extractCommon(bestPath[0], newString, oldString, 0);
  105. if (bestPath[0].newPos+1 >= newLen && oldPos+1 >= oldLen) {
  106. return bestPath[0].components;
  107. }
  108. for (var editLength = 1; editLength <= maxEditLength; editLength++) {
  109. for (var diagonalPath = -1*editLength; diagonalPath <= editLength; diagonalPath+=2) {
  110. var basePath;
  111. var addPath = bestPath[diagonalPath-1],
  112. removePath = bestPath[diagonalPath+1];
  113. oldPos = (removePath ? removePath.newPos : 0) - diagonalPath;
  114. if (addPath) {
  115. // No one else is going to attempt to use this value, clear it
  116. bestPath[diagonalPath-1] = undefined;
  117. }
  118. var canAdd = addPath && addPath.newPos+1 < newLen;
  119. var canRemove = removePath && 0 <= oldPos && oldPos < oldLen;
  120. if (!canAdd && !canRemove) {
  121. bestPath[diagonalPath] = undefined;
  122. continue;
  123. }
  124. // Select the diagonal that we want to branch from. We select the prior
  125. // path whose position in the new string is the farthest from the origin
  126. // and does not pass the bounds of the diff graph
  127. if (!canAdd || (canRemove && addPath.newPos < removePath.newPos)) {
  128. basePath = clonePath(removePath);
  129. this.pushComponent(basePath.components, oldString[oldPos], undefined, true);
  130. } else {
  131. basePath = clonePath(addPath);
  132. basePath.newPos++;
  133. this.pushComponent(basePath.components, newString[basePath.newPos], true, undefined);
  134. }
  135. var oldPos = this.extractCommon(basePath, newString, oldString, diagonalPath);
  136. if (basePath.newPos+1 >= newLen && oldPos+1 >= oldLen) {
  137. return basePath.components;
  138. } else {
  139. bestPath[diagonalPath] = basePath;
  140. }
  141. }
  142. }
  143. },
  144. pushComponent: function(components, value, added, removed) {
  145. var last = components[components.length-1];
  146. if (last && last.added === added && last.removed === removed) {
  147. // We need to clone here as the component clone operation is just
  148. // as shallow array clone
  149. components[components.length-1] =
  150. {value: this.join(last.value, value), added: added, removed: removed };
  151. } else {
  152. components.push({value: value, added: added, removed: removed });
  153. }
  154. },
  155. extractCommon: function(basePath, newString, oldString, diagonalPath) {
  156. var newLen = newString.length,
  157. oldLen = oldString.length,
  158. newPos = basePath.newPos,
  159. oldPos = newPos - diagonalPath;
  160. while (newPos+1 < newLen && oldPos+1 < oldLen && this.equals(newString[newPos+1], oldString[oldPos+1])) {
  161. newPos++;
  162. oldPos++;
  163. this.pushComponent(basePath.components, newString[newPos], undefined, undefined);
  164. }
  165. basePath.newPos = newPos;
  166. return oldPos;
  167. },
  168. equals: function(left, right) {
  169. var reWhitespace = /\S/;
  170. if (this.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right)) {
  171. return true;
  172. } else {
  173. return left === right;
  174. }
  175. },
  176. join: function(left, right) {
  177. return left + right;
  178. },
  179. tokenize: function(value) {
  180. return value;
  181. }
  182. };
  183. var CharDiff = new Diff();
  184. var WordDiff = new Diff(true);
  185. var WordWithSpaceDiff = new Diff();
  186. WordDiff.tokenize = WordWithSpaceDiff.tokenize = function(value) {
  187. return removeEmpty(value.split(/(\s+|\b)/));
  188. };
  189. var CssDiff = new Diff(true);
  190. CssDiff.tokenize = function(value) {
  191. return removeEmpty(value.split(/([{}:;,]|\s+)/));
  192. };
  193. var LineDiff = new Diff();
  194. LineDiff.tokenize = function(value) {
  195. return value.split(/^/m);
  196. };
  197. return {
  198. Diff: Diff,
  199. diffChars: function(oldStr, newStr) { return CharDiff.diff(oldStr, newStr); },
  200. diffWords: function(oldStr, newStr) { return WordDiff.diff(oldStr, newStr); },
  201. diffWordsWithSpace: function(oldStr, newStr) { return WordWithSpaceDiff.diff(oldStr, newStr); },
  202. diffLines: function(oldStr, newStr) { return LineDiff.diff(oldStr, newStr); },
  203. diffCss: function(oldStr, newStr) { return CssDiff.diff(oldStr, newStr); },
  204. createPatch: function(fileName, oldStr, newStr, oldHeader, newHeader) {
  205. var ret = [];
  206. ret.push('Index: ' + fileName);
  207. ret.push('===================================================================');
  208. ret.push('--- ' + fileName + (typeof oldHeader === 'undefined' ? '' : '\t' + oldHeader));
  209. ret.push('+++ ' + fileName + (typeof newHeader === 'undefined' ? '' : '\t' + newHeader));
  210. var diff = LineDiff.diff(oldStr, newStr);
  211. if (!diff[diff.length-1].value) {
  212. diff.pop(); // Remove trailing newline add
  213. }
  214. diff.push({value: '', lines: []}); // Append an empty value to make cleanup easier
  215. function contextLines(lines) {
  216. return lines.map(function(entry) { return ' ' + entry; });
  217. }
  218. function eofNL(curRange, i, current) {
  219. var last = diff[diff.length-2],
  220. isLast = i === diff.length-2,
  221. isLastOfType = i === diff.length-3 && (current.added !== last.added || current.removed !== last.removed);
  222. // Figure out if this is the last line for the given file and missing NL
  223. if (!/\n$/.test(current.value) && (isLast || isLastOfType)) {
  224. curRange.push('\\ No newline at end of file');
  225. }
  226. }
  227. var oldRangeStart = 0, newRangeStart = 0, curRange = [],
  228. oldLine = 1, newLine = 1;
  229. for (var i = 0; i < diff.length; i++) {
  230. var current = diff[i],
  231. lines = current.lines || current.value.replace(/\n$/, '').split('\n');
  232. current.lines = lines;
  233. if (current.added || current.removed) {
  234. if (!oldRangeStart) {
  235. var prev = diff[i-1];
  236. oldRangeStart = oldLine;
  237. newRangeStart = newLine;
  238. if (prev) {
  239. curRange = contextLines(prev.lines.slice(-4));
  240. oldRangeStart -= curRange.length;
  241. newRangeStart -= curRange.length;
  242. }
  243. }
  244. curRange.push.apply(curRange, lines.map(function(entry) { return (current.added?'+':'-') + entry; }));
  245. eofNL(curRange, i, current);
  246. if (current.added) {
  247. newLine += lines.length;
  248. } else {
  249. oldLine += lines.length;
  250. }
  251. } else {
  252. if (oldRangeStart) {
  253. // Close out any changes that have been output (or join overlapping)
  254. if (lines.length <= 8 && i < diff.length-2) {
  255. // Overlapping
  256. curRange.push.apply(curRange, contextLines(lines));
  257. } else {
  258. // end the range and output
  259. var contextSize = Math.min(lines.length, 4);
  260. ret.push(
  261. '@@ -' + oldRangeStart + ',' + (oldLine-oldRangeStart+contextSize)
  262. + ' +' + newRangeStart + ',' + (newLine-newRangeStart+contextSize)
  263. + ' @@');
  264. ret.push.apply(ret, curRange);
  265. ret.push.apply(ret, contextLines(lines.slice(0, contextSize)));
  266. if (lines.length <= 4) {
  267. eofNL(ret, i, current);
  268. }
  269. oldRangeStart = 0; newRangeStart = 0; curRange = [];
  270. }
  271. }
  272. oldLine += lines.length;
  273. newLine += lines.length;
  274. }
  275. }
  276. return ret.join('\n') + '\n';
  277. },
  278. applyPatch: function(oldStr, uniDiff) {
  279. var diffstr = uniDiff.split('\n');
  280. var diff = [];
  281. var remEOFNL = false,
  282. addEOFNL = false;
  283. for (var i = (diffstr[0][0]==='I'?4:0); i < diffstr.length; i++) {
  284. if(diffstr[i][0] === '@') {
  285. var meh = diffstr[i].split(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/);
  286. diff.unshift({
  287. start:meh[3],
  288. oldlength:meh[2],
  289. oldlines:[],
  290. newlength:meh[4],
  291. newlines:[]
  292. });
  293. } else if(diffstr[i][0] === '+') {
  294. diff[0].newlines.push(diffstr[i].substr(1));
  295. } else if(diffstr[i][0] === '-') {
  296. diff[0].oldlines.push(diffstr[i].substr(1));
  297. } else if(diffstr[i][0] === ' ') {
  298. diff[0].newlines.push(diffstr[i].substr(1));
  299. diff[0].oldlines.push(diffstr[i].substr(1));
  300. } else if(diffstr[i][0] === '\\') {
  301. if (diffstr[i-1][0] === '+') {
  302. remEOFNL = true;
  303. } else if(diffstr[i-1][0] === '-') {
  304. addEOFNL = true;
  305. }
  306. }
  307. }
  308. var str = oldStr.split('\n');
  309. for (var i = diff.length - 1; i >= 0; i--) {
  310. var d = diff[i];
  311. for (var j = 0; j < d.oldlength; j++) {
  312. if(str[d.start-1+j] !== d.oldlines[j]) {
  313. return false;
  314. }
  315. }
  316. Array.prototype.splice.apply(str,[d.start-1,+d.oldlength].concat(d.newlines));
  317. }
  318. if (remEOFNL) {
  319. while (!str[str.length-1]) {
  320. str.pop();
  321. }
  322. } else if (addEOFNL) {
  323. str.push('');
  324. }
  325. return str.join('\n');
  326. },
  327. convertChangesToXML: function(changes){
  328. var ret = [];
  329. for ( var i = 0; i < changes.length; i++) {
  330. var change = changes[i];
  331. if (change.added) {
  332. ret.push('<ins>');
  333. } else if (change.removed) {
  334. ret.push('<del>');
  335. }
  336. ret.push(escapeHTML(change.value));
  337. if (change.added) {
  338. ret.push('</ins>');
  339. } else if (change.removed) {
  340. ret.push('</del>');
  341. }
  342. }
  343. return ret.join('');
  344. },
  345. // See: http://code.google.com/p/google-diff-match-patch/wiki/API
  346. convertChangesToDMP: function(changes){
  347. var ret = [], change;
  348. for ( var i = 0; i < changes.length; i++) {
  349. change = changes[i];
  350. ret.push([(change.added ? 1 : change.removed ? -1 : 0), change.value]);
  351. }
  352. return ret;
  353. }
  354. };
  355. })();
  356. if (typeof module !== 'undefined') {
  357. module.exports = JsDiff;
  358. }
  359. }); // module: browser/diff.js
  360. require.register("browser/escape-string-regexp.js", function(module, exports, require){
  361. 'use strict';
  362. var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
  363. module.exports = function (str) {
  364. if (typeof str !== 'string') {
  365. throw new TypeError('Expected a string');
  366. }
  367. return str.replace(matchOperatorsRe, '\\$&');
  368. };
  369. }); // module: browser/escape-string-regexp.js
  370. require.register("browser/events.js", function(module, exports, require){
  371. /**
  372. * Module exports.
  373. */
  374. exports.EventEmitter = EventEmitter;
  375. /**
  376. * Check if `obj` is an array.
  377. */
  378. function isArray(obj) {
  379. return '[object Array]' == {}.toString.call(obj);
  380. }
  381. /**
  382. * Event emitter constructor.
  383. *
  384. * @api public
  385. */
  386. function EventEmitter(){};
  387. /**
  388. * Adds a listener.
  389. *
  390. * @api public
  391. */
  392. EventEmitter.prototype.on = function (name, fn) {
  393. if (!this.$events) {
  394. this.$events = {};
  395. }
  396. if (!this.$events[name]) {
  397. this.$events[name] = fn;
  398. } else if (isArray(this.$events[name])) {
  399. this.$events[name].push(fn);
  400. } else {
  401. this.$events[name] = [this.$events[name], fn];
  402. }
  403. return this;
  404. };
  405. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  406. /**
  407. * Adds a volatile listener.
  408. *
  409. * @api public
  410. */
  411. EventEmitter.prototype.once = function (name, fn) {
  412. var self = this;
  413. function on () {
  414. self.removeListener(name, on);
  415. fn.apply(this, arguments);
  416. };
  417. on.listener = fn;
  418. this.on(name, on);
  419. return this;
  420. };
  421. /**
  422. * Removes a listener.
  423. *
  424. * @api public
  425. */
  426. EventEmitter.prototype.removeListener = function (name, fn) {
  427. if (this.$events && this.$events[name]) {
  428. var list = this.$events[name];
  429. if (isArray(list)) {
  430. var pos = -1;
  431. for (var i = 0, l = list.length; i < l; i++) {
  432. if (list[i] === fn || (list[i].listener && list[i].listener === fn)) {
  433. pos = i;
  434. break;
  435. }
  436. }
  437. if (pos < 0) {
  438. return this;
  439. }
  440. list.splice(pos, 1);
  441. if (!list.length) {
  442. delete this.$events[name];
  443. }
  444. } else if (list === fn || (list.listener && list.listener === fn)) {
  445. delete this.$events[name];
  446. }
  447. }
  448. return this;
  449. };
  450. /**
  451. * Removes all listeners for an event.
  452. *
  453. * @api public
  454. */
  455. EventEmitter.prototype.removeAllListeners = function (name) {
  456. if (name === undefined) {
  457. this.$events = {};
  458. return this;
  459. }
  460. if (this.$events && this.$events[name]) {
  461. this.$events[name] = null;
  462. }
  463. return this;
  464. };
  465. /**
  466. * Gets all listeners for a certain event.
  467. *
  468. * @api public
  469. */
  470. EventEmitter.prototype.listeners = function (name) {
  471. if (!this.$events) {
  472. this.$events = {};
  473. }
  474. if (!this.$events[name]) {
  475. this.$events[name] = [];
  476. }
  477. if (!isArray(this.$events[name])) {
  478. this.$events[name] = [this.$events[name]];
  479. }
  480. return this.$events[name];
  481. };
  482. /**
  483. * Emits an event.
  484. *
  485. * @api public
  486. */
  487. EventEmitter.prototype.emit = function (name) {
  488. if (!this.$events) {
  489. return false;
  490. }
  491. var handler = this.$events[name];
  492. if (!handler) {
  493. return false;
  494. }
  495. var args = [].slice.call(arguments, 1);
  496. if ('function' == typeof handler) {
  497. handler.apply(this, args);
  498. } else if (isArray(handler)) {
  499. var listeners = handler.slice();
  500. for (var i = 0, l = listeners.length; i < l; i++) {
  501. listeners[i].apply(this, args);
  502. }
  503. } else {
  504. return false;
  505. }
  506. return true;
  507. };
  508. }); // module: browser/events.js
  509. require.register("browser/fs.js", function(module, exports, require){
  510. }); // module: browser/fs.js
  511. require.register("browser/glob.js", function(module, exports, require){
  512. }); // module: browser/glob.js
  513. require.register("browser/path.js", function(module, exports, require){
  514. }); // module: browser/path.js
  515. require.register("browser/progress.js", function(module, exports, require){
  516. /**
  517. * Expose `Progress`.
  518. */
  519. module.exports = Progress;
  520. /**
  521. * Initialize a new `Progress` indicator.
  522. */
  523. function Progress() {
  524. this.percent = 0;
  525. this.size(0);
  526. this.fontSize(11);
  527. this.font('helvetica, arial, sans-serif');
  528. }
  529. /**
  530. * Set progress size to `n`.
  531. *
  532. * @param {Number} n
  533. * @return {Progress} for chaining
  534. * @api public
  535. */
  536. Progress.prototype.size = function(n){
  537. this._size = n;
  538. return this;
  539. };
  540. /**
  541. * Set text to `str`.
  542. *
  543. * @param {String} str
  544. * @return {Progress} for chaining
  545. * @api public
  546. */
  547. Progress.prototype.text = function(str){
  548. this._text = str;
  549. return this;
  550. };
  551. /**
  552. * Set font size to `n`.
  553. *
  554. * @param {Number} n
  555. * @return {Progress} for chaining
  556. * @api public
  557. */
  558. Progress.prototype.fontSize = function(n){
  559. this._fontSize = n;
  560. return this;
  561. };
  562. /**
  563. * Set font `family`.
  564. *
  565. * @param {String} family
  566. * @return {Progress} for chaining
  567. */
  568. Progress.prototype.font = function(family){
  569. this._font = family;
  570. return this;
  571. };
  572. /**
  573. * Update percentage to `n`.
  574. *
  575. * @param {Number} n
  576. * @return {Progress} for chaining
  577. */
  578. Progress.prototype.update = function(n){
  579. this.percent = n;
  580. return this;
  581. };
  582. /**
  583. * Draw on `ctx`.
  584. *
  585. * @param {CanvasRenderingContext2d} ctx
  586. * @return {Progress} for chaining
  587. */
  588. Progress.prototype.draw = function(ctx){
  589. try {
  590. var percent = Math.min(this.percent, 100)
  591. , size = this._size
  592. , half = size / 2
  593. , x = half
  594. , y = half
  595. , rad = half - 1
  596. , fontSize = this._fontSize;
  597. ctx.font = fontSize + 'px ' + this._font;
  598. var angle = Math.PI * 2 * (percent / 100);
  599. ctx.clearRect(0, 0, size, size);
  600. // outer circle
  601. ctx.strokeStyle = '#9f9f9f';
  602. ctx.beginPath();
  603. ctx.arc(x, y, rad, 0, angle, false);
  604. ctx.stroke();
  605. // inner circle
  606. ctx.strokeStyle = '#eee';
  607. ctx.beginPath();
  608. ctx.arc(x, y, rad - 1, 0, angle, true);
  609. ctx.stroke();
  610. // text
  611. var text = this._text || (percent | 0) + '%'
  612. , w = ctx.measureText(text).width;
  613. ctx.fillText(
  614. text
  615. , x - w / 2 + 1
  616. , y + fontSize / 2 - 1);
  617. } catch (ex) {} //don't fail if we can't render progress
  618. return this;
  619. };
  620. }); // module: browser/progress.js
  621. require.register("browser/tty.js", function(module, exports, require){
  622. exports.isatty = function(){
  623. return true;
  624. };
  625. exports.getWindowSize = function(){
  626. if ('innerHeight' in global) {
  627. return [global.innerHeight, global.innerWidth];
  628. } else {
  629. // In a Web Worker, the DOM Window is not available.
  630. return [640, 480];
  631. }
  632. };
  633. }); // module: browser/tty.js
  634. require.register("context.js", function(module, exports, require){
  635. /**
  636. * Expose `Context`.
  637. */
  638. module.exports = Context;
  639. /**
  640. * Initialize a new `Context`.
  641. *
  642. * @api private
  643. */
  644. function Context(){}
  645. /**
  646. * Set or get the context `Runnable` to `runnable`.
  647. *
  648. * @param {Runnable} runnable
  649. * @return {Context}
  650. * @api private
  651. */
  652. Context.prototype.runnable = function(runnable){
  653. if (0 == arguments.length) return this._runnable;
  654. this.test = this._runnable = runnable;
  655. return this;
  656. };
  657. /**
  658. * Set test timeout `ms`.
  659. *
  660. * @param {Number} ms
  661. * @return {Context} self
  662. * @api private
  663. */
  664. Context.prototype.timeout = function(ms){
  665. if (arguments.length === 0) return this.runnable().timeout();
  666. this.runnable().timeout(ms);
  667. return this;
  668. };
  669. /**
  670. * Set test timeout `enabled`.
  671. *
  672. * @param {Boolean} enabled
  673. * @return {Context} self
  674. * @api private
  675. */
  676. Context.prototype.enableTimeouts = function (enabled) {
  677. this.runnable().enableTimeouts(enabled);
  678. return this;
  679. };
  680. /**
  681. * Set test slowness threshold `ms`.
  682. *
  683. * @param {Number} ms
  684. * @return {Context} self
  685. * @api private
  686. */
  687. Context.prototype.slow = function(ms){
  688. this.runnable().slow(ms);
  689. return this;
  690. };
  691. /**
  692. * Inspect the context void of `._runnable`.
  693. *
  694. * @return {String}
  695. * @api private
  696. */
  697. Context.prototype.inspect = function(){
  698. return JSON.stringify(this, function(key, val){
  699. if ('_runnable' == key) return;
  700. if ('test' == key) return;
  701. return val;
  702. }, 2);
  703. };
  704. }); // module: context.js
  705. require.register("hook.js", function(module, exports, require){
  706. /**
  707. * Module dependencies.
  708. */
  709. var Runnable = require('./runnable');
  710. /**
  711. * Expose `Hook`.
  712. */
  713. module.exports = Hook;
  714. /**
  715. * Initialize a new `Hook` with the given `title` and callback `fn`.
  716. *
  717. * @param {String} title
  718. * @param {Function} fn
  719. * @api private
  720. */
  721. function Hook(title, fn) {
  722. Runnable.call(this, title, fn);
  723. this.type = 'hook';
  724. }
  725. /**
  726. * Inherit from `Runnable.prototype`.
  727. */
  728. function F(){};
  729. F.prototype = Runnable.prototype;
  730. Hook.prototype = new F;
  731. Hook.prototype.constructor = Hook;
  732. /**
  733. * Get or set the test `err`.
  734. *
  735. * @param {Error} err
  736. * @return {Error}
  737. * @api public
  738. */
  739. Hook.prototype.error = function(err){
  740. if (0 == arguments.length) {
  741. var err = this._error;
  742. this._error = null;
  743. return err;
  744. }
  745. this._error = err;
  746. };
  747. }); // module: hook.js
  748. require.register("interfaces/bdd.js", function(module, exports, require){
  749. /**
  750. * Module dependencies.
  751. */
  752. var Suite = require('../suite')
  753. , Test = require('../test')
  754. , utils = require('../utils')
  755. , escapeRe = require('browser/escape-string-regexp');
  756. /**
  757. * BDD-style interface:
  758. *
  759. * describe('Array', function(){
  760. * describe('#indexOf()', function(){
  761. * it('should return -1 when not present', function(){
  762. *
  763. * });
  764. *
  765. * it('should return the index when present', function(){
  766. *
  767. * });
  768. * });
  769. * });
  770. *
  771. */
  772. module.exports = function(suite){
  773. var suites = [suite];
  774. suite.on('pre-require', function(context, file, mocha){
  775. /**
  776. * Execute before running tests.
  777. */
  778. context.before = function(name, fn){
  779. suites[0].beforeAll(name, fn);
  780. };
  781. /**
  782. * Execute after running tests.
  783. */
  784. context.after = function(name, fn){
  785. suites[0].afterAll(name, fn);
  786. };
  787. /**
  788. * Execute before each test case.
  789. */
  790. context.beforeEach = function(name, fn){
  791. suites[0].beforeEach(name, fn);
  792. };
  793. /**
  794. * Execute after each test case.
  795. */
  796. context.afterEach = function(name, fn){
  797. suites[0].afterEach(name, fn);
  798. };
  799. /**
  800. * Describe a "suite" with the given `title`
  801. * and callback `fn` containing nested suites
  802. * and/or tests.
  803. */
  804. context.describe = context.context = function(title, fn){
  805. var suite = Suite.create(suites[0], title);
  806. suite.file = file;
  807. suites.unshift(suite);
  808. fn.call(suite);
  809. suites.shift();
  810. return suite;
  811. };
  812. /**
  813. * Pending describe.
  814. */
  815. context.xdescribe =
  816. context.xcontext =
  817. context.describe.skip = function(title, fn){
  818. var suite = Suite.create(suites[0], title);
  819. suite.pending = true;
  820. suites.unshift(suite);
  821. fn.call(suite);
  822. suites.shift();
  823. };
  824. /**
  825. * Exclusive suite.
  826. */
  827. context.describe.only = function(title, fn){
  828. var suite = context.describe(title, fn);
  829. mocha.grep(suite.fullTitle());
  830. return suite;
  831. };
  832. /**
  833. * Describe a specification or test-case
  834. * with the given `title` and callback `fn`
  835. * acting as a thunk.
  836. */
  837. context.it = context.specify = function(title, fn){
  838. var suite = suites[0];
  839. if (suite.pending) fn = null;
  840. var test = new Test(title, fn);
  841. test.file = file;
  842. suite.addTest(test);
  843. return test;
  844. };
  845. /**
  846. * Exclusive test-case.
  847. */
  848. context.it.only = function(title, fn){
  849. var test = context.it(title, fn);
  850. var reString = '^' + escapeRe(test.fullTitle()) + '$';
  851. mocha.grep(new RegExp(reString));
  852. return test;
  853. };
  854. /**
  855. * Pending test case.
  856. */
  857. context.xit =
  858. context.xspecify =
  859. context.it.skip = function(title){
  860. context.it(title);
  861. };
  862. });
  863. };
  864. }); // module: interfaces/bdd.js
  865. require.register("interfaces/exports.js", function(module, exports, require){
  866. /**
  867. * Module dependencies.
  868. */
  869. var Suite = require('../suite')
  870. , Test = require('../test');
  871. /**
  872. * TDD-style interface:
  873. *
  874. * exports.Array = {
  875. * '#indexOf()': {
  876. * 'should return -1 when the value is not present': function(){
  877. *
  878. * },
  879. *
  880. * 'should return the correct index when the value is present': function(){
  881. *
  882. * }
  883. * }
  884. * };
  885. *
  886. */
  887. module.exports = function(suite){
  888. var suites = [suite];
  889. suite.on('require', visit);
  890. function visit(obj, file) {
  891. var suite;
  892. for (var key in obj) {
  893. if ('function' == typeof obj[key]) {
  894. var fn = obj[key];
  895. switch (key) {
  896. case 'before':
  897. suites[0].beforeAll(fn);
  898. break;
  899. case 'after':
  900. suites[0].afterAll(fn);
  901. break;
  902. case 'beforeEach':
  903. suites[0].beforeEach(fn);
  904. break;
  905. case 'afterEach':
  906. suites[0].afterEach(fn);
  907. break;
  908. default:
  909. var test = new Test(key, fn);
  910. test.file = file;
  911. suites[0].addTest(test);
  912. }
  913. } else {
  914. suite = Suite.create(suites[0], key);
  915. suites.unshift(suite);
  916. visit(obj[key]);
  917. suites.shift();
  918. }
  919. }
  920. }
  921. };
  922. }); // module: interfaces/exports.js
  923. require.register("interfaces/index.js", function(module, exports, require){
  924. exports.bdd = require('./bdd');
  925. exports.tdd = require('./tdd');
  926. exports.qunit = require('./qunit');
  927. exports.exports = require('./exports');
  928. }); // module: interfaces/index.js
  929. require.register("interfaces/qunit.js", function(module, exports, require){
  930. /**
  931. * Module dependencies.
  932. */
  933. var Suite = require('../suite')
  934. , Test = require('../test')
  935. , escapeRe = require('browser/escape-string-regexp')
  936. , utils = require('../utils');
  937. /**
  938. * QUnit-style interface:
  939. *
  940. * suite('Array');
  941. *
  942. * test('#length', function(){
  943. * var arr = [1,2,3];
  944. * ok(arr.length == 3);
  945. * });
  946. *
  947. * test('#indexOf()', function(){
  948. * var arr = [1,2,3];
  949. * ok(arr.indexOf(1) == 0);
  950. * ok(arr.indexOf(2) == 1);
  951. * ok(arr.indexOf(3) == 2);
  952. * });
  953. *
  954. * suite('String');
  955. *
  956. * test('#length', function(){
  957. * ok('foo'.length == 3);
  958. * });
  959. *
  960. */
  961. module.exports = function(suite){
  962. var suites = [suite];
  963. suite.on('pre-require', function(context, file, mocha){
  964. /**
  965. * Execute before running tests.
  966. */
  967. context.before = function(name, fn){
  968. suites[0].beforeAll(name, fn);
  969. };
  970. /**
  971. * Execute after running tests.
  972. */
  973. context.after = function(name, fn){
  974. suites[0].afterAll(name, fn);
  975. };
  976. /**
  977. * Execute before each test case.
  978. */
  979. context.beforeEach = function(name, fn){
  980. suites[0].beforeEach(name, fn);
  981. };
  982. /**
  983. * Execute after each test case.
  984. */
  985. context.afterEach = function(name, fn){
  986. suites[0].afterEach(name, fn);
  987. };
  988. /**
  989. * Describe a "suite" with the given `title`.
  990. */
  991. context.suite = function(title){
  992. if (suites.length > 1) suites.shift();
  993. var suite = Suite.create(suites[0], title);
  994. suite.file = file;
  995. suites.unshift(suite);
  996. return suite;
  997. };
  998. /**
  999. * Exclusive test-case.
  1000. */
  1001. context.suite.only = function(title, fn){
  1002. var suite = context.suite(title, fn);
  1003. mocha.grep(suite.fullTitle());
  1004. };
  1005. /**
  1006. * Describe a specification or test-case
  1007. * with the given `title` and callback `fn`
  1008. * acting as a thunk.
  1009. */
  1010. context.test = function(title, fn){
  1011. var test = new Test(title, fn);
  1012. test.file = file;
  1013. suites[0].addTest(test);
  1014. return test;
  1015. };
  1016. /**
  1017. * Exclusive test-case.
  1018. */
  1019. context.test.only = function(title, fn){
  1020. var test = context.test(title, fn);
  1021. var reString = '^' + escapeRe(test.fullTitle()) + '$';
  1022. mocha.grep(new RegExp(reString));
  1023. };
  1024. /**
  1025. * Pending test case.
  1026. */
  1027. context.test.skip = function(title){
  1028. context.test(title);
  1029. };
  1030. });
  1031. };
  1032. }); // module: interfaces/qunit.js
  1033. require.register("interfaces/tdd.js", function(module, exports, require){
  1034. /**
  1035. * Module dependencies.
  1036. */
  1037. var Suite = require('../suite')
  1038. , Test = require('../test')
  1039. , escapeRe = require('browser/escape-string-regexp')
  1040. , utils = require('../utils');
  1041. /**
  1042. * TDD-style interface:
  1043. *
  1044. * suite('Array', function(){
  1045. * suite('#indexOf()', function(){
  1046. * suiteSetup(function(){
  1047. *
  1048. * });
  1049. *
  1050. * test('should return -1 when not present', function(){
  1051. *
  1052. * });
  1053. *
  1054. * test('should return the index when present', function(){
  1055. *
  1056. * });
  1057. *
  1058. * suiteTeardown(function(){
  1059. *
  1060. * });
  1061. * });
  1062. * });
  1063. *
  1064. */
  1065. module.exports = function(suite){
  1066. var suites = [suite];
  1067. suite.on('pre-require', function(context, file, mocha){
  1068. /**
  1069. * Execute before each test case.
  1070. */
  1071. context.setup = function(name, fn){
  1072. suites[0].beforeEach(name, fn);
  1073. };
  1074. /**
  1075. * Execute after each test case.
  1076. */
  1077. context.teardown = function(name, fn){
  1078. suites[0].afterEach(name, fn);
  1079. };
  1080. /**
  1081. * Execute before the suite.
  1082. */
  1083. context.suiteSetup = function(name, fn){
  1084. suites[0].beforeAll(name, fn);
  1085. };
  1086. /**
  1087. * Execute after the suite.
  1088. */
  1089. context.suiteTeardown = function(name, fn){
  1090. suites[0].afterAll(name, fn);
  1091. };
  1092. /**
  1093. * Describe a "suite" with the given `title`
  1094. * and callback `fn` containing nested suites
  1095. * and/or tests.
  1096. */
  1097. context.suite = function(title, fn){
  1098. var suite = Suite.create(suites[0], title);
  1099. suite.file = file;
  1100. suites.unshift(suite);
  1101. fn.call(suite);
  1102. suites.shift();
  1103. return suite;
  1104. };
  1105. /**
  1106. * Pending suite.
  1107. */
  1108. context.suite.skip = function(title, fn) {
  1109. var suite = Suite.create(suites[0], title);
  1110. suite.pending = true;
  1111. suites.unshift(suite);
  1112. fn.call(suite);
  1113. suites.shift();
  1114. };
  1115. /**
  1116. * Exclusive test-case.
  1117. */
  1118. context.suite.only = function(title, fn){
  1119. var suite = context.suite(title, fn);
  1120. mocha.grep(suite.fullTitle());
  1121. };
  1122. /**
  1123. * Describe a specification or test-case
  1124. * with the given `title` and callback `fn`
  1125. * acting as a thunk.
  1126. */
  1127. context.test = function(title, fn){
  1128. var suite = suites[0];
  1129. if (suite.pending) fn = null;
  1130. var test = new Test(title, fn);
  1131. test.file = file;
  1132. suite.addTest(test);
  1133. return test;
  1134. };
  1135. /**
  1136. * Exclusive test-case.
  1137. */
  1138. context.test.only = function(title, fn){
  1139. var test = context.test(title, fn);
  1140. var reString = '^' + escapeRe(test.fullTitle()) + '$';
  1141. mocha.grep(new RegExp(reString));
  1142. };
  1143. /**
  1144. * Pending test case.
  1145. */
  1146. context.test.skip = function(title){
  1147. context.test(title);
  1148. };
  1149. });
  1150. };
  1151. }); // module: interfaces/tdd.js
  1152. require.register("mocha.js", function(module, exports, require){
  1153. /*!
  1154. * mocha
  1155. * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
  1156. * MIT Licensed
  1157. */
  1158. /**
  1159. * Module dependencies.
  1160. */
  1161. var path = require('browser/path')
  1162. , escapeRe = require('browser/escape-string-regexp')
  1163. , utils = require('./utils');
  1164. /**
  1165. * Expose `Mocha`.
  1166. */
  1167. exports = module.exports = Mocha;
  1168. /**
  1169. * To require local UIs and reporters when running in node.
  1170. */
  1171. if (typeof process !== 'undefined' && typeof process.cwd === 'function') {
  1172. var join = path.join
  1173. , cwd = process.cwd();
  1174. module.paths.push(cwd, join(cwd, 'node_modules'));
  1175. }
  1176. /**
  1177. * Expose internals.
  1178. */
  1179. exports.utils = utils;
  1180. exports.interfaces = require('./interfaces');
  1181. exports.reporters = require('./reporters');
  1182. exports.Runnable = require('./runnable');
  1183. exports.Context = require('./context');
  1184. exports.Runner = require('./runner');
  1185. exports.Suite = require('./suite');
  1186. exports.Hook = require('./hook');
  1187. exports.Test = require('./test');
  1188. /**
  1189. * Return image `name` path.
  1190. *
  1191. * @param {String} name
  1192. * @return {String}
  1193. * @api private
  1194. */
  1195. function image(name) {
  1196. return __dirname + '/../images/' + name + '.png';
  1197. }
  1198. /**
  1199. * Setup mocha with `options`.
  1200. *
  1201. * Options:
  1202. *
  1203. * - `ui` name "bdd", "tdd", "exports" etc
  1204. * - `reporter` reporter instance, defaults to `mocha.reporters.spec`
  1205. * - `globals` array of accepted globals
  1206. * - `timeout` timeout in milliseconds
  1207. * - `bail` bail on the first test failure
  1208. * - `slow` milliseconds to wait before considering a test slow
  1209. * - `ignoreLeaks` ignore global leaks
  1210. * - `grep` string or regexp to filter tests with
  1211. *
  1212. * @param {Object} options
  1213. * @api public
  1214. */
  1215. function Mocha(options) {
  1216. options = options || {};
  1217. this.files = [];
  1218. this.options = options;
  1219. this.grep(options.grep);
  1220. this.suite = new exports.Suite('', new exports.Context);
  1221. this.ui(options.ui);
  1222. this.bail(options.bail);
  1223. this.reporter(options.reporter);
  1224. if (null != options.timeout) this.timeout(options.timeout);
  1225. this.useColors(options.useColors)
  1226. if (options.enableTimeouts !== null) this.enableTimeouts(options.enableTimeouts);
  1227. if (options.slow) this.slow(options.slow);
  1228. this.suite.on('pre-require', function (context) {
  1229. exports.afterEach = context.afterEach || context.teardown;
  1230. exports.after = context.after || context.suiteTeardown;
  1231. exports.beforeEach = context.beforeEach || context.setup;
  1232. exports.before = context.before || context.suiteSetup;
  1233. exports.describe = context.describe || context.suite;
  1234. exports.it = context.it || context.test;
  1235. exports.setup = context.setup || context.beforeEach;
  1236. exports.suiteSetup = context.suiteSetup || context.before;
  1237. exports.suiteTeardown = context.suiteTeardown || context.after;
  1238. exports.suite = context.suite || context.describe;
  1239. exports.teardown = context.teardown || context.afterEach;
  1240. exports.test = context.test || context.it;
  1241. });
  1242. }
  1243. /**
  1244. * Enable or disable bailing on the first failure.
  1245. *
  1246. * @param {Boolean} [bail]
  1247. * @api public
  1248. */
  1249. Mocha.prototype.bail = function(bail){
  1250. if (0 == arguments.length) bail = true;
  1251. this.suite.bail(bail);
  1252. return this;
  1253. };
  1254. /**
  1255. * Add test `file`.
  1256. *
  1257. * @param {String} file
  1258. * @api public
  1259. */
  1260. Mocha.prototype.addFile = function(file){
  1261. this.files.push(file);
  1262. return this;
  1263. };
  1264. /**
  1265. * Set reporter to `reporter`, defaults to "spec".
  1266. *
  1267. * @param {String|Function} reporter name or constructor
  1268. * @api public
  1269. */
  1270. Mocha.prototype.reporter = function(reporter){
  1271. if ('function' == typeof reporter) {
  1272. this._reporter = reporter;
  1273. } else {
  1274. reporter = reporter || 'spec';
  1275. var _reporter;
  1276. try { _reporter = require('./reporters/' + reporter); } catch (err) {};
  1277. if (!_reporter) try { _reporter = require(reporter); } catch (err) {};
  1278. if (!_reporter && reporter === 'teamcity')
  1279. console.warn('The Teamcity reporter was moved to a package named ' +
  1280. 'mocha-teamcity-reporter ' +
  1281. '(https://npmjs.org/package/mocha-teamcity-reporter).');
  1282. if (!_reporter) throw new Error('invalid reporter "' + reporter + '"');
  1283. this._reporter = _reporter;
  1284. }
  1285. return this;
  1286. };
  1287. /**
  1288. * Set test UI `name`, defaults to "bdd".
  1289. *
  1290. * @param {String} bdd
  1291. * @api public
  1292. */
  1293. Mocha.prototype.ui = function(name){
  1294. name = name || 'bdd';
  1295. this._ui = exports.interfaces[name];
  1296. if (!this._ui) try { this._ui = require(name); } catch (err) {};
  1297. if (!this._ui) throw new Error('invalid interface "' + name + '"');
  1298. this._ui = this._ui(this.suite);
  1299. return this;
  1300. };
  1301. /**
  1302. * Load registered files.
  1303. *
  1304. * @api private
  1305. */
  1306. Mocha.prototype.loadFiles = function(fn){
  1307. var self = this;
  1308. var suite = this.suite;
  1309. var pending = this.files.length;
  1310. this.files.forEach(function(file){
  1311. file = path.resolve(file);
  1312. suite.emit('pre-require', global, file, self);
  1313. suite.emit('require', require(file), file, self);
  1314. suite.emit('post-require', global, file, self);
  1315. --pending || (fn && fn());
  1316. });
  1317. };
  1318. /**
  1319. * Enable growl support.
  1320. *
  1321. * @api private
  1322. */
  1323. Mocha.prototype._growl = function(runner, reporter) {
  1324. var notify = require('growl');
  1325. runner.on('end', function(){
  1326. var stats = reporter.stats;
  1327. if (stats.failures) {
  1328. var msg = stats.failures + ' of ' + runner.total + ' tests failed';
  1329. notify(msg, { name: 'mocha', title: 'Failed', image: image('error') });
  1330. } else {
  1331. notify(stats.passes + ' tests passed in ' + stats.duration + 'ms', {
  1332. name: 'mocha'
  1333. , title: 'Passed'
  1334. , image: image('ok')
  1335. });
  1336. }
  1337. });
  1338. };
  1339. /**
  1340. * Add regexp to grep, if `re` is a string it is escaped.
  1341. *
  1342. * @param {RegExp|String} re
  1343. * @return {Mocha}
  1344. * @api public
  1345. */
  1346. Mocha.prototype.grep = function(re){
  1347. this.options.grep = 'string' == typeof re
  1348. ? new RegExp(escapeRe(re))
  1349. : re;
  1350. return this;
  1351. };
  1352. /**
  1353. * Invert `.grep()` matches.
  1354. *
  1355. * @return {Mocha}
  1356. * @api public
  1357. */
  1358. Mocha.prototype.invert = function(){
  1359. this.options.invert = true;
  1360. return this;
  1361. };
  1362. /**
  1363. * Ignore global leaks.
  1364. *
  1365. * @param {Boolean} ignore
  1366. * @return {Mocha}
  1367. * @api public
  1368. */
  1369. Mocha.prototype.ignoreLeaks = function(ignore){
  1370. this.options.ignoreLeaks = !!ignore;
  1371. return this;
  1372. };
  1373. /**
  1374. * Enable global leak checking.
  1375. *
  1376. * @return {Mocha}
  1377. * @api public
  1378. */
  1379. Mocha.prototype.checkLeaks = function(){
  1380. this.options.ignoreLeaks = false;
  1381. return this;
  1382. };
  1383. /**
  1384. * Enable growl support.
  1385. *
  1386. * @return {Mocha}
  1387. * @api public
  1388. */
  1389. Mocha.prototype.growl = function(){
  1390. this.options.growl = true;
  1391. return this;
  1392. };
  1393. /**
  1394. * Ignore `globals` array or string.
  1395. *
  1396. * @param {Array|String} globals
  1397. * @return {Mocha}
  1398. * @api public
  1399. */
  1400. Mocha.prototype.globals = function(globals){
  1401. this.options.globals = (this.options.globals || []).concat(globals);
  1402. return this;
  1403. };
  1404. /**
  1405. * Emit color output.
  1406. *
  1407. * @param {Boolean} colors
  1408. * @return {Mocha}
  1409. * @api public
  1410. */
  1411. Mocha.prototype.useColors = function(colors){
  1412. this.options.useColors = arguments.length && colors != undefined
  1413. ? colors
  1414. : true;
  1415. return this;
  1416. };
  1417. /**
  1418. * Use inline diffs rather than +/-.
  1419. *
  1420. * @param {Boolean} inlineDiffs
  1421. * @return {Mocha}
  1422. * @api public
  1423. */
  1424. Mocha.prototype.useInlineDiffs = function(inlineDiffs) {
  1425. this.options.useInlineDiffs = arguments.length && inlineDiffs != undefined
  1426. ? inlineDiffs
  1427. : false;
  1428. return this;
  1429. };
  1430. /**
  1431. * Set the timeout in milliseconds.
  1432. *
  1433. * @param {Number} timeout
  1434. * @return {Mocha}
  1435. * @api public
  1436. */
  1437. Mocha.prototype.timeout = function(timeout){
  1438. this.suite.timeout(timeout);
  1439. return this;
  1440. };
  1441. /**
  1442. * Set slowness threshold in milliseconds.
  1443. *
  1444. * @param {Number} slow
  1445. * @return {Mocha}
  1446. * @api public
  1447. */
  1448. Mocha.prototype.slow = function(slow){
  1449. this.suite.slow(slow);
  1450. return this;
  1451. };
  1452. /**
  1453. * Enable timeouts.
  1454. *
  1455. * @param {Boolean} enabled
  1456. * @return {Mocha}
  1457. * @api public
  1458. */
  1459. Mocha.prototype.enableTimeouts = function(enabled) {
  1460. this.suite.enableTimeouts(arguments.length && enabled !== undefined
  1461. ? enabled
  1462. : true);
  1463. return this
  1464. };
  1465. /**
  1466. * Makes all tests async (accepting a callback)
  1467. *
  1468. * @return {Mocha}
  1469. * @api public
  1470. */
  1471. Mocha.prototype.asyncOnly = function(){
  1472. this.options.asyncOnly = true;
  1473. return this;
  1474. };
  1475. /**
  1476. * Disable syntax highlighting (in browser).
  1477. * @returns {Mocha}
  1478. * @api public
  1479. */
  1480. Mocha.prototype.noHighlighting = function() {
  1481. this.options.noHighlighting = true;
  1482. return this;
  1483. };
  1484. /**
  1485. * Run tests and invoke `fn()` when complete.
  1486. *
  1487. * @param {Function} fn
  1488. * @return {Runner}
  1489. * @api public
  1490. */
  1491. Mocha.prototype.run = function(fn){
  1492. if (this.files.length) this.loadFiles();
  1493. var suite = this.suite;
  1494. var options = this.options;
  1495. options.files = this.files;
  1496. var runner = new exports.Runner(suite);
  1497. var reporter = new this._reporter(runner, options);
  1498. runner.ignoreLeaks = false !== options.ignoreLeaks;
  1499. runner.asyncOnly = options.asyncOnly;
  1500. if (options.grep) runner.grep(options.grep, options.invert);
  1501. if (options.globals) runner.globals(options.globals);
  1502. if (options.growl) this._growl(runner, reporter);
  1503. exports.reporters.Base.useColors = options.useColors;
  1504. exports.reporters.Base.inlineDiffs = options.useInlineDiffs;
  1505. return runner.run(fn);
  1506. };
  1507. }); // module: mocha.js
  1508. require.register("ms.js", function(module, exports, require){
  1509. /**
  1510. * Helpers.
  1511. */
  1512. var s = 1000;
  1513. var m = s * 60;
  1514. var h = m * 60;
  1515. var d = h * 24;
  1516. var y = d * 365.25;
  1517. /**
  1518. * Parse or format the given `val`.
  1519. *
  1520. * Options:
  1521. *
  1522. * - `long` verbose formatting [false]
  1523. *
  1524. * @param {String|Number} val
  1525. * @param {Object} options
  1526. * @return {String|Number}
  1527. * @api public
  1528. */
  1529. module.exports = function(val, options){
  1530. options = options || {};
  1531. if ('string' == typeof val) return parse(val);
  1532. return options['long'] ? longFormat(val) : shortFormat(val);
  1533. };
  1534. /**
  1535. * Parse the given `str` and return milliseconds.
  1536. *
  1537. * @param {String} str
  1538. * @return {Number}
  1539. * @api private
  1540. */
  1541. function parse(str) {
  1542. var match = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|y)?$/i.exec(str);
  1543. if (!match) return;
  1544. var n = parseFloat(match[1]);
  1545. var type = (match[2] || 'ms').toLowerCase();
  1546. switch (type) {
  1547. case 'years':
  1548. case 'year':
  1549. case 'y':
  1550. return n * y;
  1551. case 'days':
  1552. case 'day':
  1553. case 'd':
  1554. return n * d;
  1555. case 'hours':
  1556. case 'hour':
  1557. case 'h':
  1558. return n * h;
  1559. case 'minutes':
  1560. case 'minute':
  1561. case 'm':
  1562. return n * m;
  1563. case 'seconds':
  1564. case 'second':
  1565. case 's':
  1566. return n * s;
  1567. case 'ms':
  1568. return n;
  1569. }
  1570. }
  1571. /**
  1572. * Short format for `ms`.
  1573. *
  1574. * @param {Number} ms
  1575. * @return {String}
  1576. * @api private
  1577. */
  1578. function shortFormat(ms) {
  1579. if (ms >= d) return Math.round(ms / d) + 'd';
  1580. if (ms >= h) return Math.round(ms / h) + 'h';
  1581. if (ms >= m) return Math.round(ms / m) + 'm';
  1582. if (ms >= s) return Math.round(ms / s) + 's';
  1583. return ms + 'ms';
  1584. }
  1585. /**
  1586. * Long format for `ms`.
  1587. *
  1588. * @param {Number} ms
  1589. * @return {String}
  1590. * @api private
  1591. */
  1592. function longFormat(ms) {
  1593. return plural(ms, d, 'day')
  1594. || plural(ms, h, 'hour')
  1595. || plural(ms, m, 'minute')
  1596. || plural(ms, s, 'second')
  1597. || ms + ' ms';
  1598. }
  1599. /**
  1600. * Pluralization helper.
  1601. */
  1602. function plural(ms, n, name) {
  1603. if (ms < n) return;
  1604. if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name;
  1605. return Math.ceil(ms / n) + ' ' + name + 's';
  1606. }
  1607. }); // module: ms.js
  1608. require.register("reporters/base.js", function(module, exports, require){
  1609. /**
  1610. * Module dependencies.
  1611. */
  1612. var tty = require('browser/tty')
  1613. , diff = require('browser/diff')
  1614. , ms = require('../ms')
  1615. , utils = require('../utils');
  1616. /**
  1617. * Save timer references to avoid Sinon interfering (see GH-237).
  1618. */
  1619. var Date = global.Date
  1620. , setTimeout = global.setTimeout
  1621. , setInterval = global.setInterval
  1622. , clearTimeout = global.clearTimeout
  1623. , clearInterval = global.clearInterval;
  1624. /**
  1625. * Check if both stdio streams are associated with a tty.
  1626. */
  1627. var isatty = tty.isatty(1) && tty.isatty(2);
  1628. /**
  1629. * Expose `Base`.
  1630. */
  1631. exports = module.exports = Base;
  1632. /**
  1633. * Enable coloring by default.
  1634. */
  1635. exports.useColors = isatty || (process.env.MOCHA_COLORS !== undefined);
  1636. /**
  1637. * Inline diffs instead of +/-
  1638. */
  1639. exports.inlineDiffs = false;
  1640. /**
  1641. * Default color map.
  1642. */
  1643. exports.colors = {
  1644. 'pass': 90
  1645. , 'fail': 31
  1646. , 'bright pass': 92
  1647. , 'bright fail': 91
  1648. , 'bright yellow': 93
  1649. , 'pending': 36
  1650. , 'suite': 0
  1651. , 'error title': 0
  1652. , 'error message': 31
  1653. , 'error stack': 90
  1654. , 'checkmark': 32
  1655. , 'fast': 90
  1656. , 'medium': 33
  1657. , 'slow': 31
  1658. , 'green': 32
  1659. , 'light': 90
  1660. , 'diff gutter': 90
  1661. , 'diff added': 42
  1662. , 'diff removed': 41
  1663. };
  1664. /**
  1665. * Default symbol map.
  1666. */
  1667. exports.symbols = {
  1668. ok: '✓',
  1669. err: '✖',
  1670. dot: '․'
  1671. };
  1672. // With node.js on Windows: use symbols available in terminal default fonts
  1673. if ('win32' == process.platform) {
  1674. exports.symbols.ok = '\u221A';
  1675. exports.symbols.err = '\u00D7';
  1676. exports.symbols.dot = '.';
  1677. }
  1678. /**
  1679. * Color `str` with the given `type`,
  1680. * allowing colors to be disabled,
  1681. * as well as user-defined color
  1682. * schemes.
  1683. *
  1684. * @param {String} type
  1685. * @param {String} str
  1686. * @return {String}
  1687. * @api private
  1688. */
  1689. var color = exports.color = function(type, str) {
  1690. if (!exports.useColors) return str;
  1691. return '\u001b[' + exports.colors[type] + 'm' + str + '\u001b[0m';
  1692. };
  1693. /**
  1694. * Expose term window size, with some
  1695. * defaults for when stderr is not a tty.
  1696. */
  1697. exports.window = {
  1698. width: isatty
  1699. ? process.stdout.getWindowSize
  1700. ? process.stdout.getWindowSize(1)[0]
  1701. : tty.getWindowSize()[1]
  1702. : 75
  1703. };
  1704. /**
  1705. * Expose some basic cursor interactions
  1706. * that are common among reporters.
  1707. */
  1708. exports.cursor = {
  1709. hide: function(){
  1710. isatty && process.stdout.write('\u001b[?25l');
  1711. },
  1712. show: function(){
  1713. isatty && process.stdout.write('\u001b[?25h');
  1714. },
  1715. deleteLine: function(){
  1716. isatty && process.stdout.write('\u001b[2K');
  1717. },
  1718. beginningOfLine: function(){
  1719. isatty && process.stdout.write('\u001b[0G');
  1720. },
  1721. CR: function(){
  1722. if (isatty) {
  1723. exports.cursor.deleteLine();
  1724. exports.cursor.beginningOfLine();
  1725. } else {
  1726. process.stdout.write('\r');
  1727. }
  1728. }
  1729. };
  1730. /**
  1731. * Outut the given `failures` as a list.
  1732. *
  1733. * @param {Array} failures
  1734. * @api public
  1735. */
  1736. exports.list = function(failures){
  1737. console.error();
  1738. failures.forEach(function(test, i){
  1739. // format
  1740. var fmt = color('error title', ' %s) %s:\n')
  1741. + color('error message', ' %s')
  1742. + color('error stack', '\n%s\n');
  1743. // msg
  1744. var err = test.err
  1745. , message = err.message || ''
  1746. , stack = err.stack || message
  1747. , index = stack.indexOf(message) + message.length
  1748. , msg = stack.slice(0, index)
  1749. , actual = err.actual
  1750. , expected = err.expected
  1751. , escape = true;
  1752. // uncaught
  1753. if (err.uncaught) {
  1754. msg = 'Uncaught ' + msg;
  1755. }
  1756. // explicitly show diff
  1757. if (err.showDiff && sameType(actual, expected)) {
  1758. escape = false;
  1759. err.actual = actual = utils.stringify(actual);
  1760. err.expected = expected = utils.stringify(expected);
  1761. }
  1762. // actual / expected diff
  1763. if (err.showDiff && 'string' == typeof actual && 'string' == typeof expected) {
  1764. fmt = color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
  1765. var match = message.match(/^([^:]+): expected/);
  1766. msg = '\n ' + color('error message', match ? match[1] : msg);
  1767. if (exports.inlineDiffs) {
  1768. msg += inlineDiff(err, escape);
  1769. } else {
  1770. msg += unifiedDiff(err, escape);
  1771. }
  1772. }
  1773. // indent stack trace without msg
  1774. stack = stack.slice(index ? index + 1 : index)
  1775. .replace(/^/gm, ' ');
  1776. console.error(fmt, (i + 1), test.fullTitle(), msg, stack);
  1777. });
  1778. };
  1779. /**
  1780. * Initialize a new `Base` reporter.
  1781. *
  1782. * All other reporters generally
  1783. * inherit from this reporter, providing
  1784. * stats such as test duration, number
  1785. * of tests passed / failed etc.
  1786. *
  1787. * @param {Runner} runner
  1788. * @api public
  1789. */
  1790. function Base(runner) {
  1791. var self = this
  1792. , stats = this.stats = { suites: 0, tests: 0, passes: 0, pending: 0, failures: 0 }
  1793. , failures = this.failures = [];
  1794. if (!runner) return;
  1795. this.runner = runner;
  1796. runner.stats = stats;
  1797. runner.on('start', function(){
  1798. stats.start = new Date;
  1799. });
  1800. runner.on('suite', function(suite){
  1801. stats.suites = stats.suites || 0;
  1802. suite.root || stats.suites++;
  1803. });
  1804. runner.on('test end', function(test){
  1805. stats.tests = stats.tests || 0;
  1806. stats.tests++;
  1807. });
  1808. runner.on('pass', function(test){
  1809. stats.passes = stats.passes || 0;
  1810. var medium = test.slow() / 2;
  1811. test.speed = test.duration > test.slow()
  1812. ? 'slow'
  1813. : test.duration > medium
  1814. ? 'medium'
  1815. : 'fast';
  1816. stats.passes++;
  1817. });
  1818. runner.on('fail', function(test, err){
  1819. stats.failures = stats.failures || 0;
  1820. stats.failures++;
  1821. test.err = err;
  1822. failures.push(test);
  1823. });
  1824. runner.on('end', function(){
  1825. stats.end = new Date;
  1826. stats.duration = new Date - stats.start;
  1827. });
  1828. runner.on('pending', function(){
  1829. stats.pending++;
  1830. });
  1831. }
  1832. /**
  1833. * Output common epilogue used by many of
  1834. * the bundled reporters.
  1835. *
  1836. * @api public
  1837. */
  1838. Base.prototype.epilogue = function(){
  1839. var stats = this.stats;
  1840. var tests;
  1841. var fmt;
  1842. console.log();
  1843. // passes
  1844. fmt = color('bright pass', ' ')
  1845. + color('green', ' %d passing')
  1846. + color('light', ' (%s)');
  1847. console.log(fmt,
  1848. stats.passes || 0,
  1849. ms(stats.duration));
  1850. // pending
  1851. if (stats.pending) {
  1852. fmt = color('pending', ' ')
  1853. + color('pending', ' %d pending');
  1854. console.log(fmt, stats.pending);
  1855. }
  1856. // failures
  1857. if (stats.failures) {
  1858. fmt = color('fail', ' %d failing');
  1859. console.error(fmt,
  1860. stats.failures);
  1861. Base.list(this.failures);
  1862. console.error();
  1863. }
  1864. console.log();
  1865. };
  1866. /**
  1867. * Pad the given `str` to `len`.
  1868. *
  1869. * @param {String} str
  1870. * @param {String} len
  1871. * @return {String}
  1872. * @api private
  1873. */
  1874. function pad(str, len) {
  1875. str = String(str);
  1876. return Array(len - str.length + 1).join(' ') + str;
  1877. }
  1878. /**
  1879. * Returns an inline diff between 2 strings with coloured ANSI output
  1880. *
  1881. * @param {Error} Error with actual/expected
  1882. * @return {String} Diff
  1883. * @api private
  1884. */
  1885. function inlineDiff(err, escape) {
  1886. var msg = errorDiff(err, 'WordsWithSpace', escape);
  1887. // linenos
  1888. var lines = msg.split('\n');
  1889. if (lines.length > 4) {
  1890. var width = String(lines.length).length;
  1891. msg = lines.map(function(str, i){
  1892. return pad(++i, width) + ' |' + ' ' + str;
  1893. }).join('\n');
  1894. }
  1895. // legend
  1896. msg = '\n'
  1897. + color('diff removed', 'actual')
  1898. + ' '
  1899. + color('diff added', 'expected')
  1900. + '\n\n'
  1901. + msg
  1902. + '\n';
  1903. // indent
  1904. msg = msg.replace(/^/gm, ' ');
  1905. return msg;
  1906. }
  1907. /**
  1908. * Returns a unified diff between 2 strings
  1909. *
  1910. * @param {Error} Error with actual/expected
  1911. * @return {String} Diff
  1912. * @api private
  1913. */
  1914. function unifiedDiff(err, escape) {
  1915. var indent = ' ';
  1916. function cleanUp(line) {
  1917. if (escape) {
  1918. line = escapeInvisibles(line);
  1919. }
  1920. if (line[0] === '+') return indent + colorLines('diff added', line);
  1921. if (line[0] === '-') return indent + colorLines('diff removed', line);
  1922. if (line.match(/\@\@/)) return null;
  1923. if (line.match(/\\ No newline/)) return null;
  1924. else return indent + line;
  1925. }
  1926. function notBlank(line) {
  1927. return line != null;
  1928. }
  1929. msg = diff.createPatch('string', err.actual, err.expected);
  1930. var lines = msg.split('\n').splice(4);
  1931. return '\n '
  1932. + colorLines('diff added', '+ expected') + ' '
  1933. + colorLines('diff removed', '- actual')
  1934. + '\n\n'
  1935. + lines.map(cleanUp).filter(notBlank).join('\n');
  1936. }
  1937. /**
  1938. * Return a character diff for `err`.
  1939. *
  1940. * @param {Error} err
  1941. * @return {String}
  1942. * @api private
  1943. */
  1944. function errorDiff(err, type, escape) {
  1945. var actual = escape ? escapeInvisibles(err.actual) : err.actual;
  1946. var expected = escape ? escapeInvisibles(err.expected) : err.expected;
  1947. return diff['diff' + type](actual, expected).map(function(str){
  1948. if (str.added) return colorLines('diff added', str.value);
  1949. if (str.removed) return colorLines('diff removed', str.value);
  1950. return str.value;
  1951. }).join('');
  1952. }
  1953. /**
  1954. * Returns a string with all invisible characters in plain text
  1955. *
  1956. * @param {String} line
  1957. * @return {String}
  1958. * @api private
  1959. */
  1960. function escapeInvisibles(line) {
  1961. return line.replace(/\t/g, '<tab>')
  1962. .replace(/\r/g, '<CR>')
  1963. .replace(/\n/g, '<LF>\n');
  1964. }
  1965. /**
  1966. * Color lines for `str`, using the color `name`.
  1967. *
  1968. * @param {String} name
  1969. * @param {String} str
  1970. * @return {String}
  1971. * @api private
  1972. */
  1973. function colorLines(name, str) {
  1974. return str.split('\n').map(function(str){
  1975. return color(name, str);
  1976. }).join('\n');
  1977. }
  1978. /**
  1979. * Check that a / b have the same type.
  1980. *
  1981. * @param {Object} a
  1982. * @param {Object} b
  1983. * @return {Boolean}
  1984. * @api private
  1985. */
  1986. function sameType(a, b) {
  1987. a = Object.prototype.toString.call(a);
  1988. b = Object.prototype.toString.call(b);
  1989. return a == b;
  1990. }
  1991. }); // module: reporters/base.js
  1992. require.register("reporters/doc.js", function(module, exports, require){
  1993. /**
  1994. * Module dependencies.
  1995. */
  1996. var Base = require('./base')
  1997. , utils = require('../utils');
  1998. /**
  1999. * Expose `Doc`.
  2000. */
  2001. exports = module.exports = Doc;
  2002. /**
  2003. * Initialize a new `Doc` reporter.
  2004. *
  2005. * @param {Runner} runner
  2006. * @api public
  2007. */
  2008. function Doc(runner) {
  2009. Base.call(this, runner);
  2010. var self = this
  2011. , stats = this.stats
  2012. , total = runner.total
  2013. , indents = 2;
  2014. function indent() {
  2015. return Array(indents).join(' ');
  2016. }
  2017. runner.on('suite', function(suite){
  2018. if (suite.root) return;
  2019. ++indents;
  2020. console.log('%s<section class="suite">', indent());
  2021. ++indents;
  2022. console.log('%s<h1>%s</h1>', indent(), utils.escape(suite.title));
  2023. console.log('%s<dl>', indent());
  2024. });
  2025. runner.on('suite end', function(suite){
  2026. if (suite.root) return;
  2027. console.log('%s</dl>', indent());
  2028. --indents;
  2029. console.log('%s</section>', indent());
  2030. --indents;
  2031. });
  2032. runner.on('pass', function(test){
  2033. console.log('%s <dt>%s</dt>', indent(), utils.escape(test.title));
  2034. var code = utils.escape(utils.clean(test.fn.toString()));
  2035. console.log('%s <dd><pre><code>%s</code></pre></dd>', indent(), code);
  2036. });
  2037. runner.on('fail', function(test, err){
  2038. console.log('%s <dt class="error">%s</dt>', indent(), utils.escape(test.title));
  2039. var code = utils.escape(utils.clean(test.fn.toString()));
  2040. console.log('%s <dd class="error"><pre><code>%s</code></pre></dd>', indent(), code);
  2041. console.log('%s <dd class="error">%s</dd>', indent(), utils.escape(err));
  2042. });
  2043. }
  2044. }); // module: reporters/doc.js
  2045. require.register("reporters/dot.js", function(module, exports, require){
  2046. /**
  2047. * Module dependencies.
  2048. */
  2049. var Base = require('./base')
  2050. , color = Base.color;
  2051. /**
  2052. * Expose `Dot`.
  2053. */
  2054. exports = module.exports = Dot;
  2055. /**
  2056. * Initialize a new `Dot` matrix test reporter.
  2057. *
  2058. * @param {Runner} runner
  2059. * @api public
  2060. */
  2061. function Dot(runner) {
  2062. Base.call(this, runner);
  2063. var self = this
  2064. , stats = this.stats
  2065. , width = Base.window.width * .75 | 0
  2066. , n = -1;
  2067. runner.on('start', function(){
  2068. process.stdout.write('\n ');
  2069. });
  2070. runner.on('pending', function(test){
  2071. if (++n % width == 0) process.stdout.write('\n ');
  2072. process.stdout.write(color('pending', Base.symbols.dot));
  2073. });
  2074. runner.on('pass', function(test){
  2075. if (++n % width == 0) process.stdout.write('\n ');
  2076. if ('slow' == test.speed) {
  2077. process.stdout.write(color('bright yellow', Base.symbols.dot));
  2078. } else {
  2079. process.stdout.write(color(test.speed, Base.symbols.dot));
  2080. }
  2081. });
  2082. runner.on('fail', function(test, err){
  2083. if (++n % width == 0) process.stdout.write('\n ');
  2084. process.stdout.write(color('fail', Base.symbols.dot));
  2085. });
  2086. runner.on('end', function(){
  2087. console.log();
  2088. self.epilogue();
  2089. });
  2090. }
  2091. /**
  2092. * Inherit from `Base.prototype`.
  2093. */
  2094. function F(){};
  2095. F.prototype = Base.prototype;
  2096. Dot.prototype = new F;
  2097. Dot.prototype.constructor = Dot;
  2098. }); // module: reporters/dot.js
  2099. require.register("reporters/html-cov.js", function(module, exports, require){
  2100. /**
  2101. * Module dependencies.
  2102. */
  2103. var JSONCov = require('./json-cov')
  2104. , fs = require('browser/fs');
  2105. /**
  2106. * Expose `HTMLCov`.
  2107. */
  2108. exports = module.exports = HTMLCov;
  2109. /**
  2110. * Initialize a new `JsCoverage` reporter.
  2111. *
  2112. * @param {Runner} runner
  2113. * @api public
  2114. */
  2115. function HTMLCov(runner) {
  2116. var jade = require('jade')
  2117. , file = __dirname + '/templates/coverage.jade'
  2118. , str = fs.readFileSync(file, 'utf8')
  2119. , fn = jade.compile(str, { filename: file })
  2120. , self = this;
  2121. JSONCov.call(this, runner, false);
  2122. runner.on('end', function(){
  2123. process.stdout.write(fn({
  2124. cov: self.cov
  2125. , coverageClass: coverageClass
  2126. }));
  2127. });
  2128. }
  2129. /**
  2130. * Return coverage class for `n`.
  2131. *
  2132. * @return {String}
  2133. * @api private
  2134. */
  2135. function coverageClass(n) {
  2136. if (n >= 75) return 'high';
  2137. if (n >= 50) return 'medium';
  2138. if (n >= 25) return 'low';
  2139. return 'terrible';
  2140. }
  2141. }); // module: reporters/html-cov.js
  2142. require.register("reporters/html.js", function(module, exports, require){
  2143. /**
  2144. * Module dependencies.
  2145. */
  2146. var Base = require('./base')
  2147. , utils = require('../utils')
  2148. , Progress = require('../browser/progress')
  2149. , escape = utils.escape;
  2150. /**
  2151. * Save timer references to avoid Sinon interfering (see GH-237).
  2152. */
  2153. var Date = global.Date
  2154. , setTimeout = global.setTimeout
  2155. , setInterval = global.setInterval
  2156. , clearTimeout = global.clearTimeout
  2157. , clearInterval = global.clearInterval;
  2158. /**
  2159. * Expose `HTML`.
  2160. */
  2161. exports = module.exports = HTML;
  2162. /**
  2163. * Stats template.
  2164. */
  2165. var statsTemplate = '<ul id="mocha-stats">'
  2166. + '<li class="progress"><canvas width="40" height="40"></canvas></li>'
  2167. + '<li class="passes"><a href="#">passes:</a> <em>0</em></li>'
  2168. + '<li class="failures"><a href="#">failures:</a> <em>0</em></li>'
  2169. + '<li class="duration">duration: <em>0</em>s</li>'
  2170. + '</ul>';
  2171. /**
  2172. * Initialize a new `HTML` reporter.
  2173. *
  2174. * @param {Runner} runner
  2175. * @api public
  2176. */
  2177. function HTML(runner) {
  2178. Base.call(this, runner);
  2179. var self = this
  2180. , stats = this.stats
  2181. , total = runner.total
  2182. , stat = fragment(statsTemplate)
  2183. , items = stat.getElementsByTagName('li')
  2184. , passes = items[1].getElementsByTagName('em')[0]
  2185. , passesLink = items[1].getElementsByTagName('a')[0]
  2186. , failures = items[2].getElementsByTagName('em')[0]
  2187. , failuresLink = items[2].getElementsByTagName('a')[0]
  2188. , duration = items[3].getElementsByTagName('em')[0]
  2189. , canvas = stat.getElementsByTagName('canvas')[0]
  2190. , report = fragment('<ul id="mocha-report"></ul>')
  2191. , stack = [report]
  2192. , progress
  2193. , ctx
  2194. , root = document.getElementById('mocha');
  2195. if (canvas.getContext) {
  2196. var ratio = window.devicePixelRatio || 1;
  2197. canvas.style.width = canvas.width;
  2198. canvas.style.height = canvas.height;
  2199. canvas.width *= ratio;
  2200. canvas.height *= ratio;
  2201. ctx = canvas.getContext('2d');
  2202. ctx.scale(ratio, ratio);
  2203. progress = new Progress;
  2204. }
  2205. if (!root) return error('#mocha div missing, add it to your document');
  2206. // pass toggle
  2207. on(passesLink, 'click', function(){
  2208. unhide();
  2209. var name = /pass/.test(report.className) ? '' : ' pass';
  2210. report.className = report.className.replace(/fail|pass/g, '') + name;
  2211. if (report.className.trim()) hideSuitesWithout('test pass');
  2212. });
  2213. // failure toggle
  2214. on(failuresLink, 'click', function(){
  2215. unhide();
  2216. var name = /fail/.test(report.className) ? '' : ' fail';
  2217. report.className = report.className.replace(/fail|pass/g, '') + name;
  2218. if (report.className.trim()) hideSuitesWithout('test fail');
  2219. });
  2220. root.appendChild(stat);
  2221. root.appendChild(report);
  2222. if (progress) progress.size(40);
  2223. runner.on('suite', function(suite){
  2224. if (suite.root) return;
  2225. // suite
  2226. var url = self.suiteURL(suite);
  2227. var el = fragment('<li class="suite"><h1><a href="%s">%s</a></h1></li>', url, escape(suite.title));
  2228. // container
  2229. stack[0].appendChild(el);
  2230. stack.unshift(document.createElement('ul'));
  2231. el.appendChild(stack[0]);
  2232. });
  2233. runner.on('suite end', function(suite){
  2234. if (suite.root) return;
  2235. stack.shift();
  2236. });
  2237. runner.on('fail', function(test, err){
  2238. if ('hook' == test.type) runner.emit('test end', test);
  2239. });
  2240. runner.on('test end', function(test){
  2241. // TODO: add to stats
  2242. var percent = stats.tests / this.total * 100 | 0;
  2243. if (progress) progress.update(percent).draw(ctx);
  2244. // update stats
  2245. var ms = new Date - stats.start;
  2246. text(passes, stats.passes);
  2247. text(failures, stats.failures);
  2248. text(duration, (ms / 1000).toFixed(2));
  2249. // test
  2250. if ('passed' == test.state) {
  2251. var url = self.testURL(test);
  2252. var el = fragment('<li class="test pass %e"><h2>%e<span class="duration">%ems</span> <a href="%s" class="replay">‣</a></h2></li>', test.speed, test.title, test.duration, url);
  2253. } else if (test.pending) {
  2254. var el = fragment('<li class="test pass pending"><h2>%e</h2></li>', test.title);
  2255. } else {
  2256. var el = fragment('<li class="test fail"><h2>%e <a href="?grep=%e" class="replay">‣</a></h2></li>', test.title, encodeURIComponent(test.fullTitle()));
  2257. var str = test.err.stack || test.err.toString();
  2258. // FF / Opera do not add the message
  2259. if (!~str.indexOf(test.err.message)) {
  2260. str = test.err.message + '\n' + str;
  2261. }
  2262. // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we
  2263. // check for the result of the stringifying.
  2264. if ('[object Error]' == str) str = test.err.message;
  2265. // Safari doesn't give you a stack. Let's at least provide a source line.
  2266. if (!test.err.stack && test.err.sourceURL && test.err.line !== undefined) {
  2267. str += "\n(" + test.err.sourceURL + ":" + test.err.line + ")";
  2268. }
  2269. el.appendChild(fragment('<pre class="error">%e</pre>', str));
  2270. }
  2271. // toggle code
  2272. // TODO: defer
  2273. if (!test.pending) {
  2274. var h2 = el.getElementsByTagName('h2')[0];
  2275. on(h2, 'click', function(){
  2276. pre.style.display = 'none' == pre.style.display
  2277. ? 'block'
  2278. : 'none';
  2279. });
  2280. var pre = fragment('<pre><code>%e</code></pre>', utils.clean(test.fn.toString()));
  2281. el.appendChild(pre);
  2282. pre.style.display = 'none';
  2283. }
  2284. // Don't call .appendChild if #mocha-report was already .shift()'ed off the stack.
  2285. if (stack[0]) stack[0].appendChild(el);
  2286. });
  2287. }
  2288. /**
  2289. * Makes a URL, preserving querystring ("search") parameters.
  2290. * @param {string} s
  2291. * @returns {string} your new URL
  2292. */
  2293. var makeUrl = function makeUrl(s) {
  2294. var search = window.location.search;
  2295. return (search ? search + '&' : '?' ) + 'grep=' + encodeURIComponent(s);
  2296. };
  2297. /**
  2298. * Provide suite URL
  2299. *
  2300. * @param {Object} [suite]
  2301. */
  2302. HTML.prototype.suiteURL = function(suite){
  2303. return makeUrl(suite.fullTitle());
  2304. };
  2305. /**
  2306. * Provide test URL
  2307. *
  2308. * @param {Object} [test]
  2309. */
  2310. HTML.prototype.testURL = function(test){
  2311. return makeUrl(test.fullTitle());
  2312. };
  2313. /**
  2314. * Display error `msg`.
  2315. */
  2316. function error(msg) {
  2317. document.body.appendChild(fragment('<div id="mocha-error">%s</div>', msg));
  2318. }
  2319. /**
  2320. * Return a DOM fragment from `html`.
  2321. */
  2322. function fragment(html) {
  2323. var args = arguments
  2324. , div = document.createElement('div')
  2325. , i = 1;
  2326. div.innerHTML = html.replace(/%([se])/g, function(_, type){
  2327. switch (type) {
  2328. case 's': return String(args[i++]);
  2329. case 'e': return escape(args[i++]);
  2330. }
  2331. });
  2332. return div.firstChild;
  2333. }
  2334. /**
  2335. * Check for suites that do not have elements
  2336. * with `classname`, and hide them.
  2337. */
  2338. function hideSuitesWithout(classname) {
  2339. var suites = document.getElementsByClassName('suite');
  2340. for (var i = 0; i < suites.length; i++) {
  2341. var els = suites[i].getElementsByClassName(classname);
  2342. if (0 == els.length) suites[i].className += ' hidden';
  2343. }
  2344. }
  2345. /**
  2346. * Unhide .hidden suites.
  2347. */
  2348. function unhide() {
  2349. var els = document.getElementsByClassName('suite hidden');
  2350. for (var i = 0; i < els.length; ++i) {
  2351. els[i].className = els[i].className.replace('suite hidden', 'suite');
  2352. }
  2353. }
  2354. /**
  2355. * Set `el` text to `str`.
  2356. */
  2357. function text(el, str) {
  2358. if (el.textContent) {
  2359. el.textContent = str;
  2360. } else {
  2361. el.innerText = str;
  2362. }
  2363. }
  2364. /**
  2365. * Listen on `event` with callback `fn`.
  2366. */
  2367. function on(el, event, fn) {
  2368. if (el.addEventListener) {
  2369. el.addEventListener(event, fn, false);
  2370. } else {
  2371. el.attachEvent('on' + event, fn);
  2372. }
  2373. }
  2374. }); // module: reporters/html.js
  2375. require.register("reporters/index.js", function(module, exports, require){
  2376. exports.Base = require('./base');
  2377. exports.Dot = require('./dot');
  2378. exports.Doc = require('./doc');
  2379. exports.TAP = require('./tap');
  2380. exports.JSON = require('./json');
  2381. exports.HTML = require('./html');
  2382. exports.List = require('./list');
  2383. exports.Min = require('./min');
  2384. exports.Spec = require('./spec');
  2385. exports.Nyan = require('./nyan');
  2386. exports.XUnit = require('./xunit');
  2387. exports.Markdown = require('./markdown');
  2388. exports.Progress = require('./progress');
  2389. exports.Landing = require('./landing');
  2390. exports.JSONCov = require('./json-cov');
  2391. exports.HTMLCov = require('./html-cov');
  2392. exports.JSONStream = require('./json-stream');
  2393. }); // module: reporters/index.js
  2394. require.register("reporters/json-cov.js", function(module, exports, require){
  2395. /**
  2396. * Module dependencies.
  2397. */
  2398. var Base = require('./base');
  2399. /**
  2400. * Expose `JSONCov`.
  2401. */
  2402. exports = module.exports = JSONCov;
  2403. /**
  2404. * Initialize a new `JsCoverage` reporter.
  2405. *
  2406. * @param {Runner} runner
  2407. * @param {Boolean} output
  2408. * @api public
  2409. */
  2410. function JSONCov(runner, output) {
  2411. var self = this
  2412. , output = 1 == arguments.length ? true : output;
  2413. Base.call(this, runner);
  2414. var tests = []
  2415. , failures = []
  2416. , passes = [];
  2417. runner.on('test end', function(test){
  2418. tests.push(test);
  2419. });
  2420. runner.on('pass', function(test){
  2421. passes.push(test);
  2422. });
  2423. runner.on('fail', function(test){
  2424. failures.push(test);
  2425. });
  2426. runner.on('end', function(){
  2427. var cov = global._$jscoverage || {};
  2428. var result = self.cov = map(cov);
  2429. result.stats = self.stats;
  2430. result.tests = tests.map(clean);
  2431. result.failures = failures.map(clean);
  2432. result.passes = passes.map(clean);
  2433. if (!output) return;
  2434. process.stdout.write(JSON.stringify(result, null, 2 ));
  2435. });
  2436. }
  2437. /**
  2438. * Map jscoverage data to a JSON structure
  2439. * suitable for reporting.
  2440. *
  2441. * @param {Object} cov
  2442. * @return {Object}
  2443. * @api private
  2444. */
  2445. function map(cov) {
  2446. var ret = {
  2447. instrumentation: 'node-jscoverage'
  2448. , sloc: 0
  2449. , hits: 0
  2450. , misses: 0
  2451. , coverage: 0
  2452. , files: []
  2453. };
  2454. for (var filename in cov) {
  2455. var data = coverage(filename, cov[filename]);
  2456. ret.files.push(data);
  2457. ret.hits += data.hits;
  2458. ret.misses += data.misses;
  2459. ret.sloc += data.sloc;
  2460. }
  2461. ret.files.sort(function(a, b) {
  2462. return a.filename.localeCompare(b.filename);
  2463. });
  2464. if (ret.sloc > 0) {
  2465. ret.coverage = (ret.hits / ret.sloc) * 100;
  2466. }
  2467. return ret;
  2468. }
  2469. /**
  2470. * Map jscoverage data for a single source file
  2471. * to a JSON structure suitable for reporting.
  2472. *
  2473. * @param {String} filename name of the source file
  2474. * @param {Object} data jscoverage coverage data
  2475. * @return {Object}
  2476. * @api private
  2477. */
  2478. function coverage(filename, data) {
  2479. var ret = {
  2480. filename: filename,
  2481. coverage: 0,
  2482. hits: 0,
  2483. misses: 0,
  2484. sloc: 0,
  2485. source: {}
  2486. };
  2487. data.source.forEach(function(line, num){
  2488. num++;
  2489. if (data[num] === 0) {
  2490. ret.misses++;
  2491. ret.sloc++;
  2492. } else if (data[num] !== undefined) {
  2493. ret.hits++;
  2494. ret.sloc++;
  2495. }
  2496. ret.source[num] = {
  2497. source: line
  2498. , coverage: data[num] === undefined
  2499. ? ''
  2500. : data[num]
  2501. };
  2502. });
  2503. ret.coverage = ret.hits / ret.sloc * 100;
  2504. return ret;
  2505. }
  2506. /**
  2507. * Return a plain-object representation of `test`
  2508. * free of cyclic properties etc.
  2509. *
  2510. * @param {Object} test
  2511. * @return {Object}
  2512. * @api private
  2513. */
  2514. function clean(test) {
  2515. return {
  2516. title: test.title
  2517. , fullTitle: test.fullTitle()
  2518. , duration: test.duration
  2519. }
  2520. }
  2521. }); // module: reporters/json-cov.js
  2522. require.register("reporters/json-stream.js", function(module, exports, require){
  2523. /**
  2524. * Module dependencies.
  2525. */
  2526. var Base = require('./base')
  2527. , color = Base.color;
  2528. /**
  2529. * Expose `List`.
  2530. */
  2531. exports = module.exports = List;
  2532. /**
  2533. * Initialize a new `List` test reporter.
  2534. *
  2535. * @param {Runner} runner
  2536. * @api public
  2537. */
  2538. function List(runner) {
  2539. Base.call(this, runner);
  2540. var self = this
  2541. , stats = this.stats
  2542. , total = runner.total;
  2543. runner.on('start', function(){
  2544. console.log(JSON.stringify(['start', { total: total }]));
  2545. });
  2546. runner.on('pass', function(test){
  2547. console.log(JSON.stringify(['pass', clean(test)]));
  2548. });
  2549. runner.on('fail', function(test, err){
  2550. test = clean(test);
  2551. test.err = err.message;
  2552. console.log(JSON.stringify(['fail', test]));
  2553. });
  2554. runner.on('end', function(){
  2555. process.stdout.write(JSON.stringify(['end', self.stats]));
  2556. });
  2557. }
  2558. /**
  2559. * Return a plain-object representation of `test`
  2560. * free of cyclic properties etc.
  2561. *
  2562. * @param {Object} test
  2563. * @return {Object}
  2564. * @api private
  2565. */
  2566. function clean(test) {
  2567. return {
  2568. title: test.title
  2569. , fullTitle: test.fullTitle()
  2570. , duration: test.duration
  2571. }
  2572. }
  2573. }); // module: reporters/json-stream.js
  2574. require.register("reporters/json.js", function(module, exports, require){
  2575. /**
  2576. * Module dependencies.
  2577. */
  2578. var Base = require('./base')
  2579. , cursor = Base.cursor
  2580. , color = Base.color;
  2581. /**
  2582. * Expose `JSON`.
  2583. */
  2584. exports = module.exports = JSONReporter;
  2585. /**
  2586. * Initialize a new `JSON` reporter.
  2587. *
  2588. * @param {Runner} runner
  2589. * @api public
  2590. */
  2591. function JSONReporter(runner) {
  2592. var self = this;
  2593. Base.call(this, runner);
  2594. var tests = []
  2595. , pending = []
  2596. , failures = []
  2597. , passes = [];
  2598. runner.on('test end', function(test){
  2599. tests.push(test);
  2600. });
  2601. runner.on('pass', function(test){
  2602. passes.push(test);
  2603. });
  2604. runner.on('fail', function(test){
  2605. failures.push(test);
  2606. });
  2607. runner.on('pending', function(test){
  2608. pending.push(test);
  2609. });
  2610. runner.on('end', function(){
  2611. var obj = {
  2612. stats: self.stats,
  2613. tests: tests.map(clean),
  2614. pending: pending.map(clean),
  2615. failures: failures.map(clean),
  2616. passes: passes.map(clean)
  2617. };
  2618. runner.testResults = obj;
  2619. process.stdout.write(JSON.stringify(obj, null, 2));
  2620. });
  2621. }
  2622. /**
  2623. * Return a plain-object representation of `test`
  2624. * free of cyclic properties etc.
  2625. *
  2626. * @param {Object} test
  2627. * @return {Object}
  2628. * @api private
  2629. */
  2630. function clean(test) {
  2631. return {
  2632. title: test.title,
  2633. fullTitle: test.fullTitle(),
  2634. duration: test.duration,
  2635. err: errorJSON(test.err || {})
  2636. }
  2637. }
  2638. /**
  2639. * Transform `error` into a JSON object.
  2640. * @param {Error} err
  2641. * @return {Object}
  2642. */
  2643. function errorJSON(err) {
  2644. var res = {};
  2645. Object.getOwnPropertyNames(err).forEach(function(key) {
  2646. res[key] = err[key];
  2647. }, err);
  2648. return res;
  2649. }
  2650. }); // module: reporters/json.js
  2651. require.register("reporters/landing.js", function(module, exports, require){
  2652. /**
  2653. * Module dependencies.
  2654. */
  2655. var Base = require('./base')
  2656. , cursor = Base.cursor
  2657. , color = Base.color;
  2658. /**
  2659. * Expose `Landing`.
  2660. */
  2661. exports = module.exports = Landing;
  2662. /**
  2663. * Airplane color.
  2664. */
  2665. Base.colors.plane = 0;
  2666. /**
  2667. * Airplane crash color.
  2668. */
  2669. Base.colors['plane crash'] = 31;
  2670. /**
  2671. * Runway color.
  2672. */
  2673. Base.colors.runway = 90;
  2674. /**
  2675. * Initialize a new `Landing` reporter.
  2676. *
  2677. * @param {Runner} runner
  2678. * @api public
  2679. */
  2680. function Landing(runner) {
  2681. Base.call(this, runner);
  2682. var self = this
  2683. , stats = this.stats
  2684. , width = Base.window.width * .75 | 0
  2685. , total = runner.total
  2686. , stream = process.stdout
  2687. , plane = color('plane', '✈')
  2688. , crashed = -1
  2689. , n = 0;
  2690. function runway() {
  2691. var buf = Array(width).join('-');
  2692. return ' ' + color('runway', buf);
  2693. }
  2694. runner.on('start', function(){
  2695. stream.write('\n\n\n ');
  2696. cursor.hide();
  2697. });
  2698. runner.on('test end', function(test){
  2699. // check if the plane crashed
  2700. var col = -1 == crashed
  2701. ? width * ++n / total | 0
  2702. : crashed;
  2703. // show the crash
  2704. if ('failed' == test.state) {
  2705. plane = color('plane crash', '✈');
  2706. crashed = col;
  2707. }
  2708. // render landing strip
  2709. stream.write('\u001b['+(width+1)+'D\u001b[2A');
  2710. stream.write(runway());
  2711. stream.write('\n ');
  2712. stream.write(color('runway', Array(col).join('⋅')));
  2713. stream.write(plane)
  2714. stream.write(color('runway', Array(width - col).join('⋅') + '\n'));
  2715. stream.write(runway());
  2716. stream.write('\u001b[0m');
  2717. });
  2718. runner.on('end', function(){
  2719. cursor.show();
  2720. console.log();
  2721. self.epilogue();
  2722. });
  2723. }
  2724. /**
  2725. * Inherit from `Base.prototype`.
  2726. */
  2727. function F(){};
  2728. F.prototype = Base.prototype;
  2729. Landing.prototype = new F;
  2730. Landing.prototype.constructor = Landing;
  2731. }); // module: reporters/landing.js
  2732. require.register("reporters/list.js", function(module, exports, require){
  2733. /**
  2734. * Module dependencies.
  2735. */
  2736. var Base = require('./base')
  2737. , cursor = Base.cursor
  2738. , color = Base.color;
  2739. /**
  2740. * Expose `List`.
  2741. */
  2742. exports = module.exports = List;
  2743. /**
  2744. * Initialize a new `List` test reporter.
  2745. *
  2746. * @param {Runner} runner
  2747. * @api public
  2748. */
  2749. function List(runner) {
  2750. Base.call(this, runner);
  2751. var self = this
  2752. , stats = this.stats
  2753. , n = 0;
  2754. runner.on('start', function(){
  2755. console.log();
  2756. });
  2757. runner.on('test', function(test){
  2758. process.stdout.write(color('pass', ' ' + test.fullTitle() + ': '));
  2759. });
  2760. runner.on('pending', function(test){
  2761. var fmt = color('checkmark', ' -')
  2762. + color('pending', ' %s');
  2763. console.log(fmt, test.fullTitle());
  2764. });
  2765. runner.on('pass', function(test){
  2766. var fmt = color('checkmark', ' '+Base.symbols.dot)
  2767. + color('pass', ' %s: ')
  2768. + color(test.speed, '%dms');
  2769. cursor.CR();
  2770. console.log(fmt, test.fullTitle(), test.duration);
  2771. });
  2772. runner.on('fail', function(test, err){
  2773. cursor.CR();
  2774. console.log(color('fail', ' %d) %s'), ++n, test.fullTitle());
  2775. });
  2776. runner.on('end', self.epilogue.bind(self));
  2777. }
  2778. /**
  2779. * Inherit from `Base.prototype`.
  2780. */
  2781. function F(){};
  2782. F.prototype = Base.prototype;
  2783. List.prototype = new F;
  2784. List.prototype.constructor = List;
  2785. }); // module: reporters/list.js
  2786. require.register("reporters/markdown.js", function(module, exports, require){
  2787. /**
  2788. * Module dependencies.
  2789. */
  2790. var Base = require('./base')
  2791. , utils = require('../utils');
  2792. /**
  2793. * Expose `Markdown`.
  2794. */
  2795. exports = module.exports = Markdown;
  2796. /**
  2797. * Initialize a new `Markdown` reporter.
  2798. *
  2799. * @param {Runner} runner
  2800. * @api public
  2801. */
  2802. function Markdown(runner) {
  2803. Base.call(this, runner);
  2804. var self = this
  2805. , stats = this.stats
  2806. , level = 0
  2807. , buf = '';
  2808. function title(str) {
  2809. return Array(level).join('#') + ' ' + str;
  2810. }
  2811. function indent() {
  2812. return Array(level).join(' ');
  2813. }
  2814. function mapTOC(suite, obj) {
  2815. var ret = obj;
  2816. obj = obj[suite.title] = obj[suite.title] || { suite: suite };
  2817. suite.suites.forEach(function(suite){
  2818. mapTOC(suite, obj);
  2819. });
  2820. return ret;
  2821. }
  2822. function stringifyTOC(obj, level) {
  2823. ++level;
  2824. var buf = '';
  2825. var link;
  2826. for (var key in obj) {
  2827. if ('suite' == key) continue;
  2828. if (key) link = ' - [' + key + '](#' + utils.slug(obj[key].suite.fullTitle()) + ')\n';
  2829. if (key) buf += Array(level).join(' ') + link;
  2830. buf += stringifyTOC(obj[key], level);
  2831. }
  2832. --level;
  2833. return buf;
  2834. }
  2835. function generateTOC(suite) {
  2836. var obj = mapTOC(suite, {});
  2837. return stringifyTOC(obj, 0);
  2838. }
  2839. generateTOC(runner.suite);
  2840. runner.on('suite', function(suite){
  2841. ++level;
  2842. var slug = utils.slug(suite.fullTitle());
  2843. buf += '<a name="' + slug + '"></a>' + '\n';
  2844. buf += title(suite.title) + '\n';
  2845. });
  2846. runner.on('suite end', function(suite){
  2847. --level;
  2848. });
  2849. runner.on('pass', function(test){
  2850. var code = utils.clean(test.fn.toString());
  2851. buf += test.title + '.\n';
  2852. buf += '\n```js\n';
  2853. buf += code + '\n';
  2854. buf += '```\n\n';
  2855. });
  2856. runner.on('end', function(){
  2857. process.stdout.write('# TOC\n');
  2858. process.stdout.write(generateTOC(runner.suite));
  2859. process.stdout.write(buf);
  2860. });
  2861. }
  2862. }); // module: reporters/markdown.js
  2863. require.register("reporters/min.js", function(module, exports, require){
  2864. /**
  2865. * Module dependencies.
  2866. */
  2867. var Base = require('./base');
  2868. /**
  2869. * Expose `Min`.
  2870. */
  2871. exports = module.exports = Min;
  2872. /**
  2873. * Initialize a new `Min` minimal test reporter (best used with --watch).
  2874. *
  2875. * @param {Runner} runner
  2876. * @api public
  2877. */
  2878. function Min(runner) {
  2879. Base.call(this, runner);
  2880. runner.on('start', function(){
  2881. // clear screen
  2882. process.stdout.write('\u001b[2J');
  2883. // set cursor position
  2884. process.stdout.write('\u001b[1;3H');
  2885. });
  2886. runner.on('end', this.epilogue.bind(this));
  2887. }
  2888. /**
  2889. * Inherit from `Base.prototype`.
  2890. */
  2891. function F(){};
  2892. F.prototype = Base.prototype;
  2893. Min.prototype = new F;
  2894. Min.prototype.constructor = Min;
  2895. }); // module: reporters/min.js
  2896. require.register("reporters/nyan.js", function(module, exports, require){
  2897. /**
  2898. * Module dependencies.
  2899. */
  2900. var Base = require('./base')
  2901. , color = Base.color;
  2902. /**
  2903. * Expose `Dot`.
  2904. */
  2905. exports = module.exports = NyanCat;
  2906. /**
  2907. * Initialize a new `Dot` matrix test reporter.
  2908. *
  2909. * @param {Runner} runner
  2910. * @api public
  2911. */
  2912. function NyanCat(runner) {
  2913. Base.call(this, runner);
  2914. var self = this
  2915. , stats = this.stats
  2916. , width = Base.window.width * .75 | 0
  2917. , rainbowColors = this.rainbowColors = self.generateColors()
  2918. , colorIndex = this.colorIndex = 0
  2919. , numerOfLines = this.numberOfLines = 4
  2920. , trajectories = this.trajectories = [[], [], [], []]
  2921. , nyanCatWidth = this.nyanCatWidth = 11
  2922. , trajectoryWidthMax = this.trajectoryWidthMax = (width - nyanCatWidth)
  2923. , scoreboardWidth = this.scoreboardWidth = 5
  2924. , tick = this.tick = 0
  2925. , n = 0;
  2926. runner.on('start', function(){
  2927. Base.cursor.hide();
  2928. self.draw();
  2929. });
  2930. runner.on('pending', function(test){
  2931. self.draw();
  2932. });
  2933. runner.on('pass', function(test){
  2934. self.draw();
  2935. });
  2936. runner.on('fail', function(test, err){
  2937. self.draw();
  2938. });
  2939. runner.on('end', function(){
  2940. Base.cursor.show();
  2941. for (var i = 0; i < self.numberOfLines; i++) write('\n');
  2942. self.epilogue();
  2943. });
  2944. }
  2945. /**
  2946. * Draw the nyan cat
  2947. *
  2948. * @api private
  2949. */
  2950. NyanCat.prototype.draw = function(){
  2951. this.appendRainbow();
  2952. this.drawScoreboard();
  2953. this.drawRainbow();
  2954. this.drawNyanCat();
  2955. this.tick = !this.tick;
  2956. };
  2957. /**
  2958. * Draw the "scoreboard" showing the number
  2959. * of passes, failures and pending tests.
  2960. *
  2961. * @api private
  2962. */
  2963. NyanCat.prototype.drawScoreboard = function(){
  2964. var stats = this.stats;
  2965. var colors = Base.colors;
  2966. function draw(color, n) {
  2967. write(' ');
  2968. write('\u001b[' + color + 'm' + n + '\u001b[0m');
  2969. write('\n');
  2970. }
  2971. draw(colors.green, stats.passes);
  2972. draw(colors.fail, stats.failures);
  2973. draw(colors.pending, stats.pending);
  2974. write('\n');
  2975. this.cursorUp(this.numberOfLines);
  2976. };
  2977. /**
  2978. * Append the rainbow.
  2979. *
  2980. * @api private
  2981. */
  2982. NyanCat.prototype.appendRainbow = function(){
  2983. var segment = this.tick ? '_' : '-';
  2984. var rainbowified = this.rainbowify(segment);
  2985. for (var index = 0; index < this.numberOfLines; index++) {
  2986. var trajectory = this.trajectories[index];
  2987. if (trajectory.length >= this.trajectoryWidthMax) trajectory.shift();
  2988. trajectory.push(rainbowified);
  2989. }
  2990. };
  2991. /**
  2992. * Draw the rainbow.
  2993. *
  2994. * @api private
  2995. */
  2996. NyanCat.prototype.drawRainbow = function(){
  2997. var self = this;
  2998. this.trajectories.forEach(function(line, index) {
  2999. write('\u001b[' + self.scoreboardWidth + 'C');
  3000. write(line.join(''));
  3001. write('\n');
  3002. });
  3003. this.cursorUp(this.numberOfLines);
  3004. };
  3005. /**
  3006. * Draw the nyan cat
  3007. *
  3008. * @api private
  3009. */
  3010. NyanCat.prototype.drawNyanCat = function() {
  3011. var self = this;
  3012. var startWidth = this.scoreboardWidth + this.trajectories[0].length;
  3013. var color = '\u001b[' + startWidth + 'C';
  3014. var padding = '';
  3015. write(color);
  3016. write('_,------,');
  3017. write('\n');
  3018. write(color);
  3019. padding = self.tick ? ' ' : ' ';
  3020. write('_|' + padding + '/\\_/\\ ');
  3021. write('\n');
  3022. write(color);
  3023. padding = self.tick ? '_' : '__';
  3024. var tail = self.tick ? '~' : '^';
  3025. var face;
  3026. write(tail + '|' + padding + this.face() + ' ');
  3027. write('\n');
  3028. write(color);
  3029. padding = self.tick ? ' ' : ' ';
  3030. write(padding + '"" "" ');
  3031. write('\n');
  3032. this.cursorUp(this.numberOfLines);
  3033. };
  3034. /**
  3035. * Draw nyan cat face.
  3036. *
  3037. * @return {String}
  3038. * @api private
  3039. */
  3040. NyanCat.prototype.face = function() {
  3041. var stats = this.stats;
  3042. if (stats.failures) {
  3043. return '( x .x)';
  3044. } else if (stats.pending) {
  3045. return '( o .o)';
  3046. } else if(stats.passes) {
  3047. return '( ^ .^)';
  3048. } else {
  3049. return '( - .-)';
  3050. }
  3051. };
  3052. /**
  3053. * Move cursor up `n`.
  3054. *
  3055. * @param {Number} n
  3056. * @api private
  3057. */
  3058. NyanCat.prototype.cursorUp = function(n) {
  3059. write('\u001b[' + n + 'A');
  3060. };
  3061. /**
  3062. * Move cursor down `n`.
  3063. *
  3064. * @param {Number} n
  3065. * @api private
  3066. */
  3067. NyanCat.prototype.cursorDown = function(n) {
  3068. write('\u001b[' + n + 'B');
  3069. };
  3070. /**
  3071. * Generate rainbow colors.
  3072. *
  3073. * @return {Array}
  3074. * @api private
  3075. */
  3076. NyanCat.prototype.generateColors = function(){
  3077. var colors = [];
  3078. for (var i = 0; i < (6 * 7); i++) {
  3079. var pi3 = Math.floor(Math.PI / 3);
  3080. var n = (i * (1.0 / 6));
  3081. var r = Math.floor(3 * Math.sin(n) + 3);
  3082. var g = Math.floor(3 * Math.sin(n + 2 * pi3) + 3);
  3083. var b = Math.floor(3 * Math.sin(n + 4 * pi3) + 3);
  3084. colors.push(36 * r + 6 * g + b + 16);
  3085. }
  3086. return colors;
  3087. };
  3088. /**
  3089. * Apply rainbow to the given `str`.
  3090. *
  3091. * @param {String} str
  3092. * @return {String}
  3093. * @api private
  3094. */
  3095. NyanCat.prototype.rainbowify = function(str){
  3096. var color = this.rainbowColors[this.colorIndex % this.rainbowColors.length];
  3097. this.colorIndex += 1;
  3098. return '\u001b[38;5;' + color + 'm' + str + '\u001b[0m';
  3099. };
  3100. /**
  3101. * Stdout helper.
  3102. */
  3103. function write(string) {
  3104. process.stdout.write(string);
  3105. }
  3106. /**
  3107. * Inherit from `Base.prototype`.
  3108. */
  3109. function F(){};
  3110. F.prototype = Base.prototype;
  3111. NyanCat.prototype = new F;
  3112. NyanCat.prototype.constructor = NyanCat;
  3113. }); // module: reporters/nyan.js
  3114. require.register("reporters/progress.js", function(module, exports, require){
  3115. /**
  3116. * Module dependencies.
  3117. */
  3118. var Base = require('./base')
  3119. , cursor = Base.cursor
  3120. , color = Base.color;
  3121. /**
  3122. * Expose `Progress`.
  3123. */
  3124. exports = module.exports = Progress;
  3125. /**
  3126. * General progress bar color.
  3127. */
  3128. Base.colors.progress = 90;
  3129. /**
  3130. * Initialize a new `Progress` bar test reporter.
  3131. *
  3132. * @param {Runner} runner
  3133. * @param {Object} options
  3134. * @api public
  3135. */
  3136. function Progress(runner, options) {
  3137. Base.call(this, runner);
  3138. var self = this
  3139. , options = options || {}
  3140. , stats = this.stats
  3141. , width = Base.window.width * .50 | 0
  3142. , total = runner.total
  3143. , complete = 0
  3144. , max = Math.max
  3145. , lastN = -1;
  3146. // default chars
  3147. options.open = options.open || '[';
  3148. options.complete = options.complete || '▬';
  3149. options.incomplete = options.incomplete || Base.symbols.dot;
  3150. options.close = options.close || ']';
  3151. options.verbose = false;
  3152. // tests started
  3153. runner.on('start', function(){
  3154. console.log();
  3155. cursor.hide();
  3156. });
  3157. // tests complete
  3158. runner.on('test end', function(){
  3159. complete++;
  3160. var incomplete = total - complete
  3161. , percent = complete / total
  3162. , n = width * percent | 0
  3163. , i = width - n;
  3164. if (lastN === n && !options.verbose) {
  3165. // Don't re-render the line if it hasn't changed
  3166. return;
  3167. }
  3168. lastN = n;
  3169. cursor.CR();
  3170. process.stdout.write('\u001b[J');
  3171. process.stdout.write(color('progress', ' ' + options.open));
  3172. process.stdout.write(Array(n).join(options.complete));
  3173. process.stdout.write(Array(i).join(options.incomplete));
  3174. process.stdout.write(color('progress', options.close));
  3175. if (options.verbose) {
  3176. process.stdout.write(color('progress', ' ' + complete + ' of ' + total));
  3177. }
  3178. });
  3179. // tests are complete, output some stats
  3180. // and the failures if any
  3181. runner.on('end', function(){
  3182. cursor.show();
  3183. console.log();
  3184. self.epilogue();
  3185. });
  3186. }
  3187. /**
  3188. * Inherit from `Base.prototype`.
  3189. */
  3190. function F(){};
  3191. F.prototype = Base.prototype;
  3192. Progress.prototype = new F;
  3193. Progress.prototype.constructor = Progress;
  3194. }); // module: reporters/progress.js
  3195. require.register("reporters/spec.js", function(module, exports, require){
  3196. /**
  3197. * Module dependencies.
  3198. */
  3199. var Base = require('./base')
  3200. , cursor = Base.cursor
  3201. , color = Base.color;
  3202. /**
  3203. * Expose `Spec`.
  3204. */
  3205. exports = module.exports = Spec;
  3206. /**
  3207. * Initialize a new `Spec` test reporter.
  3208. *
  3209. * @param {Runner} runner
  3210. * @api public
  3211. */
  3212. function Spec(runner) {
  3213. Base.call(this, runner);
  3214. var self = this
  3215. , stats = this.stats
  3216. , indents = 0
  3217. , n = 0;
  3218. function indent() {
  3219. return Array(indents).join(' ')
  3220. }
  3221. runner.on('start', function(){
  3222. console.log();
  3223. });
  3224. runner.on('suite', function(suite){
  3225. ++indents;
  3226. console.log(color('suite', '%s%s'), indent(), suite.title);
  3227. });
  3228. runner.on('suite end', function(suite){
  3229. --indents;
  3230. if (1 == indents) console.log();
  3231. });
  3232. runner.on('pending', function(test){
  3233. var fmt = indent() + color('pending', ' - %s');
  3234. console.log(fmt, test.title);
  3235. });
  3236. runner.on('pass', function(test){
  3237. if ('fast' == test.speed) {
  3238. var fmt = indent()
  3239. + color('checkmark', ' ' + Base.symbols.ok)
  3240. + color('pass', ' %s ');
  3241. cursor.CR();
  3242. console.log(fmt, test.title);
  3243. } else {
  3244. var fmt = indent()
  3245. + color('checkmark', ' ' + Base.symbols.ok)
  3246. + color('pass', ' %s ')
  3247. + color(test.speed, '(%dms)');
  3248. cursor.CR();
  3249. console.log(fmt, test.title, test.duration);
  3250. }
  3251. });
  3252. runner.on('fail', function(test, err){
  3253. cursor.CR();
  3254. console.log(indent() + color('fail', ' %d) %s'), ++n, test.title);
  3255. });
  3256. runner.on('end', self.epilogue.bind(self));
  3257. }
  3258. /**
  3259. * Inherit from `Base.prototype`.
  3260. */
  3261. function F(){};
  3262. F.prototype = Base.prototype;
  3263. Spec.prototype = new F;
  3264. Spec.prototype.constructor = Spec;
  3265. }); // module: reporters/spec.js
  3266. require.register("reporters/tap.js", function(module, exports, require){
  3267. /**
  3268. * Module dependencies.
  3269. */
  3270. var Base = require('./base')
  3271. , cursor = Base.cursor
  3272. , color = Base.color;
  3273. /**
  3274. * Expose `TAP`.
  3275. */
  3276. exports = module.exports = TAP;
  3277. /**
  3278. * Initialize a new `TAP` reporter.
  3279. *
  3280. * @param {Runner} runner
  3281. * @api public
  3282. */
  3283. function TAP(runner) {
  3284. Base.call(this, runner);
  3285. var self = this
  3286. , stats = this.stats
  3287. , n = 1
  3288. , passes = 0
  3289. , failures = 0;
  3290. runner.on('start', function(){
  3291. var total = runner.grepTotal(runner.suite);
  3292. console.log('%d..%d', 1, total);
  3293. });
  3294. runner.on('test end', function(){
  3295. ++n;
  3296. });
  3297. runner.on('pending', function(test){
  3298. console.log('ok %d %s # SKIP -', n, title(test));
  3299. });
  3300. runner.on('pass', function(test){
  3301. passes++;
  3302. console.log('ok %d %s', n, title(test));
  3303. });
  3304. runner.on('fail', function(test, err){
  3305. failures++;
  3306. console.log('not ok %d %s', n, title(test));
  3307. if (err.stack) console.log(err.stack.replace(/^/gm, ' '));
  3308. });
  3309. runner.on('end', function(){
  3310. console.log('# tests ' + (passes + failures));
  3311. console.log('# pass ' + passes);
  3312. console.log('# fail ' + failures);
  3313. });
  3314. }
  3315. /**
  3316. * Return a TAP-safe title of `test`
  3317. *
  3318. * @param {Object} test
  3319. * @return {String}
  3320. * @api private
  3321. */
  3322. function title(test) {
  3323. return test.fullTitle().replace(/#/g, '');
  3324. }
  3325. }); // module: reporters/tap.js
  3326. require.register("reporters/xunit.js", function(module, exports, require){
  3327. /**
  3328. * Module dependencies.
  3329. */
  3330. var Base = require('./base')
  3331. , utils = require('../utils')
  3332. , escape = utils.escape;
  3333. /**
  3334. * Save timer references to avoid Sinon interfering (see GH-237).
  3335. */
  3336. var Date = global.Date
  3337. , setTimeout = global.setTimeout
  3338. , setInterval = global.setInterval
  3339. , clearTimeout = global.clearTimeout
  3340. , clearInterval = global.clearInterval;
  3341. /**
  3342. * Expose `XUnit`.
  3343. */
  3344. exports = module.exports = XUnit;
  3345. /**
  3346. * Initialize a new `XUnit` reporter.
  3347. *
  3348. * @param {Runner} runner
  3349. * @api public
  3350. */
  3351. function XUnit(runner) {
  3352. Base.call(this, runner);
  3353. var stats = this.stats
  3354. , tests = []
  3355. , self = this;
  3356. runner.on('pending', function(test){
  3357. tests.push(test);
  3358. });
  3359. runner.on('pass', function(test){
  3360. tests.push(test);
  3361. });
  3362. runner.on('fail', function(test){
  3363. tests.push(test);
  3364. });
  3365. runner.on('end', function(){
  3366. console.log(tag('testsuite', {
  3367. name: 'Mocha Tests'
  3368. , tests: stats.tests
  3369. , failures: stats.failures
  3370. , errors: stats.failures
  3371. , skipped: stats.tests - stats.failures - stats.passes
  3372. , timestamp: (new Date).toUTCString()
  3373. , time: (stats.duration / 1000) || 0
  3374. }, false));
  3375. tests.forEach(test);
  3376. console.log('</testsuite>');
  3377. });
  3378. }
  3379. /**
  3380. * Inherit from `Base.prototype`.
  3381. */
  3382. function F(){};
  3383. F.prototype = Base.prototype;
  3384. XUnit.prototype = new F;
  3385. XUnit.prototype.constructor = XUnit;
  3386. /**
  3387. * Output tag for the given `test.`
  3388. */
  3389. function test(test) {
  3390. var attrs = {
  3391. classname: test.parent.fullTitle()
  3392. , name: test.title
  3393. , time: (test.duration / 1000) || 0
  3394. };
  3395. if ('failed' == test.state) {
  3396. var err = test.err;
  3397. console.log(tag('testcase', attrs, false, tag('failure', {}, false, cdata(escape(err.message) + "\n" + err.stack))));
  3398. } else if (test.pending) {
  3399. console.log(tag('testcase', attrs, false, tag('skipped', {}, true)));
  3400. } else {
  3401. console.log(tag('testcase', attrs, true) );
  3402. }
  3403. }
  3404. /**
  3405. * HTML tag helper.
  3406. */
  3407. function tag(name, attrs, close, content) {
  3408. var end = close ? '/>' : '>'
  3409. , pairs = []
  3410. , tag;
  3411. for (var key in attrs) {
  3412. pairs.push(key + '="' + escape(attrs[key]) + '"');
  3413. }
  3414. tag = '<' + name + (pairs.length ? ' ' + pairs.join(' ') : '') + end;
  3415. if (content) tag += content + '</' + name + end;
  3416. return tag;
  3417. }
  3418. /**
  3419. * Return cdata escaped CDATA `str`.
  3420. */
  3421. function cdata(str) {
  3422. return '<![CDATA[' + escape(str) + ']]>';
  3423. }
  3424. }); // module: reporters/xunit.js
  3425. require.register("runnable.js", function(module, exports, require){
  3426. /**
  3427. * Module dependencies.
  3428. */
  3429. var EventEmitter = require('browser/events').EventEmitter
  3430. , debug = require('browser/debug')('mocha:runnable')
  3431. , milliseconds = require('./ms');
  3432. /**
  3433. * Save timer references to avoid Sinon interfering (see GH-237).
  3434. */
  3435. var Date = global.Date
  3436. , setTimeout = global.setTimeout
  3437. , setInterval = global.setInterval
  3438. , clearTimeout = global.clearTimeout
  3439. , clearInterval = global.clearInterval;
  3440. /**
  3441. * Object#toString().
  3442. */
  3443. var toString = Object.prototype.toString;
  3444. /**
  3445. * Expose `Runnable`.
  3446. */
  3447. module.exports = Runnable;
  3448. /**
  3449. * Initialize a new `Runnable` with the given `title` and callback `fn`.
  3450. *
  3451. * @param {String} title
  3452. * @param {Function} fn
  3453. * @api private
  3454. */
  3455. function Runnable(title, fn) {
  3456. this.title = title;
  3457. this.fn = fn;
  3458. this.async = fn && fn.length;
  3459. this.sync = ! this.async;
  3460. this._timeout = 2000;
  3461. this._slow = 75;
  3462. this._enableTimeouts = true;
  3463. this.timedOut = false;
  3464. this._trace = new Error('done() called multiple times')
  3465. }
  3466. /**
  3467. * Inherit from `EventEmitter.prototype`.
  3468. */
  3469. function F(){};
  3470. F.prototype = EventEmitter.prototype;
  3471. Runnable.prototype = new F;
  3472. Runnable.prototype.constructor = Runnable;
  3473. /**
  3474. * Set & get timeout `ms`.
  3475. *
  3476. * @param {Number|String} ms
  3477. * @return {Runnable|Number} ms or self
  3478. * @api private
  3479. */
  3480. Runnable.prototype.timeout = function(ms){
  3481. if (0 == arguments.length) return this._timeout;
  3482. if (ms === 0) this._enableTimeouts = false;
  3483. if ('string' == typeof ms) ms = milliseconds(ms);
  3484. debug('timeout %d', ms);
  3485. this._timeout = ms;
  3486. if (this.timer) this.resetTimeout();
  3487. return this;
  3488. };
  3489. /**
  3490. * Set & get slow `ms`.
  3491. *
  3492. * @param {Number|String} ms
  3493. * @return {Runnable|Number} ms or self
  3494. * @api private
  3495. */
  3496. Runnable.prototype.slow = function(ms){
  3497. if (0 === arguments.length) return this._slow;
  3498. if ('string' == typeof ms) ms = milliseconds(ms);
  3499. debug('timeout %d', ms);
  3500. this._slow = ms;
  3501. return this;
  3502. };
  3503. /**
  3504. * Set and & get timeout `enabled`.
  3505. *
  3506. * @param {Boolean} enabled
  3507. * @return {Runnable|Boolean} enabled or self
  3508. * @api private
  3509. */
  3510. Runnable.prototype.enableTimeouts = function(enabled){
  3511. if (arguments.length === 0) return this._enableTimeouts;
  3512. debug('enableTimeouts %s', enabled);
  3513. this._enableTimeouts = enabled;
  3514. return this;
  3515. };
  3516. /**
  3517. * Return the full title generated by recursively
  3518. * concatenating the parent's full title.
  3519. *
  3520. * @return {String}
  3521. * @api public
  3522. */
  3523. Runnable.prototype.fullTitle = function(){
  3524. return this.parent.fullTitle() + ' ' + this.title;
  3525. };
  3526. /**
  3527. * Clear the timeout.
  3528. *
  3529. * @api private
  3530. */
  3531. Runnable.prototype.clearTimeout = function(){
  3532. clearTimeout(this.timer);
  3533. };
  3534. /**
  3535. * Inspect the runnable void of private properties.
  3536. *
  3537. * @return {String}
  3538. * @api private
  3539. */
  3540. Runnable.prototype.inspect = function(){
  3541. return JSON.stringify(this, function(key, val){
  3542. if ('_' == key[0]) return;
  3543. if ('parent' == key) return '#<Suite>';
  3544. if ('ctx' == key) return '#<Context>';
  3545. return val;
  3546. }, 2);
  3547. };
  3548. /**
  3549. * Reset the timeout.
  3550. *
  3551. * @api private
  3552. */
  3553. Runnable.prototype.resetTimeout = function(){
  3554. var self = this;
  3555. var ms = this.timeout() || 1e9;
  3556. if (!this._enableTimeouts) return;
  3557. this.clearTimeout();
  3558. this.timer = setTimeout(function(){
  3559. if (!self._enableTimeouts) return;
  3560. self.callback(new Error('timeout of ' + ms + 'ms exceeded'));
  3561. self.timedOut = true;
  3562. }, ms);
  3563. };
  3564. /**
  3565. * Whitelist these globals for this test run
  3566. *
  3567. * @api private
  3568. */
  3569. Runnable.prototype.globals = function(arr){
  3570. var self = this;
  3571. this._allowedGlobals = arr;
  3572. };
  3573. /**
  3574. * Run the test and invoke `fn(err)`.
  3575. *
  3576. * @param {Function} fn
  3577. * @api private
  3578. */
  3579. Runnable.prototype.run = function(fn){
  3580. var self = this
  3581. , start = new Date
  3582. , ctx = this.ctx
  3583. , finished
  3584. , emitted;
  3585. // Some times the ctx exists but it is not runnable
  3586. if (ctx && ctx.runnable) ctx.runnable(this);
  3587. // called multiple times
  3588. function multiple(err) {
  3589. if (emitted) return;
  3590. emitted = true;
  3591. self.emit('error', err || new Error('done() called multiple times; stacktrace may be inaccurate'));
  3592. }
  3593. // finished
  3594. function done(err) {
  3595. var ms = self.timeout();
  3596. if (self.timedOut) return;
  3597. if (finished) return multiple(err || self._trace);
  3598. self.clearTimeout();
  3599. self.duration = new Date - start;
  3600. finished = true;
  3601. if (!err && self.duration > ms && self._enableTimeouts) err = new Error('timeout of ' + ms + 'ms exceeded');
  3602. fn(err);
  3603. }
  3604. // for .resetTimeout()
  3605. this.callback = done;
  3606. // explicit async with `done` argument
  3607. if (this.async) {
  3608. this.resetTimeout();
  3609. try {
  3610. this.fn.call(ctx, function(err){
  3611. if (err instanceof Error || toString.call(err) === "[object Error]") return done(err);
  3612. if (null != err) {
  3613. if (Object.prototype.toString.call(err) === '[object Object]') {
  3614. return done(new Error('done() invoked with non-Error: ' + JSON.stringify(err)));
  3615. } else {
  3616. return done(new Error('done() invoked with non-Error: ' + err));
  3617. }
  3618. }
  3619. done();
  3620. });
  3621. } catch (err) {
  3622. done(err);
  3623. }
  3624. return;
  3625. }
  3626. if (this.asyncOnly) {
  3627. return done(new Error('--async-only option in use without declaring `done()`'));
  3628. }
  3629. // sync or promise-returning
  3630. try {
  3631. if (this.pending) {
  3632. done();
  3633. } else {
  3634. callFn(this.fn);
  3635. }
  3636. } catch (err) {
  3637. done(err);
  3638. }
  3639. function callFn(fn) {
  3640. var result = fn.call(ctx);
  3641. if (result && typeof result.then === 'function') {
  3642. self.resetTimeout();
  3643. result
  3644. .then(function() {
  3645. done()
  3646. },
  3647. function(reason) {
  3648. done(reason || new Error('Promise rejected with no or falsy reason'))
  3649. });
  3650. } else {
  3651. done();
  3652. }
  3653. }
  3654. };
  3655. }); // module: runnable.js
  3656. require.register("runner.js", function(module, exports, require){
  3657. /**
  3658. * Module dependencies.
  3659. */
  3660. var EventEmitter = require('browser/events').EventEmitter
  3661. , debug = require('browser/debug')('mocha:runner')
  3662. , Test = require('./test')
  3663. , utils = require('./utils')
  3664. , filter = utils.filter
  3665. , keys = utils.keys;
  3666. /**
  3667. * Non-enumerable globals.
  3668. */
  3669. var globals = [
  3670. 'setTimeout',
  3671. 'clearTimeout',
  3672. 'setInterval',
  3673. 'clearInterval',
  3674. 'XMLHttpRequest',
  3675. 'Date'
  3676. ];
  3677. /**
  3678. * Expose `Runner`.
  3679. */
  3680. module.exports = Runner;
  3681. /**
  3682. * Initialize a `Runner` for the given `suite`.
  3683. *
  3684. * Events:
  3685. *
  3686. * - `start` execution started
  3687. * - `end` execution complete
  3688. * - `suite` (suite) test suite execution started
  3689. * - `suite end` (suite) all tests (and sub-suites) have finished
  3690. * - `test` (test) test execution started
  3691. * - `test end` (test) test completed
  3692. * - `hook` (hook) hook execution started
  3693. * - `hook end` (hook) hook complete
  3694. * - `pass` (test) test passed
  3695. * - `fail` (test, err) test failed
  3696. * - `pending` (test) test pending
  3697. *
  3698. * @api public
  3699. */
  3700. function Runner(suite) {
  3701. var self = this;
  3702. this._globals = [];
  3703. this._abort = false;
  3704. this.suite = suite;
  3705. this.total = suite.total();
  3706. this.failures = 0;
  3707. this.on('test end', function(test){ self.checkGlobals(test); });
  3708. this.on('hook end', function(hook){ self.checkGlobals(hook); });
  3709. this.grep(/.*/);
  3710. this.globals(this.globalProps().concat(extraGlobals()));
  3711. }
  3712. /**
  3713. * Wrapper for setImmediate, process.nextTick, or browser polyfill.
  3714. *
  3715. * @param {Function} fn
  3716. * @api private
  3717. */
  3718. Runner.immediately = global.setImmediate || process.nextTick;
  3719. /**
  3720. * Inherit from `EventEmitter.prototype`.
  3721. */
  3722. function F(){};
  3723. F.prototype = EventEmitter.prototype;
  3724. Runner.prototype = new F;
  3725. Runner.prototype.constructor = Runner;
  3726. /**
  3727. * Run tests with full titles matching `re`. Updates runner.total
  3728. * with number of tests matched.
  3729. *
  3730. * @param {RegExp} re
  3731. * @param {Boolean} invert
  3732. * @return {Runner} for chaining
  3733. * @api public
  3734. */
  3735. Runner.prototype.grep = function(re, invert){
  3736. debug('grep %s', re);
  3737. this._grep = re;
  3738. this._invert = invert;
  3739. this.total = this.grepTotal(this.suite);
  3740. return this;
  3741. };
  3742. /**
  3743. * Returns the number of tests matching the grep search for the
  3744. * given suite.
  3745. *
  3746. * @param {Suite} suite
  3747. * @return {Number}
  3748. * @api public
  3749. */
  3750. Runner.prototype.grepTotal = function(suite) {
  3751. var self = this;
  3752. var total = 0;
  3753. suite.eachTest(function(test){
  3754. var match = self._grep.test(test.fullTitle());
  3755. if (self._invert) match = !match;
  3756. if (match) total++;
  3757. });
  3758. return total;
  3759. };
  3760. /**
  3761. * Return a list of global properties.
  3762. *
  3763. * @return {Array}
  3764. * @api private
  3765. */
  3766. Runner.prototype.globalProps = function() {
  3767. var props = utils.keys(global);
  3768. // non-enumerables
  3769. for (var i = 0; i < globals.length; ++i) {
  3770. if (~utils.indexOf(props, globals[i])) continue;
  3771. props.push(globals[i]);
  3772. }
  3773. return props;
  3774. };
  3775. /**
  3776. * Allow the given `arr` of globals.
  3777. *
  3778. * @param {Array} arr
  3779. * @return {Runner} for chaining
  3780. * @api public
  3781. */
  3782. Runner.prototype.globals = function(arr){
  3783. if (0 == arguments.length) return this._globals;
  3784. debug('globals %j', arr);
  3785. this._globals = this._globals.concat(arr);
  3786. return this;
  3787. };
  3788. /**
  3789. * Check for global variable leaks.
  3790. *
  3791. * @api private
  3792. */
  3793. Runner.prototype.checkGlobals = function(test){
  3794. if (this.ignoreLeaks) return;
  3795. var ok = this._globals;
  3796. var globals = this.globalProps();
  3797. var leaks;
  3798. if (test) {
  3799. ok = ok.concat(test._allowedGlobals || []);
  3800. }
  3801. if(this.prevGlobalsLength == globals.length) return;
  3802. this.prevGlobalsLength = globals.length;
  3803. leaks = filterLeaks(ok, globals);
  3804. this._globals = this._globals.concat(leaks);
  3805. if (leaks.length > 1) {
  3806. this.fail(test, new Error('global leaks detected: ' + leaks.join(', ') + ''));
  3807. } else if (leaks.length) {
  3808. this.fail(test, new Error('global leak detected: ' + leaks[0]));
  3809. }
  3810. };
  3811. /**
  3812. * Fail the given `test`.
  3813. *
  3814. * @param {Test} test
  3815. * @param {Error} err
  3816. * @api private
  3817. */
  3818. Runner.prototype.fail = function(test, err){
  3819. ++this.failures;
  3820. test.state = 'failed';
  3821. if ('string' == typeof err) {
  3822. err = new Error('the string "' + err + '" was thrown, throw an Error :)');
  3823. }
  3824. this.emit('fail', test, err);
  3825. };
  3826. /**
  3827. * Fail the given `hook` with `err`.
  3828. *
  3829. * Hook failures work in the following pattern:
  3830. * - If bail, then exit
  3831. * - Failed `before` hook skips all tests in a suite and subsuites,
  3832. * but jumps to corresponding `after` hook
  3833. * - Failed `before each` hook skips remaining tests in a
  3834. * suite and jumps to corresponding `after each` hook,
  3835. * which is run only once
  3836. * - Failed `after` hook does not alter
  3837. * execution order
  3838. * - Failed `after each` hook skips remaining tests in a
  3839. * suite and subsuites, but executes other `after each`
  3840. * hooks
  3841. *
  3842. * @param {Hook} hook
  3843. * @param {Error} err
  3844. * @api private
  3845. */
  3846. Runner.prototype.failHook = function(hook, err){
  3847. this.fail(hook, err);
  3848. if (this.suite.bail()) {
  3849. this.emit('end');
  3850. }
  3851. };
  3852. /**
  3853. * Run hook `name` callbacks and then invoke `fn()`.
  3854. *
  3855. * @param {String} name
  3856. * @param {Function} function
  3857. * @api private
  3858. */
  3859. Runner.prototype.hook = function(name, fn){
  3860. var suite = this.suite
  3861. , hooks = suite['_' + name]
  3862. , self = this
  3863. , timer;
  3864. function next(i) {
  3865. var hook = hooks[i];
  3866. if (!hook) return fn();
  3867. if (self.failures && suite.bail()) return fn();
  3868. self.currentRunnable = hook;
  3869. hook.ctx.currentTest = self.test;
  3870. self.emit('hook', hook);
  3871. hook.on('error', function(err){
  3872. self.failHook(hook, err);
  3873. });
  3874. hook.run(function(err){
  3875. hook.removeAllListeners('error');
  3876. var testError = hook.error();
  3877. if (testError) self.fail(self.test, testError);
  3878. if (err) {
  3879. self.failHook(hook, err);
  3880. // stop executing hooks, notify callee of hook err
  3881. return fn(err);
  3882. }
  3883. self.emit('hook end', hook);
  3884. delete hook.ctx.currentTest;
  3885. next(++i);
  3886. });
  3887. }
  3888. Runner.immediately(function(){
  3889. next(0);
  3890. });
  3891. };
  3892. /**
  3893. * Run hook `name` for the given array of `suites`
  3894. * in order, and callback `fn(err, errSuite)`.
  3895. *
  3896. * @param {String} name
  3897. * @param {Array} suites
  3898. * @param {Function} fn
  3899. * @api private
  3900. */
  3901. Runner.prototype.hooks = function(name, suites, fn){
  3902. var self = this
  3903. , orig = this.suite;
  3904. function next(suite) {
  3905. self.suite = suite;
  3906. if (!suite) {
  3907. self.suite = orig;
  3908. return fn();
  3909. }
  3910. self.hook(name, function(err){
  3911. if (err) {
  3912. var errSuite = self.suite;
  3913. self.suite = orig;
  3914. return fn(err, errSuite);
  3915. }
  3916. next(suites.pop());
  3917. });
  3918. }
  3919. next(suites.pop());
  3920. };
  3921. /**
  3922. * Run hooks from the top level down.
  3923. *
  3924. * @param {String} name
  3925. * @param {Function} fn
  3926. * @api private
  3927. */
  3928. Runner.prototype.hookUp = function(name, fn){
  3929. var suites = [this.suite].concat(this.parents()).reverse();
  3930. this.hooks(name, suites, fn);
  3931. };
  3932. /**
  3933. * Run hooks from the bottom up.
  3934. *
  3935. * @param {String} name
  3936. * @param {Function} fn
  3937. * @api private
  3938. */
  3939. Runner.prototype.hookDown = function(name, fn){
  3940. var suites = [this.suite].concat(this.parents());
  3941. this.hooks(name, suites, fn);
  3942. };
  3943. /**
  3944. * Return an array of parent Suites from
  3945. * closest to furthest.
  3946. *
  3947. * @return {Array}
  3948. * @api private
  3949. */
  3950. Runner.prototype.parents = function(){
  3951. var suite = this.suite
  3952. , suites = [];
  3953. while (suite = suite.parent) suites.push(suite);
  3954. return suites;
  3955. };
  3956. /**
  3957. * Run the current test and callback `fn(err)`.
  3958. *
  3959. * @param {Function} fn
  3960. * @api private
  3961. */
  3962. Runner.prototype.runTest = function(fn){
  3963. var test = this.test
  3964. , self = this;
  3965. if (this.asyncOnly) test.asyncOnly = true;
  3966. try {
  3967. test.on('error', function(err){
  3968. self.fail(test, err);
  3969. });
  3970. test.run(fn);
  3971. } catch (err) {
  3972. fn(err);
  3973. }
  3974. };
  3975. /**
  3976. * Run tests in the given `suite` and invoke
  3977. * the callback `fn()` when complete.
  3978. *
  3979. * @param {Suite} suite
  3980. * @param {Function} fn
  3981. * @api private
  3982. */
  3983. Runner.prototype.runTests = function(suite, fn){
  3984. var self = this
  3985. , tests = suite.tests.slice()
  3986. , test;
  3987. function hookErr(err, errSuite, after) {
  3988. // before/after Each hook for errSuite failed:
  3989. var orig = self.suite;
  3990. // for failed 'after each' hook start from errSuite parent,
  3991. // otherwise start from errSuite itself
  3992. self.suite = after ? errSuite.parent : errSuite;
  3993. if (self.suite) {
  3994. // call hookUp afterEach
  3995. self.hookUp('afterEach', function(err2, errSuite2) {
  3996. self.suite = orig;
  3997. // some hooks may fail even now
  3998. if (err2) return hookErr(err2, errSuite2, true);
  3999. // report error suite
  4000. fn(errSuite);
  4001. });
  4002. } else {
  4003. // there is no need calling other 'after each' hooks
  4004. self.suite = orig;
  4005. fn(errSuite);
  4006. }
  4007. }
  4008. function next(err, errSuite) {
  4009. // if we bail after first err
  4010. if (self.failures && suite._bail) return fn();
  4011. if (self._abort) return fn();
  4012. if (err) return hookErr(err, errSuite, true);
  4013. // next test
  4014. test = tests.shift();
  4015. // all done
  4016. if (!test) return fn();
  4017. // grep
  4018. var match = self._grep.test(test.fullTitle());
  4019. if (self._invert) match = !match;
  4020. if (!match) return next();
  4021. // pending
  4022. if (test.pending) {
  4023. self.emit('pending', test);
  4024. self.emit('test end', test);
  4025. return next();
  4026. }
  4027. // execute test and hook(s)
  4028. self.emit('test', self.test = test);
  4029. self.hookDown('beforeEach', function(err, errSuite){
  4030. if (err) return hookErr(err, errSuite, false);
  4031. self.currentRunnable = self.test;
  4032. self.runTest(function(err){
  4033. test = self.test;
  4034. if (err) {
  4035. self.fail(test, err);
  4036. self.emit('test end', test);
  4037. return self.hookUp('afterEach', next);
  4038. }
  4039. test.state = 'passed';
  4040. self.emit('pass', test);
  4041. self.emit('test end', test);
  4042. self.hookUp('afterEach', next);
  4043. });
  4044. });
  4045. }
  4046. this.next = next;
  4047. next();
  4048. };
  4049. /**
  4050. * Run the given `suite` and invoke the
  4051. * callback `fn()` when complete.
  4052. *
  4053. * @param {Suite} suite
  4054. * @param {Function} fn
  4055. * @api private
  4056. */
  4057. Runner.prototype.runSuite = function(suite, fn){
  4058. var total = this.grepTotal(suite)
  4059. , self = this
  4060. , i = 0;
  4061. debug('run suite %s', suite.fullTitle());
  4062. if (!total) return fn();
  4063. this.emit('suite', this.suite = suite);
  4064. function next(errSuite) {
  4065. if (errSuite) {
  4066. // current suite failed on a hook from errSuite
  4067. if (errSuite == suite) {
  4068. // if errSuite is current suite
  4069. // continue to the next sibling suite
  4070. return done();
  4071. } else {
  4072. // errSuite is among the parents of current suite
  4073. // stop execution of errSuite and all sub-suites
  4074. return done(errSuite);
  4075. }
  4076. }
  4077. if (self._abort) return done();
  4078. var curr = suite.suites[i++];
  4079. if (!curr) return done();
  4080. self.runSuite(curr, next);
  4081. }
  4082. function done(errSuite) {
  4083. self.suite = suite;
  4084. self.hook('afterAll', function(){
  4085. self.emit('suite end', suite);
  4086. fn(errSuite);
  4087. });
  4088. }
  4089. this.hook('beforeAll', function(err){
  4090. if (err) return done();
  4091. self.runTests(suite, next);
  4092. });
  4093. };
  4094. /**
  4095. * Handle uncaught exceptions.
  4096. *
  4097. * @param {Error} err
  4098. * @api private
  4099. */
  4100. Runner.prototype.uncaught = function(err){
  4101. if (err) {
  4102. debug('uncaught exception %s', err !== function () {
  4103. return this;
  4104. }.call(err) ? err : ( err.message || err ));
  4105. } else {
  4106. debug('uncaught undefined exception');
  4107. err = new Error('Caught undefined error, did you throw without specifying what?');
  4108. }
  4109. err.uncaught = true;
  4110. var runnable = this.currentRunnable;
  4111. if (!runnable) return;
  4112. var wasAlreadyDone = runnable.state;
  4113. this.fail(runnable, err);
  4114. runnable.clearTimeout();
  4115. if (wasAlreadyDone) return;
  4116. // recover from test
  4117. if ('test' == runnable.type) {
  4118. this.emit('test end', runnable);
  4119. this.hookUp('afterEach', this.next);
  4120. return;
  4121. }
  4122. // bail on hooks
  4123. this.emit('end');
  4124. };
  4125. /**
  4126. * Run the root suite and invoke `fn(failures)`
  4127. * on completion.
  4128. *
  4129. * @param {Function} fn
  4130. * @return {Runner} for chaining
  4131. * @api public
  4132. */
  4133. Runner.prototype.run = function(fn){
  4134. var self = this
  4135. , fn = fn || function(){};
  4136. function uncaught(err){
  4137. self.uncaught(err);
  4138. }
  4139. debug('start');
  4140. // callback
  4141. this.on('end', function(){
  4142. debug('end');
  4143. process.removeListener('uncaughtException', uncaught);
  4144. fn(self.failures);
  4145. });
  4146. // run suites
  4147. this.emit('start');
  4148. this.runSuite(this.suite, function(){
  4149. debug('finished running');
  4150. self.emit('end');
  4151. });
  4152. // uncaught exception
  4153. process.on('uncaughtException', uncaught);
  4154. return this;
  4155. };
  4156. /**
  4157. * Cleanly abort execution
  4158. *
  4159. * @return {Runner} for chaining
  4160. * @api public
  4161. */
  4162. Runner.prototype.abort = function(){
  4163. debug('aborting');
  4164. this._abort = true;
  4165. };
  4166. /**
  4167. * Filter leaks with the given globals flagged as `ok`.
  4168. *
  4169. * @param {Array} ok
  4170. * @param {Array} globals
  4171. * @return {Array}
  4172. * @api private
  4173. */
  4174. function filterLeaks(ok, globals) {
  4175. return filter(globals, function(key){
  4176. // Firefox and Chrome exposes iframes as index inside the window object
  4177. if (/^d+/.test(key)) return false;
  4178. // in firefox
  4179. // if runner runs in an iframe, this iframe's window.getInterface method not init at first
  4180. // it is assigned in some seconds
  4181. if (global.navigator && /^getInterface/.test(key)) return false;
  4182. // an iframe could be approached by window[iframeIndex]
  4183. // in ie6,7,8 and opera, iframeIndex is enumerable, this could cause leak
  4184. if (global.navigator && /^\d+/.test(key)) return false;
  4185. // Opera and IE expose global variables for HTML element IDs (issue #243)
  4186. if (/^mocha-/.test(key)) return false;
  4187. var matched = filter(ok, function(ok){
  4188. if (~ok.indexOf('*')) return 0 == key.indexOf(ok.split('*')[0]);
  4189. return key == ok;
  4190. });
  4191. return matched.length == 0 && (!global.navigator || 'onerror' !== key);
  4192. });
  4193. }
  4194. /**
  4195. * Array of globals dependent on the environment.
  4196. *
  4197. * @return {Array}
  4198. * @api private
  4199. */
  4200. function extraGlobals() {
  4201. if (typeof(process) === 'object' &&
  4202. typeof(process.version) === 'string') {
  4203. var nodeVersion = process.version.split('.').reduce(function(a, v) {
  4204. return a << 8 | v;
  4205. });
  4206. // 'errno' was renamed to process._errno in v0.9.11.
  4207. if (nodeVersion < 0x00090B) {
  4208. return ['errno'];
  4209. }
  4210. }
  4211. return [];
  4212. }
  4213. }); // module: runner.js
  4214. require.register("suite.js", function(module, exports, require){
  4215. /**
  4216. * Module dependencies.
  4217. */
  4218. var EventEmitter = require('browser/events').EventEmitter
  4219. , debug = require('browser/debug')('mocha:suite')
  4220. , milliseconds = require('./ms')
  4221. , utils = require('./utils')
  4222. , Hook = require('./hook');
  4223. /**
  4224. * Expose `Suite`.
  4225. */
  4226. exports = module.exports = Suite;
  4227. /**
  4228. * Create a new `Suite` with the given `title`
  4229. * and parent `Suite`. When a suite with the
  4230. * same title is already present, that suite
  4231. * is returned to provide nicer reporter
  4232. * and more flexible meta-testing.
  4233. *
  4234. * @param {Suite} parent
  4235. * @param {String} title
  4236. * @return {Suite}
  4237. * @api public
  4238. */
  4239. exports.create = function(parent, title){
  4240. var suite = new Suite(title, parent.ctx);
  4241. suite.parent = parent;
  4242. if (parent.pending) suite.pending = true;
  4243. title = suite.fullTitle();
  4244. parent.addSuite(suite);
  4245. return suite;
  4246. };
  4247. /**
  4248. * Initialize a new `Suite` with the given
  4249. * `title` and `ctx`.
  4250. *
  4251. * @param {String} title
  4252. * @param {Context} ctx
  4253. * @api private
  4254. */
  4255. function Suite(title, parentContext) {
  4256. this.title = title;
  4257. var context = function() {};
  4258. context.prototype = parentContext;
  4259. this.ctx = new context();
  4260. this.suites = [];
  4261. this.tests = [];
  4262. this.pending = false;
  4263. this._beforeEach = [];
  4264. this._beforeAll = [];
  4265. this._afterEach = [];
  4266. this._afterAll = [];
  4267. this.root = !title;
  4268. this._timeout = 2000;
  4269. this._enableTimeouts = true;
  4270. this._slow = 75;
  4271. this._bail = false;
  4272. }
  4273. /**
  4274. * Inherit from `EventEmitter.prototype`.
  4275. */
  4276. function F(){};
  4277. F.prototype = EventEmitter.prototype;
  4278. Suite.prototype = new F;
  4279. Suite.prototype.constructor = Suite;
  4280. /**
  4281. * Return a clone of this `Suite`.
  4282. *
  4283. * @return {Suite}
  4284. * @api private
  4285. */
  4286. Suite.prototype.clone = function(){
  4287. var suite = new Suite(this.title);
  4288. debug('clone');
  4289. suite.ctx = this.ctx;
  4290. suite.timeout(this.timeout());
  4291. suite.enableTimeouts(this.enableTimeouts());
  4292. suite.slow(this.slow());
  4293. suite.bail(this.bail());
  4294. return suite;
  4295. };
  4296. /**
  4297. * Set timeout `ms` or short-hand such as "2s".
  4298. *
  4299. * @param {Number|String} ms
  4300. * @return {Suite|Number} for chaining
  4301. * @api private
  4302. */
  4303. Suite.prototype.timeout = function(ms){
  4304. if (0 == arguments.length) return this._timeout;
  4305. if (ms === 0) this._enableTimeouts = false;
  4306. if ('string' == typeof ms) ms = milliseconds(ms);
  4307. debug('timeout %d', ms);
  4308. this._timeout = parseInt(ms, 10);
  4309. return this;
  4310. };
  4311. /**
  4312. * Set timeout `enabled`.
  4313. *
  4314. * @param {Boolean} enabled
  4315. * @return {Suite|Boolean} self or enabled
  4316. * @api private
  4317. */
  4318. Suite.prototype.enableTimeouts = function(enabled){
  4319. if (arguments.length === 0) return this._enableTimeouts;
  4320. debug('enableTimeouts %s', enabled);
  4321. this._enableTimeouts = enabled;
  4322. return this;
  4323. };
  4324. /**
  4325. * Set slow `ms` or short-hand such as "2s".
  4326. *
  4327. * @param {Number|String} ms
  4328. * @return {Suite|Number} for chaining
  4329. * @api private
  4330. */
  4331. Suite.prototype.slow = function(ms){
  4332. if (0 === arguments.length) return this._slow;
  4333. if ('string' == typeof ms) ms = milliseconds(ms);
  4334. debug('slow %d', ms);
  4335. this._slow = ms;
  4336. return this;
  4337. };
  4338. /**
  4339. * Sets whether to bail after first error.
  4340. *
  4341. * @parma {Boolean} bail
  4342. * @return {Suite|Number} for chaining
  4343. * @api private
  4344. */
  4345. Suite.prototype.bail = function(bail){
  4346. if (0 == arguments.length) return this._bail;
  4347. debug('bail %s', bail);
  4348. this._bail = bail;
  4349. return this;
  4350. };
  4351. /**
  4352. * Run `fn(test[, done])` before running tests.
  4353. *
  4354. * @param {Function} fn
  4355. * @return {Suite} for chaining
  4356. * @api private
  4357. */
  4358. Suite.prototype.beforeAll = function(title, fn){
  4359. if (this.pending) return this;
  4360. if ('function' === typeof title) {
  4361. fn = title;
  4362. title = fn.name;
  4363. }
  4364. title = '"before all" hook' + (title ? ': ' + title : '');
  4365. var hook = new Hook(title, fn);
  4366. hook.parent = this;
  4367. hook.timeout(this.timeout());
  4368. hook.enableTimeouts(this.enableTimeouts());
  4369. hook.slow(this.slow());
  4370. hook.ctx = this.ctx;
  4371. this._beforeAll.push(hook);
  4372. this.emit('beforeAll', hook);
  4373. return this;
  4374. };
  4375. /**
  4376. * Run `fn(test[, done])` after running tests.
  4377. *
  4378. * @param {Function} fn
  4379. * @return {Suite} for chaining
  4380. * @api private
  4381. */
  4382. Suite.prototype.afterAll = function(title, fn){
  4383. if (this.pending) return this;
  4384. if ('function' === typeof title) {
  4385. fn = title;
  4386. title = fn.name;
  4387. }
  4388. title = '"after all" hook' + (title ? ': ' + title : '');
  4389. var hook = new Hook(title, fn);
  4390. hook.parent = this;
  4391. hook.timeout(this.timeout());
  4392. hook.enableTimeouts(this.enableTimeouts());
  4393. hook.slow(this.slow());
  4394. hook.ctx = this.ctx;
  4395. this._afterAll.push(hook);
  4396. this.emit('afterAll', hook);
  4397. return this;
  4398. };
  4399. /**
  4400. * Run `fn(test[, done])` before each test case.
  4401. *
  4402. * @param {Function} fn
  4403. * @return {Suite} for chaining
  4404. * @api private
  4405. */
  4406. Suite.prototype.beforeEach = function(title, fn){
  4407. if (this.pending) return this;
  4408. if ('function' === typeof title) {
  4409. fn = title;
  4410. title = fn.name;
  4411. }
  4412. title = '"before each" hook' + (title ? ': ' + title : '');
  4413. var hook = new Hook(title, fn);
  4414. hook.parent = this;
  4415. hook.timeout(this.timeout());
  4416. hook.enableTimeouts(this.enableTimeouts());
  4417. hook.slow(this.slow());
  4418. hook.ctx = this.ctx;
  4419. this._beforeEach.push(hook);
  4420. this.emit('beforeEach', hook);
  4421. return this;
  4422. };
  4423. /**
  4424. * Run `fn(test[, done])` after each test case.
  4425. *
  4426. * @param {Function} fn
  4427. * @return {Suite} for chaining
  4428. * @api private
  4429. */
  4430. Suite.prototype.afterEach = function(title, fn){
  4431. if (this.pending) return this;
  4432. if ('function' === typeof title) {
  4433. fn = title;
  4434. title = fn.name;
  4435. }
  4436. title = '"after each" hook' + (title ? ': ' + title : '');
  4437. var hook = new Hook(title, fn);
  4438. hook.parent = this;
  4439. hook.timeout(this.timeout());
  4440. hook.enableTimeouts(this.enableTimeouts());
  4441. hook.slow(this.slow());
  4442. hook.ctx = this.ctx;
  4443. this._afterEach.push(hook);
  4444. this.emit('afterEach', hook);
  4445. return this;
  4446. };
  4447. /**
  4448. * Add a test `suite`.
  4449. *
  4450. * @param {Suite} suite
  4451. * @return {Suite} for chaining
  4452. * @api private
  4453. */
  4454. Suite.prototype.addSuite = function(suite){
  4455. suite.parent = this;
  4456. suite.timeout(this.timeout());
  4457. suite.enableTimeouts(this.enableTimeouts());
  4458. suite.slow(this.slow());
  4459. suite.bail(this.bail());
  4460. this.suites.push(suite);
  4461. this.emit('suite', suite);
  4462. return this;
  4463. };
  4464. /**
  4465. * Add a `test` to this suite.
  4466. *
  4467. * @param {Test} test
  4468. * @return {Suite} for chaining
  4469. * @api private
  4470. */
  4471. Suite.prototype.addTest = function(test){
  4472. test.parent = this;
  4473. test.timeout(this.timeout());
  4474. test.enableTimeouts(this.enableTimeouts());
  4475. test.slow(this.slow());
  4476. test.ctx = this.ctx;
  4477. this.tests.push(test);
  4478. this.emit('test', test);
  4479. return this;
  4480. };
  4481. /**
  4482. * Return the full title generated by recursively
  4483. * concatenating the parent's full title.
  4484. *
  4485. * @return {String}
  4486. * @api public
  4487. */
  4488. Suite.prototype.fullTitle = function(){
  4489. if (this.parent) {
  4490. var full = this.parent.fullTitle();
  4491. if (full) return full + ' ' + this.title;
  4492. }
  4493. return this.title;
  4494. };
  4495. /**
  4496. * Return the total number of tests.
  4497. *
  4498. * @return {Number}
  4499. * @api public
  4500. */
  4501. Suite.prototype.total = function(){
  4502. return utils.reduce(this.suites, function(sum, suite){
  4503. return sum + suite.total();
  4504. }, 0) + this.tests.length;
  4505. };
  4506. /**
  4507. * Iterates through each suite recursively to find
  4508. * all tests. Applies a function in the format
  4509. * `fn(test)`.
  4510. *
  4511. * @param {Function} fn
  4512. * @return {Suite}
  4513. * @api private
  4514. */
  4515. Suite.prototype.eachTest = function(fn){
  4516. utils.forEach(this.tests, fn);
  4517. utils.forEach(this.suites, function(suite){
  4518. suite.eachTest(fn);
  4519. });
  4520. return this;
  4521. };
  4522. }); // module: suite.js
  4523. require.register("test.js", function(module, exports, require){
  4524. /**
  4525. * Module dependencies.
  4526. */
  4527. var Runnable = require('./runnable');
  4528. /**
  4529. * Expose `Test`.
  4530. */
  4531. module.exports = Test;
  4532. /**
  4533. * Initialize a new `Test` with the given `title` and callback `fn`.
  4534. *
  4535. * @param {String} title
  4536. * @param {Function} fn
  4537. * @api private
  4538. */
  4539. function Test(title, fn) {
  4540. Runnable.call(this, title, fn);
  4541. this.pending = !fn;
  4542. this.type = 'test';
  4543. }
  4544. /**
  4545. * Inherit from `Runnable.prototype`.
  4546. */
  4547. function F(){};
  4548. F.prototype = Runnable.prototype;
  4549. Test.prototype = new F;
  4550. Test.prototype.constructor = Test;
  4551. }); // module: test.js
  4552. require.register("utils.js", function(module, exports, require){
  4553. /**
  4554. * Module dependencies.
  4555. */
  4556. var fs = require('browser/fs')
  4557. , path = require('browser/path')
  4558. , basename = path.basename
  4559. , exists = fs.existsSync || path.existsSync
  4560. , glob = require('browser/glob')
  4561. , join = path.join
  4562. , debug = require('browser/debug')('mocha:watch');
  4563. /**
  4564. * Ignored directories.
  4565. */
  4566. var ignore = ['node_modules', '.git'];
  4567. /**
  4568. * Escape special characters in the given string of html.
  4569. *
  4570. * @param {String} html
  4571. * @return {String}
  4572. * @api private
  4573. */
  4574. exports.escape = function(html){
  4575. return String(html)
  4576. .replace(/&/g, '&amp;')
  4577. .replace(/"/g, '&quot;')
  4578. .replace(/</g, '&lt;')
  4579. .replace(/>/g, '&gt;');
  4580. };
  4581. /**
  4582. * Array#forEach (<=IE8)
  4583. *
  4584. * @param {Array} array
  4585. * @param {Function} fn
  4586. * @param {Object} scope
  4587. * @api private
  4588. */
  4589. exports.forEach = function(arr, fn, scope){
  4590. for (var i = 0, l = arr.length; i < l; i++)
  4591. fn.call(scope, arr[i], i);
  4592. };
  4593. /**
  4594. * Array#map (<=IE8)
  4595. *
  4596. * @param {Array} array
  4597. * @param {Function} fn
  4598. * @param {Object} scope
  4599. * @api private
  4600. */
  4601. exports.map = function(arr, fn, scope){
  4602. var result = [];
  4603. for (var i = 0, l = arr.length; i < l; i++)
  4604. result.push(fn.call(scope, arr[i], i));
  4605. return result;
  4606. };
  4607. /**
  4608. * Array#indexOf (<=IE8)
  4609. *
  4610. * @parma {Array} arr
  4611. * @param {Object} obj to find index of
  4612. * @param {Number} start
  4613. * @api private
  4614. */
  4615. exports.indexOf = function(arr, obj, start){
  4616. for (var i = start || 0, l = arr.length; i < l; i++) {
  4617. if (arr[i] === obj)
  4618. return i;
  4619. }
  4620. return -1;
  4621. };
  4622. /**
  4623. * Array#reduce (<=IE8)
  4624. *
  4625. * @param {Array} array
  4626. * @param {Function} fn
  4627. * @param {Object} initial value
  4628. * @api private
  4629. */
  4630. exports.reduce = function(arr, fn, val){
  4631. var rval = val;
  4632. for (var i = 0, l = arr.length; i < l; i++) {
  4633. rval = fn(rval, arr[i], i, arr);
  4634. }
  4635. return rval;
  4636. };
  4637. /**
  4638. * Array#filter (<=IE8)
  4639. *
  4640. * @param {Array} array
  4641. * @param {Function} fn
  4642. * @api private
  4643. */
  4644. exports.filter = function(arr, fn){
  4645. var ret = [];
  4646. for (var i = 0, l = arr.length; i < l; i++) {
  4647. var val = arr[i];
  4648. if (fn(val, i, arr)) ret.push(val);
  4649. }
  4650. return ret;
  4651. };
  4652. /**
  4653. * Object.keys (<=IE8)
  4654. *
  4655. * @param {Object} obj
  4656. * @return {Array} keys
  4657. * @api private
  4658. */
  4659. exports.keys = Object.keys || function(obj) {
  4660. var keys = []
  4661. , has = Object.prototype.hasOwnProperty // for `window` on <=IE8
  4662. for (var key in obj) {
  4663. if (has.call(obj, key)) {
  4664. keys.push(key);
  4665. }
  4666. }
  4667. return keys;
  4668. };
  4669. /**
  4670. * Watch the given `files` for changes
  4671. * and invoke `fn(file)` on modification.
  4672. *
  4673. * @param {Array} files
  4674. * @param {Function} fn
  4675. * @api private
  4676. */
  4677. exports.watch = function(files, fn){
  4678. var options = { interval: 100 };
  4679. files.forEach(function(file){
  4680. debug('file %s', file);
  4681. fs.watchFile(file, options, function(curr, prev){
  4682. if (prev.mtime < curr.mtime) fn(file);
  4683. });
  4684. });
  4685. };
  4686. /**
  4687. * Ignored files.
  4688. */
  4689. function ignored(path){
  4690. return !~ignore.indexOf(path);
  4691. }
  4692. /**
  4693. * Lookup files in the given `dir`.
  4694. *
  4695. * @return {Array}
  4696. * @api private
  4697. */
  4698. exports.files = function(dir, ext, ret){
  4699. ret = ret || [];
  4700. ext = ext || ['js'];
  4701. var re = new RegExp('\\.(' + ext.join('|') + ')$');
  4702. fs.readdirSync(dir)
  4703. .filter(ignored)
  4704. .forEach(function(path){
  4705. path = join(dir, path);
  4706. if (fs.statSync(path).isDirectory()) {
  4707. exports.files(path, ext, ret);
  4708. } else if (path.match(re)) {
  4709. ret.push(path);
  4710. }
  4711. });
  4712. return ret;
  4713. };
  4714. /**
  4715. * Compute a slug from the given `str`.
  4716. *
  4717. * @param {String} str
  4718. * @return {String}
  4719. * @api private
  4720. */
  4721. exports.slug = function(str){
  4722. return str
  4723. .toLowerCase()
  4724. .replace(/ +/g, '-')
  4725. .replace(/[^-\w]/g, '');
  4726. };
  4727. /**
  4728. * Strip the function definition from `str`,
  4729. * and re-indent for pre whitespace.
  4730. */
  4731. exports.clean = function(str) {
  4732. str = str
  4733. .replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '')
  4734. .replace(/^function *\(.*\) *{|\(.*\) *=> *{?/, '')
  4735. .replace(/\s+\}$/, '');
  4736. var spaces = str.match(/^\n?( *)/)[1].length
  4737. , tabs = str.match(/^\n?(\t*)/)[1].length
  4738. , re = new RegExp('^\n?' + (tabs ? '\t' : ' ') + '{' + (tabs ? tabs : spaces) + '}', 'gm');
  4739. str = str.replace(re, '');
  4740. return exports.trim(str);
  4741. };
  4742. /**
  4743. * Trim the given `str`.
  4744. *
  4745. * @param {String} str
  4746. * @return {String}
  4747. * @api private
  4748. */
  4749. exports.trim = function(str){
  4750. return str.replace(/^\s+|\s+$/g, '');
  4751. };
  4752. /**
  4753. * Parse the given `qs`.
  4754. *
  4755. * @param {String} qs
  4756. * @return {Object}
  4757. * @api private
  4758. */
  4759. exports.parseQuery = function(qs){
  4760. return exports.reduce(qs.replace('?', '').split('&'), function(obj, pair){
  4761. var i = pair.indexOf('=')
  4762. , key = pair.slice(0, i)
  4763. , val = pair.slice(++i);
  4764. obj[key] = decodeURIComponent(val);
  4765. return obj;
  4766. }, {});
  4767. };
  4768. /**
  4769. * Highlight the given string of `js`.
  4770. *
  4771. * @param {String} js
  4772. * @return {String}
  4773. * @api private
  4774. */
  4775. function highlight(js) {
  4776. return js
  4777. .replace(/</g, '&lt;')
  4778. .replace(/>/g, '&gt;')
  4779. .replace(/\/\/(.*)/gm, '<span class="comment">//$1</span>')
  4780. .replace(/('.*?')/gm, '<span class="string">$1</span>')
  4781. .replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
  4782. .replace(/(\d+)/gm, '<span class="number">$1</span>')
  4783. .replace(/\bnew[ \t]+(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
  4784. .replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')
  4785. }
  4786. /**
  4787. * Highlight the contents of tag `name`.
  4788. *
  4789. * @param {String} name
  4790. * @api private
  4791. */
  4792. exports.highlightTags = function(name) {
  4793. var code = document.getElementById('mocha').getElementsByTagName(name);
  4794. for (var i = 0, len = code.length; i < len; ++i) {
  4795. code[i].innerHTML = highlight(code[i].innerHTML);
  4796. }
  4797. };
  4798. /**
  4799. * Stringify `obj`.
  4800. *
  4801. * @param {Object} obj
  4802. * @return {String}
  4803. * @api private
  4804. */
  4805. exports.stringify = function(obj) {
  4806. if (obj instanceof RegExp) return obj.toString();
  4807. return JSON.stringify(exports.canonicalize(obj), null, 2).replace(/,(\n|$)/g, '$1');
  4808. };
  4809. /**
  4810. * Return a new object that has the keys in sorted order.
  4811. * @param {Object} obj
  4812. * @param {Array} [stack]
  4813. * @return {Object}
  4814. * @api private
  4815. */
  4816. exports.canonicalize = function(obj, stack) {
  4817. stack = stack || [];
  4818. if (exports.indexOf(stack, obj) !== -1) return '[Circular]';
  4819. var canonicalizedObj;
  4820. if ({}.toString.call(obj) === '[object Array]') {
  4821. stack.push(obj);
  4822. canonicalizedObj = exports.map(obj, function (item) {
  4823. return exports.canonicalize(item, stack);
  4824. });
  4825. stack.pop();
  4826. } else if (typeof obj === 'object' && obj !== null) {
  4827. stack.push(obj);
  4828. canonicalizedObj = {};
  4829. exports.forEach(exports.keys(obj).sort(), function (key) {
  4830. canonicalizedObj[key] = exports.canonicalize(obj[key], stack);
  4831. });
  4832. stack.pop();
  4833. } else {
  4834. canonicalizedObj = obj;
  4835. }
  4836. return canonicalizedObj;
  4837. };
  4838. /**
  4839. * Lookup file names at the given `path`.
  4840. */
  4841. exports.lookupFiles = function lookupFiles(path, extensions, recursive) {
  4842. var files = [];
  4843. var re = new RegExp('\\.(' + extensions.join('|') + ')$');
  4844. if (!exists(path)) {
  4845. if (exists(path + '.js')) {
  4846. path += '.js';
  4847. } else {
  4848. files = glob.sync(path);
  4849. if (!files.length) throw new Error("cannot resolve path (or pattern) '" + path + "'");
  4850. return files;
  4851. }
  4852. }
  4853. try {
  4854. var stat = fs.statSync(path);
  4855. if (stat.isFile()) return path;
  4856. }
  4857. catch (ignored) {
  4858. return;
  4859. }
  4860. fs.readdirSync(path).forEach(function(file){
  4861. file = join(path, file);
  4862. try {
  4863. var stat = fs.statSync(file);
  4864. if (stat.isDirectory()) {
  4865. if (recursive) {
  4866. files = files.concat(lookupFiles(file, extensions, recursive));
  4867. }
  4868. return;
  4869. }
  4870. }
  4871. catch (ignored) {
  4872. return;
  4873. }
  4874. if (!stat.isFile() || !re.test(file) || basename(file)[0] === '.') return;
  4875. files.push(file);
  4876. });
  4877. return files;
  4878. };
  4879. }); // module: utils.js
  4880. // The global object is "self" in Web Workers.
  4881. var global = (function() { return this; })();
  4882. /**
  4883. * Save timer references to avoid Sinon interfering (see GH-237).
  4884. */
  4885. var Date = global.Date;
  4886. var setTimeout = global.setTimeout;
  4887. var setInterval = global.setInterval;
  4888. var clearTimeout = global.clearTimeout;
  4889. var clearInterval = global.clearInterval;
  4890. /**
  4891. * Node shims.
  4892. *
  4893. * These are meant only to allow
  4894. * mocha.js to run untouched, not
  4895. * to allow running node code in
  4896. * the browser.
  4897. */
  4898. var process = {};
  4899. process.exit = function(status){};
  4900. process.stdout = {};
  4901. var uncaughtExceptionHandlers = [];
  4902. var originalOnerrorHandler = global.onerror;
  4903. /**
  4904. * Remove uncaughtException listener.
  4905. * Revert to original onerror handler if previously defined.
  4906. */
  4907. process.removeListener = function(e, fn){
  4908. if ('uncaughtException' == e) {
  4909. if (originalOnerrorHandler) {
  4910. global.onerror = originalOnerrorHandler;
  4911. } else {
  4912. global.onerror = function() {};
  4913. }
  4914. var i = Mocha.utils.indexOf(uncaughtExceptionHandlers, fn);
  4915. if (i != -1) { uncaughtExceptionHandlers.splice(i, 1); }
  4916. }
  4917. };
  4918. /**
  4919. * Implements uncaughtException listener.
  4920. */
  4921. process.on = function(e, fn){
  4922. if ('uncaughtException' == e) {
  4923. global.onerror = function(err, url, line){
  4924. fn(new Error(err + ' (' + url + ':' + line + ')'));
  4925. return true;
  4926. };
  4927. uncaughtExceptionHandlers.push(fn);
  4928. }
  4929. };
  4930. /**
  4931. * Expose mocha.
  4932. */
  4933. var Mocha = global.Mocha = require('mocha'),
  4934. mocha = global.mocha = new Mocha({ reporter: 'html' });
  4935. // The BDD UI is registered by default, but no UI will be functional in the
  4936. // browser without an explicit call to the overridden `mocha.ui` (see below).
  4937. // Ensure that this default UI does not expose its methods to the global scope.
  4938. mocha.suite.removeAllListeners('pre-require');
  4939. var immediateQueue = []
  4940. , immediateTimeout;
  4941. function timeslice() {
  4942. var immediateStart = new Date().getTime();
  4943. while (immediateQueue.length && (new Date().getTime() - immediateStart) < 100) {
  4944. immediateQueue.shift()();
  4945. }
  4946. if (immediateQueue.length) {
  4947. immediateTimeout = setTimeout(timeslice, 0);
  4948. } else {
  4949. immediateTimeout = null;
  4950. }
  4951. }
  4952. /**
  4953. * High-performance override of Runner.immediately.
  4954. */
  4955. Mocha.Runner.immediately = function(callback) {
  4956. immediateQueue.push(callback);
  4957. if (!immediateTimeout) {
  4958. immediateTimeout = setTimeout(timeslice, 0);
  4959. }
  4960. };
  4961. /**
  4962. * Function to allow assertion libraries to throw errors directly into mocha.
  4963. * This is useful when running tests in a browser because window.onerror will
  4964. * only receive the 'message' attribute of the Error.
  4965. */
  4966. mocha.throwError = function(err) {
  4967. Mocha.utils.forEach(uncaughtExceptionHandlers, function (fn) {
  4968. fn(err);
  4969. });
  4970. throw err;
  4971. };
  4972. /**
  4973. * Override ui to ensure that the ui functions are initialized.
  4974. * Normally this would happen in Mocha.prototype.loadFiles.
  4975. */
  4976. mocha.ui = function(ui){
  4977. Mocha.prototype.ui.call(this, ui);
  4978. this.suite.emit('pre-require', global, null, this);
  4979. return this;
  4980. };
  4981. /**
  4982. * Setup mocha with the given setting options.
  4983. */
  4984. mocha.setup = function(opts){
  4985. if ('string' == typeof opts) opts = { ui: opts };
  4986. for (var opt in opts) this[opt](opts[opt]);
  4987. return this;
  4988. };
  4989. /**
  4990. * Run mocha, returning the Runner.
  4991. */
  4992. mocha.run = function(fn){
  4993. var options = mocha.options;
  4994. mocha.globals('location');
  4995. var query = Mocha.utils.parseQuery(global.location.search || '');
  4996. if (query.grep) mocha.grep(query.grep);
  4997. if (query.invert) mocha.invert();
  4998. return Mocha.prototype.run.call(mocha, function(err){
  4999. // The DOM Document is not available in Web Workers.
  5000. var document = global.document;
  5001. if (document && document.getElementById('mocha') && options.noHighlighting !== true) {
  5002. Mocha.utils.highlightTags('code');
  5003. }
  5004. if (fn) fn(err);
  5005. });
  5006. };
  5007. /**
  5008. * Expose the process shim.
  5009. */
  5010. Mocha.process = process;
  5011. })();