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

Side by Side Diff: test/subscriptionClasses.js

Issue 29934588: Issue 7094 - Encapsulate management of subscription filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add tests Created Nov. 17, 2018, 11:36 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 | « test/filterStorage_readwrite.js ('k') | test/synchronizer.js » ('j') | 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-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"),
38 {Filter} = sandboxedRequire("../lib/filterClasses")
35 ); 39 );
36 40
41 f$ = Filter.fromText;
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");
57 } 63 }
58 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 {
74 test.equal(subscription.filterAt(i).text, expected[i]);
75 test.ok(subscription.hasFilter(f$(expected[i])));
76 }
77 }
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
67 test.done(); 87 test.done();
68 }; 88 };
(...skipping 37 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", "bar"]);
154
155 // The first occurrence is found.
156 test.equal(subscription.searchFilter(f$("bar")), 1);
157
158 subscription.deleteFilterAt(0);
159 compareSubscriptionFilters(test, subscription, ["bar", "bar"]);
160 test.equal(subscription.searchFilter(f$("bar")), 0);
161 test.ok(!subscription.hasFilter(f$("foo")));
162
163 subscription.insertFilterAt(f$("foo"), 0);
164 compareSubscriptionFilters(test, subscription, ["foo", "bar", "bar"]);
165 test.equal(subscription.searchFilter(f$("bar")), 1);
166
167 subscription.deleteFilterAt(1);
168 compareSubscriptionFilters(test, subscription, ["foo", "bar"]);
169 test.equal(subscription.searchFilter(f$("bar")), 1);
170
171 subscription.deleteFilterAt(1);
172 compareSubscriptionFilters(test, subscription, ["foo"]);
173 test.equal(subscription.searchFilter(f$("bar")), -1);
174 test.ok(!subscription.hasFilter(f$("bar")));
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.ok(!subscription.hasFilter(f$("foo")));
183 test.ok(!subscription.hasFilter(f$("bar")));
184 test.equal(subscription.searchFilter(f$("foo")), -1);
185 test.equal(subscription.searchFilter(f$("bar")), -1);
186
187 subscription.addFilter(f$("bar"));
188 compareSubscriptionFilters(test, subscription, ["bar"]);
189
190 subscription.addFilter(f$("foo"));
191 compareSubscriptionFilters(test, subscription, ["bar", "foo"]);
192 test.equal(subscription.searchFilter(f$("bar")), 0);
193 test.equal(subscription.searchFilter(f$("foo")), 1);
194
195 // Insert outside of bounds.
196 subscription.insertFilterAt(f$("lambda"), 1000);
197 compareSubscriptionFilters(test, subscription, ["bar", "foo", "lambda"]);
198 test.equal(subscription.searchFilter(f$("lambda")), 2);
199
200 // Delete outside of bounds.
201 subscription.deleteFilterAt(1000);
202 compareSubscriptionFilters(test, subscription, ["bar", "foo", "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", "foo",
208 "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", "foo",
214 "lambda"]);
215 test.equal(subscription.searchFilter(f$("lambda")), 0);
216
217 test.done();
218 };
OLDNEW
« no previous file with comments | « test/filterStorage_readwrite.js ('k') | test/synchronizer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld