try-catch-finally-nested.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. test("Nested try/catch/finally with exceptions", () => {
  2. // This test uses a combination of boolean "checkpoint" flags
  3. // and expect().fail() to ensure certain code paths have been
  4. // reached and others haven't.
  5. var level1TryHasBeenExecuted = false;
  6. var level1CatchHasBeenExecuted = false;
  7. var level1FinallyHasBeenExecuted = false;
  8. var level2TryHasBeenExecuted = false;
  9. var level2CatchHasBeenExecuted = false;
  10. var level3TryHasBeenExecuted = false;
  11. var level3CatchHasBeenExecuted = false;
  12. var level3FinallyHasBeenExecuted = false;
  13. expect(() => {
  14. try {
  15. level1TryHasBeenExecuted = true;
  16. foo();
  17. expect().fail();
  18. } catch (e) {
  19. level1CatchHasBeenExecuted = true;
  20. try {
  21. level2TryHasBeenExecuted = true;
  22. try {
  23. level3TryHasBeenExecuted = true;
  24. bar();
  25. expect().fail();
  26. } catch (e) {
  27. level3CatchHasBeenExecuted = true;
  28. } finally {
  29. level3FinallyHasBeenExecuted = true;
  30. baz();
  31. expect().fail();
  32. }
  33. expect().fail();
  34. } catch (e) {
  35. level2CatchHasBeenExecuted = true;
  36. qux();
  37. expect().fail();
  38. }
  39. expect().fail();
  40. } finally {
  41. level1FinallyHasBeenExecuted = true;
  42. throw Error("Error in final finally");
  43. expect().fail();
  44. }
  45. expect().fail();
  46. }).toThrow(Error, "Error in final finally");
  47. expect(level1TryHasBeenExecuted).toBeTrue();
  48. expect(level1CatchHasBeenExecuted).toBeTrue();
  49. expect(level1FinallyHasBeenExecuted).toBeTrue();
  50. expect(level2TryHasBeenExecuted).toBeTrue();
  51. expect(level2CatchHasBeenExecuted).toBeTrue();
  52. expect(level3TryHasBeenExecuted).toBeTrue();
  53. expect(level3CatchHasBeenExecuted).toBeTrue();
  54. expect(level3FinallyHasBeenExecuted).toBeTrue();
  55. });
  56. test("Nested try/catch/finally with return in inner context", () => {
  57. success = false;
  58. (() => {
  59. try {
  60. try {
  61. return;
  62. } catch (e) {
  63. expect().fail();
  64. }
  65. } finally {
  66. success = true;
  67. }
  68. expect().fail();
  69. })();
  70. expect(success).toBeTrue();
  71. });
  72. test("Deeply nested try/catch/finally with return in inner context", () => {
  73. success = 0;
  74. (() => {
  75. try {
  76. try {
  77. try {
  78. try {
  79. try {
  80. return;
  81. } catch (e) {
  82. expect().fail();
  83. } finally {
  84. success += 4;
  85. }
  86. } catch (e) {
  87. expect().fail();
  88. }
  89. } catch (e) {
  90. expect().fail();
  91. } finally {
  92. success += 2;
  93. }
  94. } catch (e) {
  95. expect().fail();
  96. }
  97. } finally {
  98. success += 1;
  99. }
  100. expect().fail();
  101. })();
  102. expect(success).toBe(7);
  103. });
  104. test("Nested try/finally/catch with exception in inner context ", () => {
  105. success = 0;
  106. try {
  107. try {
  108. throw Error("Error in inner try");
  109. } finally {
  110. success += 1;
  111. }
  112. expect.fail();
  113. } catch (e) {
  114. success += 1;
  115. }
  116. expect(success).toBe(2);
  117. });
  118. test("Nested try/catch/finally with exception in inner most finally inside loop", () => {
  119. success = 0;
  120. try {
  121. try {
  122. do {
  123. try {
  124. throw 1;
  125. } finally {
  126. break;
  127. }
  128. expect.fail();
  129. } while (expect.fail());
  130. } catch (e) {
  131. expect.fail();
  132. }
  133. } finally {
  134. success = 1;
  135. }
  136. expect(success).toBe(1);
  137. });