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(); |
+}; |