Skip to content

Commit

Permalink
Merge pull request #1523 from fatso83/1521-stub-array-filter-fails
Browse files Browse the repository at this point in the history
Cache references to Array.prototype.filter
  • Loading branch information
fatso83 committed Aug 10, 2017
2 parents 3df68a7 + 052a2d0 commit 81c23fb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
9 changes: 7 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Expand Up @@ -13,11 +13,16 @@
#### Solution - optional
> When contributing code (and not just fixing typos, documentation and configuration), please describe why/how your solution works. This helps reviewers spot any mistakes in the implementation.
>
> Example:
> Example:
> "This solution works by adding a `paintBlue()` method"
> Then your reviewer might spot a mistake in the implementation, if `paintBlue()` uses the colour red.
#### How to verify - mandatory
1. Check out this branch (see github instructions below)
1. Check out this branch
2. `npm install`
3. <your-steps-here>

#### Checklist for author

- [ ] `npm run lint` passes
- [ ] References to standard library functions are [cached](https://github.com/sinonjs/sinon/pull/1523).
5 changes: 3 additions & 2 deletions lib/sinon/call.js
Expand Up @@ -6,6 +6,7 @@ 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;

function throwYieldError(proxy, text, args) {
var msg = functionName(proxy) + text;
Expand Down Expand Up @@ -131,7 +132,7 @@ var callProto = {

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

Expand All @@ -148,7 +149,7 @@ var callProto = {

yieldToOn: function (prop, thisValue) {
var args = slice.call(this.args);
var yieldArg = args.filter(function (arg) {
var yieldArg = filter.call(args, function (arg) {
return arg && typeof arg[prop] === "function";
})[0];
var yieldFn = yieldArg && yieldArg[prop];
Expand Down
5 changes: 3 additions & 2 deletions lib/sinon/collection.js
Expand Up @@ -6,7 +6,8 @@ var sinonMock = require("./mock");
var collectOwnMethods = require("./collect-own-methods");
var valueToString = require("./util/core/value-to-string");

var push = [].push;
var push = Array.prototype.push;
var filter = Array.prototype.filter;

function getFakes(fakeCollection) {
if (!fakeCollection.fakes) {
Expand All @@ -18,7 +19,7 @@ function getFakes(fakeCollection) {

function each(fakeCollection, method) {
var fakes = getFakes(fakeCollection);
var matchingFakes = fakes.filter(function (fake) {
var matchingFakes = filter.call(fakes, function (fake) {
return typeof fake[method] === "function";
});

Expand Down
5 changes: 3 additions & 2 deletions lib/sinon/mock.js
Expand Up @@ -8,6 +8,7 @@ var deepEqual = require("./util/core/deep-equal").use(match);
var wrapMethod = require("./util/core/wrap-method");

var push = Array.prototype.push;
var filter = Array.prototype.filter;

function mock(object) {
if (!object || typeof object === "string") {
Expand Down Expand Up @@ -118,13 +119,13 @@ extend(mock, {
var currentArgs = args || [];
var available;

var expectationsWithMatchingArgs = expectations.filter(function (expectation) {
var expectationsWithMatchingArgs = filter.call(expectations, function (expectation) {
var expectedArgs = expectation.expectedArguments || [];

return arrayEquals(expectedArgs, currentArgs, expectation.expectsExactArgCount);
});

var expectationsToApply = expectationsWithMatchingArgs.filter(function (expectation) {
var expectationsToApply = filter.call(expectationsWithMatchingArgs, function (expectation) {
return !expectation.met() && expectation.allowsCall(thisValue, args);
});

Expand Down
7 changes: 5 additions & 2 deletions lib/sinon/spy.js
Expand Up @@ -12,11 +12,14 @@ var wrapMethod = require("./util/core/wrap-method");
var sinonFormat = require("./util/core/format");
var valueToString = require("./util/core/value-to-string");

/* cache references to library methods so that they also can be stubbed without problems */
var push = Array.prototype.push;
var slice = Array.prototype.slice;
var callId = 0;
var filter = Array.prototype.filter;
var ErrorConstructor = Error.prototype.constructor;

var callId = 0;

function spy(object, property, types) {
var descriptor, methodDesc;

Expand Down Expand Up @@ -340,7 +343,7 @@ var spyApi = {
},

matchingFakes: function (args, strict) {
return (this.fakes || []).filter(function (fake) {
return filter.call(this.fakes || [], function (fake) {
return fake.matches(args, strict);
});
},
Expand Down
22 changes: 21 additions & 1 deletion test/issues/issues-test.js
Expand Up @@ -321,7 +321,7 @@ describe("issues", function () {
});
});

describe("#1512", function () {
describe("#1512 - sandbox.stub(obj,protoMethod)", function () {
var sandbox;

beforeEach(function () {
Expand All @@ -341,4 +341,24 @@ describe("issues", function () {
assert(stub.called);
});
});

describe("#1521 - stubbing Array.prototype.filter", function () {
var orgFilter;

before(function () {
orgFilter = Array.prototype.filter;
});

afterEach(function () {
/* eslint-disable no-extend-native */
Array.prototype.filter = orgFilter;
});

it("should be possible stub filter", function () {
var stub = sinon.stub(Array.prototype, "filter");
[1, 2, 3].filter(function () { return false; });
assert(stub.calledOnce);
});

});
});

0 comments on commit 81c23fb

Please sign in to comment.