From 4a0415a6bdfc24e7dd533d20bf0e8016f734f11c Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 9 Jan 2016 01:05:43 -0800 Subject: [PATCH] Increase test coverage of symbols. --- lodash.js | 9 ++++++--- test/index.html | 7 +++++++ test/test.js | 22 ++++++++++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lodash.js b/lodash.js index 267e02a8f..393ba77f6 100644 --- a/lodash.js +++ b/lodash.js @@ -3669,7 +3669,7 @@ * @returns {Object} Returns the cloned symbol object. */ function cloneSymbol(symbol) { - return Object(symbolValueOf.call(symbol)); + return _Symbol ? Object(symbolValueOf.call(symbol)) : {}; } /** @@ -4557,7 +4557,7 @@ equalFunc(convert(object), convert(other), customizer, bitmask | UNORDERED_COMPARE_FLAG); case symbolTag: - return symbolValueOf.call(object) == symbolValueOf.call(other); + return !!_Symbol && (symbolValueOf.call(object) == symbolValueOf.call(other)); } return false; } @@ -10347,7 +10347,10 @@ if (value == null) { return ''; } - var result = isSymbol(value) ? symbolToString.call(value) : (value + ''); + if (isSymbol(value)) { + return _Symbol ? symbolToString.call(value) : ''; + } + var result = (value + ''); return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result; } diff --git a/test/index.html b/test/index.html index 2fbb2867e..00cadc606 100644 --- a/test/index.html +++ b/test/index.html @@ -112,6 +112,9 @@ setProperty(window, '_Set', window.Set); setProperty(window, 'Set', noop); + setProperty(window, '_Symbol', window.Symbol); + setProperty(window, 'Symbol', undefined); + setProperty(window, '_WeakMap', window.WeakMap); setProperty(window, 'WeakMap', noop); @@ -148,6 +151,9 @@ } else { setProperty(window, 'Set', undefined); } + if (_Symbol) { + Symbol = _Symbol; + } if (_WeakMap) { WeakMap = _WeakMap; } else { @@ -157,6 +163,7 @@ setProperty(window, '_Map', undefined); setProperty(window, '_Set', undefined); + setProperty(window, '_Symbol', undefined); setProperty(window, '_WeakMap', undefined); setProperty(window, 'WinRTError', undefined); diff --git a/test/test.js b/test/test.js index d9211b546..ba5e4e7bc 100644 --- a/test/test.js +++ b/test/test.js @@ -461,6 +461,7 @@ setProperty(Object, 'getOwnPropertySymbols', undefined); setProperty(root, 'Set', noop); + setProperty(root, 'Symbol', undefined); setProperty(root, 'WeakMap', noop); // Fake `WinRTError`. @@ -489,6 +490,11 @@ } else { delete root.Set; } + if (Symbol) { + setProperty(root, 'Symbol', Symbol); + } else { + delete root.Symbol; + } if (WeakMap) { setProperty(root, 'WeakMap', WeakMap); } else { @@ -700,7 +706,7 @@ }); QUnit.test('should avoid overwritten native methods', function(assert) { - assert.expect(5); + assert.expect(6); function message(lodashMethod, nativeMethod) { return '`' + lodashMethod + '` should avoid overwritten native `' + nativeMethod + '`'; @@ -747,6 +753,18 @@ } assert.deepEqual(actual, [object, object], 'Object.getOwnPropertySymbols'); + try { + var symObject = Object(symbol); + actual = [ + Symbol ? lodashBizarro.clone(symObject) : {}, + Symbol ? lodashBizarro.isEqual(symObject, Object(symbol)) : false, + Symbol ? lodashBizarro.toString(symObject) : '' + ]; + } catch (e) { + actual = null; + } + assert.deepEqual(actual, [{}, false, ''], 'Symbol'); + try { var map = new lodashBizarro.memoize.Cache; actual = map.set('a', 1).get('a'); @@ -769,7 +787,7 @@ assert.deepEqual(actual, [], label); } else { - skipTest(assert, 5); + skipTest(assert, 6); } }); }());