Skip to content

Commit

Permalink
Bring calledWith messages more in line with Sinon's output (#1)
Browse files Browse the repository at this point in the history
This changes the nonNegatedSuffix parameter to instead take a pair of suffixes, nonNegated and negated. This allows us to specify %D for the nonNegated case, which eliminates the noisy duplication, while still using %*%C for the negated case. This matches assert.js in core Sinon as best I can tell.
  • Loading branch information
cincodenada authored Apr 13, 2022
1 parent 74a1bc0 commit 4c6d391
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 130 deletions.
52 changes: 32 additions & 20 deletions lib/sinon-chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,21 @@
}
}

function getMessages(spy, action, nonNegatedSuffix, always, args) {
function getMessages(spy, action, suffixes, always, args) {
var verbPhrase = always ? "always have " : "have ";
nonNegatedSuffix = nonNegatedSuffix || "";

// Suffixes is [nonNegatedSuffix, negatedSuffix], or just
// nonNegatedSuffix if there is no negatedSuffix
var negatedSuffix;
var nonNegatedSuffix;
if (Array.isArray(suffixes)) {
nonNegatedSuffix = suffixes[0] || "";
negatedSuffix = suffixes[1] || "";
} else {
nonNegatedSuffix = suffixes || "";
negatedSuffix = "";
}

if (isSpy(spy.proxy)) {
spy = spy.proxy;
}
Expand All @@ -70,38 +82,38 @@
return printfArray(["expected %n to " + verbPhrase + action + nonNegatedSuffix].concat(args));
},
negative: function () {
return printfArray(["expected %n to not " + verbPhrase + action].concat(args));
return printfArray(["expected %n to not " + verbPhrase + action + negatedSuffix].concat(args));
}
};
}

function sinonProperty(name, action, nonNegatedSuffix) {
function sinonProperty(name, action, suffixes) {
utils.addProperty(chai.Assertion.prototype, name, function () {
assertCanWorkWith(this);

var messages = getMessages(this._obj, action, nonNegatedSuffix, false);
var messages = getMessages(this._obj, action, suffixes, false);
this.assert(this._obj[name], messages.affirmative, messages.negative);
});
}

function sinonPropertyAsBooleanMethod(name, action, nonNegatedSuffix) {
function sinonPropertyAsBooleanMethod(name, action, suffixes) {
utils.addMethod(chai.Assertion.prototype, name, function (arg) {
assertCanWorkWith(this);

var messages = getMessages(this._obj, action, nonNegatedSuffix, false, [timesInWords(arg)]);
var messages = getMessages(this._obj, action, suffixes, false, [timesInWords(arg)]);
this.assert(this._obj[name] === arg, messages.affirmative, messages.negative);
});
}

function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) {
function createSinonMethodHandler(sinonName, action, suffixes) {
return function () {
assertCanWorkWith(this);

var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1);
var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[alwaysSinonMethod] === "function";
var sinonMethodName = shouldBeAlways ? alwaysSinonMethod : sinonName;

var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments));
var messages = getMessages(this._obj, action, suffixes, shouldBeAlways, slice.call(arguments));
this.assert(
this._obj[sinonMethodName].apply(this._obj, arguments),
messages.affirmative,
Expand All @@ -110,18 +122,18 @@
};
}

function sinonMethodAsProperty(name, action, nonNegatedSuffix) {
var handler = createSinonMethodHandler(name, action, nonNegatedSuffix);
function sinonMethodAsProperty(name, action, suffixes) {
var handler = createSinonMethodHandler(name, action, suffixes);
utils.addProperty(chai.Assertion.prototype, name, handler);
}

function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffix) {
var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix);
function exceptionalSinonMethod(chaiName, sinonName, action, suffixes) {
var handler = createSinonMethodHandler(sinonName, action, suffixes);
utils.addMethod(chai.Assertion.prototype, chaiName, handler);
}

function sinonMethod(name, action, nonNegatedSuffix) {
exceptionalSinonMethod(name, name, action, nonNegatedSuffix);
function sinonMethod(name, action, suffixes) {
exceptionalSinonMethod(name, name, action, suffixes);
}

utils.addProperty(chai.Assertion.prototype, "always", function () {
Expand All @@ -139,11 +151,11 @@
sinonMethod("calledImmediatelyBefore", "been called immediately before %1");
sinonMethod("calledImmediatelyAfter", "been called immediately after %1");
sinonMethod("calledOn", "been called with %1 as this", ", but it was called with %t instead");
sinonMethod("calledWith", "been called with arguments %*", "%D");
sinonMethod("calledOnceWith", "been called exactly once with arguments %*", "%D");
sinonMethod("calledWithExactly", "been called with exact arguments %*", "%D");
sinonMethod("calledOnceWithExactly", "been called exactly once with exact arguments %*", "%D");
sinonMethod("calledWithMatch", "been called with arguments matching %*", "%D");
sinonMethod("calledWith", "been called with arguments ", ["%D", "%*%C"]);
sinonMethod("calledOnceWith", "been called exactly once with arguments ", ["%D", "%*%C"]);
sinonMethod("calledWithExactly", "been called with exact arguments ", ["%D", "%*%C"]);
sinonMethod("calledOnceWithExactly", "been called exactly once with exact arguments ", ["%D", "%*%C"]);
sinonMethod("calledWithMatch", "been called with arguments matching ", ["%D", "%*%C"]);
sinonMethod("returned", "returned %1");
exceptionalSinonMethod("thrown", "threw", "thrown %1");
}));
116 changes: 103 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"opener": "^1.5.1",
"sinon": "^9.0.0"
"sinon": "^9.0.0",
"strip-ansi": "^6.0.0"
}
}
Loading

0 comments on commit 4c6d391

Please sign in to comment.