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,33 @@ |
{ |
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.hasFilter(f$(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 +131,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); |
+ test.ok(!subscription.hasFilter(f$("foo"))); |
+ |
+ 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); |
+ test.ok(!subscription.hasFilter(f$("bar"))); |
+ |
+ subscription.addFilter(f$("bar")); |
+ compareSubscriptionFilters(test, subscription, ["foo", "bar"]); |
+ test.equal(subscription.searchFilter(f$("bar")), 1); |
+ |
+ subscription.clearFilters(); |
+ compareSubscriptionFilters(test, subscription, []); |
+ test.ok(!subscription.hasFilter(f$("foo"))); |
+ test.ok(!subscription.hasFilter(f$("bar"))); |
+ 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(); |
+}; |