Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/caching.js

Issue 30025555: Issue 6820 - Move tests to mocha (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created March 7, 2019, 1:14 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/browser/snippets.js ('k') | test/domainRestrictions.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const assert = require("assert");
20 const {createSandbox} = require("./_common"); 21 const {createSandbox} = require("./_common");
21 22
22 let Cache = null; 23 let Cache = null;
23 24
24 exports.setUp = function(callback) 25 describe("Caching", () =>
25 { 26 {
26 let sandboxedRequire = createSandbox(); 27 beforeEach(() =>
27 ( 28 {
28 {Cache} = sandboxedRequire("../lib/caching") 29 let sandboxedRequire = createSandbox();
29 ); 30 (
31 {Cache} = sandboxedRequire("../lib/caching")
32 );
33 });
30 34
31 callback(); 35 it("Cache", () =>
32 }; 36 {
37 // A capacity must be specificed and it must be coercable to a positive
38 // number greater than or equal to one.
39 assert.throws(() => { new Cache(); },
40 "capacity must be a positive number.");
41 assert.throws(() => { new Cache(0); },
42 "capacity must be a positive number.");
43 assert.throws(() => { new Cache(-1); },
44 "capacity must be a positive number.");
45 assert.throws(() => { new Cache(0.1); },
46 "capacity must be a positive number.");
47 assert.throws(() => { new Cache(Number.MIN_VALUE); },
48 "capacity must be a positive number.");
49 assert.throws(() => { new Cache(-Infinity); },
50 "capacity must be a positive number.");
51 assert.throws(() => { new Cache("ten"); },
52 "capacity must be a positive number.");
53 assert.doesNotThrow(() => { new Cache(1); },
54 "capacity must be a positive number.");
55 assert.doesNotThrow(() => { new Cache(1.1); },
56 "capacity must be a positive number.");
57 assert.doesNotThrow(() => { new Cache(10); },
58 "capacity must be a positive number.");
59 assert.doesNotThrow(() => { new Cache(Number.MAX_VALUE); },
60 "capacity must be a positive number.");
61 assert.doesNotThrow(() => { new Cache(Infinity); },
62 "capacity must be a positive number.");
63 assert.doesNotThrow(() => { new Cache("10"); },
64 "capacity must be a positive number.");
33 65
34 exports.testCache = function(test) 66 let cache = new Cache(100);
35 {
36 // A capacity must be specificed and it must be coercable to a positive
37 // number greater than or equal to one.
38 test.throws(() => { new Cache(); }, "capacity must be a positive number.");
39 test.throws(() => { new Cache(0); }, "capacity must be a positive number.");
40 test.throws(() => { new Cache(-1); }, "capacity must be a positive number.");
41 test.throws(() => { new Cache(0.1); }, "capacity must be a positive number.");
42 test.throws(() => { new Cache(Number.MIN_VALUE); },
43 "capacity must be a positive number.");
44 test.throws(() => { new Cache(-Infinity); },
45 "capacity must be a positive number.");
46 test.throws(() => { new Cache("ten"); },
47 "capacity must be a positive number.");
48 test.doesNotThrow(() => { new Cache(1); },
49 "capacity must be a positive number.");
50 test.doesNotThrow(() => { new Cache(1.1); },
51 "capacity must be a positive number.");
52 test.doesNotThrow(() => { new Cache(10); },
53 "capacity must be a positive number.");
54 test.doesNotThrow(() => { new Cache(Number.MAX_VALUE); },
55 "capacity must be a positive number.");
56 test.doesNotThrow(() => { new Cache(Infinity); },
57 "capacity must be a positive number.");
58 test.doesNotThrow(() => { new Cache("10"); },
59 "capacity must be a positive number.");
60 67
61 let cache = new Cache(100); 68 cache.set("1", "one");
69 assert.equal(cache.get("1"), "one");
62 70
63 cache.set("1", "one"); 71 cache.set(2, "two");
64 test.equal(cache.get("1"), "one"); 72 assert.equal(cache.get(2), "two");
65 73
66 cache.set(2, "two"); 74 // No type coercion.
67 test.equal(cache.get(2), "two"); 75 assert.equal(cache.get("2"), undefined);
68 76
69 // No type coercion. 77 // Neither key nor value can be undefined.
70 test.equal(cache.get("2"), undefined); 78 assert.throws(() => { cache.set(undefined, "three"); },
79 "key must not be undefined.");
80 assert.throws(() => { cache.set(4, undefined); },
81 "value must not be undefined.");
71 82
72 // Neither key nor value can be undefined. 83 // Keys and values can be null.
73 test.throws(() => { cache.set(undefined, "three"); }, 84 cache.set(null, "five");
74 "key must not be undefined."); 85 cache.set(5, null);
75 test.throws(() => { cache.set(4, undefined); },
76 "value must not be undefined.");
77 86
78 // Keys and values can be null. 87 cache.clear();
79 cache.set(null, "five");
80 cache.set(5, null);
81 88
82 cache.clear(); 89 assert.equal(cache.get("1"), undefined);
90 assert.equal(cache.get(2), undefined);
91 assert.equal(cache.get(null), undefined);
92 assert.equal(cache.get(5), undefined);
83 93
84 test.equal(cache.get("1"), undefined); 94 // Fill to capacity.
85 test.equal(cache.get(2), undefined); 95 for (let i = 0; i < 100; i++)
86 test.equal(cache.get(null), undefined); 96 cache.set(i, i);
87 test.equal(cache.get(5), undefined);
88 97
89 // Fill to capacity. 98 // All entries exist.
90 for (let i = 0; i < 100; i++) 99 for (let i = 0; i < 100; i++)
91 cache.set(i, i); 100 assert.equal(cache.get(i), i);
92 101
93 // All entries exist. 102 // Add an existing entry again.
94 for (let i = 0; i < 100; i++) 103 cache.set(0, 0);
95 test.equal(cache.get(i), i);
96 104
97 // Add an existing entry again. 105 // All entries still exist.
98 cache.set(0, 0); 106 for (let i = 0; i < 100; i++)
107 assert.equal(cache.get(i), i);
99 108
100 // All entries still exist. 109 // Exceed capacity.
101 for (let i = 0; i < 100; i++) 110 cache.set(100, 100);
102 test.equal(cache.get(i), i);
103 111
104 // Exceed capacity. 112 // Only the last entry exists.
105 cache.set(100, 100); 113 for (let i = 0; i <= 100; i++)
106 114 assert.equal(cache.get(i), i == 100 ? 100 : undefined);
107 // Only the last entry exists. 115 });
108 for (let i = 0; i <= 100; i++) 116 });
109 test.equal(cache.get(i), i == 100 ? 100 : undefined);
110
111 test.done();
112 };
OLDNEW
« no previous file with comments | « test/browser/snippets.js ('k') | test/domainRestrictions.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld