sinon.js 124 KB

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