sinon-1.7.1.js 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299
  1. /**
  2. * Sinon.JS 1.7.1, 2013/05/07
  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-2013, 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. this.sinon = (function () {
  36. var buster = (function (setTimeout, B) {
  37. var isNode = typeof require == "function" && typeof module == "object";
  38. var div = typeof document != "undefined" && document.createElement("div");
  39. var F = function () {};
  40. var buster = {
  41. bind: function bind(obj, methOrProp) {
  42. var method = typeof methOrProp == "string" ? obj[methOrProp] : methOrProp;
  43. var args = Array.prototype.slice.call(arguments, 2);
  44. return function () {
  45. var allArgs = args.concat(Array.prototype.slice.call(arguments));
  46. return method.apply(obj, allArgs);
  47. };
  48. },
  49. partial: function partial(fn) {
  50. var args = [].slice.call(arguments, 1);
  51. return function () {
  52. return fn.apply(this, args.concat([].slice.call(arguments)));
  53. };
  54. },
  55. create: function create(object) {
  56. F.prototype = object;
  57. return new F();
  58. },
  59. extend: function extend(target) {
  60. if (!target) { return; }
  61. for (var i = 1, l = arguments.length, prop; i < l; ++i) {
  62. for (prop in arguments[i]) {
  63. target[prop] = arguments[i][prop];
  64. }
  65. }
  66. return target;
  67. },
  68. nextTick: function nextTick(callback) {
  69. if (typeof process != "undefined" && process.nextTick) {
  70. return process.nextTick(callback);
  71. }
  72. setTimeout(callback, 0);
  73. },
  74. functionName: function functionName(func) {
  75. if (!func) return "";
  76. if (func.displayName) return func.displayName;
  77. if (func.name) return func.name;
  78. var matches = func.toString().match(/function\s+([^\(]+)/m);
  79. return matches && matches[1] || "";
  80. },
  81. isNode: function isNode(obj) {
  82. if (!div) return false;
  83. try {
  84. obj.appendChild(div);
  85. obj.removeChild(div);
  86. } catch (e) {
  87. return false;
  88. }
  89. return true;
  90. },
  91. isElement: function isElement(obj) {
  92. return obj && obj.nodeType === 1 && buster.isNode(obj);
  93. },
  94. isArray: function isArray(arr) {
  95. return Object.prototype.toString.call(arr) == "[object Array]";
  96. },
  97. flatten: function flatten(arr) {
  98. var result = [], arr = arr || [];
  99. for (var i = 0, l = arr.length; i < l; ++i) {
  100. result = result.concat(buster.isArray(arr[i]) ? flatten(arr[i]) : arr[i]);
  101. }
  102. return result;
  103. },
  104. each: function each(arr, callback) {
  105. for (var i = 0, l = arr.length; i < l; ++i) {
  106. callback(arr[i]);
  107. }
  108. },
  109. map: function map(arr, callback) {
  110. var results = [];
  111. for (var i = 0, l = arr.length; i < l; ++i) {
  112. results.push(callback(arr[i]));
  113. }
  114. return results;
  115. },
  116. parallel: function parallel(fns, callback) {
  117. function cb(err, res) {
  118. if (typeof callback == "function") {
  119. callback(err, res);
  120. callback = null;
  121. }
  122. }
  123. if (fns.length == 0) { return cb(null, []); }
  124. var remaining = fns.length, results = [];
  125. function makeDone(num) {
  126. return function done(err, result) {
  127. if (err) { return cb(err); }
  128. results[num] = result;
  129. if (--remaining == 0) { cb(null, results); }
  130. };
  131. }
  132. for (var i = 0, l = fns.length; i < l; ++i) {
  133. fns[i](makeDone(i));
  134. }
  135. },
  136. series: function series(fns, callback) {
  137. function cb(err, res) {
  138. if (typeof callback == "function") {
  139. callback(err, res);
  140. }
  141. }
  142. var remaining = fns.slice();
  143. var results = [];
  144. function callNext() {
  145. if (remaining.length == 0) return cb(null, results);
  146. var promise = remaining.shift()(next);
  147. if (promise && typeof promise.then == "function") {
  148. promise.then(buster.partial(next, null), next);
  149. }
  150. }
  151. function next(err, result) {
  152. if (err) return cb(err);
  153. results.push(result);
  154. callNext();
  155. }
  156. callNext();
  157. },
  158. countdown: function countdown(num, done) {
  159. return function () {
  160. if (--num == 0) done();
  161. };
  162. }
  163. };
  164. if (typeof process === "object" &&
  165. typeof require === "function" && typeof module === "object") {
  166. var crypto = require("crypto");
  167. var path = require("path");
  168. buster.tmpFile = function (fileName) {
  169. var hashed = crypto.createHash("sha1");
  170. hashed.update(fileName);
  171. var tmpfileName = hashed.digest("hex");
  172. if (process.platform == "win32") {
  173. return path.join(process.env["TEMP"], tmpfileName);
  174. } else {
  175. return path.join("/tmp", tmpfileName);
  176. }
  177. };
  178. }
  179. if (Array.prototype.some) {
  180. buster.some = function (arr, fn, thisp) {
  181. return arr.some(fn, thisp);
  182. };
  183. } else {
  184. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
  185. buster.some = function (arr, fun, thisp) {
  186. if (arr == null) { throw new TypeError(); }
  187. arr = Object(arr);
  188. var len = arr.length >>> 0;
  189. if (typeof fun !== "function") { throw new TypeError(); }
  190. for (var i = 0; i < len; i++) {
  191. if (arr.hasOwnProperty(i) && fun.call(thisp, arr[i], i, arr)) {
  192. return true;
  193. }
  194. }
  195. return false;
  196. };
  197. }
  198. if (Array.prototype.filter) {
  199. buster.filter = function (arr, fn, thisp) {
  200. return arr.filter(fn, thisp);
  201. };
  202. } else {
  203. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
  204. buster.filter = function (fn, thisp) {
  205. if (this == null) { throw new TypeError(); }
  206. var t = Object(this);
  207. var len = t.length >>> 0;
  208. if (typeof fn != "function") { throw new TypeError(); }
  209. var res = [];
  210. for (var i = 0; i < len; i++) {
  211. if (i in t) {
  212. var val = t[i]; // in case fun mutates this
  213. if (fn.call(thisp, val, i, t)) { res.push(val); }
  214. }
  215. }
  216. return res;
  217. };
  218. }
  219. if (isNode) {
  220. module.exports = buster;
  221. buster.eventEmitter = require("./buster-event-emitter");
  222. Object.defineProperty(buster, "defineVersionGetter", {
  223. get: function () {
  224. return require("./define-version-getter");
  225. }
  226. });
  227. }
  228. return buster.extend(B || {}, buster);
  229. }(setTimeout, buster));
  230. if (typeof buster === "undefined") {
  231. var buster = {};
  232. }
  233. if (typeof module === "object" && typeof require === "function") {
  234. buster = require("buster-core");
  235. }
  236. buster.format = buster.format || {};
  237. buster.format.excludeConstructors = ["Object", /^.$/];
  238. buster.format.quoteStrings = true;
  239. buster.format.ascii = (function () {
  240. var hasOwn = Object.prototype.hasOwnProperty;
  241. var specialObjects = [];
  242. if (typeof global != "undefined") {
  243. specialObjects.push({ obj: global, value: "[object global]" });
  244. }
  245. if (typeof document != "undefined") {
  246. specialObjects.push({ obj: document, value: "[object HTMLDocument]" });
  247. }
  248. if (typeof window != "undefined") {
  249. specialObjects.push({ obj: window, value: "[object Window]" });
  250. }
  251. function keys(object) {
  252. var k = Object.keys && Object.keys(object) || [];
  253. if (k.length == 0) {
  254. for (var prop in object) {
  255. if (hasOwn.call(object, prop)) {
  256. k.push(prop);
  257. }
  258. }
  259. }
  260. return k.sort();
  261. }
  262. function isCircular(object, objects) {
  263. if (typeof object != "object") {
  264. return false;
  265. }
  266. for (var i = 0, l = objects.length; i < l; ++i) {
  267. if (objects[i] === object) {
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. function ascii(object, processed, indent) {
  274. if (typeof object == "string") {
  275. var quote = typeof this.quoteStrings != "boolean" || this.quoteStrings;
  276. return processed || quote ? '"' + object + '"' : object;
  277. }
  278. if (typeof object == "function" && !(object instanceof RegExp)) {
  279. return ascii.func(object);
  280. }
  281. processed = processed || [];
  282. if (isCircular(object, processed)) {
  283. return "[Circular]";
  284. }
  285. if (Object.prototype.toString.call(object) == "[object Array]") {
  286. return ascii.array.call(this, object, processed);
  287. }
  288. if (!object) {
  289. return "" + object;
  290. }
  291. if (buster.isElement(object)) {
  292. return ascii.element(object);
  293. }
  294. if (typeof object.toString == "function" &&
  295. object.toString !== Object.prototype.toString) {
  296. return object.toString();
  297. }
  298. for (var i = 0, l = specialObjects.length; i < l; i++) {
  299. if (object === specialObjects[i].obj) {
  300. return specialObjects[i].value;
  301. }
  302. }
  303. return ascii.object.call(this, object, processed, indent);
  304. }
  305. ascii.func = function (func) {
  306. return "function " + buster.functionName(func) + "() {}";
  307. };
  308. ascii.array = function (array, processed) {
  309. processed = processed || [];
  310. processed.push(array);
  311. var pieces = [];
  312. for (var i = 0, l = array.length; i < l; ++i) {
  313. pieces.push(ascii.call(this, array[i], processed));
  314. }
  315. return "[" + pieces.join(", ") + "]";
  316. };
  317. ascii.object = function (object, processed, indent) {
  318. processed = processed || [];
  319. processed.push(object);
  320. indent = indent || 0;
  321. var pieces = [], properties = keys(object), prop, str, obj;
  322. var is = "";
  323. var length = 3;
  324. for (var i = 0, l = indent; i < l; ++i) {
  325. is += " ";
  326. }
  327. for (i = 0, l = properties.length; i < l; ++i) {
  328. prop = properties[i];
  329. obj = object[prop];
  330. if (isCircular(obj, processed)) {
  331. str = "[Circular]";
  332. } else {
  333. str = ascii.call(this, obj, processed, indent + 2);
  334. }
  335. str = (/\s/.test(prop) ? '"' + prop + '"' : prop) + ": " + str;
  336. length += str.length;
  337. pieces.push(str);
  338. }
  339. var cons = ascii.constructorName.call(this, object);
  340. var prefix = cons ? "[" + cons + "] " : ""
  341. return (length + indent) > 80 ?
  342. prefix + "{\n " + is + pieces.join(",\n " + is) + "\n" + is + "}" :
  343. prefix + "{ " + pieces.join(", ") + " }";
  344. };
  345. ascii.element = function (element) {
  346. var tagName = element.tagName.toLowerCase();
  347. var attrs = element.attributes, attribute, pairs = [], attrName;
  348. for (var i = 0, l = attrs.length; i < l; ++i) {
  349. attribute = attrs.item(i);
  350. attrName = attribute.nodeName.toLowerCase().replace("html:", "");
  351. if (attrName == "contenteditable" && attribute.nodeValue == "inherit") {
  352. continue;
  353. }
  354. if (!!attribute.nodeValue) {
  355. pairs.push(attrName + "=\"" + attribute.nodeValue + "\"");
  356. }
  357. }
  358. var formatted = "<" + tagName + (pairs.length > 0 ? " " : "");
  359. var content = element.innerHTML;
  360. if (content.length > 20) {
  361. content = content.substr(0, 20) + "[...]";
  362. }
  363. var res = formatted + pairs.join(" ") + ">" + content + "</" + tagName + ">";
  364. return res.replace(/ contentEditable="inherit"/, "");
  365. };
  366. ascii.constructorName = function (object) {
  367. var name = buster.functionName(object && object.constructor);
  368. var excludes = this.excludeConstructors || buster.format.excludeConstructors || [];
  369. for (var i = 0, l = excludes.length; i < l; ++i) {
  370. if (typeof excludes[i] == "string" && excludes[i] == name) {
  371. return "";
  372. } else if (excludes[i].test && excludes[i].test(name)) {
  373. return "";
  374. }
  375. }
  376. return name;
  377. };
  378. return ascii;
  379. }());
  380. if (typeof module != "undefined") {
  381. module.exports = buster.format;
  382. }
  383. /*jslint eqeqeq: false, onevar: false, forin: true, nomen: false, regexp: false, plusplus: false*/
  384. /*global module, require, __dirname, document*/
  385. /**
  386. * Sinon core utilities. For internal use only.
  387. *
  388. * @author Christian Johansen (christian@cjohansen.no)
  389. * @license BSD
  390. *
  391. * Copyright (c) 2010-2013 Christian Johansen
  392. */
  393. var sinon = (function (buster) {
  394. var div = typeof document != "undefined" && document.createElement("div");
  395. var hasOwn = Object.prototype.hasOwnProperty;
  396. function isDOMNode(obj) {
  397. var success = false;
  398. try {
  399. obj.appendChild(div);
  400. success = div.parentNode == obj;
  401. } catch (e) {
  402. return false;
  403. } finally {
  404. try {
  405. obj.removeChild(div);
  406. } catch (e) {
  407. // Remove failed, not much we can do about that
  408. }
  409. }
  410. return success;
  411. }
  412. function isElement(obj) {
  413. return div && obj && obj.nodeType === 1 && isDOMNode(obj);
  414. }
  415. function isFunction(obj) {
  416. return typeof obj === "function" || !!(obj && obj.constructor && obj.call && obj.apply);
  417. }
  418. function mirrorProperties(target, source) {
  419. for (var prop in source) {
  420. if (!hasOwn.call(target, prop)) {
  421. target[prop] = source[prop];
  422. }
  423. }
  424. }
  425. function isRestorable (obj) {
  426. return typeof obj === "function" && typeof obj.restore === "function" && obj.restore.sinon;
  427. }
  428. var sinon = {
  429. wrapMethod: function wrapMethod(object, property, method) {
  430. if (!object) {
  431. throw new TypeError("Should wrap property of object");
  432. }
  433. if (typeof method != "function") {
  434. throw new TypeError("Method wrapper should be function");
  435. }
  436. var wrappedMethod = object[property];
  437. if (!isFunction(wrappedMethod)) {
  438. throw new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
  439. property + " as function");
  440. }
  441. if (wrappedMethod.restore && wrappedMethod.restore.sinon) {
  442. throw new TypeError("Attempted to wrap " + property + " which is already wrapped");
  443. }
  444. if (wrappedMethod.calledBefore) {
  445. var verb = !!wrappedMethod.returns ? "stubbed" : "spied on";
  446. throw new TypeError("Attempted to wrap " + property + " which is already " + verb);
  447. }
  448. // IE 8 does not support hasOwnProperty on the window object.
  449. var owned = hasOwn.call(object, property);
  450. object[property] = method;
  451. method.displayName = property;
  452. method.restore = function () {
  453. // For prototype properties try to reset by delete first.
  454. // If this fails (ex: localStorage on mobile safari) then force a reset
  455. // via direct assignment.
  456. if (!owned) {
  457. delete object[property];
  458. }
  459. if (object[property] === method) {
  460. object[property] = wrappedMethod;
  461. }
  462. };
  463. method.restore.sinon = true;
  464. mirrorProperties(method, wrappedMethod);
  465. return method;
  466. },
  467. extend: function extend(target) {
  468. for (var i = 1, l = arguments.length; i < l; i += 1) {
  469. for (var prop in arguments[i]) {
  470. if (arguments[i].hasOwnProperty(prop)) {
  471. target[prop] = arguments[i][prop];
  472. }
  473. // DONT ENUM bug, only care about toString
  474. if (arguments[i].hasOwnProperty("toString") &&
  475. arguments[i].toString != target.toString) {
  476. target.toString = arguments[i].toString;
  477. }
  478. }
  479. }
  480. return target;
  481. },
  482. create: function create(proto) {
  483. var F = function () {};
  484. F.prototype = proto;
  485. return new F();
  486. },
  487. deepEqual: function deepEqual(a, b) {
  488. if (sinon.match && sinon.match.isMatcher(a)) {
  489. return a.test(b);
  490. }
  491. if (typeof a != "object" || typeof b != "object") {
  492. return a === b;
  493. }
  494. if (isElement(a) || isElement(b)) {
  495. return a === b;
  496. }
  497. if (a === b) {
  498. return true;
  499. }
  500. if ((a === null && b !== null) || (a !== null && b === null)) {
  501. return false;
  502. }
  503. var aString = Object.prototype.toString.call(a);
  504. if (aString != Object.prototype.toString.call(b)) {
  505. return false;
  506. }
  507. if (aString == "[object Array]") {
  508. if (a.length !== b.length) {
  509. return false;
  510. }
  511. for (var i = 0, l = a.length; i < l; i += 1) {
  512. if (!deepEqual(a[i], b[i])) {
  513. return false;
  514. }
  515. }
  516. return true;
  517. }
  518. var prop, aLength = 0, bLength = 0;
  519. for (prop in a) {
  520. aLength += 1;
  521. if (!deepEqual(a[prop], b[prop])) {
  522. return false;
  523. }
  524. }
  525. for (prop in b) {
  526. bLength += 1;
  527. }
  528. return aLength == bLength;
  529. },
  530. functionName: function functionName(func) {
  531. var name = func.displayName || func.name;
  532. // Use function decomposition as a last resort to get function
  533. // name. Does not rely on function decomposition to work - if it
  534. // doesn't debugging will be slightly less informative
  535. // (i.e. toString will say 'spy' rather than 'myFunc').
  536. if (!name) {
  537. var matches = func.toString().match(/function ([^\s\(]+)/);
  538. name = matches && matches[1];
  539. }
  540. return name;
  541. },
  542. functionToString: function toString() {
  543. if (this.getCall && this.callCount) {
  544. var thisValue, prop, i = this.callCount;
  545. while (i--) {
  546. thisValue = this.getCall(i).thisValue;
  547. for (prop in thisValue) {
  548. if (thisValue[prop] === this) {
  549. return prop;
  550. }
  551. }
  552. }
  553. }
  554. return this.displayName || "sinon fake";
  555. },
  556. getConfig: function (custom) {
  557. var config = {};
  558. custom = custom || {};
  559. var defaults = sinon.defaultConfig;
  560. for (var prop in defaults) {
  561. if (defaults.hasOwnProperty(prop)) {
  562. config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaults[prop];
  563. }
  564. }
  565. return config;
  566. },
  567. format: function (val) {
  568. return "" + val;
  569. },
  570. defaultConfig: {
  571. injectIntoThis: true,
  572. injectInto: null,
  573. properties: ["spy", "stub", "mock", "clock", "server", "requests"],
  574. useFakeTimers: true,
  575. useFakeServer: true
  576. },
  577. timesInWords: function timesInWords(count) {
  578. return count == 1 && "once" ||
  579. count == 2 && "twice" ||
  580. count == 3 && "thrice" ||
  581. (count || 0) + " times";
  582. },
  583. calledInOrder: function (spies) {
  584. for (var i = 1, l = spies.length; i < l; i++) {
  585. if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) {
  586. return false;
  587. }
  588. }
  589. return true;
  590. },
  591. orderByFirstCall: function (spies) {
  592. return spies.sort(function (a, b) {
  593. // uuid, won't ever be equal
  594. var aCall = a.getCall(0);
  595. var bCall = b.getCall(0);
  596. var aId = aCall && aCall.callId || -1;
  597. var bId = bCall && bCall.callId || -1;
  598. return aId < bId ? -1 : 1;
  599. });
  600. },
  601. log: function () {},
  602. logError: function (label, err) {
  603. var msg = label + " threw exception: "
  604. sinon.log(msg + "[" + err.name + "] " + err.message);
  605. if (err.stack) { sinon.log(err.stack); }
  606. setTimeout(function () {
  607. err.message = msg + err.message;
  608. throw err;
  609. }, 0);
  610. },
  611. typeOf: function (value) {
  612. if (value === null) {
  613. return "null";
  614. }
  615. else if (value === undefined) {
  616. return "undefined";
  617. }
  618. var string = Object.prototype.toString.call(value);
  619. return string.substring(8, string.length - 1).toLowerCase();
  620. },
  621. createStubInstance: function (constructor) {
  622. if (typeof constructor !== "function") {
  623. throw new TypeError("The constructor should be a function.");
  624. }
  625. return sinon.stub(sinon.create(constructor.prototype));
  626. },
  627. restore: function (object) {
  628. if (object !== null && typeof object === "object") {
  629. for (var prop in object) {
  630. if (isRestorable(object[prop])) {
  631. object[prop].restore();
  632. }
  633. }
  634. }
  635. else if (isRestorable(object)) {
  636. object.restore();
  637. }
  638. }
  639. };
  640. var isNode = typeof module == "object" && typeof require == "function";
  641. if (isNode) {
  642. try {
  643. buster = { format: require("buster-format") };
  644. } catch (e) {}
  645. module.exports = sinon;
  646. module.exports.spy = require("./sinon/spy");
  647. module.exports.spyCall = require("./sinon/call");
  648. module.exports.stub = require("./sinon/stub");
  649. module.exports.mock = require("./sinon/mock");
  650. module.exports.collection = require("./sinon/collection");
  651. module.exports.assert = require("./sinon/assert");
  652. module.exports.sandbox = require("./sinon/sandbox");
  653. module.exports.test = require("./sinon/test");
  654. module.exports.testCase = require("./sinon/test_case");
  655. module.exports.assert = require("./sinon/assert");
  656. module.exports.match = require("./sinon/match");
  657. }
  658. if (buster) {
  659. var formatter = sinon.create(buster.format);
  660. formatter.quoteStrings = false;
  661. sinon.format = function () {
  662. return formatter.ascii.apply(formatter, arguments);
  663. };
  664. } else if (isNode) {
  665. try {
  666. var util = require("util");
  667. sinon.format = function (value) {
  668. return typeof value == "object" && value.toString === Object.prototype.toString ? util.inspect(value) : value;
  669. };
  670. } catch (e) {
  671. /* Node, but no util module - would be very old, but better safe than
  672. sorry */
  673. }
  674. }
  675. return sinon;
  676. }(typeof buster == "object" && buster));
  677. /* @depend ../sinon.js */
  678. /*jslint eqeqeq: false, onevar: false, plusplus: false*/
  679. /*global module, require, sinon*/
  680. /**
  681. * Match functions
  682. *
  683. * @author Maximilian Antoni (mail@maxantoni.de)
  684. * @license BSD
  685. *
  686. * Copyright (c) 2012 Maximilian Antoni
  687. */
  688. (function (sinon) {
  689. var commonJSModule = typeof module == "object" && typeof require == "function";
  690. if (!sinon && commonJSModule) {
  691. sinon = require("../sinon");
  692. }
  693. if (!sinon) {
  694. return;
  695. }
  696. function assertType(value, type, name) {
  697. var actual = sinon.typeOf(value);
  698. if (actual !== type) {
  699. throw new TypeError("Expected type of " + name + " to be " +
  700. type + ", but was " + actual);
  701. }
  702. }
  703. var matcher = {
  704. toString: function () {
  705. return this.message;
  706. }
  707. };
  708. function isMatcher(object) {
  709. return matcher.isPrototypeOf(object);
  710. }
  711. function matchObject(expectation, actual) {
  712. if (actual === null || actual === undefined) {
  713. return false;
  714. }
  715. for (var key in expectation) {
  716. if (expectation.hasOwnProperty(key)) {
  717. var exp = expectation[key];
  718. var act = actual[key];
  719. if (match.isMatcher(exp)) {
  720. if (!exp.test(act)) {
  721. return false;
  722. }
  723. } else if (sinon.typeOf(exp) === "object") {
  724. if (!matchObject(exp, act)) {
  725. return false;
  726. }
  727. } else if (!sinon.deepEqual(exp, act)) {
  728. return false;
  729. }
  730. }
  731. }
  732. return true;
  733. }
  734. matcher.or = function (m2) {
  735. if (!isMatcher(m2)) {
  736. throw new TypeError("Matcher expected");
  737. }
  738. var m1 = this;
  739. var or = sinon.create(matcher);
  740. or.test = function (actual) {
  741. return m1.test(actual) || m2.test(actual);
  742. };
  743. or.message = m1.message + ".or(" + m2.message + ")";
  744. return or;
  745. };
  746. matcher.and = function (m2) {
  747. if (!isMatcher(m2)) {
  748. throw new TypeError("Matcher expected");
  749. }
  750. var m1 = this;
  751. var and = sinon.create(matcher);
  752. and.test = function (actual) {
  753. return m1.test(actual) && m2.test(actual);
  754. };
  755. and.message = m1.message + ".and(" + m2.message + ")";
  756. return and;
  757. };
  758. var match = function (expectation, message) {
  759. var m = sinon.create(matcher);
  760. var type = sinon.typeOf(expectation);
  761. switch (type) {
  762. case "object":
  763. if (typeof expectation.test === "function") {
  764. m.test = function (actual) {
  765. return expectation.test(actual) === true;
  766. };
  767. m.message = "match(" + sinon.functionName(expectation.test) + ")";
  768. return m;
  769. }
  770. var str = [];
  771. for (var key in expectation) {
  772. if (expectation.hasOwnProperty(key)) {
  773. str.push(key + ": " + expectation[key]);
  774. }
  775. }
  776. m.test = function (actual) {
  777. return matchObject(expectation, actual);
  778. };
  779. m.message = "match(" + str.join(", ") + ")";
  780. break;
  781. case "number":
  782. m.test = function (actual) {
  783. return expectation == actual;
  784. };
  785. break;
  786. case "string":
  787. m.test = function (actual) {
  788. if (typeof actual !== "string") {
  789. return false;
  790. }
  791. return actual.indexOf(expectation) !== -1;
  792. };
  793. m.message = "match(\"" + expectation + "\")";
  794. break;
  795. case "regexp":
  796. m.test = function (actual) {
  797. if (typeof actual !== "string") {
  798. return false;
  799. }
  800. return expectation.test(actual);
  801. };
  802. break;
  803. case "function":
  804. m.test = expectation;
  805. if (message) {
  806. m.message = message;
  807. } else {
  808. m.message = "match(" + sinon.functionName(expectation) + ")";
  809. }
  810. break;
  811. default:
  812. m.test = function (actual) {
  813. return sinon.deepEqual(expectation, actual);
  814. };
  815. }
  816. if (!m.message) {
  817. m.message = "match(" + expectation + ")";
  818. }
  819. return m;
  820. };
  821. match.isMatcher = isMatcher;
  822. match.any = match(function () {
  823. return true;
  824. }, "any");
  825. match.defined = match(function (actual) {
  826. return actual !== null && actual !== undefined;
  827. }, "defined");
  828. match.truthy = match(function (actual) {
  829. return !!actual;
  830. }, "truthy");
  831. match.falsy = match(function (actual) {
  832. return !actual;
  833. }, "falsy");
  834. match.same = function (expectation) {
  835. return match(function (actual) {
  836. return expectation === actual;
  837. }, "same(" + expectation + ")");
  838. };
  839. match.typeOf = function (type) {
  840. assertType(type, "string", "type");
  841. return match(function (actual) {
  842. return sinon.typeOf(actual) === type;
  843. }, "typeOf(\"" + type + "\")");
  844. };
  845. match.instanceOf = function (type) {
  846. assertType(type, "function", "type");
  847. return match(function (actual) {
  848. return actual instanceof type;
  849. }, "instanceOf(" + sinon.functionName(type) + ")");
  850. };
  851. function createPropertyMatcher(propertyTest, messagePrefix) {
  852. return function (property, value) {
  853. assertType(property, "string", "property");
  854. var onlyProperty = arguments.length === 1;
  855. var message = messagePrefix + "(\"" + property + "\"";
  856. if (!onlyProperty) {
  857. message += ", " + value;
  858. }
  859. message += ")";
  860. return match(function (actual) {
  861. if (actual === undefined || actual === null ||
  862. !propertyTest(actual, property)) {
  863. return false;
  864. }
  865. return onlyProperty || sinon.deepEqual(value, actual[property]);
  866. }, message);
  867. };
  868. }
  869. match.has = createPropertyMatcher(function (actual, property) {
  870. if (typeof actual === "object") {
  871. return property in actual;
  872. }
  873. return actual[property] !== undefined;
  874. }, "has");
  875. match.hasOwn = createPropertyMatcher(function (actual, property) {
  876. return actual.hasOwnProperty(property);
  877. }, "hasOwn");
  878. match.bool = match.typeOf("boolean");
  879. match.number = match.typeOf("number");
  880. match.string = match.typeOf("string");
  881. match.object = match.typeOf("object");
  882. match.func = match.typeOf("function");
  883. match.array = match.typeOf("array");
  884. match.regexp = match.typeOf("regexp");
  885. match.date = match.typeOf("date");
  886. if (commonJSModule) {
  887. module.exports = match;
  888. } else {
  889. sinon.match = match;
  890. }
  891. }(typeof sinon == "object" && sinon || null));
  892. /**
  893. * @depend ../sinon.js
  894. * @depend match.js
  895. */
  896. /*jslint eqeqeq: false, onevar: false, plusplus: false*/
  897. /*global module, require, sinon*/
  898. /**
  899. * Spy calls
  900. *
  901. * @author Christian Johansen (christian@cjohansen.no)
  902. * @author Maximilian Antoni (mail@maxantoni.de)
  903. * @license BSD
  904. *
  905. * Copyright (c) 2010-2013 Christian Johansen
  906. * Copyright (c) 2013 Maximilian Antoni
  907. */
  908. (function (sinon) {
  909. var commonJSModule = typeof module == "object" && typeof require == "function";
  910. if (!sinon && commonJSModule) {
  911. sinon = require("../sinon");
  912. }
  913. if (!sinon) {
  914. return;
  915. }
  916. function throwYieldError(proxy, text, args) {
  917. var msg = sinon.functionName(proxy) + text;
  918. if (args.length) {
  919. msg += " Received [" + slice.call(args).join(", ") + "]";
  920. }
  921. throw new Error(msg);
  922. }
  923. var slice = Array.prototype.slice;
  924. var callProto = {
  925. calledOn: function calledOn(thisValue) {
  926. if (sinon.match && sinon.match.isMatcher(thisValue)) {
  927. return thisValue.test(this.thisValue);
  928. }
  929. return this.thisValue === thisValue;
  930. },
  931. calledWith: function calledWith() {
  932. for (var i = 0, l = arguments.length; i < l; i += 1) {
  933. if (!sinon.deepEqual(arguments[i], this.args[i])) {
  934. return false;
  935. }
  936. }
  937. return true;
  938. },
  939. calledWithMatch: function calledWithMatch() {
  940. for (var i = 0, l = arguments.length; i < l; i += 1) {
  941. var actual = this.args[i];
  942. var expectation = arguments[i];
  943. if (!sinon.match || !sinon.match(expectation).test(actual)) {
  944. return false;
  945. }
  946. }
  947. return true;
  948. },
  949. calledWithExactly: function calledWithExactly() {
  950. return arguments.length == this.args.length &&
  951. this.calledWith.apply(this, arguments);
  952. },
  953. notCalledWith: function notCalledWith() {
  954. return !this.calledWith.apply(this, arguments);
  955. },
  956. notCalledWithMatch: function notCalledWithMatch() {
  957. return !this.calledWithMatch.apply(this, arguments);
  958. },
  959. returned: function returned(value) {
  960. return sinon.deepEqual(value, this.returnValue);
  961. },
  962. threw: function threw(error) {
  963. if (typeof error === "undefined" || !this.exception) {
  964. return !!this.exception;
  965. }
  966. return this.exception === error || this.exception.name === error;
  967. },
  968. calledWithNew: function calledWithNew(thisValue) {
  969. return this.thisValue instanceof this.proxy;
  970. },
  971. calledBefore: function (other) {
  972. return this.callId < other.callId;
  973. },
  974. calledAfter: function (other) {
  975. return this.callId > other.callId;
  976. },
  977. callArg: function (pos) {
  978. this.args[pos]();
  979. },
  980. callArgOn: function (pos, thisValue) {
  981. this.args[pos].apply(thisValue);
  982. },
  983. callArgWith: function (pos) {
  984. this.callArgOnWith.apply(this, [pos, null].concat(slice.call(arguments, 1)));
  985. },
  986. callArgOnWith: function (pos, thisValue) {
  987. var args = slice.call(arguments, 2);
  988. this.args[pos].apply(thisValue, args);
  989. },
  990. "yield": function () {
  991. this.yieldOn.apply(this, [null].concat(slice.call(arguments, 0)));
  992. },
  993. yieldOn: function (thisValue) {
  994. var args = this.args;
  995. for (var i = 0, l = args.length; i < l; ++i) {
  996. if (typeof args[i] === "function") {
  997. args[i].apply(thisValue, slice.call(arguments, 1));
  998. return;
  999. }
  1000. }
  1001. throwYieldError(this.proxy, " cannot yield since no callback was passed.", args);
  1002. },
  1003. yieldTo: function (prop) {
  1004. this.yieldToOn.apply(this, [prop, null].concat(slice.call(arguments, 1)));
  1005. },
  1006. yieldToOn: function (prop, thisValue) {
  1007. var args = this.args;
  1008. for (var i = 0, l = args.length; i < l; ++i) {
  1009. if (args[i] && typeof args[i][prop] === "function") {
  1010. args[i][prop].apply(thisValue, slice.call(arguments, 2));
  1011. return;
  1012. }
  1013. }
  1014. throwYieldError(this.proxy, " cannot yield to '" + prop +
  1015. "' since no callback was passed.", args);
  1016. },
  1017. toString: function () {
  1018. var callStr = this.proxy.toString() + "(";
  1019. var args = [];
  1020. for (var i = 0, l = this.args.length; i < l; ++i) {
  1021. args.push(sinon.format(this.args[i]));
  1022. }
  1023. callStr = callStr + args.join(", ") + ")";
  1024. if (typeof this.returnValue != "undefined") {
  1025. callStr += " => " + sinon.format(this.returnValue);
  1026. }
  1027. if (this.exception) {
  1028. callStr += " !" + this.exception.name;
  1029. if (this.exception.message) {
  1030. callStr += "(" + this.exception.message + ")";
  1031. }
  1032. }
  1033. return callStr;
  1034. }
  1035. };
  1036. callProto.invokeCallback = callProto.yield;
  1037. function createSpyCall(spy, thisValue, args, returnValue, exception, id) {
  1038. if (typeof id !== "number") {
  1039. throw new TypeError("Call id is not a number");
  1040. }
  1041. var proxyCall = sinon.create(callProto);
  1042. proxyCall.proxy = spy;
  1043. proxyCall.thisValue = thisValue;
  1044. proxyCall.args = args;
  1045. proxyCall.returnValue = returnValue;
  1046. proxyCall.exception = exception;
  1047. proxyCall.callId = id;
  1048. return proxyCall;
  1049. };
  1050. createSpyCall.toString = callProto.toString; // used by mocks
  1051. if (commonJSModule) {
  1052. module.exports = createSpyCall;
  1053. } else {
  1054. sinon.spyCall = createSpyCall;
  1055. }
  1056. }(typeof sinon == "object" && sinon || null));
  1057. /**
  1058. * @depend ../sinon.js
  1059. * @depend call.js
  1060. */
  1061. /*jslint eqeqeq: false, onevar: false, plusplus: false*/
  1062. /*global module, require, sinon*/
  1063. /**
  1064. * Spy functions
  1065. *
  1066. * @author Christian Johansen (christian@cjohansen.no)
  1067. * @license BSD
  1068. *
  1069. * Copyright (c) 2010-2013 Christian Johansen
  1070. */
  1071. (function (sinon) {
  1072. var commonJSModule = typeof module == "object" && typeof require == "function";
  1073. var push = Array.prototype.push;
  1074. var slice = Array.prototype.slice;
  1075. var callId = 0;
  1076. if (!sinon && commonJSModule) {
  1077. sinon = require("../sinon");
  1078. }
  1079. if (!sinon) {
  1080. return;
  1081. }
  1082. function spy(object, property) {
  1083. if (!property && typeof object == "function") {
  1084. return spy.create(object);
  1085. }
  1086. if (!object && !property) {
  1087. return spy.create(function () { });
  1088. }
  1089. var method = object[property];
  1090. return sinon.wrapMethod(object, property, spy.create(method));
  1091. }
  1092. function matchingFake(fakes, args, strict) {
  1093. if (!fakes) {
  1094. return;
  1095. }
  1096. var alen = args.length;
  1097. for (var i = 0, l = fakes.length; i < l; i++) {
  1098. if (fakes[i].matches(args, strict)) {
  1099. return fakes[i];
  1100. }
  1101. }
  1102. }
  1103. function incrementCallCount() {
  1104. this.called = true;
  1105. this.callCount += 1;
  1106. this.notCalled = false;
  1107. this.calledOnce = this.callCount == 1;
  1108. this.calledTwice = this.callCount == 2;
  1109. this.calledThrice = this.callCount == 3;
  1110. }
  1111. function createCallProperties() {
  1112. this.firstCall = this.getCall(0);
  1113. this.secondCall = this.getCall(1);
  1114. this.thirdCall = this.getCall(2);
  1115. this.lastCall = this.getCall(this.callCount - 1);
  1116. }
  1117. var vars = "a,b,c,d,e,f,g,h,i,j,k,l";
  1118. function createProxy(func) {
  1119. // Retain the function length:
  1120. var p;
  1121. if (func.length) {
  1122. eval("p = (function proxy(" + vars.substring(0, func.length * 2 - 1) +
  1123. ") { return p.invoke(func, this, slice.call(arguments)); });");
  1124. }
  1125. else {
  1126. p = function proxy() {
  1127. return p.invoke(func, this, slice.call(arguments));
  1128. };
  1129. }
  1130. return p;
  1131. }
  1132. var uuid = 0;
  1133. // Public API
  1134. var spyApi = {
  1135. reset: function () {
  1136. this.called = false;
  1137. this.notCalled = true;
  1138. this.calledOnce = false;
  1139. this.calledTwice = false;
  1140. this.calledThrice = false;
  1141. this.callCount = 0;
  1142. this.firstCall = null;
  1143. this.secondCall = null;
  1144. this.thirdCall = null;
  1145. this.lastCall = null;
  1146. this.args = [];
  1147. this.returnValues = [];
  1148. this.thisValues = [];
  1149. this.exceptions = [];
  1150. this.callIds = [];
  1151. if (this.fakes) {
  1152. for (var i = 0; i < this.fakes.length; i++) {
  1153. this.fakes[i].reset();
  1154. }
  1155. }
  1156. },
  1157. create: function create(func) {
  1158. var name;
  1159. if (typeof func != "function") {
  1160. func = function () { };
  1161. } else {
  1162. name = sinon.functionName(func);
  1163. }
  1164. var proxy = createProxy(func);
  1165. sinon.extend(proxy, spy);
  1166. delete proxy.create;
  1167. sinon.extend(proxy, func);
  1168. proxy.reset();
  1169. proxy.prototype = func.prototype;
  1170. proxy.displayName = name || "spy";
  1171. proxy.toString = sinon.functionToString;
  1172. proxy._create = sinon.spy.create;
  1173. proxy.id = "spy#" + uuid++;
  1174. return proxy;
  1175. },
  1176. invoke: function invoke(func, thisValue, args) {
  1177. var matching = matchingFake(this.fakes, args);
  1178. var exception, returnValue;
  1179. incrementCallCount.call(this);
  1180. push.call(this.thisValues, thisValue);
  1181. push.call(this.args, args);
  1182. push.call(this.callIds, callId++);
  1183. try {
  1184. if (matching) {
  1185. returnValue = matching.invoke(func, thisValue, args);
  1186. } else {
  1187. returnValue = (this.func || func).apply(thisValue, args);
  1188. }
  1189. } catch (e) {
  1190. push.call(this.returnValues, undefined);
  1191. exception = e;
  1192. throw e;
  1193. } finally {
  1194. push.call(this.exceptions, exception);
  1195. }
  1196. push.call(this.returnValues, returnValue);
  1197. createCallProperties.call(this);
  1198. return returnValue;
  1199. },
  1200. getCall: function getCall(i) {
  1201. if (i < 0 || i >= this.callCount) {
  1202. return null;
  1203. }
  1204. return sinon.spyCall(this, this.thisValues[i], this.args[i],
  1205. this.returnValues[i], this.exceptions[i],
  1206. this.callIds[i]);
  1207. },
  1208. calledBefore: function calledBefore(spyFn) {
  1209. if (!this.called) {
  1210. return false;
  1211. }
  1212. if (!spyFn.called) {
  1213. return true;
  1214. }
  1215. return this.callIds[0] < spyFn.callIds[spyFn.callIds.length - 1];
  1216. },
  1217. calledAfter: function calledAfter(spyFn) {
  1218. if (!this.called || !spyFn.called) {
  1219. return false;
  1220. }
  1221. return this.callIds[this.callCount - 1] > spyFn.callIds[spyFn.callCount - 1];
  1222. },
  1223. withArgs: function () {
  1224. var args = slice.call(arguments);
  1225. if (this.fakes) {
  1226. var match = matchingFake(this.fakes, args, true);
  1227. if (match) {
  1228. return match;
  1229. }
  1230. } else {
  1231. this.fakes = [];
  1232. }
  1233. var original = this;
  1234. var fake = this._create();
  1235. fake.matchingAguments = args;
  1236. push.call(this.fakes, fake);
  1237. fake.withArgs = function () {
  1238. return original.withArgs.apply(original, arguments);
  1239. };
  1240. for (var i = 0; i < this.args.length; i++) {
  1241. if (fake.matches(this.args[i])) {
  1242. incrementCallCount.call(fake);
  1243. push.call(fake.thisValues, this.thisValues[i]);
  1244. push.call(fake.args, this.args[i]);
  1245. push.call(fake.returnValues, this.returnValues[i]);
  1246. push.call(fake.exceptions, this.exceptions[i]);
  1247. push.call(fake.callIds, this.callIds[i]);
  1248. }
  1249. }
  1250. createCallProperties.call(fake);
  1251. return fake;
  1252. },
  1253. matches: function (args, strict) {
  1254. var margs = this.matchingAguments;
  1255. if (margs.length <= args.length &&
  1256. sinon.deepEqual(margs, args.slice(0, margs.length))) {
  1257. return !strict || margs.length == args.length;
  1258. }
  1259. },
  1260. printf: function (format) {
  1261. var spy = this;
  1262. var args = slice.call(arguments, 1);
  1263. var formatter;
  1264. return (format || "").replace(/%(.)/g, function (match, specifyer) {
  1265. formatter = spyApi.formatters[specifyer];
  1266. if (typeof formatter == "function") {
  1267. return formatter.call(null, spy, args);
  1268. } else if (!isNaN(parseInt(specifyer), 10)) {
  1269. return sinon.format(args[specifyer - 1]);
  1270. }
  1271. return "%" + specifyer;
  1272. });
  1273. }
  1274. };
  1275. function delegateToCalls(method, matchAny, actual, notCalled) {
  1276. spyApi[method] = function () {
  1277. if (!this.called) {
  1278. if (notCalled) {
  1279. return notCalled.apply(this, arguments);
  1280. }
  1281. return false;
  1282. }
  1283. var currentCall;
  1284. var matches = 0;
  1285. for (var i = 0, l = this.callCount; i < l; i += 1) {
  1286. currentCall = this.getCall(i);
  1287. if (currentCall[actual || method].apply(currentCall, arguments)) {
  1288. matches += 1;
  1289. if (matchAny) {
  1290. return true;
  1291. }
  1292. }
  1293. }
  1294. return matches === this.callCount;
  1295. };
  1296. }
  1297. delegateToCalls("calledOn", true);
  1298. delegateToCalls("alwaysCalledOn", false, "calledOn");
  1299. delegateToCalls("calledWith", true);
  1300. delegateToCalls("calledWithMatch", true);
  1301. delegateToCalls("alwaysCalledWith", false, "calledWith");
  1302. delegateToCalls("alwaysCalledWithMatch", false, "calledWithMatch");
  1303. delegateToCalls("calledWithExactly", true);
  1304. delegateToCalls("alwaysCalledWithExactly", false, "calledWithExactly");
  1305. delegateToCalls("neverCalledWith", false, "notCalledWith",
  1306. function () { return true; });
  1307. delegateToCalls("neverCalledWithMatch", false, "notCalledWithMatch",
  1308. function () { return true; });
  1309. delegateToCalls("threw", true);
  1310. delegateToCalls("alwaysThrew", false, "threw");
  1311. delegateToCalls("returned", true);
  1312. delegateToCalls("alwaysReturned", false, "returned");
  1313. delegateToCalls("calledWithNew", true);
  1314. delegateToCalls("alwaysCalledWithNew", false, "calledWithNew");
  1315. delegateToCalls("callArg", false, "callArgWith", function () {
  1316. throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
  1317. });
  1318. spyApi.callArgWith = spyApi.callArg;
  1319. delegateToCalls("callArgOn", false, "callArgOnWith", function () {
  1320. throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
  1321. });
  1322. spyApi.callArgOnWith = spyApi.callArgOn;
  1323. delegateToCalls("yield", false, "yield", function () {
  1324. throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
  1325. });
  1326. // "invokeCallback" is an alias for "yield" since "yield" is invalid in strict mode.
  1327. spyApi.invokeCallback = spyApi.yield;
  1328. delegateToCalls("yieldOn", false, "yieldOn", function () {
  1329. throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
  1330. });
  1331. delegateToCalls("yieldTo", false, "yieldTo", function (property) {
  1332. throw new Error(this.toString() + " cannot yield to '" + property +
  1333. "' since it was not yet invoked.");
  1334. });
  1335. delegateToCalls("yieldToOn", false, "yieldToOn", function (property) {
  1336. throw new Error(this.toString() + " cannot yield to '" + property +
  1337. "' since it was not yet invoked.");
  1338. });
  1339. spyApi.formatters = {
  1340. "c": function (spy) {
  1341. return sinon.timesInWords(spy.callCount);
  1342. },
  1343. "n": function (spy) {
  1344. return spy.toString();
  1345. },
  1346. "C": function (spy) {
  1347. var calls = [];
  1348. for (var i = 0, l = spy.callCount; i < l; ++i) {
  1349. var stringifiedCall = " " + spy.getCall(i).toString();
  1350. if (/\n/.test(calls[i - 1])) {
  1351. stringifiedCall = "\n" + stringifiedCall;
  1352. }
  1353. push.call(calls, stringifiedCall);
  1354. }
  1355. return calls.length > 0 ? "\n" + calls.join("\n") : "";
  1356. },
  1357. "t": function (spy) {
  1358. var objects = [];
  1359. for (var i = 0, l = spy.callCount; i < l; ++i) {
  1360. push.call(objects, sinon.format(spy.thisValues[i]));
  1361. }
  1362. return objects.join(", ");
  1363. },
  1364. "*": function (spy, args) {
  1365. var formatted = [];
  1366. for (var i = 0, l = args.length; i < l; ++i) {
  1367. push.call(formatted, sinon.format(args[i]));
  1368. }
  1369. return formatted.join(", ");
  1370. }
  1371. };
  1372. sinon.extend(spy, spyApi);
  1373. spy.spyCall = sinon.spyCall;
  1374. if (commonJSModule) {
  1375. module.exports = spy;
  1376. } else {
  1377. sinon.spy = spy;
  1378. }
  1379. }(typeof sinon == "object" && sinon || null));
  1380. /**
  1381. * @depend ../sinon.js
  1382. * @depend spy.js
  1383. */
  1384. /*jslint eqeqeq: false, onevar: false*/
  1385. /*global module, require, sinon*/
  1386. /**
  1387. * Stub functions
  1388. *
  1389. * @author Christian Johansen (christian@cjohansen.no)
  1390. * @license BSD
  1391. *
  1392. * Copyright (c) 2010-2013 Christian Johansen
  1393. */
  1394. (function (sinon) {
  1395. var commonJSModule = typeof module == "object" && typeof require == "function";
  1396. if (!sinon && commonJSModule) {
  1397. sinon = require("../sinon");
  1398. }
  1399. if (!sinon) {
  1400. return;
  1401. }
  1402. function stub(object, property, func) {
  1403. if (!!func && typeof func != "function") {
  1404. throw new TypeError("Custom stub should be function");
  1405. }
  1406. var wrapper;
  1407. if (func) {
  1408. wrapper = sinon.spy && sinon.spy.create ? sinon.spy.create(func) : func;
  1409. } else {
  1410. wrapper = stub.create();
  1411. }
  1412. if (!object && !property) {
  1413. return sinon.stub.create();
  1414. }
  1415. if (!property && !!object && typeof object == "object") {
  1416. for (var prop in object) {
  1417. if (typeof object[prop] === "function") {
  1418. stub(object, prop);
  1419. }
  1420. }
  1421. return object;
  1422. }
  1423. return sinon.wrapMethod(object, property, wrapper);
  1424. }
  1425. function getChangingValue(stub, property) {
  1426. var index = stub.callCount - 1;
  1427. var values = stub[property];
  1428. var prop = index in values ? values[index] : values[values.length - 1];
  1429. stub[property + "Last"] = prop;
  1430. return prop;
  1431. }
  1432. function getCallback(stub, args) {
  1433. var callArgAt = getChangingValue(stub, "callArgAts");
  1434. if (callArgAt < 0) {
  1435. var callArgProp = getChangingValue(stub, "callArgProps");
  1436. for (var i = 0, l = args.length; i < l; ++i) {
  1437. if (!callArgProp && typeof args[i] == "function") {
  1438. return args[i];
  1439. }
  1440. if (callArgProp && args[i] &&
  1441. typeof args[i][callArgProp] == "function") {
  1442. return args[i][callArgProp];
  1443. }
  1444. }
  1445. return null;
  1446. }
  1447. return args[callArgAt];
  1448. }
  1449. var join = Array.prototype.join;
  1450. function getCallbackError(stub, func, args) {
  1451. if (stub.callArgAtsLast < 0) {
  1452. var msg;
  1453. if (stub.callArgPropsLast) {
  1454. msg = sinon.functionName(stub) +
  1455. " expected to yield to '" + stub.callArgPropsLast +
  1456. "', but no object with such a property was passed."
  1457. } else {
  1458. msg = sinon.functionName(stub) +
  1459. " expected to yield, but no callback was passed."
  1460. }
  1461. if (args.length > 0) {
  1462. msg += " Received [" + join.call(args, ", ") + "]";
  1463. }
  1464. return msg;
  1465. }
  1466. return "argument at index " + stub.callArgAtsLast + " is not a function: " + func;
  1467. }
  1468. var nextTick = (function () {
  1469. if (typeof process === "object" && typeof process.nextTick === "function") {
  1470. return process.nextTick;
  1471. } else if (typeof setImmediate === "function") {
  1472. return setImmediate;
  1473. } else {
  1474. return function (callback) {
  1475. setTimeout(callback, 0);
  1476. };
  1477. }
  1478. })();
  1479. function callCallback(stub, args) {
  1480. if (stub.callArgAts.length > 0) {
  1481. var func = getCallback(stub, args);
  1482. if (typeof func != "function") {
  1483. throw new TypeError(getCallbackError(stub, func, args));
  1484. }
  1485. var callbackArguments = getChangingValue(stub, "callbackArguments");
  1486. var callbackContext = getChangingValue(stub, "callbackContexts");
  1487. if (stub.callbackAsync) {
  1488. nextTick(function() {
  1489. func.apply(callbackContext, callbackArguments);
  1490. });
  1491. } else {
  1492. func.apply(callbackContext, callbackArguments);
  1493. }
  1494. }
  1495. }
  1496. var uuid = 0;
  1497. sinon.extend(stub, (function () {
  1498. var slice = Array.prototype.slice, proto;
  1499. function throwsException(error, message) {
  1500. if (typeof error == "string") {
  1501. this.exception = new Error(message || "");
  1502. this.exception.name = error;
  1503. } else if (!error) {
  1504. this.exception = new Error("Error");
  1505. } else {
  1506. this.exception = error;
  1507. }
  1508. return this;
  1509. }
  1510. proto = {
  1511. create: function create() {
  1512. var functionStub = function () {
  1513. callCallback(functionStub, arguments);
  1514. if (functionStub.exception) {
  1515. throw functionStub.exception;
  1516. } else if (typeof functionStub.returnArgAt == 'number') {
  1517. return arguments[functionStub.returnArgAt];
  1518. } else if (functionStub.returnThis) {
  1519. return this;
  1520. }
  1521. return functionStub.returnValue;
  1522. };
  1523. functionStub.id = "stub#" + uuid++;
  1524. var orig = functionStub;
  1525. functionStub = sinon.spy.create(functionStub);
  1526. functionStub.func = orig;
  1527. functionStub.callArgAts = [];
  1528. functionStub.callbackArguments = [];
  1529. functionStub.callbackContexts = [];
  1530. functionStub.callArgProps = [];
  1531. sinon.extend(functionStub, stub);
  1532. functionStub._create = sinon.stub.create;
  1533. functionStub.displayName = "stub";
  1534. functionStub.toString = sinon.functionToString;
  1535. return functionStub;
  1536. },
  1537. resetBehavior: function () {
  1538. var i;
  1539. this.callArgAts = [];
  1540. this.callbackArguments = [];
  1541. this.callbackContexts = [];
  1542. this.callArgProps = [];
  1543. delete this.returnValue;
  1544. delete this.returnArgAt;
  1545. this.returnThis = false;
  1546. if (this.fakes) {
  1547. for (i = 0; i < this.fakes.length; i++) {
  1548. this.fakes[i].resetBehavior();
  1549. }
  1550. }
  1551. },
  1552. returns: function returns(value) {
  1553. this.returnValue = value;
  1554. return this;
  1555. },
  1556. returnsArg: function returnsArg(pos) {
  1557. if (typeof pos != "number") {
  1558. throw new TypeError("argument index is not number");
  1559. }
  1560. this.returnArgAt = pos;
  1561. return this;
  1562. },
  1563. returnsThis: function returnsThis() {
  1564. this.returnThis = true;
  1565. return this;
  1566. },
  1567. "throws": throwsException,
  1568. throwsException: throwsException,
  1569. callsArg: function callsArg(pos) {
  1570. if (typeof pos != "number") {
  1571. throw new TypeError("argument index is not number");
  1572. }
  1573. this.callArgAts.push(pos);
  1574. this.callbackArguments.push([]);
  1575. this.callbackContexts.push(undefined);
  1576. this.callArgProps.push(undefined);
  1577. return this;
  1578. },
  1579. callsArgOn: function callsArgOn(pos, context) {
  1580. if (typeof pos != "number") {
  1581. throw new TypeError("argument index is not number");
  1582. }
  1583. if (typeof context != "object") {
  1584. throw new TypeError("argument context is not an object");
  1585. }
  1586. this.callArgAts.push(pos);
  1587. this.callbackArguments.push([]);
  1588. this.callbackContexts.push(context);
  1589. this.callArgProps.push(undefined);
  1590. return this;
  1591. },
  1592. callsArgWith: function callsArgWith(pos) {
  1593. if (typeof pos != "number") {
  1594. throw new TypeError("argument index is not number");
  1595. }
  1596. this.callArgAts.push(pos);
  1597. this.callbackArguments.push(slice.call(arguments, 1));
  1598. this.callbackContexts.push(undefined);
  1599. this.callArgProps.push(undefined);
  1600. return this;
  1601. },
  1602. callsArgOnWith: function callsArgWith(pos, context) {
  1603. if (typeof pos != "number") {
  1604. throw new TypeError("argument index is not number");
  1605. }
  1606. if (typeof context != "object") {
  1607. throw new TypeError("argument context is not an object");
  1608. }
  1609. this.callArgAts.push(pos);
  1610. this.callbackArguments.push(slice.call(arguments, 2));
  1611. this.callbackContexts.push(context);
  1612. this.callArgProps.push(undefined);
  1613. return this;
  1614. },
  1615. yields: function () {
  1616. this.callArgAts.push(-1);
  1617. this.callbackArguments.push(slice.call(arguments, 0));
  1618. this.callbackContexts.push(undefined);
  1619. this.callArgProps.push(undefined);
  1620. return this;
  1621. },
  1622. yieldsOn: function (context) {
  1623. if (typeof context != "object") {
  1624. throw new TypeError("argument context is not an object");
  1625. }
  1626. this.callArgAts.push(-1);
  1627. this.callbackArguments.push(slice.call(arguments, 1));
  1628. this.callbackContexts.push(context);
  1629. this.callArgProps.push(undefined);
  1630. return this;
  1631. },
  1632. yieldsTo: function (prop) {
  1633. this.callArgAts.push(-1);
  1634. this.callbackArguments.push(slice.call(arguments, 1));
  1635. this.callbackContexts.push(undefined);
  1636. this.callArgProps.push(prop);
  1637. return this;
  1638. },
  1639. yieldsToOn: function (prop, context) {
  1640. if (typeof context != "object") {
  1641. throw new TypeError("argument context is not an object");
  1642. }
  1643. this.callArgAts.push(-1);
  1644. this.callbackArguments.push(slice.call(arguments, 2));
  1645. this.callbackContexts.push(context);
  1646. this.callArgProps.push(prop);
  1647. return this;
  1648. }
  1649. };
  1650. // create asynchronous versions of callsArg* and yields* methods
  1651. for (var method in proto) {
  1652. // need to avoid creating anotherasync versions of the newly added async methods
  1653. if (proto.hasOwnProperty(method) &&
  1654. method.match(/^(callsArg|yields|thenYields$)/) &&
  1655. !method.match(/Async/)) {
  1656. proto[method + 'Async'] = (function (syncFnName) {
  1657. return function () {
  1658. this.callbackAsync = true;
  1659. return this[syncFnName].apply(this, arguments);
  1660. };
  1661. })(method);
  1662. }
  1663. }
  1664. return proto;
  1665. }()));
  1666. if (commonJSModule) {
  1667. module.exports = stub;
  1668. } else {
  1669. sinon.stub = stub;
  1670. }
  1671. }(typeof sinon == "object" && sinon || null));
  1672. /**
  1673. * @depend ../sinon.js
  1674. * @depend stub.js
  1675. */
  1676. /*jslint eqeqeq: false, onevar: false, nomen: false*/
  1677. /*global module, require, sinon*/
  1678. /**
  1679. * Mock functions.
  1680. *
  1681. * @author Christian Johansen (christian@cjohansen.no)
  1682. * @license BSD
  1683. *
  1684. * Copyright (c) 2010-2013 Christian Johansen
  1685. */
  1686. (function (sinon) {
  1687. var commonJSModule = typeof module == "object" && typeof require == "function";
  1688. var push = [].push;
  1689. if (!sinon && commonJSModule) {
  1690. sinon = require("../sinon");
  1691. }
  1692. if (!sinon) {
  1693. return;
  1694. }
  1695. function mock(object) {
  1696. if (!object) {
  1697. return sinon.expectation.create("Anonymous mock");
  1698. }
  1699. return mock.create(object);
  1700. }
  1701. sinon.mock = mock;
  1702. sinon.extend(mock, (function () {
  1703. function each(collection, callback) {
  1704. if (!collection) {
  1705. return;
  1706. }
  1707. for (var i = 0, l = collection.length; i < l; i += 1) {
  1708. callback(collection[i]);
  1709. }
  1710. }
  1711. return {
  1712. create: function create(object) {
  1713. if (!object) {
  1714. throw new TypeError("object is null");
  1715. }
  1716. var mockObject = sinon.extend({}, mock);
  1717. mockObject.object = object;
  1718. delete mockObject.create;
  1719. return mockObject;
  1720. },
  1721. expects: function expects(method) {
  1722. if (!method) {
  1723. throw new TypeError("method is falsy");
  1724. }
  1725. if (!this.expectations) {
  1726. this.expectations = {};
  1727. this.proxies = [];
  1728. }
  1729. if (!this.expectations[method]) {
  1730. this.expectations[method] = [];
  1731. var mockObject = this;
  1732. sinon.wrapMethod(this.object, method, function () {
  1733. return mockObject.invokeMethod(method, this, arguments);
  1734. });
  1735. push.call(this.proxies, method);
  1736. }
  1737. var expectation = sinon.expectation.create(method);
  1738. push.call(this.expectations[method], expectation);
  1739. return expectation;
  1740. },
  1741. restore: function restore() {
  1742. var object = this.object;
  1743. each(this.proxies, function (proxy) {
  1744. if (typeof object[proxy].restore == "function") {
  1745. object[proxy].restore();
  1746. }
  1747. });
  1748. },
  1749. verify: function verify() {
  1750. var expectations = this.expectations || {};
  1751. var messages = [], met = [];
  1752. each(this.proxies, function (proxy) {
  1753. each(expectations[proxy], function (expectation) {
  1754. if (!expectation.met()) {
  1755. push.call(messages, expectation.toString());
  1756. } else {
  1757. push.call(met, expectation.toString());
  1758. }
  1759. });
  1760. });
  1761. this.restore();
  1762. if (messages.length > 0) {
  1763. sinon.expectation.fail(messages.concat(met).join("\n"));
  1764. } else {
  1765. sinon.expectation.pass(messages.concat(met).join("\n"));
  1766. }
  1767. return true;
  1768. },
  1769. invokeMethod: function invokeMethod(method, thisValue, args) {
  1770. var expectations = this.expectations && this.expectations[method];
  1771. var length = expectations && expectations.length || 0, i;
  1772. for (i = 0; i < length; i += 1) {
  1773. if (!expectations[i].met() &&
  1774. expectations[i].allowsCall(thisValue, args)) {
  1775. return expectations[i].apply(thisValue, args);
  1776. }
  1777. }
  1778. var messages = [], available, exhausted = 0;
  1779. for (i = 0; i < length; i += 1) {
  1780. if (expectations[i].allowsCall(thisValue, args)) {
  1781. available = available || expectations[i];
  1782. } else {
  1783. exhausted += 1;
  1784. }
  1785. push.call(messages, " " + expectations[i].toString());
  1786. }
  1787. if (exhausted === 0) {
  1788. return available.apply(thisValue, args);
  1789. }
  1790. messages.unshift("Unexpected call: " + sinon.spyCall.toString.call({
  1791. proxy: method,
  1792. args: args
  1793. }));
  1794. sinon.expectation.fail(messages.join("\n"));
  1795. }
  1796. };
  1797. }()));
  1798. var times = sinon.timesInWords;
  1799. sinon.expectation = (function () {
  1800. var slice = Array.prototype.slice;
  1801. var _invoke = sinon.spy.invoke;
  1802. function callCountInWords(callCount) {
  1803. if (callCount == 0) {
  1804. return "never called";
  1805. } else {
  1806. return "called " + times(callCount);
  1807. }
  1808. }
  1809. function expectedCallCountInWords(expectation) {
  1810. var min = expectation.minCalls;
  1811. var max = expectation.maxCalls;
  1812. if (typeof min == "number" && typeof max == "number") {
  1813. var str = times(min);
  1814. if (min != max) {
  1815. str = "at least " + str + " and at most " + times(max);
  1816. }
  1817. return str;
  1818. }
  1819. if (typeof min == "number") {
  1820. return "at least " + times(min);
  1821. }
  1822. return "at most " + times(max);
  1823. }
  1824. function receivedMinCalls(expectation) {
  1825. var hasMinLimit = typeof expectation.minCalls == "number";
  1826. return !hasMinLimit || expectation.callCount >= expectation.minCalls;
  1827. }
  1828. function receivedMaxCalls(expectation) {
  1829. if (typeof expectation.maxCalls != "number") {
  1830. return false;
  1831. }
  1832. return expectation.callCount == expectation.maxCalls;
  1833. }
  1834. return {
  1835. minCalls: 1,
  1836. maxCalls: 1,
  1837. create: function create(methodName) {
  1838. var expectation = sinon.extend(sinon.stub.create(), sinon.expectation);
  1839. delete expectation.create;
  1840. expectation.method = methodName;
  1841. return expectation;
  1842. },
  1843. invoke: function invoke(func, thisValue, args) {
  1844. this.verifyCallAllowed(thisValue, args);
  1845. return _invoke.apply(this, arguments);
  1846. },
  1847. atLeast: function atLeast(num) {
  1848. if (typeof num != "number") {
  1849. throw new TypeError("'" + num + "' is not number");
  1850. }
  1851. if (!this.limitsSet) {
  1852. this.maxCalls = null;
  1853. this.limitsSet = true;
  1854. }
  1855. this.minCalls = num;
  1856. return this;
  1857. },
  1858. atMost: function atMost(num) {
  1859. if (typeof num != "number") {
  1860. throw new TypeError("'" + num + "' is not number");
  1861. }
  1862. if (!this.limitsSet) {
  1863. this.minCalls = null;
  1864. this.limitsSet = true;
  1865. }
  1866. this.maxCalls = num;
  1867. return this;
  1868. },
  1869. never: function never() {
  1870. return this.exactly(0);
  1871. },
  1872. once: function once() {
  1873. return this.exactly(1);
  1874. },
  1875. twice: function twice() {
  1876. return this.exactly(2);
  1877. },
  1878. thrice: function thrice() {
  1879. return this.exactly(3);
  1880. },
  1881. exactly: function exactly(num) {
  1882. if (typeof num != "number") {
  1883. throw new TypeError("'" + num + "' is not a number");
  1884. }
  1885. this.atLeast(num);
  1886. return this.atMost(num);
  1887. },
  1888. met: function met() {
  1889. return !this.failed && receivedMinCalls(this);
  1890. },
  1891. verifyCallAllowed: function verifyCallAllowed(thisValue, args) {
  1892. if (receivedMaxCalls(this)) {
  1893. this.failed = true;
  1894. sinon.expectation.fail(this.method + " already called " + times(this.maxCalls));
  1895. }
  1896. if ("expectedThis" in this && this.expectedThis !== thisValue) {
  1897. sinon.expectation.fail(this.method + " called with " + thisValue + " as thisValue, expected " +
  1898. this.expectedThis);
  1899. }
  1900. if (!("expectedArguments" in this)) {
  1901. return;
  1902. }
  1903. if (!args) {
  1904. sinon.expectation.fail(this.method + " received no arguments, expected " +
  1905. sinon.format(this.expectedArguments));
  1906. }
  1907. if (args.length < this.expectedArguments.length) {
  1908. sinon.expectation.fail(this.method + " received too few arguments (" + sinon.format(args) +
  1909. "), expected " + sinon.format(this.expectedArguments));
  1910. }
  1911. if (this.expectsExactArgCount &&
  1912. args.length != this.expectedArguments.length) {
  1913. sinon.expectation.fail(this.method + " received too many arguments (" + sinon.format(args) +
  1914. "), expected " + sinon.format(this.expectedArguments));
  1915. }
  1916. for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
  1917. if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
  1918. sinon.expectation.fail(this.method + " received wrong arguments " + sinon.format(args) +
  1919. ", expected " + sinon.format(this.expectedArguments));
  1920. }
  1921. }
  1922. },
  1923. allowsCall: function allowsCall(thisValue, args) {
  1924. if (this.met() && receivedMaxCalls(this)) {
  1925. return false;
  1926. }
  1927. if ("expectedThis" in this && this.expectedThis !== thisValue) {
  1928. return false;
  1929. }
  1930. if (!("expectedArguments" in this)) {
  1931. return true;
  1932. }
  1933. args = args || [];
  1934. if (args.length < this.expectedArguments.length) {
  1935. return false;
  1936. }
  1937. if (this.expectsExactArgCount &&
  1938. args.length != this.expectedArguments.length) {
  1939. return false;
  1940. }
  1941. for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
  1942. if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
  1943. return false;
  1944. }
  1945. }
  1946. return true;
  1947. },
  1948. withArgs: function withArgs() {
  1949. this.expectedArguments = slice.call(arguments);
  1950. return this;
  1951. },
  1952. withExactArgs: function withExactArgs() {
  1953. this.withArgs.apply(this, arguments);
  1954. this.expectsExactArgCount = true;
  1955. return this;
  1956. },
  1957. on: function on(thisValue) {
  1958. this.expectedThis = thisValue;
  1959. return this;
  1960. },
  1961. toString: function () {
  1962. var args = (this.expectedArguments || []).slice();
  1963. if (!this.expectsExactArgCount) {
  1964. push.call(args, "[...]");
  1965. }
  1966. var callStr = sinon.spyCall.toString.call({
  1967. proxy: this.method || "anonymous mock expectation",
  1968. args: args
  1969. });
  1970. var message = callStr.replace(", [...", "[, ...") + " " +
  1971. expectedCallCountInWords(this);
  1972. if (this.met()) {
  1973. return "Expectation met: " + message;
  1974. }
  1975. return "Expected " + message + " (" +
  1976. callCountInWords(this.callCount) + ")";
  1977. },
  1978. verify: function verify() {
  1979. if (!this.met()) {
  1980. sinon.expectation.fail(this.toString());
  1981. } else {
  1982. sinon.expectation.pass(this.toString());
  1983. }
  1984. return true;
  1985. },
  1986. pass: function(message) {
  1987. sinon.assert.pass(message);
  1988. },
  1989. fail: function (message) {
  1990. var exception = new Error(message);
  1991. exception.name = "ExpectationError";
  1992. throw exception;
  1993. }
  1994. };
  1995. }());
  1996. if (commonJSModule) {
  1997. module.exports = mock;
  1998. } else {
  1999. sinon.mock = mock;
  2000. }
  2001. }(typeof sinon == "object" && sinon || null));
  2002. /**
  2003. * @depend ../sinon.js
  2004. * @depend stub.js
  2005. * @depend mock.js
  2006. */
  2007. /*jslint eqeqeq: false, onevar: false, forin: true*/
  2008. /*global module, require, sinon*/
  2009. /**
  2010. * Collections of stubs, spies and mocks.
  2011. *
  2012. * @author Christian Johansen (christian@cjohansen.no)
  2013. * @license BSD
  2014. *
  2015. * Copyright (c) 2010-2013 Christian Johansen
  2016. */
  2017. (function (sinon) {
  2018. var commonJSModule = typeof module == "object" && typeof require == "function";
  2019. var push = [].push;
  2020. var hasOwnProperty = Object.prototype.hasOwnProperty;
  2021. if (!sinon && commonJSModule) {
  2022. sinon = require("../sinon");
  2023. }
  2024. if (!sinon) {
  2025. return;
  2026. }
  2027. function getFakes(fakeCollection) {
  2028. if (!fakeCollection.fakes) {
  2029. fakeCollection.fakes = [];
  2030. }
  2031. return fakeCollection.fakes;
  2032. }
  2033. function each(fakeCollection, method) {
  2034. var fakes = getFakes(fakeCollection);
  2035. for (var i = 0, l = fakes.length; i < l; i += 1) {
  2036. if (typeof fakes[i][method] == "function") {
  2037. fakes[i][method]();
  2038. }
  2039. }
  2040. }
  2041. function compact(fakeCollection) {
  2042. var fakes = getFakes(fakeCollection);
  2043. var i = 0;
  2044. while (i < fakes.length) {
  2045. fakes.splice(i, 1);
  2046. }
  2047. }
  2048. var collection = {
  2049. verify: function resolve() {
  2050. each(this, "verify");
  2051. },
  2052. restore: function restore() {
  2053. each(this, "restore");
  2054. compact(this);
  2055. },
  2056. verifyAndRestore: function verifyAndRestore() {
  2057. var exception;
  2058. try {
  2059. this.verify();
  2060. } catch (e) {
  2061. exception = e;
  2062. }
  2063. this.restore();
  2064. if (exception) {
  2065. throw exception;
  2066. }
  2067. },
  2068. add: function add(fake) {
  2069. push.call(getFakes(this), fake);
  2070. return fake;
  2071. },
  2072. spy: function spy() {
  2073. return this.add(sinon.spy.apply(sinon, arguments));
  2074. },
  2075. stub: function stub(object, property, value) {
  2076. if (property) {
  2077. var original = object[property];
  2078. if (typeof original != "function") {
  2079. if (!hasOwnProperty.call(object, property)) {
  2080. throw new TypeError("Cannot stub non-existent own property " + property);
  2081. }
  2082. object[property] = value;
  2083. return this.add({
  2084. restore: function () {
  2085. object[property] = original;
  2086. }
  2087. });
  2088. }
  2089. }
  2090. if (!property && !!object && typeof object == "object") {
  2091. var stubbedObj = sinon.stub.apply(sinon, arguments);
  2092. for (var prop in stubbedObj) {
  2093. if (typeof stubbedObj[prop] === "function") {
  2094. this.add(stubbedObj[prop]);
  2095. }
  2096. }
  2097. return stubbedObj;
  2098. }
  2099. return this.add(sinon.stub.apply(sinon, arguments));
  2100. },
  2101. mock: function mock() {
  2102. return this.add(sinon.mock.apply(sinon, arguments));
  2103. },
  2104. inject: function inject(obj) {
  2105. var col = this;
  2106. obj.spy = function () {
  2107. return col.spy.apply(col, arguments);
  2108. };
  2109. obj.stub = function () {
  2110. return col.stub.apply(col, arguments);
  2111. };
  2112. obj.mock = function () {
  2113. return col.mock.apply(col, arguments);
  2114. };
  2115. return obj;
  2116. }
  2117. };
  2118. if (commonJSModule) {
  2119. module.exports = collection;
  2120. } else {
  2121. sinon.collection = collection;
  2122. }
  2123. }(typeof sinon == "object" && sinon || null));
  2124. /*jslint eqeqeq: false, plusplus: false, evil: true, onevar: false, browser: true, forin: false*/
  2125. /*global module, require, window*/
  2126. /**
  2127. * Fake timer API
  2128. * setTimeout
  2129. * setInterval
  2130. * clearTimeout
  2131. * clearInterval
  2132. * tick
  2133. * reset
  2134. * Date
  2135. *
  2136. * Inspired by jsUnitMockTimeOut from JsUnit
  2137. *
  2138. * @author Christian Johansen (christian@cjohansen.no)
  2139. * @license BSD
  2140. *
  2141. * Copyright (c) 2010-2013 Christian Johansen
  2142. */
  2143. if (typeof sinon == "undefined") {
  2144. var sinon = {};
  2145. }
  2146. (function (global) {
  2147. var id = 1;
  2148. function addTimer(args, recurring) {
  2149. if (args.length === 0) {
  2150. throw new Error("Function requires at least 1 parameter");
  2151. }
  2152. var toId = id++;
  2153. var delay = args[1] || 0;
  2154. if (!this.timeouts) {
  2155. this.timeouts = {};
  2156. }
  2157. this.timeouts[toId] = {
  2158. id: toId,
  2159. func: args[0],
  2160. callAt: this.now + delay,
  2161. invokeArgs: Array.prototype.slice.call(args, 2)
  2162. };
  2163. if (recurring === true) {
  2164. this.timeouts[toId].interval = delay;
  2165. }
  2166. return toId;
  2167. }
  2168. function parseTime(str) {
  2169. if (!str) {
  2170. return 0;
  2171. }
  2172. var strings = str.split(":");
  2173. var l = strings.length, i = l;
  2174. var ms = 0, parsed;
  2175. if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) {
  2176. throw new Error("tick only understands numbers and 'h:m:s'");
  2177. }
  2178. while (i--) {
  2179. parsed = parseInt(strings[i], 10);
  2180. if (parsed >= 60) {
  2181. throw new Error("Invalid time " + str);
  2182. }
  2183. ms += parsed * Math.pow(60, (l - i - 1));
  2184. }
  2185. return ms * 1000;
  2186. }
  2187. function createObject(object) {
  2188. var newObject;
  2189. if (Object.create) {
  2190. newObject = Object.create(object);
  2191. } else {
  2192. var F = function () {};
  2193. F.prototype = object;
  2194. newObject = new F();
  2195. }
  2196. newObject.Date.clock = newObject;
  2197. return newObject;
  2198. }
  2199. sinon.clock = {
  2200. now: 0,
  2201. create: function create(now) {
  2202. var clock = createObject(this);
  2203. if (typeof now == "number") {
  2204. clock.now = now;
  2205. }
  2206. if (!!now && typeof now == "object") {
  2207. throw new TypeError("now should be milliseconds since UNIX epoch");
  2208. }
  2209. return clock;
  2210. },
  2211. setTimeout: function setTimeout(callback, timeout) {
  2212. return addTimer.call(this, arguments, false);
  2213. },
  2214. clearTimeout: function clearTimeout(timerId) {
  2215. if (!this.timeouts) {
  2216. this.timeouts = [];
  2217. }
  2218. if (timerId in this.timeouts) {
  2219. delete this.timeouts[timerId];
  2220. }
  2221. },
  2222. setInterval: function setInterval(callback, timeout) {
  2223. return addTimer.call(this, arguments, true);
  2224. },
  2225. clearInterval: function clearInterval(timerId) {
  2226. this.clearTimeout(timerId);
  2227. },
  2228. tick: function tick(ms) {
  2229. ms = typeof ms == "number" ? ms : parseTime(ms);
  2230. var tickFrom = this.now, tickTo = this.now + ms, previous = this.now;
  2231. var timer = this.firstTimerInRange(tickFrom, tickTo);
  2232. var firstException;
  2233. while (timer && tickFrom <= tickTo) {
  2234. if (this.timeouts[timer.id]) {
  2235. tickFrom = this.now = timer.callAt;
  2236. try {
  2237. this.callTimer(timer);
  2238. } catch (e) {
  2239. firstException = firstException || e;
  2240. }
  2241. }
  2242. timer = this.firstTimerInRange(previous, tickTo);
  2243. previous = tickFrom;
  2244. }
  2245. this.now = tickTo;
  2246. if (firstException) {
  2247. throw firstException;
  2248. }
  2249. return this.now;
  2250. },
  2251. firstTimerInRange: function (from, to) {
  2252. var timer, smallest, originalTimer;
  2253. for (var id in this.timeouts) {
  2254. if (this.timeouts.hasOwnProperty(id)) {
  2255. if (this.timeouts[id].callAt < from || this.timeouts[id].callAt > to) {
  2256. continue;
  2257. }
  2258. if (!smallest || this.timeouts[id].callAt < smallest) {
  2259. originalTimer = this.timeouts[id];
  2260. smallest = this.timeouts[id].callAt;
  2261. timer = {
  2262. func: this.timeouts[id].func,
  2263. callAt: this.timeouts[id].callAt,
  2264. interval: this.timeouts[id].interval,
  2265. id: this.timeouts[id].id,
  2266. invokeArgs: this.timeouts[id].invokeArgs
  2267. };
  2268. }
  2269. }
  2270. }
  2271. return timer || null;
  2272. },
  2273. callTimer: function (timer) {
  2274. if (typeof timer.interval == "number") {
  2275. this.timeouts[timer.id].callAt += timer.interval;
  2276. } else {
  2277. delete this.timeouts[timer.id];
  2278. }
  2279. try {
  2280. if (typeof timer.func == "function") {
  2281. timer.func.apply(null, timer.invokeArgs);
  2282. } else {
  2283. eval(timer.func);
  2284. }
  2285. } catch (e) {
  2286. var exception = e;
  2287. }
  2288. if (!this.timeouts[timer.id]) {
  2289. if (exception) {
  2290. throw exception;
  2291. }
  2292. return;
  2293. }
  2294. if (exception) {
  2295. throw exception;
  2296. }
  2297. },
  2298. reset: function reset() {
  2299. this.timeouts = {};
  2300. },
  2301. Date: (function () {
  2302. var NativeDate = Date;
  2303. function ClockDate(year, month, date, hour, minute, second, ms) {
  2304. // Defensive and verbose to avoid potential harm in passing
  2305. // explicit undefined when user does not pass argument
  2306. switch (arguments.length) {
  2307. case 0:
  2308. return new NativeDate(ClockDate.clock.now);
  2309. case 1:
  2310. return new NativeDate(year);
  2311. case 2:
  2312. return new NativeDate(year, month);
  2313. case 3:
  2314. return new NativeDate(year, month, date);
  2315. case 4:
  2316. return new NativeDate(year, month, date, hour);
  2317. case 5:
  2318. return new NativeDate(year, month, date, hour, minute);
  2319. case 6:
  2320. return new NativeDate(year, month, date, hour, minute, second);
  2321. default:
  2322. return new NativeDate(year, month, date, hour, minute, second, ms);
  2323. }
  2324. }
  2325. return mirrorDateProperties(ClockDate, NativeDate);
  2326. }())
  2327. };
  2328. function mirrorDateProperties(target, source) {
  2329. if (source.now) {
  2330. target.now = function now() {
  2331. return target.clock.now;
  2332. };
  2333. } else {
  2334. delete target.now;
  2335. }
  2336. if (source.toSource) {
  2337. target.toSource = function toSource() {
  2338. return source.toSource();
  2339. };
  2340. } else {
  2341. delete target.toSource;
  2342. }
  2343. target.toString = function toString() {
  2344. return source.toString();
  2345. };
  2346. target.prototype = source.prototype;
  2347. target.parse = source.parse;
  2348. target.UTC = source.UTC;
  2349. target.prototype.toUTCString = source.prototype.toUTCString;
  2350. return target;
  2351. }
  2352. var methods = ["Date", "setTimeout", "setInterval",
  2353. "clearTimeout", "clearInterval"];
  2354. function restore() {
  2355. var method;
  2356. for (var i = 0, l = this.methods.length; i < l; i++) {
  2357. method = this.methods[i];
  2358. if (global[method].hadOwnProperty) {
  2359. global[method] = this["_" + method];
  2360. } else {
  2361. delete global[method];
  2362. }
  2363. }
  2364. // Prevent multiple executions which will completely remove these props
  2365. this.methods = [];
  2366. }
  2367. function stubGlobal(method, clock) {
  2368. clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(global, method);
  2369. clock["_" + method] = global[method];
  2370. if (method == "Date") {
  2371. var date = mirrorDateProperties(clock[method], global[method]);
  2372. global[method] = date;
  2373. } else {
  2374. global[method] = function () {
  2375. return clock[method].apply(clock, arguments);
  2376. };
  2377. for (var prop in clock[method]) {
  2378. if (clock[method].hasOwnProperty(prop)) {
  2379. global[method][prop] = clock[method][prop];
  2380. }
  2381. }
  2382. }
  2383. global[method].clock = clock;
  2384. }
  2385. sinon.useFakeTimers = function useFakeTimers(now) {
  2386. var clock = sinon.clock.create(now);
  2387. clock.restore = restore;
  2388. clock.methods = Array.prototype.slice.call(arguments,
  2389. typeof now == "number" ? 1 : 0);
  2390. if (clock.methods.length === 0) {
  2391. clock.methods = methods;
  2392. }
  2393. for (var i = 0, l = clock.methods.length; i < l; i++) {
  2394. stubGlobal(clock.methods[i], clock);
  2395. }
  2396. return clock;
  2397. };
  2398. }(typeof global != "undefined" && typeof global !== "function" ? global : this));
  2399. sinon.timers = {
  2400. setTimeout: setTimeout,
  2401. clearTimeout: clearTimeout,
  2402. setInterval: setInterval,
  2403. clearInterval: clearInterval,
  2404. Date: Date
  2405. };
  2406. if (typeof module == "object" && typeof require == "function") {
  2407. module.exports = sinon;
  2408. }
  2409. /*jslint eqeqeq: false, onevar: false*/
  2410. /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
  2411. /**
  2412. * Minimal Event interface implementation
  2413. *
  2414. * Original implementation by Sven Fuchs: https://gist.github.com/995028
  2415. * Modifications and tests by Christian Johansen.
  2416. *
  2417. * @author Sven Fuchs (svenfuchs@artweb-design.de)
  2418. * @author Christian Johansen (christian@cjohansen.no)
  2419. * @license BSD
  2420. *
  2421. * Copyright (c) 2011 Sven Fuchs, Christian Johansen
  2422. */
  2423. if (typeof sinon == "undefined") {
  2424. this.sinon = {};
  2425. }
  2426. (function () {
  2427. var push = [].push;
  2428. sinon.Event = function Event(type, bubbles, cancelable, target) {
  2429. this.initEvent(type, bubbles, cancelable, target);
  2430. };
  2431. sinon.Event.prototype = {
  2432. initEvent: function(type, bubbles, cancelable, target) {
  2433. this.type = type;
  2434. this.bubbles = bubbles;
  2435. this.cancelable = cancelable;
  2436. this.target = target;
  2437. },
  2438. stopPropagation: function () {},
  2439. preventDefault: function () {
  2440. this.defaultPrevented = true;
  2441. }
  2442. };
  2443. sinon.EventTarget = {
  2444. addEventListener: function addEventListener(event, listener, useCapture) {
  2445. this.eventListeners = this.eventListeners || {};
  2446. this.eventListeners[event] = this.eventListeners[event] || [];
  2447. push.call(this.eventListeners[event], listener);
  2448. },
  2449. removeEventListener: function removeEventListener(event, listener, useCapture) {
  2450. var listeners = this.eventListeners && this.eventListeners[event] || [];
  2451. for (var i = 0, l = listeners.length; i < l; ++i) {
  2452. if (listeners[i] == listener) {
  2453. return listeners.splice(i, 1);
  2454. }
  2455. }
  2456. },
  2457. dispatchEvent: function dispatchEvent(event) {
  2458. var type = event.type;
  2459. var listeners = this.eventListeners && this.eventListeners[type] || [];
  2460. for (var i = 0; i < listeners.length; i++) {
  2461. if (typeof listeners[i] == "function") {
  2462. listeners[i].call(this, event);
  2463. } else {
  2464. listeners[i].handleEvent(event);
  2465. }
  2466. }
  2467. return !!event.defaultPrevented;
  2468. }
  2469. };
  2470. }());
  2471. /**
  2472. * @depend ../../sinon.js
  2473. * @depend event.js
  2474. */
  2475. /*jslint eqeqeq: false, onevar: false*/
  2476. /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
  2477. /**
  2478. * Fake XMLHttpRequest object
  2479. *
  2480. * @author Christian Johansen (christian@cjohansen.no)
  2481. * @license BSD
  2482. *
  2483. * Copyright (c) 2010-2013 Christian Johansen
  2484. */
  2485. if (typeof sinon == "undefined") {
  2486. this.sinon = {};
  2487. }
  2488. sinon.xhr = { XMLHttpRequest: this.XMLHttpRequest };
  2489. // wrapper for global
  2490. (function(global) {
  2491. var xhr = sinon.xhr;
  2492. xhr.GlobalXMLHttpRequest = global.XMLHttpRequest;
  2493. xhr.GlobalActiveXObject = global.ActiveXObject;
  2494. xhr.supportsActiveX = typeof xhr.GlobalActiveXObject != "undefined";
  2495. xhr.supportsXHR = typeof xhr.GlobalXMLHttpRequest != "undefined";
  2496. xhr.workingXHR = xhr.supportsXHR ? xhr.GlobalXMLHttpRequest : xhr.supportsActiveX
  2497. ? function() { return new xhr.GlobalActiveXObject("MSXML2.XMLHTTP.3.0") } : false;
  2498. /*jsl:ignore*/
  2499. var unsafeHeaders = {
  2500. "Accept-Charset": true,
  2501. "Accept-Encoding": true,
  2502. "Connection": true,
  2503. "Content-Length": true,
  2504. "Cookie": true,
  2505. "Cookie2": true,
  2506. "Content-Transfer-Encoding": true,
  2507. "Date": true,
  2508. "Expect": true,
  2509. "Host": true,
  2510. "Keep-Alive": true,
  2511. "Referer": true,
  2512. "TE": true,
  2513. "Trailer": true,
  2514. "Transfer-Encoding": true,
  2515. "Upgrade": true,
  2516. "User-Agent": true,
  2517. "Via": true
  2518. };
  2519. /*jsl:end*/
  2520. function FakeXMLHttpRequest() {
  2521. this.readyState = FakeXMLHttpRequest.UNSENT;
  2522. this.requestHeaders = {};
  2523. this.requestBody = null;
  2524. this.status = 0;
  2525. this.statusText = "";
  2526. var xhr = this;
  2527. ["loadstart", "load", "abort", "loadend"].forEach(function (eventName) {
  2528. xhr.addEventListener(eventName, function (event) {
  2529. var listener = xhr["on" + eventName];
  2530. if (listener && typeof listener == "function") {
  2531. listener(event);
  2532. }
  2533. });
  2534. });
  2535. if (typeof FakeXMLHttpRequest.onCreate == "function") {
  2536. FakeXMLHttpRequest.onCreate(this);
  2537. }
  2538. }
  2539. function verifyState(xhr) {
  2540. if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
  2541. throw new Error("INVALID_STATE_ERR");
  2542. }
  2543. if (xhr.sendFlag) {
  2544. throw new Error("INVALID_STATE_ERR");
  2545. }
  2546. }
  2547. // filtering to enable a white-list version of Sinon FakeXhr,
  2548. // where whitelisted requests are passed through to real XHR
  2549. function each(collection, callback) {
  2550. if (!collection) return;
  2551. for (var i = 0, l = collection.length; i < l; i += 1) {
  2552. callback(collection[i]);
  2553. }
  2554. }
  2555. function some(collection, callback) {
  2556. for (var index = 0; index < collection.length; index++) {
  2557. if(callback(collection[index]) === true) return true;
  2558. };
  2559. return false;
  2560. }
  2561. // largest arity in XHR is 5 - XHR#open
  2562. var apply = function(obj,method,args) {
  2563. switch(args.length) {
  2564. case 0: return obj[method]();
  2565. case 1: return obj[method](args[0]);
  2566. case 2: return obj[method](args[0],args[1]);
  2567. case 3: return obj[method](args[0],args[1],args[2]);
  2568. case 4: return obj[method](args[0],args[1],args[2],args[3]);
  2569. case 5: return obj[method](args[0],args[1],args[2],args[3],args[4]);
  2570. };
  2571. };
  2572. FakeXMLHttpRequest.filters = [];
  2573. FakeXMLHttpRequest.addFilter = function(fn) {
  2574. this.filters.push(fn)
  2575. };
  2576. var IE6Re = /MSIE 6/;
  2577. FakeXMLHttpRequest.defake = function(fakeXhr,xhrArgs) {
  2578. var xhr = new sinon.xhr.workingXHR();
  2579. each(["open","setRequestHeader","send","abort","getResponseHeader",
  2580. "getAllResponseHeaders","addEventListener","overrideMimeType","removeEventListener"],
  2581. function(method) {
  2582. fakeXhr[method] = function() {
  2583. return apply(xhr,method,arguments);
  2584. };
  2585. });
  2586. var copyAttrs = function(args) {
  2587. each(args, function(attr) {
  2588. try {
  2589. fakeXhr[attr] = xhr[attr]
  2590. } catch(e) {
  2591. if(!IE6Re.test(navigator.userAgent)) throw e;
  2592. }
  2593. });
  2594. };
  2595. var stateChange = function() {
  2596. fakeXhr.readyState = xhr.readyState;
  2597. if(xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) {
  2598. copyAttrs(["status","statusText"]);
  2599. }
  2600. if(xhr.readyState >= FakeXMLHttpRequest.LOADING) {
  2601. copyAttrs(["responseText"]);
  2602. }
  2603. if(xhr.readyState === FakeXMLHttpRequest.DONE) {
  2604. copyAttrs(["responseXML"]);
  2605. }
  2606. if(fakeXhr.onreadystatechange) fakeXhr.onreadystatechange.call(fakeXhr);
  2607. };
  2608. if(xhr.addEventListener) {
  2609. for(var event in fakeXhr.eventListeners) {
  2610. if(fakeXhr.eventListeners.hasOwnProperty(event)) {
  2611. each(fakeXhr.eventListeners[event],function(handler) {
  2612. xhr.addEventListener(event, handler);
  2613. });
  2614. }
  2615. }
  2616. xhr.addEventListener("readystatechange",stateChange);
  2617. } else {
  2618. xhr.onreadystatechange = stateChange;
  2619. }
  2620. apply(xhr,"open",xhrArgs);
  2621. };
  2622. FakeXMLHttpRequest.useFilters = false;
  2623. function verifyRequestSent(xhr) {
  2624. if (xhr.readyState == FakeXMLHttpRequest.DONE) {
  2625. throw new Error("Request done");
  2626. }
  2627. }
  2628. function verifyHeadersReceived(xhr) {
  2629. if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
  2630. throw new Error("No headers received");
  2631. }
  2632. }
  2633. function verifyResponseBodyType(body) {
  2634. if (typeof body != "string") {
  2635. var error = new Error("Attempted to respond to fake XMLHttpRequest with " +
  2636. body + ", which is not a string.");
  2637. error.name = "InvalidBodyException";
  2638. throw error;
  2639. }
  2640. }
  2641. sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, {
  2642. async: true,
  2643. open: function open(method, url, async, username, password) {
  2644. this.method = method;
  2645. this.url = url;
  2646. this.async = typeof async == "boolean" ? async : true;
  2647. this.username = username;
  2648. this.password = password;
  2649. this.responseText = null;
  2650. this.responseXML = null;
  2651. this.requestHeaders = {};
  2652. this.sendFlag = false;
  2653. if(sinon.FakeXMLHttpRequest.useFilters === true) {
  2654. var xhrArgs = arguments;
  2655. var defake = some(FakeXMLHttpRequest.filters,function(filter) {
  2656. return filter.apply(this,xhrArgs)
  2657. });
  2658. if (defake) {
  2659. return sinon.FakeXMLHttpRequest.defake(this,arguments);
  2660. }
  2661. }
  2662. this.readyStateChange(FakeXMLHttpRequest.OPENED);
  2663. },
  2664. readyStateChange: function readyStateChange(state) {
  2665. this.readyState = state;
  2666. if (typeof this.onreadystatechange == "function") {
  2667. try {
  2668. this.onreadystatechange();
  2669. } catch (e) {
  2670. sinon.logError("Fake XHR onreadystatechange handler", e);
  2671. }
  2672. }
  2673. this.dispatchEvent(new sinon.Event("readystatechange"));
  2674. switch (this.readyState) {
  2675. case FakeXMLHttpRequest.DONE:
  2676. this.dispatchEvent(new sinon.Event("load", false, false, this));
  2677. this.dispatchEvent(new sinon.Event("loadend", false, false, this));
  2678. break;
  2679. }
  2680. },
  2681. setRequestHeader: function setRequestHeader(header, value) {
  2682. verifyState(this);
  2683. if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
  2684. throw new Error("Refused to set unsafe header \"" + header + "\"");
  2685. }
  2686. if (this.requestHeaders[header]) {
  2687. this.requestHeaders[header] += "," + value;
  2688. } else {
  2689. this.requestHeaders[header] = value;
  2690. }
  2691. },
  2692. // Helps testing
  2693. setResponseHeaders: function setResponseHeaders(headers) {
  2694. this.responseHeaders = {};
  2695. for (var header in headers) {
  2696. if (headers.hasOwnProperty(header)) {
  2697. this.responseHeaders[header] = headers[header];
  2698. }
  2699. }
  2700. if (this.async) {
  2701. this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
  2702. } else {
  2703. this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
  2704. }
  2705. },
  2706. // Currently treats ALL data as a DOMString (i.e. no Document)
  2707. send: function send(data) {
  2708. verifyState(this);
  2709. if (!/^(get|head)$/i.test(this.method)) {
  2710. if (this.requestHeaders["Content-Type"]) {
  2711. var value = this.requestHeaders["Content-Type"].split(";");
  2712. this.requestHeaders["Content-Type"] = value[0] + ";charset=utf-8";
  2713. } else {
  2714. this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
  2715. }
  2716. this.requestBody = data;
  2717. }
  2718. this.errorFlag = false;
  2719. this.sendFlag = this.async;
  2720. this.readyStateChange(FakeXMLHttpRequest.OPENED);
  2721. if (typeof this.onSend == "function") {
  2722. this.onSend(this);
  2723. }
  2724. this.dispatchEvent(new sinon.Event("loadstart", false, false, this));
  2725. },
  2726. abort: function abort() {
  2727. this.aborted = true;
  2728. this.responseText = null;
  2729. this.errorFlag = true;
  2730. this.requestHeaders = {};
  2731. if (this.readyState > sinon.FakeXMLHttpRequest.UNSENT && this.sendFlag) {
  2732. this.readyStateChange(sinon.FakeXMLHttpRequest.DONE);
  2733. this.sendFlag = false;
  2734. }
  2735. this.readyState = sinon.FakeXMLHttpRequest.UNSENT;
  2736. this.dispatchEvent(new sinon.Event("abort", false, false, this));
  2737. if (typeof this.onerror === "function") {
  2738. this.onerror();
  2739. }
  2740. },
  2741. getResponseHeader: function getResponseHeader(header) {
  2742. if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
  2743. return null;
  2744. }
  2745. if (/^Set-Cookie2?$/i.test(header)) {
  2746. return null;
  2747. }
  2748. header = header.toLowerCase();
  2749. for (var h in this.responseHeaders) {
  2750. if (h.toLowerCase() == header) {
  2751. return this.responseHeaders[h];
  2752. }
  2753. }
  2754. return null;
  2755. },
  2756. getAllResponseHeaders: function getAllResponseHeaders() {
  2757. if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
  2758. return "";
  2759. }
  2760. var headers = "";
  2761. for (var header in this.responseHeaders) {
  2762. if (this.responseHeaders.hasOwnProperty(header) &&
  2763. !/^Set-Cookie2?$/i.test(header)) {
  2764. headers += header + ": " + this.responseHeaders[header] + "\r\n";
  2765. }
  2766. }
  2767. return headers;
  2768. },
  2769. setResponseBody: function setResponseBody(body) {
  2770. verifyRequestSent(this);
  2771. verifyHeadersReceived(this);
  2772. verifyResponseBodyType(body);
  2773. var chunkSize = this.chunkSize || 10;
  2774. var index = 0;
  2775. this.responseText = "";
  2776. do {
  2777. if (this.async) {
  2778. this.readyStateChange(FakeXMLHttpRequest.LOADING);
  2779. }
  2780. this.responseText += body.substring(index, index + chunkSize);
  2781. index += chunkSize;
  2782. } while (index < body.length);
  2783. var type = this.getResponseHeader("Content-Type");
  2784. if (this.responseText &&
  2785. (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
  2786. try {
  2787. this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText);
  2788. } catch (e) {
  2789. // Unable to parse XML - no biggie
  2790. }
  2791. }
  2792. if (this.async) {
  2793. this.readyStateChange(FakeXMLHttpRequest.DONE);
  2794. } else {
  2795. this.readyState = FakeXMLHttpRequest.DONE;
  2796. }
  2797. },
  2798. respond: function respond(status, headers, body) {
  2799. this.setResponseHeaders(headers || {});
  2800. this.status = typeof status == "number" ? status : 200;
  2801. this.statusText = FakeXMLHttpRequest.statusCodes[this.status];
  2802. this.setResponseBody(body || "");
  2803. if (typeof this.onload === "function"){
  2804. this.onload();
  2805. }
  2806. }
  2807. });
  2808. sinon.extend(FakeXMLHttpRequest, {
  2809. UNSENT: 0,
  2810. OPENED: 1,
  2811. HEADERS_RECEIVED: 2,
  2812. LOADING: 3,
  2813. DONE: 4
  2814. });
  2815. // Borrowed from JSpec
  2816. FakeXMLHttpRequest.parseXML = function parseXML(text) {
  2817. var xmlDoc;
  2818. if (typeof DOMParser != "undefined") {
  2819. var parser = new DOMParser();
  2820. xmlDoc = parser.parseFromString(text, "text/xml");
  2821. } else {
  2822. xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  2823. xmlDoc.async = "false";
  2824. xmlDoc.loadXML(text);
  2825. }
  2826. return xmlDoc;
  2827. };
  2828. FakeXMLHttpRequest.statusCodes = {
  2829. 100: "Continue",
  2830. 101: "Switching Protocols",
  2831. 200: "OK",
  2832. 201: "Created",
  2833. 202: "Accepted",
  2834. 203: "Non-Authoritative Information",
  2835. 204: "No Content",
  2836. 205: "Reset Content",
  2837. 206: "Partial Content",
  2838. 300: "Multiple Choice",
  2839. 301: "Moved Permanently",
  2840. 302: "Found",
  2841. 303: "See Other",
  2842. 304: "Not Modified",
  2843. 305: "Use Proxy",
  2844. 307: "Temporary Redirect",
  2845. 400: "Bad Request",
  2846. 401: "Unauthorized",
  2847. 402: "Payment Required",
  2848. 403: "Forbidden",
  2849. 404: "Not Found",
  2850. 405: "Method Not Allowed",
  2851. 406: "Not Acceptable",
  2852. 407: "Proxy Authentication Required",
  2853. 408: "Request Timeout",
  2854. 409: "Conflict",
  2855. 410: "Gone",
  2856. 411: "Length Required",
  2857. 412: "Precondition Failed",
  2858. 413: "Request Entity Too Large",
  2859. 414: "Request-URI Too Long",
  2860. 415: "Unsupported Media Type",
  2861. 416: "Requested Range Not Satisfiable",
  2862. 417: "Expectation Failed",
  2863. 422: "Unprocessable Entity",
  2864. 500: "Internal Server Error",
  2865. 501: "Not Implemented",
  2866. 502: "Bad Gateway",
  2867. 503: "Service Unavailable",
  2868. 504: "Gateway Timeout",
  2869. 505: "HTTP Version Not Supported"
  2870. };
  2871. sinon.useFakeXMLHttpRequest = function () {
  2872. sinon.FakeXMLHttpRequest.restore = function restore(keepOnCreate) {
  2873. if (xhr.supportsXHR) {
  2874. global.XMLHttpRequest = xhr.GlobalXMLHttpRequest;
  2875. }
  2876. if (xhr.supportsActiveX) {
  2877. global.ActiveXObject = xhr.GlobalActiveXObject;
  2878. }
  2879. delete sinon.FakeXMLHttpRequest.restore;
  2880. if (keepOnCreate !== true) {
  2881. delete sinon.FakeXMLHttpRequest.onCreate;
  2882. }
  2883. };
  2884. if (xhr.supportsXHR) {
  2885. global.XMLHttpRequest = sinon.FakeXMLHttpRequest;
  2886. }
  2887. if (xhr.supportsActiveX) {
  2888. global.ActiveXObject = function ActiveXObject(objId) {
  2889. if (objId == "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) {
  2890. return new sinon.FakeXMLHttpRequest();
  2891. }
  2892. return new xhr.GlobalActiveXObject(objId);
  2893. };
  2894. }
  2895. return sinon.FakeXMLHttpRequest;
  2896. };
  2897. sinon.FakeXMLHttpRequest = FakeXMLHttpRequest;
  2898. })(this);
  2899. if (typeof module == "object" && typeof require == "function") {
  2900. module.exports = sinon;
  2901. }
  2902. /**
  2903. * @depend fake_xml_http_request.js
  2904. */
  2905. /*jslint eqeqeq: false, onevar: false, regexp: false, plusplus: false*/
  2906. /*global module, require, window*/
  2907. /**
  2908. * The Sinon "server" mimics a web server that receives requests from
  2909. * sinon.FakeXMLHttpRequest and provides an API to respond to those requests,
  2910. * both synchronously and asynchronously. To respond synchronuously, canned
  2911. * answers have to be provided upfront.
  2912. *
  2913. * @author Christian Johansen (christian@cjohansen.no)
  2914. * @license BSD
  2915. *
  2916. * Copyright (c) 2010-2013 Christian Johansen
  2917. */
  2918. if (typeof sinon == "undefined") {
  2919. var sinon = {};
  2920. }
  2921. sinon.fakeServer = (function () {
  2922. var push = [].push;
  2923. function F() {}
  2924. function create(proto) {
  2925. F.prototype = proto;
  2926. return new F();
  2927. }
  2928. function responseArray(handler) {
  2929. var response = handler;
  2930. if (Object.prototype.toString.call(handler) != "[object Array]") {
  2931. response = [200, {}, handler];
  2932. }
  2933. if (typeof response[2] != "string") {
  2934. throw new TypeError("Fake server response body should be string, but was " +
  2935. typeof response[2]);
  2936. }
  2937. return response;
  2938. }
  2939. var wloc = typeof window !== "undefined" ? window.location : {};
  2940. var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
  2941. function matchOne(response, reqMethod, reqUrl) {
  2942. var rmeth = response.method;
  2943. var matchMethod = !rmeth || rmeth.toLowerCase() == reqMethod.toLowerCase();
  2944. var url = response.url;
  2945. var matchUrl = !url || url == reqUrl || (typeof url.test == "function" && url.test(reqUrl));
  2946. return matchMethod && matchUrl;
  2947. }
  2948. function match(response, request) {
  2949. var requestMethod = this.getHTTPMethod(request);
  2950. var requestUrl = request.url;
  2951. if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
  2952. requestUrl = requestUrl.replace(rCurrLoc, "");
  2953. }
  2954. if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
  2955. if (typeof response.response == "function") {
  2956. var ru = response.url;
  2957. var args = [request].concat(!ru ? [] : requestUrl.match(ru).slice(1));
  2958. return response.response.apply(response, args);
  2959. }
  2960. return true;
  2961. }
  2962. return false;
  2963. }
  2964. function log(response, request) {
  2965. var str;
  2966. str = "Request:\n" + sinon.format(request) + "\n\n";
  2967. str += "Response:\n" + sinon.format(response) + "\n\n";
  2968. sinon.log(str);
  2969. }
  2970. return {
  2971. create: function () {
  2972. var server = create(this);
  2973. this.xhr = sinon.useFakeXMLHttpRequest();
  2974. server.requests = [];
  2975. this.xhr.onCreate = function (xhrObj) {
  2976. server.addRequest(xhrObj);
  2977. };
  2978. return server;
  2979. },
  2980. addRequest: function addRequest(xhrObj) {
  2981. var server = this;
  2982. push.call(this.requests, xhrObj);
  2983. xhrObj.onSend = function () {
  2984. server.handleRequest(this);
  2985. };
  2986. if (this.autoRespond && !this.responding) {
  2987. setTimeout(function () {
  2988. server.responding = false;
  2989. server.respond();
  2990. }, this.autoRespondAfter || 10);
  2991. this.responding = true;
  2992. }
  2993. },
  2994. getHTTPMethod: function getHTTPMethod(request) {
  2995. if (this.fakeHTTPMethods && /post/i.test(request.method)) {
  2996. var matches = (request.requestBody || "").match(/_method=([^\b;]+)/);
  2997. return !!matches ? matches[1] : request.method;
  2998. }
  2999. return request.method;
  3000. },
  3001. handleRequest: function handleRequest(xhr) {
  3002. if (xhr.async) {
  3003. if (!this.queue) {
  3004. this.queue = [];
  3005. }
  3006. push.call(this.queue, xhr);
  3007. } else {
  3008. this.processRequest(xhr);
  3009. }
  3010. },
  3011. respondWith: function respondWith(method, url, body) {
  3012. if (arguments.length == 1 && typeof method != "function") {
  3013. this.response = responseArray(method);
  3014. return;
  3015. }
  3016. if (!this.responses) { this.responses = []; }
  3017. if (arguments.length == 1) {
  3018. body = method;
  3019. url = method = null;
  3020. }
  3021. if (arguments.length == 2) {
  3022. body = url;
  3023. url = method;
  3024. method = null;
  3025. }
  3026. push.call(this.responses, {
  3027. method: method,
  3028. url: url,
  3029. response: typeof body == "function" ? body : responseArray(body)
  3030. });
  3031. },
  3032. respond: function respond() {
  3033. if (arguments.length > 0) this.respondWith.apply(this, arguments);
  3034. var queue = this.queue || [];
  3035. var request;
  3036. while(request = queue.shift()) {
  3037. this.processRequest(request);
  3038. }
  3039. },
  3040. processRequest: function processRequest(request) {
  3041. try {
  3042. if (request.aborted) {
  3043. return;
  3044. }
  3045. var response = this.response || [404, {}, ""];
  3046. if (this.responses) {
  3047. for (var i = 0, l = this.responses.length; i < l; i++) {
  3048. if (match.call(this, this.responses[i], request)) {
  3049. response = this.responses[i].response;
  3050. break;
  3051. }
  3052. }
  3053. }
  3054. if (request.readyState != 4) {
  3055. log(response, request);
  3056. request.respond(response[0], response[1], response[2]);
  3057. }
  3058. } catch (e) {
  3059. sinon.logError("Fake server request processing", e);
  3060. }
  3061. },
  3062. restore: function restore() {
  3063. return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
  3064. }
  3065. };
  3066. }());
  3067. if (typeof module == "object" && typeof require == "function") {
  3068. module.exports = sinon;
  3069. }
  3070. /**
  3071. * @depend fake_server.js
  3072. * @depend fake_timers.js
  3073. */
  3074. /*jslint browser: true, eqeqeq: false, onevar: false*/
  3075. /*global sinon*/
  3076. /**
  3077. * Add-on for sinon.fakeServer that automatically handles a fake timer along with
  3078. * the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery
  3079. * 1.3.x, which does not use xhr object's onreadystatehandler at all - instead,
  3080. * it polls the object for completion with setInterval. Dispite the direct
  3081. * motivation, there is nothing jQuery-specific in this file, so it can be used
  3082. * in any environment where the ajax implementation depends on setInterval or
  3083. * setTimeout.
  3084. *
  3085. * @author Christian Johansen (christian@cjohansen.no)
  3086. * @license BSD
  3087. *
  3088. * Copyright (c) 2010-2013 Christian Johansen
  3089. */
  3090. (function () {
  3091. function Server() {}
  3092. Server.prototype = sinon.fakeServer;
  3093. sinon.fakeServerWithClock = new Server();
  3094. sinon.fakeServerWithClock.addRequest = function addRequest(xhr) {
  3095. if (xhr.async) {
  3096. if (typeof setTimeout.clock == "object") {
  3097. this.clock = setTimeout.clock;
  3098. } else {
  3099. this.clock = sinon.useFakeTimers();
  3100. this.resetClock = true;
  3101. }
  3102. if (!this.longestTimeout) {
  3103. var clockSetTimeout = this.clock.setTimeout;
  3104. var clockSetInterval = this.clock.setInterval;
  3105. var server = this;
  3106. this.clock.setTimeout = function (fn, timeout) {
  3107. server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
  3108. return clockSetTimeout.apply(this, arguments);
  3109. };
  3110. this.clock.setInterval = function (fn, timeout) {
  3111. server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
  3112. return clockSetInterval.apply(this, arguments);
  3113. };
  3114. }
  3115. }
  3116. return sinon.fakeServer.addRequest.call(this, xhr);
  3117. };
  3118. sinon.fakeServerWithClock.respond = function respond() {
  3119. var returnVal = sinon.fakeServer.respond.apply(this, arguments);
  3120. if (this.clock) {
  3121. this.clock.tick(this.longestTimeout || 0);
  3122. this.longestTimeout = 0;
  3123. if (this.resetClock) {
  3124. this.clock.restore();
  3125. this.resetClock = false;
  3126. }
  3127. }
  3128. return returnVal;
  3129. };
  3130. sinon.fakeServerWithClock.restore = function restore() {
  3131. if (this.clock) {
  3132. this.clock.restore();
  3133. }
  3134. return sinon.fakeServer.restore.apply(this, arguments);
  3135. };
  3136. }());
  3137. /**
  3138. * @depend ../sinon.js
  3139. * @depend collection.js
  3140. * @depend util/fake_timers.js
  3141. * @depend util/fake_server_with_clock.js
  3142. */
  3143. /*jslint eqeqeq: false, onevar: false, plusplus: false*/
  3144. /*global require, module*/
  3145. /**
  3146. * Manages fake collections as well as fake utilities such as Sinon's
  3147. * timers and fake XHR implementation in one convenient object.
  3148. *
  3149. * @author Christian Johansen (christian@cjohansen.no)
  3150. * @license BSD
  3151. *
  3152. * Copyright (c) 2010-2013 Christian Johansen
  3153. */
  3154. if (typeof module == "object" && typeof require == "function") {
  3155. var sinon = require("../sinon");
  3156. sinon.extend(sinon, require("./util/fake_timers"));
  3157. }
  3158. (function () {
  3159. var push = [].push;
  3160. function exposeValue(sandbox, config, key, value) {
  3161. if (!value) {
  3162. return;
  3163. }
  3164. if (config.injectInto) {
  3165. config.injectInto[key] = value;
  3166. } else {
  3167. push.call(sandbox.args, value);
  3168. }
  3169. }
  3170. function prepareSandboxFromConfig(config) {
  3171. var sandbox = sinon.create(sinon.sandbox);
  3172. if (config.useFakeServer) {
  3173. if (typeof config.useFakeServer == "object") {
  3174. sandbox.serverPrototype = config.useFakeServer;
  3175. }
  3176. sandbox.useFakeServer();
  3177. }
  3178. if (config.useFakeTimers) {
  3179. if (typeof config.useFakeTimers == "object") {
  3180. sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers);
  3181. } else {
  3182. sandbox.useFakeTimers();
  3183. }
  3184. }
  3185. return sandbox;
  3186. }
  3187. sinon.sandbox = sinon.extend(sinon.create(sinon.collection), {
  3188. useFakeTimers: function useFakeTimers() {
  3189. this.clock = sinon.useFakeTimers.apply(sinon, arguments);
  3190. return this.add(this.clock);
  3191. },
  3192. serverPrototype: sinon.fakeServer,
  3193. useFakeServer: function useFakeServer() {
  3194. var proto = this.serverPrototype || sinon.fakeServer;
  3195. if (!proto || !proto.create) {
  3196. return null;
  3197. }
  3198. this.server = proto.create();
  3199. return this.add(this.server);
  3200. },
  3201. inject: function (obj) {
  3202. sinon.collection.inject.call(this, obj);
  3203. if (this.clock) {
  3204. obj.clock = this.clock;
  3205. }
  3206. if (this.server) {
  3207. obj.server = this.server;
  3208. obj.requests = this.server.requests;
  3209. }
  3210. return obj;
  3211. },
  3212. create: function (config) {
  3213. if (!config) {
  3214. return sinon.create(sinon.sandbox);
  3215. }
  3216. var sandbox = prepareSandboxFromConfig(config);
  3217. sandbox.args = sandbox.args || [];
  3218. var prop, value, exposed = sandbox.inject({});
  3219. if (config.properties) {
  3220. for (var i = 0, l = config.properties.length; i < l; i++) {
  3221. prop = config.properties[i];
  3222. value = exposed[prop] || prop == "sandbox" && sandbox;
  3223. exposeValue(sandbox, config, prop, value);
  3224. }
  3225. } else {
  3226. exposeValue(sandbox, config, "sandbox", value);
  3227. }
  3228. return sandbox;
  3229. }
  3230. });
  3231. sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;
  3232. if (typeof module == "object" && typeof require == "function") {
  3233. module.exports = sinon.sandbox;
  3234. }
  3235. }());
  3236. /**
  3237. * @depend ../sinon.js
  3238. * @depend stub.js
  3239. * @depend mock.js
  3240. * @depend sandbox.js
  3241. */
  3242. /*jslint eqeqeq: false, onevar: false, forin: true, plusplus: false*/
  3243. /*global module, require, sinon*/
  3244. /**
  3245. * Test function, sandboxes fakes
  3246. *
  3247. * @author Christian Johansen (christian@cjohansen.no)
  3248. * @license BSD
  3249. *
  3250. * Copyright (c) 2010-2013 Christian Johansen
  3251. */
  3252. (function (sinon) {
  3253. var commonJSModule = typeof module == "object" && typeof require == "function";
  3254. if (!sinon && commonJSModule) {
  3255. sinon = require("../sinon");
  3256. }
  3257. if (!sinon) {
  3258. return;
  3259. }
  3260. function test(callback) {
  3261. var type = typeof callback;
  3262. if (type != "function") {
  3263. throw new TypeError("sinon.test needs to wrap a test function, got " + type);
  3264. }
  3265. return function () {
  3266. var config = sinon.getConfig(sinon.config);
  3267. config.injectInto = config.injectIntoThis && this || config.injectInto;
  3268. var sandbox = sinon.sandbox.create(config);
  3269. var exception, result;
  3270. var args = Array.prototype.slice.call(arguments).concat(sandbox.args);
  3271. try {
  3272. result = callback.apply(this, args);
  3273. } catch (e) {
  3274. exception = e;
  3275. }
  3276. if (typeof exception !== "undefined") {
  3277. sandbox.restore();
  3278. throw exception;
  3279. }
  3280. else {
  3281. sandbox.verifyAndRestore();
  3282. }
  3283. return result;
  3284. };
  3285. }
  3286. test.config = {
  3287. injectIntoThis: true,
  3288. injectInto: null,
  3289. properties: ["spy", "stub", "mock", "clock", "server", "requests"],
  3290. useFakeTimers: true,
  3291. useFakeServer: true
  3292. };
  3293. if (commonJSModule) {
  3294. module.exports = test;
  3295. } else {
  3296. sinon.test = test;
  3297. }
  3298. }(typeof sinon == "object" && sinon || null));
  3299. /**
  3300. * @depend ../sinon.js
  3301. * @depend test.js
  3302. */
  3303. /*jslint eqeqeq: false, onevar: false, eqeqeq: false*/
  3304. /*global module, require, sinon*/
  3305. /**
  3306. * Test case, sandboxes all test functions
  3307. *
  3308. * @author Christian Johansen (christian@cjohansen.no)
  3309. * @license BSD
  3310. *
  3311. * Copyright (c) 2010-2013 Christian Johansen
  3312. */
  3313. (function (sinon) {
  3314. var commonJSModule = typeof module == "object" && typeof require == "function";
  3315. if (!sinon && commonJSModule) {
  3316. sinon = require("../sinon");
  3317. }
  3318. if (!sinon || !Object.prototype.hasOwnProperty) {
  3319. return;
  3320. }
  3321. function createTest(property, setUp, tearDown) {
  3322. return function () {
  3323. if (setUp) {
  3324. setUp.apply(this, arguments);
  3325. }
  3326. var exception, result;
  3327. try {
  3328. result = property.apply(this, arguments);
  3329. } catch (e) {
  3330. exception = e;
  3331. }
  3332. if (tearDown) {
  3333. tearDown.apply(this, arguments);
  3334. }
  3335. if (exception) {
  3336. throw exception;
  3337. }
  3338. return result;
  3339. };
  3340. }
  3341. function testCase(tests, prefix) {
  3342. /*jsl:ignore*/
  3343. if (!tests || typeof tests != "object") {
  3344. throw new TypeError("sinon.testCase needs an object with test functions");
  3345. }
  3346. /*jsl:end*/
  3347. prefix = prefix || "test";
  3348. var rPrefix = new RegExp("^" + prefix);
  3349. var methods = {}, testName, property, method;
  3350. var setUp = tests.setUp;
  3351. var tearDown = tests.tearDown;
  3352. for (testName in tests) {
  3353. if (tests.hasOwnProperty(testName)) {
  3354. property = tests[testName];
  3355. if (/^(setUp|tearDown)$/.test(testName)) {
  3356. continue;
  3357. }
  3358. if (typeof property == "function" && rPrefix.test(testName)) {
  3359. method = property;
  3360. if (setUp || tearDown) {
  3361. method = createTest(property, setUp, tearDown);
  3362. }
  3363. methods[testName] = sinon.test(method);
  3364. } else {
  3365. methods[testName] = tests[testName];
  3366. }
  3367. }
  3368. }
  3369. return methods;
  3370. }
  3371. if (commonJSModule) {
  3372. module.exports = testCase;
  3373. } else {
  3374. sinon.testCase = testCase;
  3375. }
  3376. }(typeof sinon == "object" && sinon || null));
  3377. /**
  3378. * @depend ../sinon.js
  3379. * @depend stub.js
  3380. */
  3381. /*jslint eqeqeq: false, onevar: false, nomen: false, plusplus: false*/
  3382. /*global module, require, sinon*/
  3383. /**
  3384. * Assertions matching the test spy retrieval interface.
  3385. *
  3386. * @author Christian Johansen (christian@cjohansen.no)
  3387. * @license BSD
  3388. *
  3389. * Copyright (c) 2010-2013 Christian Johansen
  3390. */
  3391. (function (sinon, global) {
  3392. var commonJSModule = typeof module == "object" && typeof require == "function";
  3393. var slice = Array.prototype.slice;
  3394. var assert;
  3395. if (!sinon && commonJSModule) {
  3396. sinon = require("../sinon");
  3397. }
  3398. if (!sinon) {
  3399. return;
  3400. }
  3401. function verifyIsStub() {
  3402. var method;
  3403. for (var i = 0, l = arguments.length; i < l; ++i) {
  3404. method = arguments[i];
  3405. if (!method) {
  3406. assert.fail("fake is not a spy");
  3407. }
  3408. if (typeof method != "function") {
  3409. assert.fail(method + " is not a function");
  3410. }
  3411. if (typeof method.getCall != "function") {
  3412. assert.fail(method + " is not stubbed");
  3413. }
  3414. }
  3415. }
  3416. function failAssertion(object, msg) {
  3417. object = object || global;
  3418. var failMethod = object.fail || assert.fail;
  3419. failMethod.call(object, msg);
  3420. }
  3421. function mirrorPropAsAssertion(name, method, message) {
  3422. if (arguments.length == 2) {
  3423. message = method;
  3424. method = name;
  3425. }
  3426. assert[name] = function (fake) {
  3427. verifyIsStub(fake);
  3428. var args = slice.call(arguments, 1);
  3429. var failed = false;
  3430. if (typeof method == "function") {
  3431. failed = !method(fake);
  3432. } else {
  3433. failed = typeof fake[method] == "function" ?
  3434. !fake[method].apply(fake, args) : !fake[method];
  3435. }
  3436. if (failed) {
  3437. failAssertion(this, fake.printf.apply(fake, [message].concat(args)));
  3438. } else {
  3439. assert.pass(name);
  3440. }
  3441. };
  3442. }
  3443. function exposedName(prefix, prop) {
  3444. return !prefix || /^fail/.test(prop) ? prop :
  3445. prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1);
  3446. };
  3447. assert = {
  3448. failException: "AssertError",
  3449. fail: function fail(message) {
  3450. var error = new Error(message);
  3451. error.name = this.failException || assert.failException;
  3452. throw error;
  3453. },
  3454. pass: function pass(assertion) {},
  3455. callOrder: function assertCallOrder() {
  3456. verifyIsStub.apply(null, arguments);
  3457. var expected = "", actual = "";
  3458. if (!sinon.calledInOrder(arguments)) {
  3459. try {
  3460. expected = [].join.call(arguments, ", ");
  3461. var calls = slice.call(arguments);
  3462. var i = calls.length;
  3463. while (i) {
  3464. if (!calls[--i].called) {
  3465. calls.splice(i, 1);
  3466. }
  3467. }
  3468. actual = sinon.orderByFirstCall(calls).join(", ");
  3469. } catch (e) {
  3470. // If this fails, we'll just fall back to the blank string
  3471. }
  3472. failAssertion(this, "expected " + expected + " to be " +
  3473. "called in order but were called as " + actual);
  3474. } else {
  3475. assert.pass("callOrder");
  3476. }
  3477. },
  3478. callCount: function assertCallCount(method, count) {
  3479. verifyIsStub(method);
  3480. if (method.callCount != count) {
  3481. var msg = "expected %n to be called " + sinon.timesInWords(count) +
  3482. " but was called %c%C";
  3483. failAssertion(this, method.printf(msg));
  3484. } else {
  3485. assert.pass("callCount");
  3486. }
  3487. },
  3488. expose: function expose(target, options) {
  3489. if (!target) {
  3490. throw new TypeError("target is null or undefined");
  3491. }
  3492. var o = options || {};
  3493. var prefix = typeof o.prefix == "undefined" && "assert" || o.prefix;
  3494. var includeFail = typeof o.includeFail == "undefined" || !!o.includeFail;
  3495. for (var method in this) {
  3496. if (method != "export" && (includeFail || !/^(fail)/.test(method))) {
  3497. target[exposedName(prefix, method)] = this[method];
  3498. }
  3499. }
  3500. return target;
  3501. }
  3502. };
  3503. mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
  3504. mirrorPropAsAssertion("notCalled", function (spy) { return !spy.called; },
  3505. "expected %n to not have been called but was called %c%C");
  3506. mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
  3507. mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
  3508. mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
  3509. mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
  3510. mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
  3511. mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
  3512. mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
  3513. mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
  3514. mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
  3515. mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
  3516. mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C");
  3517. mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
  3518. mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
  3519. mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
  3520. mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
  3521. mirrorPropAsAssertion("threw", "%n did not throw exception%C");
  3522. mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
  3523. if (commonJSModule) {
  3524. module.exports = assert;
  3525. } else {
  3526. sinon.assert = assert;
  3527. }
  3528. }(typeof sinon == "object" && sinon || null, typeof window != "undefined" ? window : (typeof self != "undefined") ? self : global));
  3529. return sinon;}.call(typeof window != 'undefined' && window || {}));