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: Properly test and document duplicate selector behaviour Created May 23, 2016, 3:48 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
(...skipping 20 matching lines...) Expand all
31 { 31 {
32 function fromText(f) 32 function fromText(f)
33 { 33 {
34 return (filterText) => f(Filter.fromText(filterText)); 34 return (filterText) => f(Filter.fromText(filterText));
35 } 35 }
36 let addFilter = fromText(ElemHide.add); 36 let addFilter = fromText(ElemHide.add);
37 let removeFilter = fromText(ElemHide.remove); 37 let removeFilter = fromText(ElemHide.remove);
38 38
39 function normalizeSelectors(selectors) 39 function normalizeSelectors(selectors)
40 { 40 {
41 let lastSelector; 41 // getSelectorsForDomain is currently allowed to return duplicate selectors
42 return selectors.sort().filter(selector => 42 // for performance reasons, so we need to remove duplicates here.
43 return selectors.sort().filter((selector, index, selectors) =>
43 { 44 {
44 let unique = lastSelector != selector; 45 return index == 0 || selector != selectors[index - 1];
45 lastSelector = selector;
46 return unique;
47 }); 46 });
Wladimir Palant 2016/05/23 17:34:48 How about a simpler filter function? (selector,
kzar 2016/05/23 18:56:26 Cool, didn't realise filter provided the index and
48 } 47 }
49 function selectorsEqual(domain, expectedSelectors, specificOnly) 48 function selectorsEqual(domain, expectedSelectors, specificOnly)
50 { 49 {
51 test.deepEqual( 50 test.deepEqual(
52 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, specificOnly)), 51 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, specificOnly)),
53 normalizeSelectors(expectedSelectors) 52 normalizeSelectors(expectedSelectors)
54 ); 53 );
55 } 54 }
56 55
57 selectorsEqual("", []); 56 selectorsEqual("", []);
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 137
139 addFilter("example.com##dupe"); 138 addFilter("example.com##dupe");
140 addFilter("example.com##dupe"); 139 addFilter("example.com##dupe");
141 selectorsEqual("example.com", ["dupe"]); 140 selectorsEqual("example.com", ["dupe"]);
142 removeFilter("example.com##dupe"); 141 removeFilter("example.com##dupe");
143 selectorsEqual("example.com", []); 142 selectorsEqual("example.com", []);
144 removeFilter("example.com##dupe"); 143 removeFilter("example.com##dupe");
145 144
146 addFilter("~foo.example.com,example.com##foo"); 145 addFilter("~foo.example.com,example.com##foo");
147 146
148 // getSelectorsForDomain is currently allowed to return duplicate selectors
149 // for performance reasons. In reality example.com gives ["foo", "foo"] here
150 // but we normalize the selectors, removing duplicates, before performing the
151 // comparison.
Wladimir Palant 2016/05/23 17:34:48 I think that this explanation belongs into normali
kzar 2016/05/23 18:56:26 Done.
152 addFilter("##foo"); 147 addFilter("##foo");
153 selectorsEqual("foo.example.com", ["foo"]); 148 selectorsEqual("foo.example.com", ["foo"]);
154 selectorsEqual("example.com", ["foo"]); 149 selectorsEqual("example.com", ["foo"]);
155 selectorsEqual("com", ["foo"]); 150 selectorsEqual("com", ["foo"]);
156 selectorsEqual("", ["foo"]); 151 selectorsEqual("", ["foo"]);
157 removeFilter("##foo"); 152 removeFilter("##foo");
158 153
159 addFilter("example.org##foo"); 154 addFilter("example.org##foo");
160 selectorsEqual("foo.example.com", []); 155 selectorsEqual("foo.example.com", []);
161 selectorsEqual("example.com", ["foo"]); 156 selectorsEqual("example.com", ["foo"]);
162 selectorsEqual("com", []); 157 selectorsEqual("com", []);
163 selectorsEqual("", []); 158 selectorsEqual("", []);
164 removeFilter("example.org##foo"); 159 removeFilter("example.org##foo");
165 160
166 addFilter("~example.com##foo"); 161 addFilter("~example.com##foo");
167 selectorsEqual("foo.example.com", []); 162 selectorsEqual("foo.example.com", []);
168 selectorsEqual("example.com", ["foo"]); 163 selectorsEqual("example.com", ["foo"]);
169 selectorsEqual("com", ["foo"]); 164 selectorsEqual("com", ["foo"]);
170 selectorsEqual("", ["foo"]); 165 selectorsEqual("", ["foo"]);
171 removeFilter("example.org##foo"); 166 removeFilter("example.org##foo");
172 167
173 test.done(); 168 test.done();
174 }; 169 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld