1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_eval_func.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,86 @@ 1.4 +/* vim: set ts=2 et sw=2 tw=80: */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 + 1.8 +function test() 1.9 +{ 1.10 + waitForExplicitFinish(); 1.11 + 1.12 + gBrowser.selectedTab = gBrowser.addTab(); 1.13 + gBrowser.selectedBrowser.addEventListener("load", function onLoad() { 1.14 + gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); 1.15 + openScratchpad(runTests); 1.16 + }, true); 1.17 + 1.18 + content.location = "data:text/html;charset=utf8,test Scratchpad eval function."; 1.19 +} 1.20 + 1.21 +function reportErrorAndQuit(error) { 1.22 + DevToolsUtils.reportException("browser_scratchpad_eval_func.js", error); 1.23 + ok(false); 1.24 + finish(); 1.25 +} 1.26 + 1.27 +function runTests(sw) 1.28 +{ 1.29 + const sp = sw.Scratchpad; 1.30 + 1.31 + let foo = "" + function main() { console.log(1); }; 1.32 + let bar = "var bar = " + (() => { console.log(2); }); 1.33 + 1.34 + const fullText = 1.35 + foo + "\n" + 1.36 + "\n" + 1.37 + bar + "\n" 1.38 + 1.39 + sp.setText(fullText); 1.40 + 1.41 + // On the function declaration. 1.42 + sp.editor.setCursor({ line: 0, ch: 18 }); 1.43 + sp.evalTopLevelFunction() 1.44 + .then(([text, error, result]) => { 1.45 + is(text, foo, "Should re-eval foo."); 1.46 + ok(!error, "Should not have got an error."); 1.47 + ok(result, "Should have got a result."); 1.48 + }) 1.49 + 1.50 + // On the arrow function. 1.51 + .then(() => { 1.52 + sp.editor.setCursor({ line: 2, ch: 18 }); 1.53 + return sp.evalTopLevelFunction(); 1.54 + }) 1.55 + .then(([text, error, result]) => { 1.56 + is(text, bar.replace("var ", ""), "Should re-eval bar."); 1.57 + ok(!error, "Should not have got an error."); 1.58 + ok(result, "Should have got a result."); 1.59 + }) 1.60 + 1.61 + // On the empty line. 1.62 + .then(() => { 1.63 + sp.editor.setCursor({ line: 1, ch: 0 }); 1.64 + return sp.evalTopLevelFunction(); 1.65 + }) 1.66 + .then(([text, error, result]) => { 1.67 + is(text, fullText, 1.68 + "Should get full text back since we didn't find a specific function."); 1.69 + ok(!error, "Should not have got an error."); 1.70 + ok(!result, "Should not have got a result."); 1.71 + }) 1.72 + 1.73 + // Syntax error. 1.74 + .then(() => { 1.75 + sp.setText("function {}"); 1.76 + sp.editor.setCursor({ line: 0, ch: 9 }); 1.77 + return sp.evalTopLevelFunction(); 1.78 + }) 1.79 + .then(([text, error, result]) => { 1.80 + is(text, "function {}", 1.81 + "Should get the full text back since there was a parse error."); 1.82 + ok(!error, "Should not have got an error"); 1.83 + ok(!result, "Should not have got a result"); 1.84 + ok(sp.getText().contains("SyntaxError"), 1.85 + "We should have written the syntax error to the scratchpad."); 1.86 + }) 1.87 + 1.88 + .then(finish, reportErrorAndQuit); 1.89 +}