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

Cache references to Array.prototype.filter #1523

Merged
merged 3 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
});

});
});