try-finally-continue.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. test("Nested try/catch/finally with continue", () => {
  2. const executionOrder = [];
  3. function callFromUpdateBlock(i) {
  4. expect(i).toBe(2);
  5. executionOrder.push(4);
  6. }
  7. function callFromTestBlock(i) {
  8. expect(i).toBe(2);
  9. executionOrder.push(5);
  10. return false;
  11. }
  12. try {
  13. const foo = 0;
  14. expect(foo).toBe(0);
  15. for (let i = 1; i >= 2 ? callFromTestBlock(i) : true; ++i, callFromUpdateBlock(i)) {
  16. const bar = 2;
  17. expect(foo).toBe(0);
  18. expect(i).toBe(1);
  19. expect(bar).toBe(2);
  20. try {
  21. const baz = 3;
  22. expect(foo).toBe(0);
  23. expect(i).toBe(1);
  24. expect(bar).toBe(2);
  25. expect(baz).toBe(3);
  26. try {
  27. const serenity = 4;
  28. expect(foo).toBe(0);
  29. expect(i).toBe(1);
  30. expect(bar).toBe(2);
  31. expect(baz).toBe(3);
  32. expect(serenity).toBe(4);
  33. try {
  34. const whf = 5;
  35. expect(foo).toBe(0);
  36. expect(i).toBe(1);
  37. expect(bar).toBe(2);
  38. expect(baz).toBe(3);
  39. expect(serenity).toBe(4);
  40. expect(whf).toBe(5);
  41. continue;
  42. } finally {
  43. const innerFinally1 = 6;
  44. expect(() => {
  45. whf;
  46. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  47. expect(foo).toBe(0);
  48. expect(i).toBe(1);
  49. expect(bar).toBe(2);
  50. expect(baz).toBe(3);
  51. expect(serenity).toBe(4);
  52. expect(innerFinally1).toBe(6);
  53. executionOrder.push(1);
  54. }
  55. expect().fail("Running code after most inner try in for loop");
  56. } finally {
  57. const innerFinally2 = 7;
  58. expect(() => {
  59. serenity;
  60. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  61. expect(() => {
  62. whf;
  63. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  64. expect(() => {
  65. innerFinally1;
  66. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  67. expect(foo).toBe(0);
  68. expect(i).toBe(1);
  69. expect(bar).toBe(2);
  70. expect(baz).toBe(3);
  71. expect(innerFinally2).toBe(7);
  72. executionOrder.push(2);
  73. }
  74. expect().fail("Running code from after the middle try in for loop");
  75. } finally {
  76. const innerFinally3 = 8;
  77. expect(() => {
  78. baz;
  79. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  80. expect(() => {
  81. serenity;
  82. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  83. expect(() => {
  84. whf;
  85. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  86. expect(() => {
  87. innerFinally1;
  88. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  89. expect(() => {
  90. innerFinally2;
  91. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  92. expect(foo).toBe(0);
  93. expect(i).toBe(1);
  94. expect(bar).toBe(2);
  95. expect(innerFinally3).toBe(8);
  96. executionOrder.push(3);
  97. }
  98. expect().fail("Running code from after the outer try in for loop");
  99. }
  100. executionOrder.push(6);
  101. expect(() => {
  102. i;
  103. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  104. expect(() => {
  105. bar;
  106. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  107. expect(() => {
  108. baz;
  109. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  110. expect(() => {
  111. serenity;
  112. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  113. expect(() => {
  114. whf;
  115. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  116. expect(() => {
  117. innerFinally1;
  118. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  119. expect(() => {
  120. innerFinally2;
  121. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  122. expect(() => {
  123. innerFinally3;
  124. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  125. expect(foo).toBe(0);
  126. } finally {
  127. const innerFinally4 = 9;
  128. expect(() => {
  129. foo;
  130. }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  131. expect(() => {
  132. i;
  133. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  134. expect(() => {
  135. bar;
  136. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  137. expect(() => {
  138. baz;
  139. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  140. expect(() => {
  141. serenity;
  142. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  143. expect(() => {
  144. whf;
  145. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  146. expect(() => {
  147. innerFinally1;
  148. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  149. expect(() => {
  150. innerFinally2;
  151. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  152. expect(() => {
  153. innerFinally3;
  154. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  155. expect(innerFinally4).toBe(9);
  156. executionOrder.push(7);
  157. }
  158. expect(() => {
  159. foo;
  160. }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  161. expect(() => {
  162. i;
  163. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  164. expect(() => {
  165. bar;
  166. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  167. expect(() => {
  168. baz;
  169. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  170. expect(() => {
  171. serenity;
  172. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  173. expect(() => {
  174. whf;
  175. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  176. expect(() => {
  177. innerFinally1;
  178. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  179. expect(() => {
  180. innerFinally2;
  181. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  182. expect(() => {
  183. innerFinally3;
  184. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  185. expect(() => {
  186. innerFinally4;
  187. }).toThrowWithMessage(ReferenceError, "'innerFinally4' is not defined");
  188. expect(executionOrder).toEqual([1, 2, 3, 4, 5, 6, 7]);
  189. });
  190. test("Nested try/catch/finally with labelled continue", () => {
  191. const executionOrder = [];
  192. function callFromUpdateBlock(j) {
  193. expect(j).toBe(2);
  194. executionOrder.push(5);
  195. }
  196. function callFromTestBlock(j) {
  197. expect(j).toBe(2);
  198. executionOrder.push(6);
  199. return false;
  200. }
  201. try {
  202. const foo = 0;
  203. expect(foo).toBe(0);
  204. outer: for (let j = 1; j >= 2 ? callFromTestBlock(j) : true; ++j, callFromUpdateBlock(j)) {
  205. const bar = 2;
  206. expect(foo).toBe(0);
  207. expect(j).toBe(1);
  208. expect(bar).toBe(2);
  209. try {
  210. const baz = 3;
  211. expect(foo).toBe(0);
  212. expect(j).toBe(1);
  213. expect(bar).toBe(2);
  214. expect(baz).toBe(3);
  215. for (const i = 4; ; expect().fail("Jumped to inner for loop update block")) {
  216. const serenity = 5;
  217. expect(foo).toBe(0);
  218. expect(j).toBe(1);
  219. expect(bar).toBe(2);
  220. expect(baz).toBe(3);
  221. expect(i).toBe(4);
  222. expect(serenity).toBe(5);
  223. try {
  224. const whf = 6;
  225. expect(foo).toBe(0);
  226. expect(j).toBe(1);
  227. expect(bar).toBe(2);
  228. expect(baz).toBe(3);
  229. expect(i).toBe(4);
  230. expect(serenity).toBe(5);
  231. expect(whf).toBe(6);
  232. try {
  233. const beforeContinueTry = 7;
  234. expect(foo).toBe(0);
  235. expect(j).toBe(1);
  236. expect(bar).toBe(2);
  237. expect(baz).toBe(3);
  238. expect(i).toBe(4);
  239. expect(serenity).toBe(5);
  240. expect(whf).toBe(6);
  241. expect(beforeContinueTry).toBe(7);
  242. try {
  243. const continueTry = 8;
  244. expect(foo).toBe(0);
  245. expect(j).toBe(1);
  246. expect(bar).toBe(2);
  247. expect(baz).toBe(3);
  248. expect(i).toBe(4);
  249. expect(serenity).toBe(5);
  250. expect(whf).toBe(6);
  251. expect(beforeContinueTry).toBe(7);
  252. expect(continueTry).toBe(8);
  253. continue outer;
  254. } finally {
  255. const innerFinally1 = 9;
  256. expect(() => {
  257. continueTry;
  258. }).toThrowWithMessage(
  259. ReferenceError,
  260. "'continueTry' is not defined"
  261. );
  262. expect(foo).toBe(0);
  263. expect(j).toBe(1);
  264. expect(bar).toBe(2);
  265. expect(baz).toBe(3);
  266. expect(i).toBe(4);
  267. expect(serenity).toBe(5);
  268. expect(whf).toBe(6);
  269. expect(beforeContinueTry).toBe(7);
  270. expect(innerFinally1).toBe(9);
  271. executionOrder.push(1);
  272. }
  273. expect().fail("Running code after most inner try");
  274. } finally {
  275. const innerFinally2 = 10;
  276. expect(() => {
  277. beforeContinueTry;
  278. }).toThrowWithMessage(
  279. ReferenceError,
  280. "'beforeContinueTry' is not defined"
  281. );
  282. expect(() => {
  283. continueTry;
  284. }).toThrowWithMessage(ReferenceError, "'continueTry' is not defined");
  285. expect(() => {
  286. innerFinally1;
  287. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  288. expect(foo).toBe(0);
  289. expect(j).toBe(1);
  290. expect(bar).toBe(2);
  291. expect(baz).toBe(3);
  292. expect(i).toBe(4);
  293. expect(serenity).toBe(5);
  294. expect(whf).toBe(6);
  295. expect(innerFinally2).toBe(10);
  296. executionOrder.push(2);
  297. }
  298. expect().fail("Running code after second to most inner try");
  299. } finally {
  300. const innerFinally3 = 11;
  301. expect(() => {
  302. whf;
  303. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  304. expect(() => {
  305. beforeContinueTry;
  306. }).toThrowWithMessage(ReferenceError, "'beforeContinueTry' is not defined");
  307. expect(() => {
  308. continueTry;
  309. }).toThrowWithMessage(ReferenceError, "'continueTry' is not defined");
  310. expect(() => {
  311. innerFinally1;
  312. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  313. expect(() => {
  314. innerFinally2;
  315. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  316. expect(foo).toBe(0);
  317. expect(j).toBe(1);
  318. expect(bar).toBe(2);
  319. expect(baz).toBe(3);
  320. expect(i).toBe(4);
  321. expect(serenity).toBe(5);
  322. expect(innerFinally3).toBe(11);
  323. executionOrder.push(3);
  324. }
  325. expect().fail("Running code after third to most inner try");
  326. }
  327. expect().fail("Running code after inner for loop");
  328. } finally {
  329. const innerFinally4 = 12;
  330. expect(() => {
  331. baz;
  332. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  333. expect(() => {
  334. i;
  335. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  336. expect(() => {
  337. serenity;
  338. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  339. expect(() => {
  340. whf;
  341. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  342. expect(() => {
  343. beforeContinueTry;
  344. }).toThrowWithMessage(ReferenceError, "'beforeContinueTry' is not defined");
  345. expect(() => {
  346. continueTry;
  347. }).toThrowWithMessage(ReferenceError, "'continueTry' is not defined");
  348. expect(() => {
  349. innerFinally1;
  350. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  351. expect(() => {
  352. innerFinally2;
  353. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  354. expect(() => {
  355. innerFinally3;
  356. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  357. expect(foo).toBe(0);
  358. expect(j).toBe(1);
  359. expect(bar).toBe(2);
  360. expect(innerFinally4).toBe(12);
  361. executionOrder.push(4);
  362. }
  363. expect().fail("Running code after try in outer for loop");
  364. }
  365. expect(() => {
  366. j;
  367. }).toThrowWithMessage(ReferenceError, "'j' is not defined");
  368. expect(() => {
  369. bar;
  370. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  371. expect(() => {
  372. baz;
  373. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  374. expect(() => {
  375. i;
  376. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  377. expect(() => {
  378. serenity;
  379. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  380. expect(() => {
  381. whf;
  382. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  383. expect(() => {
  384. beforeContinueTry;
  385. }).toThrowWithMessage(ReferenceError, "'beforeContinueTry' is not defined");
  386. expect(() => {
  387. continueTry;
  388. }).toThrowWithMessage(ReferenceError, "'continueTry' is not defined");
  389. expect(() => {
  390. innerFinally1;
  391. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  392. expect(() => {
  393. innerFinally2;
  394. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  395. expect(() => {
  396. innerFinally3;
  397. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  398. expect(() => {
  399. innerFinally4;
  400. }).toThrowWithMessage(ReferenceError, "'innerFinally4' is not defined");
  401. expect(foo).toBe(0);
  402. executionOrder.push(7);
  403. } finally {
  404. const innerFinally5 = 13;
  405. expect(() => {
  406. foo;
  407. }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  408. expect(() => {
  409. j;
  410. }).toThrowWithMessage(ReferenceError, "'j' is not defined");
  411. expect(() => {
  412. bar;
  413. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  414. expect(() => {
  415. baz;
  416. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  417. expect(() => {
  418. i;
  419. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  420. expect(() => {
  421. serenity;
  422. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  423. expect(() => {
  424. whf;
  425. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  426. expect(() => {
  427. beforeContinueTry;
  428. }).toThrowWithMessage(ReferenceError, "'beforeContinueTry' is not defined");
  429. expect(() => {
  430. continueTry;
  431. }).toThrowWithMessage(ReferenceError, "'continueTry' is not defined");
  432. expect(() => {
  433. innerFinally1;
  434. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  435. expect(() => {
  436. innerFinally2;
  437. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  438. expect(() => {
  439. innerFinally3;
  440. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  441. expect(() => {
  442. innerFinally4;
  443. }).toThrowWithMessage(ReferenceError, "'innerFinally4' is not defined");
  444. expect(innerFinally5).toBe(13);
  445. executionOrder.push(8);
  446. }
  447. expect(() => {
  448. foo;
  449. }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  450. expect(() => {
  451. j;
  452. }).toThrowWithMessage(ReferenceError, "'j' is not defined");
  453. expect(() => {
  454. bar;
  455. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  456. expect(() => {
  457. baz;
  458. }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
  459. expect(() => {
  460. i;
  461. }).toThrowWithMessage(ReferenceError, "'i' is not defined");
  462. expect(() => {
  463. serenity;
  464. }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
  465. expect(() => {
  466. whf;
  467. }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
  468. expect(() => {
  469. beforeContinueTry;
  470. }).toThrowWithMessage(ReferenceError, "'beforeContinueTry' is not defined");
  471. expect(() => {
  472. continueTry;
  473. }).toThrowWithMessage(ReferenceError, "'continueTry' is not defined");
  474. expect(() => {
  475. innerFinally1;
  476. }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
  477. expect(() => {
  478. innerFinally2;
  479. }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
  480. expect(() => {
  481. innerFinally3;
  482. }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
  483. expect(() => {
  484. innerFinally4;
  485. }).toThrowWithMessage(ReferenceError, "'innerFinally4' is not defined");
  486. expect(() => {
  487. innerFinally5;
  488. }).toThrowWithMessage(ReferenceError, "'innerFinally5' is not defined");
  489. expect(executionOrder).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
  490. });
  491. test("labelled continue in finally overrides labelled continue in try", () => {
  492. const executionOrder = [];
  493. function callFromUpdateBlock(i) {
  494. expect(i).toBe(2);
  495. executionOrder.push(3);
  496. }
  497. function callFromTestBlock(i) {
  498. expect(i).toBe(2);
  499. executionOrder.push(4);
  500. return false;
  501. }
  502. outer: for (let i = 1; i >= 2 ? callFromTestBlock(i) : true; ++i, callFromUpdateBlock(i)) {
  503. inner: for (const j = 2; ; expect().fail("Jumped to inner for loop update block")) {
  504. try {
  505. executionOrder.push(1);
  506. continue inner;
  507. } finally {
  508. executionOrder.push(2);
  509. continue outer;
  510. }
  511. expect().fail("Running code after try block");
  512. }
  513. expect().fail("Running code after inner for loop");
  514. }
  515. expect(executionOrder).toEqual([1, 2, 3, 4]);
  516. });