try-catch-finally.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. test("try/catch without exception", () => {
  2. var tryHasBeenExecuted = false;
  3. var catchHasBeenExecuted = false;
  4. try {
  5. tryHasBeenExecuted = true;
  6. } catch (e) {
  7. catchHasBeenExecuted = true;
  8. }
  9. expect(tryHasBeenExecuted).toBeTrue();
  10. expect(catchHasBeenExecuted).toBeFalse();
  11. });
  12. test("try/catch with exception in try", () => {
  13. var tryHasBeenExecuted = false;
  14. var catchHasBeenExecuted = false;
  15. var tryError = Error("Error in try");
  16. try {
  17. tryHasBeenExecuted = true;
  18. throw tryError;
  19. expect().fail();
  20. } catch (e) {
  21. catchHasBeenExecuted = true;
  22. expect(e).toBe(tryError);
  23. }
  24. expect(tryHasBeenExecuted).toBeTrue();
  25. expect(catchHasBeenExecuted).toBeTrue();
  26. });
  27. test("try/catch with exception in try and catch", () => {
  28. var tryHasBeenExecuted = false;
  29. var catchHasBeenExecuted = false;
  30. var tryError = Error("Error in try");
  31. var catchError = Error("Error in catch");
  32. expect(() => {
  33. try {
  34. tryHasBeenExecuted = true;
  35. throw tryError;
  36. expect().fail();
  37. } catch (e) {
  38. catchHasBeenExecuted = true;
  39. expect(e).toBe(tryError);
  40. throw catchError;
  41. expect().fail();
  42. }
  43. }).toThrow(Error, "Error in catch");
  44. expect(tryHasBeenExecuted).toBeTrue();
  45. expect(catchHasBeenExecuted).toBeTrue();
  46. });
  47. test("try/catch/finally without exception", () => {
  48. var tryHasBeenExecuted = false;
  49. var catchHasBeenExecuted = false;
  50. var finallyHasBeenExecuted = false;
  51. try {
  52. tryHasBeenExecuted = true;
  53. } catch (e) {
  54. catchHasBeenExecuted = true;
  55. } finally {
  56. finallyHasBeenExecuted = true;
  57. }
  58. expect(tryHasBeenExecuted).toBeTrue();
  59. expect(catchHasBeenExecuted).toBeFalse();
  60. expect(finallyHasBeenExecuted).toBeTrue();
  61. });
  62. test("try/catch/finally with exception in try and catch", () => {
  63. var tryHasBeenExecuted = false;
  64. var catchHasBeenExecuted = false;
  65. var finallyHasBeenExecuted = false;
  66. var tryError = Error("Error in try");
  67. var catchError = Error("Error in catch");
  68. expect(() => {
  69. try {
  70. tryHasBeenExecuted = true;
  71. throw tryError;
  72. expect().fail();
  73. } catch (e) {
  74. catchHasBeenExecuted = true;
  75. expect(e).toBe(tryError);
  76. throw catchError;
  77. expect().fail();
  78. } finally {
  79. finallyHasBeenExecuted = true;
  80. }
  81. }).toThrow(Error, "Error in catch");
  82. expect(tryHasBeenExecuted).toBeTrue();
  83. expect(catchHasBeenExecuted).toBeTrue();
  84. expect(finallyHasBeenExecuted).toBeTrue();
  85. });
  86. test("try/catch/finally with exception in finally", () => {
  87. var tryHasBeenExecuted = false;
  88. var catchHasBeenExecuted = false;
  89. var finallyHasBeenExecuted = false;
  90. var finallyError = Error("Error in finally");
  91. expect(() => {
  92. try {
  93. tryHasBeenExecuted = true;
  94. } catch (e) {
  95. catchHasBeenExecuted = true;
  96. } finally {
  97. finallyHasBeenExecuted = true;
  98. throw finallyError;
  99. expect().fail();
  100. }
  101. }).toThrow(Error, "Error in finally");
  102. expect(tryHasBeenExecuted).toBeTrue();
  103. expect(catchHasBeenExecuted).toBeFalse();
  104. expect(finallyHasBeenExecuted).toBeTrue();
  105. });
  106. test("try/catch/finally with exception in try and finally", () => {
  107. var tryHasBeenExecuted = false;
  108. var catchHasBeenExecuted = false;
  109. var finallyHasBeenExecuted = false;
  110. var tryError = Error("Error in try");
  111. var finallyError = Error("Error in finally");
  112. expect(() => {
  113. try {
  114. tryHasBeenExecuted = true;
  115. throw tryError;
  116. expect().fail();
  117. } catch (e) {
  118. catchHasBeenExecuted = true;
  119. expect(e).toBe(tryError);
  120. } finally {
  121. finallyHasBeenExecuted = true;
  122. throw finallyError;
  123. expect().fail();
  124. }
  125. }).toThrow(Error, "Error in finally");
  126. expect(tryHasBeenExecuted).toBeTrue();
  127. expect(catchHasBeenExecuted).toBeTrue();
  128. expect(finallyHasBeenExecuted).toBeTrue();
  129. });
  130. test("try/catch/finally with exception in try, catch and finally", () => {
  131. var tryHasBeenExecuted = false;
  132. var catchHasBeenExecuted = false;
  133. var finallyHasBeenExecuted = false;
  134. var tryError = Error("Error in try");
  135. var catchError = Error("Error in catch");
  136. var finallyError = Error("Error in finally");
  137. expect(() => {
  138. try {
  139. tryHasBeenExecuted = true;
  140. throw tryError;
  141. expect().fail();
  142. } catch (e) {
  143. catchHasBeenExecuted = true;
  144. expect(e).toBe(tryError);
  145. throw catchError;
  146. expect().fail();
  147. } finally {
  148. finallyHasBeenExecuted = true;
  149. throw finallyError;
  150. expect().fail();
  151. }
  152. }).toThrow(Error, "Error in finally");
  153. expect(tryHasBeenExecuted).toBeTrue();
  154. expect(catchHasBeenExecuted).toBeTrue();
  155. expect(finallyHasBeenExecuted).toBeTrue();
  156. });
  157. test("try statement must have either 'catch' or 'finally' clause", () => {
  158. expect("try {} catch {}").toEval();
  159. expect("try {} catch (e) {}").toEval();
  160. expect("try {} finally {}").toEval();
  161. expect("try {} catch {} finally {}").toEval();
  162. expect("try {} catch (e) {} finally {}").toEval();
  163. expect("try {}").not.toEval();
  164. });