try-finally-break.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. test("Nested try/catch/finally with break", () => {
  2. const executionOrder = [];
  3. try {
  4. for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
  5. try {
  6. try {
  7. try {
  8. break;
  9. } finally {
  10. expect(i).toBe(1337);
  11. executionOrder.push(1);
  12. }
  13. } finally {
  14. expect(i).toBe(1337);
  15. executionOrder.push(2);
  16. }
  17. } finally {
  18. expect(i).toBe(1337);
  19. executionOrder.push(3);
  20. }
  21. expect().fail("Running code after second to most outer try");
  22. }
  23. } finally {
  24. expect(() => {
  25. i;
  26. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  27. executionOrder.push(4);
  28. }
  29. expect(() => {
  30. i;
  31. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  32. expect(executionOrder).toEqual([1, 2, 3, 4]);
  33. });
  34. test("Nested try/finally with labelled break", () => {
  35. const executionOrder = [];
  36. outer: try {
  37. const libjs = 0;
  38. expect(libjs).toBe(0);
  39. try {
  40. const serenity = 1;
  41. expect(libjs).toBe(0);
  42. expect(serenity).toBe(1);
  43. for (const i = 2; ; expect().fail("Jumped to for loop update block")) {
  44. const foo = 3;
  45. expect(libjs).toBe(0);
  46. expect(serenity).toBe(1);
  47. expect(i).toBe(2);
  48. expect(foo).toBe(3);
  49. try {
  50. const bar = 4;
  51. expect(libjs).toBe(0);
  52. expect(serenity).toBe(1);
  53. expect(i).toBe(2);
  54. expect(foo).toBe(3);
  55. expect(bar).toBe(4);
  56. try {
  57. const baz = 5;
  58. expect(libjs).toBe(0);
  59. expect(serenity).toBe(1);
  60. expect(i).toBe(2);
  61. expect(foo).toBe(3);
  62. expect(bar).toBe(4);
  63. expect(baz).toBe(5);
  64. try {
  65. const whf = 6;
  66. expect(libjs).toBe(0);
  67. expect(serenity).toBe(1);
  68. expect(i).toBe(2);
  69. expect(foo).toBe(3);
  70. expect(bar).toBe(4);
  71. expect(baz).toBe(5);
  72. expect(whf).toBe(6);
  73. break outer;
  74. } finally {
  75. const innerFinally1 = 7;
  76. expect(() => {
  77. whf;
  78. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  79. expect(libjs).toBe(0);
  80. expect(serenity).toBe(1);
  81. expect(i).toBe(2);
  82. expect(foo).toBe(3);
  83. expect(bar).toBe(4);
  84. expect(baz).toBe(5);
  85. expect(innerFinally1).toBe(7);
  86. executionOrder.push(1);
  87. }
  88. expect().fail("Running code after most inner try");
  89. } finally {
  90. const innerFinally2 = 8;
  91. expect(() => {
  92. baz;
  93. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  94. expect(() => {
  95. whf;
  96. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  97. expect(() => {
  98. innerFinally1;
  99. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  100. expect(libjs).toBe(0);
  101. expect(serenity).toBe(1);
  102. expect(i).toBe(2);
  103. expect(foo).toBe(3);
  104. expect(bar).toBe(4);
  105. expect(innerFinally2).toBe(8);
  106. executionOrder.push(2);
  107. }
  108. expect().fail("Running code after second to most inner try");
  109. } finally {
  110. const innerFinally3 = 9;
  111. expect(() => {
  112. bar;
  113. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  114. expect(() => {
  115. baz;
  116. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  117. expect(() => {
  118. whf;
  119. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  120. expect(() => {
  121. innerFinally1;
  122. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  123. expect(() => {
  124. innerFinally2;
  125. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  126. expect(libjs).toBe(0);
  127. expect(serenity).toBe(1);
  128. expect(i).toBe(2);
  129. expect(foo).toBe(3);
  130. expect(innerFinally3).toBe(9);
  131. executionOrder.push(3);
  132. }
  133. expect().fail("Running code after third to most inner try");
  134. }
  135. expect().fail("Running code after for loop");
  136. } finally {
  137. const innerFinally4 = 10;
  138. expect(() => {
  139. serenity;
  140. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  141. expect(() => {
  142. i;
  143. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  144. expect(() => {
  145. foo;
  146. }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  147. expect(() => {
  148. bar;
  149. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  150. expect(() => {
  151. baz;
  152. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  153. expect(() => {
  154. whf;
  155. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  156. expect(() => {
  157. innerFinally1;
  158. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  159. expect(() => {
  160. innerFinally2;
  161. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  162. expect(() => {
  163. innerFinally3;
  164. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  165. expect(libjs).toBe(0);
  166. expect(innerFinally4).toBe(10);
  167. executionOrder.push(4);
  168. }
  169. expect().fail("Running code after second to outer try");
  170. } finally {
  171. const innerFinally5 = 11;
  172. expect(() => {
  173. libjs;
  174. }).toThrowWithMessage(ReferenceError, "'libjs' is not defined");
  175. expect(() => {
  176. serenity;
  177. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  178. expect(() => {
  179. i;
  180. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  181. expect(() => {
  182. foo;
  183. }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  184. expect(() => {
  185. bar;
  186. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  187. expect(() => {
  188. baz;
  189. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  190. expect(() => {
  191. whf;
  192. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  193. expect(() => {
  194. innerFinally1;
  195. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  196. expect(() => {
  197. innerFinally2;
  198. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  199. expect(() => {
  200. innerFinally3;
  201. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  202. expect(() => {
  203. innerFinally4;
  204. }).toThrowWithMessage(ReferenceError, "'innerFinally4' is not defined");
  205. expect(innerFinally5).toBe(11);
  206. executionOrder.push(5);
  207. }
  208. expect(() => {
  209. libjs;
  210. }).toThrowWithMessage(ReferenceError, "'libjs' is not defined");
  211. expect(() => {
  212. serenity;
  213. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  214. expect(() => {
  215. i;
  216. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  217. expect(() => {
  218. foo;
  219. }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  220. expect(() => {
  221. bar;
  222. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  223. expect(() => {
  224. baz;
  225. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  226. expect(() => {
  227. whf;
  228. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  229. expect(() => {
  230. innerFinally1;
  231. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  232. expect(() => {
  233. innerFinally2;
  234. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  235. expect(() => {
  236. innerFinally3;
  237. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  238. expect(() => {
  239. innerFinally4;
  240. }).toThrowWithMessage(ReferenceError, "'innerFinally4' is not defined");
  241. expect(() => {
  242. innerFinally5;
  243. }).toThrowWithMessage(ReferenceError, "'innerFinally5' is not defined");
  244. expect(executionOrder).toEqual([1, 2, 3, 4, 5]);
  245. });
  246. test("labelled break in finally overrides labelled break in try", () => {
  247. const executionOrder = [];
  248. outer: for (const i = 1; ; expect().fail("Jumped to outer for loop update block")) {
  249. inner: for (const j = 2; ; expect().fail("Jumped to inner for loop update block")) {
  250. try {
  251. executionOrder.push(1);
  252. break inner;
  253. } finally {
  254. executionOrder.push(2);
  255. break outer;
  256. }
  257. expect().fail("Running code after try block");
  258. }
  259. expect().fail("Running code after inner for loop");
  260. }
  261. expect(executionOrder).toEqual([1, 2]);
  262. });
  263. test("Throw while breaking", () => {
  264. const executionOrder = [];
  265. try {
  266. for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
  267. try {
  268. executionOrder.push(1);
  269. break;
  270. } finally {
  271. executionOrder.push(2);
  272. throw 1;
  273. }
  274. }
  275. } finally {
  276. executionOrder.push(3);
  277. }
  278. expect(() => {
  279. i;
  280. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  281. expect(executionOrder).toEqual([1, 2, 3]);
  282. });
  283. test("Throw while breaking with nested try-catch in finalizer", () => {
  284. const executionOrder = [];
  285. try {
  286. for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
  287. try {
  288. executionOrder.push(1);
  289. break;
  290. } finally {
  291. try {
  292. throw 1;
  293. } catch {
  294. executionOrder.push(2);
  295. }
  296. executionOrder.push(3);
  297. }
  298. expect().fail("Jumped out of inner finally statement");
  299. }
  300. } finally {
  301. executionOrder.push(4);
  302. }
  303. expect(() => {
  304. i;
  305. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  306. expect(executionOrder).toEqual([1, 2, 3, 4]);
  307. });
  308. test("Throw while breaking with nested try-catch-finally in finalizer", () => {
  309. const executionOrder = [];
  310. try {
  311. for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
  312. try {
  313. executionOrder.push(1);
  314. break;
  315. } finally {
  316. try {
  317. executionOrder.push(2);
  318. } catch {
  319. expect.fail("Entered catch");
  320. } finally {
  321. executionOrder.push(3);
  322. }
  323. executionOrder.push(4);
  324. }
  325. expect().fail("Jumped out of inner finally statement");
  326. }
  327. } finally {
  328. executionOrder.push(5);
  329. }
  330. expect(() => {
  331. i;
  332. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  333. expect(executionOrder).toEqual([1, 2, 3, 4, 5]);
  334. });
  335. test("Throw while breaking with nested try-catch-finally with throw in finalizer", () => {
  336. const executionOrder = [];
  337. try {
  338. for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
  339. try {
  340. executionOrder.push(1);
  341. break;
  342. } finally {
  343. try {
  344. throw 1;
  345. } catch {
  346. executionOrder.push(2);
  347. } finally {
  348. executionOrder.push(3);
  349. }
  350. executionOrder.push(4);
  351. }
  352. expect().fail("Jumped out of inner finally statement");
  353. }
  354. } finally {
  355. executionOrder.push(5);
  356. }
  357. expect(() => {
  358. i;
  359. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  360. expect(executionOrder).toEqual([1, 2, 3, 4, 5]);
  361. });