Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Enforce tests for Prism #259

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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