Skip to content

Commit

Permalink
Move "fakes" property from proxy to spy and stub
Browse files Browse the repository at this point in the history
Implement a base `matchingFakes` function and move the actual
implementation into `spy.js`.
  • Loading branch information
mantoni committed Oct 19, 2019
1 parent 30bf0e9 commit cd4f6d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/sinon/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var proxyInvoke = require("./proxy-invoke");
var push = arrayProto.push;
var forEach = arrayProto.forEach;
var slice = arrayProto.slice;
var filter = Array.prototype.filter;

var emptyFakes = Object.freeze([]);

// Public API
var proxyApi = {
Expand All @@ -30,10 +31,12 @@ var proxyApi = {

invoke: proxyInvoke,

matchingFakes: function(args, strict) {
return filter.call(this.fakes, function(fake) {
return fake.matches(args, strict);
});
/*
* Hook for derived implementation to return fake instances matching the
* given arguments.
*/
matchingFakes: function(/*args, strict*/) {
return emptyFakes;
},

getCall: function getCall(i) {
Expand Down Expand Up @@ -127,9 +130,11 @@ var proxyApi = {
this.callIds = [];
this.errorsWithCallStack = [];

forEach(this.fakes, function(fake) {
fake.resetHistory();
});
if (this.fakes) {
forEach(this.fakes, function(fake) {
fake.resetHistory();
});
}

return this;
}
Expand Down Expand Up @@ -276,8 +281,7 @@ function wrapFunction(func, originalFunc) {
thisValues: [],
exceptions: [],
callIds: [],
errorsWithCallStack: [],
fakes: []
errorsWithCallStack: []
});
return p;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/sinon/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var forEach = arrayProto.forEach;
var pop = arrayProto.pop;
var push = arrayProto.push;
var slice = arrayProto.slice;
var filter = Array.prototype.filter;

var uuid = 0;

Expand Down Expand Up @@ -60,6 +61,13 @@ var spyApi = {
return fake;
},

// Override proxy default implementation
matchingFakes: function(args, strict) {
return filter.call(this.fakes, function(fake) {
return fake.matches(args, strict);
});
},

matches: function(args, strict) {
var margs = this.matchingArguments;

Expand Down Expand Up @@ -140,6 +148,7 @@ function createSpy(func) {
extend.nonEnum(proxy, spyApi);
extend.nonEnum(proxy, {
displayName: name || "spy",
fakes: [],
instantiateFake: createSpy,
id: "spy#" + uuid++
});
Expand Down
1 change: 1 addition & 0 deletions lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function createStub(originalFunc) {

var name = originalFunc ? functionName(originalFunc) : null;
extend.nonEnum(proxy, {
fakes: [],
instantiateFake: createStub,
displayName: name || "stub",
defaultBehavior: null,
Expand Down

0 comments on commit cd4f6d0

Please sign in to comment.