1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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);
});
});
|