optional-catch-binding.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // Tests optional catch binding syntax
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var tests = [
  8. {
  9. name: "Try-catch with no catch binding",
  10. body: function() {
  11. try {} catch {}
  12. },
  13. },
  14. {
  15. name: "Try-catch-finally with no catch binding",
  16. body: function() {
  17. try {} catch {} finally {}
  18. },
  19. },
  20. {
  21. name: "Try-catch with no catching binding lexical scope",
  22. body: function() {
  23. let x = 1;
  24. let ranCatch = false;
  25. try {
  26. x = 2;
  27. throw new Error();
  28. } catch {
  29. let x = 3;
  30. let y = true;
  31. ranCatch = true;
  32. }
  33. assert.isTrue(ranCatch, 'executed `catch` block');
  34. assert.areEqual(x, 2);
  35. assert.throws(function() { y; }, ReferenceError);
  36. },
  37. },
  38. {
  39. name: "Optional catch must not have empty parens",
  40. body: function() {
  41. assert.throws(function() { eval("try {} catch () {}"); }, SyntaxError);
  42. },
  43. },
  44. {
  45. name: "Errors are correctly thrown from catch",
  46. body: function() {
  47. class Err {}
  48. assert.throws(function() {
  49. try {
  50. throw new Error();
  51. } catch {
  52. throw new Err();
  53. }
  54. }, Err);
  55. },
  56. },
  57. {
  58. name: "Variables in catch block are properly scoped",
  59. body: function() {
  60. let x = 1;
  61. try {
  62. throw 1;
  63. } catch {
  64. let x = 2;
  65. var f1 = function () { return 'f1'; }
  66. function f2() { return 'f2'; }
  67. }
  68. assert.areEqual(x, 1);
  69. assert.areEqual(f1(), 'f1');
  70. assert.areEqual(f2(), 'f2');
  71. },
  72. },
  73. {
  74. name: "With scope in catch block",
  75. body: function() {
  76. function f() {
  77. try {
  78. throw 1;
  79. } catch {
  80. with ({ x: 1 }) {
  81. return function() { return x };
  82. }
  83. }
  84. }
  85. assert.areEqual(f()(), 1);
  86. },
  87. },
  88. {
  89. name: "Eval in catch block",
  90. body: function() {
  91. function f() {
  92. let x = 1;
  93. try {
  94. throw 1;
  95. } catch {
  96. let x = 2;
  97. return eval('function g() { return x }; g');
  98. }
  99. }
  100. assert.areEqual(f()(), 2);
  101. },
  102. },
  103. {
  104. name: "Async function with catch block with no binding",
  105. body: function() {
  106. async function foo() {
  107. try { throw "anything" } catch { await 5;}
  108. }
  109. assert.isTrue(foo() instanceof Promise, "await returns a promise");
  110. }
  111. },
  112. {
  113. name: "Async function with catch block with no binding",
  114. body: function() {
  115. function* foo() {
  116. try { throw "anything" } catch { yield 5;}
  117. }
  118. assert.areEqual(5, foo().next().value, "generator returns an object with yielded value");
  119. }
  120. },
  121. ];
  122. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });