Skip to content

Commit

Permalink
Merge pull request sinonjs#1826 from fearphage/refactor-native-calls
Browse files Browse the repository at this point in the history
Refactored native calls
  • Loading branch information
fearphage authored Jul 28, 2018
2 parents 79ed8ba + 6f0f843 commit 15ce1f9
Show file tree
Hide file tree
Showing 30 changed files with 347 additions and 216 deletions.
34 changes: 21 additions & 13 deletions lib/sinon/assert.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
"use strict";

var arrayProto = require("./var/array");
var calledInOrder = require("./util/core/called-in-order");
var orderByFirstCall = require("./util/core/order-by-first-call");
var timesInWords = require("./util/core/times-in-words");
var format = require("./util/core/format");
var sinonMatch = require("./match");
var stringSlice = require("./var/string").slice;

var slice = Array.prototype.slice;
var arraySlice = arrayProto.slice;
var concat = arrayProto.concat;
var forEach = arrayProto.forEach;
var join = arrayProto.join;
var splice = arrayProto.splice;

var assert;

function verifyIsStub() {
var args = Array.prototype.slice.call(arguments);
var args = arraySlice(arguments);

args.forEach(function (method) {
forEach(args, function (method) {
if (!method) {
assert.fail("fake is not a spy");
}
Expand Down Expand Up @@ -64,7 +70,7 @@ function mirrorPropAsAssertion(name, method, message) {
assert[name] = function (fake) {
verifyIsStub(fake);

var args = slice.call(arguments, 1);
var args = arraySlice(arguments, 1);
var failed = false;

verifyIsValidAssertion(name, args);
Expand All @@ -77,16 +83,18 @@ function mirrorPropAsAssertion(name, method, message) {
}

if (failed) {
failAssertion(this, (fake.printf || fake.proxy.printf).apply(fake, [message].concat(args)));
failAssertion(this, (fake.printf || fake.proxy.printf).apply(fake, concat([message], args)));
} else {
assert.pass(name);
}
};
}

function exposedName(prefix, prop) {
return !prefix || /^fail/.test(prop) ? prop :
prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1);
return !prefix || /^fail/.test(prop)
? prop
: prefix + stringSlice(prop, 0, 1).toUpperCase() + stringSlice(prop, 1)
;
}

assert = {
Expand All @@ -108,15 +116,15 @@ assert = {

if (!calledInOrder(arguments)) {
try {
expected = [].join.call(arguments, ", ");
var calls = slice.call(arguments);
expected = join(arguments, ", ");
var calls = arraySlice(arguments);
var i = calls.length;
while (i) {
if (!calls[--i].called) {
calls.splice(i, 1);
splice(calls, i, 1);
}
}
actual = orderByFirstCall(calls).join(", ");
actual = join(orderByFirstCall(calls), ", ");
} catch (e) {
// If this fails, we'll just fall back to the blank string
}
Expand Down Expand Up @@ -150,7 +158,7 @@ assert = {
var includeFail = typeof o.includeFail === "undefined" || !!o.includeFail;
var instance = this;

Object.keys(instance).forEach(function (method) {
forEach(Object.keys(instance), function (method) {
if (method !== "expose" && (includeFail || !/^(fail)/.test(method))) {
target[exposedName(prefix, method)] = instance[method];
}
Expand All @@ -170,7 +178,7 @@ assert = {
" actual = " + format(actual)
];

failAssertion(this, formatted.join("\n"));
failAssertion(this, join(formatted, "\n"));
}
}
};
Expand Down
17 changes: 11 additions & 6 deletions lib/sinon/behavior.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
"use strict";

var arrayProto = require("./var/array");
var extend = require("./util/core/extend");
var functionName = require("./util/core/function-name");
var nextTick = require("./util/core/next-tick");
var valueToString = require("./util/core/value-to-string");

var slice = Array.prototype.slice;
var join = Array.prototype.join;
var concat = arrayProto.concat;
var forEach = arrayProto.forEach;
var join = arrayProto.join;
var reverse = arrayProto.reverse;
var slice = arrayProto.slice;

var useLeftMostCallback = -1;
var useRightMostCallback = -2;

Expand All @@ -24,7 +29,7 @@ function getCallback(behavior, args) {
}

if (callArgAt === useRightMostCallback) {
argumentList = slice.call(args).reverse();
argumentList = reverse(slice(args));
}

var callArgProp = behavior.callArgProp;
Expand Down Expand Up @@ -57,7 +62,7 @@ function getCallbackError(behavior, func, args) {
}

if (args.length > 0) {
msg += " Received [" + join.call(args, ", ") + "]";
msg += " Received [" + join(args, ", ") + "]";
}

return msg;
Expand Down Expand Up @@ -209,7 +214,7 @@ function createAsyncVersion(syncFnName) {
}

// create asynchronous versions of callsArg* and yields* methods
Object.keys(proto).forEach(function (method) {
forEach(Object.keys(proto), function (method) {
// need to avoid creating anotherasync versions of the newly added async methods
if (method.match(/^(callsArg|yields)/) && !method.match(/Async/)) {
proto[method + "Async"] = createAsyncVersion(method);
Expand All @@ -226,7 +231,7 @@ function createBehavior(behaviorMethod) {

function addBehavior(stub, name, fn) {
proto[name] = function () {
fn.apply(this, [this].concat([].slice.call(arguments)));
fn.apply(this, concat([this], slice(arguments)));
return this.stub || this;
};

Expand Down
46 changes: 26 additions & 20 deletions lib/sinon/call.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
"use strict";

var arrayProto = require("./var/array");
var sinonMatch = require("./match");
var deepEqual = require("./util/core/deep-equal").use(sinonMatch);
var functionName = require("./util/core/function-name");
var sinonFormat = require("./util/core/format");
var valueToString = require("./util/core/value-to-string");
var slice = Array.prototype.slice;
var filter = Array.prototype.filter;

var concat = arrayProto.concat;
var filter = arrayProto.filter;
var join = arrayProto.join;
var map = arrayProto.map;
var reduce = arrayProto.reduce;
var slice = arrayProto.slice;

function throwYieldError(proxy, text, args) {
var msg = functionName(proxy) + text;
if (args.length) {
msg += " Received [" + slice.call(args).join(", ") + "]";
msg += " Received [" + join(slice(args), ", ") + "]";
}
throw new Error(msg);
}
Expand All @@ -26,26 +32,26 @@ var callProto = {

calledWith: function calledWith() {
var self = this;
var calledWithArgs = slice.call(arguments);
var calledWithArgs = slice(arguments);

if (calledWithArgs.length > self.args.length) {
return false;
}

return calledWithArgs.reduce(function (prev, arg, i) {
return reduce(calledWithArgs, function (prev, arg, i) {
return prev && deepEqual(arg, self.args[i]);
}, true);
},

calledWithMatch: function calledWithMatch() {
var self = this;
var calledWithMatchArgs = slice.call(arguments);
var calledWithMatchArgs = slice(arguments);

if (calledWithMatchArgs.length > self.args.length) {
return false;
}

return calledWithMatchArgs.reduce(function (prev, expectation, i) {
return reduce(calledWithMatchArgs, function (prev, expectation, i) {
var actual = self.args[i];

return prev && (sinonMatch && sinonMatch(expectation).test(actual));
Expand Down Expand Up @@ -108,12 +114,12 @@ var callProto = {
},

callArgWith: function (pos) {
return this.callArgOnWith.apply(this, [pos, null].concat(slice.call(arguments, 1)));
return this.callArgOnWith.apply(this, concat([pos, null], slice(arguments, 1)));
},

callArgOnWith: function (pos, thisValue) {
this.ensureArgIsAFunction(pos);
var args = slice.call(arguments, 2);
var args = slice(arguments, 2);
return this.args[pos].apply(thisValue, args);
},

Expand All @@ -130,29 +136,29 @@ var callProto = {
},

yield: function () {
return this.yieldOn.apply(this, [null].concat(slice.call(arguments, 0)));
return this.yieldOn.apply(this, concat([null], slice(arguments, 0)));
},

yieldOn: function (thisValue) {
var args = slice.call(this.args);
var yieldFn = filter.call(args, function (arg) {
var args = slice(this.args);
var yieldFn = filter(args, function (arg) {
return typeof arg === "function";
})[0];

if (!yieldFn) {
throwYieldError(this.proxy, " cannot yield since no callback was passed.", args);
}

return yieldFn.apply(thisValue, slice.call(arguments, 1));
return yieldFn.apply(thisValue, slice(arguments, 1));
},

yieldTo: function (prop) {
return this.yieldToOn.apply(this, [prop, null].concat(slice.call(arguments, 1)));
return this.yieldToOn.apply(this, concat([prop, null], slice(arguments, 1)));
},

yieldToOn: function (prop, thisValue) {
var args = slice.call(this.args);
var yieldArg = filter.call(args, function (arg) {
var args = slice(this.args);
var yieldArg = filter(args, function (arg) {
return arg && typeof arg[prop] === "function";
})[0];
var yieldFn = yieldArg && yieldArg[prop];
Expand All @@ -162,22 +168,22 @@ var callProto = {
"' since no callback was passed.", args);
}

return yieldFn.apply(thisValue, slice.call(arguments, 2));
return yieldFn.apply(thisValue, slice(arguments, 2));
},

toString: function () {
var callStr = this.proxy ? this.proxy.toString() + "(" : "";
var callStr = this.proxy ? String(this.proxy) + "(" : "";
var formattedArgs;

if (!this.args) {
return ":(";
}

formattedArgs = slice.call(this.args).map(function (arg) {
formattedArgs = map(this.args, function (arg) {
return sinonFormat(arg);
});

callStr = callStr + formattedArgs.join(", ") + ")";
callStr = callStr + join(formattedArgs, ", ") + ")";

if (typeof this.returnValue !== "undefined") {
callStr += " => " + sinonFormat(this.returnValue);
Expand Down
6 changes: 4 additions & 2 deletions lib/sinon/collect-own-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

var walk = require("./util/core/walk");
var getPropertyDescriptor = require("./util/core/get-property-descriptor");
var hasOwnProperty = require("./var/object").hasOwnProperty;
var push = require("./var/array").push;

function collectMethod(methods, object, prop, propOwner) {
if (
typeof getPropertyDescriptor(propOwner, prop).value === "function" &&
object.hasOwnProperty(prop)
hasOwnProperty(object, prop)
) {
methods.push(object[prop]);
push(methods, object[prop]);
}
}

Expand Down
11 changes: 7 additions & 4 deletions lib/sinon/create-sandbox.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"use strict";

var arrayProto = require("./var/array");
var Sandbox = require("./sandbox");
var push = [].push;

var forEach = arrayProto.forEach;
var push = arrayProto.push;

function prepareSandboxFromConfig(config) {
var sandbox = new Sandbox();
Expand Down Expand Up @@ -32,9 +35,9 @@ function exposeValue(sandbox, config, key, value) {

if (config.injectInto && !(key in config.injectInto)) {
config.injectInto[key] = value;
sandbox.injectedKeys.push(key);
push(sandbox.injectedKeys, key);
} else {
push.call(sandbox.args, value);
push(sandbox.args, value);
}
}

Expand All @@ -50,7 +53,7 @@ function createSandbox(config) {
var exposed = configuredSandbox.inject({});

if (config.properties) {
config.properties.forEach(function (prop) {
forEach(config.properties, function (prop) {
var value = exposed[prop] || prop === "sandbox" && configuredSandbox;
exposeValue(configuredSandbox, config, prop, value);
});
Expand Down
Loading

0 comments on commit 15ce1f9

Please sign in to comment.