michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function test() michael@0: { michael@0: waitForExplicitFinish(); michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function onLoad() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); michael@0: openScratchpad(runTests); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html;charset=utf8,test Scratchpad eval function."; michael@0: } michael@0: michael@0: function reportErrorAndQuit(error) { michael@0: DevToolsUtils.reportException("browser_scratchpad_eval_func.js", error); michael@0: ok(false); michael@0: finish(); michael@0: } michael@0: michael@0: function runTests(sw) michael@0: { michael@0: const sp = sw.Scratchpad; michael@0: michael@0: let foo = "" + function main() { console.log(1); }; michael@0: let bar = "var bar = " + (() => { console.log(2); }); michael@0: michael@0: const fullText = michael@0: foo + "\n" + michael@0: "\n" + michael@0: bar + "\n" michael@0: michael@0: sp.setText(fullText); michael@0: michael@0: // On the function declaration. michael@0: sp.editor.setCursor({ line: 0, ch: 18 }); michael@0: sp.evalTopLevelFunction() michael@0: .then(([text, error, result]) => { michael@0: is(text, foo, "Should re-eval foo."); michael@0: ok(!error, "Should not have got an error."); michael@0: ok(result, "Should have got a result."); michael@0: }) michael@0: michael@0: // On the arrow function. michael@0: .then(() => { michael@0: sp.editor.setCursor({ line: 2, ch: 18 }); michael@0: return sp.evalTopLevelFunction(); michael@0: }) michael@0: .then(([text, error, result]) => { michael@0: is(text, bar.replace("var ", ""), "Should re-eval bar."); michael@0: ok(!error, "Should not have got an error."); michael@0: ok(result, "Should have got a result."); michael@0: }) michael@0: michael@0: // On the empty line. michael@0: .then(() => { michael@0: sp.editor.setCursor({ line: 1, ch: 0 }); michael@0: return sp.evalTopLevelFunction(); michael@0: }) michael@0: .then(([text, error, result]) => { michael@0: is(text, fullText, michael@0: "Should get full text back since we didn't find a specific function."); michael@0: ok(!error, "Should not have got an error."); michael@0: ok(!result, "Should not have got a result."); michael@0: }) michael@0: michael@0: // Syntax error. michael@0: .then(() => { michael@0: sp.setText("function {}"); michael@0: sp.editor.setCursor({ line: 0, ch: 9 }); michael@0: return sp.evalTopLevelFunction(); michael@0: }) michael@0: .then(([text, error, result]) => { michael@0: is(text, "function {}", michael@0: "Should get the full text back since there was a parse error."); michael@0: ok(!error, "Should not have got an error"); michael@0: ok(!result, "Should not have got a result"); michael@0: ok(sp.getText().contains("SyntaxError"), michael@0: "We should have written the syntax error to the scratchpad."); michael@0: }) michael@0: michael@0: .then(finish, reportErrorAndQuit); michael@0: }