Skip to content

Commit

Permalink
Use @sinonjs/formatio to format values
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni authored and mroderick committed Aug 9, 2018
1 parent f538ce7 commit d3d71a4
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 196 deletions.
4 changes: 2 additions & 2 deletions lib/assert-exception-unexpected-exception.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("assert.exception unexpected exception", function() {
referee.assert.match(
e.message,
"[assert.exception] Wow: Expected " +
"[object Object] but threw Error " +
'{ name: "TypeError" } but threw Error ' +
"(:()\nError: :(\n"
);
}
Expand All @@ -37,7 +37,7 @@ describe("assert.exception unexpected exception", function() {
referee.assert.match(
e.message,
"[assert.exception] Wow: Expected " +
"[object Object] but threw " +
'{ message: "Aww", name: "Error" } but threw ' +
"Error (:()\nError: :(\n"
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/assertions/contains.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module.exports = function(referee) {
return includes(haystack, needle);
},
assertMessage:
"${customMessage}Expected [${actual}] to contain ${expected}",
"${customMessage}Expected ${actual} to contain ${expected}",
refuteMessage:
"${customMessage}Expected [${actual}] not to contain ${expected}",
"${customMessage}Expected ${actual} not to contain ${expected}",
expectation: "toContain",
values: actualAndExpectedMessageValues
});
Expand Down
57 changes: 11 additions & 46 deletions lib/assertions/equals.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,19 @@
"use strict";

var samsam = require("samsam");
var interpolateProperties = require("../interpolate-properties");
var actualAndExpectedMessageValues = require("../actual-and-expected-message-values");

// Extract/replace with separate module that does a more detailed
// visualization of multi-line strings
function multiLineStringDiff(referee, actual, expected, message) {
if (actual === expected) {
return true;
function escapeNewlines(value) {
if (typeof value === "string") {
return value.replace(/\n/g, "\\n");
}

var heading = referee.assert.equals.multiLineStringHeading;
var failureText = interpolateProperties(referee, heading, {
customMessage: message
});
var actualLines = actual.split("\n");
var expectedLines = expected.split("\n");
var lineCount = Math.max(expectedLines.length, actualLines.length);
var lines = [];

for (var i = 0; i < lineCount; ++i) {
if (expectedLines[i] !== actualLines[i]) {
lines.push(
"line " +
(i + 1) +
": " +
(expectedLines[i] || "") +
"\nwas: " +
(actualLines[i] || "")
);
}
}

referee.fail("[assert.equals] " + failureText + lines.join("\n\n"));
return false;
return value;
}

module.exports = function(referee) {
referee.add("equals", {
// Uses arguments[2] because the function's .length is used to determine
// the minimum required number of arguments.
assert: function(actual, expected) {
if (
typeof actual === "string" &&
typeof expected === "string" &&
(actual.indexOf("\n") >= 0 || expected.indexOf("\n") >= 0)
) {
return multiLineStringDiff(
referee,
actual,
expected,
arguments[2]
);
}

return samsam.deepEqual(actual, expected);
},

Expand All @@ -67,7 +26,13 @@ module.exports = function(referee) {
refuteMessage:
"${customMessage}${actual} expected not to be equal to ${expected}",
expectation: "toEqual",
values: actualAndExpectedMessageValues
values: function(actual, expected, message) {
return {
actual: escapeNewlines(actual),
expected: escapeNewlines(expected),
customMessage: message
};
}
});

referee.assert.equals.multiLineStringHeading =
Expand Down
9 changes: 8 additions & 1 deletion lib/referee.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
"use strict";

var formatio = require("@sinonjs/formatio");
var assertArgNum = require("./assert-arg-num");
var interpolatePosArg = require("./interpolate-pos-arg");
var interpolateProperties = require("./interpolate-properties");
var expect = require("./expect");
var bane = require("bane");
var Promise = require("es6-promise").Promise;

// Setup formatter the same way as Sinon does:
var formatter = formatio.configure({
quoteStrings: false,
limitChildrenCount: 250
});

var slice = Array.prototype.slice;
var referee = bane.createEventEmitter();

Expand Down Expand Up @@ -198,7 +205,7 @@ referee.fail = function(message) {
};

referee.format = function(object) {
return String(object);
return formatter.ascii(object);
};

referee.prepareMessage = function msg(message) {
Expand Down
Loading

0 comments on commit d3d71a4

Please sign in to comment.