mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Add bizarro tests for Object.create.
This commit is contained in:
@@ -84,6 +84,9 @@
|
|||||||
return !(key == 'valueOf' && this && this.valueOf === 1) && _propertyIsEnumerable.call(this, key);
|
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', window.Map);
|
||||||
setProperty(window, 'Map', noop);
|
setProperty(window, 'Map', noop);
|
||||||
|
|
||||||
@@ -106,6 +109,11 @@
|
|||||||
var funcProto = Function.prototype,
|
var funcProto = Function.prototype,
|
||||||
objectProto = Object.prototype;
|
objectProto = Object.prototype;
|
||||||
|
|
||||||
|
if (Object._create) {
|
||||||
|
Object.create = Object._create;
|
||||||
|
} else {
|
||||||
|
delete Object.create;
|
||||||
|
}
|
||||||
if (window._Map) {
|
if (window._Map) {
|
||||||
Map = _Map;
|
Map = _Map;
|
||||||
} else {
|
} else {
|
||||||
@@ -135,6 +143,7 @@
|
|||||||
|
|
||||||
delete funcProto._method;
|
delete funcProto._method;
|
||||||
delete objectProto._propertyIsEnumerable;
|
delete objectProto._propertyIsEnumerable;
|
||||||
|
delete Object._create;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load lodash to expose it to the bad extensions/shims.
|
// Load lodash to expose it to the bad extensions/shims.
|
||||||
|
|||||||
29
test/test.js
29
test/test.js
@@ -422,6 +422,7 @@
|
|||||||
return !(key == 'valueOf' && this && this.valueOf === 1) && _propertyIsEnumerable.call(this, key);
|
return !(key == 'valueOf' && this && this.valueOf === 1) && _propertyIsEnumerable.call(this, key);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setProperty(Object, 'create', _.noop);
|
||||||
setProperty(root, 'Map', _.noop);
|
setProperty(root, 'Map', _.noop);
|
||||||
setProperty(root, 'Set', _.noop);
|
setProperty(root, 'Set', _.noop);
|
||||||
setProperty(root, 'WeakMap', _.noop);
|
setProperty(root, 'WeakMap', _.noop);
|
||||||
@@ -438,6 +439,7 @@
|
|||||||
|
|
||||||
// Restore built-in methods.
|
// Restore built-in methods.
|
||||||
setProperty(objectProto, 'propertyIsEnumerable', _propertyIsEnumerable);
|
setProperty(objectProto, 'propertyIsEnumerable', _propertyIsEnumerable);
|
||||||
|
setProperty(Object, 'create', create);
|
||||||
|
|
||||||
if (Map) {
|
if (Map) {
|
||||||
setProperty(root, 'Map', Map);
|
setProperty(root, 'Map', Map);
|
||||||
@@ -650,7 +652,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should avoid overwritten native methods', function(assert) {
|
QUnit.test('should avoid overwritten native methods', function(assert) {
|
||||||
assert.expect(2);
|
assert.expect(3);
|
||||||
|
|
||||||
function message(lodashMethod, nativeMethod) {
|
function message(lodashMethod, nativeMethod) {
|
||||||
return '`' + lodashMethod + '` should avoid overwritten native `' + nativeMethod + '`';
|
return '`' + lodashMethod + '` should avoid overwritten native `' + nativeMethod + '`';
|
||||||
@@ -659,17 +661,23 @@
|
|||||||
function Foo() { this.a = 1; }
|
function Foo() { this.a = 1; }
|
||||||
Foo.prototype.b = 2;
|
Foo.prototype.b = 2;
|
||||||
|
|
||||||
var object = { 'a': 1 },
|
var cyclical = {},
|
||||||
|
object = { 'a': 1 },
|
||||||
otherObject = { 'b': 2 },
|
otherObject = { 'b': 2 },
|
||||||
largeArray = _.times(LARGE_ARRAY_SIZE, _.constant(object));
|
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) {
|
if (lodashBizarro) {
|
||||||
try {
|
try {
|
||||||
var actual = _.keysIn(new Foo).sort();
|
var actual = _.keysIn(new Foo).sort();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
actual = null;
|
actual = null;
|
||||||
}
|
}
|
||||||
assert.deepEqual(actual, ['a', 'b'], message('_.keysIn', 'Object#propertyIsEnumerable'));
|
var label = message('_.keysIn', 'Object#propertyIsEnumerable');
|
||||||
|
assert.deepEqual(actual, ['a', 'b'], label);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
actual = [
|
actual = [
|
||||||
@@ -680,10 +688,21 @@
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
actual = null;
|
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 {
|
else {
|
||||||
skipTest(assert, 2);
|
skipTest(assert, 3);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|||||||
Reference in New Issue
Block a user