You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4289 lines
130 KiB

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