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: Clean up test Created April 25, 2018, 8:20 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" */
19
18 "use strict"; 20 "use strict";
19 21
20 const {createSandbox} = require("./_common"); 22 const {createSandbox} = require("./_common");
21 23
22 let SnippetFilter = null;
23 let Snippets = null; 24 let Snippets = null;
24 let ElemHide = null; 25 let parseScript = null;
26 let compileScript = null;
25 let Filter = null; 27 let Filter = null;
26 28
27 exports.setUp = function(callback) 29 exports.setUp = function(callback)
28 { 30 {
29 let sandboxedRequire = createSandbox(); 31 let sandboxedRequire = createSandbox();
30 ( 32 (
31 {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"), 33 {Filter} = sandboxedRequire("../lib/filterClasses"),
32 {ElemHide} = sandboxedRequire("../lib/elemHide"), 34 {Snippets, parseScript, compileScript} = sandboxedRequire("../lib/snippets")
33 {Snippets} = sandboxedRequire("../lib/snippets")
34 ); 35 );
35 36
36 callback(); 37 callback();
37 }; 38 };
38 39
39 exports.testDomainRestrictions = function(test) 40 exports.testDomainRestrictions = function(test)
40 { 41 {
41 function testSelectorMatches(description, filters, domain, expectedMatches) 42 function testScriptMatches(description, filters, domain, expectedMatches)
42 { 43 {
43 for (let filter of filters) 44 for (let filter of filters)
44 { 45 Snippets.add(Filter.fromText(filter));
45 filter = Filter.fromText(filter); 46
46 if (filter instanceof SnippetFilter) 47 let matches = Snippets.getScriptsForDomain(domain);
47 Snippets.add(filter);
48 else
49 ElemHide.add(filter);
50 }
51
52 let matches = Snippets.getScriptsForDomain(domain)
53 .map(filter => filter.text);
54 test.deepEqual(matches.sort(), expectedMatches.sort(), description); 48 test.deepEqual(matches.sort(), expectedMatches.sort(), description);
55 49
56 Snippets.clear(); 50 Snippets.clear();
57 ElemHide.clear(); 51 }
58 } 52
59 53 testScriptMatches(
60 testSelectorMatches(
61 "Ignore selectors with exceptions",
62 [
63 "example.com#$#foo",
64 "example.com#$#bar",
65 "example.com#@#foo"
66 ],
67 "example.com",
68 ["example.com#$#bar"]
69 );
70 testSelectorMatches(
71 "Ignore filters that include parent domain but exclude subdomain", 54 "Ignore filters that include parent domain but exclude subdomain",
72 [ 55 [
73 "~www.example.com,example.com#$#foo" 56 "~www.example.com,example.com#$#foo"
74 ], 57 ],
75 "www.example.com", 58 "www.example.com",
76 [] 59 []
77 ); 60 );
78 testSelectorMatches( 61 testScriptMatches(
79 "Ignore filters with parent domain if exception matches subdomain",
80 [
81 "www.example.com#@#foo",
82 "example.com#$#foo"
83 ],
84 "www.example.com",
85 []
86 );
87 testSelectorMatches(
88 "Ignore filters for other subdomain", 62 "Ignore filters for other subdomain",
89 [ 63 [
90 "www.example.com#$#foo", 64 "www.example.com#$#foo",
91 "other.example.com#$#foo" 65 "other.example.com#$#foo"
92 ], 66 ],
93 "other.example.com", 67 "other.example.com",
94 ["other.example.com#$#foo"] 68 ["foo"]
95 ); 69 );
96 70
97 test.done(); 71 test.done();
98 }; 72 };
99 73
100 exports.testSnippetFiltersContainer = function(test) 74 exports.testSnippetFiltersContainer = function(test)
101 { 75 {
102 function compareRules(description, domain, expectedMatches) 76 function compareRules(description, domain, expectedMatches)
103 { 77 {
104 let result = Snippets.getScriptsForDomain(domain) 78 let result = Snippets.getScriptsForDomain(domain);
105 .map(filter => filter.text); 79 expectedMatches = expectedMatches.map(filter => filter.script);
106 expectedMatches = expectedMatches.map(filter => filter.text);
107 test.deepEqual(result.sort(), expectedMatches.sort(), description); 80 test.deepEqual(result.sort(), expectedMatches.sort(), description);
108 } 81 }
109 82
110 let domainFilter = Filter.fromText("example.com#$#filter1"); 83 let domainFilter = Filter.fromText("example.com#$#filter1");
111 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); 84 let subdomainFilter = Filter.fromText("www.example.com#$#filter2");
112 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); 85 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3");
113 86
114 Snippets.add(domainFilter); 87 Snippets.add(domainFilter);
115 Snippets.add(subdomainFilter); 88 Snippets.add(subdomainFilter);
116 Snippets.add(otherDomainFilter); 89 Snippets.add(otherDomainFilter);
(...skipping 17 matching lines...) Expand all
134 [] 107 []
135 ); 108 );
136 109
137 test.done(); 110 test.done();
138 }; 111 };
139 112
140 exports.testScriptParsing = function(test) 113 exports.testScriptParsing = function(test)
141 { 114 {
142 function checkParsedScript(description, script, expectedTree) 115 function checkParsedScript(description, script, expectedTree)
143 { 116 {
144 let tree = Snippets.parseScript(script); 117 let tree = parseScript(script);
145 test.deepEqual(tree, expectedTree, description); 118 test.deepEqual(tree, expectedTree, description);
146 } 119 }
147 120
148 checkParsedScript("Script with no arguments", "foo", [["foo"]]); 121 checkParsedScript("Script with no arguments", "foo", [["foo"]]);
149 checkParsedScript("Script with one argument", "foo 1", [["foo", "1"]]); 122 checkParsedScript("Script with one argument", "foo 1", [["foo", "1"]]);
150 checkParsedScript("Script with two arguments", "foo 1 Hello", 123 checkParsedScript("Script with two arguments", "foo 1 Hello",
151 [["foo", "1", "Hello"]]); 124 [["foo", "1", "Hello"]]);
152 checkParsedScript("Script with argument containing an escaped space", 125 checkParsedScript("Script with argument containing an escaped space",
153 "foo Hello\\ world", 126 "foo Hello\\ world",
154 [["foo", "Hello world"]]); 127 [["foo", "Hello world"]]);
155 checkParsedScript("Script with argument containing a quoted space", 128 checkParsedScript("Script with argument containing a quoted space",
156 "foo 'Hello world'", 129 "foo 'Hello world'",
157 [["foo", "Hello world"]]); 130 [["foo", "Hello world"]]);
158 checkParsedScript("Script with argument containing a quoted escaped quote", 131 checkParsedScript("Script with argument containing a quoted escaped quote",
159 "foo 'Hello \\'world\\''", 132 "foo 'Hello \\'world\\''",
160 [["foo", "Hello 'world'"]]); 133 [["foo", "Hello 'world'"]]);
161 checkParsedScript("Script with argument containing an escaped semicolon", 134 checkParsedScript("Script with argument containing an escaped semicolon",
162 "foo TL\\;DR", 135 "foo TL\\;DR",
163 [["foo", "TL;DR"]]); 136 [["foo", "TL;DR"]]);
164 checkParsedScript("Script with argument containing a quoted semicolon", 137 checkParsedScript("Script with argument containing a quoted semicolon",
165 "foo 'TL;DR'", 138 "foo 'TL;DR'",
166 [["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 ]);
167 checkParsedScript("Script with multiple commands", "foo; bar", 149 checkParsedScript("Script with multiple commands", "foo; bar",
168 [["foo"], ["bar"]]); 150 [["foo"], ["bar"]]);
169 checkParsedScript("Script with multiple commands and multiple arguments each", 151 checkParsedScript("Script with multiple commands and multiple arguments each",
170 "foo 1 Hello; bar world! #", 152 "foo 1 Hello; bar world! #",
171 [["foo", "1", "Hello"], ["bar", "world!", "#"]]); 153 [["foo", "1", "Hello"], ["bar", "world!", "#"]]);
172 checkParsedScript( 154 checkParsedScript("Script with multiple commands and multiple " +
173 "Script with multiple commands and multiple " + 155 "escaped and quoted arguments each",
174 "escaped and quoted arguments each", 156 "foo 1 'Hello, \\'Tommy\\'!' ;" +
175 "foo 1 'Hello, \\'Tommy\\'!' ;" + 157 "bar Hi!\\ How\\ are\\ you? http://example.com", [
176 "bar Hi!\\ How\\ are\\ you? http://example.com", 158 ["foo", "1", "Hello, 'Tommy'!"],
177 [["foo", "1", "Hello, 'Tommy'!"], 159 ["bar", "Hi! How are you?", "http://example.com"]
178 ["bar", "Hi! How are you?", "http://example.com"]] 160 ]);
179 );
180 checkParsedScript("Script with command names containing " + 161 checkParsedScript("Script with command names containing " +
181 "whitespace (spaces, tabs, newlines, etc.), " + 162 "whitespace (spaces, tabs, newlines, etc.), " +
182 "quotes, and semicolons", 163 "quotes, and semicolons",
183 "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",
184 [["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"]]);
185 checkParsedScript("Script containing Unicode composite characters", 166 checkParsedScript("Script containing Unicode composite characters",
186 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", 167 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r",
187 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); 168 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]);
188 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", 169 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1",
189 [["foo"], ["bar", "1"]]); 170 [["foo"], ["bar", "1"]]);
190 171
191 test.done(); 172 test.done();
192 }; 173 };
193 174
194 exports.testScriptCompilation = function(test) 175 exports.testScriptCompilation = function(test)
195 { 176 {
196 let {compileScript, parseScript} = Snippets;
197
198 let libraries = [ 177 let libraries = [
199 function(exports) 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";
200 { 196 {
201 exports.hello = function(message) 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)
202 { 206 {
203 console.log(message); 207 if (Object.prototype.hasOwnProperty.call(imports, name))
204 };
205 },
206 function(exports)
207 {
208 exports.hello = function(message)
209 {
210 console.log(message || "Hello.");
211 };
212 }
213 ]
214 .map(library => library.toString());
215
216 let template = `
217 "use strict";
218 {
219 const libraries = ${JSON.stringify(libraries)};
220
221 const script = {{{script}}};
222
223 let imports = Object.create(null);
224 for (let library of libraries)
225 new Function("exports", library)(imports);
226
227 for (let [name, ...args] of script)
228 { 208 {
229 if (Object.prototype.hasOwnProperty.call(imports, name)) 209 let value = imports[name];
230 { 210 if (typeof value == "function")
231 let value = imports[name]; 211 value(...args);
232 if (typeof value == "function")
233 value(...args);
234 }
235 } 212 }
236 } 213 }
214 }
237 `; 215 `;
238 216
239 function verifyExecutable(script) 217 function verifyExecutable(script)
240 { 218 {
241 let actual = compileScript(script, libraries); 219 let actual = compileScript(script, libraries);
242 let expected = template.replace("{{{script}}}", 220 let expected = template.replace("{{{script}}}",
243 JSON.stringify(parseScript(script))); 221 JSON.stringify(parseScript(script)));
244 222
245 // 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
246 test.equal(expected.trim(), actual.trim());
247 } 224 }
248 225
249 verifyExecutable("hello 'How are you?'"); 226 verifyExecutable("hello 'How are you?'");
250 227
251 test.done(); 228 // Test script execution.
252 }; 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 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld