summaryrefslogtreecommitdiff
path: root/test/test-cache-map.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-09-22 20:09:12 -0400
committerGitHub <noreply@github.com>2020-09-22 20:09:12 -0400
commit7d78e8737f2bb9cd75313c61e3cbf31be5db72f9 (patch)
tree473fce7fed23e796a929430c56f27238066e54ac /test/test-cache-map.js
parentd395a2a6bfe33feef467f0e0886a089afccd8438 (diff)
Cache map improvements (#856)
* Update CacheMap API; get=>getOrCreate; add get; add set; add has * Update tests * Add more tests
Diffstat (limited to 'test/test-cache-map.js')
-rw-r--r--test/test-cache-map.js121
1 files changed, 87 insertions, 34 deletions
diff --git a/test/test-cache-map.js b/test/test-cache-map.js
index 9307dd2c..12367a90 100644
--- a/test/test-cache-map.js
+++ b/test/test-cache-map.js
@@ -57,101 +57,154 @@ function testApi() {
maxCount: 1,
expectedCount: 1,
calls: [
- ['get', 'a', 'b', 'c']
+ {func: 'getOrCreate', args: [['a', 'b', 'c']]}
]
},
{
maxCount: 10,
expectedCount: 1,
calls: [
- ['get', 'a', 'b', 'c'],
- ['get', 'a', 'b', 'c'],
- ['get', 'a', 'b', 'c']
+ {func: 'getOrCreate', args: [['a', 'b', 'c']]},
+ {func: 'getOrCreate', args: [['a', 'b', 'c']]},
+ {func: 'getOrCreate', args: [['a', 'b', 'c']]}
]
},
{
maxCount: 10,
expectedCount: 3,
calls: [
- ['get', 'a1', 'b', 'c'],
- ['get', 'a2', 'b', 'c'],
- ['get', 'a3', 'b', 'c']
+ {func: 'getOrCreate', args: [['a1', 'b', 'c']]},
+ {func: 'getOrCreate', args: [['a2', 'b', 'c']]},
+ {func: 'getOrCreate', args: [['a3', 'b', 'c']]}
]
},
{
maxCount: 10,
expectedCount: 3,
calls: [
- ['get', 'a', 'b1', 'c'],
- ['get', 'a', 'b2', 'c'],
- ['get', 'a', 'b3', 'c']
+ {func: 'getOrCreate', args: [['a', 'b1', 'c']]},
+ {func: 'getOrCreate', args: [['a', 'b2', 'c']]},
+ {func: 'getOrCreate', args: [['a', 'b3', 'c']]}
]
},
{
maxCount: 10,
expectedCount: 3,
calls: [
- ['get', 'a', 'b', 'c1'],
- ['get', 'a', 'b', 'c2'],
- ['get', 'a', 'b', 'c3']
+ {func: 'getOrCreate', args: [['a', 'b', 'c1']]},
+ {func: 'getOrCreate', args: [['a', 'b', 'c2']]},
+ {func: 'getOrCreate', args: [['a', 'b', 'c3']]}
]
},
{
maxCount: 1,
expectedCount: 1,
calls: [
- ['get', 'a1', 'b', 'c'],
- ['get', 'a2', 'b', 'c'],
- ['get', 'a3', 'b', 'c']
+ {func: 'getOrCreate', args: [['a1', 'b', 'c']]},
+ {func: 'getOrCreate', args: [['a2', 'b', 'c']]},
+ {func: 'getOrCreate', args: [['a3', 'b', 'c']]}
]
},
{
maxCount: 1,
expectedCount: 1,
calls: [
- ['get', 'a', 'b1', 'c'],
- ['get', 'a', 'b2', 'c'],
- ['get', 'a', 'b3', 'c']
+ {func: 'getOrCreate', args: [['a', 'b1', 'c']]},
+ {func: 'getOrCreate', args: [['a', 'b2', 'c']]},
+ {func: 'getOrCreate', args: [['a', 'b3', 'c']]}
]
},
{
maxCount: 1,
expectedCount: 1,
calls: [
- ['get', 'a', 'b', 'c1'],
- ['get', 'a', 'b', 'c2'],
- ['get', 'a', 'b', 'c3']
+ {func: 'getOrCreate', args: [['a', 'b', 'c1']]},
+ {func: 'getOrCreate', args: [['a', 'b', 'c2']]},
+ {func: 'getOrCreate', args: [['a', 'b', 'c3']]}
]
},
{
maxCount: 10,
expectedCount: 0,
calls: [
- ['get', 'a', 'b', 'c1'],
- ['get', 'a', 'b', 'c2'],
- ['get', 'a', 'b', 'c3'],
- ['clear']
+ {func: 'getOrCreate', args: [['a', 'b', 'c1']]},
+ {func: 'getOrCreate', args: [['a', 'b', 'c2']]},
+ {func: 'getOrCreate', args: [['a', 'b', 'c3']]},
+ {func: 'clear', args: []}
]
},
{
maxCount: 0,
expectedCount: 0,
calls: [
- ['get', 'a1', 'b', 'c'],
- ['get', 'a', 'b2', 'c'],
- ['get', 'a', 'b', 'c3']
+ {func: 'getOrCreate', args: [['a1', 'b', 'c']]},
+ {func: 'getOrCreate', args: [['a', 'b2', 'c']]},
+ {func: 'getOrCreate', args: [['a', 'b', 'c3']]}
+ ]
+ },
+ {
+ maxCount: 10,
+ expectedCount: 1,
+ calls: [
+ {func: 'get', args: [['a1', 'b', 'c']], returnValue: void 0},
+ {func: 'has', args: [['a1', 'b', 'c']], returnValue: false},
+ {func: 'set', args: [['a1', 'b', 'c'], 32], returnValue: void 0},
+ {func: 'get', args: [['a1', 'b', 'c']], returnValue: 32},
+ {func: 'has', args: [['a1', 'b', 'c']], returnValue: true}
+ ]
+ },
+ {
+ maxCount: 10,
+ expectedCount: 2,
+ calls: [
+ {func: 'set', args: [['a1', 'b', 'c'], 32], returnValue: void 0},
+ {func: 'get', args: [['a1', 'b', 'c']], returnValue: 32},
+ {func: 'set', args: [['a1', 'b', 'c'], 64], returnValue: void 0},
+ {func: 'get', args: [['a1', 'b', 'c']], returnValue: 64},
+ {func: 'set', args: [['a2', 'b', 'c'], 96], returnValue: void 0},
+ {func: 'get', args: [['a2', 'b', 'c']], returnValue: 96}
+ ]
+ },
+ {
+ maxCount: 2,
+ expectedCount: 2,
+ calls: [
+ {func: 'has', args: [['a1', 'b', 'c']], returnValue: false},
+ {func: 'has', args: [['a2', 'b', 'c']], returnValue: false},
+ {func: 'has', args: [['a3', 'b', 'c']], returnValue: false},
+ {func: 'set', args: [['a1', 'b', 'c'], 1], returnValue: void 0},
+ {func: 'has', args: [['a1', 'b', 'c']], returnValue: true},
+ {func: 'has', args: [['a2', 'b', 'c']], returnValue: false},
+ {func: 'has', args: [['a3', 'b', 'c']], returnValue: false},
+ {func: 'set', args: [['a2', 'b', 'c'], 2], returnValue: void 0},
+ {func: 'has', args: [['a1', 'b', 'c']], returnValue: true},
+ {func: 'has', args: [['a2', 'b', 'c']], returnValue: true},
+ {func: 'has', args: [['a3', 'b', 'c']], returnValue: false},
+ {func: 'set', args: [['a3', 'b', 'c'], 3], returnValue: void 0},
+ {func: 'has', args: [['a1', 'b', 'c']], returnValue: false},
+ {func: 'has', args: [['a2', 'b', 'c']], returnValue: true},
+ {func: 'has', args: [['a3', 'b', 'c']], returnValue: true}
]
}
];
- const create = (...args) => args.join(',');
+ const create = (args) => args.join(',');
for (const {maxCount, expectedCount, calls} of data) {
const cache = new CacheMap(maxCount, create);
assert.strictEqual(cache.maxCount, maxCount);
- for (const [name, ...args] of calls) {
- switch (name) {
- case 'get': cache.get(...args); break;
- case 'clear': cache.clear(); break;
+ for (const call of calls) {
+ const {func, args} = call;
+ let returnValue;
+ switch (func) {
+ case 'get': returnValue = cache.get(...args); break;
+ case 'getOrCreate': returnValue = cache.getOrCreate(...args); break;
+ case 'set': returnValue = cache.set(...args); break;
+ case 'has': returnValue = cache.has(...args); break;
+ case 'clear': returnValue = cache.clear(...args); break;
+ }
+ if (Object.prototype.hasOwnProperty.call(call, 'returnValue')) {
+ const {returnValue: expectedReturnValue} = call;
+ assert.deepStrictEqual(returnValue, expectedReturnValue);
}
}
assert.strictEqual(cache.count, expectedCount);