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

Unified Diff: test/subscriptionClasses.js

Issue 29934588: Issue 7094 - Encapsulate management of subscription filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Remove hasFilter and related code Created Nov. 18, 2018, 10:21 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
Index: test/subscriptionClasses.js
===================================================================
--- a/test/subscriptionClasses.js
+++ b/test/subscriptionClasses.js
@@ -14,31 +14,37 @@
* 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 {createSandbox} = require("./_common");
+let f$ = null;
+
let Subscription = null;
let SpecialSubscription = null;
let DownloadableSubscription = null;
let RegularSubscription = null;
let ExternalSubscription = null;
+let Filter = null;
exports.setUp = function(callback)
{
let sandboxedRequire = createSandbox();
(
{Subscription, SpecialSubscription,
DownloadableSubscription, RegularSubscription,
- ExternalSubscription} = sandboxedRequire("../lib/subscriptionClasses")
+ ExternalSubscription} = sandboxedRequire("../lib/subscriptionClasses"),
+ {Filter} = sandboxedRequire("../lib/filterClasses")
);
+ f$ = Filter.fromText;
+
callback();
};
function compareSubscription(test, url, expected, postInit)
{
expected.push("[Subscription]");
let subscription = Subscription.fromURL(url);
if (postInit)
@@ -51,16 +57,30 @@
{
if (/(.*?)=(.*)/.test(line))
map[RegExp.$1] = RegExp.$2;
}
let subscription2 = Subscription.fromObject(map);
test.equal(subscription.toString(), subscription2.toString(), url + " deserialization");
}
+function compareSubscriptionFilters(test, subscription, expected)
+{
+ test.deepEqual([...subscription.filterText()], expected);
+ test.deepEqual([...subscription.filters()], expected.map(f$));
+
+ test.equal(subscription.filterCount, expected.length);
+
+ for (let i = 0; i < subscription.filterCount; i++)
+ test.equal(subscription.filterAt(i).text, expected[i]);
+
+ test.ok(!subscription.filterAt(subscription.filterCount));
+ test.ok(!subscription.filterAt(-1));
+}
+
exports.testSubscriptionClassDefinitions = function(test)
{
test.equal(typeof Subscription, "function", "typeof Subscription");
test.equal(typeof SpecialSubscription, "function", "typeof SpecialSubscription");
test.equal(typeof RegularSubscription, "function", "typeof RegularSubscription");
test.equal(typeof ExternalSubscription, "function", "typeof ExternalSubscription");
test.equal(typeof DownloadableSubscription, "function", "typeof DownloadableSubscription");
@@ -108,8 +128,91 @@
{
subscription.title = "Test group";
subscription.disabled = true;
}
);
test.done();
};
+
+exports.testFilterManagement = function(test)
+{
+ let subscription = Subscription.fromURL("https://example.com/");
+
+ compareSubscriptionFilters(test, subscription, []);
+
+ subscription.addFilter(f$("##.foo"));
+ compareSubscriptionFilters(test, subscription, ["##.foo"]);
+ test.equal(subscription.searchFilter(f$("##.foo")), 0);
+
+ subscription.addFilter(f$("##.bar"));
+ compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]);
+ test.equal(subscription.searchFilter(f$("##.bar")), 1);
+
+ // Repeat filter.
+ subscription.addFilter(f$("##.bar"));
+ compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar",
+ "##.bar"]);
+
+ // The first occurrence is found.
+ test.equal(subscription.searchFilter(f$("##.bar")), 1);
+
+ subscription.deleteFilterAt(0);
+ compareSubscriptionFilters(test, subscription, ["##.bar", "##.bar"]);
+ test.equal(subscription.searchFilter(f$("##.bar")), 0);
+
+ subscription.insertFilterAt(f$("##.foo"), 0);
+ compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar",
+ "##.bar"]);
+ test.equal(subscription.searchFilter(f$("##.bar")), 1);
+
+ subscription.deleteFilterAt(1);
+ compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]);
+ test.equal(subscription.searchFilter(f$("##.bar")), 1);
+
+ subscription.deleteFilterAt(1);
+ compareSubscriptionFilters(test, subscription, ["##.foo"]);
+ test.equal(subscription.searchFilter(f$("##.bar")), -1);
+
+ subscription.addFilter(f$("##.bar"));
+ compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]);
+ test.equal(subscription.searchFilter(f$("##.bar")), 1);
+
+ subscription.clearFilters();
+ compareSubscriptionFilters(test, subscription, []);
+ test.equal(subscription.searchFilter(f$("##.foo")), -1);
+ test.equal(subscription.searchFilter(f$("##.bar")), -1);
+
+ subscription.addFilter(f$("##.bar"));
+ compareSubscriptionFilters(test, subscription, ["##.bar"]);
+
+ subscription.addFilter(f$("##.foo"));
+ compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo"]);
+ test.equal(subscription.searchFilter(f$("##.bar")), 0);
+ test.equal(subscription.searchFilter(f$("##.foo")), 1);
+
+ // Insert outside of bounds.
+ subscription.insertFilterAt(f$("##.lambda"), 1000);
+ compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo",
+ "##.lambda"]);
+ test.equal(subscription.searchFilter(f$("##.lambda")), 2);
+
+ // Delete outside of bounds.
+ subscription.deleteFilterAt(1000);
+ compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo",
+ "##.lambda"]);
+ test.equal(subscription.searchFilter(f$("##.lambda")), 2);
+
+ // Insert outside of bounds (negative).
+ subscription.insertFilterAt(f$("##.lambda"), -1000);
+ compareSubscriptionFilters(test, subscription, ["##.lambda", "##.bar",
+ "##.foo", "##.lambda"]);
+ test.equal(subscription.searchFilter(f$("##.lambda")), 0);
+
+ // Delete outside of bounds (negative).
+ subscription.deleteFilterAt(-1000);
+ compareSubscriptionFilters(test, subscription, ["##.lambda", "##.bar",
+ "##.foo", "##.lambda"]);
+ test.equal(subscription.searchFilter(f$("##.lambda")), 0);
+
+ test.done();
+};

Powered by Google App Engine
This is Rietveld