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

Unified Diff: test/snippets.js

Issue 29761612: Issue 6538, 6781 - Implement script compilation for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Test script execution as well Created April 25, 2018, 9:07 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« lib/snippets.js ('K') | « lib/snippets.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/snippets.js
===================================================================
--- a/test/snippets.js
+++ b/test/snippets.js
@@ -10,16 +10,18 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
+/* eslint no-new-func: "off" */
Manish Jethani 2018/04/26 13:46:00 ESLint won't let me use new Function(...) even tho
+
"use strict";
const {createSandbox} = require("./_common");
let SnippetFilter = null;
let Snippets = null;
let ElemHide = null;
let Filter = null;
@@ -185,8 +187,81 @@
checkParsedScript("Script containing Unicode composite characters",
"f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r",
[["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]);
checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1",
[["foo"], ["bar", "1"]]);
test.done();
};
+
+exports.testScriptCompilation = function(test)
+{
+ let {compileScript, parseScript} = Snippets;
+
+ let libraries = [
+ `
+ let foo = 0;
+
+ exports.setFoo = function(value)
+ {
+ foo = value;
+ };
+
+ exports.assertFoo = function(expected)
+ {
+ if (foo != expected)
+ throw new Error("Value mismatch");
+ };
+ `
+ ];
+
+ let template = `
+ "use strict";
+ {
+ const libraries = ${JSON.stringify(libraries)};
+
+ const script = {{{script}}};
+
+ let imports = Object.create(null);
+ for (let library of libraries)
+ new Function("exports", library)(imports);
+
+ for (let [name, ...args] of script)
+ {
+ if (Object.prototype.hasOwnProperty.call(imports, name))
+ {
+ let value = imports[name];
+ if (typeof value == "function")
+ value(...args);
+ }
+ }
+ }
+ `;
+
+ function verifyExecutable(script)
Manish Jethani 2018/04/26 13:46:00 I don't know how valuable this is because "templat
+ {
+ let actual = compileScript(script, libraries);
+ let expected = template.replace("{{{script}}}",
+ JSON.stringify(parseScript(script)));
+
+ // Trim surrounding spaces because of possible difference in indentation.
+ test.equal(expected.trim(), actual.trim());
+ }
+
+ verifyExecutable("hello 'How are you?'");
+
+ // Test script execution.
+ new Function(compileScript("setFoo 123; assertFoo 123", libraries))();
+
+ // Override setFoo in a second library, without overriding assertFoo. A
+ // couple of things to note here: (1) each library has its own variables;
+ // (2) script execution is stateless, i.e. the values are not retained
+ // between executions. In the example below, assertFoo does not find 456 but
+ // it doesn't find 123 either. It's the initial value 0.
+ new Function(
+ compileScript("setFoo 456; assertFoo 0", [
+ ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };"
+ ])
+ )();
+
+ test.done();
+};
« lib/snippets.js ('K') | « lib/snippets.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld