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

Delta Between Two Patch Sets: test/subscriptionClasses.js

Issue 29934588: Issue 7094 - Encapsulate management of subscription filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Reworked Created Nov. 3, 2018, 2:20 a.m.
Right Patch Set: Remove hasFilter and related code Created Nov. 18, 2018, 10:21 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
LEFTRIGHT
(no file at all)
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 const {createSandbox} = require("./_common"); 20 const {createSandbox} = require("./_common");
21 21
22 let f$ = null;
23
22 let Subscription = null; 24 let Subscription = null;
23 let SpecialSubscription = null; 25 let SpecialSubscription = null;
24 let DownloadableSubscription = null; 26 let DownloadableSubscription = null;
25 let RegularSubscription = null; 27 let RegularSubscription = null;
26 let ExternalSubscription = null; 28 let ExternalSubscription = null;
29 let Filter = null;
27 30
28 exports.setUp = function(callback) 31 exports.setUp = function(callback)
29 { 32 {
30 let sandboxedRequire = createSandbox(); 33 let sandboxedRequire = createSandbox();
31 ( 34 (
32 {Subscription, SpecialSubscription, 35 {Subscription, SpecialSubscription,
33 DownloadableSubscription, RegularSubscription, 36 DownloadableSubscription, RegularSubscription,
34 ExternalSubscription} = sandboxedRequire("../lib/subscriptionClasses") 37 ExternalSubscription} = sandboxedRequire("../lib/subscriptionClasses"),
35 ); 38 {Filter} = sandboxedRequire("../lib/filterClasses")
39 );
40
41 f$ = Filter.fromText;
36 42
37 callback(); 43 callback();
38 }; 44 };
39 45
40 function compareSubscription(test, url, expected, postInit) 46 function compareSubscription(test, url, expected, postInit)
41 { 47 {
42 expected.push("[Subscription]"); 48 expected.push("[Subscription]");
43 let subscription = Subscription.fromURL(url); 49 let subscription = Subscription.fromURL(url);
44 if (postInit) 50 if (postInit)
45 postInit(subscription); 51 postInit(subscription);
46 let result = [...subscription.serialize()]; 52 let result = [...subscription.serialize()];
47 test.equal(result.sort().join("\n"), expected.sort().join("\n"), url); 53 test.equal(result.sort().join("\n"), expected.sort().join("\n"), url);
48 54
49 let map = Object.create(null); 55 let map = Object.create(null);
50 for (let line of result.slice(1)) 56 for (let line of result.slice(1))
51 { 57 {
52 if (/(.*?)=(.*)/.test(line)) 58 if (/(.*?)=(.*)/.test(line))
53 map[RegExp.$1] = RegExp.$2; 59 map[RegExp.$1] = RegExp.$2;
54 } 60 }
55 let subscription2 = Subscription.fromObject(map); 61 let subscription2 = Subscription.fromObject(map);
56 test.equal(subscription.toString(), subscription2.toString(), url + " deserial ization"); 62 test.equal(subscription.toString(), subscription2.toString(), url + " deserial ization");
63 }
64
65 function compareSubscriptionFilters(test, subscription, expected)
66 {
67 test.deepEqual([...subscription.filterText()], expected);
68 test.deepEqual([...subscription.filters()], expected.map(f$));
69
70 test.equal(subscription.filterCount, expected.length);
71
72 for (let i = 0; i < subscription.filterCount; i++)
73 test.equal(subscription.filterAt(i).text, expected[i]);
74
75 test.ok(!subscription.filterAt(subscription.filterCount));
76 test.ok(!subscription.filterAt(-1));
57 } 77 }
58 78
59 exports.testSubscriptionClassDefinitions = function(test) 79 exports.testSubscriptionClassDefinitions = function(test)
60 { 80 {
61 test.equal(typeof Subscription, "function", "typeof Subscription"); 81 test.equal(typeof Subscription, "function", "typeof Subscription");
62 test.equal(typeof SpecialSubscription, "function", "typeof SpecialSubscription "); 82 test.equal(typeof SpecialSubscription, "function", "typeof SpecialSubscription ");
63 test.equal(typeof RegularSubscription, "function", "typeof RegularSubscription "); 83 test.equal(typeof RegularSubscription, "function", "typeof RegularSubscription ");
64 test.equal(typeof ExternalSubscription, "function", "typeof ExternalSubscripti on"); 84 test.equal(typeof ExternalSubscription, "function", "typeof ExternalSubscripti on");
65 test.equal(typeof DownloadableSubscription, "function", "typeof DownloadableSu bscription"); 85 test.equal(typeof DownloadableSubscription, "function", "typeof DownloadableSu bscription");
66 86
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 test, "~wl~", ["url=~wl~", "disabled=true", "title=Test group"], 126 test, "~wl~", ["url=~wl~", "disabled=true", "title=Test group"],
107 subscription => 127 subscription =>
108 { 128 {
109 subscription.title = "Test group"; 129 subscription.title = "Test group";
110 subscription.disabled = true; 130 subscription.disabled = true;
111 } 131 }
112 ); 132 );
113 133
114 test.done(); 134 test.done();
115 }; 135 };
136
137 exports.testFilterManagement = function(test)
138 {
139 let subscription = Subscription.fromURL("https://example.com/");
140
141 compareSubscriptionFilters(test, subscription, []);
142
143 subscription.addFilter(f$("##.foo"));
144 compareSubscriptionFilters(test, subscription, ["##.foo"]);
145 test.equal(subscription.searchFilter(f$("##.foo")), 0);
146
147 subscription.addFilter(f$("##.bar"));
148 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]);
149 test.equal(subscription.searchFilter(f$("##.bar")), 1);
150
151 // Repeat filter.
152 subscription.addFilter(f$("##.bar"));
153 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar",
154 "##.bar"]);
155
156 // The first occurrence is found.
157 test.equal(subscription.searchFilter(f$("##.bar")), 1);
158
159 subscription.deleteFilterAt(0);
160 compareSubscriptionFilters(test, subscription, ["##.bar", "##.bar"]);
161 test.equal(subscription.searchFilter(f$("##.bar")), 0);
162
163 subscription.insertFilterAt(f$("##.foo"), 0);
164 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar",
165 "##.bar"]);
166 test.equal(subscription.searchFilter(f$("##.bar")), 1);
167
168 subscription.deleteFilterAt(1);
169 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]);
170 test.equal(subscription.searchFilter(f$("##.bar")), 1);
171
172 subscription.deleteFilterAt(1);
173 compareSubscriptionFilters(test, subscription, ["##.foo"]);
174 test.equal(subscription.searchFilter(f$("##.bar")), -1);
175
176 subscription.addFilter(f$("##.bar"));
177 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]);
178 test.equal(subscription.searchFilter(f$("##.bar")), 1);
179
180 subscription.clearFilters();
181 compareSubscriptionFilters(test, subscription, []);
182 test.equal(subscription.searchFilter(f$("##.foo")), -1);
183 test.equal(subscription.searchFilter(f$("##.bar")), -1);
184
185 subscription.addFilter(f$("##.bar"));
186 compareSubscriptionFilters(test, subscription, ["##.bar"]);
187
188 subscription.addFilter(f$("##.foo"));
189 compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo"]);
190 test.equal(subscription.searchFilter(f$("##.bar")), 0);
191 test.equal(subscription.searchFilter(f$("##.foo")), 1);
192
193 // Insert outside of bounds.
194 subscription.insertFilterAt(f$("##.lambda"), 1000);
195 compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo",
196 "##.lambda"]);
197 test.equal(subscription.searchFilter(f$("##.lambda")), 2);
198
199 // Delete outside of bounds.
200 subscription.deleteFilterAt(1000);
201 compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo",
202 "##.lambda"]);
203 test.equal(subscription.searchFilter(f$("##.lambda")), 2);
204
205 // Insert outside of bounds (negative).
206 subscription.insertFilterAt(f$("##.lambda"), -1000);
207 compareSubscriptionFilters(test, subscription, ["##.lambda", "##.bar",
208 "##.foo", "##.lambda"]);
209 test.equal(subscription.searchFilter(f$("##.lambda")), 0);
210
211 // Delete outside of bounds (negative).
212 subscription.deleteFilterAt(-1000);
213 compareSubscriptionFilters(test, subscription, ["##.lambda", "##.bar",
214 "##.foo", "##.lambda"]);
215 test.equal(subscription.searchFilter(f$("##.lambda")), 0);
216
217 test.done();
218 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld