From ca40f32b251b51394b161cb5c2f31b552d8624b5 Mon Sep 17 00:00:00 2001 From: George Schneeloch Date: Thu, 10 Aug 2017 15:47:57 -0400 Subject: [PATCH] Add clearFields function to put logic clearing common fields in one place --- lib/sinon/default-behaviors.js | 64 ++++++++++++++++------------------ 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index 5c5568595..dec981655 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -6,7 +6,24 @@ var slice = [].slice; var useLeftMostCallback = -1; var useRightMostCallback = -2; +function clearFields(fake) { + fake.returnValue = undefined; + fake.resolve = false; + fake.resolveThis = false; + fake.reject = false; + fake.returnValueDefined = false; + fake.exception = undefined; + fake.exceptionCreator = undefined; + fake.fakeFn = undefined; + fake.callArgAt = undefined; + fake.callbackArguments = []; + fake.callbackContext = undefined; + fake.callArgProp = undefined; + fake.callbackAsync = false; +} + function throwsException(fake, error, message) { + clearFields(fake); if (typeof error === "function") { fake.exceptionCreator = error; } else if (typeof error === "string") { @@ -32,6 +49,7 @@ function isPropertyConfigurable(obj, propName) { module.exports = { callsFake: function callsFake(fake, fn) { + clearFields(fake); fake.fakeFn = fn; }, @@ -40,11 +58,9 @@ module.exports = { throw new TypeError("argument index is not number"); } + clearFields(fake); fake.callArgAt = pos; fake.callbackArguments = []; - fake.callbackContext = undefined; - fake.callArgProp = undefined; - fake.callbackAsync = false; }, callsArgOn: function callsArgOn(fake, pos, context) { @@ -52,11 +68,9 @@ module.exports = { throw new TypeError("argument index is not number"); } + clearFields(fake); fake.callArgAt = pos; - fake.callbackArguments = []; fake.callbackContext = context; - fake.callArgProp = undefined; - fake.callbackAsync = false; }, callsArgWith: function callsArgWith(fake, pos) { @@ -64,11 +78,9 @@ module.exports = { throw new TypeError("argument index is not number"); } + clearFields(fake); fake.callArgAt = pos; fake.callbackArguments = slice.call(arguments, 2); - fake.callbackContext = undefined; - fake.callArgProp = undefined; - fake.callbackAsync = false; }, callsArgOnWith: function callsArgWith(fake, pos, context) { @@ -76,11 +88,10 @@ module.exports = { throw new TypeError("argument index is not number"); } + clearFields(fake); fake.callArgAt = pos; fake.callbackArguments = slice.call(arguments, 3); fake.callbackContext = context; - fake.callArgProp = undefined; - fake.callbackAsync = false; }, usingPromise: function usingPromise(fake, promiseLibrary) { @@ -124,20 +135,15 @@ module.exports = { fake.callbackArguments = slice.call(arguments, 3); fake.callbackContext = context; fake.callArgProp = prop; - fake.callbackAsync = false; }, throws: throwsException, throwsException: throwsException, returns: function returns(fake, value) { + clearFields(fake); fake.returnValue = value; - fake.resolve = false; - fake.reject = false; fake.returnValueDefined = true; - fake.exception = undefined; - fake.exceptionCreator = undefined; - fake.fakeFn = undefined; }, returnsArg: function returnsArg(fake, pos) { @@ -145,6 +151,7 @@ module.exports = { throw new TypeError("argument index is not number"); } + clearFields(fake); fake.returnArgAt = pos; }, @@ -153,22 +160,20 @@ module.exports = { throw new TypeError("argument index is not number"); } + clearFields(fake); fake.throwArgAt = pos; }, returnsThis: function returnsThis(fake) { + clearFields(fake); fake.returnThis = true; }, resolves: function resolves(fake, value) { + clearFields(fake); fake.returnValue = value; fake.resolve = true; - fake.resolveThis = false; - fake.reject = false; fake.returnValueDefined = true; - fake.exception = undefined; - fake.exceptionCreator = undefined; - fake.fakeFn = undefined; }, rejects: function rejects(fake, error, message) { @@ -181,29 +186,22 @@ module.exports = { } else { reason = error; } + + clearFields(fake); fake.returnValue = reason; - fake.resolve = false; - fake.resolveThis = false; fake.reject = true; fake.returnValueDefined = true; - fake.exception = undefined; - fake.exceptionCreator = undefined; - fake.fakeFn = undefined; return fake; }, resolvesThis: function resolvesThis(fake) { - fake.returnValue = undefined; - fake.resolve = false; + clearFields(fake); fake.resolveThis = true; - fake.reject = false; - fake.returnValueDefined = false; - fake.exception = undefined; - fake.fakeFn = undefined; }, callThrough: function callThrough(fake) { + clearFields(fake); fake.callsThrough = true; },