summaryrefslogtreecommitdiff
path: root/src/utils/anonymous.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/anonymous.test.ts')
-rw-r--r--src/utils/anonymous.test.ts130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/utils/anonymous.test.ts b/src/utils/anonymous.test.ts
new file mode 100644
index 0000000..2da613f
--- /dev/null
+++ b/src/utils/anonymous.test.ts
@@ -0,0 +1,130 @@
+import { assert, assertEquals, assertFalse } from "@std/assert";
+import { describe, it } from "@std/testing/bdd";
+import {
+ defined,
+ equal,
+ extremeBy,
+ get,
+ getCall,
+ identity,
+ instanciate,
+ pass,
+} from "./anonymous.ts";
+import { assertSpyCalls, spy } from "@std/testing/mock";
+import { FALSE, TRUE } from "../../tests/fixtures/test_data.ts";
+
+describe("identity", () => {
+ it("returns the same value", () => {
+ assertEquals(identity(42), 42);
+ assertEquals(identity("hello"), "hello");
+ const obj = { a: 1 };
+ assertEquals(identity(obj), obj);
+ });
+});
+
+describe("defined", () => {
+ it("returns true for non-null/undefined values", () => {
+ assert(defined(0));
+ assert(defined(""));
+ const FALSE = false;
+ assert(defined(FALSE));
+ });
+
+ it("returns false for null and undefined", () => {
+ assertFalse(defined(undefined));
+ assertFalse(defined(null));
+ });
+});
+
+describe("instanciate", () => {
+ class MyClass {
+ constructor(public value: number) {}
+ }
+
+ it("creates a new instance with the given argument", () => {
+ const create = instanciate(MyClass);
+ const instance = create(10);
+ assert(instance instanceof MyClass);
+ assertEquals(instance.value, 10);
+ });
+});
+
+describe("get", () => {
+ it("returns the value at the specified key", () => {
+ const obj = { a: 123, b: "hello" };
+ const getA = get("a");
+ const getB = get("b");
+
+ assertEquals(getA(obj), 123);
+ assertEquals(getB(obj), "hello");
+ });
+});
+
+describe("getCall", () => {
+ it("returns the return value at the specified key", () => {
+ const obj = { a: () => "a", b: (c: unknown) => c };
+ const getA = getCall("a");
+ const getB = getCall("b", "d");
+
+ assertEquals(getA(obj), "a");
+ assertEquals(getB(obj), "d");
+ });
+});
+
+describe("pass", () => {
+ it("calls the given function and returns the input", () => {
+ let a: number | null = null;
+ const f = spy((x: number) => a = x);
+
+ const result = pass(f)(5);
+ assertSpyCalls(f, 1);
+ assertEquals(f.calls[0].args[0], 5);
+ assertEquals(result, 5);
+ assertEquals(a, 5);
+ });
+});
+
+describe("equal", () => {
+ it("returns true when primitive values are strictly equal", () => {
+ const isFive = equal(5);
+ assert(isFive(5));
+ assertFalse(isFive(6));
+
+ const isHello = equal("hello");
+ assert(isHello("hello"));
+ assertFalse(isHello("world"));
+ });
+
+ it("returns true only for same object reference", () => {
+ const obj = { a: 1 };
+ const isObj = equal(obj);
+ assert(isObj(obj));
+ assertFalse(isObj({ a: 1 }));
+ });
+
+ it("handles boolean values correctly", () => {
+ const isTrue = equal(TRUE);
+ assert(isTrue(TRUE));
+ assertFalse(isTrue(FALSE));
+ });
+});
+
+describe("extremeBy", () => {
+ it("returns the maximum value from projected numbers", () => {
+ const data = [1, 3, 2];
+ const result = extremeBy(data, "max");
+ assertEquals(result, 3);
+ });
+
+ it("returns the minimum value from projected numbers", () => {
+ const data = [10, 4, 7];
+ const result = extremeBy(data, "min");
+ assertEquals(result, 4);
+ });
+
+ it("returns -Infinity/Infinity for empty array", () => {
+ const data: number[] = [];
+ assertEquals(extremeBy(data, "max"), -Infinity);
+ assertEquals(extremeBy(data, "min"), Infinity);
+ });
+});