-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcurator_test.ts
More file actions
128 lines (123 loc) · 4.24 KB
/
curator_test.ts
File metadata and controls
128 lines (123 loc) · 4.24 KB
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
import { assertEquals } from "@std/assert";
import { DenopsStub } from "@denops/test/stub";
import { assertType, type IsExact } from "@std/testing/types";
import type { Detail } from "./item.ts";
import { composeCurators, type Curator, defineCurator } from "./curator.ts";
Deno.test("defineCurator", async (t) => {
await t.step("without type contraint", async () => {
const curator = defineCurator(async function* () {
yield { id: 1, value: "1", detail: { a: "" } };
yield { id: 2, value: "2", detail: { a: "" } };
yield { id: 3, value: "3", detail: { a: "" } };
});
assertType<IsExact<typeof curator, Curator<{ a: string }>>>(true);
const denops = new DenopsStub();
const params = {
args: [],
query: "",
};
const items = await Array.fromAsync(curator.curate(denops, params, {}));
assertEquals(items, [
{ id: 1, value: "1", detail: { a: "" } },
{ id: 2, value: "2", detail: { a: "" } },
{ id: 3, value: "3", detail: { a: "" } },
]);
});
await t.step("with type contraint", async () => {
type C = { a: string };
// @ts-expect-error: 'detail' does not follow the type constraint
defineCurator<C>(async function* () {
yield { id: 1, value: "1", detail: "invalid detail" };
});
const curator = defineCurator<C>(async function* () {
yield { id: 1, value: "1", detail: { a: "" } };
yield { id: 2, value: "2", detail: { a: "" } };
yield { id: 3, value: "3", detail: { a: "" } };
});
assertType<IsExact<typeof curator, Curator<C>>>(true);
const denops = new DenopsStub();
const params = {
args: [],
query: "",
};
const items = await Array.fromAsync(curator.curate(denops, params, {}));
assertEquals(items, [
{ id: 1, value: "1", detail: { a: "" } },
{ id: 2, value: "2", detail: { a: "" } },
{ id: 3, value: "3", detail: { a: "" } },
]);
});
});
Deno.test("composeCurators", async (t) => {
await t.step("with bear curators", async (t) => {
await t.step("curators are applied in order", async () => {
const results: string[] = [];
const curator1 = defineCurator(async function* () {
results.push("curator1");
yield* Array.from({ length: 3 }).map((_, id) => ({
id,
value: `A-${id}`,
detail: {
a: id,
},
}));
});
const curator2 = defineCurator(async function* () {
results.push("curator2");
yield* Array.from({ length: 3 }).map((_, id) => ({
id,
value: `B-${id}`,
detail: {
b: id,
},
}));
});
const curator3 = defineCurator(async function* () {
results.push("curator3");
yield* Array.from({ length: 3 }).map((_, id) => ({
id,
value: `C-${id}`,
detail: {
c: id,
},
}));
});
const curator = composeCurators(curator2, curator1, curator3);
const denops = new DenopsStub();
const params = {
args: [],
query: "",
};
const items = await Array.fromAsync(curator.curate(denops, params, {}));
assertEquals(results, ["curator2", "curator1", "curator3"]);
assertEquals(items.map((v) => v.value), [
"B-0",
"B-1",
"B-2",
"A-0",
"A-1",
"A-2",
"C-0",
"C-1",
"C-2",
]);
});
await t.step("without type constraint", () => {
const curator1 = defineCurator(async function* () {});
const curator2 = defineCurator(async function* () {});
const curator3 = defineCurator(async function* () {});
const curator = composeCurators(curator2, curator1, curator3);
assertType<IsExact<typeof curator, Curator<Detail>>>(true);
});
await t.step("with type constraint", () => {
type C1 = { a: string };
type C2 = { b: string };
type C3 = { c: string };
const curator1 = defineCurator<C1>(async function* () {});
const curator2 = defineCurator<C2>(async function* () {});
const curator3 = defineCurator<C3>(async function* () {});
const curator = composeCurators(curator2, curator1, curator3);
assertType<IsExact<typeof curator, Curator<C1 | C2 | C3>>>(true);
});
});
});