mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 03:17:49 +00:00
Add bizarro test for Object#propertyIsEnumerable.
This commit is contained in:
@@ -81,6 +81,11 @@
|
|||||||
funcProto._method = noop;
|
funcProto._method = noop;
|
||||||
|
|
||||||
// Set bad shims.
|
// 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', window.Set);
|
||||||
setProperty(window, 'Set', noop);
|
setProperty(window, 'Set', noop);
|
||||||
|
|
||||||
@@ -97,7 +102,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function removeBizarroMethods() {
|
function removeBizarroMethods() {
|
||||||
var funcProto = Function.prototype;
|
var funcProto = Function.prototype,
|
||||||
|
objectProto = Object.prototype;
|
||||||
|
|
||||||
if (window._Set) {
|
if (window._Set) {
|
||||||
Set = _Set;
|
Set = _Set;
|
||||||
@@ -115,7 +121,10 @@
|
|||||||
setProperty(window, 'global', undefined);
|
setProperty(window, 'global', undefined);
|
||||||
setProperty(window, 'module', undefined);
|
setProperty(window, 'module', undefined);
|
||||||
|
|
||||||
|
setProperty(objectProto, 'propertyIsEnumerable', objectProto._propertyIsEnumerable);
|
||||||
|
|
||||||
delete funcProto._method;
|
delete funcProto._method;
|
||||||
|
delete objectProto._propertyIsEnumerable;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load lodash to expose it to the bad extensions/shims.
|
// Load lodash to expose it to the bad extensions/shims.
|
||||||
|
|||||||
27
test/test.js
27
test/test.js
@@ -48,7 +48,6 @@
|
|||||||
create = Object.create,
|
create = Object.create,
|
||||||
fnToString = funcProto.toString,
|
fnToString = funcProto.toString,
|
||||||
freeze = Object.freeze,
|
freeze = Object.freeze,
|
||||||
hasOwnProperty = objectProto.hasOwnProperty,
|
|
||||||
JSON = root.JSON,
|
JSON = root.JSON,
|
||||||
objToString = objectProto.toString,
|
objToString = objectProto.toString,
|
||||||
noop = function() {},
|
noop = function() {},
|
||||||
@@ -428,6 +427,12 @@
|
|||||||
// Add prototype extensions.
|
// Add prototype extensions.
|
||||||
funcProto._method = _.noop;
|
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;
|
var _Set = root.Set;
|
||||||
setProperty(root, 'Set', _.noop);
|
setProperty(root, 'Set', _.noop);
|
||||||
|
|
||||||
@@ -445,6 +450,8 @@
|
|||||||
root._ = oldDash;
|
root._ = oldDash;
|
||||||
|
|
||||||
// Restore built-in methods.
|
// Restore built-in methods.
|
||||||
|
setProperty(objectProto, 'propertyIsEnumerable', _propertyIsEnumerable);
|
||||||
|
|
||||||
if (_Set) {
|
if (_Set) {
|
||||||
setProperty(root, 'Set', Set);
|
setProperty(root, 'Set', Set);
|
||||||
} else {
|
} else {
|
||||||
@@ -620,20 +627,28 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should avoid overwritten native methods', 1, function() {
|
test('should avoid overwritten native methods', 2, function() {
|
||||||
function Foo() {}
|
|
||||||
|
|
||||||
function message(lodashMethod, nativeMethod) {
|
function message(lodashMethod, nativeMethod) {
|
||||||
return '`' + lodashMethod + '` should avoid overwritten native `' + nativeMethod + '`';
|
return '`' + lodashMethod + '` should avoid overwritten native `' + nativeMethod + '`';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Foo() { this.a = 1; }
|
||||||
|
Foo.prototype.b = 2;
|
||||||
|
|
||||||
var object = { 'a': 1 },
|
var object = { 'a': 1 },
|
||||||
otherObject = { 'b': 2 },
|
otherObject = { 'b': 2 },
|
||||||
largeArray = _.times(LARGE_ARRAY_SIZE, _.constant(object));
|
largeArray = _.times(LARGE_ARRAY_SIZE, _.constant(object));
|
||||||
|
|
||||||
if (lodashBizarro) {
|
if (lodashBizarro) {
|
||||||
try {
|
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.difference([object, otherObject], largeArray),
|
||||||
lodashBizarro.intersection(largeArray, [object]),
|
lodashBizarro.intersection(largeArray, [object]),
|
||||||
lodashBizarro.uniq(largeArray)
|
lodashBizarro.uniq(largeArray)
|
||||||
@@ -644,7 +659,7 @@
|
|||||||
deepEqual(actual, [[otherObject], [object], [object]], message('_.difference`, `_.intersection`, and `_.uniq', 'Set'));
|
deepEqual(actual, [[otherObject], [object], [object]], message('_.difference`, `_.intersection`, and `_.uniq', 'Set'));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
skipTest();
|
skipTest(2);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|||||||
Reference in New Issue
Block a user