Skip to content

Commit

Permalink
test: Enforce tests for Prism (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Aug 15, 2024
1 parent b061fd0 commit 1ea254c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/optical/prism.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { assertEquals } from "../../deps.ts";
import { opticCat } from "../optical.ts";
import { ifSome, none, type Option, some } from "../option.ts";
import { key } from "./lens.ts";
import { only } from "./prism.ts";
import { filter } from "./prism.ts";
import { only, unreachable } from "./prism.ts";

Deno.test("optional", () => {
const obj = {
Expand All @@ -26,7 +27,32 @@ Deno.test("optional", () => {
);
});

Deno.test("unreachable", () => {
assertEquals(opticCat(4).feed(unreachable()).get(), none());
});

Deno.test("only", () => {
assertEquals(opticCat(4).feed(only(4)).get(), some(4));
assertEquals(opticCat(4).feed(only(5)).get(), none());
assertEquals(opticCat(4).feed(only(4)).set(6), 6);
assertEquals(opticCat(4).feed(only(5)).set(6), 4);
});

Deno.test("filter", () => {
assertEquals(
opticCat(6).feed(filter((x: number) => x % 2 === 0)).get(),
some(6),
);
assertEquals(
opticCat(7).feed(filter((x: number) => x % 2 === 0)).get(),
none(),
);
assertEquals(
opticCat(6).feed(filter((x: number) => x % 2 === 0)).set(8),
8,
);
assertEquals(
opticCat(7).feed(filter((x: number) => x % 2 === 0)).set(8),
7,
);
});
2 changes: 1 addition & 1 deletion src/optical/prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const newPrismSimple =
newPrism(upcast)((s) => okOr(s)(downcast(s)));

/**
* @returns The optic which matches nothing. Getting a value through this will throw an error.
* @returns The optic which matches nothing. Setting a value through this will throw an error (but will be probably a type error).
*/
export const unreachable = <S, A>(): Optic<S, S, A, never> =>
newPrism<never, S>(absurd)<S, A>(err);
Expand Down

0 comments on commit 1ea254c

Please sign in to comment.