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

Delta Between Two Patch Sets: test/snippets.js

Issue 29761612: Issue 6538, 6781 - Implement script compilation for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Test script execution as well Created April 25, 2018, 9:07 p.m.
Right Patch Set: Rebase Created May 23, 2018, 4:18 a.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
« lib/snippets.js ('K') | « lib/snippets.js ('k') | no next file » | 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-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 /* eslint no-new-func: "off" */ 18 /* eslint no-new-func: "off" */
Manish Jethani 2018/04/26 13:46:00 ESLint won't let me use new Function(...) even tho
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {createSandbox} = require("./_common"); 22 const {createSandbox} = require("./_common");
23 23
24 let SnippetFilter = null;
25 let Snippets = null; 24 let Snippets = null;
26 let ElemHide = null; 25 let parseScript = null;
26 let compileScript = null;
27 let Filter = null; 27 let Filter = null;
28 28
29 exports.setUp = function(callback) 29 exports.setUp = function(callback)
30 { 30 {
31 let sandboxedRequire = createSandbox(); 31 let sandboxedRequire = createSandbox();
32 ( 32 (
33 {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"), 33 {Filter} = sandboxedRequire("../lib/filterClasses"),
34 {ElemHide} = sandboxedRequire("../lib/elemHide"), 34 {Snippets, parseScript, compileScript} = sandboxedRequire("../lib/snippets")
35 {Snippets} = sandboxedRequire("../lib/snippets")
36 ); 35 );
37 36
38 callback(); 37 callback();
39 }; 38 };
40 39
41 exports.testDomainRestrictions = function(test) 40 exports.testDomainRestrictions = function(test)
42 { 41 {
43 function testSelectorMatches(description, filters, domain, expectedMatches) 42 function testScriptMatches(description, filters, domain, expectedMatches)
44 { 43 {
45 for (let filter of filters) 44 for (let filter of filters)
46 { 45 Snippets.add(Filter.fromText(filter));
47 filter = Filter.fromText(filter); 46
48 if (filter instanceof SnippetFilter) 47 let matches = Snippets.getScriptsForDomain(domain);
49 Snippets.add(filter);
50 else
51 ElemHide.add(filter);
52 }
53
54 let matches = Snippets.getScriptsForDomain(domain)
55 .map(filter => filter.text);
56 test.deepEqual(matches.sort(), expectedMatches.sort(), description); 48 test.deepEqual(matches.sort(), expectedMatches.sort(), description);
57 49
58 Snippets.clear(); 50 Snippets.clear();
59 ElemHide.clear(); 51 }
60 } 52
61 53 testScriptMatches(
62 testSelectorMatches(
63 "Ignore selectors with exceptions",
64 [
65 "example.com#$#foo",
66 "example.com#$#bar",
67 "example.com#@#foo"
68 ],
69 "example.com",
70 ["example.com#$#bar"]
71 );
72 testSelectorMatches(
73 "Ignore filters that include parent domain but exclude subdomain", 54 "Ignore filters that include parent domain but exclude subdomain",
74 [ 55 [
75 "~www.example.com,example.com#$#foo" 56 "~www.example.com,example.com#$#foo"
76 ], 57 ],
77 "www.example.com", 58 "www.example.com",
78 [] 59 []
79 ); 60 );
80 testSelectorMatches( 61 testScriptMatches(
81 "Ignore filters with parent domain if exception matches subdomain",
82 [
83 "www.example.com#@#foo",
84 "example.com#$#foo"
85 ],
86 "www.example.com",
87 []
88 );
89 testSelectorMatches(
90 "Ignore filters for other subdomain", 62 "Ignore filters for other subdomain",
91 [ 63 [
92 "www.example.com#$#foo", 64 "www.example.com#$#foo",
93 "other.example.com#$#foo" 65 "other.example.com#$#foo"
94 ], 66 ],
95 "other.example.com", 67 "other.example.com",
96 ["other.example.com#$#foo"] 68 ["foo"]
97 ); 69 );
98 70
99 test.done(); 71 test.done();
100 }; 72 };
101 73
102 exports.testSnippetFiltersContainer = function(test) 74 exports.testSnippetFiltersContainer = function(test)
103 { 75 {
104 function compareRules(description, domain, expectedMatches) 76 function compareRules(description, domain, expectedMatches)
105 { 77 {
106 let result = Snippets.getScriptsForDomain(domain) 78 let result = Snippets.getScriptsForDomain(domain);
107 .map(filter => filter.text); 79 expectedMatches = expectedMatches.map(filter => filter.script);
108 expectedMatches = expectedMatches.map(filter => filter.text);
109 test.deepEqual(result.sort(), expectedMatches.sort(), description); 80 test.deepEqual(result.sort(), expectedMatches.sort(), description);
110 } 81 }
111 82
112 let domainFilter = Filter.fromText("example.com#$#filter1"); 83 let domainFilter = Filter.fromText("example.com#$#filter1");
113 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); 84 let subdomainFilter = Filter.fromText("www.example.com#$#filter2");
114 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); 85 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3");
115 86
116 Snippets.add(domainFilter); 87 Snippets.add(domainFilter);
117 Snippets.add(subdomainFilter); 88 Snippets.add(subdomainFilter);
118 Snippets.add(otherDomainFilter); 89 Snippets.add(otherDomainFilter);
(...skipping 17 matching lines...) Expand all
136 [] 107 []
137 ); 108 );
138 109
139 test.done(); 110 test.done();
140 }; 111 };
141 112
142 exports.testScriptParsing = function(test) 113 exports.testScriptParsing = function(test)
143 { 114 {
144 function checkParsedScript(description, script, expectedTree) 115 function checkParsedScript(description, script, expectedTree)
145 { 116 {
146 let tree = Snippets.parseScript(script); 117 let tree = parseScript(script);
147 test.deepEqual(tree, expectedTree, description); 118 test.deepEqual(tree, expectedTree, description);
148 } 119 }
149 120
150 checkParsedScript("Script with no arguments", "foo", [["foo"]]); 121 checkParsedScript("Script with no arguments", "foo", [["foo"]]);
151 checkParsedScript("Script with one argument", "foo 1", [["foo", "1"]]); 122 checkParsedScript("Script with one argument", "foo 1", [["foo", "1"]]);
152 checkParsedScript("Script with two arguments", "foo 1 Hello", 123 checkParsedScript("Script with two arguments", "foo 1 Hello",
153 [["foo", "1", "Hello"]]); 124 [["foo", "1", "Hello"]]);
154 checkParsedScript("Script with argument containing an escaped space", 125 checkParsedScript("Script with argument containing an escaped space",
155 "foo Hello\\ world", 126 "foo Hello\\ world",
156 [["foo", "Hello world"]]); 127 [["foo", "Hello world"]]);
157 checkParsedScript("Script with argument containing a quoted space", 128 checkParsedScript("Script with argument containing a quoted space",
158 "foo 'Hello world'", 129 "foo 'Hello world'",
159 [["foo", "Hello world"]]); 130 [["foo", "Hello world"]]);
160 checkParsedScript("Script with argument containing a quoted escaped quote", 131 checkParsedScript("Script with argument containing a quoted escaped quote",
161 "foo 'Hello \\'world\\''", 132 "foo 'Hello \\'world\\''",
162 [["foo", "Hello 'world'"]]); 133 [["foo", "Hello 'world'"]]);
163 checkParsedScript("Script with argument containing an escaped semicolon", 134 checkParsedScript("Script with argument containing an escaped semicolon",
164 "foo TL\\;DR", 135 "foo TL\\;DR",
165 [["foo", "TL;DR"]]); 136 [["foo", "TL;DR"]]);
166 checkParsedScript("Script with argument containing a quoted semicolon", 137 checkParsedScript("Script with argument containing a quoted semicolon",
167 "foo 'TL;DR'", 138 "foo 'TL;DR'",
168 [["foo", "TL;DR"]]); 139 [["foo", "TL;DR"]]);
140 checkParsedScript("Script with argument containing single character " +
141 "escape sequences",
142 "foo yin\\tyang\\n",
143 [["foo", "yin\tyang\n"]]);
144 checkParsedScript("Script with argument containing Unicode escape sequences",
145 "foo \\u0062\\ud83d\\ude42r " +
146 "'l\\ud83d\\ude02mbd\\ud83d\\ude02'", [
147 ["foo", "b\ud83d\ude42r", "l\ud83d\ude02mbd\ud83d\ude02"]
148 ]);
169 checkParsedScript("Script with multiple commands", "foo; bar", 149 checkParsedScript("Script with multiple commands", "foo; bar",
170 [["foo"], ["bar"]]); 150 [["foo"], ["bar"]]);
171 checkParsedScript("Script with multiple commands and multiple arguments each", 151 checkParsedScript("Script with multiple commands and multiple arguments each",
172 "foo 1 Hello; bar world! #", 152 "foo 1 Hello; bar world! #",
173 [["foo", "1", "Hello"], ["bar", "world!", "#"]]); 153 [["foo", "1", "Hello"], ["bar", "world!", "#"]]);
174 checkParsedScript( 154 checkParsedScript("Script with multiple commands and multiple " +
175 "Script with multiple commands and multiple " + 155 "escaped and quoted arguments each",
176 "escaped and quoted arguments each", 156 "foo 1 'Hello, \\'Tommy\\'!' ;" +
177 "foo 1 'Hello, \\'Tommy\\'!' ;" + 157 "bar Hi!\\ How\\ are\\ you? http://example.com", [
178 "bar Hi!\\ How\\ are\\ you? http://example.com", 158 ["foo", "1", "Hello, 'Tommy'!"],
179 [["foo", "1", "Hello, 'Tommy'!"], 159 ["bar", "Hi! How are you?", "http://example.com"]
180 ["bar", "Hi! How are you?", "http://example.com"]] 160 ]);
181 );
182 checkParsedScript("Script with command names containing " + 161 checkParsedScript("Script with command names containing " +
183 "whitespace (spaces, tabs, newlines, etc.), " + 162 "whitespace (spaces, tabs, newlines, etc.), " +
184 "quotes, and semicolons", 163 "quotes, and semicolons",
185 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", 164 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2",
186 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]); 165 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]);
187 checkParsedScript("Script containing Unicode composite characters", 166 checkParsedScript("Script containing Unicode composite characters",
188 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", 167 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r",
189 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); 168 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]);
190 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", 169 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1",
191 [["foo"], ["bar", "1"]]); 170 [["foo"], ["bar", "1"]]);
192 171
193 test.done(); 172 test.done();
194 }; 173 };
195 174
196 exports.testScriptCompilation = function(test) 175 exports.testScriptCompilation = function(test)
197 { 176 {
198 let {compileScript, parseScript} = Snippets;
199
200 let libraries = [ 177 let libraries = [
201 ` 178 `
202 let foo = 0; 179 let foo = 0;
203 180
204 exports.setFoo = function(value) 181 exports.setFoo = function(value)
205 { 182 {
206 foo = value; 183 foo = value;
207 }; 184 };
208 185
209 exports.assertFoo = function(expected) 186 exports.assertFoo = function(expected)
210 { 187 {
211 if (foo != expected) 188 if (foo != expected)
212 throw new Error("Value mismatch"); 189 throw new Error("Value mismatch");
213 }; 190 };
214 ` 191 `
215 ]; 192 ];
216 193
217 let template = ` 194 let template = `
218 "use strict"; 195 "use strict";
196 {
197 const libraries = ${JSON.stringify(libraries)};
198
199 const script = {{{script}}};
200
201 let imports = Object.create(null);
202 for (let library of libraries)
203 new Function("exports", library)(imports);
204
205 for (let [name, ...args] of script)
219 { 206 {
220 const libraries = ${JSON.stringify(libraries)}; 207 if (Object.prototype.hasOwnProperty.call(imports, name))
221
222 const script = {{{script}}};
223
224 let imports = Object.create(null);
225 for (let library of libraries)
226 new Function("exports", library)(imports);
227
228 for (let [name, ...args] of script)
229 { 208 {
230 if (Object.prototype.hasOwnProperty.call(imports, name)) 209 let value = imports[name];
231 { 210 if (typeof value == "function")
232 let value = imports[name]; 211 value(...args);
233 if (typeof value == "function")
234 value(...args);
235 }
236 } 212 }
237 } 213 }
214 }
238 `; 215 `;
239 216
240 function verifyExecutable(script) 217 function verifyExecutable(script)
Manish Jethani 2018/04/26 13:46:00 I don't know how valuable this is because "templat
241 { 218 {
242 let actual = compileScript(script, libraries); 219 let actual = compileScript(script, libraries);
243 let expected = template.replace("{{{script}}}", 220 let expected = template.replace("{{{script}}}",
244 JSON.stringify(parseScript(script))); 221 JSON.stringify(parseScript(script)));
245 222
246 // Trim surrounding spaces because of possible difference in indentation. 223 test.equal(expected, actual);
Sebastian Noack 2018/07/16 16:37:35 Instead of copying the boilerplate over to the tes
Manish Jethani 2018/07/17 19:57:15 Let me try something. Yeah I agree that the curre
Manish Jethani 2018/07/17 20:10:26 Wait, this is what the next test does! It tests th
247 test.equal(expected.trim(), actual.trim());
248 } 224 }
249 225
250 verifyExecutable("hello 'How are you?'"); 226 verifyExecutable("hello 'How are you?'");
251 227
252 // Test script execution. 228 // Test script execution.
253 new Function(compileScript("setFoo 123; assertFoo 123", libraries))(); 229 new Function(compileScript("setFoo 123; assertFoo 123", libraries))();
254 230
255 // Override setFoo in a second library, without overriding assertFoo. A 231 // Override setFoo in a second library, without overriding assertFoo. A
256 // couple of things to note here: (1) each library has its own variables; 232 // couple of things to note here: (1) each library has its own variables;
257 // (2) script execution is stateless, i.e. the values are not retained 233 // (2) script execution is stateless, i.e. the values are not retained
258 // between executions. In the example below, assertFoo does not find 456 but 234 // between executions. In the example below, assertFoo does not find 456 but
259 // it doesn't find 123 either. It's the initial value 0. 235 // it doesn't find 123 either. It's the initial value 0.
260 new Function( 236 new Function(
261 compileScript("setFoo 456; assertFoo 0", [ 237 compileScript("setFoo 456; assertFoo 0", [
262 ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };" 238 ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };"
263 ]) 239 ])
264 )(); 240 )();
265 241
266 test.done(); 242 test.done();
267 }; 243 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld