| OLD | NEW |
| (Empty) | |
| 1 (function (sinonChai) { |
| 2 "use strict"; |
| 3 |
| 4 // Module systems magic dance. |
| 5 |
| 6 /* istanbul ignore else */ |
| 7 if (typeof require === "function" && typeof exports === "object" && typeof m
odule === "object") { |
| 8 // NodeJS |
| 9 module.exports = sinonChai; |
| 10 } else if (typeof define === "function" && define.amd) { |
| 11 // AMD |
| 12 define(function () { |
| 13 return sinonChai; |
| 14 }); |
| 15 } else { |
| 16 // Other environment (usually <script> tag): plug in to global chai inst
ance directly. |
| 17 chai.use(sinonChai); |
| 18 } |
| 19 }(function sinonChai(chai, utils) { |
| 20 "use strict"; |
| 21 |
| 22 var slice = Array.prototype.slice; |
| 23 |
| 24 function isSpy(putativeSpy) { |
| 25 return typeof putativeSpy === "function" && |
| 26 typeof putativeSpy.getCall === "function" && |
| 27 typeof putativeSpy.calledWithExactly === "function"; |
| 28 } |
| 29 |
| 30 function timesInWords(count) { |
| 31 return count === 1 ? "once" : |
| 32 count === 2 ? "twice" : |
| 33 count === 3 ? "thrice" : |
| 34 (count || 0) + " times"; |
| 35 } |
| 36 |
| 37 function isCall(putativeCall) { |
| 38 return putativeCall && isSpy(putativeCall.proxy); |
| 39 } |
| 40 |
| 41 function assertCanWorkWith(assertion) { |
| 42 if (!isSpy(assertion._obj) && !isCall(assertion._obj)) { |
| 43 throw new TypeError(utils.inspect(assertion._obj) + " is not a spy o
r a call to a spy!"); |
| 44 } |
| 45 } |
| 46 |
| 47 function getMessages(spy, action, nonNegatedSuffix, always, args) { |
| 48 var verbPhrase = always ? "always have " : "have "; |
| 49 nonNegatedSuffix = nonNegatedSuffix || ""; |
| 50 if (isSpy(spy.proxy)) { |
| 51 spy = spy.proxy; |
| 52 } |
| 53 |
| 54 function printfArray(array) { |
| 55 return spy.printf.apply(spy, array); |
| 56 } |
| 57 |
| 58 return { |
| 59 affirmative: function () { |
| 60 return printfArray(["expected %n to " + verbPhrase + action + no
nNegatedSuffix].concat(args)); |
| 61 }, |
| 62 negative: function () { |
| 63 return printfArray(["expected %n to not " + verbPhrase + action]
.concat(args)); |
| 64 } |
| 65 }; |
| 66 } |
| 67 |
| 68 function sinonProperty(name, action, nonNegatedSuffix) { |
| 69 utils.addProperty(chai.Assertion.prototype, name, function () { |
| 70 assertCanWorkWith(this); |
| 71 |
| 72 var messages = getMessages(this._obj, action, nonNegatedSuffix, fals
e); |
| 73 this.assert(this._obj[name], messages.affirmative, messages.negative
); |
| 74 }); |
| 75 } |
| 76 |
| 77 function sinonPropertyAsBooleanMethod(name, action, nonNegatedSuffix) { |
| 78 utils.addMethod(chai.Assertion.prototype, name, function (arg) { |
| 79 assertCanWorkWith(this); |
| 80 |
| 81 var messages = getMessages(this._obj, action, nonNegatedSuffix, fals
e, [timesInWords(arg)]); |
| 82 this.assert(this._obj[name] === arg, messages.affirmative, messages.
negative); |
| 83 }); |
| 84 } |
| 85 |
| 86 function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) { |
| 87 return function () { |
| 88 assertCanWorkWith(this); |
| 89 |
| 90 var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sino
nName.substring(1); |
| 91 var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[
alwaysSinonMethod] === "function"; |
| 92 var sinonMethod = shouldBeAlways ? alwaysSinonMethod : sinonName; |
| 93 |
| 94 var messages = getMessages(this._obj, action, nonNegatedSuffix, shou
ldBeAlways, slice.call(arguments)); |
| 95 this.assert(this._obj[sinonMethod].apply(this._obj, arguments), mess
ages.affirmative, messages.negative); |
| 96 }; |
| 97 } |
| 98 |
| 99 function sinonMethodAsProperty(name, action, nonNegatedSuffix) { |
| 100 var handler = createSinonMethodHandler(name, action, nonNegatedSuffix); |
| 101 utils.addProperty(chai.Assertion.prototype, name, handler); |
| 102 } |
| 103 |
| 104 function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffi
x) { |
| 105 var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuff
ix); |
| 106 utils.addMethod(chai.Assertion.prototype, chaiName, handler); |
| 107 } |
| 108 |
| 109 function sinonMethod(name, action, nonNegatedSuffix) { |
| 110 exceptionalSinonMethod(name, name, action, nonNegatedSuffix); |
| 111 } |
| 112 |
| 113 utils.addProperty(chai.Assertion.prototype, "always", function () { |
| 114 utils.flag(this, "always", true); |
| 115 }); |
| 116 |
| 117 sinonProperty("called", "been called", " at least once, but it was never cal
led"); |
| 118 sinonPropertyAsBooleanMethod("callCount", "been called exactly %1", ", but i
t was called %c%C"); |
| 119 sinonProperty("calledOnce", "been called exactly once", ", but it was called
%c%C"); |
| 120 sinonProperty("calledTwice", "been called exactly twice", ", but it was call
ed %c%C"); |
| 121 sinonProperty("calledThrice", "been called exactly thrice", ", but it was ca
lled %c%C"); |
| 122 sinonMethodAsProperty("calledWithNew", "been called with new"); |
| 123 sinonMethod("calledBefore", "been called before %1"); |
| 124 sinonMethod("calledAfter", "been called after %1"); |
| 125 sinonMethod("calledImmediatelyBefore", "been called immediately before %1"); |
| 126 sinonMethod("calledImmediatelyAfter", "been called immediately after %1"); |
| 127 sinonMethod("calledOn", "been called with %1 as this", ", but it was called
with %t instead"); |
| 128 sinonMethod("calledWith", "been called with arguments %*", "%C"); |
| 129 sinonMethod("calledWithExactly", "been called with exact arguments %*", "%C"
); |
| 130 sinonMethod("calledWithMatch", "been called with arguments matching %*", "%C
"); |
| 131 sinonMethod("returned", "returned %1"); |
| 132 exceptionalSinonMethod("thrown", "threw", "thrown %1"); |
| 133 })); |
| OLD | NEW |