Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)
1/*
2 * Copyright (C) 2023-2025 Yomitan Authors
3 * Copyright (C) 2020-2022 Yomichan Authors
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19import {describe, expect, test} from 'vitest';
20import {CacheMap} from '../ext/js/general/cache-map.js';
21
22describe('CacheMap', () => {
23 describe('constructor', () => {
24 const shouldThrow = [-1, 1.5, Number.NaN, Number.POSITIVE_INFINITY];
25 const shouldNotThrow = [0, 1, Number.MAX_VALUE];
26
27 test.each(shouldNotThrow)('`() => new CacheMap(%d)` should not throw', (param) => {
28 expect(() => new CacheMap(param)).not.toThrowError();
29 });
30 test.each(shouldThrow)('`() => new CacheMap(%d)` should throw', (param) => {
31 expect(() => new CacheMap(param)).toThrowError();
32 });
33 });
34
35 describe('api', () => {
36 /* eslint-disable @stylistic/no-multi-spaces */
37 const data = [
38 {
39 maxSize: 1,
40 expectedSize: 0,
41 calls: [],
42 },
43 {
44 maxSize: 10,
45 expectedSize: 1,
46 calls: [
47 {func: 'get', args: ['a1-b-c'], returnValue: void 0},
48 {func: 'has', args: ['a1-b-c'], returnValue: false},
49 {func: 'set', args: ['a1-b-c', 32], returnValue: void 0},
50 {func: 'get', args: ['a1-b-c'], returnValue: 32},
51 {func: 'has', args: ['a1-b-c'], returnValue: true},
52 ],
53 },
54 {
55 maxSize: 10,
56 expectedSize: 2,
57 calls: [
58 {func: 'set', args: ['a1-b-c', 32], returnValue: void 0},
59 {func: 'get', args: ['a1-b-c'], returnValue: 32},
60 {func: 'set', args: ['a1-b-c', 64], returnValue: void 0},
61 {func: 'get', args: ['a1-b-c'], returnValue: 64},
62 {func: 'set', args: ['a2-b-c', 96], returnValue: void 0},
63 {func: 'get', args: ['a2-b-c'], returnValue: 96},
64 ],
65 },
66 {
67 maxSize: 2,
68 expectedSize: 2,
69 calls: [
70 {func: 'has', args: ['a1-b-c'], returnValue: false},
71 {func: 'has', args: ['a2-b-c'], returnValue: false},
72 {func: 'has', args: ['a3-b-c'], returnValue: false},
73 {func: 'set', args: ['a1-b-c', 1], returnValue: void 0},
74 {func: 'has', args: ['a1-b-c'], returnValue: true},
75 {func: 'has', args: ['a2-b-c'], returnValue: false},
76 {func: 'has', args: ['a3-b-c'], returnValue: false},
77 {func: 'set', args: ['a2-b-c', 2], returnValue: void 0},
78 {func: 'has', args: ['a1-b-c'], returnValue: true},
79 {func: 'has', args: ['a2-b-c'], returnValue: true},
80 {func: 'has', args: ['a3-b-c'], returnValue: false},
81 {func: 'set', args: ['a3-b-c', 3], returnValue: void 0},
82 {func: 'has', args: ['a1-b-c'], returnValue: false},
83 {func: 'has', args: ['a2-b-c'], returnValue: true},
84 {func: 'has', args: ['a3-b-c'], returnValue: true},
85 ],
86 },
87 ];
88 /* eslint-enable @stylistic/no-multi-spaces */
89
90 test.each(data)('api-test-%#', ({maxSize, expectedSize, calls}) => {
91 const cache = new CacheMap(maxSize);
92 expect(cache.maxSize).toStrictEqual(maxSize);
93 for (const call of calls) {
94 const {func, args} = call;
95 let returnValue;
96 switch (func) {
97 case 'get': returnValue = cache.get(args[0]); break;
98 case 'set': returnValue = cache.set(args[0], args[1]); break;
99 case 'has': returnValue = cache.has(args[0]); break;
100 case 'clear': returnValue = cache.clear(); break;
101 }
102 if (Object.prototype.hasOwnProperty.call(call, 'returnValue')) {
103 const {returnValue: expectedReturnValue} = call;
104 expect(returnValue).toStrictEqual(expectedReturnValue);
105 }
106 }
107 expect(cache.size).toStrictEqual(expectedSize);
108 });
109 });
110});