|
| 1 | +import { assertEquals } from "@std/assert"; |
| 2 | +import { map, str } from "@claudiu-ceia/combine"; |
| 3 | +import { guard, safe } from "../src/guard.ts"; |
| 4 | + |
| 5 | +Deno.test("guard: passes when predicate returns true", () => { |
| 6 | + const p = guard(str("abc"), (v) => v === "abc", "expected abc"); |
| 7 | + const res = p({ text: "abc", index: 0 }); |
| 8 | + assertEquals(res.success, true); |
| 9 | + if (res.success) assertEquals(res.value, "abc"); |
| 10 | +}); |
| 11 | + |
| 12 | +Deno.test("guard: fails when predicate returns false", () => { |
| 13 | + const p = guard(str("abc"), () => false, "rejected"); |
| 14 | + const res = p({ text: "abc", index: 0 }); |
| 15 | + assertEquals(res.success, false); |
| 16 | + if (!res.success) assertEquals(res.expected, "rejected"); |
| 17 | +}); |
| 18 | + |
| 19 | +Deno.test("guard: propagates inner parser failure", () => { |
| 20 | + const p = guard(str("abc"), () => true, "guard"); |
| 21 | + const res = p({ text: "xyz", index: 0 }); |
| 22 | + assertEquals(res.success, false); |
| 23 | +}); |
| 24 | + |
| 25 | +Deno.test("guard: does not consume input on predicate failure", () => { |
| 26 | + const p = guard(str("abc"), () => false, "rejected"); |
| 27 | + const res = p({ text: "abcdef", index: 0 }); |
| 28 | + assertEquals(res.success, false); |
| 29 | + if (!res.success) assertEquals(res.ctx.index, 0); |
| 30 | +}); |
| 31 | + |
| 32 | +Deno.test("safe: passes through successful parse", () => { |
| 33 | + const p = safe(str("ok"), "safe"); |
| 34 | + const res = p({ text: "ok", index: 0 }); |
| 35 | + assertEquals(res.success, true); |
| 36 | + if (res.success) assertEquals(res.value, "ok"); |
| 37 | +}); |
| 38 | + |
| 39 | +Deno.test("safe: passes through normal parse failure", () => { |
| 40 | + const p = safe(str("ok"), "safe"); |
| 41 | + const res = p({ text: "no", index: 0 }); |
| 42 | + assertEquals(res.success, false); |
| 43 | +}); |
| 44 | + |
| 45 | +Deno.test("safe: catches thrown exception and returns failure", () => { |
| 46 | + const throwing = map(str("boom"), () => { |
| 47 | + throw new Error("kaboom"); |
| 48 | + }); |
| 49 | + const p = safe(throwing, "caught"); |
| 50 | + const res = p({ text: "boom", index: 0 }); |
| 51 | + assertEquals(res.success, false); |
| 52 | + if (!res.success) assertEquals(res.expected, "caught"); |
| 53 | +}); |
| 54 | + |
| 55 | +Deno.test("safe: does not consume input when exception is caught", () => { |
| 56 | + const throwing = map(str("boom"), () => { |
| 57 | + throw new Error("kaboom"); |
| 58 | + }); |
| 59 | + const p = safe(throwing, "caught"); |
| 60 | + const res = p({ text: "boomstuff", index: 0 }); |
| 61 | + assertEquals(res.success, false); |
| 62 | + if (!res.success) assertEquals(res.ctx.index, 0); |
| 63 | +}); |
| 64 | + |
| 65 | +Deno.test("safe: catches invalid Date toISOString", () => { |
| 66 | + const dateParser = map(str("bad-date"), () => { |
| 67 | + return new Date("not-a-date").toISOString(); |
| 68 | + }); |
| 69 | + const p = safe(dateParser, "valid date"); |
| 70 | + const res = p({ text: "bad-date", index: 0 }); |
| 71 | + assertEquals(res.success, false); |
| 72 | + if (!res.success) assertEquals(res.expected, "valid date"); |
| 73 | +}); |
0 commit comments