summaryrefslogtreecommitdiff
path: root/src/utils/anonymous.test.ts
blob: 45896bfde18f1a760b8f6c918255c8a4e866d212 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { assert, assertEquals, assertFalse } from "@std/assert";
import { describe, it } from "@std/testing/bdd";
import {
  defined,
  equal,
  extremeBy,
  get,
  getCall,
  identity,
  instanciate,
  pass,
  transform,
} 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);
  });
});

describe("transform", () => {
  it("applies the function to the input", () => {
    const result = transform(5, (x) => x * 2);
    assertEquals(result, 10);
  });

  it("works with strings", () => {
    const result = transform("hello", (x) => x.toUpperCase());
    assertEquals(result, "HELLO");
  });

  it("works with objects", () => {
    const input = { a: 1, b: 2 };
    const result = transform(input, ({ a, b }) => a + b);
    assertEquals(result, 3);
  });

  it("returns the correct type", () => {
    const TRUE = true;
    const FALSE = false;
    const result = transform(TRUE, (x) => !x);
    assertEquals(result, FALSE);
  });

  it("works with identity function", () => {
    const input = [1, 2, 3];
    const result = transform(input, identity);
    assertEquals(result, input);
  });
});