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

Side by Side Diff: test/snippets.js

Issue 29761612: Issue 6538, 6781 - Implement script compilation for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
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:
View unified diff | Download patch
« lib/snippets.js ('K') | « lib/snippets.js ('k') | no next file » | 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 /* eslint no-new-func: "off" */
19
18 "use strict"; 20 "use strict";
19 21
20 const {createSandbox} = require("./_common"); 22 const {createSandbox} = require("./_common");
21 23
22 let Snippets = null; 24 let Snippets = null;
23 let parseScript = null; 25 let parseScript = null;
26 let compileScript = null;
24 let Filter = null; 27 let Filter = null;
25 28
26 exports.setUp = function(callback) 29 exports.setUp = function(callback)
27 { 30 {
28 let sandboxedRequire = createSandbox(); 31 let sandboxedRequire = createSandbox();
29 ( 32 (
30 {Filter} = sandboxedRequire("../lib/filterClasses"), 33 {Filter} = sandboxedRequire("../lib/filterClasses"),
31 {Snippets, parseScript} = sandboxedRequire("../lib/snippets") 34 {Snippets, parseScript, compileScript} = sandboxedRequire("../lib/snippets")
32 ); 35 );
33 36
34 callback(); 37 callback();
35 }; 38 };
36 39
37 exports.testDomainRestrictions = function(test) 40 exports.testDomainRestrictions = function(test)
38 { 41 {
39 function testScriptMatches(description, filters, domain, expectedMatches) 42 function testScriptMatches(description, filters, domain, expectedMatches)
40 { 43 {
41 for (let filter of filters) 44 for (let filter of filters)
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 "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",
162 [["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"]]);
163 checkParsedScript("Script containing Unicode composite characters", 166 checkParsedScript("Script containing Unicode composite characters",
164 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", 167 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r",
165 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); 168 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]);
166 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", 169 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1",
167 [["foo"], ["bar", "1"]]); 170 [["foo"], ["bar", "1"]]);
168 171
169 test.done(); 172 test.done();
170 }; 173 };
174
175 exports.testScriptCompilation = function(test)
176 {
177 let libraries = [
178 `
179 let foo = 0;
180
181 exports.setFoo = function(value)
182 {
183 foo = value;
184 };
185
186 exports.assertFoo = function(expected)
187 {
188 if (foo != expected)
189 throw new Error("Value mismatch");
190 };
191 `
192 ];
193
194 let template = `
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)
206 {
207 if (Object.prototype.hasOwnProperty.call(imports, name))
208 {
209 let value = imports[name];
210 if (typeof value == "function")
211 value(...args);
212 }
213 }
214 }
215 `;
216
217 function verifyExecutable(script)
218 {
219 let actual = compileScript(script, libraries);
220 let expected = template.replace("{{{script}}}",
221 JSON.stringify(parseScript(script)));
222
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
224 }
225
226 verifyExecutable("hello 'How are you?'");
227
228 // Test script execution.
229 new Function(compileScript("setFoo 123; assertFoo 123", libraries))();
230
231 // Override setFoo in a second library, without overriding assertFoo. A
232 // couple of things to note here: (1) each library has its own variables;
233 // (2) script execution is stateless, i.e. the values are not retained
234 // between executions. In the example below, assertFoo does not find 456 but
235 // it doesn't find 123 either. It's the initial value 0.
236 new Function(
237 compileScript("setFoo 456; assertFoo 0", [
238 ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };"
239 ])
240 )();
241
242 test.done();
243 };
OLDNEW
« lib/snippets.js ('K') | « lib/snippets.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld