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

Side by Side Diff: test/abp2blocklist.js

Issue 29426594: Issue 3673 - Merge closely matching rules (Closed) Base URL: https://hg.adblockplus.org/abp2blocklist
Patch Set: Fix bugs and add unit tests Created May 3, 2017, 12:25 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/abp2blocklist.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 let Filter = require("filterClasses").Filter; 20 let Filter = require("filterClasses").Filter;
21 let ContentBlockerList = require("../lib/abp2blocklist.js").ContentBlockerList; 21 let ContentBlockerList = require("../lib/abp2blocklist.js").ContentBlockerList;
22 22
23 function testRules(test, filters, expected, transformFunction) 23 function testRules(test, filters, expected, transformFunction,
24 {merge = false} = {})
24 { 25 {
25 let blockerList = new ContentBlockerList(); 26 let blockerList = new ContentBlockerList();
26 for (let filter of filters) 27 for (let filter of filters)
27 blockerList.addFilter(Filter.fromText(filter)); 28 blockerList.addFilter(Filter.fromText(filter));
28 29
29 let rules = blockerList.generateRules(); 30 let rules = blockerList.generateRules({merge});
30 if (transformFunction) 31 if (transformFunction)
31 rules = transformFunction(rules); 32 rules = transformFunction(rules);
32 33
33 test.deepEqual(rules, expected); 34 test.deepEqual(rules, expected);
34 } 35 }
35 36
36 exports.generateRules = { 37 exports.generateRules = {
37 testElementHiding: function(test) 38 testElementHiding: function(test)
38 { 39 {
39 testRules(test, ["##.whatever"], [ 40 testRules(test, ["##.whatever"], [
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 }, 225 },
225 226
226 testUnicode: function(test) 227 testUnicode: function(test)
227 { 228 {
228 testRules(test, ["$domain=🐈.cat"], ["xn--zn8h.cat", "www.xn--zn8h.cat"], 229 testRules(test, ["$domain=🐈.cat"], ["xn--zn8h.cat", "www.xn--zn8h.cat"],
229 rules => rules[0]["trigger"]["if-domain"]); 230 rules => rules[0]["trigger"]["if-domain"]);
230 testRules(test, ["🐈$domain=🐈.cat"], []); 231 testRules(test, ["🐈$domain=🐈.cat"], []);
231 testRules(test, ["###🐈"], []); 232 testRules(test, ["###🐈"], []);
232 233
233 test.done(); 234 test.done();
235 },
236
237 testMerging: function(test)
238 {
239 testRules(test, ["/ads", "/adv"], ["^https?://.*/ad[sv]"],
240 rules => rules.map(rule => rule.trigger["url-filter"]),
241 {merge: true});
242 testRules(test, ["/ads", "/adv", "/ad"], ["^https?://.*/ad[sv]?"],
243 rules => rules.map(rule => rule.trigger["url-filter"]),
244 {merge: true});
245 testRules(test, ["/ad", "/ads", "/adv"], ["^https?://.*/ad[sv]?"],
246 rules => rules.map(rule => rule.trigger["url-filter"]),
247 {merge: true});
248 testRules(test, ["/a", "/ad", "/ads", "/adv"],
249 ["^https?://.*/a", "^https?://.*/ad[sv]?"],
250 rules => rules.map(rule => rule.trigger["url-filter"]),
251 {merge: true});
252 testRules(test, ["/ad", "/a", "/ads", "/adv"],
253 ["^https?://.*/a", "^https?://.*/ad[sv]?"],
254 rules => rules.map(rule => rule.trigger["url-filter"]),
255 {merge: true});
256 testRules(test, ["/ads", "/adv", "/ad", "/a"],
257 ["^https?://.*/ad[sv]?", "^https?://.*/a"],
258 rules => rules.map(rule => rule.trigger["url-filter"]),
259 {merge: true});
260 testRules(test, ["/ads", "/adv", "/a", "/ad"],
261 ["^https?://.*/ad[sv]?", "^https?://.*/a"],
262 rules => rules.map(rule => rule.trigger["url-filter"]),
263 {merge: true});
264 testRules(test, ["/ad", "/a", "/ads", "/adv", "/adx"],
265 ["^https?://.*/a", "^https?://.*/ad[svx]?"],
266 rules => rules.map(rule => rule.trigger["url-filter"]),
267 {merge: true});
268 testRules(test, ["/ads", "/a", "/ad", "/adv", "/adx"],
269 ["^https?://.*/ad[svx]?", "^https?://.*/a"],
270 rules => rules.map(rule => rule.trigger["url-filter"]),
271 {merge: true});
272 testRules(test, ["/ads", "/a", "/ad", "/adv", "/adx", "/adxs"],
273 ["^https?://.*/ad[svx]?", "^https?://.*/a", "^https?://.*/adxs"],
274 rules => rules.map(rule => rule.trigger["url-filter"]),
275 {merge: true});
276 testRules(test, ["/adxs", "/a", "/ad", "/ads", "/adv", "/adx"],
277 ["^https?://.*/adxs", "^https?://.*/a", "^https?://.*/ad[svx]?"],
278 rules => rules.map(rule => rule.trigger["url-filter"]),
279 {merge: true});
280 testRules(test, ["/adxs", "/a", "/ads", "/ad", "/adv", "/adx"],
281 ["^https?://.*/adxs", "^https?://.*/a", "^https?://.*/ad[svx]?"],
282 rules => rules.map(rule => rule.trigger["url-filter"]),
283 {merge: true});
284 testRules(test, ["/adxs", "/a", "/ads", "/adv", "/ad", "/adx"],
285 ["^https?://.*/adxs", "^https?://.*/a", "^https?://.*/ad[svx]?"],
286 rules => rules.map(rule => rule.trigger["url-filter"]),
287 {merge: true});
288 testRules(test, ["/adsxi", "/adxs", "/a", "/ads", "/adv", "/ad", "/adx"],
289 ["^https?://.*/adsxi", "^https?://.*/adxs", "^https?://.*/a",
290 "^https?://.*/ad[svx]?"],
291 rules => rules.map(rule => rule.trigger["url-filter"]),
292 {merge: true});
293 testRules(test, ["/adxsi", "/adsxi", "/adxs", "/a", "/ads", "/adv", "/ad",
294 "/adx"],
295 ["^https?://.*/adxsi?", "^https?://.*/adsxi", "^https?://.*/a",
296 "^https?://.*/ad[svx]?"],
297 rules => rules.map(rule => rule.trigger["url-filter"]),
298 {merge: true});
299
300 // Given "ads", "bds", "adv", "bdv", "adx", and "bdx", we want "ad[svx]"
301 // and "bd[svx]" (2 rules), not "[ab]ds", "[ab]dv", and "[ab]dx" (3 rules).
302 testRules(test, ["/ads", "/bds", "/adv", "/bdv", "/adx", "/bdx"],
303 ["^https?://.*/ad[svx]", "^https?://.*/bd[svx]"],
304 rules => rules.map(rule => rule.trigger["url-filter"]),
305 {merge: true});
306 testRules(test, ["/ads", "/bds", "/adv", "/bdv", "/bdx"],
307 ["^https?://.*/ad[sv]", "^https?://.*/bd[svx]"],
308 rules => rules.map(rule => rule.trigger["url-filter"]),
309 {merge: true});
310
311 // Make sure it ignores special characters.
312 testRules(test, ["/ads?", "/ads"],
313 ["^https?://.*/ads\\?", "^https?://.*/ads"],
314 rules => rules.map(rule => rule.trigger["url-filter"]),
315 {merge: true});
316 testRules(test, ["/ads?", "/ads-"],
317 ["^https?://.*/ads\\?", "^https?://.*/ads-"],
318 rules => rules.map(rule => rule.trigger["url-filter"]),
319 {merge: true});
320 testRules(test, ["/ads?-", "/ads-"],
321 ["^https?://.*/ads\\?-", "^https?://.*/ads-"],
322 rules => rules.map(rule => rule.trigger["url-filter"]),
323 {merge: true});
324
325 test.done();
234 } 326 }
235 }; 327 };
OLDNEW
« no previous file with comments | « lib/abp2blocklist.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld