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

Delta Between Two Patch Sets: test/elemHide.js

Issue 29342884: Issue 4055 - Test ElemHide.getSelectorsFordomain (Closed)
Left Patch Set: Add test and note about known limitation. Created May 22, 2016, 8:30 p.m.
Right Patch Set: Simplify filter function and move comment Created May 23, 2016, 6:55 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « package.json ('k') | test/stub-modules/io.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 GLOBAL.Ci = { }; 20 GLOBAL.Ci = {};
21 GLOBAL.Cu = { 21 GLOBAL.Cu = {
22 import: function() { } 22 import: function()
23 {
24 }
23 }; 25 };
24 26
25 let {ElemHide} = require("elemHide"); 27 let {ElemHide} = require("elemHide");
26 let {Filter} = require("filterClasses"); 28 let {Filter} = require("filterClasses");
27 29
28 exports.testGetSelectorsForDomain = function(test) 30 exports.testGetSelectorsForDomain = function(test)
29 { 31 {
30 function fromText(f) 32 function fromText(f)
31 { 33 {
32 return (filterText) => ElemHide[f](Filter.fromText(filterText)); 34 return (filterText) => f(Filter.fromText(filterText));
33 } 35 }
34 let addFilter = fromText("add"); 36 let addFilter = fromText(ElemHide.add);
35 let removeFilter = fromText("remove"); 37 let removeFilter = fromText(ElemHide.remove);
36 38
37 function selectorsEqual(domain, selectors, specificOnly) 39 function normalizeSelectors(selectors)
38 { 40 {
39 test.deepEqual(ElemHide.getSelectorsForDomain(domain, specificOnly), 41 // getSelectorsForDomain is currently allowed to return duplicate selectors
40 selectors); 42 // for performance reasons, so we need to remove duplicates here.
43 return selectors.sort().filter((selector, index, selectors) =>
44 {
45 return index == 0 || selector != selectors[index - 1];
46 });
47 }
48 function selectorsEqual(domain, expectedSelectors, specificOnly)
49 {
50 test.deepEqual(
51 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, specificOnly)),
52 normalizeSelectors(expectedSelectors)
53 );
41 } 54 }
42 55
43 selectorsEqual("", []); 56 selectorsEqual("", []);
44 57
45 addFilter("~foo.example.com,example.com##foo"); 58 addFilter("~foo.example.com,example.com##foo");
59 selectorsEqual("barfoo.example.com", ["foo"]);
60 selectorsEqual("bar.foo.example.com", []);
46 selectorsEqual("foo.example.com", []); 61 selectorsEqual("foo.example.com", []);
47 selectorsEqual("example.com", ["foo"]); 62 selectorsEqual("example.com", ["foo"]);
48 selectorsEqual("com", []); 63 selectorsEqual("com", []);
49 selectorsEqual("", []); 64 selectorsEqual("", []);
50 65
51 addFilter("foo.example.com##turnip"); 66 addFilter("foo.example.com##turnip");
52 selectorsEqual("foo.example.com", ["turnip"]); 67 selectorsEqual("foo.example.com", ["turnip"]);
53 selectorsEqual("example.com", ["foo"]); 68 selectorsEqual("example.com", ["foo"]);
54 selectorsEqual("com", []); 69 selectorsEqual("com", []);
55 selectorsEqual("", []); 70 selectorsEqual("", []);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 selectorsEqual("example.com", []); 128 selectorsEqual("example.com", []);
114 selectorsEqual("com", []); 129 selectorsEqual("com", []);
115 selectorsEqual("", []); 130 selectorsEqual("", []);
116 131
117 removeFilter("foo.example.com##turnip"); 132 removeFilter("foo.example.com##turnip");
118 selectorsEqual("foo.example.com", []); 133 selectorsEqual("foo.example.com", []);
119 selectorsEqual("example.com", []); 134 selectorsEqual("example.com", []);
120 selectorsEqual("com", []); 135 selectorsEqual("com", []);
121 selectorsEqual("", []); 136 selectorsEqual("", []);
122 137
123 // As a consession for performance we don't handle the case of more than one
124 // identical rule being added, then one of them removed. Ideally the final
125 // result here would be ["dupe"] instead of [].
126 addFilter("example.com##dupe"); 138 addFilter("example.com##dupe");
127 addFilter("example.com##dupe"); 139 addFilter("example.com##dupe");
128 selectorsEqual("example.com", ["dupe"]); 140 selectorsEqual("example.com", ["dupe"]);
129 removeFilter("example.com##dupe"); 141 removeFilter("example.com##dupe");
130 selectorsEqual("example.com", []); 142 selectorsEqual("example.com", []);
143 removeFilter("example.com##dupe");
144
145 addFilter("~foo.example.com,example.com##foo");
146
147 addFilter("##foo");
148 selectorsEqual("foo.example.com", ["foo"]);
149 selectorsEqual("example.com", ["foo"]);
150 selectorsEqual("com", ["foo"]);
151 selectorsEqual("", ["foo"]);
152 removeFilter("##foo");
153
154 addFilter("example.org##foo");
155 selectorsEqual("foo.example.com", []);
156 selectorsEqual("example.com", ["foo"]);
157 selectorsEqual("com", []);
158 selectorsEqual("", []);
159 removeFilter("example.org##foo");
160
161 addFilter("~example.com##foo");
162 selectorsEqual("foo.example.com", []);
163 selectorsEqual("example.com", ["foo"]);
164 selectorsEqual("com", ["foo"]);
165 selectorsEqual("", ["foo"]);
166 removeFilter("example.org##foo");
131 167
132 test.done(); 168 test.done();
133 }; 169 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld