From b75fbfa9e57ba9b9c1b639b68646b1d054e0a7e3 Mon Sep 17 00:00:00 2001 From: Morgan Roderick Date: Mon, 28 Nov 2022 18:06:44 +0100 Subject: [PATCH] Fix 2448: remove custom formatter Remove option to pass a custom formatter. The sub libraries of Sinon has long moved on to use `util.inspect` from Node. By using that in Sinon itself, we align all the libraries. --- lib/sinon.js | 3 --- lib/sinon/assert.js | 8 ++++---- lib/sinon/mock-expectation.js | 18 +++++++++--------- lib/sinon/proxy-call.js | 6 +++--- lib/sinon/proxy.js | 4 ++-- lib/sinon/spy-formatters.js | 10 +++++----- 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/lib/sinon.js b/lib/sinon.js index 8f1dedc8c..19ffde756 100644 --- a/lib/sinon.js +++ b/lib/sinon.js @@ -4,7 +4,6 @@ var behavior = require("./sinon/behavior"); var createSandbox = require("./sinon/create-sandbox"); var extend = require("./sinon/util/core/extend"); var fakeTimers = require("./sinon/util/fake-timers"); -var format = require("./sinon/util/core/format"); var nise = require("nise"); var Sandbox = require("./sinon/sandbox"); var stub = require("./sinon/stub"); @@ -19,8 +18,6 @@ var apiMethods = { expectation: require("./sinon/mock-expectation"), defaultConfig: require("./sinon/util/core/default-config"), - setFormatter: format.setFormatter, - // fake timers timers: fakeTimers.timers, diff --git a/lib/sinon/assert.js b/lib/sinon/assert.js index 5af71bbb6..7d36b41c1 100644 --- a/lib/sinon/assert.js +++ b/lib/sinon/assert.js @@ -5,7 +5,7 @@ var calledInOrder = require("@sinonjs/commons").calledInOrder; var createMatcher = require("@sinonjs/samsam").createMatcher; var orderByFirstCall = require("@sinonjs/commons").orderByFirstCall; var timesInWords = require("./util/core/times-in-words"); -var format = require("./util/core/format"); +var inspect = require("util").inspect; var stringSlice = require("@sinonjs/commons").prototypes.string.slice; var globalObject = require("@sinonjs/commons").global; @@ -162,7 +162,7 @@ function createAssertObject() { var msg; if (typeof count !== "number") { msg = - `expected ${format(count)} to be a number ` + + `expected ${inspect(count)} to be a number ` + `but was of type ${typeof count}`; failAssertion(this, msg); } else if (method.callCount !== count) { @@ -206,8 +206,8 @@ function createAssertObject() { } else { var formatted = [ "expected value to match", - ` expected = ${format(expectation)}`, - ` actual = ${format(actual)}`, + ` expected = ${inspect(expectation)}`, + ` actual = ${inspect(actual)}`, ]; failAssertion(this, join(formatted, "\n")); diff --git a/lib/sinon/mock-expectation.js b/lib/sinon/mock-expectation.js index ffef5700b..bf9aeb584 100644 --- a/lib/sinon/mock-expectation.js +++ b/lib/sinon/mock-expectation.js @@ -9,7 +9,7 @@ var match = require("@sinonjs/samsam").createMatcher; var stub = require("./stub"); var assert = require("./assert"); var deepEqual = require("@sinonjs/samsam").deepEqual; -var format = require("./util/core/format"); +var inspect = require("util").inspect; var valueToString = require("@sinonjs/commons").valueToString; var every = arrayProto.every; @@ -166,7 +166,7 @@ var mockExpectation = { if (!args) { mockExpectation.fail( - `${this.method} received no arguments, expected ${format( + `${this.method} received no arguments, expected ${inspect( expectedArguments )}` ); @@ -174,9 +174,9 @@ var mockExpectation = { if (args.length < expectedArguments.length) { mockExpectation.fail( - `${this.method} received too few arguments (${format( + `${this.method} received too few arguments (${inspect( args - )}), expected ${format(expectedArguments)}` + )}), expected ${inspect(expectedArguments)}` ); } @@ -185,9 +185,9 @@ var mockExpectation = { args.length !== expectedArguments.length ) { mockExpectation.fail( - `${this.method} received too many arguments (${format( + `${this.method} received too many arguments (${inspect( args - )}), expected ${format(expectedArguments)}` + )}), expected ${inspect(expectedArguments)}` ); } @@ -196,7 +196,7 @@ var mockExpectation = { function (expectedArgument, i) { if (!verifyMatcher(expectedArgument, args[i])) { mockExpectation.fail( - `${this.method} received wrong arguments ${format( + `${this.method} received wrong arguments ${inspect( args )}, didn't match ${String(expectedArguments)}` ); @@ -204,9 +204,9 @@ var mockExpectation = { if (!deepEqual(args[i], expectedArgument)) { mockExpectation.fail( - `${this.method} received wrong arguments ${format( + `${this.method} received wrong arguments ${inspect( args - )}, expected ${format(expectedArguments)}` + )}, expected ${inspect(expectedArguments)}` ); } }, diff --git a/lib/sinon/proxy-call.js b/lib/sinon/proxy-call.js index 899579fc1..87c1df44f 100644 --- a/lib/sinon/proxy-call.js +++ b/lib/sinon/proxy-call.js @@ -4,7 +4,7 @@ var arrayProto = require("@sinonjs/commons").prototypes.array; var match = require("@sinonjs/samsam").createMatcher; var deepEqual = require("@sinonjs/samsam").deepEqual; var functionName = require("@sinonjs/commons").functionName; -var sinonFormat = require("./util/core/format"); +var inspect = require("util").inspect; var valueToString = require("@sinonjs/commons").valueToString; var concat = arrayProto.concat; @@ -208,13 +208,13 @@ var callProto = { } formattedArgs = map(this.args, function (arg) { - return sinonFormat(arg); + return inspect(arg); }); callStr = `${callStr + join(formattedArgs, ", ")})`; if (typeof this.returnValue !== "undefined") { - callStr += ` => ${sinonFormat(this.returnValue)}`; + callStr += ` => ${inspect(this.returnValue)}`; } if (this.exception) { diff --git a/lib/sinon/proxy.js b/lib/sinon/proxy.js index 82505c536..1627ff103 100644 --- a/lib/sinon/proxy.js +++ b/lib/sinon/proxy.js @@ -6,7 +6,7 @@ var functionToString = require("./util/core/function-to-string"); var proxyCall = require("./proxy-call"); var proxyCallUtil = require("./proxy-call-util"); var proxyInvoke = require("./proxy-invoke"); -var sinonFormat = require("./util/core/format"); +var inspect = require("util").inspect; var push = arrayProto.push; var forEach = arrayProto.forEach; @@ -126,7 +126,7 @@ var proxyApi = { if (typeof formatter === "function") { return String(formatter(spyInstance, args)); } else if (!isNaN(parseInt(specifier, 10))) { - return sinonFormat(args[specifier - 1]); + return inspect(args[specifier - 1]); } return `%${specifier}`; diff --git a/lib/sinon/spy-formatters.js b/lib/sinon/spy-formatters.js index 58685453d..f2ddce139 100644 --- a/lib/sinon/spy-formatters.js +++ b/lib/sinon/spy-formatters.js @@ -4,7 +4,7 @@ var arrayProto = require("@sinonjs/commons").prototypes.array; var color = require("./color"); var match = require("@sinonjs/samsam").createMatcher; var timesInWords = require("./util/core/times-in-words"); -var sinonFormat = require("./util/core/format"); +var inspect = require("util").inspect; var jsDiff = require("diff"); var join = arrayProto.join; @@ -85,7 +85,7 @@ module.exports = { message += "\n"; var calledArgMessage = - j < calledArgs.length ? sinonFormat(calledArg) : ""; + j < calledArgs.length ? inspect(calledArg) : ""; if (match.isMatcher(expectedArg)) { message += colorSinonMatchText( expectedArg, @@ -94,7 +94,7 @@ module.exports = { ); } else { var expectedArgMessage = - j < expectedArgs.length ? sinonFormat(expectedArg) : ""; + j < expectedArgs.length ? inspect(expectedArg) : ""; var diff = jsDiff.diffJson( calledArgMessage, expectedArgMessage @@ -126,7 +126,7 @@ module.exports = { var objects = []; for (var i = 0, l = spyInstance.callCount; i < l; ++i) { - push(objects, sinonFormat(spyInstance.thisValues[i])); + push(objects, inspect(spyInstance.thisValues[i])); } return join(objects, ", "); @@ -135,7 +135,7 @@ module.exports = { "*": function (spyInstance, args) { return join( map(args, function (arg) { - return sinonFormat(arg); + return inspect(arg); }), ", " );