From 08a4ae1f563eda9c9127ca6e47ba0da211ac9306 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 27 Aug 2016 23:40:56 -0700 Subject: [PATCH] Add `_.memoize` tests for max cache size. --- test/test.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/test.js b/test/test.js index 2088a164e..c1ac3f7eb 100644 --- a/test/test.js +++ b/test/test.js @@ -12,6 +12,9 @@ /** Used as the `TypeError` message for "Functions" methods. */ var FUNC_ERROR_TEXT = 'Expected a function'; + /** Used as the maximum memoize cache size. */ + var MAX_MEMOIZE_SIZE = 500; + /** Used as references for various `Number` constants. */ var MAX_SAFE_INTEGER = 9007199254740991, MAX_INTEGER = 1.7976931348623157e+308; @@ -14662,6 +14665,39 @@ _.memoize.Cache = oldCache; }); + + QUnit.test('should enforce a max cache size of `MAX_MEMOIZE_SIZE`', function(assert) { + assert.expect(2); + + var memoized = _.memoize(identity), + cache = memoized.cache; + + lodashStable.times(MAX_MEMOIZE_SIZE, memoized); + assert.strictEqual(cache.size, MAX_MEMOIZE_SIZE); + + memoized(MAX_MEMOIZE_SIZE); + assert.strictEqual(cache.size, 1); + }); + + QUnit.test('should not error when the max cache size is exceeded with a weak map', function(assert) { + assert.expect(1); + + if (WeakMap) { + var memoized = _.memoize(identity), + pass = true; + + try { + memoized.cache = new WeakMap; + lodashStable.times(MAX_MEMOIZE_SIZE + 1, function() { memoized({}); }); + } catch (e) { + pass = false; + } + assert.ok(pass); + } + else { + skipAssert(assert); + } + }); }()); /*--------------------------------------------------------------------------*/