|
1 /* vim: set ts=2 et sw=2 tw=80: */ |
|
2 /* Any copyright is dedicated to the Public Domain. |
|
3 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
4 |
|
5 function test() |
|
6 { |
|
7 waitForExplicitFinish(); |
|
8 |
|
9 gBrowser.selectedTab = gBrowser.addTab(); |
|
10 gBrowser.selectedBrowser.addEventListener("load", function onLoad() { |
|
11 gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); |
|
12 openScratchpad(runTests); |
|
13 }, true); |
|
14 |
|
15 content.location = "data:text/html;charset=utf8,test Scratchpad eval function."; |
|
16 } |
|
17 |
|
18 function reportErrorAndQuit(error) { |
|
19 DevToolsUtils.reportException("browser_scratchpad_eval_func.js", error); |
|
20 ok(false); |
|
21 finish(); |
|
22 } |
|
23 |
|
24 function runTests(sw) |
|
25 { |
|
26 const sp = sw.Scratchpad; |
|
27 |
|
28 let foo = "" + function main() { console.log(1); }; |
|
29 let bar = "var bar = " + (() => { console.log(2); }); |
|
30 |
|
31 const fullText = |
|
32 foo + "\n" + |
|
33 "\n" + |
|
34 bar + "\n" |
|
35 |
|
36 sp.setText(fullText); |
|
37 |
|
38 // On the function declaration. |
|
39 sp.editor.setCursor({ line: 0, ch: 18 }); |
|
40 sp.evalTopLevelFunction() |
|
41 .then(([text, error, result]) => { |
|
42 is(text, foo, "Should re-eval foo."); |
|
43 ok(!error, "Should not have got an error."); |
|
44 ok(result, "Should have got a result."); |
|
45 }) |
|
46 |
|
47 // On the arrow function. |
|
48 .then(() => { |
|
49 sp.editor.setCursor({ line: 2, ch: 18 }); |
|
50 return sp.evalTopLevelFunction(); |
|
51 }) |
|
52 .then(([text, error, result]) => { |
|
53 is(text, bar.replace("var ", ""), "Should re-eval bar."); |
|
54 ok(!error, "Should not have got an error."); |
|
55 ok(result, "Should have got a result."); |
|
56 }) |
|
57 |
|
58 // On the empty line. |
|
59 .then(() => { |
|
60 sp.editor.setCursor({ line: 1, ch: 0 }); |
|
61 return sp.evalTopLevelFunction(); |
|
62 }) |
|
63 .then(([text, error, result]) => { |
|
64 is(text, fullText, |
|
65 "Should get full text back since we didn't find a specific function."); |
|
66 ok(!error, "Should not have got an error."); |
|
67 ok(!result, "Should not have got a result."); |
|
68 }) |
|
69 |
|
70 // Syntax error. |
|
71 .then(() => { |
|
72 sp.setText("function {}"); |
|
73 sp.editor.setCursor({ line: 0, ch: 9 }); |
|
74 return sp.evalTopLevelFunction(); |
|
75 }) |
|
76 .then(([text, error, result]) => { |
|
77 is(text, "function {}", |
|
78 "Should get the full text back since there was a parse error."); |
|
79 ok(!error, "Should not have got an error"); |
|
80 ok(!result, "Should not have got a result"); |
|
81 ok(sp.getText().contains("SyntaxError"), |
|
82 "We should have written the syntax error to the scratchpad."); |
|
83 }) |
|
84 |
|
85 .then(finish, reportErrorAndQuit); |
|
86 } |