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

Side by Side Diff: test/elemHide.js

Issue 29342884: Issue 4055 - Test ElemHide.getSelectorsFordomain (Closed)
Patch Set: Properly test and document duplicate selector behaviour Created May 23, 2016, 3:48 p.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 | « package.json ('k') | test/stub-modules/io.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 "use strict";
19
20 GLOBAL.Ci = {};
21 GLOBAL.Cu = {
22 import: function()
23 {
24 }
25 };
26
27 let {ElemHide} = require("elemHide");
28 let {Filter} = require("filterClasses");
29
30 exports.testGetSelectorsForDomain = function(test)
31 {
32 function fromText(f)
33 {
34 return (filterText) => f(Filter.fromText(filterText));
35 }
36 let addFilter = fromText(ElemHide.add);
37 let removeFilter = fromText(ElemHide.remove);
38
39 function normalizeSelectors(selectors)
40 {
41 let lastSelector;
42 return selectors.sort().filter(selector =>
43 {
44 let unique = lastSelector != selector;
45 lastSelector = selector;
46 return unique;
47 });
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 }
49 function selectorsEqual(domain, expectedSelectors, specificOnly)
50 {
51 test.deepEqual(
52 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, specificOnly)),
53 normalizeSelectors(expectedSelectors)
54 );
55 }
56
57 selectorsEqual("", []);
58
59 addFilter("~foo.example.com,example.com##foo");
60 selectorsEqual("barfoo.example.com", ["foo"]);
61 selectorsEqual("bar.foo.example.com", []);
62 selectorsEqual("foo.example.com", []);
63 selectorsEqual("example.com", ["foo"]);
64 selectorsEqual("com", []);
65 selectorsEqual("", []);
66
67 addFilter("foo.example.com##turnip");
68 selectorsEqual("foo.example.com", ["turnip"]);
69 selectorsEqual("example.com", ["foo"]);
70 selectorsEqual("com", []);
71 selectorsEqual("", []);
72
73 addFilter("example.com#@#foo");
74 selectorsEqual("foo.example.com", ["turnip"]);
75 selectorsEqual("example.com", []);
76 selectorsEqual("com", []);
77 selectorsEqual("", []);
78
79 addFilter("com##bar");
80 selectorsEqual("foo.example.com", ["turnip", "bar"]);
81 selectorsEqual("example.com", ["bar"]);
82 selectorsEqual("com", ["bar"]);
83 selectorsEqual("", []);
84
85 addFilter("example.com#@#bar");
86 selectorsEqual("foo.example.com", ["turnip"]);
87 selectorsEqual("example.com", []);
88 selectorsEqual("com", ["bar"]);
89 selectorsEqual("", []);
90
91 removeFilter("example.com#@#foo");
92 selectorsEqual("foo.example.com", ["turnip"]);
93 selectorsEqual("example.com", ["foo"]);
94 selectorsEqual("com", ["bar"]);
95 selectorsEqual("", []);
96
97 removeFilter("example.com#@#bar");
98 selectorsEqual("foo.example.com", ["turnip", "bar"]);
99 selectorsEqual("example.com", ["foo", "bar"]);
100 selectorsEqual("com", ["bar"]);
101 selectorsEqual("", []);
102
103 addFilter("##generic");
104 selectorsEqual("foo.example.com", ["turnip", "bar", "generic"]);
105 selectorsEqual("example.com", ["foo", "bar", "generic"]);
106 selectorsEqual("com", ["bar", "generic"]);
107 selectorsEqual("", ["generic"]);
108 selectorsEqual("foo.example.com", ["turnip", "bar"], true);
109 selectorsEqual("example.com", ["foo", "bar"], true);
110 selectorsEqual("com", ["bar"], true);
111 selectorsEqual("", [], true);
112 removeFilter("##generic");
113
114 addFilter("~adblockplus.org##example");
115 selectorsEqual("adblockplus.org", []);
116 selectorsEqual("", ["example"]);
117 selectorsEqual("foo.example.com", ["turnip", "bar", "example"]);
118 selectorsEqual("foo.example.com", ["turnip", "bar"], true);
119 removeFilter("~adblockplus.org##example");
120
121 removeFilter("~foo.example.com,example.com##foo");
122 selectorsEqual("foo.example.com", ["turnip", "bar"]);
123 selectorsEqual("example.com", ["bar"]);
124 selectorsEqual("com", ["bar"]);
125 selectorsEqual("", []);
126
127 removeFilter("com##bar");
128 selectorsEqual("foo.example.com", ["turnip"]);
129 selectorsEqual("example.com", []);
130 selectorsEqual("com", []);
131 selectorsEqual("", []);
132
133 removeFilter("foo.example.com##turnip");
134 selectorsEqual("foo.example.com", []);
135 selectorsEqual("example.com", []);
136 selectorsEqual("com", []);
137 selectorsEqual("", []);
138
139 addFilter("example.com##dupe");
140 addFilter("example.com##dupe");
141 selectorsEqual("example.com", ["dupe"]);
142 removeFilter("example.com##dupe");
143 selectorsEqual("example.com", []);
144 removeFilter("example.com##dupe");
145
146 addFilter("~foo.example.com,example.com##foo");
147
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");
153 selectorsEqual("foo.example.com", ["foo"]);
154 selectorsEqual("example.com", ["foo"]);
155 selectorsEqual("com", ["foo"]);
156 selectorsEqual("", ["foo"]);
157 removeFilter("##foo");
158
159 addFilter("example.org##foo");
160 selectorsEqual("foo.example.com", []);
161 selectorsEqual("example.com", ["foo"]);
162 selectorsEqual("com", []);
163 selectorsEqual("", []);
164 removeFilter("example.org##foo");
165
166 addFilter("~example.com##foo");
167 selectorsEqual("foo.example.com", []);
168 selectorsEqual("example.com", ["foo"]);
169 selectorsEqual("com", ["foo"]);
170 selectorsEqual("", ["foo"]);
171 removeFilter("example.org##foo");
172
173 test.done();
174 };
OLDNEW
« no previous file with comments | « package.json ('k') | test/stub-modules/io.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld