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: Clean up test Created April 25, 2018, 8:20 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 | « 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
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", 183 "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"]]); 184 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]);
185 checkParsedScript("Script containing Unicode composite characters", 185 checkParsedScript("Script containing Unicode composite characters",
186 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", 186 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r",
187 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); 187 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]);
188 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", 188 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1",
189 [["foo"], ["bar", "1"]]); 189 [["foo"], ["bar", "1"]]);
190 190
191 test.done(); 191 test.done();
192 }; 192 };
193
194 exports.testScriptCompilation = function(test)
195 {
196 let {compileScript, parseScript} = Snippets;
197
198 let libraries = [
199 function(exports)
200 {
201 exports.hello = function(message)
202 {
203 console.log(message);
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 {
229 if (Object.prototype.hasOwnProperty.call(imports, name))
230 {
231 let value = imports[name];
232 if (typeof value == "function")
233 value(...args);
234 }
235 }
236 }
237 `;
238
239 function verifyExecutable(script)
240 {
241 let actual = compileScript(script, libraries);
242 let expected = template.replace("{{{script}}}",
243 JSON.stringify(parseScript(script)));
244
245 // Trim surrounding spaces because of possible difference in indentation.
246 test.equal(expected.trim(), actual.trim());
247 }
248
249 verifyExecutable("hello 'How are you?'");
250
251 test.done();
252 };
OLDNEW
« no previous file with comments | « lib/snippets.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld