Add bizarro tests for Object.create.

This commit is contained in:
John-David Dalton
2015-10-06 08:07:38 -07:00
parent 9c27ed8bda
commit 7b61569d32
2 changed files with 33 additions and 5 deletions

View File

@@ -84,6 +84,9 @@
return !(key == 'valueOf' && this && this.valueOf === 1) && _propertyIsEnumerable.call(this, key);
});
setProperty(Object, '_create', window.create);
setProperty(Object, 'create', noop);
setProperty(window, '_Map', window.Map);
setProperty(window, 'Map', noop);
@@ -106,6 +109,11 @@
var funcProto = Function.prototype,
objectProto = Object.prototype;
if (Object._create) {
Object.create = Object._create;
} else {
delete Object.create;
}
if (window._Map) {
Map = _Map;
} else {
@@ -135,6 +143,7 @@
delete funcProto._method;
delete objectProto._propertyIsEnumerable;
delete Object._create;
}
// Load lodash to expose it to the bad extensions/shims.

View File

@@ -422,6 +422,7 @@
return !(key == 'valueOf' && this && this.valueOf === 1) && _propertyIsEnumerable.call(this, key);
});
setProperty(Object, 'create', _.noop);
setProperty(root, 'Map', _.noop);
setProperty(root, 'Set', _.noop);
setProperty(root, 'WeakMap', _.noop);
@@ -438,6 +439,7 @@
// Restore built-in methods.
setProperty(objectProto, 'propertyIsEnumerable', _propertyIsEnumerable);
setProperty(Object, 'create', create);
if (Map) {
setProperty(root, 'Map', Map);
@@ -650,7 +652,7 @@
});
QUnit.test('should avoid overwritten native methods', function(assert) {
assert.expect(2);
assert.expect(3);
function message(lodashMethod, nativeMethod) {
return '`' + lodashMethod + '` should avoid overwritten native `' + nativeMethod + '`';
@@ -659,17 +661,23 @@
function Foo() { this.a = 1; }
Foo.prototype.b = 2;
var object = { 'a': 1 },
var cyclical = {},
object = { 'a': 1 },
otherObject = { 'b': 2 },
largeArray = _.times(LARGE_ARRAY_SIZE, _.constant(object));
_.times(LARGE_ARRAY_SIZE + 1, function(index) {
cyclical['v' + index] = [index ? cyclical['v' + (index - 1)] : cyclical];
});
if (lodashBizarro) {
try {
var actual = _.keysIn(new Foo).sort();
} catch (e) {
actual = null;
}
assert.deepEqual(actual, ['a', 'b'], message('_.keysIn', 'Object#propertyIsEnumerable'));
var label = message('_.keysIn', 'Object#propertyIsEnumerable');
assert.deepEqual(actual, ['a', 'b'], label);
try {
actual = [
@@ -680,10 +688,21 @@
} catch (e) {
actual = null;
}
assert.deepEqual(actual, [[otherObject], [object], [object]], message('_.difference`, `_.intersection`, and `_.uniq', 'Set'));
label = message('_.difference`, `_.intersection`, and `_.uniq', 'Object.create` and `Map');
assert.deepEqual(actual, [[otherObject], [object], [object]], label);
try {
var clone = _.cloneDeep(cyclical);
actual = clone['v' + LARGE_ARRAY_SIZE][0];
} catch (e) {
actual = null;
clone = {};
}
label = message('_.cloneDeep', 'Object.create` and `Map');
assert.strictEqual(actual, clone['v' + (LARGE_ARRAY_SIZE - 1)], label);
}
else {
skipTest(assert, 2);
skipTest(assert, 3);
}
});
}());