Index: test/filterText.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/test/filterText.js |
@@ -0,0 +1,106 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-present eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * 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 {createSandbox} = require("./_common"); |
+ |
+let LONG_DOMAINS_THRESHOLD = null; |
+let optimizeContentFilterText = null; |
+let optimizeRegExpFilterText = null; |
+ |
+function randomName() |
+{ |
+ let str = Math.random().toString(36).substring(2); |
+ return str.substring(0, Math.ceil(Math.random() * str.length)); |
+} |
+ |
+function randomDomain() |
+{ |
+ // Create a domain with between one and four parts to cover all the usual |
+ // cases. |
+ let domain = randomName(); |
+ for (let i = 0; i < Math.floor(Math.random() * 4); i++) |
+ domain += "." + randomName(); |
+ return domain; |
+} |
+ |
+exports.setUp = function(callback) |
+{ |
+ let sandboxedRequire = createSandbox({ |
+ extraExports: { |
+ filterText: ["LONG_DOMAINS_THRESHOLD"] |
+ } |
+ }); |
+ ( |
+ {LONG_DOMAINS_THRESHOLD, |
+ optimizeContentFilterText, |
+ optimizeRegExpFilterText} = sandboxedRequire("../lib/filterText") |
+ ); |
+ |
+ callback(); |
+}; |
+ |
+exports.testOptimizeContentFilterText = function(test) |
+{ |
+ let domains = randomDomain(); |
+ while (domains.length < LONG_DOMAINS_THRESHOLD) |
+ domains += "," + randomDomain(); |
+ |
+ let filterParts = [ |
+ [domains, null, "." + randomName()], |
+ [domains, "@", "." + randomName()], |
+ [domains, "?", `.${randomName()}:-abp-contains(${randomName()})`], |
+ [domains, "$", |
+ `${randomName()} '${randomName()} ${randomName()}' ${randomName()}`] |
+ ]; |
+ |
+ let filters = filterParts.map( |
+ ([domains_, type, selector]) => |
+ [domains_ + "#" + (type || "") + "#" + selector, |
+ domains_, type, selector] |
+ ); |
+ |
+ // We just have to make sure that the parts returned are the same as the |
+ // parts passed in to the function. |
+ for (let filter of filters) |
+ test.deepEqual(optimizeContentFilterText(...filter), filter); |
+ |
+ test.done(); |
+}; |
+ |
+exports.testOptimizeRegExpFilterText = function(test) |
+{ |
+ let domains = randomDomain(); |
+ while (domains.length < LONG_DOMAINS_THRESHOLD) |
+ domains += "|" + randomDomain(); |
+ |
+ let filterParts = [ |
+ ["", randomName(), domains], |
+ ["@@", randomName(), domains] |
+ ]; |
+ |
+ let filters = filterParts.map( |
+ ([marker, pattern, domains_]) => [marker + pattern + "$domain=" + domains, |
+ pattern, domains, null, null, null] |
+ ); |
+ |
+ for (let filter of filters) |
+ test.deepEqual(optimizeRegExpFilterText(...filter), filter); |
+ |
+ test.done(); |
+}; |