// Generated by ReScript, PLEASE EDIT WITH CARE import * as Mocha from "mocha"; import * as Test_utils from "./test_utils.mjs"; import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.mjs"; let Boom = /* @__PURE__ */Primitive_exceptions.create("Try_finally_test.Boom"); Mocha.describe("Try_finally_test", () => { Mocha.test("finally runs after the body", () => { let log = []; let result; try { log.push("body"); result = 1; } finally { log.push("finally"); } Test_utils.eq("File \"try_finally_test.res\", line 18, characters 7-14", [ "body", "finally" ], log); Test_utils.eq("File \"try_finally_test.res\", line 19, characters 7-14", 1, result); }); Mocha.test("finally runs when the body throws", () => { let log = []; let run = () => { try { log.push("body"); throw { RE_EXN_ID: Boom, _1: 1, Error: new Error() }; } finally { log.push("finally"); } }; let caught; try { caught = run(); } catch (raw_n) { let n = Primitive_exceptions.internalToException(raw_n); if (n.RE_EXN_ID === Boom) { caught = n._1; } else { throw n; } } Test_utils.eq("File \"try_finally_test.res\", line 34, characters 7-14", [ "body", "finally" ], log); Test_utils.eq("File \"try_finally_test.res\", line 35, characters 7-14", 1, caught); }); Mocha.test("catch runs before finally", () => { let log = []; let result; try { log.push("body"); throw { RE_EXN_ID: Boom, _1: 2, Error: new Error() }; } catch (raw_n) { let n = Primitive_exceptions.internalToException(raw_n); if (n.RE_EXN_ID === Boom) { log.push("catch"); result = n._1; } else { throw n; } } finally { log.push("finally"); } Test_utils.eq("File \"try_finally_test.res\", line 50, characters 7-14", [ "body", "catch", "finally" ], log); Test_utils.eq("File \"try_finally_test.res\", line 51, characters 7-14", 2, result); }); Mocha.test("finally runs when the catch handler throws", () => { let log = []; let run = () => { try { throw { RE_EXN_ID: Boom, _1: 3, Error: new Error() }; } catch (raw_n) { let n = Primitive_exceptions.internalToException(raw_n); if (n.RE_EXN_ID === Boom) { throw { RE_EXN_ID: Boom, _1: n._1 + 1 | 0, Error: new Error() }; } throw n; } finally { log.push("finally"); } }; let caught; try { caught = run(); } catch (raw_n) { let n = Primitive_exceptions.internalToException(raw_n); if (n.RE_EXN_ID === Boom) { caught = n._1; } else { throw n; } } Test_utils.eq("File \"try_finally_test.res\", line 67, characters 7-14", ["finally"], log); Test_utils.eq("File \"try_finally_test.res\", line 68, characters 7-14", 4, caught); }); Mocha.test("an exception the catch does not match passes through finally", () => { let log = []; let run = () => { try { throw { RE_EXN_ID: Boom, _1: 5, Error: new Error() }; } catch (raw_exn) { let exn = Primitive_exceptions.internalToException(raw_exn); if (exn.RE_EXN_ID === "Not_found") { return 0; } throw exn; } finally { log.push("finally"); } }; let caught; try { caught = run(); } catch (raw_n) { let n = Primitive_exceptions.internalToException(raw_n); if (n.RE_EXN_ID === Boom) { caught = n._1; } else { throw n; } } Test_utils.eq("File \"try_finally_test.res\", line 84, characters 7-14", ["finally"], log); Test_utils.eq("File \"try_finally_test.res\", line 85, characters 7-14", 5, caught); }); Mocha.test("finally runs on break and continue", () => { let log = []; for (let i = 0; i <= 5; ++i) { try { if (i === 1) { continue; } if (i === 3) { break; } log.push(i.toString()); } finally { log.push("f" + i.toString()); } } Test_utils.eq("File \"try_finally_test.res\", line 103, characters 7-14", [ "0", "f0", "f1", "2", "f2", "f3" ], log); }); Mocha.test("finally runs on break from a switch inside the try", () => { let log = []; let i = 0; loop_0: while (i < 5) { i = i + 1 | 0; try { let n = i; switch (n) { case 2 : continue loop_0; case 4 : break loop_0; default: log.push(n.toString()); } } finally { log.push("f" + i.toString()); } }; Test_utils.eq("File \"try_finally_test.res\", line 121, characters 7-14", [ "1", "f1", "f2", "3", "f3", "f4" ], log); }); Mocha.test("loops inside finally can use break", () => { let log = []; try { log.push("body"); } finally { for (let i = 0; i <= 5; ++i) { if (i === 2) { break; } log.push(i.toString()); } } Test_utils.eq("File \"try_finally_test.res\", line 136, characters 7-14", [ "body", "0", "1" ], log); }); Mocha.test("nested finally blocks run innermost first", () => { let log = []; let caught; try { try { try { throw { RE_EXN_ID: Boom, _1: 6, Error: new Error() }; } finally { log.push("inner"); } } finally { log.push("outer"); } } catch (raw_n) { let n = Primitive_exceptions.internalToException(raw_n); if (n.RE_EXN_ID === Boom) { caught = n._1; } else { throw n; } } Test_utils.eq("File \"try_finally_test.res\", line 154, characters 7-14", [ "inner", "outer" ], log); Test_utils.eq("File \"try_finally_test.res\", line 155, characters 7-14", 6, caught); }); Mocha.test("recursion under finally keeps call order", () => { let log = []; let count = (n, retried) => { try { if (n > 0) { return count(n - 1 | 0, retried); } if (retried) { return 0; } throw { RE_EXN_ID: Boom, _1: n, Error: new Error() }; } catch (raw_exn) { let exn = Primitive_exceptions.internalToException(raw_exn); if (exn.RE_EXN_ID === Boom) { return count(n + 2 | 0, true); } throw exn; } finally { log.push(n); } }; Test_utils.eq("File \"try_finally_test.res\", line 176, characters 7-14", 0, count(2, false)); Test_utils.eq("File \"try_finally_test.res\", line 177, characters 7-14", [ 0, 1, 2, 0, 1, 2 ], log); }); Mocha.test("finally runs after an awaited body", async () => { let log = []; let run = async () => { try { let value = await Promise.resolve(7); log.push("body"); return value; } finally { log.push("finally"); } }; let result = await run(); Test_utils.eq("File \"try_finally_test.res\", line 191, characters 7-14", [ "body", "finally" ], log); return Test_utils.eq("File \"try_finally_test.res\", line 192, characters 7-14", 7, result); }); Mocha.test("finally runs after a rejected await", async () => { let log = []; let run = async () => { try { return await Promise.reject({ RE_EXN_ID: Boom, _1: 8 }); } finally { log.push("finally"); } }; let caught; try { caught = await run(); } catch (raw_n) { let n = Primitive_exceptions.internalToException(raw_n); if (n.RE_EXN_ID === Boom) { caught = n._1; } else { throw n; } } Test_utils.eq("File \"try_finally_test.res\", line 206, characters 7-14", ["finally"], log); return Test_utils.eq("File \"try_finally_test.res\", line 207, characters 7-14", 8, caught); }); }); export { Boom, } /* Not a pure module */