sinon.js 204 KB

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