diff options
Diffstat (limited to 'test/test-cache-map.js')
-rw-r--r-- | test/test-cache-map.js | 121 |
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); |