-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsource_test.ts
127 lines (122 loc) · 4.16 KB
/
source_test.ts
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
import { assertEquals } from "@std/assert";
import { assertType, type IsExact } from "@std/testing/types";
import { DenopsStub } from "@denops/test/stub";
import type { Detail } from "./item.ts";
import { composeSources, defineSource, type Source } from "./source.ts";
Deno.test("defineSource", async (t) => {
await t.step("without type contraint", async () => {
const source = defineSource(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 source, Source<{ a: string }>>>(true);
const denops = new DenopsStub();
const params = {
args: [],
query: "",
};
const items = await Array.fromAsync(source.collect(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
defineSource<C>(async function* () {
yield { id: 1, value: "1", detail: "invalid detail" };
});
const source = defineSource<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 source, Source<C>>>(true);
const denops = new DenopsStub();
const params = {
args: [],
query: "",
};
const items = await Array.fromAsync(source.collect(denops, params, {}));
assertEquals(items, [
{ id: 1, value: "1", detail: { a: "" } },
{ id: 2, value: "2", detail: { a: "" } },
{ id: 3, value: "3", detail: { a: "" } },
]);
});
});
Deno.test("composeSources", async (t) => {
await t.step("with bear sources", async (t) => {
await t.step("sources are applied in order", async () => {
const results: string[] = [];
const source1 = defineSource(async function* () {
results.push("source1");
yield* Array.from({ length: 3 }).map((_, id) => ({
id,
value: `A-${id}`,
detail: {
a: id,
},
}));
});
const source2 = defineSource(async function* () {
results.push("source2");
yield* Array.from({ length: 3 }).map((_, id) => ({
id,
value: `B-${id}`,
detail: {
b: id,
},
}));
});
const source3 = defineSource(async function* () {
results.push("source3");
yield* Array.from({ length: 3 }).map((_, id) => ({
id,
value: `C-${id}`,
detail: {
c: id,
},
}));
});
const source = composeSources(source2, source1, source3);
const denops = new DenopsStub();
const params = {
args: [],
};
const items = await Array.fromAsync(source.collect(denops, params, {}));
assertEquals(results, ["source2", "source1", "source3"]);
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 source1 = defineSource(async function* () {});
const source2 = defineSource(async function* () {});
const source3 = defineSource(async function* () {});
const source = composeSources(source2, source1, source3);
assertType<IsExact<typeof source, Source<Detail>>>(true);
});
await t.step("with type constraint", () => {
type C1 = { a: string };
type C2 = { b: string };
type C3 = { c: string };
const source1 = defineSource<C1>(async function* () {});
const source2 = defineSource<C2>(async function* () {});
const source3 = defineSource<C3>(async function* () {});
const source = composeSources(source2, source1, source3);
assertType<IsExact<typeof source, Source<C1 | C2 | C3>>>(true);
});
});
});