sinon-chai-2.4.0.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. (function (sinonChai) {
  2. "use strict";
  3. // Module systems magic dance.
  4. if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
  5. // NodeJS
  6. module.exports = sinonChai;
  7. } else if (typeof define === "function" && define.amd) {
  8. // AMD
  9. define(function () {
  10. return sinonChai;
  11. });
  12. } else {
  13. // Other environment (usually <script> tag): plug in to global chai instance directly.
  14. chai.use(sinonChai);
  15. }
  16. }(function sinonChai(chai, utils) {
  17. "use strict";
  18. var slice = Array.prototype.slice;
  19. function isSpy(putativeSpy) {
  20. return typeof putativeSpy === "function" &&
  21. typeof putativeSpy.getCall === "function" &&
  22. typeof putativeSpy.calledWithExactly === "function";
  23. }
  24. function isCall(putativeCall) {
  25. return putativeCall && isSpy(putativeCall.proxy);
  26. }
  27. function assertCanWorkWith(assertion) {
  28. if (!isSpy(assertion._obj) && !isCall(assertion._obj)) {
  29. throw new TypeError(utils.inspect(assertion._obj) + " is not a spy or a call to a spy!");
  30. }
  31. }
  32. function getMessages(spy, action, nonNegatedSuffix, always, args) {
  33. var verbPhrase = always ? "always have " : "have ";
  34. nonNegatedSuffix = nonNegatedSuffix || "";
  35. if (isSpy(spy.proxy)) {
  36. spy = spy.proxy;
  37. }
  38. function printfArray(array) {
  39. return spy.printf.apply(spy, array);
  40. }
  41. return {
  42. affirmative: printfArray(["expected %n to " + verbPhrase + action + nonNegatedSuffix].concat(args)),
  43. negative: printfArray(["expected %n to not " + verbPhrase + action].concat(args))
  44. };
  45. }
  46. function sinonProperty(name, action, nonNegatedSuffix) {
  47. utils.addProperty(chai.Assertion.prototype, name, function () {
  48. assertCanWorkWith(this);
  49. var messages = getMessages(this._obj, action, nonNegatedSuffix, false);
  50. this.assert(this._obj[name], messages.affirmative, messages.negative);
  51. });
  52. }
  53. function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) {
  54. return function () {
  55. assertCanWorkWith(this);
  56. var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1);
  57. var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[alwaysSinonMethod] === "function";
  58. var sinonMethod = shouldBeAlways ? alwaysSinonMethod : sinonName;
  59. var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments));
  60. this.assert(this._obj[sinonMethod].apply(this._obj, arguments), messages.affirmative, messages.negative);
  61. };
  62. }
  63. function sinonMethodAsProperty(name, action, nonNegatedSuffix) {
  64. var handler = createSinonMethodHandler(name, action, nonNegatedSuffix);
  65. utils.addProperty(chai.Assertion.prototype, name, handler);
  66. }
  67. function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffix) {
  68. var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix);
  69. utils.addMethod(chai.Assertion.prototype, chaiName, handler);
  70. }
  71. function sinonMethod(name, action, nonNegatedSuffix) {
  72. exceptionalSinonMethod(name, name, action, nonNegatedSuffix);
  73. }
  74. utils.addProperty(chai.Assertion.prototype, "always", function () {
  75. utils.flag(this, "always", true);
  76. });
  77. sinonProperty("called", "been called", " at least once, but it was never called");
  78. sinonProperty("calledOnce", "been called exactly once", ", but it was called %c%C");
  79. sinonProperty("calledTwice", "been called exactly twice", ", but it was called %c%C");
  80. sinonProperty("calledThrice", "been called exactly thrice", ", but it was called %c%C");
  81. sinonMethodAsProperty("calledWithNew", "been called with new");
  82. sinonMethod("calledBefore", "been called before %1");
  83. sinonMethod("calledAfter", "been called after %1");
  84. sinonMethod("calledOn", "been called with %1 as this", ", but it was called with %t instead");
  85. sinonMethod("calledWith", "been called with arguments %*", "%C");
  86. sinonMethod("calledWithExactly", "been called with exact arguments %*", "%C");
  87. sinonMethod("calledWithMatch", "been called with arguments matching %*", "%C");
  88. sinonMethod("returned", "returned %1");
  89. exceptionalSinonMethod("thrown", "threw", "thrown %1");
  90. }));