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 clearFields function to put logic clearing common fields in one place #1525

Closed
wants to merge 1 commit into from
Closed
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
64 changes: 31 additions & 33 deletions lib/sinon/default-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -32,6 +49,7 @@ function isPropertyConfigurable(obj, propName) {

module.exports = {
callsFake: function callsFake(fake, fn) {
clearFields(fake);
fake.fakeFn = fn;
},

Expand All @@ -40,47 +58,40 @@ 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) {
if (typeof pos !== "number") {
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) {
if (typeof pos !== "number") {
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) {
if (typeof pos !== "number") {
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) {
Expand Down Expand Up @@ -124,27 +135,23 @@ 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) {
if (typeof pos !== "number") {
throw new TypeError("argument index is not number");
}

clearFields(fake);
fake.returnArgAt = pos;
},

Expand All @@ -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) {
Expand All @@ -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;
},

Expand Down