Add bizarro test for Object#propertyIsEnumerable.

This commit is contained in:
John-David Dalton
2015-07-19 09:21:21 -07:00
parent 4eb2eea809
commit 0a24e9854b
2 changed files with 31 additions and 7 deletions

View File

@@ -81,6 +81,11 @@
funcProto._method = noop;
// Set bad shims.
setProperty(objectProto, '_propertyIsEnumerable', propertyIsEnumerable);
setProperty(objectProto, 'propertyIsEnumerable', function(key) {
return !(key == 'valueOf' && this && this.valueOf === 1) && _propertyIsEnumerable.call(this, key);
});
setProperty(window, '_Set', window.Set);
setProperty(window, 'Set', noop);
@@ -97,7 +102,8 @@
}
function removeBizarroMethods() {
var funcProto = Function.prototype;
var funcProto = Function.prototype,
objectProto = Object.prototype;
if (window._Set) {
Set = _Set;
@@ -115,7 +121,10 @@
setProperty(window, 'global', undefined);
setProperty(window, 'module', undefined);
setProperty(objectProto, 'propertyIsEnumerable', objectProto._propertyIsEnumerable);
delete funcProto._method;
delete objectProto._propertyIsEnumerable;
}
// Load lodash to expose it to the bad extensions/shims.

View File

@@ -48,7 +48,6 @@
create = Object.create,
fnToString = funcProto.toString,
freeze = Object.freeze,
hasOwnProperty = objectProto.hasOwnProperty,
JSON = root.JSON,
objToString = objectProto.toString,
noop = function() {},
@@ -428,6 +427,12 @@
// Add prototype extensions.
funcProto._method = _.noop;
// Set bad shims.
var _propertyIsEnumerable = objectProto.propertyIsEnumerable;
setProperty(objectProto, 'propertyIsEnumerable', function(key) {
return !(key == 'valueOf' && this && this.valueOf === 1) && _propertyIsEnumerable.call(this, key);
});
var _Set = root.Set;
setProperty(root, 'Set', _.noop);
@@ -445,6 +450,8 @@
root._ = oldDash;
// Restore built-in methods.
setProperty(objectProto, 'propertyIsEnumerable', _propertyIsEnumerable);
if (_Set) {
setProperty(root, 'Set', Set);
} else {
@@ -620,20 +627,28 @@
}
});
test('should avoid overwritten native methods', 1, function() {
function Foo() {}
test('should avoid overwritten native methods', 2, function() {
function message(lodashMethod, nativeMethod) {
return '`' + lodashMethod + '` should avoid overwritten native `' + nativeMethod + '`';
}
function Foo() { this.a = 1; }
Foo.prototype.b = 2;
var object = { 'a': 1 },
otherObject = { 'b': 2 },
largeArray = _.times(LARGE_ARRAY_SIZE, _.constant(object));
if (lodashBizarro) {
try {
var actual = [
var actual = _.keysIn(new Foo).sort();
} catch(e) {
actual = null;
}
deepEqual(actual, ['a', 'b'], message('_.keysIn', 'Object#propertyIsEnumerable'));
try {
actual = [
lodashBizarro.difference([object, otherObject], largeArray),
lodashBizarro.intersection(largeArray, [object]),
lodashBizarro.uniq(largeArray)
@@ -644,7 +659,7 @@
deepEqual(actual, [[otherObject], [object], [object]], message('_.difference`, `_.intersection`, and `_.uniq', 'Set'));
}
else {
skipTest();
skipTest(2);
}
});
}());