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: Created April 25, 2018, 8:03 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
« no previous file with comments | « 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
@@ -185,8 +185,71 @@
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 library1 = function(exports)
+ {
+ exports.hello = function(message)
+ {
+ console.log(message);
+ };
+ };
+
+ let library2 = function(exports)
+ {
+ exports.hello = function(message)
+ {
+ console.log(message || "Hello.");
+ };
+ };
+
+ let libraries = [library1.toString(), library2.toString()];
+ let stringifiedLibraries = JSON.stringify(libraries);
+
+ let template = `
+ "use strict";
+ {
+ const libraries = {{{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)
+ {
+ let parsedScript = parseScript(script);
+
+ let actual = compileScript(script, libraries);
+ let expected = template.replace("{{{libraries}}}", stringifiedLibraries)
+ .replace("{{{script}}}", JSON.stringify(parsedScript));
+
+ // Trim surrounding spaces because of possible difference in indentation.
+ test.equal(expected.trim(), actual.trim());
+ }
+
+ verifyExecutable("hello 'How are you?'");
+
+ test.done();
+};
« 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