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

Unified Diff: test/caching.js

Issue 30025555: Issue 6820 - Move tests to mocha (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebased. Created April 5, 2019, 4:16 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/browser/snippets.js ('k') | test/domainRestrictions.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/caching.js
===================================================================
--- a/test/caching.js
+++ b/test/caching.js
@@ -12,101 +12,105 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
+const assert = require("assert");
const {createSandbox} = require("./_common");
let Cache = null;
-exports.setUp = function(callback)
-{
- let sandboxedRequire = createSandbox();
- (
- {Cache} = sandboxedRequire("../lib/caching")
- );
-
- callback();
-};
-
-exports.testCache = function(test)
+describe("Caching", () =>
{
- // A capacity must be specificed and it must be coercable to a positive
- // number greater than or equal to one.
- test.throws(() => { new Cache(); }, "capacity must be a positive number.");
- test.throws(() => { new Cache(0); }, "capacity must be a positive number.");
- test.throws(() => { new Cache(-1); }, "capacity must be a positive number.");
- test.throws(() => { new Cache(0.1); }, "capacity must be a positive number.");
- test.throws(() => { new Cache(Number.MIN_VALUE); },
- "capacity must be a positive number.");
- test.throws(() => { new Cache(-Infinity); },
- "capacity must be a positive number.");
- test.throws(() => { new Cache("ten"); },
- "capacity must be a positive number.");
- test.doesNotThrow(() => { new Cache(1); },
- "capacity must be a positive number.");
- test.doesNotThrow(() => { new Cache(1.1); },
- "capacity must be a positive number.");
- test.doesNotThrow(() => { new Cache(10); },
- "capacity must be a positive number.");
- test.doesNotThrow(() => { new Cache(Number.MAX_VALUE); },
- "capacity must be a positive number.");
- test.doesNotThrow(() => { new Cache(Infinity); },
- "capacity must be a positive number.");
- test.doesNotThrow(() => { new Cache("10"); },
- "capacity must be a positive number.");
+ beforeEach(() =>
+ {
+ let sandboxedRequire = createSandbox();
+ (
+ {Cache} = sandboxedRequire("../lib/caching")
+ );
+ });
- let cache = new Cache(100);
+ it("Cache", () =>
+ {
+ // A capacity must be specificed and it must be coercable to a positive
+ // number greater than or equal to one.
+ assert.throws(() => { new Cache(); },
+ "capacity must be a positive number.");
+ assert.throws(() => { new Cache(0); },
+ "capacity must be a positive number.");
+ assert.throws(() => { new Cache(-1); },
+ "capacity must be a positive number.");
+ assert.throws(() => { new Cache(0.1); },
+ "capacity must be a positive number.");
+ assert.throws(() => { new Cache(Number.MIN_VALUE); },
+ "capacity must be a positive number.");
+ assert.throws(() => { new Cache(-Infinity); },
+ "capacity must be a positive number.");
+ assert.throws(() => { new Cache("ten"); },
+ "capacity must be a positive number.");
+ assert.doesNotThrow(() => { new Cache(1); },
+ "capacity must be a positive number.");
+ assert.doesNotThrow(() => { new Cache(1.1); },
+ "capacity must be a positive number.");
+ assert.doesNotThrow(() => { new Cache(10); },
+ "capacity must be a positive number.");
+ assert.doesNotThrow(() => { new Cache(Number.MAX_VALUE); },
+ "capacity must be a positive number.");
+ assert.doesNotThrow(() => { new Cache(Infinity); },
+ "capacity must be a positive number.");
+ assert.doesNotThrow(() => { new Cache("10"); },
+ "capacity must be a positive number.");
- cache.set("1", "one");
- test.equal(cache.get("1"), "one");
+ let cache = new Cache(100);
- cache.set(2, "two");
- test.equal(cache.get(2), "two");
+ cache.set("1", "one");
+ assert.equal(cache.get("1"), "one");
- // No type coercion.
- test.equal(cache.get("2"), undefined);
+ cache.set(2, "two");
+ assert.equal(cache.get(2), "two");
- // Neither key nor value can be undefined.
- test.throws(() => { cache.set(undefined, "three"); },
- "key must not be undefined.");
- test.throws(() => { cache.set(4, undefined); },
- "value must not be undefined.");
+ // No type coercion.
+ assert.equal(cache.get("2"), undefined);
- // Keys and values can be null.
- cache.set(null, "five");
- cache.set(5, null);
+ // Neither key nor value can be undefined.
+ assert.throws(() => { cache.set(undefined, "three"); },
+ "key must not be undefined.");
+ assert.throws(() => { cache.set(4, undefined); },
+ "value must not be undefined.");
- cache.clear();
+ // Keys and values can be null.
+ cache.set(null, "five");
+ cache.set(5, null);
- test.equal(cache.get("1"), undefined);
- test.equal(cache.get(2), undefined);
- test.equal(cache.get(null), undefined);
- test.equal(cache.get(5), undefined);
+ cache.clear();
+
+ assert.equal(cache.get("1"), undefined);
+ assert.equal(cache.get(2), undefined);
+ assert.equal(cache.get(null), undefined);
+ assert.equal(cache.get(5), undefined);
- // Fill to capacity.
- for (let i = 0; i < 100; i++)
- cache.set(i, i);
+ // Fill to capacity.
+ for (let i = 0; i < 100; i++)
+ cache.set(i, i);
- // All entries exist.
- for (let i = 0; i < 100; i++)
- test.equal(cache.get(i), i);
+ // All entries exist.
+ for (let i = 0; i < 100; i++)
+ assert.equal(cache.get(i), i);
- // Add an existing entry again.
- cache.set(0, 0);
+ // Add an existing entry again.
+ cache.set(0, 0);
- // All entries still exist.
- for (let i = 0; i < 100; i++)
- test.equal(cache.get(i), i);
-
- // Exceed capacity.
- cache.set(100, 100);
+ // All entries still exist.
+ for (let i = 0; i < 100; i++)
+ assert.equal(cache.get(i), i);
- // Only the last entry exists.
- for (let i = 0; i <= 100; i++)
- test.equal(cache.get(i), i == 100 ? 100 : undefined);
+ // Exceed capacity.
+ cache.set(100, 100);
- test.done();
-};
+ // Only the last entry exists.
+ for (let i = 0; i <= 100; i++)
+ assert.equal(cache.get(i), i == 100 ? 100 : undefined);
+ });
+});
« 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