browser_scratchpad_eval_func.js 2.4 KB

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