From 7b61569d32697219e558e2714bcd00250907f8d1 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 6 Oct 2015 08:07:38 -0700 Subject: [PATCH] Add bizarro tests for `Object.create`. --- test/index.html | 9 +++++++++ test/test.js | 29 ++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/test/index.html b/test/index.html index 853256c28..ae666d35a 100644 --- a/test/index.html +++ b/test/index.html @@ -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. diff --git a/test/test.js b/test/test.js index 71ab95327..1defc17ef 100644 --- a/test/test.js +++ b/test/test.js @@ -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); } }); }());