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: Clean up test Created April 25, 2018, 8:20 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,68 @@
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 = [
+ function(exports)
+ {
+ exports.hello = function(message)
+ {
+ console.log(message);
+ };
+ },
+ function(exports)
+ {
+ exports.hello = function(message)
+ {
+ console.log(message || "Hello.");
+ };
+ }
+ ]
+ .map(library => library.toString());
+
+ 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)
+ {
+ 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.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