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

Do not invoke getters in walk #1059

Merged
merged 1 commit into from
Jun 5, 2016
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
2 changes: 1 addition & 1 deletion lib/sinon/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ var collection = {
var col = this;
var stubbedObj = sinon.stub.apply(sinon, arguments);

walk(stubbedObj, function (val, prop, propOwner) {
walk(stubbedObj, function (prop, propOwner) {
if (
typeof getPropertyDescriptor(propOwner, prop).value === "function"
) {
Expand Down
2 changes: 1 addition & 1 deletion lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function stub(object, property, func) {
}

if (typeof property === "undefined" && typeof object === "object") {
walk(object || {}, function (value, prop, propOwner) {
walk(object || {}, function (prop, propOwner) {
// we don't want to stub things like toString(), valueOf(), etc. so we only stub if the object
// is not Object.prototype
if (
Expand Down
2 changes: 1 addition & 1 deletion lib/sinon/util/core/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function isRestorable(obj) {

module.exports = function restore(object) {
if (object !== null && typeof object === "object") {
walk(object, function (value, prop) {
walk(object, function (prop) {
if (isRestorable(object[prop])) {
object[prop].restore();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/sinon/util/core/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function walkInternal(obj, iterator, context, originalObj, seen) {
seen[k] = true;
var target = typeof Object.getOwnPropertyDescriptor(obj, k).get === "function" ?
originalObj : obj;
iterator.call(context, target[k], k, target);
iterator.call(context, k, target);
}
});

Expand Down
34 changes: 16 additions & 18 deletions test/util/core/walk-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ describe("util/core/walk", function () {

assert(iterator.calledTwice);
assert(iterator.alwaysCalledOn(rcvr));
assert(iterator.calledWithExactly("world", "hello", target));
assert(iterator.calledWithExactly(15, "foo", target));
assert(iterator.calledWithExactly("hello", target));
assert(iterator.calledWithExactly("foo", target));
});

it("should work with non-enumerable properties", function () {
Expand All @@ -35,8 +35,8 @@ describe("util/core/walk", function () {
walk(target, iterator);

assert(iterator.calledTwice);
assert(iterator.calledWith("world", "hello"));
assert(iterator.calledWith(15, "foo"));
assert(iterator.calledWith("hello"));
assert(iterator.calledWith("foo"));
});

it("should walk the prototype chain of an object", function () {
Expand Down Expand Up @@ -77,30 +77,28 @@ describe("util/core/walk", function () {
walk(target, iterator);

assert.equals(iterator.callCount, 6);
assert(iterator.calledWith("non-enumerable own prop", "nonEnumerableOwnProp", target));
assert(iterator.calledWith("enumerable own prop", "enumerableOwnProp", target));
assert(iterator.calledWith("non-enumerable prop", "nonEnumerableProp", proto));
assert(iterator.calledWith("enumerable prop", "enumerableProp", proto));
assert(iterator.calledWith("non-enumerable parent prop", "nonEnumerableParentProp", parentProto));
assert(iterator.calledWith("enumerable parent prop", "enumerableParentProp", parentProto));
assert(iterator.calledWith("nonEnumerableOwnProp", target));
assert(iterator.calledWith("enumerableOwnProp", target));
assert(iterator.calledWith("nonEnumerableProp", proto));
assert(iterator.calledWith("enumerableProp", proto));
assert(iterator.calledWith("nonEnumerableParentProp", parentProto));
assert(iterator.calledWith("enumerableParentProp", parentProto));
});

it("should always invoke getters on the original receiving object", function () {
var Target = function Target() {
this.o = { foo: "foo" };
};
it("should not invoke getters on the original receiving object", function () {
var Target = function Target() {};
var getter = createSpy();
Object.defineProperty(Target.prototype, "computedFoo", {
enumerable: true,
get: function () {
return "computed " + this.o.foo;
}
get: getter
});
var target = new Target();
var iterator = createSpy();

walk(target, iterator);

assert(iterator.calledWith("computed foo", "computedFoo", target));
assert(iterator.calledWith("computedFoo", target));
assert(getter.notCalled);
});

it("should fall back to for..in if getOwnPropertyNames is not available", function () {
Expand Down