|
1 | 1 | import { assertType, type IsExact } from "@std/testing/types"; |
2 | 2 | import { DenopsStub } from "@denops/test/stub"; |
3 | | -import type { IdItem } from "./item.ts"; |
| 3 | +import type { UnitDetail } from "./item.ts"; |
4 | 4 | import type { Matcher, MatchParams } from "./matcher.ts"; |
5 | 5 |
|
6 | 6 | Deno.test("Matcher", async (t) => { |
7 | 7 | const denops = new DenopsStub(); |
8 | | - const matcher: Matcher<{ a: string }> = { |
9 | | - match: async function* () {}, |
10 | | - }; |
11 | 8 |
|
12 | | - await t.step("passed type is equal to the type restriction", () => { |
13 | | - const items = matcher.match( |
14 | | - denops, |
15 | | - {} as MatchParams<{ a: string }>, |
16 | | - {}, |
17 | | - ); |
18 | | - assertType< |
19 | | - IsExact< |
20 | | - typeof items, |
21 | | - AsyncIterableIterator<IdItem<{ a: string }>> |
22 | | - > |
23 | | - >(true); |
| 9 | + await t.step("without type constraint is equal to UnitDetail", () => { |
| 10 | + assertType<IsExact<Matcher, Matcher<UnitDetail>>>(true); |
24 | 11 | }); |
25 | 12 |
|
26 | | - await t.step("passed type establishes the type restriction", () => { |
27 | | - const items = matcher.match( |
28 | | - denops, |
29 | | - {} as MatchParams<{ a: string; b: string }>, |
30 | | - {}, |
31 | | - ); |
32 | | - assertType< |
33 | | - IsExact< |
34 | | - typeof items, |
35 | | - AsyncIterableIterator<IdItem<{ a: string; b: string }>> |
36 | | - > |
37 | | - >(true); |
| 13 | + await t.step("match follows the type constraint", () => { |
| 14 | + const matcher: Matcher<{ a: string }> = { match: async function* () {} }; |
| 15 | + matcher.match(denops, {} as MatchParams<{ a: string }>, {}); |
| 16 | + matcher.match(denops, {} as MatchParams<{ a: string; b: string }>, {}); |
| 17 | + // @ts-expect-error: 'a' is missing |
| 18 | + matcher.match(denops, {} as MatchParams<{ b: string }>, {}); |
| 19 | + // @ts-expect-error: 'a' is missing |
| 20 | + matcher.match(denops, {} as MatchParams<Detail>, {}); |
| 21 | + // @ts-expect-error: 'a' is missing |
| 22 | + matcher.match(denops, {} as MatchParams<UnitDetail>, {}); |
38 | 23 | }); |
39 | | - |
40 | | - await t.step("passed type does not establish the type restriction", () => { |
41 | | - const items = matcher.match( |
42 | | - denops, |
43 | | - // @ts-expect-error: 'a' is missing |
44 | | - {} as MatchParams<{ b: string }>, |
45 | | - {}, |
46 | | - ); |
47 | | - assertType< |
48 | | - IsExact< |
49 | | - typeof items, |
50 | | - AsyncIterableIterator<IdItem<{ a: string }>> |
51 | | - > |
52 | | - >(true); |
53 | | - }); |
54 | | - |
55 | | - await t.step( |
56 | | - "check if the type constraint correctly triggers the type checking", |
57 | | - () => { |
58 | | - const matcher1: Matcher<{ a: string }> = { |
59 | | - match: async function* () {}, |
60 | | - }; |
61 | | - const matcher2: Matcher<{ b: string }> = { |
62 | | - match: async function* () {}, |
63 | | - }; |
64 | | - const matcher3: Matcher<{ c: string }> = { |
65 | | - match: async function* () {}, |
66 | | - }; |
67 | | - function strictFunction<T extends { a: string }>(_: Matcher<T>) {} |
68 | | - strictFunction(matcher1); |
69 | | - // @ts-expect-error: 'a' is missing |
70 | | - strictFunction(matcher2); |
71 | | - // @ts-expect-error: 'a' is missing |
72 | | - strictFunction(matcher3); |
73 | | - }, |
74 | | - ); |
75 | 24 | }); |
0 commit comments