sinon-1.16.1.js 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195
  1. /**
  2. * Sinon.JS 1.16.1, 2015/08/20
  3. *
  4. * @author Christian Johansen (christian@cjohansen.no)
  5. * @author Contributors: https://github.com/cjohansen/Sinon.JS/blob/master/AUTHORS
  6. *
  7. * (The BSD License)
  8. *
  9. * Copyright (c) 2010-2014, Christian Johansen, christian@cjohansen.no
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * * Neither the name of Christian Johansen nor the names of his contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  33. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. (function (root, factory) {
  36. 'use strict';
  37. if (typeof define === 'function' && define.amd) {
  38. define('sinon', [], function () {
  39. return (root.sinon = factory());
  40. });
  41. } else if (typeof exports === 'object') {
  42. module.exports = factory();
  43. } else {
  44. root.sinon = factory();
  45. }
  46. }(this, function () {
  47. 'use strict';
  48. var samsam, formatio, lolex;
  49. (function () {
  50. function define(mod, deps, fn) {
  51. if (mod == "samsam") {
  52. samsam = deps();
  53. } else if (typeof deps === "function" && mod.length === 0) {
  54. lolex = deps();
  55. } else if (typeof fn === "function") {
  56. formatio = fn(samsam);
  57. }
  58. }
  59. define.amd = {};
  60. ((typeof define === "function" && define.amd && function (m) { define("samsam", m); }) ||
  61. (typeof module === "object" &&
  62. function (m) { module.exports = m(); }) || // Node
  63. function (m) { this.samsam = m(); } // Browser globals
  64. )(function () {
  65. var o = Object.prototype;
  66. var div = typeof document !== "undefined" && document.createElement("div");
  67. function isNaN(value) {
  68. // Unlike global isNaN, this avoids type coercion
  69. // typeof check avoids IE host object issues, hat tip to
  70. // lodash
  71. var val = value; // JsLint thinks value !== value is "weird"
  72. return typeof value === "number" && value !== val;
  73. }
  74. function getClass(value) {
  75. // Returns the internal [[Class]] by calling Object.prototype.toString
  76. // with the provided value as this. Return value is a string, naming the
  77. // internal class, e.g. "Array"
  78. return o.toString.call(value).split(/[ \]]/)[1];
  79. }
  80. /**
  81. * @name samsam.isArguments
  82. * @param Object object
  83. *
  84. * Returns ``true`` if ``object`` is an ``arguments`` object,
  85. * ``false`` otherwise.
  86. */
  87. function isArguments(object) {
  88. if (getClass(object) === 'Arguments') { return true; }
  89. if (typeof object !== "object" || typeof object.length !== "number" ||
  90. getClass(object) === "Array") {
  91. return false;
  92. }
  93. if (typeof object.callee == "function") { return true; }
  94. try {
  95. object[object.length] = 6;
  96. delete object[object.length];
  97. } catch (e) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * @name samsam.isElement
  104. * @param Object object
  105. *
  106. * Returns ``true`` if ``object`` is a DOM element node. Unlike
  107. * Underscore.js/lodash, this function will return ``false`` if ``object``
  108. * is an *element-like* object, i.e. a regular object with a ``nodeType``
  109. * property that holds the value ``1``.
  110. */
  111. function isElement(object) {
  112. if (!object || object.nodeType !== 1 || !div) { return false; }
  113. try {
  114. object.appendChild(div);
  115. object.removeChild(div);
  116. } catch (e) {
  117. return false;
  118. }
  119. return true;
  120. }
  121. /**
  122. * @name samsam.keys
  123. * @param Object object
  124. *
  125. * Return an array of own property names.
  126. */
  127. function keys(object) {
  128. var ks = [], prop;
  129. for (prop in object) {
  130. if (o.hasOwnProperty.call(object, prop)) { ks.push(prop); }
  131. }
  132. return ks;
  133. }
  134. /**
  135. * @name samsam.isDate
  136. * @param Object value
  137. *
  138. * Returns true if the object is a ``Date``, or *date-like*. Duck typing
  139. * of date objects work by checking that the object has a ``getTime``
  140. * function whose return value equals the return value from the object's
  141. * ``valueOf``.
  142. */
  143. function isDate(value) {
  144. return typeof value.getTime == "function" &&
  145. value.getTime() == value.valueOf();
  146. }
  147. /**
  148. * @name samsam.isNegZero
  149. * @param Object value
  150. *
  151. * Returns ``true`` if ``value`` is ``-0``.
  152. */
  153. function isNegZero(value) {
  154. return value === 0 && 1 / value === -Infinity;
  155. }
  156. /**
  157. * @name samsam.equal
  158. * @param Object obj1
  159. * @param Object obj2
  160. *
  161. * Returns ``true`` if two objects are strictly equal. Compared to
  162. * ``===`` there are two exceptions:
  163. *
  164. * - NaN is considered equal to NaN
  165. * - -0 and +0 are not considered equal
  166. */
  167. function identical(obj1, obj2) {
  168. if (obj1 === obj2 || (isNaN(obj1) && isNaN(obj2))) {
  169. return obj1 !== 0 || isNegZero(obj1) === isNegZero(obj2);
  170. }
  171. }
  172. /**
  173. * @name samsam.deepEqual
  174. * @param Object obj1
  175. * @param Object obj2
  176. *
  177. * Deep equal comparison. Two values are "deep equal" if:
  178. *
  179. * - They are equal, according to samsam.identical
  180. * - They are both date objects representing the same time
  181. * - They are both arrays containing elements that are all deepEqual
  182. * - They are objects with the same set of properties, and each property
  183. * in ``obj1`` is deepEqual to the corresponding property in ``obj2``
  184. *
  185. * Supports cyclic objects.
  186. */
  187. function deepEqualCyclic(obj1, obj2) {
  188. // used for cyclic comparison
  189. // contain already visited objects
  190. var objects1 = [],
  191. objects2 = [],
  192. // contain pathes (position in the object structure)
  193. // of the already visited objects
  194. // indexes same as in objects arrays
  195. paths1 = [],
  196. paths2 = [],
  197. // contains combinations of already compared objects
  198. // in the manner: { "$1['ref']$2['ref']": true }
  199. compared = {};
  200. /**
  201. * used to check, if the value of a property is an object
  202. * (cyclic logic is only needed for objects)
  203. * only needed for cyclic logic
  204. */
  205. function isObject(value) {
  206. if (typeof value === 'object' && value !== null &&
  207. !(value instanceof Boolean) &&
  208. !(value instanceof Date) &&
  209. !(value instanceof Number) &&
  210. !(value instanceof RegExp) &&
  211. !(value instanceof String)) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. /**
  217. * returns the index of the given object in the
  218. * given objects array, -1 if not contained
  219. * only needed for cyclic logic
  220. */
  221. function getIndex(objects, obj) {
  222. var i;
  223. for (i = 0; i < objects.length; i++) {
  224. if (objects[i] === obj) {
  225. return i;
  226. }
  227. }
  228. return -1;
  229. }
  230. // does the recursion for the deep equal check
  231. return (function deepEqual(obj1, obj2, path1, path2) {
  232. var type1 = typeof obj1;
  233. var type2 = typeof obj2;
  234. // == null also matches undefined
  235. if (obj1 === obj2 ||
  236. isNaN(obj1) || isNaN(obj2) ||
  237. obj1 == null || obj2 == null ||
  238. type1 !== "object" || type2 !== "object") {
  239. return identical(obj1, obj2);
  240. }
  241. // Elements are only equal if identical(expected, actual)
  242. if (isElement(obj1) || isElement(obj2)) { return false; }
  243. var isDate1 = isDate(obj1), isDate2 = isDate(obj2);
  244. if (isDate1 || isDate2) {
  245. if (!isDate1 || !isDate2 || obj1.getTime() !== obj2.getTime()) {
  246. return false;
  247. }
  248. }
  249. if (obj1 instanceof RegExp && obj2 instanceof RegExp) {
  250. if (obj1.toString() !== obj2.toString()) { return false; }
  251. }
  252. var class1 = getClass(obj1);
  253. var class2 = getClass(obj2);
  254. var keys1 = keys(obj1);
  255. var keys2 = keys(obj2);
  256. if (isArguments(obj1) || isArguments(obj2)) {
  257. if (obj1.length !== obj2.length) { return false; }
  258. } else {
  259. if (type1 !== type2 || class1 !== class2 ||
  260. keys1.length !== keys2.length) {
  261. return false;
  262. }
  263. }
  264. var key, i, l,
  265. // following vars are used for the cyclic logic
  266. value1, value2,
  267. isObject1, isObject2,
  268. index1, index2,
  269. newPath1, newPath2;
  270. for (i = 0, l = keys1.length; i < l; i++) {
  271. key = keys1[i];
  272. if (!o.hasOwnProperty.call(obj2, key)) {
  273. return false;
  274. }
  275. // Start of the cyclic logic
  276. value1 = obj1[key];
  277. value2 = obj2[key];
  278. isObject1 = isObject(value1);
  279. isObject2 = isObject(value2);
  280. // determine, if the objects were already visited
  281. // (it's faster to check for isObject first, than to
  282. // get -1 from getIndex for non objects)
  283. index1 = isObject1 ? getIndex(objects1, value1) : -1;
  284. index2 = isObject2 ? getIndex(objects2, value2) : -1;
  285. // determine the new pathes of the objects
  286. // - for non cyclic objects the current path will be extended
  287. // by current property name
  288. // - for cyclic objects the stored path is taken
  289. newPath1 = index1 !== -1
  290. ? paths1[index1]
  291. : path1 + '[' + JSON.stringify(key) + ']';
  292. newPath2 = index2 !== -1
  293. ? paths2[index2]
  294. : path2 + '[' + JSON.stringify(key) + ']';
  295. // stop recursion if current objects are already compared
  296. if (compared[newPath1 + newPath2]) {
  297. return true;
  298. }
  299. // remember the current objects and their pathes
  300. if (index1 === -1 && isObject1) {
  301. objects1.push(value1);
  302. paths1.push(newPath1);
  303. }
  304. if (index2 === -1 && isObject2) {
  305. objects2.push(value2);
  306. paths2.push(newPath2);
  307. }
  308. // remember that the current objects are already compared
  309. if (isObject1 && isObject2) {
  310. compared[newPath1 + newPath2] = true;
  311. }
  312. // End of cyclic logic
  313. // neither value1 nor value2 is a cycle
  314. // continue with next level
  315. if (!deepEqual(value1, value2, newPath1, newPath2)) {
  316. return false;
  317. }
  318. }
  319. return true;
  320. }(obj1, obj2, '$1', '$2'));
  321. }
  322. var match;
  323. function arrayContains(array, subset) {
  324. if (subset.length === 0) { return true; }
  325. var i, l, j, k;
  326. for (i = 0, l = array.length; i < l; ++i) {
  327. if (match(array[i], subset[0])) {
  328. for (j = 0, k = subset.length; j < k; ++j) {
  329. if (!match(array[i + j], subset[j])) { return false; }
  330. }
  331. return true;
  332. }
  333. }
  334. return false;
  335. }
  336. /**
  337. * @name samsam.match
  338. * @param Object object
  339. * @param Object matcher
  340. *
  341. * Compare arbitrary value ``object`` with matcher.
  342. */
  343. match = function match(object, matcher) {
  344. if (matcher && typeof matcher.test === "function") {
  345. return matcher.test(object);
  346. }
  347. if (typeof matcher === "function") {
  348. return matcher(object) === true;
  349. }
  350. if (typeof matcher === "string") {
  351. matcher = matcher.toLowerCase();
  352. var notNull = typeof object === "string" || !!object;
  353. return notNull &&
  354. (String(object)).toLowerCase().indexOf(matcher) >= 0;
  355. }
  356. if (typeof matcher === "number") {
  357. return matcher === object;
  358. }
  359. if (typeof matcher === "boolean") {
  360. return matcher === object;
  361. }
  362. if (typeof(matcher) === "undefined") {
  363. return typeof(object) === "undefined";
  364. }
  365. if (matcher === null) {
  366. return object === null;
  367. }
  368. if (getClass(object) === "Array" && getClass(matcher) === "Array") {
  369. return arrayContains(object, matcher);
  370. }
  371. if (matcher && typeof matcher === "object") {
  372. if (matcher === object) {
  373. return true;
  374. }
  375. var prop;
  376. for (prop in matcher) {
  377. var value = object[prop];
  378. if (typeof value === "undefined" &&
  379. typeof object.getAttribute === "function") {
  380. value = object.getAttribute(prop);
  381. }
  382. if (matcher[prop] === null || typeof matcher[prop] === 'undefined') {
  383. if (value !== matcher[prop]) {
  384. return false;
  385. }
  386. } else if (typeof value === "undefined" || !match(value, matcher[prop])) {
  387. return false;
  388. }
  389. }
  390. return true;
  391. }
  392. throw new Error("Matcher was not a string, a number, a " +
  393. "function, a boolean or an object");
  394. };
  395. return {
  396. isArguments: isArguments,
  397. isElement: isElement,
  398. isDate: isDate,
  399. isNegZero: isNegZero,
  400. identical: identical,
  401. deepEqual: deepEqualCyclic,
  402. match: match,
  403. keys: keys
  404. };
  405. });
  406. ((typeof define === "function" && define.amd && function (m) {
  407. define("formatio", ["samsam"], m);
  408. }) || (typeof module === "object" && function (m) {
  409. module.exports = m(require("samsam"));
  410. }) || function (m) { this.formatio = m(this.samsam); }
  411. )(function (samsam) {
  412. var formatio = {
  413. excludeConstructors: ["Object", /^.$/],
  414. quoteStrings: true,
  415. limitChildrenCount: 0
  416. };
  417. var hasOwn = Object.prototype.hasOwnProperty;
  418. var specialObjects = [];
  419. if (typeof global !== "undefined") {
  420. specialObjects.push({ object: global, value: "[object global]" });
  421. }
  422. if (typeof document !== "undefined") {
  423. specialObjects.push({
  424. object: document,
  425. value: "[object HTMLDocument]"
  426. });
  427. }
  428. if (typeof window !== "undefined") {
  429. specialObjects.push({ object: window, value: "[object Window]" });
  430. }
  431. function functionName(func) {
  432. if (!func) { return ""; }
  433. if (func.displayName) { return func.displayName; }
  434. if (func.name) { return func.name; }
  435. var matches = func.toString().match(/function\s+([^\(]+)/m);
  436. return (matches && matches[1]) || "";
  437. }
  438. function constructorName(f, object) {
  439. var name = functionName(object && object.constructor);
  440. var excludes = f.excludeConstructors ||
  441. formatio.excludeConstructors || [];
  442. var i, l;
  443. for (i = 0, l = excludes.length; i < l; ++i) {
  444. if (typeof excludes[i] === "string" && excludes[i] === name) {
  445. return "";
  446. } else if (excludes[i].test && excludes[i].test(name)) {
  447. return "";
  448. }
  449. }
  450. return name;
  451. }
  452. function isCircular(object, objects) {
  453. if (typeof object !== "object") { return false; }
  454. var i, l;
  455. for (i = 0, l = objects.length; i < l; ++i) {
  456. if (objects[i] === object) { return true; }
  457. }
  458. return false;
  459. }
  460. function ascii(f, object, processed, indent) {
  461. if (typeof object === "string") {
  462. var qs = f.quoteStrings;
  463. var quote = typeof qs !== "boolean" || qs;
  464. return processed || quote ? '"' + object + '"' : object;
  465. }
  466. if (typeof object === "function" && !(object instanceof RegExp)) {
  467. return ascii.func(object);
  468. }
  469. processed = processed || [];
  470. if (isCircular(object, processed)) { return "[Circular]"; }
  471. if (Object.prototype.toString.call(object) === "[object Array]") {
  472. return ascii.array.call(f, object, processed);
  473. }
  474. if (!object) { return String((1/object) === -Infinity ? "-0" : object); }
  475. if (samsam.isElement(object)) { return ascii.element(object); }
  476. if (typeof object.toString === "function" &&
  477. object.toString !== Object.prototype.toString) {
  478. return object.toString();
  479. }
  480. var i, l;
  481. for (i = 0, l = specialObjects.length; i < l; i++) {
  482. if (object === specialObjects[i].object) {
  483. return specialObjects[i].value;
  484. }
  485. }
  486. return ascii.object.call(f, object, processed, indent);
  487. }
  488. ascii.func = function (func) {
  489. return "function " + functionName(func) + "() {}";
  490. };
  491. ascii.array = function (array, processed) {
  492. processed = processed || [];
  493. processed.push(array);
  494. var pieces = [];
  495. var i, l;
  496. l = (this.limitChildrenCount > 0) ?
  497. Math.min(this.limitChildrenCount, array.length) : array.length;
  498. for (i = 0; i < l; ++i) {
  499. pieces.push(ascii(this, array[i], processed));
  500. }
  501. if(l < array.length)
  502. pieces.push("[... " + (array.length - l) + " more elements]");
  503. return "[" + pieces.join(", ") + "]";
  504. };
  505. ascii.object = function (object, processed, indent) {
  506. processed = processed || [];
  507. processed.push(object);
  508. indent = indent || 0;
  509. var pieces = [], properties = samsam.keys(object).sort();
  510. var length = 3;
  511. var prop, str, obj, i, k, l;
  512. l = (this.limitChildrenCount > 0) ?
  513. Math.min(this.limitChildrenCount, properties.length) : properties.length;
  514. for (i = 0; i < l; ++i) {
  515. prop = properties[i];
  516. obj = object[prop];
  517. if (isCircular(obj, processed)) {
  518. str = "[Circular]";
  519. } else {
  520. str = ascii(this, obj, processed, indent + 2);
  521. }
  522. str = (/\s/.test(prop) ? '"' + prop + '"' : prop) + ": " + str;
  523. length += str.length;
  524. pieces.push(str);
  525. }
  526. var cons = constructorName(this, object);
  527. var prefix = cons ? "[" + cons + "] " : "";
  528. var is = "";
  529. for (i = 0, k = indent; i < k; ++i) { is += " "; }
  530. if(l < properties.length)
  531. pieces.push("[... " + (properties.length - l) + " more elements]");
  532. if (length + indent > 80) {
  533. return prefix + "{\n " + is + pieces.join(",\n " + is) + "\n" +
  534. is + "}";
  535. }
  536. return prefix + "{ " + pieces.join(", ") + " }";
  537. };
  538. ascii.element = function (element) {
  539. var tagName = element.tagName.toLowerCase();
  540. var attrs = element.attributes, attr, pairs = [], attrName, i, l, val;
  541. for (i = 0, l = attrs.length; i < l; ++i) {
  542. attr = attrs.item(i);
  543. attrName = attr.nodeName.toLowerCase().replace("html:", "");
  544. val = attr.nodeValue;
  545. if (attrName !== "contenteditable" || val !== "inherit") {
  546. if (!!val) { pairs.push(attrName + "=\"" + val + "\""); }
  547. }
  548. }
  549. var formatted = "<" + tagName + (pairs.length > 0 ? " " : "");
  550. var content = element.innerHTML;
  551. if (content.length > 20) {
  552. content = content.substr(0, 20) + "[...]";
  553. }
  554. var res = formatted + pairs.join(" ") + ">" + content +
  555. "</" + tagName + ">";
  556. return res.replace(/ contentEditable="inherit"/, "");
  557. };
  558. function Formatio(options) {
  559. for (var opt in options) {
  560. this[opt] = options[opt];
  561. }
  562. }
  563. Formatio.prototype = {
  564. functionName: functionName,
  565. configure: function (options) {
  566. return new Formatio(options);
  567. },
  568. constructorName: function (object) {
  569. return constructorName(this, object);
  570. },
  571. ascii: function (object, processed, indent) {
  572. return ascii(this, object, processed, indent);
  573. }
  574. };
  575. return Formatio.prototype;
  576. });
  577. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.lolex=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  578. (function (global){
  579. /*global global, window*/
  580. /**
  581. * @author Christian Johansen (christian@cjohansen.no) and contributors
  582. * @license BSD
  583. *
  584. * Copyright (c) 2010-2014 Christian Johansen
  585. */
  586. (function (global) {
  587. // Make properties writable in IE, as per
  588. // http://www.adequatelygood.com/Replacing-setTimeout-Globally.html
  589. // JSLint being anal
  590. var glbl = global;
  591. global.setTimeout = glbl.setTimeout;
  592. global.clearTimeout = glbl.clearTimeout;
  593. global.setImmediate = glbl.setImmediate;
  594. global.clearImmediate = glbl.clearImmediate;
  595. global.setInterval = glbl.setInterval;
  596. global.clearInterval = glbl.clearInterval;
  597. global.Date = glbl.Date;
  598. // node expects setTimeout/setInterval to return a fn object w/ .ref()/.unref()
  599. // browsers, a number.
  600. // see https://github.com/cjohansen/Sinon.JS/pull/436
  601. var NOOP = function () { return undefined; };
  602. var timeoutResult = setTimeout(NOOP, 0);
  603. var addTimerReturnsObject = typeof timeoutResult === "object";
  604. clearTimeout(timeoutResult);
  605. var NativeDate = Date;
  606. var uniqueTimerId = 1;
  607. /**
  608. * Parse strings like "01:10:00" (meaning 1 hour, 10 minutes, 0 seconds) into
  609. * number of milliseconds. This is used to support human-readable strings passed
  610. * to clock.tick()
  611. */
  612. function parseTime(str) {
  613. if (!str) {
  614. return 0;
  615. }
  616. var strings = str.split(":");
  617. var l = strings.length, i = l;
  618. var ms = 0, parsed;
  619. if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) {
  620. throw new Error("tick only understands numbers and 'h:m:s'");
  621. }
  622. while (i--) {
  623. parsed = parseInt(strings[i], 10);
  624. if (parsed >= 60) {
  625. throw new Error("Invalid time " + str);
  626. }
  627. ms += parsed * Math.pow(60, (l - i - 1));
  628. }
  629. return ms * 1000;
  630. }
  631. /**
  632. * Used to grok the `now` parameter to createClock.
  633. */
  634. function getEpoch(epoch) {
  635. if (!epoch) { return 0; }
  636. if (typeof epoch.getTime === "function") { return epoch.getTime(); }
  637. if (typeof epoch === "number") { return epoch; }
  638. throw new TypeError("now should be milliseconds since UNIX epoch");
  639. }
  640. function inRange(from, to, timer) {
  641. return timer && timer.callAt >= from && timer.callAt <= to;
  642. }
  643. function mirrorDateProperties(target, source) {
  644. var prop;
  645. for (prop in source) {
  646. if (source.hasOwnProperty(prop)) {
  647. target[prop] = source[prop];
  648. }
  649. }
  650. // set special now implementation
  651. if (source.now) {
  652. target.now = function now() {
  653. return target.clock.now;
  654. };
  655. } else {
  656. delete target.now;
  657. }
  658. // set special toSource implementation
  659. if (source.toSource) {
  660. target.toSource = function toSource() {
  661. return source.toSource();
  662. };
  663. } else {
  664. delete target.toSource;
  665. }
  666. // set special toString implementation
  667. target.toString = function toString() {
  668. return source.toString();
  669. };
  670. target.prototype = source.prototype;
  671. target.parse = source.parse;
  672. target.UTC = source.UTC;
  673. target.prototype.toUTCString = source.prototype.toUTCString;
  674. return target;
  675. }
  676. function createDate() {
  677. function ClockDate(year, month, date, hour, minute, second, ms) {
  678. // Defensive and verbose to avoid potential harm in passing
  679. // explicit undefined when user does not pass argument
  680. switch (arguments.length) {
  681. case 0:
  682. return new NativeDate(ClockDate.clock.now);
  683. case 1:
  684. return new NativeDate(year);
  685. case 2:
  686. return new NativeDate(year, month);
  687. case 3:
  688. return new NativeDate(year, month, date);
  689. case 4:
  690. return new NativeDate(year, month, date, hour);
  691. case 5:
  692. return new NativeDate(year, month, date, hour, minute);
  693. case 6:
  694. return new NativeDate(year, month, date, hour, minute, second);
  695. default:
  696. return new NativeDate(year, month, date, hour, minute, second, ms);
  697. }
  698. }
  699. return mirrorDateProperties(ClockDate, NativeDate);
  700. }
  701. function addTimer(clock, timer) {
  702. if (timer.func === undefined) {
  703. throw new Error("Callback must be provided to timer calls");
  704. }
  705. if (!clock.timers) {
  706. clock.timers = {};
  707. }
  708. timer.id = uniqueTimerId++;
  709. timer.createdAt = clock.now;
  710. timer.callAt = clock.now + (timer.delay || (clock.duringTick ? 1 : 0));
  711. clock.timers[timer.id] = timer;
  712. if (addTimerReturnsObject) {
  713. return {
  714. id: timer.id,
  715. ref: NOOP,
  716. unref: NOOP
  717. };
  718. }
  719. return timer.id;
  720. }
  721. function compareTimers(a, b) {
  722. // Sort first by absolute timing
  723. if (a.callAt < b.callAt) {
  724. return -1;
  725. }
  726. if (a.callAt > b.callAt) {
  727. return 1;
  728. }
  729. // Sort next by immediate, immediate timers take precedence
  730. if (a.immediate && !b.immediate) {
  731. return -1;
  732. }
  733. if (!a.immediate && b.immediate) {
  734. return 1;
  735. }
  736. // Sort next by creation time, earlier-created timers take precedence
  737. if (a.createdAt < b.createdAt) {
  738. return -1;
  739. }
  740. if (a.createdAt > b.createdAt) {
  741. return 1;
  742. }
  743. // Sort next by id, lower-id timers take precedence
  744. if (a.id < b.id) {
  745. return -1;
  746. }
  747. if (a.id > b.id) {
  748. return 1;
  749. }
  750. // As timer ids are unique, no fallback `0` is necessary
  751. }
  752. function firstTimerInRange(clock, from, to) {
  753. var timers = clock.timers,
  754. timer = null,
  755. id,
  756. isInRange;
  757. for (id in timers) {
  758. if (timers.hasOwnProperty(id)) {
  759. isInRange = inRange(from, to, timers[id]);
  760. if (isInRange && (!timer || compareTimers(timer, timers[id]) === 1)) {
  761. timer = timers[id];
  762. }
  763. }
  764. }
  765. return timer;
  766. }
  767. function callTimer(clock, timer) {
  768. var exception;
  769. if (typeof timer.interval === "number") {
  770. clock.timers[timer.id].callAt += timer.interval;
  771. } else {
  772. delete clock.timers[timer.id];
  773. }
  774. try {
  775. if (typeof timer.func === "function") {
  776. timer.func.apply(null, timer.args);
  777. } else {
  778. eval(timer.func);
  779. }
  780. } catch (e) {
  781. exception = e;
  782. }
  783. if (!clock.timers[timer.id]) {
  784. if (exception) {
  785. throw exception;
  786. }
  787. return;
  788. }
  789. if (exception) {
  790. throw exception;
  791. }
  792. }
  793. function uninstall(clock, target) {
  794. var method,
  795. i,
  796. l;
  797. for (i = 0, l = clock.methods.length; i < l; i++) {
  798. method = clock.methods[i];
  799. if (target[method].hadOwnProperty) {
  800. target[method] = clock["_" + method];
  801. } else {
  802. try {
  803. delete target[method];
  804. } catch (ignore) {}
  805. }
  806. }
  807. // Prevent multiple executions which will completely remove these props
  808. clock.methods = [];
  809. }
  810. function hijackMethod(target, method, clock) {
  811. var prop;
  812. clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(target, method);
  813. clock["_" + method] = target[method];
  814. if (method === "Date") {
  815. var date = mirrorDateProperties(clock[method], target[method]);
  816. target[method] = date;
  817. } else {
  818. target[method] = function () {
  819. return clock[method].apply(clock, arguments);
  820. };
  821. for (prop in clock[method]) {
  822. if (clock[method].hasOwnProperty(prop)) {
  823. target[method][prop] = clock[method][prop];
  824. }
  825. }
  826. }
  827. target[method].clock = clock;
  828. }
  829. var timers = {
  830. setTimeout: setTimeout,
  831. clearTimeout: clearTimeout,
  832. setImmediate: global.setImmediate,
  833. clearImmediate: global.clearImmediate,
  834. setInterval: setInterval,
  835. clearInterval: clearInterval,
  836. Date: Date
  837. };
  838. var keys = Object.keys || function (obj) {
  839. var ks = [],
  840. key;
  841. for (key in obj) {
  842. if (obj.hasOwnProperty(key)) {
  843. ks.push(key);
  844. }
  845. }
  846. return ks;
  847. };
  848. exports.timers = timers;
  849. function createClock(now) {
  850. var clock = {
  851. now: getEpoch(now),
  852. timeouts: {},
  853. Date: createDate()
  854. };
  855. clock.Date.clock = clock;
  856. clock.setTimeout = function setTimeout(func, timeout) {
  857. return addTimer(clock, {
  858. func: func,
  859. args: Array.prototype.slice.call(arguments, 2),
  860. delay: timeout
  861. });
  862. };
  863. clock.clearTimeout = function clearTimeout(timerId) {
  864. if (!timerId) {
  865. // null appears to be allowed in most browsers, and appears to be
  866. // relied upon by some libraries, like Bootstrap carousel
  867. return;
  868. }
  869. if (!clock.timers) {
  870. clock.timers = [];
  871. }
  872. // in Node, timerId is an object with .ref()/.unref(), and
  873. // its .id field is the actual timer id.
  874. if (typeof timerId === "object") {
  875. timerId = timerId.id;
  876. }
  877. if (clock.timers.hasOwnProperty(timerId)) {
  878. delete clock.timers[timerId];
  879. }
  880. };
  881. clock.setInterval = function setInterval(func, timeout) {
  882. return addTimer(clock, {
  883. func: func,
  884. args: Array.prototype.slice.call(arguments, 2),
  885. delay: timeout,
  886. interval: timeout
  887. });
  888. };
  889. clock.clearInterval = function clearInterval(timerId) {
  890. clock.clearTimeout(timerId);
  891. };
  892. clock.setImmediate = function setImmediate(func) {
  893. return addTimer(clock, {
  894. func: func,
  895. args: Array.prototype.slice.call(arguments, 1),
  896. immediate: true
  897. });
  898. };
  899. clock.clearImmediate = function clearImmediate(timerId) {
  900. clock.clearTimeout(timerId);
  901. };
  902. clock.tick = function tick(ms) {
  903. ms = typeof ms === "number" ? ms : parseTime(ms);
  904. var tickFrom = clock.now, tickTo = clock.now + ms, previous = clock.now;
  905. var timer = firstTimerInRange(clock, tickFrom, tickTo);
  906. clock.duringTick = true;
  907. var firstException;
  908. while (timer && tickFrom <= tickTo) {
  909. if (clock.timers[timer.id]) {
  910. tickFrom = clock.now = timer.callAt;
  911. try {
  912. callTimer(clock, timer);
  913. } catch (e) {
  914. firstException = firstException || e;
  915. }
  916. }
  917. timer = firstTimerInRange(clock, previous, tickTo);
  918. previous = tickFrom;
  919. }
  920. clock.duringTick = false;
  921. clock.now = tickTo;
  922. if (firstException) {
  923. throw firstException;
  924. }
  925. return clock.now;
  926. };
  927. clock.reset = function reset() {
  928. clock.timers = {};
  929. };
  930. return clock;
  931. }
  932. exports.createClock = createClock;
  933. function detectKnownFailSituation(methods) {
  934. if (methods.indexOf("Date") < 0) { return; }
  935. if (methods.indexOf("setTimeout") < 0) {
  936. throw new Error("Native setTimeout will not work when Date is faked");
  937. }
  938. if (methods.indexOf("setImmediate") < 0) {
  939. throw new Error("Native setImmediate will not work when Date is faked");
  940. }
  941. }
  942. exports.install = function install(target, now, toFake) {
  943. var i,
  944. l;
  945. if (typeof target === "number") {
  946. toFake = now;
  947. now = target;
  948. target = null;
  949. }
  950. if (!target) {
  951. target = global;
  952. }
  953. var clock = createClock(now);
  954. clock.uninstall = function () {
  955. uninstall(clock, target);
  956. };
  957. clock.methods = toFake || [];
  958. if (clock.methods.length === 0) {
  959. clock.methods = keys(timers);
  960. }
  961. detectKnownFailSituation(clock.methods);
  962. for (i = 0, l = clock.methods.length; i < l; i++) {
  963. hijackMethod(target, clock.methods[i], clock);
  964. }
  965. return clock;
  966. };
  967. }(global || this));
  968. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  969. },{}]},{},[1])(1)
  970. });
  971. })();
  972. var define;
  973. /**
  974. * Sinon core utilities. For internal use only.
  975. *
  976. * @author Christian Johansen (christian@cjohansen.no)
  977. * @license BSD
  978. *
  979. * Copyright (c) 2010-2013 Christian Johansen
  980. */
  981. var sinon = (function () {
  982. "use strict";
  983. // eslint-disable-line no-unused-vars
  984. var sinonModule;
  985. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  986. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  987. function loadDependencies(require, exports, module) {
  988. sinonModule = module.exports = require("./sinon/util/core");
  989. require("./sinon/extend");
  990. require("./sinon/typeOf");
  991. require("./sinon/times_in_words");
  992. require("./sinon/spy");
  993. require("./sinon/call");
  994. require("./sinon/behavior");
  995. require("./sinon/stub");
  996. require("./sinon/mock");
  997. require("./sinon/collection");
  998. require("./sinon/assert");
  999. require("./sinon/sandbox");
  1000. require("./sinon/test");
  1001. require("./sinon/test_case");
  1002. require("./sinon/match");
  1003. require("./sinon/format");
  1004. require("./sinon/log_error");
  1005. }
  1006. if (isAMD) {
  1007. define(loadDependencies);
  1008. } else if (isNode) {
  1009. loadDependencies(require, module.exports, module);
  1010. sinonModule = module.exports;
  1011. } else {
  1012. sinonModule = {};
  1013. }
  1014. return sinonModule;
  1015. }());
  1016. /**
  1017. * @depend ../../sinon.js
  1018. */
  1019. /**
  1020. * Sinon core utilities. For internal use only.
  1021. *
  1022. * @author Christian Johansen (christian@cjohansen.no)
  1023. * @license BSD
  1024. *
  1025. * Copyright (c) 2010-2013 Christian Johansen
  1026. */
  1027. (function (sinonGlobal) {
  1028. var div = typeof document !== "undefined" && document.createElement("div");
  1029. var hasOwn = Object.prototype.hasOwnProperty;
  1030. function isDOMNode(obj) {
  1031. var success = false;
  1032. try {
  1033. obj.appendChild(div);
  1034. success = div.parentNode === obj;
  1035. } catch (e) {
  1036. return false;
  1037. } finally {
  1038. try {
  1039. obj.removeChild(div);
  1040. } catch (e) {
  1041. // Remove failed, not much we can do about that
  1042. }
  1043. }
  1044. return success;
  1045. }
  1046. function isElement(obj) {
  1047. return div && obj && obj.nodeType === 1 && isDOMNode(obj);
  1048. }
  1049. function isFunction(obj) {
  1050. return typeof obj === "function" || !!(obj && obj.constructor && obj.call && obj.apply);
  1051. }
  1052. function isReallyNaN(val) {
  1053. return typeof val === "number" && isNaN(val);
  1054. }
  1055. function mirrorProperties(target, source) {
  1056. for (var prop in source) {
  1057. if (!hasOwn.call(target, prop)) {
  1058. target[prop] = source[prop];
  1059. }
  1060. }
  1061. }
  1062. function isRestorable(obj) {
  1063. return typeof obj === "function" && typeof obj.restore === "function" && obj.restore.sinon;
  1064. }
  1065. // Cheap way to detect if we have ES5 support.
  1066. var hasES5Support = "keys" in Object;
  1067. function makeApi(sinon) {
  1068. sinon.wrapMethod = function wrapMethod(object, property, method) {
  1069. if (!object) {
  1070. throw new TypeError("Should wrap property of object");
  1071. }
  1072. if (typeof method !== "function" && typeof method !== "object") {
  1073. throw new TypeError("Method wrapper should be a function or a property descriptor");
  1074. }
  1075. function checkWrappedMethod(wrappedMethod) {
  1076. var error;
  1077. if (!isFunction(wrappedMethod)) {
  1078. error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
  1079. property + " as function");
  1080. } else if (wrappedMethod.restore && wrappedMethod.restore.sinon) {
  1081. error = new TypeError("Attempted to wrap " + property + " which is already wrapped");
  1082. } else if (wrappedMethod.calledBefore) {
  1083. var verb = wrappedMethod.returns ? "stubbed" : "spied on";
  1084. error = new TypeError("Attempted to wrap " + property + " which is already " + verb);
  1085. }
  1086. if (error) {
  1087. if (wrappedMethod && wrappedMethod.stackTrace) {
  1088. error.stack += "\n--------------\n" + wrappedMethod.stackTrace;
  1089. }
  1090. throw error;
  1091. }
  1092. }
  1093. var error, wrappedMethod, i;
  1094. // IE 8 does not support hasOwnProperty on the window object and Firefox has a problem
  1095. // when using hasOwn.call on objects from other frames.
  1096. var owned = object.hasOwnProperty ? object.hasOwnProperty(property) : hasOwn.call(object, property);
  1097. if (hasES5Support) {
  1098. var methodDesc = (typeof method === "function") ? {value: method} : method;
  1099. var wrappedMethodDesc = sinon.getPropertyDescriptor(object, property);
  1100. if (!wrappedMethodDesc) {
  1101. error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
  1102. property + " as function");
  1103. } else if (wrappedMethodDesc.restore && wrappedMethodDesc.restore.sinon) {
  1104. error = new TypeError("Attempted to wrap " + property + " which is already wrapped");
  1105. }
  1106. if (error) {
  1107. if (wrappedMethodDesc && wrappedMethodDesc.stackTrace) {
  1108. error.stack += "\n--------------\n" + wrappedMethodDesc.stackTrace;
  1109. }
  1110. throw error;
  1111. }
  1112. var types = sinon.objectKeys(methodDesc);
  1113. for (i = 0; i < types.length; i++) {
  1114. wrappedMethod = wrappedMethodDesc[types[i]];
  1115. checkWrappedMethod(wrappedMethod);
  1116. }
  1117. mirrorProperties(methodDesc, wrappedMethodDesc);
  1118. for (i = 0; i < types.length; i++) {
  1119. mirrorProperties(methodDesc[types[i]], wrappedMethodDesc[types[i]]);
  1120. }
  1121. Object.defineProperty(object, property, methodDesc);
  1122. } else {
  1123. wrappedMethod = object[property];
  1124. checkWrappedMethod(wrappedMethod);
  1125. object[property] = method;
  1126. method.displayName = property;
  1127. }
  1128. method.displayName = property;
  1129. // Set up a stack trace which can be used later to find what line of
  1130. // code the original method was created on.
  1131. method.stackTrace = (new Error("Stack Trace for original")).stack;
  1132. method.restore = function () {
  1133. // For prototype properties try to reset by delete first.
  1134. // If this fails (ex: localStorage on mobile safari) then force a reset
  1135. // via direct assignment.
  1136. if (!owned) {
  1137. // In some cases `delete` may throw an error
  1138. try {
  1139. delete object[property];
  1140. } catch (e) {} // eslint-disable-line no-empty
  1141. // For native code functions `delete` fails without throwing an error
  1142. // on Chrome < 43, PhantomJS, etc.
  1143. } else if (hasES5Support) {
  1144. Object.defineProperty(object, property, wrappedMethodDesc);
  1145. }
  1146. // Use strict equality comparison to check failures then force a reset
  1147. // via direct assignment.
  1148. if (object[property] === method) {
  1149. object[property] = wrappedMethod;
  1150. }
  1151. };
  1152. method.restore.sinon = true;
  1153. if (!hasES5Support) {
  1154. mirrorProperties(method, wrappedMethod);
  1155. }
  1156. return method;
  1157. };
  1158. sinon.create = function create(proto) {
  1159. var F = function () {};
  1160. F.prototype = proto;
  1161. return new F();
  1162. };
  1163. sinon.deepEqual = function deepEqual(a, b) {
  1164. if (sinon.match && sinon.match.isMatcher(a)) {
  1165. return a.test(b);
  1166. }
  1167. if (typeof a !== "object" || typeof b !== "object") {
  1168. return isReallyNaN(a) && isReallyNaN(b) || a === b;
  1169. }
  1170. if (isElement(a) || isElement(b)) {
  1171. return a === b;
  1172. }
  1173. if (a === b) {
  1174. return true;
  1175. }
  1176. if ((a === null && b !== null) || (a !== null && b === null)) {
  1177. return false;
  1178. }
  1179. if (a instanceof RegExp && b instanceof RegExp) {
  1180. return (a.source === b.source) && (a.global === b.global) &&
  1181. (a.ignoreCase === b.ignoreCase) && (a.multiline === b.multiline);
  1182. }
  1183. var aString = Object.prototype.toString.call(a);
  1184. if (aString !== Object.prototype.toString.call(b)) {
  1185. return false;
  1186. }
  1187. if (aString === "[object Date]") {
  1188. return a.valueOf() === b.valueOf();
  1189. }
  1190. var prop;
  1191. var aLength = 0;
  1192. var bLength = 0;
  1193. if (aString === "[object Array]" && a.length !== b.length) {
  1194. return false;
  1195. }
  1196. for (prop in a) {
  1197. if (a.hasOwnProperty(prop)) {
  1198. aLength += 1;
  1199. if (!(prop in b)) {
  1200. return false;
  1201. }
  1202. if (!deepEqual(a[prop], b[prop])) {
  1203. return false;
  1204. }
  1205. }
  1206. }
  1207. for (prop in b) {
  1208. if (b.hasOwnProperty(prop)) {
  1209. bLength += 1;
  1210. }
  1211. }
  1212. return aLength === bLength;
  1213. };
  1214. sinon.functionName = function functionName(func) {
  1215. var name = func.displayName || func.name;
  1216. // Use function decomposition as a last resort to get function
  1217. // name. Does not rely on function decomposition to work - if it
  1218. // doesn't debugging will be slightly less informative
  1219. // (i.e. toString will say 'spy' rather than 'myFunc').
  1220. if (!name) {
  1221. var matches = func.toString().match(/function ([^\s\(]+)/);
  1222. name = matches && matches[1];
  1223. }
  1224. return name;
  1225. };
  1226. sinon.functionToString = function toString() {
  1227. if (this.getCall && this.callCount) {
  1228. var thisValue,
  1229. prop;
  1230. var i = this.callCount;
  1231. while (i--) {
  1232. thisValue = this.getCall(i).thisValue;
  1233. for (prop in thisValue) {
  1234. if (thisValue[prop] === this) {
  1235. return prop;
  1236. }
  1237. }
  1238. }
  1239. }
  1240. return this.displayName || "sinon fake";
  1241. };
  1242. sinon.objectKeys = function objectKeys(obj) {
  1243. if (obj !== Object(obj)) {
  1244. throw new TypeError("sinon.objectKeys called on a non-object");
  1245. }
  1246. var keys = [];
  1247. var key;
  1248. for (key in obj) {
  1249. if (hasOwn.call(obj, key)) {
  1250. keys.push(key);
  1251. }
  1252. }
  1253. return keys;
  1254. };
  1255. sinon.getPropertyDescriptor = function getPropertyDescriptor(object, property) {
  1256. var proto = object;
  1257. var descriptor;
  1258. while (proto && !(descriptor = Object.getOwnPropertyDescriptor(proto, property))) {
  1259. proto = Object.getPrototypeOf(proto);
  1260. }
  1261. return descriptor;
  1262. };
  1263. sinon.getConfig = function (custom) {
  1264. var config = {};
  1265. custom = custom || {};
  1266. var defaults = sinon.defaultConfig;
  1267. for (var prop in defaults) {
  1268. if (defaults.hasOwnProperty(prop)) {
  1269. config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaults[prop];
  1270. }
  1271. }
  1272. return config;
  1273. };
  1274. sinon.defaultConfig = {
  1275. injectIntoThis: true,
  1276. injectInto: null,
  1277. properties: ["spy", "stub", "mock", "clock", "server", "requests"],
  1278. useFakeTimers: true,
  1279. useFakeServer: true
  1280. };
  1281. sinon.timesInWords = function timesInWords(count) {
  1282. return count === 1 && "once" ||
  1283. count === 2 && "twice" ||
  1284. count === 3 && "thrice" ||
  1285. (count || 0) + " times";
  1286. };
  1287. sinon.calledInOrder = function (spies) {
  1288. for (var i = 1, l = spies.length; i < l; i++) {
  1289. if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) {
  1290. return false;
  1291. }
  1292. }
  1293. return true;
  1294. };
  1295. sinon.orderByFirstCall = function (spies) {
  1296. return spies.sort(function (a, b) {
  1297. // uuid, won't ever be equal
  1298. var aCall = a.getCall(0);
  1299. var bCall = b.getCall(0);
  1300. var aId = aCall && aCall.callId || -1;
  1301. var bId = bCall && bCall.callId || -1;
  1302. return aId < bId ? -1 : 1;
  1303. });
  1304. };
  1305. sinon.createStubInstance = function (constructor) {
  1306. if (typeof constructor !== "function") {
  1307. throw new TypeError("The constructor should be a function.");
  1308. }
  1309. return sinon.stub(sinon.create(constructor.prototype));
  1310. };
  1311. sinon.restore = function (object) {
  1312. if (object !== null && typeof object === "object") {
  1313. for (var prop in object) {
  1314. if (isRestorable(object[prop])) {
  1315. object[prop].restore();
  1316. }
  1317. }
  1318. } else if (isRestorable(object)) {
  1319. object.restore();
  1320. }
  1321. };
  1322. return sinon;
  1323. }
  1324. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  1325. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  1326. function loadDependencies(require, exports) {
  1327. makeApi(exports);
  1328. }
  1329. if (isAMD) {
  1330. define(loadDependencies);
  1331. return;
  1332. }
  1333. if (isNode) {
  1334. loadDependencies(require, module.exports, module);
  1335. return;
  1336. }
  1337. if (sinonGlobal) {
  1338. makeApi(sinonGlobal);
  1339. }
  1340. }(
  1341. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  1342. ));
  1343. /**
  1344. * @depend util/core.js
  1345. */
  1346. (function (sinonGlobal) {
  1347. function makeApi(sinon) {
  1348. // Adapted from https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
  1349. var hasDontEnumBug = (function () {
  1350. var obj = {
  1351. constructor: function () {
  1352. return "0";
  1353. },
  1354. toString: function () {
  1355. return "1";
  1356. },
  1357. valueOf: function () {
  1358. return "2";
  1359. },
  1360. toLocaleString: function () {
  1361. return "3";
  1362. },
  1363. prototype: function () {
  1364. return "4";
  1365. },
  1366. isPrototypeOf: function () {
  1367. return "5";
  1368. },
  1369. propertyIsEnumerable: function () {
  1370. return "6";
  1371. },
  1372. hasOwnProperty: function () {
  1373. return "7";
  1374. },
  1375. length: function () {
  1376. return "8";
  1377. },
  1378. unique: function () {
  1379. return "9";
  1380. }
  1381. };
  1382. var result = [];
  1383. for (var prop in obj) {
  1384. if (obj.hasOwnProperty(prop)) {
  1385. result.push(obj[prop]());
  1386. }
  1387. }
  1388. return result.join("") !== "0123456789";
  1389. })();
  1390. /* Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will
  1391. * override properties in previous sources.
  1392. *
  1393. * target - The Object to extend
  1394. * sources - Objects to copy properties from.
  1395. *
  1396. * Returns the extended target
  1397. */
  1398. function extend(target /*, sources */) {
  1399. var sources = Array.prototype.slice.call(arguments, 1);
  1400. var source, i, prop;
  1401. for (i = 0; i < sources.length; i++) {
  1402. source = sources[i];
  1403. for (prop in source) {
  1404. if (source.hasOwnProperty(prop)) {
  1405. target[prop] = source[prop];
  1406. }
  1407. }
  1408. // Make sure we copy (own) toString method even when in JScript with DontEnum bug
  1409. // See https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
  1410. if (hasDontEnumBug && source.hasOwnProperty("toString") && source.toString !== target.toString) {
  1411. target.toString = source.toString;
  1412. }
  1413. }
  1414. return target;
  1415. }
  1416. sinon.extend = extend;
  1417. return sinon.extend;
  1418. }
  1419. function loadDependencies(require, exports, module) {
  1420. var sinon = require("./util/core");
  1421. module.exports = makeApi(sinon);
  1422. }
  1423. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  1424. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  1425. if (isAMD) {
  1426. define(loadDependencies);
  1427. return;
  1428. }
  1429. if (isNode) {
  1430. loadDependencies(require, module.exports, module);
  1431. return;
  1432. }
  1433. if (sinonGlobal) {
  1434. makeApi(sinonGlobal);
  1435. }
  1436. }(
  1437. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  1438. ));
  1439. /**
  1440. * @depend util/core.js
  1441. */
  1442. (function (sinonGlobal) {
  1443. function makeApi(sinon) {
  1444. function timesInWords(count) {
  1445. switch (count) {
  1446. case 1:
  1447. return "once";
  1448. case 2:
  1449. return "twice";
  1450. case 3:
  1451. return "thrice";
  1452. default:
  1453. return (count || 0) + " times";
  1454. }
  1455. }
  1456. sinon.timesInWords = timesInWords;
  1457. return sinon.timesInWords;
  1458. }
  1459. function loadDependencies(require, exports, module) {
  1460. var core = require("./util/core");
  1461. module.exports = makeApi(core);
  1462. }
  1463. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  1464. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  1465. if (isAMD) {
  1466. define(loadDependencies);
  1467. return;
  1468. }
  1469. if (isNode) {
  1470. loadDependencies(require, module.exports, module);
  1471. return;
  1472. }
  1473. if (sinonGlobal) {
  1474. makeApi(sinonGlobal);
  1475. }
  1476. }(
  1477. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  1478. ));
  1479. /**
  1480. * @depend util/core.js
  1481. */
  1482. /**
  1483. * Format functions
  1484. *
  1485. * @author Christian Johansen (christian@cjohansen.no)
  1486. * @license BSD
  1487. *
  1488. * Copyright (c) 2010-2014 Christian Johansen
  1489. */
  1490. (function (sinonGlobal) {
  1491. function makeApi(sinon) {
  1492. function typeOf(value) {
  1493. if (value === null) {
  1494. return "null";
  1495. } else if (value === undefined) {
  1496. return "undefined";
  1497. }
  1498. var string = Object.prototype.toString.call(value);
  1499. return string.substring(8, string.length - 1).toLowerCase();
  1500. }
  1501. sinon.typeOf = typeOf;
  1502. return sinon.typeOf;
  1503. }
  1504. function loadDependencies(require, exports, module) {
  1505. var core = require("./util/core");
  1506. module.exports = makeApi(core);
  1507. }
  1508. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  1509. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  1510. if (isAMD) {
  1511. define(loadDependencies);
  1512. return;
  1513. }
  1514. if (isNode) {
  1515. loadDependencies(require, module.exports, module);
  1516. return;
  1517. }
  1518. if (sinonGlobal) {
  1519. makeApi(sinonGlobal);
  1520. }
  1521. }(
  1522. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  1523. ));
  1524. /**
  1525. * @depend util/core.js
  1526. * @depend typeOf.js
  1527. */
  1528. /*jslint eqeqeq: false, onevar: false, plusplus: false*/
  1529. /*global module, require, sinon*/
  1530. /**
  1531. * Match functions
  1532. *
  1533. * @author Maximilian Antoni (mail@maxantoni.de)
  1534. * @license BSD
  1535. *
  1536. * Copyright (c) 2012 Maximilian Antoni
  1537. */
  1538. (function (sinonGlobal) {
  1539. function makeApi(sinon) {
  1540. function assertType(value, type, name) {
  1541. var actual = sinon.typeOf(value);
  1542. if (actual !== type) {
  1543. throw new TypeError("Expected type of " + name + " to be " +
  1544. type + ", but was " + actual);
  1545. }
  1546. }
  1547. var matcher = {
  1548. toString: function () {
  1549. return this.message;
  1550. }
  1551. };
  1552. function isMatcher(object) {
  1553. return matcher.isPrototypeOf(object);
  1554. }
  1555. function matchObject(expectation, actual) {
  1556. if (actual === null || actual === undefined) {
  1557. return false;
  1558. }
  1559. for (var key in expectation) {
  1560. if (expectation.hasOwnProperty(key)) {
  1561. var exp = expectation[key];
  1562. var act = actual[key];
  1563. if (isMatcher(exp)) {
  1564. if (!exp.test(act)) {
  1565. return false;
  1566. }
  1567. } else if (sinon.typeOf(exp) === "object") {
  1568. if (!matchObject(exp, act)) {
  1569. return false;
  1570. }
  1571. } else if (!sinon.deepEqual(exp, act)) {
  1572. return false;
  1573. }
  1574. }
  1575. }
  1576. return true;
  1577. }
  1578. function match(expectation, message) {
  1579. var m = sinon.create(matcher);
  1580. var type = sinon.typeOf(expectation);
  1581. switch (type) {
  1582. case "object":
  1583. if (typeof expectation.test === "function") {
  1584. m.test = function (actual) {
  1585. return expectation.test(actual) === true;
  1586. };
  1587. m.message = "match(" + sinon.functionName(expectation.test) + ")";
  1588. return m;
  1589. }
  1590. var str = [];
  1591. for (var key in expectation) {
  1592. if (expectation.hasOwnProperty(key)) {
  1593. str.push(key + ": " + expectation[key]);
  1594. }
  1595. }
  1596. m.test = function (actual) {
  1597. return matchObject(expectation, actual);
  1598. };
  1599. m.message = "match(" + str.join(", ") + ")";
  1600. break;
  1601. case "number":
  1602. m.test = function (actual) {
  1603. // we need type coercion here
  1604. return expectation == actual; // eslint-disable-line eqeqeq
  1605. };
  1606. break;
  1607. case "string":
  1608. m.test = function (actual) {
  1609. if (typeof actual !== "string") {
  1610. return false;
  1611. }
  1612. return actual.indexOf(expectation) !== -1;
  1613. };
  1614. m.message = "match(\"" + expectation + "\")";
  1615. break;
  1616. case "regexp":
  1617. m.test = function (actual) {
  1618. if (typeof actual !== "string") {
  1619. return false;
  1620. }
  1621. return expectation.test(actual);
  1622. };
  1623. break;
  1624. case "function":
  1625. m.test = expectation;
  1626. if (message) {
  1627. m.message = message;
  1628. } else {
  1629. m.message = "match(" + sinon.functionName(expectation) + ")";
  1630. }
  1631. break;
  1632. default:
  1633. m.test = function (actual) {
  1634. return sinon.deepEqual(expectation, actual);
  1635. };
  1636. }
  1637. if (!m.message) {
  1638. m.message = "match(" + expectation + ")";
  1639. }
  1640. return m;
  1641. }
  1642. matcher.or = function (m2) {
  1643. if (!arguments.length) {
  1644. throw new TypeError("Matcher expected");
  1645. } else if (!isMatcher(m2)) {
  1646. m2 = match(m2);
  1647. }
  1648. var m1 = this;
  1649. var or = sinon.create(matcher);
  1650. or.test = function (actual) {
  1651. return m1.test(actual) || m2.test(actual);
  1652. };
  1653. or.message = m1.message + ".or(" + m2.message + ")";
  1654. return or;
  1655. };
  1656. matcher.and = function (m2) {
  1657. if (!arguments.length) {
  1658. throw new TypeError("Matcher expected");
  1659. } else if (!isMatcher(m2)) {
  1660. m2 = match(m2);
  1661. }
  1662. var m1 = this;
  1663. var and = sinon.create(matcher);
  1664. and.test = function (actual) {
  1665. return m1.test(actual) && m2.test(actual);
  1666. };
  1667. and.message = m1.message + ".and(" + m2.message + ")";
  1668. return and;
  1669. };
  1670. match.isMatcher = isMatcher;
  1671. match.any = match(function () {
  1672. return true;
  1673. }, "any");
  1674. match.defined = match(function (actual) {
  1675. return actual !== null && actual !== undefined;
  1676. }, "defined");
  1677. match.truthy = match(function (actual) {
  1678. return !!actual;
  1679. }, "truthy");
  1680. match.falsy = match(function (actual) {
  1681. return !actual;
  1682. }, "falsy");
  1683. match.same = function (expectation) {
  1684. return match(function (actual) {
  1685. return expectation === actual;
  1686. }, "same(" + expectation + ")");
  1687. };
  1688. match.typeOf = function (type) {
  1689. assertType(type, "string", "type");
  1690. return match(function (actual) {
  1691. return sinon.typeOf(actual) === type;
  1692. }, "typeOf(\"" + type + "\")");
  1693. };
  1694. match.instanceOf = function (type) {
  1695. assertType(type, "function", "type");
  1696. return match(function (actual) {
  1697. return actual instanceof type;
  1698. }, "instanceOf(" + sinon.functionName(type) + ")");
  1699. };
  1700. function createPropertyMatcher(propertyTest, messagePrefix) {
  1701. return function (property, value) {
  1702. assertType(property, "string", "property");
  1703. var onlyProperty = arguments.length === 1;
  1704. var message = messagePrefix + "(\"" + property + "\"";
  1705. if (!onlyProperty) {
  1706. message += ", " + value;
  1707. }
  1708. message += ")";
  1709. return match(function (actual) {
  1710. if (actual === undefined || actual === null ||
  1711. !propertyTest(actual, property)) {
  1712. return false;
  1713. }
  1714. return onlyProperty || sinon.deepEqual(value, actual[property]);
  1715. }, message);
  1716. };
  1717. }
  1718. match.has = createPropertyMatcher(function (actual, property) {
  1719. if (typeof actual === "object") {
  1720. return property in actual;
  1721. }
  1722. return actual[property] !== undefined;
  1723. }, "has");
  1724. match.hasOwn = createPropertyMatcher(function (actual, property) {
  1725. return actual.hasOwnProperty(property);
  1726. }, "hasOwn");
  1727. match.bool = match.typeOf("boolean");
  1728. match.number = match.typeOf("number");
  1729. match.string = match.typeOf("string");
  1730. match.object = match.typeOf("object");
  1731. match.func = match.typeOf("function");
  1732. match.array = match.typeOf("array");
  1733. match.regexp = match.typeOf("regexp");
  1734. match.date = match.typeOf("date");
  1735. sinon.match = match;
  1736. return match;
  1737. }
  1738. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  1739. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  1740. function loadDependencies(require, exports, module) {
  1741. var sinon = require("./util/core");
  1742. require("./typeOf");
  1743. module.exports = makeApi(sinon);
  1744. }
  1745. if (isAMD) {
  1746. define(loadDependencies);
  1747. return;
  1748. }
  1749. if (isNode) {
  1750. loadDependencies(require, module.exports, module);
  1751. return;
  1752. }
  1753. if (sinonGlobal) {
  1754. makeApi(sinonGlobal);
  1755. }
  1756. }(
  1757. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  1758. ));
  1759. /**
  1760. * @depend util/core.js
  1761. */
  1762. /**
  1763. * Format functions
  1764. *
  1765. * @author Christian Johansen (christian@cjohansen.no)
  1766. * @license BSD
  1767. *
  1768. * Copyright (c) 2010-2014 Christian Johansen
  1769. */
  1770. (function (sinonGlobal, formatio) {
  1771. function makeApi(sinon) {
  1772. function valueFormatter(value) {
  1773. return "" + value;
  1774. }
  1775. function getFormatioFormatter() {
  1776. var formatter = formatio.configure({
  1777. quoteStrings: false,
  1778. limitChildrenCount: 250
  1779. });
  1780. function format() {
  1781. return formatter.ascii.apply(formatter, arguments);
  1782. }
  1783. return format;
  1784. }
  1785. function getNodeFormatter() {
  1786. try {
  1787. var util = require("util");
  1788. } catch (e) {
  1789. /* Node, but no util module - would be very old, but better safe than sorry */
  1790. }
  1791. function format(v) {
  1792. var isObjectWithNativeToString = typeof v === "object" && v.toString === Object.prototype.toString;
  1793. return isObjectWithNativeToString ? util.inspect(v) : v;
  1794. }
  1795. return util ? format : valueFormatter;
  1796. }
  1797. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  1798. var formatter;
  1799. if (isNode) {
  1800. try {
  1801. formatio = require("formatio");
  1802. }
  1803. catch (e) {} // eslint-disable-line no-empty
  1804. }
  1805. if (formatio) {
  1806. formatter = getFormatioFormatter();
  1807. } else if (isNode) {
  1808. formatter = getNodeFormatter();
  1809. } else {
  1810. formatter = valueFormatter;
  1811. }
  1812. sinon.format = formatter;
  1813. return sinon.format;
  1814. }
  1815. function loadDependencies(require, exports, module) {
  1816. var sinon = require("./util/core");
  1817. module.exports = makeApi(sinon);
  1818. }
  1819. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  1820. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  1821. if (isAMD) {
  1822. define(loadDependencies);
  1823. return;
  1824. }
  1825. if (isNode) {
  1826. loadDependencies(require, module.exports, module);
  1827. return;
  1828. }
  1829. if (sinonGlobal) {
  1830. makeApi(sinonGlobal);
  1831. }
  1832. }(
  1833. typeof sinon === "object" && sinon, // eslint-disable-line no-undef
  1834. typeof formatio === "object" && formatio // eslint-disable-line no-undef
  1835. ));
  1836. /**
  1837. * @depend util/core.js
  1838. * @depend match.js
  1839. * @depend format.js
  1840. */
  1841. /**
  1842. * Spy calls
  1843. *
  1844. * @author Christian Johansen (christian@cjohansen.no)
  1845. * @author Maximilian Antoni (mail@maxantoni.de)
  1846. * @license BSD
  1847. *
  1848. * Copyright (c) 2010-2013 Christian Johansen
  1849. * Copyright (c) 2013 Maximilian Antoni
  1850. */
  1851. (function (sinonGlobal) {
  1852. var slice = Array.prototype.slice;
  1853. function makeApi(sinon) {
  1854. function throwYieldError(proxy, text, args) {
  1855. var msg = sinon.functionName(proxy) + text;
  1856. if (args.length) {
  1857. msg += " Received [" + slice.call(args).join(", ") + "]";
  1858. }
  1859. throw new Error(msg);
  1860. }
  1861. var callProto = {
  1862. calledOn: function calledOn(thisValue) {
  1863. if (sinon.match && sinon.match.isMatcher(thisValue)) {
  1864. return thisValue.test(this.thisValue);
  1865. }
  1866. return this.thisValue === thisValue;
  1867. },
  1868. calledWith: function calledWith() {
  1869. var l = arguments.length;
  1870. if (l > this.args.length) {
  1871. return false;
  1872. }
  1873. for (var i = 0; i < l; i += 1) {
  1874. if (!sinon.deepEqual(arguments[i], this.args[i])) {
  1875. return false;
  1876. }
  1877. }
  1878. return true;
  1879. },
  1880. calledWithMatch: function calledWithMatch() {
  1881. var l = arguments.length;
  1882. if (l > this.args.length) {
  1883. return false;
  1884. }
  1885. for (var i = 0; i < l; i += 1) {
  1886. var actual = this.args[i];
  1887. var expectation = arguments[i];
  1888. if (!sinon.match || !sinon.match(expectation).test(actual)) {
  1889. return false;
  1890. }
  1891. }
  1892. return true;
  1893. },
  1894. calledWithExactly: function calledWithExactly() {
  1895. return arguments.length === this.args.length &&
  1896. this.calledWith.apply(this, arguments);
  1897. },
  1898. notCalledWith: function notCalledWith() {
  1899. return !this.calledWith.apply(this, arguments);
  1900. },
  1901. notCalledWithMatch: function notCalledWithMatch() {
  1902. return !this.calledWithMatch.apply(this, arguments);
  1903. },
  1904. returned: function returned(value) {
  1905. return sinon.deepEqual(value, this.returnValue);
  1906. },
  1907. threw: function threw(error) {
  1908. if (typeof error === "undefined" || !this.exception) {
  1909. return !!this.exception;
  1910. }
  1911. return this.exception === error || this.exception.name === error;
  1912. },
  1913. calledWithNew: function calledWithNew() {
  1914. return this.proxy.prototype && this.thisValue instanceof this.proxy;
  1915. },
  1916. calledBefore: function (other) {
  1917. return this.callId < other.callId;
  1918. },
  1919. calledAfter: function (other) {
  1920. return this.callId > other.callId;
  1921. },
  1922. callArg: function (pos) {
  1923. this.args[pos]();
  1924. },
  1925. callArgOn: function (pos, thisValue) {
  1926. this.args[pos].apply(thisValue);
  1927. },
  1928. callArgWith: function (pos) {
  1929. this.callArgOnWith.apply(this, [pos, null].concat(slice.call(arguments, 1)));
  1930. },
  1931. callArgOnWith: function (pos, thisValue) {
  1932. var args = slice.call(arguments, 2);
  1933. this.args[pos].apply(thisValue, args);
  1934. },
  1935. "yield": function () {
  1936. this.yieldOn.apply(this, [null].concat(slice.call(arguments, 0)));
  1937. },
  1938. yieldOn: function (thisValue) {
  1939. var args = this.args;
  1940. for (var i = 0, l = args.length; i < l; ++i) {
  1941. if (typeof args[i] === "function") {
  1942. args[i].apply(thisValue, slice.call(arguments, 1));
  1943. return;
  1944. }
  1945. }
  1946. throwYieldError(this.proxy, " cannot yield since no callback was passed.", args);
  1947. },
  1948. yieldTo: function (prop) {
  1949. this.yieldToOn.apply(this, [prop, null].concat(slice.call(arguments, 1)));
  1950. },
  1951. yieldToOn: function (prop, thisValue) {
  1952. var args = this.args;
  1953. for (var i = 0, l = args.length; i < l; ++i) {
  1954. if (args[i] && typeof args[i][prop] === "function") {
  1955. args[i][prop].apply(thisValue, slice.call(arguments, 2));
  1956. return;
  1957. }
  1958. }
  1959. throwYieldError(this.proxy, " cannot yield to '" + prop +
  1960. "' since no callback was passed.", args);
  1961. },
  1962. getStackFrames: function () {
  1963. // Omit the error message and the two top stack frames in sinon itself:
  1964. return this.stack && this.stack.split("\n").slice(3);
  1965. },
  1966. toString: function () {
  1967. var callStr = this.proxy.toString() + "(";
  1968. var args = [];
  1969. for (var i = 0, l = this.args.length; i < l; ++i) {
  1970. args.push(sinon.format(this.args[i]));
  1971. }
  1972. callStr = callStr + args.join(", ") + ")";
  1973. if (typeof this.returnValue !== "undefined") {
  1974. callStr += " => " + sinon.format(this.returnValue);
  1975. }
  1976. if (this.exception) {
  1977. callStr += " !" + this.exception.name;
  1978. if (this.exception.message) {
  1979. callStr += "(" + this.exception.message + ")";
  1980. }
  1981. }
  1982. if (this.stack) {
  1983. callStr += this.getStackFrames()[0].replace(/^\s*(?:at\s+|@)?/, " at ");
  1984. }
  1985. return callStr;
  1986. }
  1987. };
  1988. callProto.invokeCallback = callProto.yield;
  1989. function createSpyCall(spy, thisValue, args, returnValue, exception, id, stack) {
  1990. if (typeof id !== "number") {
  1991. throw new TypeError("Call id is not a number");
  1992. }
  1993. var proxyCall = sinon.create(callProto);
  1994. proxyCall.proxy = spy;
  1995. proxyCall.thisValue = thisValue;
  1996. proxyCall.args = args;
  1997. proxyCall.returnValue = returnValue;
  1998. proxyCall.exception = exception;
  1999. proxyCall.callId = id;
  2000. proxyCall.stack = stack;
  2001. return proxyCall;
  2002. }
  2003. createSpyCall.toString = callProto.toString; // used by mocks
  2004. sinon.spyCall = createSpyCall;
  2005. return createSpyCall;
  2006. }
  2007. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  2008. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  2009. function loadDependencies(require, exports, module) {
  2010. var sinon = require("./util/core");
  2011. require("./match");
  2012. require("./format");
  2013. module.exports = makeApi(sinon);
  2014. }
  2015. if (isAMD) {
  2016. define(loadDependencies);
  2017. return;
  2018. }
  2019. if (isNode) {
  2020. loadDependencies(require, module.exports, module);
  2021. return;
  2022. }
  2023. if (sinonGlobal) {
  2024. makeApi(sinonGlobal);
  2025. }
  2026. }(
  2027. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  2028. ));
  2029. /**
  2030. * @depend times_in_words.js
  2031. * @depend util/core.js
  2032. * @depend extend.js
  2033. * @depend call.js
  2034. * @depend format.js
  2035. */
  2036. /**
  2037. * Spy functions
  2038. *
  2039. * @author Christian Johansen (christian@cjohansen.no)
  2040. * @license BSD
  2041. *
  2042. * Copyright (c) 2010-2013 Christian Johansen
  2043. */
  2044. (function (sinonGlobal) {
  2045. function makeApi(sinon) {
  2046. var push = Array.prototype.push;
  2047. var slice = Array.prototype.slice;
  2048. var callId = 0;
  2049. function spy(object, property, types) {
  2050. if (!property && typeof object === "function") {
  2051. return spy.create(object);
  2052. }
  2053. if (!object && !property) {
  2054. return spy.create(function () { });
  2055. }
  2056. if (types) {
  2057. var methodDesc = sinon.getPropertyDescriptor(object, property);
  2058. for (var i = 0; i < types.length; i++) {
  2059. methodDesc[types[i]] = spy.create(methodDesc[types[i]]);
  2060. }
  2061. return sinon.wrapMethod(object, property, methodDesc);
  2062. }
  2063. return sinon.wrapMethod(object, property, spy.create(object[property]));
  2064. }
  2065. function matchingFake(fakes, args, strict) {
  2066. if (!fakes) {
  2067. return undefined;
  2068. }
  2069. for (var i = 0, l = fakes.length; i < l; i++) {
  2070. if (fakes[i].matches(args, strict)) {
  2071. return fakes[i];
  2072. }
  2073. }
  2074. }
  2075. function incrementCallCount() {
  2076. this.called = true;
  2077. this.callCount += 1;
  2078. this.notCalled = false;
  2079. this.calledOnce = this.callCount === 1;
  2080. this.calledTwice = this.callCount === 2;
  2081. this.calledThrice = this.callCount === 3;
  2082. }
  2083. function createCallProperties() {
  2084. this.firstCall = this.getCall(0);
  2085. this.secondCall = this.getCall(1);
  2086. this.thirdCall = this.getCall(2);
  2087. this.lastCall = this.getCall(this.callCount - 1);
  2088. }
  2089. var vars = "a,b,c,d,e,f,g,h,i,j,k,l";
  2090. function createProxy(func, proxyLength) {
  2091. // Retain the function length:
  2092. var p;
  2093. if (proxyLength) {
  2094. eval("p = (function proxy(" + vars.substring(0, proxyLength * 2 - 1) + // eslint-disable-line no-eval
  2095. ") { return p.invoke(func, this, slice.call(arguments)); });");
  2096. } else {
  2097. p = function proxy() {
  2098. return p.invoke(func, this, slice.call(arguments));
  2099. };
  2100. }
  2101. p.isSinonProxy = true;
  2102. return p;
  2103. }
  2104. var uuid = 0;
  2105. // Public API
  2106. var spyApi = {
  2107. reset: function () {
  2108. if (this.invoking) {
  2109. var err = new Error("Cannot reset Sinon function while invoking it. " +
  2110. "Move the call to .reset outside of the callback.");
  2111. err.name = "InvalidResetException";
  2112. throw err;
  2113. }
  2114. this.called = false;
  2115. this.notCalled = true;
  2116. this.calledOnce = false;
  2117. this.calledTwice = false;
  2118. this.calledThrice = false;
  2119. this.callCount = 0;
  2120. this.firstCall = null;
  2121. this.secondCall = null;
  2122. this.thirdCall = null;
  2123. this.lastCall = null;
  2124. this.args = [];
  2125. this.returnValues = [];
  2126. this.thisValues = [];
  2127. this.exceptions = [];
  2128. this.callIds = [];
  2129. this.stacks = [];
  2130. if (this.fakes) {
  2131. for (var i = 0; i < this.fakes.length; i++) {
  2132. this.fakes[i].reset();
  2133. }
  2134. }
  2135. return this;
  2136. },
  2137. create: function create(func, spyLength) {
  2138. var name;
  2139. if (typeof func !== "function") {
  2140. func = function () { };
  2141. } else {
  2142. name = sinon.functionName(func);
  2143. }
  2144. if (!spyLength) {
  2145. spyLength = func.length;
  2146. }
  2147. var proxy = createProxy(func, spyLength);
  2148. sinon.extend(proxy, spy);
  2149. delete proxy.create;
  2150. sinon.extend(proxy, func);
  2151. proxy.reset();
  2152. proxy.prototype = func.prototype;
  2153. proxy.displayName = name || "spy";
  2154. proxy.toString = sinon.functionToString;
  2155. proxy.instantiateFake = sinon.spy.create;
  2156. proxy.id = "spy#" + uuid++;
  2157. return proxy;
  2158. },
  2159. invoke: function invoke(func, thisValue, args) {
  2160. var matching = matchingFake(this.fakes, args);
  2161. var exception, returnValue;
  2162. incrementCallCount.call(this);
  2163. push.call(this.thisValues, thisValue);
  2164. push.call(this.args, args);
  2165. push.call(this.callIds, callId++);
  2166. // Make call properties available from within the spied function:
  2167. createCallProperties.call(this);
  2168. try {
  2169. this.invoking = true;
  2170. if (matching) {
  2171. returnValue = matching.invoke(func, thisValue, args);
  2172. } else {
  2173. returnValue = (this.func || func).apply(thisValue, args);
  2174. }
  2175. var thisCall = this.getCall(this.callCount - 1);
  2176. if (thisCall.calledWithNew() && typeof returnValue !== "object") {
  2177. returnValue = thisValue;
  2178. }
  2179. } catch (e) {
  2180. exception = e;
  2181. } finally {
  2182. delete this.invoking;
  2183. }
  2184. push.call(this.exceptions, exception);
  2185. push.call(this.returnValues, returnValue);
  2186. push.call(this.stacks, new Error().stack);
  2187. // Make return value and exception available in the calls:
  2188. createCallProperties.call(this);
  2189. if (exception !== undefined) {
  2190. throw exception;
  2191. }
  2192. return returnValue;
  2193. },
  2194. named: function named(name) {
  2195. this.displayName = name;
  2196. return this;
  2197. },
  2198. getCall: function getCall(i) {
  2199. if (i < 0 || i >= this.callCount) {
  2200. return null;
  2201. }
  2202. return sinon.spyCall(this, this.thisValues[i], this.args[i],
  2203. this.returnValues[i], this.exceptions[i],
  2204. this.callIds[i], this.stacks[i]);
  2205. },
  2206. getCalls: function () {
  2207. var calls = [];
  2208. var i;
  2209. for (i = 0; i < this.callCount; i++) {
  2210. calls.push(this.getCall(i));
  2211. }
  2212. return calls;
  2213. },
  2214. calledBefore: function calledBefore(spyFn) {
  2215. if (!this.called) {
  2216. return false;
  2217. }
  2218. if (!spyFn.called) {
  2219. return true;
  2220. }
  2221. return this.callIds[0] < spyFn.callIds[spyFn.callIds.length - 1];
  2222. },
  2223. calledAfter: function calledAfter(spyFn) {
  2224. if (!this.called || !spyFn.called) {
  2225. return false;
  2226. }
  2227. return this.callIds[this.callCount - 1] > spyFn.callIds[spyFn.callCount - 1];
  2228. },
  2229. withArgs: function () {
  2230. var args = slice.call(arguments);
  2231. if (this.fakes) {
  2232. var match = matchingFake(this.fakes, args, true);
  2233. if (match) {
  2234. return match;
  2235. }
  2236. } else {
  2237. this.fakes = [];
  2238. }
  2239. var original = this;
  2240. var fake = this.instantiateFake();
  2241. fake.matchingAguments = args;
  2242. fake.parent = this;
  2243. push.call(this.fakes, fake);
  2244. fake.withArgs = function () {
  2245. return original.withArgs.apply(original, arguments);
  2246. };
  2247. for (var i = 0; i < this.args.length; i++) {
  2248. if (fake.matches(this.args[i])) {
  2249. incrementCallCount.call(fake);
  2250. push.call(fake.thisValues, this.thisValues[i]);
  2251. push.call(fake.args, this.args[i]);
  2252. push.call(fake.returnValues, this.returnValues[i]);
  2253. push.call(fake.exceptions, this.exceptions[i]);
  2254. push.call(fake.callIds, this.callIds[i]);
  2255. }
  2256. }
  2257. createCallProperties.call(fake);
  2258. return fake;
  2259. },
  2260. matches: function (args, strict) {
  2261. var margs = this.matchingAguments;
  2262. if (margs.length <= args.length &&
  2263. sinon.deepEqual(margs, args.slice(0, margs.length))) {
  2264. return !strict || margs.length === args.length;
  2265. }
  2266. },
  2267. printf: function (format) {
  2268. var spyInstance = this;
  2269. var args = slice.call(arguments, 1);
  2270. var formatter;
  2271. return (format || "").replace(/%(.)/g, function (match, specifyer) {
  2272. formatter = spyApi.formatters[specifyer];
  2273. if (typeof formatter === "function") {
  2274. return formatter.call(null, spyInstance, args);
  2275. } else if (!isNaN(parseInt(specifyer, 10))) {
  2276. return sinon.format(args[specifyer - 1]);
  2277. }
  2278. return "%" + specifyer;
  2279. });
  2280. }
  2281. };
  2282. function delegateToCalls(method, matchAny, actual, notCalled) {
  2283. spyApi[method] = function () {
  2284. if (!this.called) {
  2285. if (notCalled) {
  2286. return notCalled.apply(this, arguments);
  2287. }
  2288. return false;
  2289. }
  2290. var currentCall;
  2291. var matches = 0;
  2292. for (var i = 0, l = this.callCount; i < l; i += 1) {
  2293. currentCall = this.getCall(i);
  2294. if (currentCall[actual || method].apply(currentCall, arguments)) {
  2295. matches += 1;
  2296. if (matchAny) {
  2297. return true;
  2298. }
  2299. }
  2300. }
  2301. return matches === this.callCount;
  2302. };
  2303. }
  2304. delegateToCalls("calledOn", true);
  2305. delegateToCalls("alwaysCalledOn", false, "calledOn");
  2306. delegateToCalls("calledWith", true);
  2307. delegateToCalls("calledWithMatch", true);
  2308. delegateToCalls("alwaysCalledWith", false, "calledWith");
  2309. delegateToCalls("alwaysCalledWithMatch", false, "calledWithMatch");
  2310. delegateToCalls("calledWithExactly", true);
  2311. delegateToCalls("alwaysCalledWithExactly", false, "calledWithExactly");
  2312. delegateToCalls("neverCalledWith", false, "notCalledWith", function () {
  2313. return true;
  2314. });
  2315. delegateToCalls("neverCalledWithMatch", false, "notCalledWithMatch", function () {
  2316. return true;
  2317. });
  2318. delegateToCalls("threw", true);
  2319. delegateToCalls("alwaysThrew", false, "threw");
  2320. delegateToCalls("returned", true);
  2321. delegateToCalls("alwaysReturned", false, "returned");
  2322. delegateToCalls("calledWithNew", true);
  2323. delegateToCalls("alwaysCalledWithNew", false, "calledWithNew");
  2324. delegateToCalls("callArg", false, "callArgWith", function () {
  2325. throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
  2326. });
  2327. spyApi.callArgWith = spyApi.callArg;
  2328. delegateToCalls("callArgOn", false, "callArgOnWith", function () {
  2329. throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
  2330. });
  2331. spyApi.callArgOnWith = spyApi.callArgOn;
  2332. delegateToCalls("yield", false, "yield", function () {
  2333. throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
  2334. });
  2335. // "invokeCallback" is an alias for "yield" since "yield" is invalid in strict mode.
  2336. spyApi.invokeCallback = spyApi.yield;
  2337. delegateToCalls("yieldOn", false, "yieldOn", function () {
  2338. throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
  2339. });
  2340. delegateToCalls("yieldTo", false, "yieldTo", function (property) {
  2341. throw new Error(this.toString() + " cannot yield to '" + property +
  2342. "' since it was not yet invoked.");
  2343. });
  2344. delegateToCalls("yieldToOn", false, "yieldToOn", function (property) {
  2345. throw new Error(this.toString() + " cannot yield to '" + property +
  2346. "' since it was not yet invoked.");
  2347. });
  2348. spyApi.formatters = {
  2349. c: function (spyInstance) {
  2350. return sinon.timesInWords(spyInstance.callCount);
  2351. },
  2352. n: function (spyInstance) {
  2353. return spyInstance.toString();
  2354. },
  2355. C: function (spyInstance) {
  2356. var calls = [];
  2357. for (var i = 0, l = spyInstance.callCount; i < l; ++i) {
  2358. var stringifiedCall = " " + spyInstance.getCall(i).toString();
  2359. if (/\n/.test(calls[i - 1])) {
  2360. stringifiedCall = "\n" + stringifiedCall;
  2361. }
  2362. push.call(calls, stringifiedCall);
  2363. }
  2364. return calls.length > 0 ? "\n" + calls.join("\n") : "";
  2365. },
  2366. t: function (spyInstance) {
  2367. var objects = [];
  2368. for (var i = 0, l = spyInstance.callCount; i < l; ++i) {
  2369. push.call(objects, sinon.format(spyInstance.thisValues[i]));
  2370. }
  2371. return objects.join(", ");
  2372. },
  2373. "*": function (spyInstance, args) {
  2374. var formatted = [];
  2375. for (var i = 0, l = args.length; i < l; ++i) {
  2376. push.call(formatted, sinon.format(args[i]));
  2377. }
  2378. return formatted.join(", ");
  2379. }
  2380. };
  2381. sinon.extend(spy, spyApi);
  2382. spy.spyCall = sinon.spyCall;
  2383. sinon.spy = spy;
  2384. return spy;
  2385. }
  2386. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  2387. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  2388. function loadDependencies(require, exports, module) {
  2389. var core = require("./util/core");
  2390. require("./call");
  2391. require("./extend");
  2392. require("./times_in_words");
  2393. require("./format");
  2394. module.exports = makeApi(core);
  2395. }
  2396. if (isAMD) {
  2397. define(loadDependencies);
  2398. return;
  2399. }
  2400. if (isNode) {
  2401. loadDependencies(require, module.exports, module);
  2402. return;
  2403. }
  2404. if (sinonGlobal) {
  2405. makeApi(sinonGlobal);
  2406. }
  2407. }(
  2408. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  2409. ));
  2410. /**
  2411. * @depend util/core.js
  2412. * @depend extend.js
  2413. */
  2414. /**
  2415. * Stub behavior
  2416. *
  2417. * @author Christian Johansen (christian@cjohansen.no)
  2418. * @author Tim Fischbach (mail@timfischbach.de)
  2419. * @license BSD
  2420. *
  2421. * Copyright (c) 2010-2013 Christian Johansen
  2422. */
  2423. (function (sinonGlobal) {
  2424. var slice = Array.prototype.slice;
  2425. var join = Array.prototype.join;
  2426. var useLeftMostCallback = -1;
  2427. var useRightMostCallback = -2;
  2428. var nextTick = (function () {
  2429. if (typeof process === "object" && typeof process.nextTick === "function") {
  2430. return process.nextTick;
  2431. }
  2432. if (typeof setImmediate === "function") {
  2433. return setImmediate;
  2434. }
  2435. return function (callback) {
  2436. setTimeout(callback, 0);
  2437. };
  2438. })();
  2439. function throwsException(error, message) {
  2440. if (typeof error === "string") {
  2441. this.exception = new Error(message || "");
  2442. this.exception.name = error;
  2443. } else if (!error) {
  2444. this.exception = new Error("Error");
  2445. } else {
  2446. this.exception = error;
  2447. }
  2448. return this;
  2449. }
  2450. function getCallback(behavior, args) {
  2451. var callArgAt = behavior.callArgAt;
  2452. if (callArgAt >= 0) {
  2453. return args[callArgAt];
  2454. }
  2455. var argumentList;
  2456. if (callArgAt === useLeftMostCallback) {
  2457. argumentList = args;
  2458. }
  2459. if (callArgAt === useRightMostCallback) {
  2460. argumentList = slice.call(args).reverse();
  2461. }
  2462. var callArgProp = behavior.callArgProp;
  2463. for (var i = 0, l = argumentList.length; i < l; ++i) {
  2464. if (!callArgProp && typeof argumentList[i] === "function") {
  2465. return argumentList[i];
  2466. }
  2467. if (callArgProp && argumentList[i] &&
  2468. typeof argumentList[i][callArgProp] === "function") {
  2469. return argumentList[i][callArgProp];
  2470. }
  2471. }
  2472. return null;
  2473. }
  2474. function makeApi(sinon) {
  2475. function getCallbackError(behavior, func, args) {
  2476. if (behavior.callArgAt < 0) {
  2477. var msg;
  2478. if (behavior.callArgProp) {
  2479. msg = sinon.functionName(behavior.stub) +
  2480. " expected to yield to '" + behavior.callArgProp +
  2481. "', but no object with such a property was passed.";
  2482. } else {
  2483. msg = sinon.functionName(behavior.stub) +
  2484. " expected to yield, but no callback was passed.";
  2485. }
  2486. if (args.length > 0) {
  2487. msg += " Received [" + join.call(args, ", ") + "]";
  2488. }
  2489. return msg;
  2490. }
  2491. return "argument at index " + behavior.callArgAt + " is not a function: " + func;
  2492. }
  2493. function callCallback(behavior, args) {
  2494. if (typeof behavior.callArgAt === "number") {
  2495. var func = getCallback(behavior, args);
  2496. if (typeof func !== "function") {
  2497. throw new TypeError(getCallbackError(behavior, func, args));
  2498. }
  2499. if (behavior.callbackAsync) {
  2500. nextTick(function () {
  2501. func.apply(behavior.callbackContext, behavior.callbackArguments);
  2502. });
  2503. } else {
  2504. func.apply(behavior.callbackContext, behavior.callbackArguments);
  2505. }
  2506. }
  2507. }
  2508. var proto = {
  2509. create: function create(stub) {
  2510. var behavior = sinon.extend({}, sinon.behavior);
  2511. delete behavior.create;
  2512. behavior.stub = stub;
  2513. return behavior;
  2514. },
  2515. isPresent: function isPresent() {
  2516. return (typeof this.callArgAt === "number" ||
  2517. this.exception ||
  2518. typeof this.returnArgAt === "number" ||
  2519. this.returnThis ||
  2520. this.returnValueDefined);
  2521. },
  2522. invoke: function invoke(context, args) {
  2523. callCallback(this, args);
  2524. if (this.exception) {
  2525. throw this.exception;
  2526. } else if (typeof this.returnArgAt === "number") {
  2527. return args[this.returnArgAt];
  2528. } else if (this.returnThis) {
  2529. return context;
  2530. }
  2531. return this.returnValue;
  2532. },
  2533. onCall: function onCall(index) {
  2534. return this.stub.onCall(index);
  2535. },
  2536. onFirstCall: function onFirstCall() {
  2537. return this.stub.onFirstCall();
  2538. },
  2539. onSecondCall: function onSecondCall() {
  2540. return this.stub.onSecondCall();
  2541. },
  2542. onThirdCall: function onThirdCall() {
  2543. return this.stub.onThirdCall();
  2544. },
  2545. withArgs: function withArgs(/* arguments */) {
  2546. throw new Error(
  2547. "Defining a stub by invoking \"stub.onCall(...).withArgs(...)\" " +
  2548. "is not supported. Use \"stub.withArgs(...).onCall(...)\" " +
  2549. "to define sequential behavior for calls with certain arguments."
  2550. );
  2551. },
  2552. callsArg: function callsArg(pos) {
  2553. if (typeof pos !== "number") {
  2554. throw new TypeError("argument index is not number");
  2555. }
  2556. this.callArgAt = pos;
  2557. this.callbackArguments = [];
  2558. this.callbackContext = undefined;
  2559. this.callArgProp = undefined;
  2560. this.callbackAsync = false;
  2561. return this;
  2562. },
  2563. callsArgOn: function callsArgOn(pos, context) {
  2564. if (typeof pos !== "number") {
  2565. throw new TypeError("argument index is not number");
  2566. }
  2567. if (typeof context !== "object") {
  2568. throw new TypeError("argument context is not an object");
  2569. }
  2570. this.callArgAt = pos;
  2571. this.callbackArguments = [];
  2572. this.callbackContext = context;
  2573. this.callArgProp = undefined;
  2574. this.callbackAsync = false;
  2575. return this;
  2576. },
  2577. callsArgWith: function callsArgWith(pos) {
  2578. if (typeof pos !== "number") {
  2579. throw new TypeError("argument index is not number");
  2580. }
  2581. this.callArgAt = pos;
  2582. this.callbackArguments = slice.call(arguments, 1);
  2583. this.callbackContext = undefined;
  2584. this.callArgProp = undefined;
  2585. this.callbackAsync = false;
  2586. return this;
  2587. },
  2588. callsArgOnWith: function callsArgWith(pos, context) {
  2589. if (typeof pos !== "number") {
  2590. throw new TypeError("argument index is not number");
  2591. }
  2592. if (typeof context !== "object") {
  2593. throw new TypeError("argument context is not an object");
  2594. }
  2595. this.callArgAt = pos;
  2596. this.callbackArguments = slice.call(arguments, 2);
  2597. this.callbackContext = context;
  2598. this.callArgProp = undefined;
  2599. this.callbackAsync = false;
  2600. return this;
  2601. },
  2602. yields: function () {
  2603. this.callArgAt = useLeftMostCallback;
  2604. this.callbackArguments = slice.call(arguments, 0);
  2605. this.callbackContext = undefined;
  2606. this.callArgProp = undefined;
  2607. this.callbackAsync = false;
  2608. return this;
  2609. },
  2610. yieldsRight: function () {
  2611. this.callArgAt = useRightMostCallback;
  2612. this.callbackArguments = slice.call(arguments, 0);
  2613. this.callbackContext = undefined;
  2614. this.callArgProp = undefined;
  2615. this.callbackAsync = false;
  2616. return this;
  2617. },
  2618. yieldsOn: function (context) {
  2619. if (typeof context !== "object") {
  2620. throw new TypeError("argument context is not an object");
  2621. }
  2622. this.callArgAt = useLeftMostCallback;
  2623. this.callbackArguments = slice.call(arguments, 1);
  2624. this.callbackContext = context;
  2625. this.callArgProp = undefined;
  2626. this.callbackAsync = false;
  2627. return this;
  2628. },
  2629. yieldsTo: function (prop) {
  2630. this.callArgAt = useLeftMostCallback;
  2631. this.callbackArguments = slice.call(arguments, 1);
  2632. this.callbackContext = undefined;
  2633. this.callArgProp = prop;
  2634. this.callbackAsync = false;
  2635. return this;
  2636. },
  2637. yieldsToOn: function (prop, context) {
  2638. if (typeof context !== "object") {
  2639. throw new TypeError("argument context is not an object");
  2640. }
  2641. this.callArgAt = useLeftMostCallback;
  2642. this.callbackArguments = slice.call(arguments, 2);
  2643. this.callbackContext = context;
  2644. this.callArgProp = prop;
  2645. this.callbackAsync = false;
  2646. return this;
  2647. },
  2648. throws: throwsException,
  2649. throwsException: throwsException,
  2650. returns: function returns(value) {
  2651. this.returnValue = value;
  2652. this.returnValueDefined = true;
  2653. this.exception = undefined;
  2654. return this;
  2655. },
  2656. returnsArg: function returnsArg(pos) {
  2657. if (typeof pos !== "number") {
  2658. throw new TypeError("argument index is not number");
  2659. }
  2660. this.returnArgAt = pos;
  2661. return this;
  2662. },
  2663. returnsThis: function returnsThis() {
  2664. this.returnThis = true;
  2665. return this;
  2666. }
  2667. };
  2668. function createAsyncVersion(syncFnName) {
  2669. return function () {
  2670. var result = this[syncFnName].apply(this, arguments);
  2671. this.callbackAsync = true;
  2672. return result;
  2673. };
  2674. }
  2675. // create asynchronous versions of callsArg* and yields* methods
  2676. for (var method in proto) {
  2677. // need to avoid creating anotherasync versions of the newly added async methods
  2678. if (proto.hasOwnProperty(method) && method.match(/^(callsArg|yields)/) && !method.match(/Async/)) {
  2679. proto[method + "Async"] = createAsyncVersion(method);
  2680. }
  2681. }
  2682. sinon.behavior = proto;
  2683. return proto;
  2684. }
  2685. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  2686. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  2687. function loadDependencies(require, exports, module) {
  2688. var sinon = require("./util/core");
  2689. require("./extend");
  2690. module.exports = makeApi(sinon);
  2691. }
  2692. if (isAMD) {
  2693. define(loadDependencies);
  2694. return;
  2695. }
  2696. if (isNode) {
  2697. loadDependencies(require, module.exports, module);
  2698. return;
  2699. }
  2700. if (sinonGlobal) {
  2701. makeApi(sinonGlobal);
  2702. }
  2703. }(
  2704. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  2705. ));
  2706. /**
  2707. * @depend util/core.js
  2708. * @depend extend.js
  2709. * @depend spy.js
  2710. * @depend behavior.js
  2711. */
  2712. /**
  2713. * Stub functions
  2714. *
  2715. * @author Christian Johansen (christian@cjohansen.no)
  2716. * @license BSD
  2717. *
  2718. * Copyright (c) 2010-2013 Christian Johansen
  2719. */
  2720. (function (sinonGlobal) {
  2721. function makeApi(sinon) {
  2722. function stub(object, property, func) {
  2723. if (!!func && typeof func !== "function" && typeof func !== "object") {
  2724. throw new TypeError("Custom stub should be a function or a property descriptor");
  2725. }
  2726. var wrapper,
  2727. prop;
  2728. if (func) {
  2729. if (typeof func === "function") {
  2730. wrapper = sinon.spy && sinon.spy.create ? sinon.spy.create(func) : func;
  2731. } else {
  2732. wrapper = func;
  2733. if (sinon.spy && sinon.spy.create) {
  2734. var types = sinon.objectKeys(wrapper);
  2735. for (var i = 0; i < types.length; i++) {
  2736. wrapper[types[i]] = sinon.spy.create(wrapper[types[i]]);
  2737. }
  2738. }
  2739. }
  2740. } else {
  2741. var stubLength = 0;
  2742. if (typeof object === "object" && typeof object[property] === "function") {
  2743. stubLength = object[property].length;
  2744. }
  2745. wrapper = stub.create(stubLength);
  2746. }
  2747. if (!object && typeof property === "undefined") {
  2748. return sinon.stub.create();
  2749. }
  2750. if (typeof property === "undefined" && typeof object === "object") {
  2751. for (prop in object) {
  2752. if (typeof sinon.getPropertyDescriptor(object, prop).value === "function") {
  2753. stub(object, prop);
  2754. }
  2755. }
  2756. return object;
  2757. }
  2758. return sinon.wrapMethod(object, property, wrapper);
  2759. }
  2760. /*eslint-disable no-use-before-define*/
  2761. function getParentBehaviour(stubInstance) {
  2762. return (stubInstance.parent && getCurrentBehavior(stubInstance.parent));
  2763. }
  2764. function getDefaultBehavior(stubInstance) {
  2765. return stubInstance.defaultBehavior ||
  2766. getParentBehaviour(stubInstance) ||
  2767. sinon.behavior.create(stubInstance);
  2768. }
  2769. function getCurrentBehavior(stubInstance) {
  2770. var behavior = stubInstance.behaviors[stubInstance.callCount - 1];
  2771. return behavior && behavior.isPresent() ? behavior : getDefaultBehavior(stubInstance);
  2772. }
  2773. /*eslint-enable no-use-before-define*/
  2774. var uuid = 0;
  2775. var proto = {
  2776. create: function create(stubLength) {
  2777. var functionStub = function () {
  2778. return getCurrentBehavior(functionStub).invoke(this, arguments);
  2779. };
  2780. functionStub.id = "stub#" + uuid++;
  2781. var orig = functionStub;
  2782. functionStub = sinon.spy.create(functionStub, stubLength);
  2783. functionStub.func = orig;
  2784. sinon.extend(functionStub, stub);
  2785. functionStub.instantiateFake = sinon.stub.create;
  2786. functionStub.displayName = "stub";
  2787. functionStub.toString = sinon.functionToString;
  2788. functionStub.defaultBehavior = null;
  2789. functionStub.behaviors = [];
  2790. return functionStub;
  2791. },
  2792. resetBehavior: function () {
  2793. var i;
  2794. this.defaultBehavior = null;
  2795. this.behaviors = [];
  2796. delete this.returnValue;
  2797. delete this.returnArgAt;
  2798. this.returnThis = false;
  2799. if (this.fakes) {
  2800. for (i = 0; i < this.fakes.length; i++) {
  2801. this.fakes[i].resetBehavior();
  2802. }
  2803. }
  2804. },
  2805. onCall: function onCall(index) {
  2806. if (!this.behaviors[index]) {
  2807. this.behaviors[index] = sinon.behavior.create(this);
  2808. }
  2809. return this.behaviors[index];
  2810. },
  2811. onFirstCall: function onFirstCall() {
  2812. return this.onCall(0);
  2813. },
  2814. onSecondCall: function onSecondCall() {
  2815. return this.onCall(1);
  2816. },
  2817. onThirdCall: function onThirdCall() {
  2818. return this.onCall(2);
  2819. }
  2820. };
  2821. function createBehavior(behaviorMethod) {
  2822. return function () {
  2823. this.defaultBehavior = this.defaultBehavior || sinon.behavior.create(this);
  2824. this.defaultBehavior[behaviorMethod].apply(this.defaultBehavior, arguments);
  2825. return this;
  2826. };
  2827. }
  2828. for (var method in sinon.behavior) {
  2829. if (sinon.behavior.hasOwnProperty(method) &&
  2830. !proto.hasOwnProperty(method) &&
  2831. method !== "create" &&
  2832. method !== "withArgs" &&
  2833. method !== "invoke") {
  2834. proto[method] = createBehavior(method);
  2835. }
  2836. }
  2837. sinon.extend(stub, proto);
  2838. sinon.stub = stub;
  2839. return stub;
  2840. }
  2841. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  2842. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  2843. function loadDependencies(require, exports, module) {
  2844. var core = require("./util/core");
  2845. require("./behavior");
  2846. require("./spy");
  2847. require("./extend");
  2848. module.exports = makeApi(core);
  2849. }
  2850. if (isAMD) {
  2851. define(loadDependencies);
  2852. return;
  2853. }
  2854. if (isNode) {
  2855. loadDependencies(require, module.exports, module);
  2856. return;
  2857. }
  2858. if (sinonGlobal) {
  2859. makeApi(sinonGlobal);
  2860. }
  2861. }(
  2862. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  2863. ));
  2864. /**
  2865. * @depend times_in_words.js
  2866. * @depend util/core.js
  2867. * @depend call.js
  2868. * @depend extend.js
  2869. * @depend match.js
  2870. * @depend spy.js
  2871. * @depend stub.js
  2872. * @depend format.js
  2873. */
  2874. /**
  2875. * Mock functions.
  2876. *
  2877. * @author Christian Johansen (christian@cjohansen.no)
  2878. * @license BSD
  2879. *
  2880. * Copyright (c) 2010-2013 Christian Johansen
  2881. */
  2882. (function (sinonGlobal) {
  2883. function makeApi(sinon) {
  2884. var push = [].push;
  2885. var match = sinon.match;
  2886. function mock(object) {
  2887. // if (typeof console !== undefined && console.warn) {
  2888. // console.warn("mock will be removed from Sinon.JS v2.0");
  2889. // }
  2890. if (!object) {
  2891. return sinon.expectation.create("Anonymous mock");
  2892. }
  2893. return mock.create(object);
  2894. }
  2895. function each(collection, callback) {
  2896. if (!collection) {
  2897. return;
  2898. }
  2899. for (var i = 0, l = collection.length; i < l; i += 1) {
  2900. callback(collection[i]);
  2901. }
  2902. }
  2903. function arrayEquals(arr1, arr2, compareLength) {
  2904. if (compareLength && (arr1.length !== arr2.length)) {
  2905. return false;
  2906. }
  2907. for (var i = 0, l = arr1.length; i < l; i++) {
  2908. if (!sinon.deepEqual(arr1[i], arr2[i])) {
  2909. return false;
  2910. }
  2911. }
  2912. return true;
  2913. }
  2914. sinon.extend(mock, {
  2915. create: function create(object) {
  2916. if (!object) {
  2917. throw new TypeError("object is null");
  2918. }
  2919. var mockObject = sinon.extend({}, mock);
  2920. mockObject.object = object;
  2921. delete mockObject.create;
  2922. return mockObject;
  2923. },
  2924. expects: function expects(method) {
  2925. if (!method) {
  2926. throw new TypeError("method is falsy");
  2927. }
  2928. if (!this.expectations) {
  2929. this.expectations = {};
  2930. this.proxies = [];
  2931. }
  2932. if (!this.expectations[method]) {
  2933. this.expectations[method] = [];
  2934. var mockObject = this;
  2935. sinon.wrapMethod(this.object, method, function () {
  2936. return mockObject.invokeMethod(method, this, arguments);
  2937. });
  2938. push.call(this.proxies, method);
  2939. }
  2940. var expectation = sinon.expectation.create(method);
  2941. push.call(this.expectations[method], expectation);
  2942. return expectation;
  2943. },
  2944. restore: function restore() {
  2945. var object = this.object;
  2946. each(this.proxies, function (proxy) {
  2947. if (typeof object[proxy].restore === "function") {
  2948. object[proxy].restore();
  2949. }
  2950. });
  2951. },
  2952. verify: function verify() {
  2953. var expectations = this.expectations || {};
  2954. var messages = [];
  2955. var met = [];
  2956. each(this.proxies, function (proxy) {
  2957. each(expectations[proxy], function (expectation) {
  2958. if (!expectation.met()) {
  2959. push.call(messages, expectation.toString());
  2960. } else {
  2961. push.call(met, expectation.toString());
  2962. }
  2963. });
  2964. });
  2965. this.restore();
  2966. if (messages.length > 0) {
  2967. sinon.expectation.fail(messages.concat(met).join("\n"));
  2968. } else if (met.length > 0) {
  2969. sinon.expectation.pass(messages.concat(met).join("\n"));
  2970. }
  2971. return true;
  2972. },
  2973. invokeMethod: function invokeMethod(method, thisValue, args) {
  2974. var expectations = this.expectations && this.expectations[method] ? this.expectations[method] : [];
  2975. var expectationsWithMatchingArgs = [];
  2976. var currentArgs = args || [];
  2977. var i, available;
  2978. for (i = 0; i < expectations.length; i += 1) {
  2979. var expectedArgs = expectations[i].expectedArguments || [];
  2980. if (arrayEquals(expectedArgs, currentArgs, expectations[i].expectsExactArgCount)) {
  2981. expectationsWithMatchingArgs.push(expectations[i]);
  2982. }
  2983. }
  2984. for (i = 0; i < expectationsWithMatchingArgs.length; i += 1) {
  2985. if (!expectationsWithMatchingArgs[i].met() &&
  2986. expectationsWithMatchingArgs[i].allowsCall(thisValue, args)) {
  2987. return expectationsWithMatchingArgs[i].apply(thisValue, args);
  2988. }
  2989. }
  2990. var messages = [];
  2991. var exhausted = 0;
  2992. for (i = 0; i < expectationsWithMatchingArgs.length; i += 1) {
  2993. if (expectationsWithMatchingArgs[i].allowsCall(thisValue, args)) {
  2994. available = available || expectationsWithMatchingArgs[i];
  2995. } else {
  2996. exhausted += 1;
  2997. }
  2998. }
  2999. if (available && exhausted === 0) {
  3000. return available.apply(thisValue, args);
  3001. }
  3002. for (i = 0; i < expectations.length; i += 1) {
  3003. push.call(messages, " " + expectations[i].toString());
  3004. }
  3005. messages.unshift("Unexpected call: " + sinon.spyCall.toString.call({
  3006. proxy: method,
  3007. args: args
  3008. }));
  3009. sinon.expectation.fail(messages.join("\n"));
  3010. }
  3011. });
  3012. var times = sinon.timesInWords;
  3013. var slice = Array.prototype.slice;
  3014. function callCountInWords(callCount) {
  3015. if (callCount === 0) {
  3016. return "never called";
  3017. }
  3018. return "called " + times(callCount);
  3019. }
  3020. function expectedCallCountInWords(expectation) {
  3021. var min = expectation.minCalls;
  3022. var max = expectation.maxCalls;
  3023. if (typeof min === "number" && typeof max === "number") {
  3024. var str = times(min);
  3025. if (min !== max) {
  3026. str = "at least " + str + " and at most " + times(max);
  3027. }
  3028. return str;
  3029. }
  3030. if (typeof min === "number") {
  3031. return "at least " + times(min);
  3032. }
  3033. return "at most " + times(max);
  3034. }
  3035. function receivedMinCalls(expectation) {
  3036. var hasMinLimit = typeof expectation.minCalls === "number";
  3037. return !hasMinLimit || expectation.callCount >= expectation.minCalls;
  3038. }
  3039. function receivedMaxCalls(expectation) {
  3040. if (typeof expectation.maxCalls !== "number") {
  3041. return false;
  3042. }
  3043. return expectation.callCount === expectation.maxCalls;
  3044. }
  3045. function verifyMatcher(possibleMatcher, arg) {
  3046. var isMatcher = match && match.isMatcher(possibleMatcher);
  3047. return isMatcher && possibleMatcher.test(arg) || true;
  3048. }
  3049. sinon.expectation = {
  3050. minCalls: 1,
  3051. maxCalls: 1,
  3052. create: function create(methodName) {
  3053. var expectation = sinon.extend(sinon.stub.create(), sinon.expectation);
  3054. delete expectation.create;
  3055. expectation.method = methodName;
  3056. return expectation;
  3057. },
  3058. invoke: function invoke(func, thisValue, args) {
  3059. this.verifyCallAllowed(thisValue, args);
  3060. return sinon.spy.invoke.apply(this, arguments);
  3061. },
  3062. atLeast: function atLeast(num) {
  3063. if (typeof num !== "number") {
  3064. throw new TypeError("'" + num + "' is not number");
  3065. }
  3066. if (!this.limitsSet) {
  3067. this.maxCalls = null;
  3068. this.limitsSet = true;
  3069. }
  3070. this.minCalls = num;
  3071. return this;
  3072. },
  3073. atMost: function atMost(num) {
  3074. if (typeof num !== "number") {
  3075. throw new TypeError("'" + num + "' is not number");
  3076. }
  3077. if (!this.limitsSet) {
  3078. this.minCalls = null;
  3079. this.limitsSet = true;
  3080. }
  3081. this.maxCalls = num;
  3082. return this;
  3083. },
  3084. never: function never() {
  3085. return this.exactly(0);
  3086. },
  3087. once: function once() {
  3088. return this.exactly(1);
  3089. },
  3090. twice: function twice() {
  3091. return this.exactly(2);
  3092. },
  3093. thrice: function thrice() {
  3094. return this.exactly(3);
  3095. },
  3096. exactly: function exactly(num) {
  3097. if (typeof num !== "number") {
  3098. throw new TypeError("'" + num + "' is not a number");
  3099. }
  3100. this.atLeast(num);
  3101. return this.atMost(num);
  3102. },
  3103. met: function met() {
  3104. return !this.failed && receivedMinCalls(this);
  3105. },
  3106. verifyCallAllowed: function verifyCallAllowed(thisValue, args) {
  3107. if (receivedMaxCalls(this)) {
  3108. this.failed = true;
  3109. sinon.expectation.fail(this.method + " already called " + times(this.maxCalls));
  3110. }
  3111. if ("expectedThis" in this && this.expectedThis !== thisValue) {
  3112. sinon.expectation.fail(this.method + " called with " + thisValue + " as thisValue, expected " +
  3113. this.expectedThis);
  3114. }
  3115. if (!("expectedArguments" in this)) {
  3116. return;
  3117. }
  3118. if (!args) {
  3119. sinon.expectation.fail(this.method + " received no arguments, expected " +
  3120. sinon.format(this.expectedArguments));
  3121. }
  3122. if (args.length < this.expectedArguments.length) {
  3123. sinon.expectation.fail(this.method + " received too few arguments (" + sinon.format(args) +
  3124. "), expected " + sinon.format(this.expectedArguments));
  3125. }
  3126. if (this.expectsExactArgCount &&
  3127. args.length !== this.expectedArguments.length) {
  3128. sinon.expectation.fail(this.method + " received too many arguments (" + sinon.format(args) +
  3129. "), expected " + sinon.format(this.expectedArguments));
  3130. }
  3131. for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
  3132. if (!verifyMatcher(this.expectedArguments[i], args[i])) {
  3133. sinon.expectation.fail(this.method + " received wrong arguments " + sinon.format(args) +
  3134. ", didn't match " + this.expectedArguments.toString());
  3135. }
  3136. if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
  3137. sinon.expectation.fail(this.method + " received wrong arguments " + sinon.format(args) +
  3138. ", expected " + sinon.format(this.expectedArguments));
  3139. }
  3140. }
  3141. },
  3142. allowsCall: function allowsCall(thisValue, args) {
  3143. if (this.met() && receivedMaxCalls(this)) {
  3144. return false;
  3145. }
  3146. if ("expectedThis" in this && this.expectedThis !== thisValue) {
  3147. return false;
  3148. }
  3149. if (!("expectedArguments" in this)) {
  3150. return true;
  3151. }
  3152. args = args || [];
  3153. if (args.length < this.expectedArguments.length) {
  3154. return false;
  3155. }
  3156. if (this.expectsExactArgCount &&
  3157. args.length !== this.expectedArguments.length) {
  3158. return false;
  3159. }
  3160. for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
  3161. if (!verifyMatcher(this.expectedArguments[i], args[i])) {
  3162. return false;
  3163. }
  3164. if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
  3165. return false;
  3166. }
  3167. }
  3168. return true;
  3169. },
  3170. withArgs: function withArgs() {
  3171. this.expectedArguments = slice.call(arguments);
  3172. return this;
  3173. },
  3174. withExactArgs: function withExactArgs() {
  3175. this.withArgs.apply(this, arguments);
  3176. this.expectsExactArgCount = true;
  3177. return this;
  3178. },
  3179. on: function on(thisValue) {
  3180. this.expectedThis = thisValue;
  3181. return this;
  3182. },
  3183. toString: function () {
  3184. var args = (this.expectedArguments || []).slice();
  3185. if (!this.expectsExactArgCount) {
  3186. push.call(args, "[...]");
  3187. }
  3188. var callStr = sinon.spyCall.toString.call({
  3189. proxy: this.method || "anonymous mock expectation",
  3190. args: args
  3191. });
  3192. var message = callStr.replace(", [...", "[, ...") + " " +
  3193. expectedCallCountInWords(this);
  3194. if (this.met()) {
  3195. return "Expectation met: " + message;
  3196. }
  3197. return "Expected " + message + " (" +
  3198. callCountInWords(this.callCount) + ")";
  3199. },
  3200. verify: function verify() {
  3201. if (!this.met()) {
  3202. sinon.expectation.fail(this.toString());
  3203. } else {
  3204. sinon.expectation.pass(this.toString());
  3205. }
  3206. return true;
  3207. },
  3208. pass: function pass(message) {
  3209. sinon.assert.pass(message);
  3210. },
  3211. fail: function fail(message) {
  3212. var exception = new Error(message);
  3213. exception.name = "ExpectationError";
  3214. throw exception;
  3215. }
  3216. };
  3217. sinon.mock = mock;
  3218. return mock;
  3219. }
  3220. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  3221. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  3222. function loadDependencies(require, exports, module) {
  3223. var sinon = require("./util/core");
  3224. require("./times_in_words");
  3225. require("./call");
  3226. require("./extend");
  3227. require("./match");
  3228. require("./spy");
  3229. require("./stub");
  3230. require("./format");
  3231. module.exports = makeApi(sinon);
  3232. }
  3233. if (isAMD) {
  3234. define(loadDependencies);
  3235. return;
  3236. }
  3237. if (isNode) {
  3238. loadDependencies(require, module.exports, module);
  3239. return;
  3240. }
  3241. if (sinonGlobal) {
  3242. makeApi(sinonGlobal);
  3243. }
  3244. }(
  3245. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  3246. ));
  3247. /**
  3248. * @depend util/core.js
  3249. * @depend spy.js
  3250. * @depend stub.js
  3251. * @depend mock.js
  3252. */
  3253. /**
  3254. * Collections of stubs, spies and mocks.
  3255. *
  3256. * @author Christian Johansen (christian@cjohansen.no)
  3257. * @license BSD
  3258. *
  3259. * Copyright (c) 2010-2013 Christian Johansen
  3260. */
  3261. (function (sinonGlobal) {
  3262. var push = [].push;
  3263. var hasOwnProperty = Object.prototype.hasOwnProperty;
  3264. function getFakes(fakeCollection) {
  3265. if (!fakeCollection.fakes) {
  3266. fakeCollection.fakes = [];
  3267. }
  3268. return fakeCollection.fakes;
  3269. }
  3270. function each(fakeCollection, method) {
  3271. var fakes = getFakes(fakeCollection);
  3272. for (var i = 0, l = fakes.length; i < l; i += 1) {
  3273. if (typeof fakes[i][method] === "function") {
  3274. fakes[i][method]();
  3275. }
  3276. }
  3277. }
  3278. function compact(fakeCollection) {
  3279. var fakes = getFakes(fakeCollection);
  3280. var i = 0;
  3281. while (i < fakes.length) {
  3282. fakes.splice(i, 1);
  3283. }
  3284. }
  3285. function makeApi(sinon) {
  3286. var collection = {
  3287. verify: function resolve() {
  3288. each(this, "verify");
  3289. },
  3290. restore: function restore() {
  3291. each(this, "restore");
  3292. compact(this);
  3293. },
  3294. reset: function restore() {
  3295. each(this, "reset");
  3296. },
  3297. verifyAndRestore: function verifyAndRestore() {
  3298. var exception;
  3299. try {
  3300. this.verify();
  3301. } catch (e) {
  3302. exception = e;
  3303. }
  3304. this.restore();
  3305. if (exception) {
  3306. throw exception;
  3307. }
  3308. },
  3309. add: function add(fake) {
  3310. push.call(getFakes(this), fake);
  3311. return fake;
  3312. },
  3313. spy: function spy() {
  3314. return this.add(sinon.spy.apply(sinon, arguments));
  3315. },
  3316. stub: function stub(object, property, value) {
  3317. if (property) {
  3318. var original = object[property];
  3319. if (typeof original !== "function") {
  3320. if (!hasOwnProperty.call(object, property)) {
  3321. throw new TypeError("Cannot stub non-existent own property " + property);
  3322. }
  3323. object[property] = value;
  3324. return this.add({
  3325. restore: function () {
  3326. object[property] = original;
  3327. }
  3328. });
  3329. }
  3330. }
  3331. if (!property && !!object && typeof object === "object") {
  3332. var stubbedObj = sinon.stub.apply(sinon, arguments);
  3333. for (var prop in stubbedObj) {
  3334. if (typeof stubbedObj[prop] === "function") {
  3335. this.add(stubbedObj[prop]);
  3336. }
  3337. }
  3338. return stubbedObj;
  3339. }
  3340. return this.add(sinon.stub.apply(sinon, arguments));
  3341. },
  3342. mock: function mock() {
  3343. return this.add(sinon.mock.apply(sinon, arguments));
  3344. },
  3345. inject: function inject(obj) {
  3346. var col = this;
  3347. obj.spy = function () {
  3348. return col.spy.apply(col, arguments);
  3349. };
  3350. obj.stub = function () {
  3351. return col.stub.apply(col, arguments);
  3352. };
  3353. obj.mock = function () {
  3354. return col.mock.apply(col, arguments);
  3355. };
  3356. return obj;
  3357. }
  3358. };
  3359. sinon.collection = collection;
  3360. return collection;
  3361. }
  3362. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  3363. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  3364. function loadDependencies(require, exports, module) {
  3365. var sinon = require("./util/core");
  3366. require("./mock");
  3367. require("./spy");
  3368. require("./stub");
  3369. module.exports = makeApi(sinon);
  3370. }
  3371. if (isAMD) {
  3372. define(loadDependencies);
  3373. return;
  3374. }
  3375. if (isNode) {
  3376. loadDependencies(require, module.exports, module);
  3377. return;
  3378. }
  3379. if (sinonGlobal) {
  3380. makeApi(sinonGlobal);
  3381. }
  3382. }(
  3383. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  3384. ));
  3385. /**
  3386. * Fake timer API
  3387. * setTimeout
  3388. * setInterval
  3389. * clearTimeout
  3390. * clearInterval
  3391. * tick
  3392. * reset
  3393. * Date
  3394. *
  3395. * Inspired by jsUnitMockTimeOut from JsUnit
  3396. *
  3397. * @author Christian Johansen (christian@cjohansen.no)
  3398. * @license BSD
  3399. *
  3400. * Copyright (c) 2010-2013 Christian Johansen
  3401. */
  3402. (function () {
  3403. function makeApi(s, lol) {
  3404. /*global lolex */
  3405. var llx = typeof lolex !== "undefined" ? lolex : lol;
  3406. s.useFakeTimers = function () {
  3407. var now;
  3408. var methods = Array.prototype.slice.call(arguments);
  3409. if (typeof methods[0] === "string") {
  3410. now = 0;
  3411. } else {
  3412. now = methods.shift();
  3413. }
  3414. var clock = llx.install(now || 0, methods);
  3415. clock.restore = clock.uninstall;
  3416. return clock;
  3417. };
  3418. s.clock = {
  3419. create: function (now) {
  3420. return llx.createClock(now);
  3421. }
  3422. };
  3423. s.timers = {
  3424. setTimeout: setTimeout,
  3425. clearTimeout: clearTimeout,
  3426. setImmediate: (typeof setImmediate !== "undefined" ? setImmediate : undefined),
  3427. clearImmediate: (typeof clearImmediate !== "undefined" ? clearImmediate : undefined),
  3428. setInterval: setInterval,
  3429. clearInterval: clearInterval,
  3430. Date: Date
  3431. };
  3432. }
  3433. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  3434. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  3435. function loadDependencies(require, epxorts, module, lolex) {
  3436. var core = require("./core");
  3437. makeApi(core, lolex);
  3438. module.exports = core;
  3439. }
  3440. if (isAMD) {
  3441. define(loadDependencies);
  3442. } else if (isNode) {
  3443. loadDependencies(require, module.exports, module, require("lolex"));
  3444. } else {
  3445. makeApi(sinon); // eslint-disable-line no-undef
  3446. }
  3447. }());
  3448. /**
  3449. * Minimal Event interface implementation
  3450. *
  3451. * Original implementation by Sven Fuchs: https://gist.github.com/995028
  3452. * Modifications and tests by Christian Johansen.
  3453. *
  3454. * @author Sven Fuchs (svenfuchs@artweb-design.de)
  3455. * @author Christian Johansen (christian@cjohansen.no)
  3456. * @license BSD
  3457. *
  3458. * Copyright (c) 2011 Sven Fuchs, Christian Johansen
  3459. */
  3460. if (typeof sinon === "undefined") {
  3461. this.sinon = {};
  3462. }
  3463. (function () {
  3464. var push = [].push;
  3465. function makeApi(sinon) {
  3466. sinon.Event = function Event(type, bubbles, cancelable, target) {
  3467. this.initEvent(type, bubbles, cancelable, target);
  3468. };
  3469. sinon.Event.prototype = {
  3470. initEvent: function (type, bubbles, cancelable, target) {
  3471. this.type = type;
  3472. this.bubbles = bubbles;
  3473. this.cancelable = cancelable;
  3474. this.target = target;
  3475. },
  3476. stopPropagation: function () {},
  3477. preventDefault: function () {
  3478. this.defaultPrevented = true;
  3479. }
  3480. };
  3481. sinon.ProgressEvent = function ProgressEvent(type, progressEventRaw, target) {
  3482. this.initEvent(type, false, false, target);
  3483. this.loaded = progressEventRaw.loaded || null;
  3484. this.total = progressEventRaw.total || null;
  3485. this.lengthComputable = !!progressEventRaw.total;
  3486. };
  3487. sinon.ProgressEvent.prototype = new sinon.Event();
  3488. sinon.ProgressEvent.prototype.constructor = sinon.ProgressEvent;
  3489. sinon.CustomEvent = function CustomEvent(type, customData, target) {
  3490. this.initEvent(type, false, false, target);
  3491. this.detail = customData.detail || null;
  3492. };
  3493. sinon.CustomEvent.prototype = new sinon.Event();
  3494. sinon.CustomEvent.prototype.constructor = sinon.CustomEvent;
  3495. sinon.EventTarget = {
  3496. addEventListener: function addEventListener(event, listener) {
  3497. this.eventListeners = this.eventListeners || {};
  3498. this.eventListeners[event] = this.eventListeners[event] || [];
  3499. push.call(this.eventListeners[event], listener);
  3500. },
  3501. removeEventListener: function removeEventListener(event, listener) {
  3502. var listeners = this.eventListeners && this.eventListeners[event] || [];
  3503. for (var i = 0, l = listeners.length; i < l; ++i) {
  3504. if (listeners[i] === listener) {
  3505. return listeners.splice(i, 1);
  3506. }
  3507. }
  3508. },
  3509. dispatchEvent: function dispatchEvent(event) {
  3510. var type = event.type;
  3511. var listeners = this.eventListeners && this.eventListeners[type] || [];
  3512. for (var i = 0; i < listeners.length; i++) {
  3513. if (typeof listeners[i] === "function") {
  3514. listeners[i].call(this, event);
  3515. } else {
  3516. listeners[i].handleEvent(event);
  3517. }
  3518. }
  3519. return !!event.defaultPrevented;
  3520. }
  3521. };
  3522. }
  3523. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  3524. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  3525. function loadDependencies(require) {
  3526. var sinon = require("./core");
  3527. makeApi(sinon);
  3528. }
  3529. if (isAMD) {
  3530. define(loadDependencies);
  3531. } else if (isNode) {
  3532. loadDependencies(require);
  3533. } else {
  3534. makeApi(sinon); // eslint-disable-line no-undef
  3535. }
  3536. }());
  3537. /**
  3538. * @depend util/core.js
  3539. */
  3540. /**
  3541. * Logs errors
  3542. *
  3543. * @author Christian Johansen (christian@cjohansen.no)
  3544. * @license BSD
  3545. *
  3546. * Copyright (c) 2010-2014 Christian Johansen
  3547. */
  3548. (function (sinonGlobal) {
  3549. // cache a reference to setTimeout, so that our reference won't be stubbed out
  3550. // when using fake timers and errors will still get logged
  3551. // https://github.com/cjohansen/Sinon.JS/issues/381
  3552. var realSetTimeout = setTimeout;
  3553. function makeApi(sinon) {
  3554. function log() {}
  3555. function logError(label, err) {
  3556. var msg = label + " threw exception: ";
  3557. sinon.log(msg + "[" + err.name + "] " + err.message);
  3558. if (err.stack) {
  3559. sinon.log(err.stack);
  3560. }
  3561. logError.setTimeout(function () {
  3562. err.message = msg + err.message;
  3563. throw err;
  3564. }, 0);
  3565. }
  3566. // wrap realSetTimeout with something we can stub in tests
  3567. logError.setTimeout = function (func, timeout) {
  3568. realSetTimeout(func, timeout);
  3569. };
  3570. var exports = {};
  3571. exports.log = sinon.log = log;
  3572. exports.logError = sinon.logError = logError;
  3573. return exports;
  3574. }
  3575. function loadDependencies(require, exports, module) {
  3576. var sinon = require("./util/core");
  3577. module.exports = makeApi(sinon);
  3578. }
  3579. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  3580. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  3581. if (isAMD) {
  3582. define(loadDependencies);
  3583. return;
  3584. }
  3585. if (isNode) {
  3586. loadDependencies(require, module.exports, module);
  3587. return;
  3588. }
  3589. if (sinonGlobal) {
  3590. makeApi(sinonGlobal);
  3591. }
  3592. }(
  3593. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  3594. ));
  3595. /**
  3596. * @depend core.js
  3597. * @depend ../extend.js
  3598. * @depend event.js
  3599. * @depend ../log_error.js
  3600. */
  3601. /**
  3602. * Fake XDomainRequest object
  3603. */
  3604. if (typeof sinon === "undefined") {
  3605. this.sinon = {};
  3606. }
  3607. // wrapper for global
  3608. (function (global) {
  3609. var xdr = { XDomainRequest: global.XDomainRequest };
  3610. xdr.GlobalXDomainRequest = global.XDomainRequest;
  3611. xdr.supportsXDR = typeof xdr.GlobalXDomainRequest !== "undefined";
  3612. xdr.workingXDR = xdr.supportsXDR ? xdr.GlobalXDomainRequest : false;
  3613. function makeApi(sinon) {
  3614. sinon.xdr = xdr;
  3615. function FakeXDomainRequest() {
  3616. this.readyState = FakeXDomainRequest.UNSENT;
  3617. this.requestBody = null;
  3618. this.requestHeaders = {};
  3619. this.status = 0;
  3620. this.timeout = null;
  3621. if (typeof FakeXDomainRequest.onCreate === "function") {
  3622. FakeXDomainRequest.onCreate(this);
  3623. }
  3624. }
  3625. function verifyState(x) {
  3626. if (x.readyState !== FakeXDomainRequest.OPENED) {
  3627. throw new Error("INVALID_STATE_ERR");
  3628. }
  3629. if (x.sendFlag) {
  3630. throw new Error("INVALID_STATE_ERR");
  3631. }
  3632. }
  3633. function verifyRequestSent(x) {
  3634. if (x.readyState === FakeXDomainRequest.UNSENT) {
  3635. throw new Error("Request not sent");
  3636. }
  3637. if (x.readyState === FakeXDomainRequest.DONE) {
  3638. throw new Error("Request done");
  3639. }
  3640. }
  3641. function verifyResponseBodyType(body) {
  3642. if (typeof body !== "string") {
  3643. var error = new Error("Attempted to respond to fake XDomainRequest with " +
  3644. body + ", which is not a string.");
  3645. error.name = "InvalidBodyException";
  3646. throw error;
  3647. }
  3648. }
  3649. sinon.extend(FakeXDomainRequest.prototype, sinon.EventTarget, {
  3650. open: function open(method, url) {
  3651. this.method = method;
  3652. this.url = url;
  3653. this.responseText = null;
  3654. this.sendFlag = false;
  3655. this.readyStateChange(FakeXDomainRequest.OPENED);
  3656. },
  3657. readyStateChange: function readyStateChange(state) {
  3658. this.readyState = state;
  3659. var eventName = "";
  3660. switch (this.readyState) {
  3661. case FakeXDomainRequest.UNSENT:
  3662. break;
  3663. case FakeXDomainRequest.OPENED:
  3664. break;
  3665. case FakeXDomainRequest.LOADING:
  3666. if (this.sendFlag) {
  3667. //raise the progress event
  3668. eventName = "onprogress";
  3669. }
  3670. break;
  3671. case FakeXDomainRequest.DONE:
  3672. if (this.isTimeout) {
  3673. eventName = "ontimeout";
  3674. } else if (this.errorFlag || (this.status < 200 || this.status > 299)) {
  3675. eventName = "onerror";
  3676. } else {
  3677. eventName = "onload";
  3678. }
  3679. break;
  3680. }
  3681. // raising event (if defined)
  3682. if (eventName) {
  3683. if (typeof this[eventName] === "function") {
  3684. try {
  3685. this[eventName]();
  3686. } catch (e) {
  3687. sinon.logError("Fake XHR " + eventName + " handler", e);
  3688. }
  3689. }
  3690. }
  3691. },
  3692. send: function send(data) {
  3693. verifyState(this);
  3694. if (!/^(get|head)$/i.test(this.method)) {
  3695. this.requestBody = data;
  3696. }
  3697. this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
  3698. this.errorFlag = false;
  3699. this.sendFlag = true;
  3700. this.readyStateChange(FakeXDomainRequest.OPENED);
  3701. if (typeof this.onSend === "function") {
  3702. this.onSend(this);
  3703. }
  3704. },
  3705. abort: function abort() {
  3706. this.aborted = true;
  3707. this.responseText = null;
  3708. this.errorFlag = true;
  3709. if (this.readyState > sinon.FakeXDomainRequest.UNSENT && this.sendFlag) {
  3710. this.readyStateChange(sinon.FakeXDomainRequest.DONE);
  3711. this.sendFlag = false;
  3712. }
  3713. },
  3714. setResponseBody: function setResponseBody(body) {
  3715. verifyRequestSent(this);
  3716. verifyResponseBodyType(body);
  3717. var chunkSize = this.chunkSize || 10;
  3718. var index = 0;
  3719. this.responseText = "";
  3720. do {
  3721. this.readyStateChange(FakeXDomainRequest.LOADING);
  3722. this.responseText += body.substring(index, index + chunkSize);
  3723. index += chunkSize;
  3724. } while (index < body.length);
  3725. this.readyStateChange(FakeXDomainRequest.DONE);
  3726. },
  3727. respond: function respond(status, contentType, body) {
  3728. // content-type ignored, since XDomainRequest does not carry this
  3729. // we keep the same syntax for respond(...) as for FakeXMLHttpRequest to ease
  3730. // test integration across browsers
  3731. this.status = typeof status === "number" ? status : 200;
  3732. this.setResponseBody(body || "");
  3733. },
  3734. simulatetimeout: function simulatetimeout() {
  3735. this.status = 0;
  3736. this.isTimeout = true;
  3737. // Access to this should actually throw an error
  3738. this.responseText = undefined;
  3739. this.readyStateChange(FakeXDomainRequest.DONE);
  3740. }
  3741. });
  3742. sinon.extend(FakeXDomainRequest, {
  3743. UNSENT: 0,
  3744. OPENED: 1,
  3745. LOADING: 3,
  3746. DONE: 4
  3747. });
  3748. sinon.useFakeXDomainRequest = function useFakeXDomainRequest() {
  3749. sinon.FakeXDomainRequest.restore = function restore(keepOnCreate) {
  3750. if (xdr.supportsXDR) {
  3751. global.XDomainRequest = xdr.GlobalXDomainRequest;
  3752. }
  3753. delete sinon.FakeXDomainRequest.restore;
  3754. if (keepOnCreate !== true) {
  3755. delete sinon.FakeXDomainRequest.onCreate;
  3756. }
  3757. };
  3758. if (xdr.supportsXDR) {
  3759. global.XDomainRequest = sinon.FakeXDomainRequest;
  3760. }
  3761. return sinon.FakeXDomainRequest;
  3762. };
  3763. sinon.FakeXDomainRequest = FakeXDomainRequest;
  3764. }
  3765. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  3766. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  3767. function loadDependencies(require, exports, module) {
  3768. var sinon = require("./core");
  3769. require("../extend");
  3770. require("./event");
  3771. require("../log_error");
  3772. makeApi(sinon);
  3773. module.exports = sinon;
  3774. }
  3775. if (isAMD) {
  3776. define(loadDependencies);
  3777. } else if (isNode) {
  3778. loadDependencies(require, module.exports, module);
  3779. } else {
  3780. makeApi(sinon); // eslint-disable-line no-undef
  3781. }
  3782. })(typeof global !== "undefined" ? global : self);
  3783. /**
  3784. * @depend core.js
  3785. * @depend ../extend.js
  3786. * @depend event.js
  3787. * @depend ../log_error.js
  3788. */
  3789. /**
  3790. * Fake XMLHttpRequest object
  3791. *
  3792. * @author Christian Johansen (christian@cjohansen.no)
  3793. * @license BSD
  3794. *
  3795. * Copyright (c) 2010-2013 Christian Johansen
  3796. */
  3797. (function (sinonGlobal, global) {
  3798. function getWorkingXHR(globalScope) {
  3799. var supportsXHR = typeof globalScope.XMLHttpRequest !== "undefined";
  3800. if (supportsXHR) {
  3801. return globalScope.XMLHttpRequest;
  3802. }
  3803. var supportsActiveX = typeof globalScope.ActiveXObject !== "undefined";
  3804. if (supportsActiveX) {
  3805. return function () {
  3806. return new globalScope.ActiveXObject("MSXML2.XMLHTTP.3.0");
  3807. };
  3808. }
  3809. return false;
  3810. }
  3811. var supportsProgress = typeof ProgressEvent !== "undefined";
  3812. var supportsCustomEvent = typeof CustomEvent !== "undefined";
  3813. var supportsFormData = typeof FormData !== "undefined";
  3814. var sinonXhr = { XMLHttpRequest: global.XMLHttpRequest };
  3815. sinonXhr.GlobalXMLHttpRequest = global.XMLHttpRequest;
  3816. sinonXhr.GlobalActiveXObject = global.ActiveXObject;
  3817. sinonXhr.supportsActiveX = typeof sinonXhr.GlobalActiveXObject !== "undefined";
  3818. sinonXhr.supportsXHR = typeof sinonXhr.GlobalXMLHttpRequest !== "undefined";
  3819. sinonXhr.workingXHR = getWorkingXHR(global);
  3820. sinonXhr.supportsCORS = sinonXhr.supportsXHR && "withCredentials" in (new sinonXhr.GlobalXMLHttpRequest());
  3821. var unsafeHeaders = {
  3822. "Accept-Charset": true,
  3823. "Accept-Encoding": true,
  3824. Connection: true,
  3825. "Content-Length": true,
  3826. Cookie: true,
  3827. Cookie2: true,
  3828. "Content-Transfer-Encoding": true,
  3829. Date: true,
  3830. Expect: true,
  3831. Host: true,
  3832. "Keep-Alive": true,
  3833. Referer: true,
  3834. TE: true,
  3835. Trailer: true,
  3836. "Transfer-Encoding": true,
  3837. Upgrade: true,
  3838. "User-Agent": true,
  3839. Via: true
  3840. };
  3841. // An upload object is created for each
  3842. // FakeXMLHttpRequest and allows upload
  3843. // events to be simulated using uploadProgress
  3844. // and uploadError.
  3845. function UploadProgress() {
  3846. this.eventListeners = {
  3847. progress: [],
  3848. load: [],
  3849. abort: [],
  3850. error: []
  3851. };
  3852. }
  3853. UploadProgress.prototype.addEventListener = function addEventListener(event, listener) {
  3854. this.eventListeners[event].push(listener);
  3855. };
  3856. UploadProgress.prototype.removeEventListener = function removeEventListener(event, listener) {
  3857. var listeners = this.eventListeners[event] || [];
  3858. for (var i = 0, l = listeners.length; i < l; ++i) {
  3859. if (listeners[i] === listener) {
  3860. return listeners.splice(i, 1);
  3861. }
  3862. }
  3863. };
  3864. UploadProgress.prototype.dispatchEvent = function dispatchEvent(event) {
  3865. var listeners = this.eventListeners[event.type] || [];
  3866. for (var i = 0, listener; (listener = listeners[i]) != null; i++) {
  3867. listener(event);
  3868. }
  3869. };
  3870. // Note that for FakeXMLHttpRequest to work pre ES5
  3871. // we lose some of the alignment with the spec.
  3872. // To ensure as close a match as possible,
  3873. // set responseType before calling open, send or respond;
  3874. function FakeXMLHttpRequest() {
  3875. this.readyState = FakeXMLHttpRequest.UNSENT;
  3876. this.requestHeaders = {};
  3877. this.requestBody = null;
  3878. this.status = 0;
  3879. this.statusText = "";
  3880. this.upload = new UploadProgress();
  3881. this.responseType = "";
  3882. this.response = "";
  3883. if (sinonXhr.supportsCORS) {
  3884. this.withCredentials = false;
  3885. }
  3886. var xhr = this;
  3887. var events = ["loadstart", "load", "abort", "loadend"];
  3888. function addEventListener(eventName) {
  3889. xhr.addEventListener(eventName, function (event) {
  3890. var listener = xhr["on" + eventName];
  3891. if (listener && typeof listener === "function") {
  3892. listener.call(this, event);
  3893. }
  3894. });
  3895. }
  3896. for (var i = events.length - 1; i >= 0; i--) {
  3897. addEventListener(events[i]);
  3898. }
  3899. if (typeof FakeXMLHttpRequest.onCreate === "function") {
  3900. FakeXMLHttpRequest.onCreate(this);
  3901. }
  3902. }
  3903. function verifyState(xhr) {
  3904. if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
  3905. throw new Error("INVALID_STATE_ERR");
  3906. }
  3907. if (xhr.sendFlag) {
  3908. throw new Error("INVALID_STATE_ERR");
  3909. }
  3910. }
  3911. function getHeader(headers, header) {
  3912. header = header.toLowerCase();
  3913. for (var h in headers) {
  3914. if (h.toLowerCase() === header) {
  3915. return h;
  3916. }
  3917. }
  3918. return null;
  3919. }
  3920. // filtering to enable a white-list version of Sinon FakeXhr,
  3921. // where whitelisted requests are passed through to real XHR
  3922. function each(collection, callback) {
  3923. if (!collection) {
  3924. return;
  3925. }
  3926. for (var i = 0, l = collection.length; i < l; i += 1) {
  3927. callback(collection[i]);
  3928. }
  3929. }
  3930. function some(collection, callback) {
  3931. for (var index = 0; index < collection.length; index++) {
  3932. if (callback(collection[index]) === true) {
  3933. return true;
  3934. }
  3935. }
  3936. return false;
  3937. }
  3938. // largest arity in XHR is 5 - XHR#open
  3939. var apply = function (obj, method, args) {
  3940. switch (args.length) {
  3941. case 0: return obj[method]();
  3942. case 1: return obj[method](args[0]);
  3943. case 2: return obj[method](args[0], args[1]);
  3944. case 3: return obj[method](args[0], args[1], args[2]);
  3945. case 4: return obj[method](args[0], args[1], args[2], args[3]);
  3946. case 5: return obj[method](args[0], args[1], args[2], args[3], args[4]);
  3947. }
  3948. };
  3949. FakeXMLHttpRequest.filters = [];
  3950. FakeXMLHttpRequest.addFilter = function addFilter(fn) {
  3951. this.filters.push(fn);
  3952. };
  3953. var IE6Re = /MSIE 6/;
  3954. FakeXMLHttpRequest.defake = function defake(fakeXhr, xhrArgs) {
  3955. var xhr = new sinonXhr.workingXHR(); // eslint-disable-line new-cap
  3956. each([
  3957. "open",
  3958. "setRequestHeader",
  3959. "send",
  3960. "abort",
  3961. "getResponseHeader",
  3962. "getAllResponseHeaders",
  3963. "addEventListener",
  3964. "overrideMimeType",
  3965. "removeEventListener"
  3966. ], function (method) {
  3967. fakeXhr[method] = function () {
  3968. return apply(xhr, method, arguments);
  3969. };
  3970. });
  3971. var copyAttrs = function (args) {
  3972. each(args, function (attr) {
  3973. try {
  3974. fakeXhr[attr] = xhr[attr];
  3975. } catch (e) {
  3976. if (!IE6Re.test(navigator.userAgent)) {
  3977. throw e;
  3978. }
  3979. }
  3980. });
  3981. };
  3982. var stateChange = function stateChange() {
  3983. fakeXhr.readyState = xhr.readyState;
  3984. if (xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) {
  3985. copyAttrs(["status", "statusText"]);
  3986. }
  3987. if (xhr.readyState >= FakeXMLHttpRequest.LOADING) {
  3988. copyAttrs(["responseText", "response"]);
  3989. }
  3990. if (xhr.readyState === FakeXMLHttpRequest.DONE) {
  3991. copyAttrs(["responseXML"]);
  3992. }
  3993. if (fakeXhr.onreadystatechange) {
  3994. fakeXhr.onreadystatechange.call(fakeXhr, { target: fakeXhr });
  3995. }
  3996. };
  3997. if (xhr.addEventListener) {
  3998. for (var event in fakeXhr.eventListeners) {
  3999. if (fakeXhr.eventListeners.hasOwnProperty(event)) {
  4000. /*eslint-disable no-loop-func*/
  4001. each(fakeXhr.eventListeners[event], function (handler) {
  4002. xhr.addEventListener(event, handler);
  4003. });
  4004. /*eslint-enable no-loop-func*/
  4005. }
  4006. }
  4007. xhr.addEventListener("readystatechange", stateChange);
  4008. } else {
  4009. xhr.onreadystatechange = stateChange;
  4010. }
  4011. apply(xhr, "open", xhrArgs);
  4012. };
  4013. FakeXMLHttpRequest.useFilters = false;
  4014. function verifyRequestOpened(xhr) {
  4015. if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
  4016. throw new Error("INVALID_STATE_ERR - " + xhr.readyState);
  4017. }
  4018. }
  4019. function verifyRequestSent(xhr) {
  4020. if (xhr.readyState === FakeXMLHttpRequest.DONE) {
  4021. throw new Error("Request done");
  4022. }
  4023. }
  4024. function verifyHeadersReceived(xhr) {
  4025. if (xhr.async && xhr.readyState !== FakeXMLHttpRequest.HEADERS_RECEIVED) {
  4026. throw new Error("No headers received");
  4027. }
  4028. }
  4029. function verifyResponseBodyType(body) {
  4030. if (typeof body !== "string") {
  4031. var error = new Error("Attempted to respond to fake XMLHttpRequest with " +
  4032. body + ", which is not a string.");
  4033. error.name = "InvalidBodyException";
  4034. throw error;
  4035. }
  4036. }
  4037. FakeXMLHttpRequest.parseXML = function parseXML(text) {
  4038. var xmlDoc;
  4039. if (typeof DOMParser !== "undefined") {
  4040. var parser = new DOMParser();
  4041. xmlDoc = parser.parseFromString(text, "text/xml");
  4042. } else {
  4043. xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
  4044. xmlDoc.async = "false";
  4045. xmlDoc.loadXML(text);
  4046. }
  4047. return xmlDoc;
  4048. };
  4049. FakeXMLHttpRequest.statusCodes = {
  4050. 100: "Continue",
  4051. 101: "Switching Protocols",
  4052. 200: "OK",
  4053. 201: "Created",
  4054. 202: "Accepted",
  4055. 203: "Non-Authoritative Information",
  4056. 204: "No Content",
  4057. 205: "Reset Content",
  4058. 206: "Partial Content",
  4059. 207: "Multi-Status",
  4060. 300: "Multiple Choice",
  4061. 301: "Moved Permanently",
  4062. 302: "Found",
  4063. 303: "See Other",
  4064. 304: "Not Modified",
  4065. 305: "Use Proxy",
  4066. 307: "Temporary Redirect",
  4067. 400: "Bad Request",
  4068. 401: "Unauthorized",
  4069. 402: "Payment Required",
  4070. 403: "Forbidden",
  4071. 404: "Not Found",
  4072. 405: "Method Not Allowed",
  4073. 406: "Not Acceptable",
  4074. 407: "Proxy Authentication Required",
  4075. 408: "Request Timeout",
  4076. 409: "Conflict",
  4077. 410: "Gone",
  4078. 411: "Length Required",
  4079. 412: "Precondition Failed",
  4080. 413: "Request Entity Too Large",
  4081. 414: "Request-URI Too Long",
  4082. 415: "Unsupported Media Type",
  4083. 416: "Requested Range Not Satisfiable",
  4084. 417: "Expectation Failed",
  4085. 422: "Unprocessable Entity",
  4086. 500: "Internal Server Error",
  4087. 501: "Not Implemented",
  4088. 502: "Bad Gateway",
  4089. 503: "Service Unavailable",
  4090. 504: "Gateway Timeout",
  4091. 505: "HTTP Version Not Supported"
  4092. };
  4093. function makeApi(sinon) {
  4094. sinon.xhr = sinonXhr;
  4095. sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, {
  4096. async: true,
  4097. open: function open(method, url, async, username, password) {
  4098. this.method = method;
  4099. this.url = url;
  4100. this.async = typeof async === "boolean" ? async : true;
  4101. this.username = username;
  4102. this.password = password;
  4103. this.responseText = null;
  4104. this.response = this.responseType === "json" ? null : "";
  4105. this.responseXML = null;
  4106. this.requestHeaders = {};
  4107. this.sendFlag = false;
  4108. if (FakeXMLHttpRequest.useFilters === true) {
  4109. var xhrArgs = arguments;
  4110. var defake = some(FakeXMLHttpRequest.filters, function (filter) {
  4111. return filter.apply(this, xhrArgs);
  4112. });
  4113. if (defake) {
  4114. return FakeXMLHttpRequest.defake(this, arguments);
  4115. }
  4116. }
  4117. this.readyStateChange(FakeXMLHttpRequest.OPENED);
  4118. },
  4119. readyStateChange: function readyStateChange(state) {
  4120. this.readyState = state;
  4121. var readyStateChangeEvent = new sinon.Event("readystatechange", false, false, this);
  4122. if (typeof this.onreadystatechange === "function") {
  4123. try {
  4124. this.onreadystatechange(readyStateChangeEvent);
  4125. } catch (e) {
  4126. sinon.logError("Fake XHR onreadystatechange handler", e);
  4127. }
  4128. }
  4129. switch (this.readyState) {
  4130. case FakeXMLHttpRequest.DONE:
  4131. if (supportsProgress) {
  4132. this.upload.dispatchEvent(new sinon.ProgressEvent("progress", {loaded: 100, total: 100}));
  4133. this.dispatchEvent(new sinon.ProgressEvent("progress", {loaded: 100, total: 100}));
  4134. }
  4135. this.upload.dispatchEvent(new sinon.Event("load", false, false, this));
  4136. this.dispatchEvent(new sinon.Event("load", false, false, this));
  4137. this.dispatchEvent(new sinon.Event("loadend", false, false, this));
  4138. break;
  4139. }
  4140. this.dispatchEvent(readyStateChangeEvent);
  4141. },
  4142. setRequestHeader: function setRequestHeader(header, value) {
  4143. verifyState(this);
  4144. if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
  4145. throw new Error("Refused to set unsafe header \"" + header + "\"");
  4146. }
  4147. if (this.requestHeaders[header]) {
  4148. this.requestHeaders[header] += "," + value;
  4149. } else {
  4150. this.requestHeaders[header] = value;
  4151. }
  4152. },
  4153. // Helps testing
  4154. setResponseHeaders: function setResponseHeaders(headers) {
  4155. verifyRequestOpened(this);
  4156. this.responseHeaders = {};
  4157. for (var header in headers) {
  4158. if (headers.hasOwnProperty(header)) {
  4159. this.responseHeaders[header] = headers[header];
  4160. }
  4161. }
  4162. if (this.async) {
  4163. this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
  4164. } else {
  4165. this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
  4166. }
  4167. },
  4168. // Currently treats ALL data as a DOMString (i.e. no Document)
  4169. send: function send(data) {
  4170. verifyState(this);
  4171. if (!/^(get|head)$/i.test(this.method)) {
  4172. var contentType = getHeader(this.requestHeaders, "Content-Type");
  4173. if (this.requestHeaders[contentType]) {
  4174. var value = this.requestHeaders[contentType].split(";");
  4175. this.requestHeaders[contentType] = value[0] + ";charset=utf-8";
  4176. } else if (supportsFormData && !(data instanceof FormData)) {
  4177. this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
  4178. }
  4179. this.requestBody = data;
  4180. }
  4181. this.errorFlag = false;
  4182. this.sendFlag = this.async;
  4183. this.response = this.responseType === "json" ? null : "";
  4184. this.readyStateChange(FakeXMLHttpRequest.OPENED);
  4185. if (typeof this.onSend === "function") {
  4186. this.onSend(this);
  4187. }
  4188. this.dispatchEvent(new sinon.Event("loadstart", false, false, this));
  4189. },
  4190. abort: function abort() {
  4191. this.aborted = true;
  4192. this.responseText = null;
  4193. this.response = this.responseType === "json" ? null : "";
  4194. this.errorFlag = true;
  4195. this.requestHeaders = {};
  4196. this.responseHeaders = {};
  4197. if (this.readyState > FakeXMLHttpRequest.UNSENT && this.sendFlag) {
  4198. this.readyStateChange(FakeXMLHttpRequest.DONE);
  4199. this.sendFlag = false;
  4200. }
  4201. this.readyState = FakeXMLHttpRequest.UNSENT;
  4202. this.dispatchEvent(new sinon.Event("abort", false, false, this));
  4203. this.upload.dispatchEvent(new sinon.Event("abort", false, false, this));
  4204. if (typeof this.onerror === "function") {
  4205. this.onerror();
  4206. }
  4207. },
  4208. getResponseHeader: function getResponseHeader(header) {
  4209. if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
  4210. return null;
  4211. }
  4212. if (/^Set-Cookie2?$/i.test(header)) {
  4213. return null;
  4214. }
  4215. header = getHeader(this.responseHeaders, header);
  4216. return this.responseHeaders[header] || null;
  4217. },
  4218. getAllResponseHeaders: function getAllResponseHeaders() {
  4219. if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
  4220. return "";
  4221. }
  4222. var headers = "";
  4223. for (var header in this.responseHeaders) {
  4224. if (this.responseHeaders.hasOwnProperty(header) &&
  4225. !/^Set-Cookie2?$/i.test(header)) {
  4226. headers += header + ": " + this.responseHeaders[header] + "\r\n";
  4227. }
  4228. }
  4229. return headers;
  4230. },
  4231. setResponseBody: function setResponseBody(body) {
  4232. verifyRequestSent(this);
  4233. verifyHeadersReceived(this);
  4234. verifyResponseBodyType(body);
  4235. var chunkSize = this.chunkSize || 10;
  4236. var index = 0;
  4237. this.responseText = "";
  4238. do {
  4239. if (this.async) {
  4240. this.readyStateChange(FakeXMLHttpRequest.LOADING);
  4241. }
  4242. this.responseText += body.substring(index, index + chunkSize);
  4243. index += chunkSize;
  4244. } while (index < body.length);
  4245. var type = this.getResponseHeader("Content-Type");
  4246. if (this.responseText &&
  4247. (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
  4248. try {
  4249. this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText);
  4250. } catch (e) {
  4251. // Unable to parse XML - no biggie
  4252. }
  4253. }
  4254. this.response = this.responseType === "json" ? JSON.parse(this.responseText) : this.responseText;
  4255. this.readyStateChange(FakeXMLHttpRequest.DONE);
  4256. },
  4257. respond: function respond(status, headers, body) {
  4258. this.status = typeof status === "number" ? status : 200;
  4259. this.statusText = FakeXMLHttpRequest.statusCodes[this.status];
  4260. this.setResponseHeaders(headers || {});
  4261. this.setResponseBody(body || "");
  4262. },
  4263. uploadProgress: function uploadProgress(progressEventRaw) {
  4264. if (supportsProgress) {
  4265. this.upload.dispatchEvent(new sinon.ProgressEvent("progress", progressEventRaw));
  4266. }
  4267. },
  4268. downloadProgress: function downloadProgress(progressEventRaw) {
  4269. if (supportsProgress) {
  4270. this.dispatchEvent(new sinon.ProgressEvent("progress", progressEventRaw));
  4271. }
  4272. },
  4273. uploadError: function uploadError(error) {
  4274. if (supportsCustomEvent) {
  4275. this.upload.dispatchEvent(new sinon.CustomEvent("error", {detail: error}));
  4276. }
  4277. }
  4278. });
  4279. sinon.extend(FakeXMLHttpRequest, {
  4280. UNSENT: 0,
  4281. OPENED: 1,
  4282. HEADERS_RECEIVED: 2,
  4283. LOADING: 3,
  4284. DONE: 4
  4285. });
  4286. sinon.useFakeXMLHttpRequest = function () {
  4287. FakeXMLHttpRequest.restore = function restore(keepOnCreate) {
  4288. if (sinonXhr.supportsXHR) {
  4289. global.XMLHttpRequest = sinonXhr.GlobalXMLHttpRequest;
  4290. }
  4291. if (sinonXhr.supportsActiveX) {
  4292. global.ActiveXObject = sinonXhr.GlobalActiveXObject;
  4293. }
  4294. delete FakeXMLHttpRequest.restore;
  4295. if (keepOnCreate !== true) {
  4296. delete FakeXMLHttpRequest.onCreate;
  4297. }
  4298. };
  4299. if (sinonXhr.supportsXHR) {
  4300. global.XMLHttpRequest = FakeXMLHttpRequest;
  4301. }
  4302. if (sinonXhr.supportsActiveX) {
  4303. global.ActiveXObject = function ActiveXObject(objId) {
  4304. if (objId === "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) {
  4305. return new FakeXMLHttpRequest();
  4306. }
  4307. return new sinonXhr.GlobalActiveXObject(objId);
  4308. };
  4309. }
  4310. return FakeXMLHttpRequest;
  4311. };
  4312. sinon.FakeXMLHttpRequest = FakeXMLHttpRequest;
  4313. }
  4314. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  4315. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  4316. function loadDependencies(require, exports, module) {
  4317. var sinon = require("./core");
  4318. require("../extend");
  4319. require("./event");
  4320. require("../log_error");
  4321. makeApi(sinon);
  4322. module.exports = sinon;
  4323. }
  4324. if (isAMD) {
  4325. define(loadDependencies);
  4326. return;
  4327. }
  4328. if (isNode) {
  4329. loadDependencies(require, module.exports, module);
  4330. return;
  4331. }
  4332. if (sinonGlobal) {
  4333. makeApi(sinonGlobal);
  4334. }
  4335. }(
  4336. typeof sinon === "object" && sinon, // eslint-disable-line no-undef
  4337. typeof global !== "undefined" ? global : self
  4338. ));
  4339. /**
  4340. * @depend fake_xdomain_request.js
  4341. * @depend fake_xml_http_request.js
  4342. * @depend ../format.js
  4343. * @depend ../log_error.js
  4344. */
  4345. /**
  4346. * The Sinon "server" mimics a web server that receives requests from
  4347. * sinon.FakeXMLHttpRequest and provides an API to respond to those requests,
  4348. * both synchronously and asynchronously. To respond synchronuously, canned
  4349. * answers have to be provided upfront.
  4350. *
  4351. * @author Christian Johansen (christian@cjohansen.no)
  4352. * @license BSD
  4353. *
  4354. * Copyright (c) 2010-2013 Christian Johansen
  4355. */
  4356. (function () {
  4357. var push = [].push;
  4358. function responseArray(handler) {
  4359. var response = handler;
  4360. if (Object.prototype.toString.call(handler) !== "[object Array]") {
  4361. response = [200, {}, handler];
  4362. }
  4363. if (typeof response[2] !== "string") {
  4364. throw new TypeError("Fake server response body should be string, but was " +
  4365. typeof response[2]);
  4366. }
  4367. return response;
  4368. }
  4369. var wloc = typeof window !== "undefined" ? window.location : {};
  4370. var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
  4371. function matchOne(response, reqMethod, reqUrl) {
  4372. var rmeth = response.method;
  4373. var matchMethod = !rmeth || rmeth.toLowerCase() === reqMethod.toLowerCase();
  4374. var url = response.url;
  4375. var matchUrl = !url || url === reqUrl || (typeof url.test === "function" && url.test(reqUrl));
  4376. return matchMethod && matchUrl;
  4377. }
  4378. function match(response, request) {
  4379. var requestUrl = request.url;
  4380. if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
  4381. requestUrl = requestUrl.replace(rCurrLoc, "");
  4382. }
  4383. if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
  4384. if (typeof response.response === "function") {
  4385. var ru = response.url;
  4386. var args = [request].concat(ru && typeof ru.exec === "function" ? ru.exec(requestUrl).slice(1) : []);
  4387. return response.response.apply(response, args);
  4388. }
  4389. return true;
  4390. }
  4391. return false;
  4392. }
  4393. function makeApi(sinon) {
  4394. sinon.fakeServer = {
  4395. create: function (config) {
  4396. var server = sinon.create(this);
  4397. server.configure(config);
  4398. if (!sinon.xhr.supportsCORS) {
  4399. this.xhr = sinon.useFakeXDomainRequest();
  4400. } else {
  4401. this.xhr = sinon.useFakeXMLHttpRequest();
  4402. }
  4403. server.requests = [];
  4404. this.xhr.onCreate = function (xhrObj) {
  4405. server.addRequest(xhrObj);
  4406. };
  4407. return server;
  4408. },
  4409. configure: function (config) {
  4410. var whitelist = {
  4411. "autoRespond": true,
  4412. "autoRespondAfter": true,
  4413. "respondImmediately": true,
  4414. "fakeHTTPMethods": true
  4415. };
  4416. var setting;
  4417. config = config || {};
  4418. for (setting in config) {
  4419. if (whitelist.hasOwnProperty(setting) && config.hasOwnProperty(setting)) {
  4420. this[setting] = config[setting];
  4421. }
  4422. }
  4423. },
  4424. addRequest: function addRequest(xhrObj) {
  4425. var server = this;
  4426. push.call(this.requests, xhrObj);
  4427. xhrObj.onSend = function () {
  4428. server.handleRequest(this);
  4429. if (server.respondImmediately) {
  4430. server.respond();
  4431. } else if (server.autoRespond && !server.responding) {
  4432. setTimeout(function () {
  4433. server.responding = false;
  4434. server.respond();
  4435. }, server.autoRespondAfter || 10);
  4436. server.responding = true;
  4437. }
  4438. };
  4439. },
  4440. getHTTPMethod: function getHTTPMethod(request) {
  4441. if (this.fakeHTTPMethods && /post/i.test(request.method)) {
  4442. var matches = (request.requestBody || "").match(/_method=([^\b;]+)/);
  4443. return matches ? matches[1] : request.method;
  4444. }
  4445. return request.method;
  4446. },
  4447. handleRequest: function handleRequest(xhr) {
  4448. if (xhr.async) {
  4449. if (!this.queue) {
  4450. this.queue = [];
  4451. }
  4452. push.call(this.queue, xhr);
  4453. } else {
  4454. this.processRequest(xhr);
  4455. }
  4456. },
  4457. log: function log(response, request) {
  4458. var str;
  4459. str = "Request:\n" + sinon.format(request) + "\n\n";
  4460. str += "Response:\n" + sinon.format(response) + "\n\n";
  4461. sinon.log(str);
  4462. },
  4463. respondWith: function respondWith(method, url, body) {
  4464. if (arguments.length === 1 && typeof method !== "function") {
  4465. this.response = responseArray(method);
  4466. return;
  4467. }
  4468. if (!this.responses) {
  4469. this.responses = [];
  4470. }
  4471. if (arguments.length === 1) {
  4472. body = method;
  4473. url = method = null;
  4474. }
  4475. if (arguments.length === 2) {
  4476. body = url;
  4477. url = method;
  4478. method = null;
  4479. }
  4480. push.call(this.responses, {
  4481. method: method,
  4482. url: url,
  4483. response: typeof body === "function" ? body : responseArray(body)
  4484. });
  4485. },
  4486. respond: function respond() {
  4487. if (arguments.length > 0) {
  4488. this.respondWith.apply(this, arguments);
  4489. }
  4490. var queue = this.queue || [];
  4491. var requests = queue.splice(0, queue.length);
  4492. for (var i = 0; i < requests.length; i++) {
  4493. this.processRequest(requests[i]);
  4494. }
  4495. },
  4496. processRequest: function processRequest(request) {
  4497. try {
  4498. if (request.aborted) {
  4499. return;
  4500. }
  4501. var response = this.response || [404, {}, ""];
  4502. if (this.responses) {
  4503. for (var l = this.responses.length, i = l - 1; i >= 0; i--) {
  4504. if (match.call(this, this.responses[i], request)) {
  4505. response = this.responses[i].response;
  4506. break;
  4507. }
  4508. }
  4509. }
  4510. if (request.readyState !== 4) {
  4511. this.log(response, request);
  4512. request.respond(response[0], response[1], response[2]);
  4513. }
  4514. } catch (e) {
  4515. sinon.logError("Fake server request processing", e);
  4516. }
  4517. },
  4518. restore: function restore() {
  4519. return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
  4520. }
  4521. };
  4522. }
  4523. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  4524. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  4525. function loadDependencies(require, exports, module) {
  4526. var sinon = require("./core");
  4527. require("./fake_xdomain_request");
  4528. require("./fake_xml_http_request");
  4529. require("../format");
  4530. makeApi(sinon);
  4531. module.exports = sinon;
  4532. }
  4533. if (isAMD) {
  4534. define(loadDependencies);
  4535. } else if (isNode) {
  4536. loadDependencies(require, module.exports, module);
  4537. } else {
  4538. makeApi(sinon); // eslint-disable-line no-undef
  4539. }
  4540. }());
  4541. /**
  4542. * @depend fake_server.js
  4543. * @depend fake_timers.js
  4544. */
  4545. /**
  4546. * Add-on for sinon.fakeServer that automatically handles a fake timer along with
  4547. * the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery
  4548. * 1.3.x, which does not use xhr object's onreadystatehandler at all - instead,
  4549. * it polls the object for completion with setInterval. Dispite the direct
  4550. * motivation, there is nothing jQuery-specific in this file, so it can be used
  4551. * in any environment where the ajax implementation depends on setInterval or
  4552. * setTimeout.
  4553. *
  4554. * @author Christian Johansen (christian@cjohansen.no)
  4555. * @license BSD
  4556. *
  4557. * Copyright (c) 2010-2013 Christian Johansen
  4558. */
  4559. (function () {
  4560. function makeApi(sinon) {
  4561. function Server() {}
  4562. Server.prototype = sinon.fakeServer;
  4563. sinon.fakeServerWithClock = new Server();
  4564. sinon.fakeServerWithClock.addRequest = function addRequest(xhr) {
  4565. if (xhr.async) {
  4566. if (typeof setTimeout.clock === "object") {
  4567. this.clock = setTimeout.clock;
  4568. } else {
  4569. this.clock = sinon.useFakeTimers();
  4570. this.resetClock = true;
  4571. }
  4572. if (!this.longestTimeout) {
  4573. var clockSetTimeout = this.clock.setTimeout;
  4574. var clockSetInterval = this.clock.setInterval;
  4575. var server = this;
  4576. this.clock.setTimeout = function (fn, timeout) {
  4577. server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
  4578. return clockSetTimeout.apply(this, arguments);
  4579. };
  4580. this.clock.setInterval = function (fn, timeout) {
  4581. server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
  4582. return clockSetInterval.apply(this, arguments);
  4583. };
  4584. }
  4585. }
  4586. return sinon.fakeServer.addRequest.call(this, xhr);
  4587. };
  4588. sinon.fakeServerWithClock.respond = function respond() {
  4589. var returnVal = sinon.fakeServer.respond.apply(this, arguments);
  4590. if (this.clock) {
  4591. this.clock.tick(this.longestTimeout || 0);
  4592. this.longestTimeout = 0;
  4593. if (this.resetClock) {
  4594. this.clock.restore();
  4595. this.resetClock = false;
  4596. }
  4597. }
  4598. return returnVal;
  4599. };
  4600. sinon.fakeServerWithClock.restore = function restore() {
  4601. if (this.clock) {
  4602. this.clock.restore();
  4603. }
  4604. return sinon.fakeServer.restore.apply(this, arguments);
  4605. };
  4606. }
  4607. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  4608. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  4609. function loadDependencies(require) {
  4610. var sinon = require("./core");
  4611. require("./fake_server");
  4612. require("./fake_timers");
  4613. makeApi(sinon);
  4614. }
  4615. if (isAMD) {
  4616. define(loadDependencies);
  4617. } else if (isNode) {
  4618. loadDependencies(require);
  4619. } else {
  4620. makeApi(sinon); // eslint-disable-line no-undef
  4621. }
  4622. }());
  4623. /**
  4624. * @depend util/core.js
  4625. * @depend extend.js
  4626. * @depend collection.js
  4627. * @depend util/fake_timers.js
  4628. * @depend util/fake_server_with_clock.js
  4629. */
  4630. /**
  4631. * Manages fake collections as well as fake utilities such as Sinon's
  4632. * timers and fake XHR implementation in one convenient object.
  4633. *
  4634. * @author Christian Johansen (christian@cjohansen.no)
  4635. * @license BSD
  4636. *
  4637. * Copyright (c) 2010-2013 Christian Johansen
  4638. */
  4639. (function (sinonGlobal) {
  4640. function makeApi(sinon) {
  4641. var push = [].push;
  4642. function exposeValue(sandbox, config, key, value) {
  4643. if (!value) {
  4644. return;
  4645. }
  4646. if (config.injectInto && !(key in config.injectInto)) {
  4647. config.injectInto[key] = value;
  4648. sandbox.injectedKeys.push(key);
  4649. } else {
  4650. push.call(sandbox.args, value);
  4651. }
  4652. }
  4653. function prepareSandboxFromConfig(config) {
  4654. var sandbox = sinon.create(sinon.sandbox);
  4655. if (config.useFakeServer) {
  4656. if (typeof config.useFakeServer === "object") {
  4657. sandbox.serverPrototype = config.useFakeServer;
  4658. }
  4659. sandbox.useFakeServer();
  4660. }
  4661. if (config.useFakeTimers) {
  4662. if (typeof config.useFakeTimers === "object") {
  4663. sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers);
  4664. } else {
  4665. sandbox.useFakeTimers();
  4666. }
  4667. }
  4668. return sandbox;
  4669. }
  4670. sinon.sandbox = sinon.extend(sinon.create(sinon.collection), {
  4671. useFakeTimers: function useFakeTimers() {
  4672. this.clock = sinon.useFakeTimers.apply(sinon, arguments);
  4673. return this.add(this.clock);
  4674. },
  4675. serverPrototype: sinon.fakeServer,
  4676. useFakeServer: function useFakeServer() {
  4677. var proto = this.serverPrototype || sinon.fakeServer;
  4678. if (!proto || !proto.create) {
  4679. return null;
  4680. }
  4681. this.server = proto.create();
  4682. return this.add(this.server);
  4683. },
  4684. inject: function (obj) {
  4685. sinon.collection.inject.call(this, obj);
  4686. if (this.clock) {
  4687. obj.clock = this.clock;
  4688. }
  4689. if (this.server) {
  4690. obj.server = this.server;
  4691. obj.requests = this.server.requests;
  4692. }
  4693. obj.match = sinon.match;
  4694. return obj;
  4695. },
  4696. restore: function () {
  4697. sinon.collection.restore.apply(this, arguments);
  4698. this.restoreContext();
  4699. },
  4700. restoreContext: function () {
  4701. if (this.injectedKeys) {
  4702. for (var i = 0, j = this.injectedKeys.length; i < j; i++) {
  4703. delete this.injectInto[this.injectedKeys[i]];
  4704. }
  4705. this.injectedKeys = [];
  4706. }
  4707. },
  4708. create: function (config) {
  4709. if (!config) {
  4710. return sinon.create(sinon.sandbox);
  4711. }
  4712. var sandbox = prepareSandboxFromConfig(config);
  4713. sandbox.args = sandbox.args || [];
  4714. sandbox.injectedKeys = [];
  4715. sandbox.injectInto = config.injectInto;
  4716. var prop,
  4717. value;
  4718. var exposed = sandbox.inject({});
  4719. if (config.properties) {
  4720. for (var i = 0, l = config.properties.length; i < l; i++) {
  4721. prop = config.properties[i];
  4722. value = exposed[prop] || prop === "sandbox" && sandbox;
  4723. exposeValue(sandbox, config, prop, value);
  4724. }
  4725. } else {
  4726. exposeValue(sandbox, config, "sandbox", value);
  4727. }
  4728. return sandbox;
  4729. },
  4730. match: sinon.match
  4731. });
  4732. sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;
  4733. return sinon.sandbox;
  4734. }
  4735. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  4736. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  4737. function loadDependencies(require, exports, module) {
  4738. var sinon = require("./util/core");
  4739. require("./extend");
  4740. require("./util/fake_server_with_clock");
  4741. require("./util/fake_timers");
  4742. require("./collection");
  4743. module.exports = makeApi(sinon);
  4744. }
  4745. if (isAMD) {
  4746. define(loadDependencies);
  4747. return;
  4748. }
  4749. if (isNode) {
  4750. loadDependencies(require, module.exports, module);
  4751. return;
  4752. }
  4753. if (sinonGlobal) {
  4754. makeApi(sinonGlobal);
  4755. }
  4756. }(
  4757. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  4758. ));
  4759. /**
  4760. * @depend util/core.js
  4761. * @depend sandbox.js
  4762. */
  4763. /**
  4764. * Test function, sandboxes fakes
  4765. *
  4766. * @author Christian Johansen (christian@cjohansen.no)
  4767. * @license BSD
  4768. *
  4769. * Copyright (c) 2010-2013 Christian Johansen
  4770. */
  4771. (function (sinonGlobal) {
  4772. function makeApi(sinon) {
  4773. var slice = Array.prototype.slice;
  4774. function test(callback) {
  4775. var type = typeof callback;
  4776. if (type !== "function") {
  4777. throw new TypeError("sinon.test needs to wrap a test function, got " + type);
  4778. }
  4779. function sinonSandboxedTest() {
  4780. var config = sinon.getConfig(sinon.config);
  4781. config.injectInto = config.injectIntoThis && this || config.injectInto;
  4782. var sandbox = sinon.sandbox.create(config);
  4783. var args = slice.call(arguments);
  4784. var oldDone = args.length && args[args.length - 1];
  4785. var exception, result;
  4786. if (typeof oldDone === "function") {
  4787. args[args.length - 1] = function sinonDone(res) {
  4788. if (res) {
  4789. sandbox.restore();
  4790. throw exception;
  4791. } else {
  4792. sandbox.verifyAndRestore();
  4793. }
  4794. oldDone(res);
  4795. };
  4796. }
  4797. try {
  4798. result = callback.apply(this, args.concat(sandbox.args));
  4799. } catch (e) {
  4800. exception = e;
  4801. }
  4802. if (typeof oldDone !== "function") {
  4803. if (typeof exception !== "undefined") {
  4804. sandbox.restore();
  4805. throw exception;
  4806. } else {
  4807. sandbox.verifyAndRestore();
  4808. }
  4809. }
  4810. return result;
  4811. }
  4812. if (callback.length) {
  4813. return function sinonAsyncSandboxedTest(done) { // eslint-disable-line no-unused-vars
  4814. return sinonSandboxedTest.apply(this, arguments);
  4815. };
  4816. }
  4817. return sinonSandboxedTest;
  4818. }
  4819. test.config = {
  4820. injectIntoThis: true,
  4821. injectInto: null,
  4822. properties: ["spy", "stub", "mock", "clock", "server", "requests"],
  4823. useFakeTimers: true,
  4824. useFakeServer: true
  4825. };
  4826. sinon.test = test;
  4827. return test;
  4828. }
  4829. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  4830. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  4831. function loadDependencies(require, exports, module) {
  4832. var core = require("./util/core");
  4833. require("./sandbox");
  4834. module.exports = makeApi(core);
  4835. }
  4836. if (isAMD) {
  4837. define(loadDependencies);
  4838. } else if (isNode) {
  4839. loadDependencies(require, module.exports, module);
  4840. } else if (sinonGlobal) {
  4841. makeApi(sinonGlobal);
  4842. }
  4843. }(typeof sinon === "object" && sinon || null)); // eslint-disable-line no-undef
  4844. /**
  4845. * @depend util/core.js
  4846. * @depend test.js
  4847. */
  4848. /**
  4849. * Test case, sandboxes all test functions
  4850. *
  4851. * @author Christian Johansen (christian@cjohansen.no)
  4852. * @license BSD
  4853. *
  4854. * Copyright (c) 2010-2013 Christian Johansen
  4855. */
  4856. (function (sinonGlobal) {
  4857. function createTest(property, setUp, tearDown) {
  4858. return function () {
  4859. if (setUp) {
  4860. setUp.apply(this, arguments);
  4861. }
  4862. var exception, result;
  4863. try {
  4864. result = property.apply(this, arguments);
  4865. } catch (e) {
  4866. exception = e;
  4867. }
  4868. if (tearDown) {
  4869. tearDown.apply(this, arguments);
  4870. }
  4871. if (exception) {
  4872. throw exception;
  4873. }
  4874. return result;
  4875. };
  4876. }
  4877. function makeApi(sinon) {
  4878. function testCase(tests, prefix) {
  4879. if (!tests || typeof tests !== "object") {
  4880. throw new TypeError("sinon.testCase needs an object with test functions");
  4881. }
  4882. prefix = prefix || "test";
  4883. var rPrefix = new RegExp("^" + prefix);
  4884. var methods = {};
  4885. var setUp = tests.setUp;
  4886. var tearDown = tests.tearDown;
  4887. var testName,
  4888. property,
  4889. method;
  4890. for (testName in tests) {
  4891. if (tests.hasOwnProperty(testName) && !/^(setUp|tearDown)$/.test(testName)) {
  4892. property = tests[testName];
  4893. if (typeof property === "function" && rPrefix.test(testName)) {
  4894. method = property;
  4895. if (setUp || tearDown) {
  4896. method = createTest(property, setUp, tearDown);
  4897. }
  4898. methods[testName] = sinon.test(method);
  4899. } else {
  4900. methods[testName] = tests[testName];
  4901. }
  4902. }
  4903. }
  4904. return methods;
  4905. }
  4906. sinon.testCase = testCase;
  4907. return testCase;
  4908. }
  4909. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  4910. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  4911. function loadDependencies(require, exports, module) {
  4912. var core = require("./util/core");
  4913. require("./test");
  4914. module.exports = makeApi(core);
  4915. }
  4916. if (isAMD) {
  4917. define(loadDependencies);
  4918. return;
  4919. }
  4920. if (isNode) {
  4921. loadDependencies(require, module.exports, module);
  4922. return;
  4923. }
  4924. if (sinonGlobal) {
  4925. makeApi(sinonGlobal);
  4926. }
  4927. }(
  4928. typeof sinon === "object" && sinon // eslint-disable-line no-undef
  4929. ));
  4930. /**
  4931. * @depend times_in_words.js
  4932. * @depend util/core.js
  4933. * @depend match.js
  4934. * @depend format.js
  4935. */
  4936. /**
  4937. * Assertions matching the test spy retrieval interface.
  4938. *
  4939. * @author Christian Johansen (christian@cjohansen.no)
  4940. * @license BSD
  4941. *
  4942. * Copyright (c) 2010-2013 Christian Johansen
  4943. */
  4944. (function (sinonGlobal, global) {
  4945. var slice = Array.prototype.slice;
  4946. function makeApi(sinon) {
  4947. var assert;
  4948. function verifyIsStub() {
  4949. var method;
  4950. for (var i = 0, l = arguments.length; i < l; ++i) {
  4951. method = arguments[i];
  4952. if (!method) {
  4953. assert.fail("fake is not a spy");
  4954. }
  4955. if (method.proxy && method.proxy.isSinonProxy) {
  4956. verifyIsStub(method.proxy);
  4957. } else {
  4958. if (typeof method !== "function") {
  4959. assert.fail(method + " is not a function");
  4960. }
  4961. if (typeof method.getCall !== "function") {
  4962. assert.fail(method + " is not stubbed");
  4963. }
  4964. }
  4965. }
  4966. }
  4967. function failAssertion(object, msg) {
  4968. object = object || global;
  4969. var failMethod = object.fail || assert.fail;
  4970. failMethod.call(object, msg);
  4971. }
  4972. function mirrorPropAsAssertion(name, method, message) {
  4973. if (arguments.length === 2) {
  4974. message = method;
  4975. method = name;
  4976. }
  4977. assert[name] = function (fake) {
  4978. verifyIsStub(fake);
  4979. var args = slice.call(arguments, 1);
  4980. var failed = false;
  4981. if (typeof method === "function") {
  4982. failed = !method(fake);
  4983. } else {
  4984. failed = typeof fake[method] === "function" ?
  4985. !fake[method].apply(fake, args) : !fake[method];
  4986. }
  4987. if (failed) {
  4988. failAssertion(this, (fake.printf || fake.proxy.printf).apply(fake, [message].concat(args)));
  4989. } else {
  4990. assert.pass(name);
  4991. }
  4992. };
  4993. }
  4994. function exposedName(prefix, prop) {
  4995. return !prefix || /^fail/.test(prop) ? prop :
  4996. prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1);
  4997. }
  4998. assert = {
  4999. failException: "AssertError",
  5000. fail: function fail(message) {
  5001. var error = new Error(message);
  5002. error.name = this.failException || assert.failException;
  5003. throw error;
  5004. },
  5005. pass: function pass() {},
  5006. callOrder: function assertCallOrder() {
  5007. verifyIsStub.apply(null, arguments);
  5008. var expected = "";
  5009. var actual = "";
  5010. if (!sinon.calledInOrder(arguments)) {
  5011. try {
  5012. expected = [].join.call(arguments, ", ");
  5013. var calls = slice.call(arguments);
  5014. var i = calls.length;
  5015. while (i) {
  5016. if (!calls[--i].called) {
  5017. calls.splice(i, 1);
  5018. }
  5019. }
  5020. actual = sinon.orderByFirstCall(calls).join(", ");
  5021. } catch (e) {
  5022. // If this fails, we'll just fall back to the blank string
  5023. }
  5024. failAssertion(this, "expected " + expected + " to be " +
  5025. "called in order but were called as " + actual);
  5026. } else {
  5027. assert.pass("callOrder");
  5028. }
  5029. },
  5030. callCount: function assertCallCount(method, count) {
  5031. verifyIsStub(method);
  5032. if (method.callCount !== count) {
  5033. var msg = "expected %n to be called " + sinon.timesInWords(count) +
  5034. " but was called %c%C";
  5035. failAssertion(this, method.printf(msg));
  5036. } else {
  5037. assert.pass("callCount");
  5038. }
  5039. },
  5040. expose: function expose(target, options) {
  5041. if (!target) {
  5042. throw new TypeError("target is null or undefined");
  5043. }
  5044. var o = options || {};
  5045. var prefix = typeof o.prefix === "undefined" && "assert" || o.prefix;
  5046. var includeFail = typeof o.includeFail === "undefined" || !!o.includeFail;
  5047. for (var method in this) {
  5048. if (method !== "expose" && (includeFail || !/^(fail)/.test(method))) {
  5049. target[exposedName(prefix, method)] = this[method];
  5050. }
  5051. }
  5052. return target;
  5053. },
  5054. match: function match(actual, expectation) {
  5055. var matcher = sinon.match(expectation);
  5056. if (matcher.test(actual)) {
  5057. assert.pass("match");
  5058. } else {
  5059. var formatted = [
  5060. "expected value to match",
  5061. " expected = " + sinon.format(expectation),
  5062. " actual = " + sinon.format(actual)
  5063. ];
  5064. failAssertion(this, formatted.join("\n"));
  5065. }
  5066. }
  5067. };
  5068. mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
  5069. mirrorPropAsAssertion("notCalled", function (spy) {
  5070. return !spy.called;
  5071. }, "expected %n to not have been called but was called %c%C");
  5072. mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
  5073. mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
  5074. mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
  5075. mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
  5076. mirrorPropAsAssertion(
  5077. "alwaysCalledOn",
  5078. "expected %n to always be called with %1 as this but was called with %t"
  5079. );
  5080. mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
  5081. mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
  5082. mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
  5083. mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
  5084. mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
  5085. mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C");
  5086. mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
  5087. mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
  5088. mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
  5089. mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
  5090. mirrorPropAsAssertion("threw", "%n did not throw exception%C");
  5091. mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
  5092. sinon.assert = assert;
  5093. return assert;
  5094. }
  5095. var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
  5096. var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;
  5097. function loadDependencies(require, exports, module) {
  5098. var sinon = require("./util/core");
  5099. require("./match");
  5100. require("./format");
  5101. module.exports = makeApi(sinon);
  5102. }
  5103. if (isAMD) {
  5104. define(loadDependencies);
  5105. return;
  5106. }
  5107. if (isNode) {
  5108. loadDependencies(require, module.exports, module);
  5109. return;
  5110. }
  5111. if (sinonGlobal) {
  5112. makeApi(sinonGlobal);
  5113. }
  5114. }(
  5115. typeof sinon === "object" && sinon, // eslint-disable-line no-undef
  5116. typeof global !== "undefined" ? global : self
  5117. ));
  5118. return sinon;
  5119. }));