Skip to content

Commit

Permalink
Merge pull request #1203 from jdgreenberger/add-expectation-diff-logs
Browse files Browse the repository at this point in the history
Add expectation diff logs
  • Loading branch information
fatso83 authored Dec 21, 2016
2 parents 1924a89 + 6f346b3 commit a0c77c0
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 49 deletions.
12 changes: 6 additions & 6 deletions lib/sinon/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ 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 %*%C");
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("calledWith", "expected %n to be called with arguments %D");
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
56 changes: 56 additions & 0 deletions lib/sinon/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Copyright (c) 2010-2013 Christian Johansen
*/
"use strict";
require("colors");

var extend = require("./extend");
var functionName = require("./util/core/function-name");
Expand All @@ -19,6 +20,7 @@ var timesInWords = require("./util/core/times-in-words");
var wrapMethod = require("./util/core/wrap-method");
var sinonFormat = require("./util/core/format");
var valueToString = require("./util/core/value-to-string");
var jsDiff = require("diff");

var push = Array.prototype.push;
var slice = Array.prototype.slice;
Expand Down Expand Up @@ -416,6 +418,32 @@ delegateToCalls("yieldToOn", false, "yieldToOn", function (property) {
"' since it was not yet invoked.");
});

function colorDiffText(diff) {
var objects = diff.map(function (part) {
var text = part.value;
if (part.added) {
text = text.green;
} else if (part.removed) {
text = text.red;
}
if (diff.length === 2) {
text += " "; // format simple diffs
}
return text;
});
return objects.join("");
}

function colorSinonMatchText(matcher, calledArg, calledArgMessage) {
if (!matcher.test(calledArg)) {
matcher.message = matcher.message.red;
if (calledArgMessage) {
calledArgMessage = calledArgMessage.green;
}
}
return calledArgMessage + " " + matcher.message;
}

spyApi.formatters = {
c: function (spyInstance) {
return timesInWords(spyInstance.callCount);
Expand All @@ -425,6 +453,34 @@ spyApi.formatters = {
return spyInstance.toString();
},

D: function (spyInstance, args) {
var message = "";

for (var i = 0, l = spyInstance.callCount; i < l; ++i) {
// describe multiple calls
if (l > 1) {
if (i > 0) {
message += "\n";
}
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]) : "";
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);
}
}
}

return message;
},

C: function (spyInstance) {
var calls = [];

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"eslint-pre-commit"
],
"dependencies": {
"colors": "^1.1.2",
"diff": "^3.1.0",
"formatio": "1.1.1",
"lolex": "^1.4.0",
"samsam": "^1.1.3",
Expand Down
Loading

0 comments on commit a0c77c0

Please sign in to comment.