Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expectation diff logs #1203

Merged
merged 4 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only calledWith? Why not calledWithExactly and others?

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 @@ -415,6 +417,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 @@ -424,6 +452,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 @@ -29,6 +29,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