mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Add _.memoize tests for max cache size and immutable maps.
This commit is contained in:
109
test/test.js
109
test/test.js
@@ -14470,6 +14470,44 @@
|
|||||||
QUnit.module('lodash.memoize');
|
QUnit.module('lodash.memoize');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
function CustomCache() {
|
||||||
|
this.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
CustomCache.prototype = {
|
||||||
|
'clear': function() {
|
||||||
|
this.__data__ = [];
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
'get': function(key) {
|
||||||
|
var entry = lodashStable.find(this.__data__, ['key', key]);
|
||||||
|
return entry && entry.value;
|
||||||
|
},
|
||||||
|
'has': function(key) {
|
||||||
|
return lodashStable.some(this.__data__, ['key', key]);
|
||||||
|
},
|
||||||
|
'set': function(key, value) {
|
||||||
|
this.__data__.push({ 'key': key, 'value': value });
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function ImmutableCache() {
|
||||||
|
this.__data__ = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
ImmutableCache.prototype = lodashStable.create(CustomCache.prototype, {
|
||||||
|
'constructor': ImmutableCache,
|
||||||
|
'clear': function() {
|
||||||
|
return new ImmutableCache;
|
||||||
|
},
|
||||||
|
'set': function(key, value) {
|
||||||
|
var result = new ImmutableCache;
|
||||||
|
result.__data__ = this.__data__.concat({ 'key': key, 'value': value });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
QUnit.test('should memoize results based on the first argument given', function(assert) {
|
QUnit.test('should memoize results based on the first argument given', function(assert) {
|
||||||
assert.expect(2);
|
assert.expect(2);
|
||||||
|
|
||||||
@@ -14578,43 +14616,20 @@
|
|||||||
assert.expect(4);
|
assert.expect(4);
|
||||||
|
|
||||||
var oldCache = _.memoize.Cache;
|
var oldCache = _.memoize.Cache;
|
||||||
|
_.memoize.Cache = CustomCache;
|
||||||
function Cache() {
|
|
||||||
this.__data__ = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
Cache.prototype = {
|
|
||||||
'get': function(key) {
|
|
||||||
var entry = _.find(this.__data__, function(entry) {
|
|
||||||
return key === entry.key;
|
|
||||||
});
|
|
||||||
return entry && entry.value;
|
|
||||||
},
|
|
||||||
'has': function(key) {
|
|
||||||
return _.some(this.__data__, function(entry) {
|
|
||||||
return key === entry.key;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
'set': function(key, value) {
|
|
||||||
this.__data__.push({ 'key': key, 'value': value });
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
_.memoize.Cache = Cache;
|
|
||||||
|
|
||||||
var memoized = _.memoize(function(object) {
|
var memoized = _.memoize(function(object) {
|
||||||
return 'value:' + object.id;
|
return object.id;
|
||||||
});
|
});
|
||||||
|
|
||||||
var cache = memoized.cache,
|
var cache = memoized.cache,
|
||||||
key1 = { 'id': 'a' },
|
key1 = { 'id': 'a' },
|
||||||
key2 = { 'id': 'b' };
|
key2 = { 'id': 'b' };
|
||||||
|
|
||||||
assert.strictEqual(memoized(key1), 'value:a');
|
assert.strictEqual(memoized(key1), 'a');
|
||||||
assert.strictEqual(cache.has(key1), true);
|
assert.strictEqual(cache.has(key1), true);
|
||||||
|
|
||||||
assert.strictEqual(memoized(key2), 'value:b');
|
assert.strictEqual(memoized(key2), 'b');
|
||||||
assert.strictEqual(cache.has(key2), true);
|
assert.strictEqual(cache.has(key2), true);
|
||||||
|
|
||||||
_.memoize.Cache = oldCache;
|
_.memoize.Cache = oldCache;
|
||||||
@@ -14624,30 +14639,7 @@
|
|||||||
assert.expect(2);
|
assert.expect(2);
|
||||||
|
|
||||||
var oldCache = _.memoize.Cache;
|
var oldCache = _.memoize.Cache;
|
||||||
|
_.memoize.Cache = ImmutableCache;
|
||||||
function Cache() {
|
|
||||||
this.__data__ = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
Cache.prototype = {
|
|
||||||
'get': function(key) {
|
|
||||||
return _.find(this.__data__, function(entry) {
|
|
||||||
return key === entry.key;
|
|
||||||
}).value;
|
|
||||||
},
|
|
||||||
'has': function(key) {
|
|
||||||
return _.some(this.__data__, function(entry) {
|
|
||||||
return key === entry.key;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
'set': function(key, value) {
|
|
||||||
var result = new Cache;
|
|
||||||
result.__data__ = this.__data__.concat({ 'key': key, 'value': value });
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
_.memoize.Cache = Cache;
|
|
||||||
|
|
||||||
var memoized = _.memoize(function(object) {
|
var memoized = _.memoize(function(object) {
|
||||||
return object.id;
|
return object.id;
|
||||||
@@ -14698,6 +14690,21 @@
|
|||||||
skipAssert(assert);
|
skipAssert(assert);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should not error when the max cache size is exceeded with an immutable map', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var memoized = _.memoize(identity),
|
||||||
|
pass = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
memoized.cache = new ImmutableCache;
|
||||||
|
lodashStable.times(MAX_MEMOIZE_SIZE + 1, memoized);
|
||||||
|
} catch (e) {
|
||||||
|
pass = false;
|
||||||
|
}
|
||||||
|
assert.ok(pass);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user