Skip to content

Commit

Permalink
Convert remaining calledWith methods to use diff color formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfakerjohn committed Dec 21, 2016
1 parent 92f76fa commit 6f346b3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
11 changes: 6 additions & 5 deletions lib/sinon/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function mirrorPropAsAssertion(name, method, message) {
var failed = false;

verifyIsValidAssertion(name, args);

if (typeof method === "function") {
failed = !method(fake);
} else {
Expand Down Expand Up @@ -199,11 +200,11 @@ mirrorPropAsAssertion(
mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %D");
mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C");
mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %D");
mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %D");
mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %D");
mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %D");
mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %D");
mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
mirrorPropAsAssertion("threw", "%n did not throw exception%C");
Expand Down
4 changes: 2 additions & 2 deletions lib/sinon/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,16 +461,16 @@ spyApi.formatters = {
if (i > 0) {
message += "\n";
}
message += "Call " + i + ":";
message += "Call " + (i + 1) + ":";
}
var calledArgs = spyInstance.getCall(i).args;
for (var j = 0; j < calledArgs.length || j < args.length; ++j) {
message += "\n";
var calledArgMessage = j < calledArgs.length ? sinonFormat(calledArgs[j]) : "";
var expectedArgMessage = j < args.length ? sinonFormat(args[j]) : "";
if (sinonMatch.isMatcher(args[j])) {
message += colorSinonMatchText(args[j], calledArgs[j], calledArgMessage);
} else {
var expectedArgMessage = j < args.length ? sinonFormat(args[j]) : "";
var diff = jsDiff.diffJson(calledArgMessage, expectedArgMessage);
message += colorDiffText(diff);
}
Expand Down
45 changes: 31 additions & 14 deletions test/assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,11 +1375,11 @@ describe("assert", function () {

assert.equals(this.message("calledWith", this.obj.doSomething, 1, 3, "hey").replace(/ at.*/g, ""),
"expected doSomething to be called with arguments " +
"Call 0:\n" +
"Call 1:\n" +
"4".red + " " + "1".green + " \n" +
"3\n" +
"hey\n" +
"Call 1:\n" +
"Call 2:\n" +
"1\n" +
"3\n" +
"not".red + " " + "hey".green + " ");
Expand Down Expand Up @@ -1552,17 +1552,24 @@ describe("assert", function () {
this.obj.doSomething(1, 3, "hey");

assert.equals(this.message("calledWithMatch", this.obj.doSomething, 4, 3, "hey").replace(/ at.*/g, ""),
"expected doSomething to be called with match 4, 3, " +
"hey\n doSomething(1, 3, hey)");
"expected doSomething to be called with match \n" +
"1".red + " " + "4".green + " \n" +
"3\n" +
"hey");
});

it("assert.alwaysCalledWith exception message", function () {
this.obj.doSomething(1, 3, "hey");
this.obj.doSomething(1, "hey");

assert.equals(this.message("alwaysCalledWith", this.obj.doSomething, 1, "hey").replace(/ at.*/g, ""),
"expected doSomething to always be called with arguments 1" +
", hey\n doSomething(1, 3, hey)\n doSomething(1, hey)");
"expected doSomething to always be called with arguments Call 1:\n" +
"1\n" +
"3".red + " " + "hey".green + " \n" +
"hey".red + "\n" +
"Call 2:\n" +
"1\n" +
"hey");
});

it("assert.alwaysCalledWithMatch exception message", function () {
Expand All @@ -1571,27 +1578,37 @@ describe("assert", function () {

assert.equals(
this.message("alwaysCalledWithMatch", this.obj.doSomething, 1, "hey").replace(/ at.*/g, ""),
"expected doSomething to always be called with match 1" +
", hey\n doSomething(1, 3, hey)\n doSomething(1, hey)"
);
"expected doSomething to always be called with match Call 1:\n" +
"1\n" +
"3".red + " " + "hey".green + " \n" +
"hey".red + "\n" +
"Call 2:\n" +
"1\n" +
"hey");
});

it("assert.calledWithExactly exception message", function () {
this.obj.doSomething(1, 3, "hey");

assert.equals(this.message("calledWithExactly", this.obj.doSomething, 1, 3).replace(/ at.*/g, ""),
"expected doSomething to be called with exact arguments 1" +
", 3\n doSomething(1, 3, hey)");
"expected doSomething to be called with exact arguments \n" +
"1\n" +
"3\n" +
"hey".red);
});

it("assert.alwaysCalledWithExactly exception message", function () {
this.obj.doSomething(1, 3, "hey");
this.obj.doSomething(1, 3);

assert.equals(this.message("alwaysCalledWithExactly", this.obj.doSomething, 1, 3).replace(/ at.*/g, ""),
"expected doSomething to always be called with exact " +
"arguments 1, 3\n doSomething(1, 3, hey)\n " +
"doSomething(1, 3)");
"expected doSomething to always be called with exact arguments Call 1:\n" +
"1\n" +
"3\n" +
"hey".red + "\n" +
"Call 2:\n" +
"1\n" +
"3");
});

it("assert.neverCalledWith exception message", function () {
Expand Down

0 comments on commit 6f346b3

Please sign in to comment.