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

Unified Diff: test/elemHide.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/domainRestrictions.js ('k') | test/filterClasses.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/elemHide.js
===================================================================
--- a/test/elemHide.js
+++ b/test/elemHide.js
@@ -12,322 +12,325 @@
* 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 ElemHide = null;
let createStyleSheet = null;
let rulesFromStyleSheet = null;
let ElemHideExceptions = null;
let Filter = null;
let filtersByDomain = null;
let selectorGroupSize = null;
-exports.setUp = function(callback)
+describe("ElemHide", () =>
{
- let sandboxedRequire = createSandbox({
- extraExports: {
- elemHide: ["filtersByDomain", "selectorGroupSize"]
- }
+ beforeEach(() =>
+ {
+ let sandboxedRequire = createSandbox({
+ extraExports: {
+ elemHide: ["filtersByDomain", "selectorGroupSize"]
+ }
+ });
+ (
+ {ElemHide, createStyleSheet, rulesFromStyleSheet,
+ filtersByDomain, selectorGroupSize} = sandboxedRequire("../lib/elemHide"),
+ {ElemHideExceptions} = sandboxedRequire("../lib/elemHideExceptions"),
+ {Filter} = sandboxedRequire("../lib/filterClasses")
+ );
});
- (
- {ElemHide, createStyleSheet, rulesFromStyleSheet,
- filtersByDomain, selectorGroupSize} = sandboxedRequire("../lib/elemHide"),
- {ElemHideExceptions} = sandboxedRequire("../lib/elemHideExceptions"),
- {Filter} = sandboxedRequire("../lib/filterClasses")
- );
-
- callback();
-};
-function normalizeSelectors(selectors)
-{
- // generateStyleSheetForDomain is currently allowed to return duplicate
- // selectors for performance reasons, so we need to remove duplicates here.
- return selectors.slice().sort().filter((selector, index, sortedSelectors) =>
+ function normalizeSelectors(selectors)
{
- return index == 0 || selector != sortedSelectors[index - 1];
- });
-}
+ // generateStyleSheetForDomain is currently allowed to return duplicate
+ // selectors for performance reasons, so we need to remove duplicates here.
+ return selectors.slice().sort().filter((selector, index, sortedSelectors) =>
+ {
+ return index == 0 || selector != sortedSelectors[index - 1];
+ });
+ }
-function testResult(test, domain, expectedSelectors, specificOnly)
-{
- let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors);
+ function testResult(domain, expectedSelectors, specificOnly)
+ {
+ let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors);
- let {code, selectors} =
- ElemHide.generateStyleSheetForDomain(domain, specificOnly, true);
+ let {code, selectors} =
+ ElemHide.generateStyleSheetForDomain(domain, specificOnly, true);
- test.deepEqual(normalizeSelectors(selectors), normalizedExpectedSelectors);
+ assert.deepEqual(normalizeSelectors(selectors), normalizedExpectedSelectors);
- // Make sure each expected selector is in the actual CSS code.
- for (let selector of normalizedExpectedSelectors)
- {
- test.ok(code.includes(selector + ", ") ||
- code.includes(selector + " {display: none !important;}\n"));
+ // Make sure each expected selector is in the actual CSS code.
+ for (let selector of normalizedExpectedSelectors)
+ {
+ assert.ok(code.includes(selector + ", ") ||
+ code.includes(selector + " {display: none !important;}\n"));
+ }
}
-}
-exports.testGenerateStyleSheetForDomain = function(test)
-{
- let addFilter = filterText => ElemHide.add(Filter.fromText(filterText));
- let removeFilter = filterText => ElemHide.remove(Filter.fromText(filterText));
- let addException =
- filterText => ElemHideExceptions.add(Filter.fromText(filterText));
- let removeException =
- filterText => ElemHideExceptions.remove(Filter.fromText(filterText));
+ it("Generating Style Sheet", () =>
+ {
+ let addFilter = filterText => ElemHide.add(Filter.fromText(filterText));
+ let removeFilter =
+ filterText => ElemHide.remove(Filter.fromText(filterText));
+ let addException =
+ filterText => ElemHideExceptions.add(Filter.fromText(filterText));
+ let removeException =
+ filterText => ElemHideExceptions.remove(Filter.fromText(filterText));
- testResult(test, "", []);
+ testResult("", []);
- addFilter("~foo.example.com,example.com##foo");
- testResult(test, "barfoo.example.com", ["foo"]);
- testResult(test, "bar.foo.example.com", []);
- testResult(test, "foo.example.com", []);
- testResult(test, "example.com", ["foo"]);
- testResult(test, "com", []);
- testResult(test, "", []);
+ addFilter("~foo.example.com,example.com##foo");
+ testResult("barfoo.example.com", ["foo"]);
+ testResult("bar.foo.example.com", []);
+ testResult("foo.example.com", []);
+ testResult("example.com", ["foo"]);
+ testResult("com", []);
+ testResult("", []);
- addFilter("foo.example.com##turnip");
- testResult(test, "foo.example.com", ["turnip"]);
- testResult(test, "example.com", ["foo"]);
- testResult(test, "com", []);
- testResult(test, "", []);
+ addFilter("foo.example.com##turnip");
+ testResult("foo.example.com", ["turnip"]);
+ testResult("example.com", ["foo"]);
+ testResult("com", []);
+ testResult("", []);
- addException("example.com#@#foo");
- testResult(test, "foo.example.com", ["turnip"]);
- testResult(test, "example.com", []);
- testResult(test, "com", []);
- testResult(test, "", []);
+ addException("example.com#@#foo");
+ testResult("foo.example.com", ["turnip"]);
+ testResult("example.com", []);
+ testResult("com", []);
+ testResult("", []);
- addFilter("com##bar");
- testResult(test, "foo.example.com", ["turnip", "bar"]);
- testResult(test, "example.com", ["bar"]);
- testResult(test, "com", ["bar"]);
- testResult(test, "", []);
+ addFilter("com##bar");
+ testResult("foo.example.com", ["turnip", "bar"]);
+ testResult("example.com", ["bar"]);
+ testResult("com", ["bar"]);
+ testResult("", []);
- addException("example.com#@#bar");
- testResult(test, "foo.example.com", ["turnip"]);
- testResult(test, "example.com", []);
- testResult(test, "com", ["bar"]);
- testResult(test, "", []);
+ addException("example.com#@#bar");
+ testResult("foo.example.com", ["turnip"]);
+ testResult("example.com", []);
+ testResult("com", ["bar"]);
+ testResult("", []);
- removeException("example.com#@#foo");
- testResult(test, "foo.example.com", ["turnip"]);
- testResult(test, "example.com", ["foo"]);
- testResult(test, "com", ["bar"]);
- testResult(test, "", []);
+ removeException("example.com#@#foo");
+ testResult("foo.example.com", ["turnip"]);
+ testResult("example.com", ["foo"]);
+ testResult("com", ["bar"]);
+ testResult("", []);
- removeException("example.com#@#bar");
- testResult(test, "foo.example.com", ["turnip", "bar"]);
- testResult(test, "example.com", ["foo", "bar"]);
- testResult(test, "com", ["bar"]);
- testResult(test, "", []);
+ removeException("example.com#@#bar");
+ testResult("foo.example.com", ["turnip", "bar"]);
+ testResult("example.com", ["foo", "bar"]);
+ testResult("com", ["bar"]);
+ testResult("", []);
- addFilter("##generic");
- testResult(test, "foo.example.com", ["turnip", "bar", "generic"]);
- testResult(test, "example.com", ["foo", "bar", "generic"]);
- testResult(test, "com", ["bar", "generic"]);
- testResult(test, "", ["generic"]);
- testResult(test, "foo.example.com", ["turnip", "bar"], true);
- testResult(test, "example.com", ["foo", "bar"], true);
- testResult(test, "com", ["bar"], true);
- testResult(test, "", [], true);
- removeFilter("##generic");
+ addFilter("##generic");
+ testResult("foo.example.com", ["turnip", "bar", "generic"]);
+ testResult("example.com", ["foo", "bar", "generic"]);
+ testResult("com", ["bar", "generic"]);
+ testResult("", ["generic"]);
+ testResult("foo.example.com", ["turnip", "bar"], true);
+ testResult("example.com", ["foo", "bar"], true);
+ testResult("com", ["bar"], true);
+ testResult("", [], true);
+ removeFilter("##generic");
- addFilter("~adblockplus.org##example");
- testResult(test, "adblockplus.org", []);
- testResult(test, "", ["example"]);
- testResult(test, "foo.example.com", ["turnip", "bar", "example"]);
- testResult(test, "foo.example.com", ["turnip", "bar"], true);
- removeFilter("~adblockplus.org##example");
+ addFilter("~adblockplus.org##example");
+ testResult("adblockplus.org", []);
+ testResult("", ["example"]);
+ testResult("foo.example.com", ["turnip", "bar", "example"]);
+ testResult("foo.example.com", ["turnip", "bar"], true);
+ removeFilter("~adblockplus.org##example");
- removeFilter("~foo.example.com,example.com##foo");
- testResult(test, "foo.example.com", ["turnip", "bar"]);
- testResult(test, "example.com", ["bar"]);
- testResult(test, "com", ["bar"]);
- testResult(test, "", []);
+ removeFilter("~foo.example.com,example.com##foo");
+ testResult("foo.example.com", ["turnip", "bar"]);
+ testResult("example.com", ["bar"]);
+ testResult("com", ["bar"]);
+ testResult("", []);
- removeFilter("com##bar");
- testResult(test, "foo.example.com", ["turnip"]);
- testResult(test, "example.com", []);
- testResult(test, "com", []);
- testResult(test, "", []);
+ removeFilter("com##bar");
+ testResult("foo.example.com", ["turnip"]);
+ testResult("example.com", []);
+ testResult("com", []);
+ testResult("", []);
- removeFilter("foo.example.com##turnip");
- testResult(test, "foo.example.com", []);
- testResult(test, "example.com", []);
- testResult(test, "com", []);
- testResult(test, "", []);
+ removeFilter("foo.example.com##turnip");
+ testResult("foo.example.com", []);
+ testResult("example.com", []);
+ testResult("com", []);
+ testResult("", []);
- addFilter("example.com##dupe");
- addFilter("example.com##dupe");
- testResult(test, "example.com", ["dupe"]);
- removeFilter("example.com##dupe");
- testResult(test, "example.com", []);
- removeFilter("example.com##dupe");
+ addFilter("example.com##dupe");
+ addFilter("example.com##dupe");
+ testResult("example.com", ["dupe"]);
+ removeFilter("example.com##dupe");
+ testResult("example.com", []);
+ removeFilter("example.com##dupe");
- addFilter("~foo.example.com,example.com##foo");
+ addFilter("~foo.example.com,example.com##foo");
- addFilter("##foo");
- testResult(test, "foo.example.com", ["foo"]);
- testResult(test, "example.com", ["foo"]);
- testResult(test, "com", ["foo"]);
- testResult(test, "", ["foo"]);
- removeFilter("##foo");
+ addFilter("##foo");
+ testResult("foo.example.com", ["foo"]);
+ testResult("example.com", ["foo"]);
+ testResult("com", ["foo"]);
+ testResult("", ["foo"]);
+ removeFilter("##foo");
- addFilter("example.org##foo");
- testResult(test, "foo.example.com", []);
- testResult(test, "example.com", ["foo"]);
- testResult(test, "com", []);
- testResult(test, "", []);
- removeFilter("example.org##foo");
+ addFilter("example.org##foo");
+ testResult("foo.example.com", []);
+ testResult("example.com", ["foo"]);
+ testResult("com", []);
+ testResult("", []);
+ removeFilter("example.org##foo");
- addFilter("~example.com##foo");
- testResult(test, "foo.example.com", []);
- testResult(test, "example.com", ["foo"]);
- testResult(test, "com", ["foo"]);
- testResult(test, "", ["foo"]);
- removeFilter("~example.com##foo");
+ addFilter("~example.com##foo");
+ testResult("foo.example.com", []);
+ testResult("example.com", ["foo"]);
+ testResult("com", ["foo"]);
+ testResult("", ["foo"]);
+ removeFilter("~example.com##foo");
- removeFilter("~foo.example.com,example.com##foo");
+ removeFilter("~foo.example.com,example.com##foo");
- // Test criteria
- addFilter("##hello");
- addFilter("~example.com##world");
- addFilter("foo.com##specific");
- testResult(test, "foo.com", ["specific"], true);
- testResult(test, "foo.com", ["hello", "specific", "world"], false);
- testResult(test, "foo.com", ["hello", "specific", "world"]);
- testResult(test, "foo.com.", ["hello", "specific", "world"]);
- testResult(test, "example.com", [], true);
- removeFilter("foo.com##specific");
- removeFilter("~example.com##world");
- removeFilter("##hello");
- testResult(test, "foo.com", []);
+ // Test criteria
+ addFilter("##hello");
+ addFilter("~example.com##world");
+ addFilter("foo.com##specific");
+ testResult("foo.com", ["specific"], true);
+ testResult("foo.com", ["hello", "specific", "world"], false);
+ testResult("foo.com", ["hello", "specific", "world"]);
+ testResult("foo.com.", ["hello", "specific", "world"]);
+ testResult("example.com", [], true);
+ removeFilter("foo.com##specific");
+ removeFilter("~example.com##world");
+ removeFilter("##hello");
+ testResult("foo.com", []);
- addFilter("##hello");
- testResult(test, "foo.com", [], true);
- testResult(test, "foo.com", ["hello"], false);
- testResult(test, "foo.com", ["hello"]);
- testResult(test, "bar.com", [], true);
- testResult(test, "bar.com", ["hello"], false);
- testResult(test, "bar.com", ["hello"]);
- addException("foo.com#@#hello");
- testResult(test, "foo.com", [], true);
- testResult(test, "foo.com", [], false);
- testResult(test, "foo.com", []);
- testResult(test, "bar.com", [], true);
- testResult(test, "bar.com", ["hello"], false);
- testResult(test, "bar.com", ["hello"]);
- removeException("foo.com#@#hello");
- testResult(test, "foo.com", [], true);
- // Note: We don't take care to track conditional selectors which became
- // unconditional when a filter was removed. This was too expensive.
- testResult(test, "foo.com", ["hello"], false);
- testResult(test, "foo.com", ["hello"]);
- testResult(test, "bar.com", [], true);
- testResult(test, "bar.com", ["hello"], false);
- testResult(test, "bar.com", ["hello"]);
- removeFilter("##hello");
- testResult(test, "foo.com", []);
- testResult(test, "bar.com", []);
+ addFilter("##hello");
+ testResult("foo.com", [], true);
+ testResult("foo.com", ["hello"], false);
+ testResult("foo.com", ["hello"]);
+ testResult("bar.com", [], true);
+ testResult("bar.com", ["hello"], false);
+ testResult("bar.com", ["hello"]);
+ addException("foo.com#@#hello");
+ testResult("foo.com", [], true);
+ testResult("foo.com", [], false);
+ testResult("foo.com", []);
+ testResult("bar.com", [], true);
+ testResult("bar.com", ["hello"], false);
+ testResult("bar.com", ["hello"]);
+ removeException("foo.com#@#hello");
+ testResult("foo.com", [], true);
+ // Note: We don't take care to track conditional selectors which became
+ // unconditional when a filter was removed. This was too expensive.
+ testResult("foo.com", ["hello"], false);
+ testResult("foo.com", ["hello"]);
+ testResult("bar.com", [], true);
+ testResult("bar.com", ["hello"], false);
+ testResult("bar.com", ["hello"]);
+ removeFilter("##hello");
+ testResult("foo.com", []);
+ testResult("bar.com", []);
- addFilter("##hello");
- addFilter("foo.com##hello");
- testResult(test, "foo.com", ["hello"]);
- removeFilter("foo.com##hello");
- testResult(test, "foo.com", ["hello"]);
- removeFilter("##hello");
- testResult(test, "foo.com", []);
+ addFilter("##hello");
+ addFilter("foo.com##hello");
+ testResult("foo.com", ["hello"]);
+ removeFilter("foo.com##hello");
+ testResult("foo.com", ["hello"]);
+ removeFilter("##hello");
+ testResult("foo.com", []);
- addFilter("##hello");
- addFilter("foo.com##hello");
- testResult(test, "foo.com", ["hello"]);
- removeFilter("##hello");
- testResult(test, "foo.com", ["hello"]);
- removeFilter("foo.com##hello");
- testResult(test, "foo.com", []);
-
- test.done();
-};
+ addFilter("##hello");
+ addFilter("foo.com##hello");
+ testResult("foo.com", ["hello"]);
+ removeFilter("##hello");
+ testResult("foo.com", ["hello"]);
+ removeFilter("foo.com##hello");
+ testResult("foo.com", []);
+ });
-exports.testZeroFilterKey = function(test)
-{
- ElemHide.add(Filter.fromText("##test"));
- ElemHideExceptions.add(Filter.fromText("foo.com#@#test"));
- testResult(test, "foo.com", []);
- testResult(test, "bar.com", ["test"]);
- test.done();
-};
+ it("Zero filter key", () =>
+ {
+ ElemHide.add(Filter.fromText("##test"));
+ ElemHideExceptions.add(Filter.fromText("foo.com#@#test"));
+ testResult("foo.com", []);
+ testResult("bar.com", ["test"]);
+ });
-exports.testFiltersByDomain = function(test)
-{
- test.equal(filtersByDomain.size, 0);
+ it("Filters by Domain", () =>
+ {
+ assert.equal(filtersByDomain.size, 0);
- ElemHide.add(Filter.fromText("##test"));
- test.equal(filtersByDomain.size, 0);
+ ElemHide.add(Filter.fromText("##test"));
+ assert.equal(filtersByDomain.size, 0);
- ElemHide.add(Filter.fromText("example.com##test"));
- test.equal(filtersByDomain.size, 1);
-
- ElemHide.add(Filter.fromText("example.com,~www.example.com##test"));
- test.equal(filtersByDomain.size, 2);
+ ElemHide.add(Filter.fromText("example.com##test"));
+ assert.equal(filtersByDomain.size, 1);
- ElemHide.remove(Filter.fromText("example.com##test"));
- test.equal(filtersByDomain.size, 2);
+ ElemHide.add(Filter.fromText("example.com,~www.example.com##test"));
+ assert.equal(filtersByDomain.size, 2);
- ElemHide.remove(Filter.fromText("example.com,~www.example.com##test"));
- test.equal(filtersByDomain.size, 0);
+ ElemHide.remove(Filter.fromText("example.com##test"));
+ assert.equal(filtersByDomain.size, 2);
- test.done();
-};
+ ElemHide.remove(Filter.fromText("example.com,~www.example.com##test"));
+ assert.equal(filtersByDomain.size, 0);
+ });
-exports.testCreateStyleSheet = function(test)
-{
- test.equal(
- createStyleSheet([
- "html", "#foo", ".bar", "#foo .bar", "#foo > .bar",
- "#foo[data-bar='bar']"
- ]),
- "html, #foo, .bar, #foo .bar, #foo > .bar, #foo[data-bar='bar'] " +
- "{display: none !important;}\n",
- "Style sheet creation should work"
- );
+ describe("Creating Stylesheet", () =>
+ {
+ it("Basic creation", () =>
+ {
+ assert.equal(
+ createStyleSheet([
+ "html", "#foo", ".bar", "#foo .bar", "#foo > .bar",
+ "#foo[data-bar='bar']"
+ ]),
+ "html, #foo, .bar, #foo .bar, #foo > .bar, #foo[data-bar='bar'] " +
+ "{display: none !important;}\n",
+ "Style sheet creation should work"
+ );
+ });
- let selectors = new Array(50000).fill().map((element, index) => ".s" + index);
+ it("Splitting", () =>
+ {
+ let selectors = new Array(50000).fill().map((element, index) => ".s" + index);
- test.equal((createStyleSheet(selectors).match(/\n/g) || []).length,
- Math.ceil(50000 / selectorGroupSize),
- "Style sheet should be split up into rules with at most " +
- selectorGroupSize + " selectors each");
+ assert.equal((createStyleSheet(selectors).match(/\n/g) || []).length,
+ Math.ceil(50000 / selectorGroupSize),
+ "Style sheet should be split up into rules with at most " +
+ selectorGroupSize + " selectors each");
+ });
- test.equal(
- createStyleSheet([
- "html", "#foo", ".bar", "#foo .bar", "#foo > .bar",
- "#foo[data-bar='{foo: 1}']"
- ]),
- "html, #foo, .bar, #foo .bar, #foo > .bar, " +
- "#foo[data-bar='\\7B foo: 1\\7D '] {display: none !important;}\n",
- "Braces should be escaped"
- );
-
- test.done();
-};
+ it("Escaping", () =>
+ {
+ assert.equal(
+ createStyleSheet([
+ "html", "#foo", ".bar", "#foo .bar", "#foo > .bar",
+ "#foo[data-bar='{foo: 1}']"
+ ]),
+ "html, #foo, .bar, #foo .bar, #foo > .bar, " +
+ "#foo[data-bar='\\7B foo: 1\\7D '] {display: none !important;}\n",
+ "Braces should be escaped"
+ );
+ });
+ });
-exports.testRulesFromStyleSheet = function(test)
-{
- // Note: The rulesFromStyleSheet function assumes that each rule will be
- // terminated with a newline character, including the last rule. If this is
- // not the case, the function goes into an infinite loop. It should only be
- // used with the return value of the createStyleSheet function.
+ it("Rules from StyleSheet", () =>
+ {
+ // Note: The rulesFromStyleSheet function assumes that each rule will be
+ // terminated with a newline character, including the last rule. If this is
+ // not the case, the function goes into an infinite loop. It should only be
+ // used with the return value of the createStyleSheet function.
- test.deepEqual([...rulesFromStyleSheet("")], []);
- test.deepEqual([...rulesFromStyleSheet("#foo {}\n")], ["#foo {}"]);
- test.deepEqual([...rulesFromStyleSheet("#foo {}\n#bar {}\n")],
- ["#foo {}", "#bar {}"]);
-
- test.done();
-};
+ assert.deepEqual([...rulesFromStyleSheet("")], []);
+ assert.deepEqual([...rulesFromStyleSheet("#foo {}\n")], ["#foo {}"]);
+ assert.deepEqual([...rulesFromStyleSheet("#foo {}\n#bar {}\n")],
+ ["#foo {}", "#bar {}"]);
+ });
+});
« no previous file with comments | « test/domainRestrictions.js ('k') | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld